-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathV8Engine.h
More file actions
272 lines (195 loc) · 8.25 KB
/
V8Engine.h
File metadata and controls
272 lines (195 loc) · 8.25 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
* Tencent is pleased to support the open source community by making ScriptX available.
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <unordered_map>
#include "../../src/Engine.h"
#include "../../src/Native.h"
#include "../../src/Reference.h"
#include "../../src/Scope.h"
#include "../../src/Value.h"
#include "../../src/utils/GlobalWeakBookkeeping.hpp"
#include "V8Helper.h"
#include "V8Platform.h"
namespace script::v8_backend {
class InspectorClient;
class V8Engine : public ::script::ScriptEngine {
struct ManagedObject {
V8Engine* engine;
void* data;
std::function<void(void*)> cleanupFunc;
};
struct ThreadGlobalScope {
EngineScope scope_;
explicit ThreadGlobalScope(V8Engine* engine)
: scope_(EngineScope::InternalEnterEngine{}, engine, false) {}
};
private:
bool isOwnIsolate_ = true;
// used only for node addon
std::unique_ptr<ThreadGlobalScope> threadGlobalScope_ = nullptr;
std::unordered_map<const void*, v8::Global<v8::FunctionTemplate>> nativeRegistry_;
std::shared_ptr<V8Platform> v8Platform_;
std::unique_ptr<v8::ArrayBuffer::Allocator> allocator_;
std::shared_ptr<::script::utils::MessageQueue> messageQueue_;
// V8 don't do gc on Isolate::Dispose,
// so we must got a way to manage native object.
// key: native pointer
// value: v8 weak
std::unordered_map<ManagedObject*, v8::Global<v8::Value>> managedObject_;
std::unordered_map<size_t, v8::Global<v8::Value>> keptObject_;
size_t keptObjectId_ = 0;
bool isDestroying_ = false;
internal::GlobalWeakBookkeeping globalWeakBookkeeping_;
// create a slave engine
explicit V8Engine(V8Engine* masterEngine);
protected:
v8::Isolate* isolate_;
v8::Global<v8::Context> context_;
v8::Global<v8::Symbol> internalStoreSymbol_;
v8::Global<v8::Symbol> constructorMarkSymbol_;
explicit V8Engine(std::shared_ptr<utils::MessageQueue> messageQueue,
const std::function<v8::Isolate*()>& isolateFactory);
~V8Engine() override;
public:
explicit V8Engine(std::shared_ptr<utils::MessageQueue> messageQueue = {});
/**
* Create a ScriptEngine instance wrapping existing v8 instance, especially for NODE JS addons.
* After creating such instance, there will be a global EngineScope for CURRENT THREAD, until you
* destroy the engine (controlled by the param addGlobalEngineScope, which is true by default).
* Note: when destroying such ScriptEngine instance, the v8 isolate/context
* are not destroyed, only ScriptEngine related resources.
*
* Note: V8 has it's on event-loop, especially for node.js, so you may ignore messageQueue.
* Note: BUT, if you post message to ScriptEngine::messageQueue(), you still need to schedule
* MessageQueue::loop from time to time.
*/
explicit V8Engine(std::shared_ptr<utils::MessageQueue> messageQueue, v8::Isolate* isolate,
v8::Local<v8::Context> context, bool addGlobalEngineScope = true);
void destroy() noexcept override;
bool isDestroying() const override;
Local<Value> get(const Local<String>& key) override;
Local<Object> getGlobal();
void set(const Local<String>& key, const Local<Value>& value) override;
using ScriptEngine::set;
Local<Value> eval(const Local<String>& script, const Local<String>& sourceFile) override;
Local<Value> eval(const Local<String>& script) override;
using ScriptEngine::eval;
Local<Value> loadFile(const Local<String>& scriptFile) override;
/**
* Create a new V8 Engine that share the same isolate, but with different context.
* Caller own the returned pointer, and the returned instance
* should be deleted BEFORE this instance.
*
* and the message queue from a slave engine is the same as its master.
*/
UniqueEnginePtr newSlaveEngine();
std::shared_ptr<::script::utils::MessageQueue> messageQueue() override;
void gc() override;
size_t getHeapSize() override;
void adjustAssociatedMemory(int64_t count) override;
ScriptLanguage getLanguageType() override;
std::string getEngineVersion() override;
private:
void initContext();
Local<Value> eval(const Local<String>& script, const Local<Value>& sourceFile);
template <typename T>
void registerNativeClassImpl(const ClassDefine<T>* classDefine);
template <typename T>
Local<Object> newNativeClassImpl(const ClassDefine<T>* classDefine, size_t argc,
const Local<Value>* args);
template <typename T>
bool isInstanceOfImpl(const Local<Value>& value, const ClassDefine<T>* classDefine);
template <typename T>
T* getNativeInstanceImpl(const Local<Value>& value, const ClassDefine<T>* classDefine);
template <typename T>
v8::Local<v8::FunctionTemplate> newConstructor(const ClassDefine<T>* classDefine);
void registerNativeClassStatic(v8::Local<v8::FunctionTemplate> funcT,
const internal::StaticDefine* staticDefine);
template <typename T>
void registerNativeClassInstance(v8::Local<v8::FunctionTemplate> funcT,
const ClassDefine<T>* classDefine);
// the following function are public only for you to interact with raw v8 APIs.
template <typename T, typename... Args>
static T make(Args&&... args) {
return T(std::forward<Args>(args)...);
}
template <typename T>
static inline typename Local<T>::InternalLocalRef toV8(v8::Isolate* /*isolate*/,
const Local<T>& ref) {
return ref.val_;
}
static typename Local<Value>::InternalLocalRef toV8(v8::Isolate* isolate,
const Local<Value>& ref) {
if (ref.val_.IsEmpty()) {
return v8::Undefined(isolate).As<V8ValueType<Value>>();
}
return ref.val_;
}
static inline Arguments extractV8Arguments(V8Engine* engine,
const v8::FunctionCallbackInfo<v8::Value>& args) {
return Arguments(std::make_pair(engine, args));
}
private:
void addManagedObject(void* nativeObj, v8::Local<v8::Value> obj,
std::function<void(void*)>&& proc);
size_t keepReference(const Local<Value>& ref);
/**
* can be called withoud EngineScope
*/
void removeKeptReference(size_t id);
private:
// WHO is your friend!!!
friend class V8EngineScope;
friend class V8HandleScope;
friend class V8ExitEngineScope;
template <typename T>
friend class ::script::Local;
template <typename T>
friend class ::script::Global;
template <typename T>
friend class ::script::Weak;
friend class ::script::Object;
friend class ::script::ScriptEngine;
// in V8Helper.h
friend class v8_backend::V8HandleScope;
friend v8::Isolate* currentEngineIsolateChecked();
friend v8::Local<v8::Context> currentEngineContextChecked();
friend std::tuple<v8::Isolate*, v8::Local<v8::Context>> currentEngineIsolateAndContextChecked();
friend void checkException(v8::TryCatch& tryCatch);
friend void rethrowException(const Exception& exception);
// in V8Helper.hpp
template <typename Ret, typename Closure>
friend Ret toV8ValueArray(v8::Isolate* isolate, size_t argc, const Local<Value>* args, Closure c);
friend class ::script::Exception;
friend class ::script::Function;
friend class ::script::ByteBuffer;
friend class ::script::Arguments;
friend class ::script::ScriptClass;
friend class ::script::StringHolder;
friend class ::script::ScriptClass;
friend class ExceptionFields;
friend class InspectorClient;
template <typename T>
friend class GlobalRefState;
friend struct V8BookKeepFetcher;
friend struct ::script::v8_interop;
template <typename Ref>
static auto& refVal(Ref* ref) {
return ref->val_;
}
};
} // namespace script::v8_backend