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