forked from BabylonJS/JsRuntimeHost
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppRuntime_V8.cpp
More file actions
121 lines (98 loc) · 3.2 KB
/
Copy pathAppRuntime_V8.cpp
File metadata and controls
121 lines (98 loc) · 3.2 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "AppRuntime.h"
#include <napi/env.h>
#include <libplatform/libplatform.h>
#ifdef ENABLE_V8_INSPECTOR
#include <V8InspectorAgent.h>
#endif
#include <optional>
namespace Babylon
{
namespace
{
class Module final
{
public:
Module(const char* executablePath)
{
v8::V8::InitializeICUDefaultLocation(executablePath);
v8::V8::InitializeExternalStartupData(executablePath);
m_platform = v8::platform::NewDefaultPlatform();
v8::V8::InitializePlatform(m_platform.get());
v8::V8::Initialize();
}
~Module()
{
v8::V8::Dispose();
v8::V8::DisposePlatform();
}
static void Initialize(const char* executablePath)
{
if (s_module == nullptr)
{
s_module = std::make_unique<Module>(executablePath);
}
}
static Module& Instance()
{
if (!s_module)
{
throw std::runtime_error{"Module not available"};
}
return *s_module;
}
v8::Platform& Platform()
{
return *m_platform;
}
private:
std::unique_ptr<v8::Platform> m_platform;
static std::unique_ptr<Module> s_module;
};
std::unique_ptr<Module> Module::s_module;
}
void AppRuntime::RunEnvironmentTier(const char* executablePath)
{
// Create the isolate.
Module::Initialize(executablePath);
v8::Isolate::CreateParams create_params;
create_params.array_buffer_allocator = v8::ArrayBuffer::Allocator::NewDefaultAllocator();
v8::Isolate* isolate = v8::Isolate::New(create_params);
// Use the isolate within a scope.
{
v8::Isolate::Scope isolate_scope{isolate};
v8::HandleScope isolate_handle_scope{isolate};
v8::Local<v8::Context> context = v8::Context::New(isolate);
v8::Context::Scope context_scope{context};
Napi::Env env = Napi::Attach(context);
#ifdef ENABLE_V8_INSPECTOR
std::optional<V8InspectorAgent> agent;
if (m_options.EnableDebugger)
{
agent.emplace(Module::Instance().Platform(), isolate, context, "JsRuntimeHost");
agent->Start(5643, "JsRuntimeHost");
if (m_options.WaitForDebugger)
{
agent->WaitForDebugger();
}
}
#endif
Run(env);
#ifdef ENABLE_V8_INSPECTOR
if (agent.has_value())
{
agent->Stop();
}
#endif
Napi::Detach(env);
}
// Destroy the isolate.
// todo : GetArrayBufferAllocator not available?
// delete isolate->GetArrayBufferAllocator();
isolate->Dispose();
}
void AppRuntime::DrainMicrotasks(Napi::Env)
{
// V8 auto-drains microtasks at the end of each script/callback when
// using the default MicrotasksPolicy. No explicit pump needed.
}
}