-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathJscEngine.h
More file actions
233 lines (160 loc) · 6.85 KB
/
JscEngine.h
File metadata and controls
233 lines (160 loc) · 6.85 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
/*
* 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 <JavaScriptCore/JavaScript.h>
#include <unordered_map>
#include "../../src/Engine.h"
#include "../../src/Native.h"
#include "../../src/utils/GlobalWeakBookkeeping.hpp"
#include "JscHelper.h"
namespace script::internal {
// forward declare
// defined in JscEngine.h
template <typename T, typename Args>
struct MakeLocalHelper;
} // namespace script::internal
namespace script::jsc_backend {
class JscEngine : public ::script::ScriptEngine {
struct ClassRegistryData {
JSClassRef instanceClass{};
Global<Object> constructor{};
Global<Object> prototype{};
};
std::shared_ptr<utils::MessageQueue> messageQueue_;
std::unordered_map<const void*, ClassRegistryData> classRegistry_;
std::unordered_map<size_t, Global<Value>> keptReference_;
internal::GlobalWeakBookkeeping globalWeakBookkeeping_;
size_t keepId_ = 0;
bool isDestroying_ = false;
static JSClassRef globalClass_;
static JSClassRef externalClass_;
// >= ios10, >= macOS 10.12
static bool hasByteBufferAPI_;
protected:
std::shared_ptr<std::recursive_mutex> virtualMachineLock_ =
std::make_shared<std::recursive_mutex>();
JSContextGroupRef virtualMachine_ = nullptr;
JSGlobalContextRef context_ = nullptr;
Global<Value> internalStorageSymbol_;
Global<Value> constructorMarkSymbol_;
Global<Function> getInternalStorageBySymbolFunction_;
Global<Function> isByteBuffer_;
private:
static void checkException(JSValueRef exception);
public:
explicit JscEngine(std::shared_ptr<utils::MessageQueue> messageQueue = {});
void destroy() noexcept override;
bool isDestroying() const override { return isDestroying_; }
Local<Object> getGlobal();
Local<Value> get(const Local<String>& key) override;
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;
std::shared_ptr<utils::MessageQueue> messageQueue() override;
void gc() override;
void adjustAssociatedMemory(int64_t count) override;
ScriptLanguage getLanguageType() override;
std::string getEngineVersion() override;
protected:
~JscEngine() override;
private:
Local<Value> eval(const Local<String>& script, const Local<Value>& sourceFile);
template <typename T>
bool registerNativeClassImpl(const ClassDefine<T>* classDefine);
template <typename T>
Local<Object> newNativeClassImpl(const ClassDefine<T>* classDefine, size_t size,
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);
private:
template <typename T>
static inline typename RefTypeMap<T>::jscType toJsc(JSGlobalContextRef /*context*/,
const Local<T>& ref) {
return ref.val_;
}
static inline typename RefTypeMap<String>::jscType toJsc(JSGlobalContextRef context,
const Local<String>& ref) {
return ref.val_.getValue(context);
}
static typename RefTypeMap<Value>::jscType toJsc(JSGlobalContextRef context,
const Local<Value>& ref);
static Arguments newArguments(JscEngine* engine, JSObjectRef thisObject,
const JSValueRef* arguments, size_t size);
template <typename T, typename... Args>
static T make(Args&&... args) {
return T(std::forward<Args>(args)...);
}
Local<Function> newStaticFunction(const internal::StaticDefine::FunctionDefine& func);
Local<Value> newStaticGetter(const internal::StaticDefine::PropertyDefine& prop);
Local<Value> newStaticSetter(const internal::StaticDefine::PropertyDefine& prop);
void registerStaticDefine(const internal::StaticDefine& staticDefine,
const Local<Object>& object);
template <typename T>
void defineInstance(const ClassDefine<T>* classDefine, Local<Value>& object,
ClassRegistryData& registry);
template <typename T>
JSObjectCallAsConstructorCallback createConstructor();
template <typename T>
Local<Object> defineInstancePrototype(const ClassDefine<T>* classDefine);
template <typename T>
void defineInstanceFunction(const ClassDefine<T>* classDefine, Local<Object>& prototypeObject);
template <typename T, typename ConsumeLambda>
void defineInstanceProperties(const ClassDefine<T>* classDefine, ConsumeLambda consumerLambda);
size_t keepReference(const Local<Value>& ref);
void removeKeptReference(size_t id);
void initInternalSymbols();
bool isConstructorMarkSymbol(JSValueRef value);
private:
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::Array;
friend class ::script::Function;
friend class ::script::ByteBuffer;
friend class ::script::ScriptEngine;
friend class ::script::Exception;
friend class ::script::Arguments;
friend class ::script::ScriptClass;
friend class JscStringRefHolder;
friend class JscWeakRef;
friend JSGlobalContextRef currentEngineContextChecked();
friend JSContextGroupRef currentEngineContextGroupChecked();
friend class JscEngineScope;
friend class JscExitEngineScope;
friend class StringLocalRef;
friend struct JscBookKeepFetcher;
template <typename R, typename Fn>
friend R toJscValues(JSGlobalContextRef context, size_t length, const Local<Value>* args, Fn fn);
friend JSObjectRef valueToObj(JSGlobalContextRef context, JSValueRef value);
friend struct ::script::jsc_interop;
template <typename T, typename Args>
friend struct ::script::internal::MakeLocalHelper;
template <typename Ref>
static auto& refVal(Ref* ref) {
return ref->val_;
}
};
} // namespace script::jsc_backend