-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5e8214d
commit 863a790
Showing
2 changed files
with
341 additions
and
312 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,91 +1,93 @@ | ||
#ifndef Runtime_h | ||
#define Runtime_h | ||
|
||
#include "libplatform/libplatform.h" | ||
#include "Caches.h" | ||
#include "Common.h" | ||
#include "ModuleInternal.h" | ||
#include "MetadataBuilder.h" | ||
#include "ModuleInternal.h" | ||
#include "SpinLock.h" | ||
#include "Caches.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_; | ||
} | ||
public: | ||
Runtime(); | ||
~Runtime(); | ||
v8::Isolate* CreateIsolate(); | ||
void Init(v8::Isolate* isolate, bool isWorker = false); | ||
void RunMainScript(); | ||
v8::Isolate* GetIsolate(); | ||
|
||
void RunModule(const std::string moduleName); | ||
|
||
void RunScript(const std::string script); | ||
const int WorkerId(); | ||
|
||
static void Initialize(); | ||
void SetWorkerId(int workerId); | ||
inline bool IsRuntimeWorker() { return workerId_ > 0; } | ||
|
||
static Runtime* GetCurrentRuntime() { | ||
return currentRuntime_; | ||
} | ||
|
||
static Runtime* GetRuntime(v8::Isolate* isolate); | ||
inline CFRunLoopRef RuntimeLoop() { return runtimeLoop_; } | ||
|
||
static bool IsWorker() { | ||
if (currentRuntime_ == nullptr) { | ||
return false; | ||
} | ||
void RunModule(const std::string moduleName); | ||
|
||
return currentRuntime_->IsRuntimeWorker(); | ||
} | ||
void RunScript(const std::string script); | ||
|
||
static void Initialize(); | ||
|
||
static Runtime* GetCurrentRuntime() { return currentRuntime_; } | ||
|
||
static Runtime* GetRuntime(v8::Isolate* isolate); | ||
|
||
static std::shared_ptr<v8::Platform> GetPlatform() { | ||
return platform_; | ||
static bool IsWorker() { | ||
if (currentRuntime_ == nullptr) { | ||
return false; | ||
} | ||
|
||
static id GetAppConfigValue(std::string key); | ||
|
||
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 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_; | ||
// 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_; | ||
return currentRuntime_->IsRuntimeWorker(); | ||
} | ||
|
||
static std::shared_ptr<v8::Platform> GetPlatform() { 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<v8::Platform> 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); | ||
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_; | ||
// 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 */ |
Oops, something went wrong.