Repository navigation
Expand file tree
/
Copy pathQuickJSValue.cpp
More file actions
149 lines (140 loc) · 5.95 KB
/
Copy pathQuickJSValue.cpp
File metadata and controls
149 lines (140 loc) · 5.95 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
#include "jsi/quickjs/QuickJSRuntime.h"
#ifdef TARGET_ENGINE_QUICKJS
namespace nativescript {
namespace engine {
Value HostObject::get(Runtime&, const PropNameID&) { return Value::undefined(); }
bool HostObject::set(Runtime&, const PropNameID&, const Value&) { return true; }
std::vector<PropNameID> HostObject::getPropertyNames(Runtime&) { return {}; }
// The defaults reproduce what the engine used to do for an index: stringify it
// and take the named path. A host object that does not override these is
// therefore unaffected by the indexed routing.
Value HostObject::getValueAtIndex(Runtime& runtime, uint32_t index) {
return get(runtime, PropNameID(std::to_string(index)));
}
bool HostObject::setValueAtIndex(Runtime& runtime, uint32_t index, const Value& value) {
return set(runtime, PropNameID(std::to_string(index)), value);
}
String::String(Runtime& runtime, JSValue value)
: storage_(std::make_shared<quickjsengine::ValueStorage>(
quickjsengine::ValueStorage::Kind::QuickJS)) {
storage_->context = runtime.context();
storage_->value = JS_DupValue(runtime.context(), value);
}
std::string String::utf8(Runtime& runtime) const {
JSValue value = local(runtime);
std::string result = quickjsengine::valueToUtf8(runtime.context(), value);
JS_FreeValue(runtime.context(), value);
return result;
}
JSValue String::local(Runtime& runtime) const {
return JS_DupValue(runtime.context(), storage_->value);
}
String::operator Value() const { return Value::fromStorage(storage_); }
Value::Value(Runtime&, const Object& object) {
storage_ = object.storage_;
kind_ = storage_ ? storage_->kind : quickjsengine::ValueStorage::Kind::Undefined;
}
Value::Value(Runtime&, const Function& function) {
storage_ = function.storage_;
kind_ = storage_ ? storage_->kind : quickjsengine::ValueStorage::Kind::Undefined;
}
Value::Value(Runtime&, const Array& array) {
storage_ = array.storage_;
kind_ = storage_ ? storage_->kind : quickjsengine::ValueStorage::Kind::Undefined;
}
Value::Value(Runtime&, const ArrayBuffer& arrayBuffer) {
storage_ = arrayBuffer.storage_;
kind_ = storage_ ? storage_->kind : quickjsengine::ValueStorage::Kind::Undefined;
}
Value::Value(Runtime&, const BigInt& bigint) {
storage_ = bigint.storage_;
kind_ = storage_ ? storage_->kind : quickjsengine::ValueStorage::Kind::Undefined;
}
Object Value::asObject(Runtime& runtime) const {
if (storage_) {
return Object::fromValueStorage(storage_);
}
// Promote to owned storage for Object.
auto s = std::make_shared<quickjsengine::ValueStorage>(kind_);
if (kind_ == quickjsengine::ValueStorage::Kind::QuickJSBorrowed) {
s->kind = quickjsengine::ValueStorage::Kind::QuickJS;
s->context = borrowedContext_;
s->value = JS_DupValue(borrowedContext_, borrowedValue_);
}
return Object::fromValueStorage(std::move(s));
}
Object Value::asObjectBorrowed(Runtime& runtime) const {
return asObject(runtime);
}
String Value::asString(Runtime& runtime) const {
JSValue value = local(runtime);
String result(runtime, value);
JS_FreeValue(runtime.context(), value);
return result;
}
std::string Value::utf8(Runtime& runtime) const {
// JS_ToCStringLen does not consume its argument, so a refcounted value can be
// read in place -- no local(), which dups, and no matching free.
if (kind_ == quickjsengine::ValueStorage::Kind::QuickJS ||
kind_ == quickjsengine::ValueStorage::Kind::QuickJSBorrowed) {
return quickjsengine::valueToUtf8(runtime.context(), jsValue());
}
JSValue value = local(runtime);
std::string result = quickjsengine::valueToUtf8(runtime.context(), value);
JS_FreeValue(runtime.context(), value);
return result;
}
Value Value::createStringFromUtf8(Runtime& runtime, const char* data, size_t length) {
Value result;
result.kind_ = quickjsengine::ValueStorage::Kind::QuickJS;
result.storage_ =
std::make_shared<quickjsengine::ValueStorage>(quickjsengine::ValueStorage::Kind::QuickJS);
result.storage_->context = runtime.context();
result.storage_->value =
JS_NewStringLen(runtime.context(), data != nullptr ? data : "", length);
return result;
}
BigInt Value::getBigInt(Runtime& runtime) const {
JSValue value = local(runtime);
BigInt result(runtime, value);
JS_FreeValue(runtime.context(), value);
return result;
}
Function Object::getPropertyAsFunction(Runtime& runtime, const char* name) const {
return getProperty(runtime, name).asObject(runtime).asFunction(runtime);
}
Function Object::asFunction(Runtime&) const { return Function(*this); }
Array Object::getArray(Runtime&) const { return Array(*this); }
ArrayBuffer Object::getArrayBuffer(Runtime&) const { return ArrayBuffer(*this); }
Array Object::getPropertyNames(Runtime& runtime) const {
JSValue object = local(runtime);
JSPropertyEnum* properties = nullptr;
uint32_t count = 0;
int status = JS_GetOwnPropertyNames(runtime.context(), &properties, &count, object,
JS_GPN_STRING_MASK | JS_GPN_SYMBOL_MASK | JS_GPN_ENUM_ONLY);
JS_FreeValue(runtime.context(), object);
if (status < 0) {
throw quickjsengine::caughtError(runtime, "QuickJS property names failed.");
}
Array result(runtime, count);
for (uint32_t i = 0; i < count; i++) {
JSValue nameValue = JS_AtomToValue(runtime.context(), properties[i].atom);
result.setValueAtIndex(runtime, i, Value(runtime, nameValue));
JS_FreeValue(runtime.context(), nameValue);
JS_FreeAtom(runtime.context(), properties[i].atom);
}
js_free(runtime.context(), properties);
return result;
}
void Object::setProperty(Runtime& runtime, const char* name, const Function& value) {
setProperty(runtime, name, Value(runtime, value));
}
void Object::setProperty(Runtime& runtime, const char* name, const Array& value) {
setProperty(runtime, name, Value(runtime, value));
}
void Object::setProperty(Runtime& runtime, const char* name, const ArrayBuffer& value) {
setProperty(runtime, name, Value(runtime, value));
}
} // namespace engine
} // namespace nativescript
#endif // TARGET_ENGINE_QUICKJS