Value.cpp
10.6 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
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
// Copyright 2004-present Facebook. All Rights Reserved.
#include "Value.h"
#include <folly/json.h>
#include <folly/Conv.h>
#include "JSCHelpers.h"
#include "JavaScriptCore.h"
// See the comment under Value::fromDynamic()
#if !defined(__APPLE__) && defined(WITH_FB_JSC_TUNING)
#define USE_FAST_FOLLY_DYNAMIC_CONVERSION 1
#else
#define USE_FAST_FOLLY_DYNAMIC_CONVERSION 0
#endif
namespace facebook {
namespace react {
/* static */
Object Object::makeDate(JSContextRef ctx, Object::TimeType time) {
using std::chrono::duration_cast;
using std::chrono::milliseconds;
JSValueRef arguments[1];
arguments[0] = JSC_JSValueMakeNumber(
ctx,
duration_cast<milliseconds>(time.time_since_epoch()).count());
JSValueRef exn;
auto result = JSC_JSObjectMakeDate(ctx, 1, arguments, &exn);
if (!result) {
throw JSException(ctx, exn, "Failed to create Date");
}
return Object(ctx, result);
}
Object Object::makeArray(JSContextRef ctx, JSValueRef* elements, unsigned length) {
JSValueRef exn;
auto arr = JSC_JSObjectMakeArray(ctx, length, elements, &exn);
if (!arr) {
throw JSException(ctx, exn, "Failed to create an Array");
}
return Object(ctx, arr);
}
Value::Value(JSContextRef context, JSValueRef value)
: m_context(context), m_value(value) {}
Value::Value(JSContextRef context, JSStringRef str)
: m_context(context), m_value(JSC_JSValueMakeString(context, str)) {}
JSContextRef Value::context() const {
return m_context;
}
/* static */
std::string Value::toJSONString(unsigned indent) const {
JSValueRef exn;
auto stringToAdopt = JSC_JSValueCreateJSONString(m_context, m_value, indent, &exn);
if (!stringToAdopt) {
throw JSException(m_context, exn, "Exception creating JSON string");
}
return String::adopt(m_context, stringToAdopt).str();
}
/* static */
Value Value::fromJSON(const String& json) {
JSContextRef ctx = json.context();
auto result = JSC_JSValueMakeFromJSONString(ctx, json);
if (!result) {
throw JSException(folly::to<std::string>(
"Failed to create Value from JSON: ", json.str()).c_str());
}
return Value(ctx, result);
}
Value Value::fromDynamic(JSContextRef ctx, const folly::dynamic& value) {
// JavaScriptCore's iOS APIs have their own version of this direct conversion.
// In addition, using this requires exposing some of JSC's private APIs,
// so it's limited to non-apple platforms and to builds that use the custom JSC.
// Otherwise, we use the old way of converting through JSON.
#if USE_FAST_FOLLY_DYNAMIC_CONVERSION
// Defer GC during the creation of the JSValue, as we don't want
// intermediate objects to be collected.
// We could use JSValueProtect(), but it will make the process much slower.
JSDeferredGCRef deferGC = JSDeferGarbageCollection(ctx);
// Set a global lock for the whole process,
// instead of re-acquiring the lock for each operation.
JSLock(ctx);
JSValueRef jsVal = Value::fromDynamicInner(ctx, value);
JSUnlock(ctx);
JSResumeGarbageCollection(ctx, deferGC);
return Value(ctx, jsVal);
#else
auto json = folly::toJson(value);
return fromJSON(String(ctx, json.c_str()));
#endif
}
JSValueRef Value::fromDynamicInner(JSContextRef ctx, const folly::dynamic& obj) {
switch (obj.type()) {
// For primitive types (and strings), just create and return an equivalent JSValue
case folly::dynamic::Type::NULLT:
return JSC_JSValueMakeNull(ctx);
case folly::dynamic::Type::BOOL:
return JSC_JSValueMakeBoolean(ctx, obj.getBool());
case folly::dynamic::Type::DOUBLE:
return JSC_JSValueMakeNumber(ctx, obj.getDouble());
case folly::dynamic::Type::INT64:
return JSC_JSValueMakeNumber(ctx, obj.asDouble());
case folly::dynamic::Type::STRING:
return JSC_JSValueMakeString(ctx, String(ctx, obj.getString().c_str()));
case folly::dynamic::Type::ARRAY: {
// Collect JSValue for every element in the array
JSValueRef vals[obj.size()];
for (size_t i = 0; i < obj.size(); ++i) {
vals[i] = fromDynamicInner(ctx, obj[i]);
}
// Create a JSArray with the values
JSValueRef arr = JSC_JSObjectMakeArray(ctx, obj.size(), vals, nullptr);
return arr;
}
case folly::dynamic::Type::OBJECT: {
// Create an empty object
JSObjectRef jsObj = JSC_JSObjectMake(ctx, nullptr, nullptr);
// Create a JSValue for each of the object's children and set them in the object
for (auto it = obj.items().begin(); it != obj.items().end(); ++it) {
JSC_JSObjectSetProperty(
ctx,
jsObj,
String(ctx, it->first.asString().c_str()),
fromDynamicInner(ctx, it->second),
kJSPropertyAttributeNone,
nullptr);
}
return jsObj;
}
default:
// Assert not reached
LOG(FATAL) << "Trying to convert a folly object of unsupported type.";
return JSC_JSValueMakeNull(ctx);
}
}
Object Value::asObject() const {
JSValueRef exn;
JSObjectRef jsObj = JSC_JSValueToObject(context(), m_value, &exn);
if (!jsObj) {
throw JSException(m_context, exn, "Failed to convert to object");
}
return Object(context(), jsObj);
}
String Value::toString() const {
JSValueRef exn;
JSStringRef jsStr = JSC_JSValueToStringCopy(context(), m_value, &exn);
if (!jsStr) {
throw JSException(m_context, exn, "Failed to convert to string");
}
return String::adopt(context(), jsStr);
}
Value Value::makeError(JSContextRef ctx, const char *error, const char *stack)
{
auto errorMsg = Value(ctx, String(ctx, error));
JSValueRef args[] = {errorMsg};
if (stack) {
// Using this instead of JSObjectMakeError to actually get a stack property.
// MakeError only sets it stack when returning from the invoked function, so we
// can't extend it here.
auto errorConstructor = Object::getGlobalObject(ctx).getProperty("Error").asObject();
auto jsError = errorConstructor.callAsConstructor({errorMsg});
auto fullStack = std::string(stack) + jsError.getProperty("stack").toString().str();
jsError.setProperty("stack", String(ctx, fullStack.c_str()));
return jsError;
} else {
JSValueRef exn;
JSObjectRef errorObj = JSC_JSObjectMakeError(ctx, 1, args, &exn);
if (!errorObj) {
throw JSException(ctx, exn, "Exception making error");
}
return Value(ctx, errorObj);
}
}
void Value::throwTypeException(const std::string &expectedType) const {
std::string wat("TypeError: Expected ");
wat += expectedType;
wat += ", instead got '";
wat += toString().str();
wat += "'";
throw JSException(wat.c_str());
}
Object::operator Value() const {
return Value(m_context, m_obj);
}
Value Object::callAsFunction(std::initializer_list<JSValueRef> args) const {
return callAsFunction(nullptr, args.size(), args.begin());
}
Value Object::callAsFunction(const Object& thisObj, std::initializer_list<JSValueRef> args) const {
return callAsFunction((JSObjectRef)thisObj, args.size(), args.begin());
}
Value Object::callAsFunction(int nArgs, const JSValueRef args[]) const {
return callAsFunction(nullptr, nArgs, args);
}
Value Object::callAsFunction(const Object& thisObj, int nArgs, const JSValueRef args[]) const {
return callAsFunction(static_cast<JSObjectRef>(thisObj), nArgs, args);
}
Value Object::callAsFunction(JSObjectRef thisObj, int nArgs, const JSValueRef args[]) const {
JSValueRef exn;
JSValueRef result = JSC_JSObjectCallAsFunction(m_context, m_obj, thisObj, nArgs, args, &exn);
if (!result) {
throw JSException(m_context, exn, "Exception calling object as function");
}
return Value(m_context, result);
}
Object Object::callAsConstructor(std::initializer_list<JSValueRef> args) const {
JSValueRef exn;
JSObjectRef result = JSC_JSObjectCallAsConstructor(m_context, m_obj, args.size(), args.begin(), &exn);
if (!result) {
throw JSException(m_context, exn, "Exception calling object as constructor");
}
return Object(m_context, result);
}
Value Object::getProperty(const String& propName) const {
JSValueRef exn;
JSValueRef property = JSC_JSObjectGetProperty(m_context, m_obj, propName, &exn);
if (!property) {
throw JSException(m_context, exn, folly::to<std::string>(
"Failed to get property '", propName.str(), "'").c_str());
}
return Value(m_context, property);
}
Value Object::getPropertyAtIndex(unsigned int index) const {
JSValueRef exn;
JSValueRef property = JSC_JSObjectGetPropertyAtIndex(m_context, m_obj, index, &exn);
if (!property) {
throw JSException(m_context, exn, folly::to<std::string>(
"Failed to get property at index ", index).c_str());
}
return Value(m_context, property);
}
Value Object::getProperty(const char *propName) const {
return getProperty(String(m_context, propName));
}
void Object::setProperty(const String& propName, const Value& value) {
JSValueRef exn = nullptr;
JSC_JSObjectSetProperty(m_context, m_obj, propName, value, kJSPropertyAttributeNone, &exn);
if (exn) {
throw JSException(m_context, exn, folly::to<std::string>(
"Failed to set property '", propName.str(), "'").c_str());
}
}
void Object::setPropertyAtIndex(unsigned int index, const Value& value) {
JSValueRef exn = nullptr;
JSC_JSObjectSetPropertyAtIndex(m_context, m_obj, index, value, &exn);
if (exn) {
throw JSException(m_context, exn, folly::to<std::string>(
"Failed to set property at index ", index).c_str());
}
}
void Object::setProperty(const char *propName, const Value& value) {
setProperty(String(m_context, propName), value);
}
std::vector<String> Object::getPropertyNames() const {
auto namesRef = JSC_JSObjectCopyPropertyNames(m_context, m_obj);
size_t count = JSC_JSPropertyNameArrayGetCount(m_context, namesRef);
std::vector<String> names;
names.reserve(count);
for (size_t i = 0; i < count; i++) {
names.emplace_back(String::ref(m_context,
JSC_JSPropertyNameArrayGetNameAtIndex(m_context, namesRef, i)));
}
JSC_JSPropertyNameArrayRelease(m_context, namesRef);
return names;
}
std::unordered_map<std::string, std::string> Object::toJSONMap() const {
std::unordered_map<std::string, std::string> map;
auto namesRef = JSC_JSObjectCopyPropertyNames(m_context, m_obj);
size_t count = JSC_JSPropertyNameArrayGetCount(m_context, namesRef);
for (size_t i = 0; i < count; i++) {
auto key = String::ref(m_context,
JSC_JSPropertyNameArrayGetNameAtIndex(m_context, namesRef, i));
map.emplace(key.str(), getProperty(key).toJSONString());
}
JSC_JSPropertyNameArrayRelease(m_context, namesRef);
return map;
}
/* static */
Object Object::create(JSContextRef ctx) {
JSObjectRef newObj = JSC_JSObjectMake(
ctx,
NULL, // create instance of default object class
NULL); // no private data
return Object(ctx, newObj);
}
} }