forked from microsoft/v8-jsi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathV8Runtime_impl.h
498 lines (384 loc) · 19.2 KB
/
V8Runtime_impl.h
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
// Copyright (c) Facebook, Inc. and its affiliates.
//
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.
#pragma once
#include "V8Runtime.h"
#include "v8.h"
#include "libplatform/libplatform.h"
#include "V8Platform.h"
#include <cstdlib>
#include <iostream>
#include <mutex>
#include <atomic>
#include <list>
#include <sstream>
#include <cstdlib>
#if defined(_MSC_VER)
#define CDECL __cdecl
#else
#define CDECL
#endif
#define _ISOLATE_CONTEXT_ENTER v8::Isolate *isolate = v8::Isolate::GetCurrent(); \
v8::Isolate::Scope isolate_scope(isolate); \
v8::HandleScope handle_scope(isolate); \
v8::Context::Scope context_scope(context_.Get(isolate));
namespace facebook { namespace v8runtime {
enum class CacheType {
NoCache,
CodeCache,
FullCodeCache
};
class V8Runtime : public jsi::Runtime {
public:
V8Runtime();
V8Runtime(const std::string& cacheDir, const std::shared_ptr<Logger>& logger);
/*
V8Runtime(const v8::Platform* platform, std::shared_ptr<Logger>&& logger,
std::shared_ptr<facebook::react::MessageQueueThread>&& jsQueue, std::shared_ptr<CacheProvider>&& cacheProvider,
std::unique_ptr<InspectorInterface> inspector, std::unique_ptr<const jsi::Buffer> default_snapshot_blob,
std::unique_ptr<const jsi::Buffer> default_natives_blob, std::unique_ptr<const jsi::Buffer> custom_snapshot); */
~V8Runtime();
jsi::Value evaluateJavaScript(const std::shared_ptr<const jsi::Buffer>& buffer, const std::string& sourceURL) override;
bool drainMicrotasks(int maxMicrotasksHint) override;
jsi::Object global() override;
std::string description() override;
bool isInspectable() override;
private:
struct IHostProxy {
virtual void destroy() = 0;
};
class HostObjectLifetimeTracker {
public:
void ResetHostObject(bool isGC /*whether the call is coming from GC*/) {
assert(!isGC || !isReset_);
if (!isReset_) {
isReset_ = true;
hostProxy_->destroy();
objectTracker_.Reset();
}
}
HostObjectLifetimeTracker(V8Runtime& runtime, v8::Local<v8::Object> obj, IHostProxy* hostProxy) : hostProxy_(hostProxy) {
objectTracker_.Reset(runtime.GetIsolate(), obj);
objectTracker_.SetWeak(this, HostObjectLifetimeTracker::Destroyed, v8::WeakCallbackType::kParameter);
}
// Useful for debugging.
~HostObjectLifetimeTracker() {
assert(isReset_);
std::cout << "~HostObjectLifetimeTracker" << std::endl;
}
private:
v8::Global<v8::Object> objectTracker_;
std::atomic<bool> isReset_{ false };
IHostProxy* hostProxy_;
static void CDECL Destroyed(const v8::WeakCallbackInfo<HostObjectLifetimeTracker>& data) {
v8::HandleScope handle_scope(v8::Isolate::GetCurrent());
data.GetParameter()->ResetHostObject(true /*isGC*/);
}
};
class HostObjectProxy : public IHostProxy {
public:
static void Get(v8::Local<v8::Name> v8PropName, const v8::PropertyCallbackInfo<v8::Value>& info)
{
v8::Local<v8::External> data = v8::Local<v8::External>::Cast(info.This()->GetInternalField(0));
HostObjectProxy* hostObjectProxy = reinterpret_cast<HostObjectProxy*>(data->Value());
if (hostObjectProxy == nullptr)
std::abort();
V8Runtime& runtime = hostObjectProxy->runtime_;
std::shared_ptr<jsi::HostObject> hostObject = hostObjectProxy->hostObject_;
v8::Local<v8::String> propNameStr = v8::Local<v8::String>::Cast(v8PropName);
std::string propName;
propName.resize(propNameStr->Utf8Length(info.GetIsolate()));
propNameStr->WriteUtf8(info.GetIsolate(), &propName[0]);
jsi::PropNameID propNameId = runtime.createPropNameIDFromUtf8(reinterpret_cast<uint8_t*>(&propName[0]), propName.length());
info.GetReturnValue().Set(runtime.valueRef(hostObject->get(runtime, propNameId)));
}
static void Set(v8::Local<v8::Name> v8PropName, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value>& info)
{
v8::Local<v8::External> data = v8::Local<v8::External>::Cast(info.This()->GetInternalField(0));
HostObjectProxy* hostObjectProxy = reinterpret_cast<HostObjectProxy*>(data->Value());
if (hostObjectProxy == nullptr)
std::abort();
V8Runtime& runtime = hostObjectProxy->runtime_;
std::shared_ptr<jsi::HostObject> hostObject = hostObjectProxy->hostObject_;
v8::Local<v8::String> propNameStr = v8::Local<v8::String>::Cast(v8PropName);
std::string propName;
propName.resize(propNameStr->Utf8Length(info.GetIsolate()));
propNameStr->WriteUtf8(info.GetIsolate(), &propName[0]);
hostObject->set(runtime, runtime.createPropNameIDFromUtf8(reinterpret_cast<uint8_t*>(&propName[0]), propName.length()), runtime.createValue(value));
}
static void Enumerator(const v8::PropertyCallbackInfo<v8::Array>& info)
{
v8::Local<v8::External> data = v8::Local<v8::External>::Cast(info.This()->GetInternalField(0));
HostObjectProxy* hostObjectProxy = reinterpret_cast<HostObjectProxy*>(data->Value());
if (hostObjectProxy != nullptr) {
V8Runtime& runtime = hostObjectProxy->runtime_;
std::shared_ptr<jsi::HostObject> hostObject = hostObjectProxy->hostObject_;
std::vector<jsi::PropNameID> propIds = hostObject->getPropertyNames(runtime);
v8::Local<v8::Array> result = v8::Array::New(info.GetIsolate(), static_cast<int>(propIds.size()));
v8::Local<v8::Context> context = info.GetIsolate()->GetCurrentContext();
for (uint32_t i = 0; i < result->Length(); i++)
{
v8::Local<v8::Value> propIdValue = runtime.valueRef(propIds[i]);
if (!result->Set(context, i, propIdValue).FromJust()) { std::terminate(); };
}
info.GetReturnValue().Set(result);
}
else {
info.GetReturnValue().Set(v8::Array::New(info.GetIsolate()));
}
}
HostObjectProxy(V8Runtime& rt, const std::shared_ptr<jsi::HostObject>& hostObject) : runtime_(rt), hostObject_(hostObject) {}
std::shared_ptr<jsi::HostObject> getHostObject() { return hostObject_; }
private:
friend class HostObjectLifetimeTracker;
void destroy() override {
hostObject_.reset();
}
V8Runtime& runtime_;
std::shared_ptr<jsi::HostObject> hostObject_;
};
class HostFunctionProxy : public IHostProxy {
public:
static void call(HostFunctionProxy& hostFunctionProxy, const v8::FunctionCallbackInfo<v8::Value>& callbackInfo) {
V8Runtime& runtime = const_cast<V8Runtime&>(hostFunctionProxy.runtime_);
v8::Isolate* isolate = callbackInfo.GetIsolate();
std::vector<jsi::Value> argsVector;
for (int i = 0; i < callbackInfo.Length(); i++)
{
argsVector.push_back(hostFunctionProxy.runtime_.createValue(callbackInfo[i]));
}
const jsi::Value& thisVal = runtime.createValue(callbackInfo.This());
try {
throw std::runtime_error("Error");
} catch(const std::exception& ex) {
std::string what = ex.what();
}
jsi::Value result;
try {
result = hostFunctionProxy.func_(runtime, thisVal, argsVector.data(), callbackInfo.Length());
}
catch (const jsi::JSError& error) {
callbackInfo.GetReturnValue().Set(v8::Undefined(isolate));
// Schedule to throw the exception back to JS.
isolate->ThrowException(runtime.valueRef(error.value()));
return;
}
catch (const std::exception& ex) {
callbackInfo.GetReturnValue().Set(v8::Undefined(isolate));
// Schedule to throw the exception back to JS.
v8::Local<v8::String> message = v8::String::NewFromUtf8(isolate, ex.what(), v8::NewStringType::kNormal).ToLocalChecked();
isolate->ThrowException(v8::Exception::Error(message));
return;
}
catch (...) {
callbackInfo.GetReturnValue().Set(v8::Undefined(isolate));
// Schedule to throw the exception back to JS.
v8::Local<v8::String> message = v8::String::NewFromOneByte(isolate, reinterpret_cast<const uint8_t*>("<Unknown exception in host function callback>"), v8::NewStringType::kNormal).ToLocalChecked();
isolate->ThrowException(v8::Exception::Error(message));
return;
}
callbackInfo.GetReturnValue().Set(runtime.valueRef(result));
}
public:
static void CDECL HostFunctionCallback(const v8::FunctionCallbackInfo<v8::Value>& info)
{
v8::HandleScope handle_scope(v8::Isolate::GetCurrent());
v8::Local<v8::External> data = v8::Local<v8::External>::Cast(info.Data());
HostFunctionProxy* hostFunctionProxy = reinterpret_cast<HostFunctionProxy*> (data->Value());
hostFunctionProxy->call(*hostFunctionProxy, info);
}
HostFunctionProxy(facebook::v8runtime::V8Runtime& runtime, jsi::HostFunctionType func)
: func_(std::move(func)), runtime_(runtime) {};
private:
friend class HostObjectLifetimeTracker;
void destroy() override {
func_ = [](Runtime& rt, const jsi::Value& thisVal, const jsi::Value* args, size_t count) {return jsi::Value::undefined(); };
}
jsi::HostFunctionType func_;
facebook::v8runtime::V8Runtime& runtime_;
};
class V8StringValue final : public PointerValue {
V8StringValue(v8::Local<v8::String> str);
~V8StringValue();
void invalidate() override;
v8::Persistent<v8::String> v8String_;
protected:
friend class V8Runtime;
};
class V8ObjectValue final : public PointerValue {
V8ObjectValue(v8::Local<v8::Object> obj);
~V8ObjectValue();
void invalidate() override;
v8::Persistent<v8::Object> v8Object_;
protected:
friend class V8Runtime;
};
class V8BigIntValue final : public PointerValue {
V8BigIntValue(v8::Local<v8::BigInt> obj);
~V8BigIntValue();
void invalidate() override;
v8::Persistent<v8::BigInt> v8BigInt_;
protected:
friend class V8Runtime;
};
class ExternalOwningOneByteStringResource
: public v8::String::ExternalOneByteStringResource {
public:
explicit ExternalOwningOneByteStringResource(const std::shared_ptr<const jsi::Buffer>& buffer)
: buffer_(buffer) /*create a copy of shared_ptr*/ {}
const char* data() const override { return reinterpret_cast<const char*>(buffer_->data()); }
size_t length() const override { return buffer_->size(); }
private:
std::shared_ptr<const jsi::Buffer> buffer_;
};
std::shared_ptr<const facebook::jsi::PreparedJavaScript> prepareJavaScript(const std::shared_ptr<const facebook::jsi::Buffer> &, std::string) override;
facebook::jsi::Value evaluatePreparedJavaScript(const std::shared_ptr<const facebook::jsi::PreparedJavaScript> &) override;
std::string symbolToString(const facebook::jsi::Symbol &) override;
jsi::BigInt createBigIntFromInt64(int64_t val) override;
jsi::BigInt createBigIntFromUint64(uint64_t val) override;
bool bigintIsInt64(const jsi::BigInt&) override;
bool bigintIsUint64(const jsi::BigInt& val) override;
uint64_t truncate(const jsi::BigInt& val) override;
jsi::String bigintToString(const jsi::BigInt&, int) override;
PointerValue* cloneString(const Runtime::PointerValue* pv) override;
PointerValue *cloneSymbol(const PointerValue *) override;
PointerValue* cloneObject(const Runtime::PointerValue* pv) override;
PointerValue* clonePropNameID(const Runtime::PointerValue* pv) override;
PointerValue* cloneBigInt(const PointerValue* pv) override;
jsi::PropNameID createPropNameIDFromAscii(const char* str, size_t length)
override;
jsi::PropNameID createPropNameIDFromUtf8(const uint8_t* utf8, size_t length)
override;
jsi::PropNameID createPropNameIDFromString(const jsi::String& str) override;
jsi::PropNameID createPropNameIDFromSymbol(const jsi::Symbol& sym) override;
std::string utf8(const jsi::PropNameID&) override;
bool compare(const jsi::PropNameID&, const jsi::PropNameID&) override;
jsi::String createStringFromAscii(const char* str, size_t length) override;
jsi::String createStringFromUtf8(const uint8_t* utf8, size_t length) override;
std::string utf8(const jsi::String&) override;
jsi::Object createObject() override;
jsi::Object createObject(std::shared_ptr<jsi::HostObject> ho) override;
virtual std::shared_ptr<jsi::HostObject> getHostObject(
const jsi::Object&) override;
jsi::HostFunctionType& getHostFunction(const jsi::Function&) override;
bool hasNativeState(const jsi::Object& obj) override;
std::shared_ptr<jsi::NativeState> getNativeState(const jsi::Object& obj) override;
void setNativeState(const jsi::Object& obj, std::shared_ptr<jsi::NativeState> nativeState) override;
jsi::Value getProperty(const jsi::Object&, const jsi::String& name) override;
jsi::Value getProperty(const jsi::Object&, const jsi::PropNameID& name)
override;
bool hasProperty(const jsi::Object&, const jsi::String& name) override;
bool hasProperty(const jsi::Object&, const jsi::PropNameID& name) override;
void setPropertyValue(
jsi::Object&,
const jsi::String& name,
const jsi::Value& value) override;
void setPropertyValue(
jsi::Object&,
const jsi::PropNameID& name,
const jsi::Value& value) override;
bool isArray(const jsi::Object&) const override;
bool isArrayBuffer(const jsi::Object&) const override;
bool isFunction(const jsi::Object&) const override;
bool isHostObject(const jsi::Object&) const override;
bool isHostFunction(const jsi::Function&) const override;
jsi::Array getPropertyNames(const jsi::Object&) override;
jsi::WeakObject createWeakObject(const jsi::Object&) override;
jsi::Value lockWeakObject(jsi::WeakObject&) override;
jsi::Array createArray(size_t length) override;
jsi::ArrayBuffer createArrayBuffer(std::shared_ptr<jsi::MutableBuffer>) override;
size_t size(const jsi::Array&) override;
size_t size(const jsi::ArrayBuffer&) override;
uint8_t* data(const jsi::ArrayBuffer&) override;
jsi::Value getValueAtIndex(const jsi::Array&, size_t i) override;
void setValueAtIndexImpl(jsi::Array&, size_t i, const jsi::Value& value)
override;
jsi::Function createFunctionFromHostFunction(
const jsi::PropNameID& name,
unsigned int paramCount,
jsi::HostFunctionType func) override;
jsi::Value call(
const jsi::Function&,
const jsi::Value& jsThis,
const jsi::Value* args,
size_t count) override;
jsi::Value callAsConstructor(
const jsi::Function&,
const jsi::Value* args,
size_t count) override;
bool strictEquals(const jsi::String& a, const jsi::String& b) const override;
bool strictEquals(const jsi::BigInt& a, const jsi::BigInt& b) const override;
bool strictEquals(const jsi::Object& a, const jsi::Object& b) const override;
bool strictEquals(const jsi::Symbol& a, const jsi::Symbol& b) const override;
bool instanceOf(const jsi::Object& o, const jsi::Function& f) override;
void AddHostObjectLifetimeTracker(std::shared_ptr<HostObjectLifetimeTracker> hostObjectLifetimeTracker);
static void CDECL OnMessage(v8::Local<v8::Message> message, v8::Local<v8::Value> error);
private:
v8::Local<v8::Context> CreateContext(v8::Isolate* isolate);
// Methods to compile and execute JS script.
v8::ScriptCompiler::CachedData* TryLoadCachedData(const std::string& path);
void PersistCachedData(std::unique_ptr<v8::ScriptCompiler::CachedData> cachedData, const std::string& path);
v8::Local<v8::Script> GetCompiledScriptFromCache(const v8::Local<v8::String> &source, const std::string& sourceURL);
v8::Local<v8::Script> GetCompiledScript(const v8::Local<v8::String> &source, const std::string& sourceURL);
jsi::Value ExecuteString(v8::Local<v8::String> source, const jsi::Buffer* cache, v8::Local<v8::Value> name, bool report_exceptions);
jsi::Value ExecuteString(const v8::Local<v8::String>& source, const std::string& sourceURL);
void Log(const std::string& message, const unsigned int logLevel) {
if (logger_) {
(*logger_)("V8Runtime:: " + message, logLevel);
}
}
void ReportException(v8::TryCatch* try_catch);
v8::Isolate* GetIsolate() const { return isolate_; }
// Basically convenience casts
static v8::Local<v8::String> stringRef(const jsi::String& str);
static v8::Local<v8::Value> valueRef(const jsi::PropNameID& sym);
static v8::Local<v8::Object> objectRef(const jsi::Object& obj);
static v8::Local<v8::BigInt> bigIntRef(const jsi::BigInt& bigInt);
v8::Local<v8::Value> valueRef(const jsi::Value& value);
jsi::Value createValue(v8::Local<v8::Value> value) const;
// Factory methods for creating String/Object
jsi::String createString(v8::Local<v8::String> stringRef) const;
jsi::PropNameID createPropNameID(v8::Local<v8::Value> propValRef);
jsi::Object createObject(v8::Local<v8::Object> objectRef) const;
jsi::BigInt createBigInt(v8::Local<v8::BigInt> bigIntRef) const;
// Used by factory methods and clone methods
jsi::Runtime::PointerValue* makeStringValue(v8::Local<v8::String> str) const;
jsi::Runtime::PointerValue* makeObjectValue(v8::Local<v8::Object> obj) const;
jsi::Runtime::PointerValue* makeBigIntValue(v8::Local<v8::BigInt> bigInt) const;
v8::Isolate* isolate_;
std::unique_ptr<IsolateData> isolate_data_;
v8::Global<v8::Context> context_;
v8::StartupData startup_data_;
v8::Isolate::CreateParams create_params_;
v8::Persistent<v8::FunctionTemplate> hostFunctionTemplate_;
v8::Persistent<v8::Function> hostObjectConstructor_;
std::list<std::shared_ptr<HostObjectLifetimeTracker>> hostObjectLifetimeTrackerList_;
// These are a few configuration parameter used only on Android now.
bool isCacheEnabled_ {true};
// bool shouldProduceFullCache_ {false};
// bool shouldSetNoLazyFlag_ {false};
std::string cacheDirectory_;
// CacheType cacheType_;
bool reportException_{ true };
bool printResult_{ false };
std::string desc_;
const v8::Platform* platform_;
std::shared_ptr<Logger> logger_;
std::shared_ptr<CacheProvider> cacheProvider_;
std::unique_ptr<InspectorInterface> inspector_{nullptr};
std::unique_ptr<const jsi::Buffer> default_snapshot_blob_;
std::unique_ptr<const jsi::Buffer> default_natives_blob_;
std::unique_ptr<const jsi::Buffer> custom_snapshot_blob_;
v8::StartupData default_snapshot_startup_data_;
v8::StartupData default_natives_startup_data_;
v8::StartupData custom_snapshot_startup_data_;
std::vector<std::unique_ptr<ExternalOwningOneByteStringResource>> owned_external_string_resources_;
// v8::Platform is shared between isolates. It will be kept alive till there is an isolate using it.
// It will be shutdown when isolate count drops to zero.
// Following statics are used to manage the liftime of the v8::Platform.
static std::mutex sMutex_;
static bool sIsPlatformCreated_;
static unsigned int sCurrentIsolateCount_;
};
}} // namespace facebook::v8runtime