forked from NativeScript/ios
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRuntime.h
More file actions
103 lines (80 loc) · 3.3 KB
/
Runtime.h
File metadata and controls
103 lines (80 loc) · 3.3 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
99
100
101
102
103
#ifndef Runtime_h
#define Runtime_h
#include <atomic>
#include "Caches.h"
#include "Common.h"
#include "MetadataBuilder.h"
#include "ModuleInternal.h"
#include "SpinLock.h"
#include "libplatform/libplatform.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);
void RunScript(const std::string script);
static void Initialize();
static Runtime* GetCurrentRuntime() { return currentRuntime_; }
static Runtime* GetRuntime(v8::Isolate* isolate);
static bool IsWorker() {
if (currentRuntime_ == nullptr) {
return false;
}
return currentRuntime_->IsRuntimeWorker();
}
static std::shared_ptr<v8::Platform> GetPlatform() { return platform_; }
static id GetAppConfigValue(std::string key);
// Convenience accessor for whether to show the JS error display UI.
// Reads the boolean `showErrorDisplay` from the nativescript.config (aka,
// bundled package.json). Defaults to false when not present.
static bool showErrorDisplay();
static bool IsAlive(const v8::Isolate* isolate);
private:
static thread_local Runtime* currentRuntime_;
static std::shared_ptr<v8::Platform> platform_;
static std::vector<v8::Isolate*> isolates_;
static std::atomic<int> nextRuntimeId_;
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);
void DefineDateTimeConfigurationChangeNotificationMethod(
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_;
double startTime;
double realtimeOrigin;
// 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_;
};
} // namespace tns
#endif /* Runtime_h */