-
Notifications
You must be signed in to change notification settings - Fork 24.4k
/
Copy pathjsi.cpp
528 lines (446 loc) · 14.6 KB
/
jsi.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
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <stdexcept>
#include <jsi/instrumentation.h>
#include <jsi/jsi.h>
namespace facebook {
namespace jsi {
namespace {
// This is used for generating short exception strings.
std::string kindToString(const Value& v, Runtime* rt = nullptr) {
if (v.isUndefined()) {
return "undefined";
} else if (v.isNull()) {
return "null";
} else if (v.isBool()) {
return v.getBool() ? "true" : "false";
} else if (v.isNumber()) {
return "a number";
} else if (v.isString()) {
return "a string";
} else if (v.isSymbol()) {
return "a symbol";
} else if (v.isBigInt()) {
return "a bigint";
} else {
assert(v.isObject() && "Expecting object.");
return rt != nullptr && v.getObject(*rt).isFunction(*rt) ? "a function"
: "an object";
}
}
// getPropertyAsFunction() will try to create a JSError. If the
// failure is in building a JSError, this will lead to infinite
// recursion. This function is used in place of getPropertyAsFunction
// when building JSError, to avoid that infinite recursion.
Value callGlobalFunction(Runtime& runtime, const char* name, const Value& arg) {
Value v = runtime.global().getProperty(runtime, name);
if (!v.isObject()) {
throw JSINativeException(
std::string("callGlobalFunction: JS global property '") + name +
"' is " + kindToString(v, &runtime) + ", expected a Function");
}
Object o = v.getObject(runtime);
if (!o.isFunction(runtime)) {
throw JSINativeException(
std::string("callGlobalFunction: JS global property '") + name +
"' is a non-callable Object, expected a Function");
}
Function f = std::move(o).getFunction(runtime);
return f.call(runtime, arg);
}
} // namespace
Buffer::~Buffer() = default;
MutableBuffer::~MutableBuffer() = default;
PreparedJavaScript::~PreparedJavaScript() = default;
Value HostObject::get(Runtime&, const PropNameID&) {
return Value();
}
void HostObject::set(Runtime& rt, const PropNameID& name, const Value&) {
std::string msg("TypeError: Cannot assign to property '");
msg += name.utf8(rt);
msg += "' on HostObject with default setter";
throw JSError(rt, msg);
}
HostObject::~HostObject() {}
NativeState::~NativeState() {}
Runtime::~Runtime() {}
Instrumentation& Runtime::instrumentation() {
class NoInstrumentation : public Instrumentation {
std::string getRecordedGCStats() override {
return "";
}
std::unordered_map<std::string, int64_t> getHeapInfo(bool) override {
return std::unordered_map<std::string, int64_t>{};
}
void collectGarbage(std::string) override {}
void startTrackingHeapObjectStackTraces(
std::function<void(
uint64_t,
std::chrono::microseconds,
std::vector<HeapStatsUpdate>)>) override {}
void stopTrackingHeapObjectStackTraces() override {}
void startHeapSampling(size_t) override {}
void stopHeapSampling(std::ostream&) override {}
void createSnapshotToFile(const std::string&) override {
throw JSINativeException(
"Default instrumentation cannot create a heap snapshot");
}
void createSnapshotToStream(std::ostream&) override {
throw JSINativeException(
"Default instrumentation cannot create a heap snapshot");
}
std::string flushAndDisableBridgeTrafficTrace() override {
std::abort();
}
void writeBasicBlockProfileTraceToFile(const std::string&) const override {
std::abort();
}
void dumpProfilerSymbolsToFile(const std::string&) const override {
std::abort();
}
};
static NoInstrumentation sharedInstance;
return sharedInstance;
}
Value Runtime::createValueFromJsonUtf8(const uint8_t* json, size_t length) {
Function parseJson = global()
.getPropertyAsObject(*this, "JSON")
.getPropertyAsFunction(*this, "parse");
return parseJson.call(*this, String::createFromUtf8(*this, json, length));
}
Pointer& Pointer::operator=(Pointer&& other) {
if (ptr_) {
ptr_->invalidate();
}
ptr_ = other.ptr_;
other.ptr_ = nullptr;
return *this;
}
Object Object::getPropertyAsObject(Runtime& runtime, const char* name) const {
Value v = getProperty(runtime, name);
if (!v.isObject()) {
throw JSError(
runtime,
std::string("getPropertyAsObject: property '") + name + "' is " +
kindToString(v, &runtime) + ", expected an Object");
}
return v.getObject(runtime);
}
Function Object::getPropertyAsFunction(Runtime& runtime, const char* name)
const {
Object obj = getPropertyAsObject(runtime, name);
if (!obj.isFunction(runtime)) {
throw JSError(
runtime,
std::string("getPropertyAsFunction: property '") + name + "' is " +
kindToString(std::move(obj), &runtime) + ", expected a Function");
};
return std::move(obj).getFunction(runtime);
}
Array Object::asArray(Runtime& runtime) const& {
if (!isArray(runtime)) {
throw JSError(
runtime,
"Object is " + kindToString(Value(runtime, *this), &runtime) +
", expected an array");
}
return getArray(runtime);
}
Array Object::asArray(Runtime& runtime) && {
if (!isArray(runtime)) {
throw JSError(
runtime,
"Object is " + kindToString(Value(runtime, *this), &runtime) +
", expected an array");
}
return std::move(*this).getArray(runtime);
}
Function Object::asFunction(Runtime& runtime) const& {
if (!isFunction(runtime)) {
throw JSError(
runtime,
"Object is " + kindToString(Value(runtime, *this), &runtime) +
", expected a function");
}
return getFunction(runtime);
}
Function Object::asFunction(Runtime& runtime) && {
if (!isFunction(runtime)) {
throw JSError(
runtime,
"Object is " + kindToString(Value(runtime, *this), &runtime) +
", expected a function");
}
return std::move(*this).getFunction(runtime);
}
Value::Value(Value&& other) : Value(other.kind_) {
if (kind_ == BooleanKind) {
data_.boolean = other.data_.boolean;
} else if (kind_ == NumberKind) {
data_.number = other.data_.number;
} else if (kind_ >= PointerKind) {
new (&data_.pointer) Pointer(std::move(other.data_.pointer));
}
// when the other's dtor runs, nothing will happen.
other.kind_ = UndefinedKind;
}
Value::Value(Runtime& runtime, const Value& other) : Value(other.kind_) {
// data_ is uninitialized, so use placement new to create non-POD
// types in it. Any other kind of initialization will call a dtor
// first, which is incorrect.
if (kind_ == BooleanKind) {
data_.boolean = other.data_.boolean;
} else if (kind_ == NumberKind) {
data_.number = other.data_.number;
} else if (kind_ == SymbolKind) {
new (&data_.pointer) Pointer(runtime.cloneSymbol(other.data_.pointer.ptr_));
} else if (kind_ == BigIntKind) {
new (&data_.pointer) Pointer(runtime.cloneBigInt(other.data_.pointer.ptr_));
} else if (kind_ == StringKind) {
new (&data_.pointer) Pointer(runtime.cloneString(other.data_.pointer.ptr_));
} else if (kind_ >= ObjectKind) {
new (&data_.pointer) Pointer(runtime.cloneObject(other.data_.pointer.ptr_));
}
}
Value::~Value() {
if (kind_ >= PointerKind) {
data_.pointer.~Pointer();
}
}
bool Value::strictEquals(Runtime& runtime, const Value& a, const Value& b) {
if (a.kind_ != b.kind_) {
return false;
}
switch (a.kind_) {
case UndefinedKind:
case NullKind:
return true;
case BooleanKind:
return a.data_.boolean == b.data_.boolean;
case NumberKind:
return a.data_.number == b.data_.number;
case SymbolKind:
return runtime.strictEquals(
static_cast<const Symbol&>(a.data_.pointer),
static_cast<const Symbol&>(b.data_.pointer));
case BigIntKind:
return runtime.strictEquals(
static_cast<const BigInt&>(a.data_.pointer),
static_cast<const BigInt&>(b.data_.pointer));
case StringKind:
return runtime.strictEquals(
static_cast<const String&>(a.data_.pointer),
static_cast<const String&>(b.data_.pointer));
case ObjectKind:
return runtime.strictEquals(
static_cast<const Object&>(a.data_.pointer),
static_cast<const Object&>(b.data_.pointer));
}
return false;
}
bool Value::asBool() const {
if (!isBool()) {
throw JSINativeException(
"Value is " + kindToString(*this) + ", expected a boolean");
}
return getBool();
}
double Value::asNumber() const {
if (!isNumber()) {
throw JSINativeException(
"Value is " + kindToString(*this) + ", expected a number");
}
return getNumber();
}
Object Value::asObject(Runtime& rt) const& {
if (!isObject()) {
throw JSError(
rt, "Value is " + kindToString(*this, &rt) + ", expected an Object");
}
return getObject(rt);
}
Object Value::asObject(Runtime& rt) && {
if (!isObject()) {
throw JSError(
rt, "Value is " + kindToString(*this, &rt) + ", expected an Object");
}
auto ptr = data_.pointer.ptr_;
data_.pointer.ptr_ = nullptr;
return static_cast<Object>(ptr);
}
Symbol Value::asSymbol(Runtime& rt) const& {
if (!isSymbol()) {
throw JSError(
rt, "Value is " + kindToString(*this, &rt) + ", expected a Symbol");
}
return getSymbol(rt);
}
Symbol Value::asSymbol(Runtime& rt) && {
if (!isSymbol()) {
throw JSError(
rt, "Value is " + kindToString(*this, &rt) + ", expected a Symbol");
}
return std::move(*this).getSymbol(rt);
}
BigInt Value::asBigInt(Runtime& rt) const& {
if (!isBigInt()) {
throw JSError(
rt, "Value is " + kindToString(*this, &rt) + ", expected a BigInt");
}
return getBigInt(rt);
}
BigInt Value::asBigInt(Runtime& rt) && {
if (!isBigInt()) {
throw JSError(
rt, "Value is " + kindToString(*this, &rt) + ", expected a BigInt");
}
return std::move(*this).getBigInt(rt);
}
String Value::asString(Runtime& rt) const& {
if (!isString()) {
throw JSError(
rt, "Value is " + kindToString(*this, &rt) + ", expected a String");
}
return getString(rt);
}
String Value::asString(Runtime& rt) && {
if (!isString()) {
throw JSError(
rt, "Value is " + kindToString(*this, &rt) + ", expected a String");
}
return std::move(*this).getString(rt);
}
String Value::toString(Runtime& runtime) const {
Function toString = runtime.global().getPropertyAsFunction(runtime, "String");
return toString.call(runtime, *this).getString(runtime);
}
uint64_t BigInt::asUint64(Runtime& runtime) const {
if (!isUint64(runtime)) {
throw JSError(runtime, "Lossy truncation in BigInt64::asUint64");
}
return getUint64(runtime);
}
int64_t BigInt::asInt64(Runtime& runtime) const {
if (!isInt64(runtime)) {
throw JSError(runtime, "Lossy truncation in BigInt64::asInt64");
}
return getInt64(runtime);
}
Array Array::createWithElements(
Runtime& rt,
std::initializer_list<Value> elements) {
Array result(rt, elements.size());
size_t index = 0;
for (const auto& element : elements) {
result.setValueAtIndex(rt, index++, element);
}
return result;
}
std::vector<PropNameID> HostObject::getPropertyNames(Runtime&) {
return {};
}
Runtime::ScopeState* Runtime::pushScope() {
return nullptr;
}
void Runtime::popScope(ScopeState*) {}
JSError::JSError(Runtime& rt, Value&& value) {
setValue(rt, std::move(value));
}
JSError::JSError(Runtime& rt, std::string msg) : message_(std::move(msg)) {
try {
setValue(
rt,
callGlobalFunction(rt, "Error", String::createFromUtf8(rt, message_)));
} catch (const JSIException& ex) {
message_ = std::string(ex.what()) + " (while raising " + message_ + ")";
setValue(rt, String::createFromUtf8(rt, message_));
}
}
JSError::JSError(Runtime& rt, std::string msg, std::string stack)
: message_(std::move(msg)), stack_(std::move(stack)) {
try {
Object e(rt);
e.setProperty(rt, "message", String::createFromUtf8(rt, message_));
e.setProperty(rt, "stack", String::createFromUtf8(rt, stack_));
setValue(rt, std::move(e));
} catch (const JSIException& ex) {
setValue(rt, String::createFromUtf8(rt, ex.what()));
}
}
JSError::JSError(std::string what, Runtime& rt, Value&& value)
: JSIException(std::move(what)) {
setValue(rt, std::move(value));
}
void JSError::setValue(Runtime& rt, Value&& value) {
value_ = std::make_shared<Value>(std::move(value));
if ((message_.empty() || stack_.empty()) && value_->isObject()) {
auto obj = value_->getObject(rt);
if (message_.empty()) {
try {
Value message = obj.getProperty(rt, "message");
if (!message.isUndefined() && !message.isString()) {
message = callGlobalFunction(rt, "String", message);
}
if (message.isString()) {
message_ = message.getString(rt).utf8(rt);
} else if (!message.isUndefined()) {
message_ = "String(e.message) is a " + kindToString(message, &rt);
}
} catch (const JSIException& ex) {
message_ = std::string("[Exception while creating message string: ") +
ex.what() + "]";
}
}
if (stack_.empty()) {
try {
Value stack = obj.getProperty(rt, "stack");
if (!stack.isUndefined() && !stack.isString()) {
stack = callGlobalFunction(rt, "String", stack);
}
if (stack.isString()) {
stack_ = stack.getString(rt).utf8(rt);
} else if (!stack.isUndefined()) {
stack_ = "String(e.stack) is a " + kindToString(stack, &rt);
}
} catch (const JSIException& ex) {
message_ = std::string("[Exception while creating stack string: ") +
ex.what() + "]";
}
}
}
if (message_.empty()) {
try {
if (value_->isString()) {
message_ = value_->getString(rt).utf8(rt);
} else {
Value message = callGlobalFunction(rt, "String", *value_);
if (message.isString()) {
message_ = message.getString(rt).utf8(rt);
} else {
message_ = "String(e) is a " + kindToString(message, &rt);
}
}
} catch (const JSIException& ex) {
message_ = std::string("[Exception while creating message string: ") +
ex.what() + "]";
}
}
if (stack_.empty()) {
stack_ = "no stack";
}
if (what_.empty()) {
what_ = message_ + "\n\n" + stack_;
}
}
JSIException::~JSIException() {}
JSINativeException::~JSINativeException() {}
JSError::~JSError() {}
} // namespace jsi
} // namespace facebook