-
Notifications
You must be signed in to change notification settings - Fork 68
/
HostProxy.cpp
267 lines (236 loc) · 8.64 KB
/
HostProxy.cpp
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
/*
* Copyright (c) Kudo Chien.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "HostProxy.h"
#include "JSIV8ValueConverter.h"
namespace jsi = facebook::jsi;
namespace rnv8 {
HostObjectProxy::HostObjectProxy(
V8Runtime &runtime,
v8::Isolate *isolate,
std::shared_ptr<jsi::HostObject> hostObject)
: runtime_(runtime), isolate_(isolate), hostObject_(hostObject) {}
void HostObjectProxy::BindFinalizer(const v8::Local<v8::Object> &object) {
v8::HandleScope scopedHandle(isolate_);
weakHandle_.Reset(isolate_, object);
weakHandle_.SetWeak(this, Finalizer, v8::WeakCallbackType::kParameter);
}
std::shared_ptr<jsi::HostObject> HostObjectProxy::GetHostObject() {
return hostObject_;
}
// static
void HostObjectProxy::Getter(
v8::Local<v8::Name> property,
const v8::PropertyCallbackInfo<v8::Value> &info) {
v8::HandleScope scopedHandle(info.GetIsolate());
v8::Local<v8::External> data =
v8::Local<v8::External>::Cast(info.This()->GetInternalField(0));
HostObjectProxy *hostObjectProxy =
reinterpret_cast<HostObjectProxy *>(data->Value());
assert(hostObjectProxy);
auto &runtime = hostObjectProxy->runtime_;
jsi::PropNameID sym = JSIV8ValueConverter::ToJSIPropNameID(runtime, property);
jsi::Value ret;
try {
ret = hostObjectProxy->hostObject_->get(runtime, sym);
} catch (const jsi::JSError &error) {
info.GetIsolate()->ThrowException(
JSIV8ValueConverter::ToV8Value(runtime, error.value()));
return;
} catch (const std::exception &ex) {
auto excValue =
runtime.global()
.getPropertyAsFunction(runtime, "Error")
.call(
runtime,
std::string("Exception in HostObject::get(property:") +
JSIV8ValueConverter::ToSTLString(
info.GetIsolate(), property) +
std::string("): ") + ex.what());
info.GetIsolate()->ThrowException(
JSIV8ValueConverter::ToV8Value(runtime, excValue));
return;
} catch (...) {
auto excValue =
runtime.global()
.getPropertyAsFunction(runtime, "Error")
.call(
runtime,
std::string("Exception in HostObject::get(property:") +
JSIV8ValueConverter::ToSTLString(
info.GetIsolate(), property) +
std::string("): <unknown>"));
info.GetIsolate()->ThrowException(
JSIV8ValueConverter::ToV8Value(runtime, excValue));
return;
}
info.GetReturnValue().Set(JSIV8ValueConverter::ToV8Value(runtime, ret));
}
// static
void HostObjectProxy::Setter(
v8::Local<v8::Name> property,
v8::Local<v8::Value> value,
const v8::PropertyCallbackInfo<v8::Value> &info) {
v8::HandleScope scopedHandle(info.GetIsolate());
v8::Local<v8::External> data =
v8::Local<v8::External>::Cast(info.This()->GetInternalField(0));
HostObjectProxy *hostObjectProxy =
reinterpret_cast<HostObjectProxy *>(data->Value());
assert(hostObjectProxy);
auto &runtime = hostObjectProxy->runtime_;
jsi::PropNameID sym = JSIV8ValueConverter::ToJSIPropNameID(runtime, property);
try {
hostObjectProxy->hostObject_->set(
runtime,
sym,
JSIV8ValueConverter::ToJSIValue(info.GetIsolate(), value));
} catch (const jsi::JSError &error) {
info.GetIsolate()->ThrowException(
JSIV8ValueConverter::ToV8Value(runtime, error.value()));
return;
} catch (const std::exception &ex) {
auto excValue =
runtime.global()
.getPropertyAsFunction(runtime, "Error")
.call(
runtime,
std::string("Exception in HostObject::set(property:") +
JSIV8ValueConverter::ToSTLString(
info.GetIsolate(), property) +
std::string("): ") + ex.what());
info.GetIsolate()->ThrowException(
JSIV8ValueConverter::ToV8Value(runtime, excValue));
return;
} catch (...) {
auto excValue =
runtime.global()
.getPropertyAsFunction(runtime, "Error")
.call(
runtime,
std::string("Exception in HostObject::set(property:") +
JSIV8ValueConverter::ToSTLString(
info.GetIsolate(), property) +
std::string("): <unknown>"));
info.GetIsolate()->ThrowException(
JSIV8ValueConverter::ToV8Value(runtime, excValue));
return;
}
return;
}
// static
void HostObjectProxy::Enumerator(
const v8::PropertyCallbackInfo<v8::Array> &info) {
v8::HandleScope scopedHandle(info.GetIsolate());
v8::Local<v8::External> data =
v8::Local<v8::External>::Cast(info.This()->GetInternalField(0));
HostObjectProxy *hostObjectProxy =
reinterpret_cast<HostObjectProxy *>(data->Value());
assert(hostObjectProxy);
auto &runtime = hostObjectProxy->runtime_;
auto names = hostObjectProxy->hostObject_->getPropertyNames(runtime);
v8::Local<v8::Array> result =
v8::Array::New(info.GetIsolate(), static_cast<int>(names.size()));
v8::Local<v8::Context> context = info.GetIsolate()->GetCurrentContext();
for (uint32_t i = 0; i < result->Length(); ++i) {
v8::Local<v8::Value> value =
JSIV8ValueConverter::ToV8String(runtime, names[i]);
if (!result->Set(context, i, value).FromJust()) {
std::abort();
};
}
info.GetReturnValue().Set(result);
}
// static
void HostObjectProxy::Finalizer(
const v8::WeakCallbackInfo<HostObjectProxy> &data) {
auto *pThis = data.GetParameter();
if (pThis->hostObject_.use_count() == 1) {
pThis->hostObject_.reset();
}
pThis->weakHandle_.Reset();
delete pThis;
}
HostFunctionProxy::HostFunctionProxy(
V8Runtime &runtime,
v8::Isolate *isolate,
jsi::HostFunctionType &&hostFunction)
: runtime_(runtime),
isolate_(isolate),
hostFunction_(std::move(hostFunction)) {}
void HostFunctionProxy::BindFinalizer(const v8::Local<v8::Object> &object) {
v8::HandleScope scopedHandle(isolate_);
weakHandle_.Reset(isolate_, object);
weakHandle_.SetWeak(this, Finalizer, v8::WeakCallbackType::kParameter);
}
jsi::HostFunctionType &HostFunctionProxy::GetHostFunction() {
return hostFunction_;
}
// static
void HostFunctionProxy::Finalizer(
const v8::WeakCallbackInfo<HostFunctionProxy> &data) {
auto *pThis = data.GetParameter();
pThis->weakHandle_.Reset();
delete pThis;
}
// static
void HostFunctionProxy::FunctionCallback(
const v8::FunctionCallbackInfo<v8::Value> &info) {
v8::HandleScope scopedHandle(info.GetIsolate());
v8::Local<v8::External> data = v8::Local<v8::External>::Cast(info.Data());
auto *hostFunctionProxy =
reinterpret_cast<HostFunctionProxy *>(data->Value());
auto &runtime = hostFunctionProxy->runtime_;
int argumentCount = info.Length();
const unsigned maxStackArgCount = 8;
jsi::Value stackArgs[maxStackArgCount];
std::unique_ptr<jsi::Value[]> heapArgs;
jsi::Value *args;
if (argumentCount > maxStackArgCount) {
heapArgs = std::make_unique<jsi::Value[]>(argumentCount);
for (size_t i = 0; i < argumentCount; i++) {
heapArgs[i] = JSIV8ValueConverter::ToJSIValue(info.GetIsolate(), info[i]);
}
args = heapArgs.get();
} else {
for (size_t i = 0; i < argumentCount; i++) {
stackArgs[i] =
JSIV8ValueConverter::ToJSIValue(info.GetIsolate(), info[i]);
}
args = stackArgs;
}
v8::Local<v8::Value> result;
jsi::Value thisVal(
JSIV8ValueConverter::ToJSIValue(info.GetIsolate(), info.This()));
try {
result = JSIV8ValueConverter::ToV8Value(
runtime,
hostFunctionProxy->hostFunction_(
runtime, thisVal, args, argumentCount));
} catch (const jsi::JSError &error) {
info.GetIsolate()->ThrowException(
JSIV8ValueConverter::ToV8Value(runtime, error.value()));
return;
} catch (const std::exception &ex) {
std::string exceptionString("Exception in HostFunction: ");
exceptionString += ex.what();
auto excValue = runtime.global()
.getPropertyAsFunction(runtime, "Error")
.call(runtime, exceptionString);
info.GetIsolate()->ThrowException(
JSIV8ValueConverter::ToV8Value(runtime, excValue));
return;
} catch (...) {
std::string exceptionString("Exception in HostFunction: <unknown>");
auto excValue = runtime.global()
.getPropertyAsFunction(runtime, "Error")
.call(runtime, exceptionString);
info.GetIsolate()->ThrowException(
JSIV8ValueConverter::ToV8Value(runtime, excValue));
return;
}
info.GetReturnValue().Set(result);
}
} // namespace rnv8