/
JscLocalReference.cc
594 lines (475 loc) · 20.5 KB
/
JscLocalReference.cc
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
/*
* 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.
*/
#include <utility>
#include <vector>
#include "../../src/Native.hpp"
#include "../../src/Reference.h"
#include "../../src/Utils.h"
#include "../../src/Value.h"
#include "JscEngine.h"
#include "JscHelper.hpp"
#include "JscReference.hpp"
#include "trait/TraitReference.h"
namespace script {
namespace jsc_backend {
StringLocalRef::StringLocalRef(JSStringRef rawStringRef) : rawStringRef_(rawStringRef) {
assert(rawStringRef);
}
StringLocalRef::StringLocalRef(JSValueRef stringRef) : stringRef_(stringRef) { assert(stringRef); }
JSValueRef StringLocalRef::getValue(JSContextRef context) const {
if (!stringRef_) {
stringRef_ = JSValueMakeString(context, rawStringRef_.get());
}
return stringRef_;
}
JSStringRef StringLocalRef::getString(JSContextRef context) const {
if (!rawStringRef_) {
JSValueRef jscException = nullptr;
rawStringRef_ =
StringLocalRef::SharedStringRef{JSValueToStringCopy(context, stringRef_, &jscException)};
JscEngine::checkException(jscException);
}
return rawStringRef_.get();
}
StringLocalRef::operator JSValueRef() const {
return getValue(jsc_backend::currentEngineContextChecked());
}
StringLocalRef& StringLocalRef::operator=(nullptr_t) {
stringRef_ = nullptr;
rawStringRef_ = nullptr;
return *this;
}
StringLocalRef::SharedStringRef StringLocalRef::getSharedStringRef(JSContextRef context) const {
getString(context);
return rawStringRef_;
}
bool isNullOrUndefined(JSValueRef value) {
if (value == nullptr) return true;
auto ctx = jsc_backend::currentEngineContextChecked();
auto type = JSValueGetType(ctx, value);
return type == JSType::kJSTypeNull || type == JSType::kJSTypeUndefined;
}
JSObjectRef valueToObj(JSGlobalContextRef context, JSValueRef value) {
JSValueRef jscException = nullptr;
auto ret = JSValueToObject(context, value, &jscException);
jsc_backend::JscEngine::checkException(jscException);
return ret;
}
void valueConstructorCheck(JSValueRef value) {
SCRIPTX_UNUSED(value);
#ifndef NDEBUG
if (jsc_backend::isNullOrUndefined(value)) throw Exception("null reference");
#endif
}
void valueConstructorCheck(const StringLocalRef&) {}
} // namespace jsc_backend
// val is created from JSC, so its ref-count is 1
// the create Local directly owns it
// Local<ValueType>::Local(InternalLocalRef val)
#define REF_IMPL_BASIC_FUNC(ValueType) \
Local<ValueType>::Local(const Local<ValueType>& copy) : val_(copy.val_) {} \
Local<ValueType>::Local(Local<ValueType>&& move) noexcept : val_(std::move(move.val_)) { \
move.val_ = nullptr; \
} \
Local<ValueType>::~Local() { val_ = nullptr; } \
Local<ValueType>& Local<ValueType>::operator=(const Local& from) { \
Local(from).swap(*this); \
return *this; \
} \
Local<ValueType>& Local<ValueType>::operator=(Local&& move) noexcept { \
Local(std::move(move)).swap(*this); \
return *this; \
} \
void Local<ValueType>::swap(Local& rhs) noexcept { std::swap(val_, rhs.val_); }
#define REF_IMPL_BASIC_EQUALS(ValueType) \
bool Local<ValueType>::operator==(const script::Local<script::Value>& other) const { \
return asValue() == other; \
}
#define REF_IMPL_BASIC_NOT_VALUE(ValueType) \
Local<ValueType>::Local(InternalLocalRef val) : val_(std::move(val)) { \
jsc_backend::valueConstructorCheck(val_); \
} \
Local<String> Local<ValueType>::describe() const { return asValue().describe(); } \
std::string Local<ValueType>::describeUtf8() const { return asValue().describeUtf8(); }
#define REF_IMPL_TO_VALUE(ValueType) \
Local<Value> Local<ValueType>::asValue() const { return Local<Value>(val_); }
REF_IMPL_BASIC_FUNC(Value)
REF_IMPL_BASIC_FUNC(Object)
REF_IMPL_BASIC_NOT_VALUE(Object)
REF_IMPL_BASIC_EQUALS(Object)
REF_IMPL_TO_VALUE(Object)
REF_IMPL_BASIC_FUNC(String)
REF_IMPL_BASIC_NOT_VALUE(String)
REF_IMPL_BASIC_EQUALS(String)
REF_IMPL_BASIC_FUNC(Number)
REF_IMPL_BASIC_NOT_VALUE(Number)
REF_IMPL_BASIC_EQUALS(Number)
REF_IMPL_TO_VALUE(Number)
REF_IMPL_BASIC_FUNC(Boolean)
REF_IMPL_BASIC_NOT_VALUE(Boolean)
REF_IMPL_BASIC_EQUALS(Boolean)
REF_IMPL_TO_VALUE(Boolean)
REF_IMPL_BASIC_FUNC(Function)
REF_IMPL_BASIC_NOT_VALUE(Function)
REF_IMPL_BASIC_EQUALS(Function)
REF_IMPL_TO_VALUE(Function)
REF_IMPL_BASIC_FUNC(Array)
REF_IMPL_BASIC_NOT_VALUE(Array)
REF_IMPL_BASIC_EQUALS(Array)
REF_IMPL_TO_VALUE(Array)
REF_IMPL_BASIC_FUNC(ByteBuffer)
REF_IMPL_BASIC_NOT_VALUE(ByteBuffer)
REF_IMPL_BASIC_EQUALS(ByteBuffer)
REF_IMPL_TO_VALUE(ByteBuffer)
REF_IMPL_BASIC_FUNC(Unsupported)
REF_IMPL_BASIC_NOT_VALUE(Unsupported)
REF_IMPL_BASIC_EQUALS(Unsupported)
REF_IMPL_TO_VALUE(Unsupported)
// ==== value ====
Local<Value>::Local() noexcept : val_() {}
Local<Value>::Local(InternalLocalRef v8Local) : val_(v8Local) {}
bool Local<Value>::isNull() const { return jsc_backend::isNullOrUndefined(val_); }
void Local<Value>::reset() { val_ = nullptr; }
ValueKind Local<Value>::getKind() const {
if (val_ == nullptr) {
return ValueKind::kNull;
}
auto ctx = jsc_backend::currentEngineContextChecked();
auto type = JSValueGetType(ctx, val_);
if (type == JSType::kJSTypeUndefined || type == JSType::kJSTypeNull) {
return ValueKind::kNull;
} else if (type == JSType::kJSTypeString) {
return ValueKind::kString;
} else if (type == JSType::kJSTypeNumber) {
return ValueKind::kNumber;
} else if (type == JSType::kJSTypeBoolean) {
return ValueKind::kBoolean;
} else if (JSValueIsArray(ctx, val_)) {
return ValueKind::kArray;
} else if (JSValueIsObject(ctx, val_)) {
auto obj = jsc_backend::valueToObj(ctx, val_);
if (JSObjectIsFunction(ctx, obj)) {
return ValueKind::kFunction;
}
if (isByteBuffer()) {
return ValueKind::kByteBuffer;
}
return ValueKind::kObject;
} else {
return ValueKind::kUnsupported;
}
}
bool Local<Value>::isString() const {
if (val_ == nullptr) return false;
return JSValueGetType(jsc_backend::currentEngineContextChecked(), val_) == JSType::kJSTypeString;
}
bool Local<Value>::isNumber() const {
if (val_ == nullptr) return false;
return JSValueGetType(jsc_backend::currentEngineContextChecked(), val_) == JSType::kJSTypeNumber;
}
bool Local<Value>::isBoolean() const {
if (val_ == nullptr) return false;
return JSValueGetType(jsc_backend::currentEngineContextChecked(), val_) == JSType::kJSTypeBoolean;
}
bool Local<Value>::isFunction() const {
if (val_ == nullptr) return false;
auto ctx = jsc_backend::currentEngineContextChecked();
return JSValueIsObject(ctx, val_) && JSObjectIsFunction(ctx, const_cast<JSObjectRef>(val_));
}
bool Local<Value>::isArray() const {
if (val_ == nullptr) return false;
return JSValueIsArray(jsc_backend::currentEngineContextChecked(), val_);
}
bool Local<Value>::isByteBuffer() const {
if (val_ == nullptr) return false;
if (jsc_backend::JscEngine::hasByteBufferAPI_) {
JSValueRef jscException = nullptr;
auto context = jsc_backend::currentEngineContextChecked();
bool ret = JSValueGetTypedArrayType(context, val_, &jscException) != kJSTypedArrayTypeNone;
jsc_backend::JscEngine::checkException(jscException);
if (ret) return true;
// check is DataView
if (JSValueIsObject(context, val_)) {
auto ptr = JSObjectGetTypedArrayBytesPtr(context, jsc_backend::valueToObj(context, val_),
&jscException);
jsc_backend::JscEngine::checkException(jscException);
return ptr != nullptr;
}
} else {
return jsc_backend::currentEngine()->isByteBuffer_.get().call({}, *this).asBoolean().value();
}
return false;
}
bool Local<Value>::isObject() const {
return val_ != nullptr && JSValueIsObject(jsc_backend::currentEngineContextChecked(), val_);
}
bool Local<Value>::isUnsupported() const { return getKind() == ValueKind::kUnsupported; }
Local<String> Local<Value>::asString() const {
if (isString()) return Local<String>{jsc_backend::StringLocalRef(val_)};
throw Exception("can't cast value as String");
}
Local<Number> Local<Value>::asNumber() const {
if (isNumber()) return Local<Number>{val_};
throw Exception("can't cast value as Number");
}
Local<Boolean> Local<Value>::asBoolean() const {
if (isBoolean()) return Local<Boolean>{val_};
throw Exception("can't cast value as Boolean");
}
Local<Function> Local<Value>::asFunction() const {
if (isFunction()) {
return Local<Function>{
jsc_backend::valueToObj(jsc_backend::currentEngineContextChecked(), val_)};
}
throw Exception("can't cast value as Function");
}
Local<Array> Local<Value>::asArray() const {
if (isArray()) {
return Local<Array>{jsc_backend::valueToObj(jsc_backend::currentEngineContextChecked(), val_)};
}
throw Exception("can't cast value as Array");
}
Local<ByteBuffer> Local<Value>::asByteBuffer() const {
if (isByteBuffer()) {
return Local<ByteBuffer>{
jsc_backend::valueToObj(jsc_backend::currentEngineContextChecked(), val_)};
}
throw Exception("can't cast value as ByteBuffer");
}
Local<Object> Local<Value>::asObject() const {
if (isObject()) {
return Local<Object>{jsc_backend::valueToObj(jsc_backend::currentEngineContextChecked(), val_)};
}
throw Exception("can't cast value as Object");
}
Local<Unsupported> Local<Value>::asUnsupported() const {
if (isUnsupported()) return Local<Unsupported>{val_};
throw Exception("can't cast value as Unsupported");
}
bool Local<Value>::operator==(const script::Local<script::Value>& other) const {
if (isNull()) return other.isNull();
return JSValueIsStrictEqual(jsc_backend::currentEngineContextChecked(),
const_cast<JSValueRef>(val_), const_cast<JSValueRef>(other.val_));
}
Local<String> Local<Value>::describe() const {
if (isNull()) return String::newString("null");
if (isString()) {
return asString();
} else {
JSValueRef jscException = nullptr;
auto context = jsc_backend::currentEngineContextChecked();
auto str = JSValueToStringCopy(context, val_, &jscException);
jsc_backend::JscEngine::checkException(jscException);
return Local<String>(jsc_backend::StringLocalRef(str));
}
}
Local<Value> Local<String>::asValue() const {
return Local<Value>(val_.getValue(jsc_backend::currentEngineContextChecked()));
}
Local<Value> Local<Object>::get(const script::Local<script::String>& key) const {
JSValueRef jscException = nullptr;
auto context = jsc_backend::currentEngineContextChecked();
Local<Value> ret(JSObjectGetProperty(context, jsc_backend::JscEngine::toJsc(context, *this),
key.val_.getString(context), &jscException));
jsc_backend::JscEngine::checkException(jscException);
return ret;
}
void Local<Object>::set(const script::Local<script::String>& key,
const script::Local<script::Value>& value) const {
JSValueRef jscException = nullptr;
auto context = jsc_backend::currentEngineContextChecked();
JSObjectSetProperty(context, jsc_backend::JscEngine::toJsc(context, *this),
key.val_.getString(context), jsc_backend::JscEngine::toJsc(context, value),
kJSPropertyAttributeNone, &jscException);
jsc_backend::JscEngine::checkException(jscException);
}
void Local<Object>::remove(const Local<class script::String>& key) const {
JSValueRef jscException = nullptr;
auto context = jsc_backend::currentEngineContextChecked();
JSObjectDeleteProperty(context, jsc_backend::JscEngine::toJsc(context, *this),
key.val_.getString(context), &jscException);
jsc_backend::JscEngine::checkException(jscException);
}
bool Local<Object>::has(const Local<class script::String>& key) const {
auto context = jsc_backend::currentEngineContextChecked();
return JSObjectHasProperty(context, jsc_backend::JscEngine::toJsc(context, *this),
key.val_.getString(context));
}
bool Local<Object>::instanceOf(const Local<class script::Value>& type) const {
if (!type.isObject()) {
return false;
}
JSValueRef jscException = nullptr;
auto context = jsc_backend::currentEngineContextChecked();
auto ret = JSValueIsInstanceOfConstructor(context, jsc_backend::JscEngine::toJsc(context, *this),
jsc_backend::JscEngine::toJsc(context, type.asObject()),
&jscException);
jsc_backend::JscEngine::checkException(jscException);
return ret;
}
std::vector<Local<String>> Local<Object>::getKeys() const {
auto context = jsc_backend::currentEngineContextChecked();
auto array = JSObjectCopyPropertyNames(context, val_);
auto count = JSPropertyNameArrayGetCount(array);
std::vector<Local<String>> ret;
ret.reserve(count);
for (size_t i = 0; i < count; ++i) {
auto name = JSPropertyNameArrayGetNameAtIndex(array, i);
JSStringRetain(name);
ret.push_back(Local<String>(jsc_backend::StringLocalRef(name)));
}
JSPropertyNameArrayRelease(array);
return ret;
}
float Local<Number>::toFloat() const { return static_cast<float>(toDouble()); }
double Local<Number>::toDouble() const {
JSValueRef jscException = nullptr;
double ret = JSValueToNumber(jsc_backend::currentEngineContextChecked(), val_, &jscException);
jsc_backend::JscEngine::checkException(jscException);
return ret;
}
int32_t Local<Number>::toInt32() const { return static_cast<int32_t>(toDouble()); }
int64_t Local<Number>::toInt64() const { return static_cast<int64_t>(toDouble()); }
bool Local<Boolean>::value() const {
return JSValueToBoolean(jsc_backend::currentEngineContextChecked(), val_);
}
Local<Value> Local<Function>::callImpl(const script::Local<script::Value>& thiz, size_t size,
const Local<Value>* args) const {
JSValueRef jscException = nullptr;
auto context = jsc_backend::currentEngineContextChecked();
return jsc_backend::toJscValues<Local<Value>>(
context, size, args, [this, context, &thiz, size, &jscException](JSValueRef* array) {
Local<Value> ret(JSObjectCallAsFunction(
context, val_,
thiz.isObject() ? jsc_backend::JscEngine::toJsc(context, thiz.asObject()) : nullptr,
size, array, &jscException));
jsc_backend::JscEngine::checkException(jscException);
return ret;
});
}
size_t Local<Array>::size() const {
auto len = Local<Object>(val_).get("length");
if (!len.isNull()) return len.asNumber().toInt32();
return 0;
}
Local<Value> Local<Array>::get(size_t index) const {
JSValueRef jscException = nullptr;
Local<Value> ret(JSObjectGetPropertyAtIndex(jsc_backend::currentEngineContextChecked(), val_,
static_cast<unsigned>(index), &jscException));
jsc_backend::JscEngine::checkException(jscException);
return ret;
}
void Local<Array>::set(size_t index, const script::Local<script::Value>& value) const {
JSValueRef jscException = nullptr;
auto context = jsc_backend::currentEngineContextChecked();
JSObjectSetPropertyAtIndex(context, val_, static_cast<unsigned>(index),
jsc_backend::JscEngine::toJsc(context, value), &jscException);
jsc_backend::JscEngine::checkException(jscException);
}
void Local<Array>::add(const script::Local<script::Value>& value) const { set(size(), value); }
void Local<Array>::clear() const {
JSValueRef jscException = nullptr;
Local<Object>(val_).set("length", Number::newNumber(0));
jsc_backend::JscEngine::checkException(jscException);
}
namespace {
ByteBuffer::Type mapType(JSTypedArrayType type) {
switch (type) {
case kJSTypedArrayTypeInt8Array:
return ByteBuffer::Type::kInt8;
case kJSTypedArrayTypeUint8Array:
case kJSTypedArrayTypeUint8ClampedArray:
return ByteBuffer::Type::kUint8;
case kJSTypedArrayTypeInt16Array:
return ByteBuffer::Type::kInt16;
case kJSTypedArrayTypeUint16Array:
return ByteBuffer::Type::kUint16;
case kJSTypedArrayTypeInt32Array:
return ByteBuffer::Type::kInt32;
case kJSTypedArrayTypeUint32Array:
return ByteBuffer::Type::kUint32;
case kJSTypedArrayTypeFloat32Array:
return ByteBuffer::Type::kFloat32;
case kJSTypedArrayTypeFloat64Array:
return ByteBuffer::Type::kFloat64;
case kJSTypedArrayTypeArrayBuffer:
default:
return ByteBuffer::Type::kUnspecified;
}
}
} // namespace
static void assertLowLevelApiUsageOfByteBuffer(bool has) {
if (!has) {
throw std::runtime_error(
"Calling script::ByteBuffer related api on iOS-9/macOS-10.11 or lower is not supported for "
"now");
}
}
ByteBuffer::Type Local<ByteBuffer>::getType() const {
assertLowLevelApiUsageOfByteBuffer(jsc_backend::JscEngine::hasByteBufferAPI_);
JSValueRef jscException = nullptr;
auto type =
JSValueGetTypedArrayType(jsc_backend::currentEngineContextChecked(), val_, &jscException);
jsc_backend::JscEngine::checkException(jscException);
return mapType(type);
}
size_t Local<ByteBuffer>::byteLength() const {
assertLowLevelApiUsageOfByteBuffer(jsc_backend::JscEngine::hasByteBufferAPI_);
JSValueRef jscException = nullptr;
auto context = jsc_backend::currentEngineContextChecked();
auto type = JSValueGetTypedArrayType(context, val_, &jscException);
jsc_backend::JscEngine::checkException(jscException);
if (type == kJSTypedArrayTypeArrayBuffer) {
auto len = JSObjectGetArrayBufferByteLength(context, val_, &jscException);
jsc_backend::JscEngine::checkException(jscException);
return len;
} else {
auto len = JSObjectGetTypedArrayByteLength(context, val_, &jscException);
jsc_backend::JscEngine::checkException(jscException);
return len;
}
}
void* Local<ByteBuffer>::getRawBytes() const {
assertLowLevelApiUsageOfByteBuffer(jsc_backend::JscEngine::hasByteBufferAPI_);
auto context = jsc_backend::currentEngineContextChecked();
JSValueRef jscException = nullptr;
auto type = JSValueGetTypedArrayType(context, val_, &jscException);
jsc_backend::JscEngine::checkException(jscException);
if (type == kJSTypedArrayTypeArrayBuffer) {
auto ret = JSObjectGetArrayBufferBytesPtr(context, val_, &jscException);
jsc_backend::JscEngine::checkException(jscException);
return ret;
} else {
auto ret = JSObjectGetTypedArrayBytesPtr(context, val_, &jscException);
jsc_backend::JscEngine::checkException(jscException);
auto offset = JSObjectGetTypedArrayByteOffset(context, val_, &jscException);
jsc_backend::JscEngine::checkException(jscException);
// NOLINTNEXTLINE (cppcoreguidelines-pro-bounds-pointer-arithmetic)
return static_cast<uint8_t*>(ret) + offset;
}
}
std::shared_ptr<void> Local<ByteBuffer>::getRawBytesShared() const {
assertLowLevelApiUsageOfByteBuffer(jsc_backend::JscEngine::hasByteBufferAPI_);
auto ptr = getRawBytes();
auto engine = jsc_backend::currentEngine();
auto id = engine->keepReference(*this);
return std::shared_ptr<void>(ptr, [engine, id](void*) { engine->removeKeptReference(id); });
}
bool Local<ByteBuffer>::isShared() const { return true; }
void Local<ByteBuffer>::commit() const {}
void Local<ByteBuffer>::sync() const {}
} // namespace script