|
1 | 1 | #ifndef Runtime_h
|
2 | 2 | #define Runtime_h
|
3 | 3 |
|
4 |
| -#include "libplatform/libplatform.h" |
| 4 | +#include "Caches.h" |
5 | 5 | #include "Common.h"
|
6 |
| -#include "ModuleInternal.h" |
7 | 6 | #include "MetadataBuilder.h"
|
| 7 | +#include "ModuleInternal.h" |
8 | 8 | #include "SpinLock.h"
|
9 |
| -#include "Caches.h" |
| 9 | +#include "libplatform/libplatform.h" |
10 | 10 |
|
11 | 11 | namespace tns {
|
12 | 12 |
|
13 | 13 | class Runtime {
|
14 |
| -public: |
15 |
| - Runtime(); |
16 |
| - ~Runtime(); |
17 |
| - v8::Isolate* CreateIsolate(); |
18 |
| - void Init(v8::Isolate* isolate, bool isWorker = false); |
19 |
| - void RunMainScript(); |
20 |
| - v8::Isolate* GetIsolate(); |
21 |
| - |
22 |
| - const int WorkerId(); |
23 |
| - |
24 |
| - void SetWorkerId(int workerId); |
25 |
| - inline bool IsRuntimeWorker() { |
26 |
| - return workerId_ > 0; |
27 |
| - } |
28 |
| - |
29 |
| - inline CFRunLoopRef RuntimeLoop() { |
30 |
| - return runtimeLoop_; |
31 |
| - } |
| 14 | + public: |
| 15 | + Runtime(); |
| 16 | + ~Runtime(); |
| 17 | + v8::Isolate* CreateIsolate(); |
| 18 | + void Init(v8::Isolate* isolate, bool isWorker = false); |
| 19 | + void RunMainScript(); |
| 20 | + v8::Isolate* GetIsolate(); |
32 | 21 |
|
33 |
| - void RunModule(const std::string moduleName); |
34 |
| - |
35 |
| - void RunScript(const std::string script); |
| 22 | + const int WorkerId(); |
36 | 23 |
|
37 |
| - static void Initialize(); |
| 24 | + void SetWorkerId(int workerId); |
| 25 | + inline bool IsRuntimeWorker() { return workerId_ > 0; } |
38 | 26 |
|
39 |
| - static Runtime* GetCurrentRuntime() { |
40 |
| - return currentRuntime_; |
41 |
| - } |
42 |
| - |
43 |
| - static Runtime* GetRuntime(v8::Isolate* isolate); |
| 27 | + inline CFRunLoopRef RuntimeLoop() { return runtimeLoop_; } |
44 | 28 |
|
45 |
| - static bool IsWorker() { |
46 |
| - if (currentRuntime_ == nullptr) { |
47 |
| - return false; |
48 |
| - } |
| 29 | + void RunModule(const std::string moduleName); |
49 | 30 |
|
50 |
| - return currentRuntime_->IsRuntimeWorker(); |
51 |
| - } |
| 31 | + void RunScript(const std::string script); |
| 32 | + |
| 33 | + static void Initialize(); |
| 34 | + |
| 35 | + static Runtime* GetCurrentRuntime() { return currentRuntime_; } |
| 36 | + |
| 37 | + static Runtime* GetRuntime(v8::Isolate* isolate); |
52 | 38 |
|
53 |
| - static std::shared_ptr<v8::Platform> GetPlatform() { |
54 |
| - return platform_; |
| 39 | + static bool IsWorker() { |
| 40 | + if (currentRuntime_ == nullptr) { |
| 41 | + return false; |
55 | 42 | }
|
56 | 43 |
|
57 |
| - static id GetAppConfigValue(std::string key); |
58 |
| - |
59 |
| - static bool IsAlive(const v8::Isolate* isolate); |
60 |
| -private: |
61 |
| - static thread_local Runtime* currentRuntime_; |
62 |
| - static std::shared_ptr<v8::Platform> platform_; |
63 |
| - static std::vector<v8::Isolate*> isolates_; |
64 |
| - static SpinMutex isolatesMutex_; |
65 |
| - static bool v8Initialized_; |
66 |
| - static std::atomic<int> nextIsolateId; |
67 |
| - |
68 |
| - void DefineGlobalObject(v8::Local<v8::Context> context, bool isWorker); |
69 |
| - void DefineCollectFunction(v8::Local<v8::Context> context); |
70 |
| - void DefineNativeScriptVersion(v8::Isolate* isolate, v8::Local<v8::ObjectTemplate> globalTemplate); |
71 |
| - void DefinePerformanceObject(v8::Isolate* isolate, v8::Local<v8::ObjectTemplate> globalTemplate); |
72 |
| - void DefineTimeMethod(v8::Isolate* isolate, v8::Local<v8::ObjectTemplate> globalTemplate); |
73 |
| - void DefineDrainMicrotaskMethod(v8::Isolate* isolate, v8::Local<v8::ObjectTemplate> globalTemplate); |
74 |
| - void DefineDateTimeConfigurationChangeNotificationMethod(v8::Isolate* isolate, v8::Local<v8::ObjectTemplate> globalTemplate); |
75 |
| - |
76 |
| - static void PerformanceNowCallback(const v8::FunctionCallbackInfo<v8::Value>& args); |
77 |
| - v8::Isolate* isolate_; |
78 |
| - std::unique_ptr<ModuleInternal> moduleInternal_; |
79 |
| - int workerId_; |
80 |
| - CFRunLoopRef runtimeLoop_; |
81 |
| - // TODO: refactor this. This is only needed because, during program termination (UIApplicationMain not called) |
82 |
| - // the Cache::Workers is released (static initialization order fiasco https://en.cppreference.com/w/cpp/language/siof) |
83 |
| - // so it released the Cache::Workers shared_ptr and then releases the Runtime unique_ptr |
84 |
| - // eventually we just need to refactor so that Runtime::Initialize is responsible for its initalization |
85 |
| - // and lifecycle |
86 |
| - std::shared_ptr<ConcurrentMap<int, std::shared_ptr<Caches::WorkerState>>> workerCache_; |
| 44 | + return currentRuntime_->IsRuntimeWorker(); |
| 45 | + } |
| 46 | + |
| 47 | + static std::shared_ptr<v8::Platform> GetPlatform() { return platform_; } |
| 48 | + |
| 49 | + static id GetAppConfigValue(std::string key); |
| 50 | + |
| 51 | + static bool IsAlive(const v8::Isolate* isolate); |
| 52 | + |
| 53 | + private: |
| 54 | + static thread_local Runtime* currentRuntime_; |
| 55 | + static std::shared_ptr<v8::Platform> platform_; |
| 56 | + static std::vector<v8::Isolate*> isolates_; |
| 57 | + static SpinMutex isolatesMutex_; |
| 58 | + static bool v8Initialized_; |
| 59 | + static std::atomic<int> nextIsolateId; |
| 60 | + |
| 61 | + void DefineGlobalObject(v8::Local<v8::Context> context, bool isWorker); |
| 62 | + void DefineCollectFunction(v8::Local<v8::Context> context); |
| 63 | + void DefineNativeScriptVersion(v8::Isolate* isolate, |
| 64 | + v8::Local<v8::ObjectTemplate> globalTemplate); |
| 65 | + void DefinePerformanceObject(v8::Isolate* isolate, |
| 66 | + v8::Local<v8::ObjectTemplate> globalTemplate); |
| 67 | + void DefineTimeMethod(v8::Isolate* isolate, |
| 68 | + v8::Local<v8::ObjectTemplate> globalTemplate); |
| 69 | + void DefineDrainMicrotaskMethod(v8::Isolate* isolate, |
| 70 | + v8::Local<v8::ObjectTemplate> globalTemplate); |
| 71 | + void DefineDateTimeConfigurationChangeNotificationMethod( |
| 72 | + v8::Isolate* isolate, v8::Local<v8::ObjectTemplate> globalTemplate); |
| 73 | + |
| 74 | + static void PerformanceNowCallback( |
| 75 | + const v8::FunctionCallbackInfo<v8::Value>& args); |
| 76 | + v8::Isolate* isolate_; |
| 77 | + std::unique_ptr<ModuleInternal> moduleInternal_; |
| 78 | + int workerId_; |
| 79 | + CFRunLoopRef runtimeLoop_; |
| 80 | + // TODO: refactor this. This is only needed because, during program |
| 81 | + // termination (UIApplicationMain not called) the Cache::Workers is released |
| 82 | + // (static initialization order fiasco |
| 83 | + // https://en.cppreference.com/w/cpp/language/siof) so it released the |
| 84 | + // Cache::Workers shared_ptr and then releases the Runtime unique_ptr |
| 85 | + // eventually we just need to refactor so that Runtime::Initialize is |
| 86 | + // responsible for its initalization and lifecycle |
| 87 | + std::shared_ptr<ConcurrentMap<int, std::shared_ptr<Caches::WorkerState>>> |
| 88 | + workerCache_; |
87 | 89 | };
|
88 | 90 |
|
89 |
| -} |
| 91 | +} // namespace tns |
90 | 92 |
|
91 | 93 | #endif /* Runtime_h */
|
0 commit comments