2018-09-11 22:27:47 +00:00
|
|
|
// Copyright (c) Facebook, Inc. and its affiliates.
|
2018-05-31 22:31:03 +00:00
|
|
|
|
|
|
|
// This source code is licensed under the MIT license found in the
|
|
|
|
// LICENSE file in the root directory of this source tree.
|
2016-05-04 02:29:58 +00:00
|
|
|
|
|
|
|
#include "Value.h"
|
|
|
|
|
2017-05-30 21:56:11 +00:00
|
|
|
#include <folly/json.h>
|
|
|
|
#include <folly/Conv.h>
|
|
|
|
|
2016-05-04 02:29:58 +00:00
|
|
|
#include "JSCHelpers.h"
|
2016-11-22 14:05:36 +00:00
|
|
|
#include "JavaScriptCore.h"
|
2016-05-04 02:29:58 +00:00
|
|
|
|
2016-07-20 11:25:53 +00:00
|
|
|
// See the comment under Value::fromDynamic()
|
2016-08-10 20:05:24 +00:00
|
|
|
#if !defined(__APPLE__) && defined(WITH_FB_JSC_TUNING)
|
|
|
|
#define USE_FAST_FOLLY_DYNAMIC_CONVERSION 1
|
|
|
|
#else
|
|
|
|
#define USE_FAST_FOLLY_DYNAMIC_CONVERSION 0
|
|
|
|
#endif
|
2016-07-20 11:25:53 +00:00
|
|
|
|
2016-05-04 02:29:58 +00:00
|
|
|
namespace facebook {
|
|
|
|
namespace react {
|
|
|
|
|
2017-09-19 01:35:48 +00:00
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
|
2017-05-26 10:43:20 +00:00
|
|
|
Value::Value(JSContextRef context, JSValueRef value)
|
|
|
|
: m_context(context), m_value(value) {}
|
2016-05-04 02:29:58 +00:00
|
|
|
|
2017-05-26 10:43:20 +00:00
|
|
|
Value::Value(JSContextRef context, JSStringRef str)
|
|
|
|
: m_context(context), m_value(JSC_JSValueMakeString(context, str)) {}
|
2016-05-04 02:29:58 +00:00
|
|
|
|
|
|
|
JSContextRef Value::context() const {
|
|
|
|
return m_context;
|
|
|
|
}
|
|
|
|
|
2017-09-19 01:35:48 +00:00
|
|
|
/* static */
|
2016-07-22 00:32:42 +00:00
|
|
|
std::string Value::toJSONString(unsigned indent) const {
|
2016-05-04 02:29:58 +00:00
|
|
|
JSValueRef exn;
|
2016-11-22 14:05:38 +00:00
|
|
|
auto stringToAdopt = JSC_JSValueCreateJSONString(m_context, m_value, indent, &exn);
|
2017-05-26 10:43:20 +00:00
|
|
|
if (!stringToAdopt) {
|
|
|
|
throw JSException(m_context, exn, "Exception creating JSON string");
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
2016-11-18 14:25:29 +00:00
|
|
|
return String::adopt(m_context, stringToAdopt).str();
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
2017-06-06 10:04:56 +00:00
|
|
|
Value Value::fromJSON(const String& json) {
|
|
|
|
JSContextRef ctx = json.context();
|
2016-11-22 14:05:38 +00:00
|
|
|
auto result = JSC_JSValueMakeFromJSONString(ctx, json);
|
2016-05-04 02:29:58 +00:00
|
|
|
if (!result) {
|
2017-05-30 21:56:11 +00:00
|
|
|
throw JSException(folly::to<std::string>(
|
|
|
|
"Failed to create Value from JSON: ", json.str()).c_str());
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
return Value(ctx, result);
|
|
|
|
}
|
|
|
|
|
2017-05-19 14:05:33 +00:00
|
|
|
Value Value::fromDynamic(JSContextRef ctx, const folly::dynamic& value) {
|
2016-07-20 11:25:53 +00:00
|
|
|
// 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);
|
2017-05-19 14:05:33 +00:00
|
|
|
return Value(ctx, jsVal);
|
2016-07-20 11:25:53 +00:00
|
|
|
#else
|
2016-06-21 17:08:31 +00:00
|
|
|
auto json = folly::toJson(value);
|
2017-06-06 10:04:56 +00:00
|
|
|
return fromJSON(String(ctx, json.c_str()));
|
2016-07-20 11:25:53 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
JSValueRef Value::fromDynamicInner(JSContextRef ctx, const folly::dynamic& obj) {
|
|
|
|
switch (obj.type()) {
|
2017-01-31 13:17:33 +00:00
|
|
|
// For primitive types (and strings), just create and return an equivalent JSValue
|
2016-07-20 11:25:53 +00:00
|
|
|
case folly::dynamic::Type::NULLT:
|
2016-11-22 14:05:38 +00:00
|
|
|
return JSC_JSValueMakeNull(ctx);
|
2016-07-20 11:25:53 +00:00
|
|
|
|
|
|
|
case folly::dynamic::Type::BOOL:
|
2016-11-22 14:05:38 +00:00
|
|
|
return JSC_JSValueMakeBoolean(ctx, obj.getBool());
|
2016-07-20 11:25:53 +00:00
|
|
|
|
|
|
|
case folly::dynamic::Type::DOUBLE:
|
2016-11-22 14:05:38 +00:00
|
|
|
return JSC_JSValueMakeNumber(ctx, obj.getDouble());
|
2016-07-20 11:25:53 +00:00
|
|
|
|
|
|
|
case folly::dynamic::Type::INT64:
|
2016-11-22 14:05:38 +00:00
|
|
|
return JSC_JSValueMakeNumber(ctx, obj.asDouble());
|
2016-07-20 11:25:53 +00:00
|
|
|
|
|
|
|
case folly::dynamic::Type::STRING:
|
2016-11-22 14:05:38 +00:00
|
|
|
return JSC_JSValueMakeString(ctx, String(ctx, obj.getString().c_str()));
|
2016-07-20 11:25:53 +00:00
|
|
|
|
|
|
|
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
|
2016-11-22 14:05:38 +00:00
|
|
|
JSValueRef arr = JSC_JSObjectMakeArray(ctx, obj.size(), vals, nullptr);
|
2016-07-20 11:25:53 +00:00
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
case folly::dynamic::Type::OBJECT: {
|
|
|
|
// Create an empty object
|
2016-11-22 14:05:38 +00:00
|
|
|
JSObjectRef jsObj = JSC_JSObjectMake(ctx, nullptr, nullptr);
|
2016-07-20 11:25:53 +00:00
|
|
|
// 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) {
|
2016-11-22 14:05:38 +00:00
|
|
|
JSC_JSObjectSetProperty(
|
2016-07-20 11:25:53 +00:00
|
|
|
ctx,
|
|
|
|
jsObj,
|
2016-11-18 14:25:29 +00:00
|
|
|
String(ctx, it->first.asString().c_str()),
|
2016-07-20 11:25:53 +00:00
|
|
|
fromDynamicInner(ctx, it->second),
|
|
|
|
kJSPropertyAttributeNone,
|
|
|
|
nullptr);
|
|
|
|
}
|
|
|
|
return jsObj;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
// Assert not reached
|
|
|
|
LOG(FATAL) << "Trying to convert a folly object of unsupported type.";
|
2016-11-22 14:05:38 +00:00
|
|
|
return JSC_JSValueMakeNull(ctx);
|
2016-07-20 11:25:53 +00:00
|
|
|
}
|
2016-06-21 17:08:31 +00:00
|
|
|
}
|
|
|
|
|
2017-05-26 10:43:20 +00:00
|
|
|
Object Value::asObject() const {
|
2016-05-04 02:29:58 +00:00
|
|
|
JSValueRef exn;
|
2016-11-22 14:05:38 +00:00
|
|
|
JSObjectRef jsObj = JSC_JSValueToObject(context(), m_value, &exn);
|
2016-05-04 02:29:58 +00:00
|
|
|
if (!jsObj) {
|
2017-05-26 10:43:20 +00:00
|
|
|
throw JSException(m_context, exn, "Failed to convert to object");
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
2017-05-19 14:05:33 +00:00
|
|
|
return Object(context(), jsObj);
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
2017-05-26 10:43:20 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-06-14 16:16:26 +00:00
|
|
|
Value Value::makeError(JSContextRef ctx, const char *error, const char *stack)
|
2016-11-18 14:25:24 +00:00
|
|
|
{
|
2017-06-14 16:16:26 +00:00
|
|
|
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);
|
2016-11-18 14:25:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-11 22:11:00 +00:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2016-05-04 02:29:58 +00:00
|
|
|
Object::operator Value() const {
|
|
|
|
return Value(m_context, m_obj);
|
|
|
|
}
|
|
|
|
|
2016-06-21 17:08:31 +00:00
|
|
|
Value Object::callAsFunction(std::initializer_list<JSValueRef> args) const {
|
2016-07-06 13:30:25 +00:00
|
|
|
return callAsFunction(nullptr, args.size(), args.begin());
|
|
|
|
}
|
|
|
|
|
|
|
|
Value Object::callAsFunction(const Object& thisObj, std::initializer_list<JSValueRef> args) const {
|
2016-11-18 14:25:24 +00:00
|
|
|
return callAsFunction((JSObjectRef)thisObj, args.size(), args.begin());
|
2016-06-21 17:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Value Object::callAsFunction(int nArgs, const JSValueRef args[]) const {
|
2016-07-06 13:30:25 +00:00
|
|
|
return callAsFunction(nullptr, nArgs, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value Object::callAsFunction(const Object& thisObj, int nArgs, const JSValueRef args[]) const {
|
2016-11-18 14:25:24 +00:00
|
|
|
return callAsFunction(static_cast<JSObjectRef>(thisObj), nArgs, args);
|
2016-07-06 13:30:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Value Object::callAsFunction(JSObjectRef thisObj, int nArgs, const JSValueRef args[]) const {
|
2016-05-04 02:29:58 +00:00
|
|
|
JSValueRef exn;
|
2016-11-22 14:05:38 +00:00
|
|
|
JSValueRef result = JSC_JSObjectCallAsFunction(m_context, m_obj, thisObj, nArgs, args, &exn);
|
2016-05-04 02:29:58 +00:00
|
|
|
if (!result) {
|
2017-05-26 10:43:20 +00:00
|
|
|
throw JSException(m_context, exn, "Exception calling object as function");
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
return Value(m_context, result);
|
|
|
|
}
|
|
|
|
|
2016-07-06 13:30:25 +00:00
|
|
|
Object Object::callAsConstructor(std::initializer_list<JSValueRef> args) const {
|
|
|
|
JSValueRef exn;
|
2016-11-22 14:05:38 +00:00
|
|
|
JSObjectRef result = JSC_JSObjectCallAsConstructor(m_context, m_obj, args.size(), args.begin(), &exn);
|
2016-07-06 13:30:25 +00:00
|
|
|
if (!result) {
|
2017-05-26 10:43:20 +00:00
|
|
|
throw JSException(m_context, exn, "Exception calling object as constructor");
|
2016-07-06 13:30:25 +00:00
|
|
|
}
|
|
|
|
return Object(m_context, result);
|
|
|
|
}
|
|
|
|
|
2016-05-04 02:29:58 +00:00
|
|
|
Value Object::getProperty(const String& propName) const {
|
|
|
|
JSValueRef exn;
|
2016-11-22 14:05:38 +00:00
|
|
|
JSValueRef property = JSC_JSObjectGetProperty(m_context, m_obj, propName, &exn);
|
2016-05-04 02:29:58 +00:00
|
|
|
if (!property) {
|
2017-05-30 21:56:11 +00:00
|
|
|
throw JSException(m_context, exn, folly::to<std::string>(
|
|
|
|
"Failed to get property '", propName.str(), "'").c_str());
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
return Value(m_context, property);
|
|
|
|
}
|
|
|
|
|
2017-05-19 14:05:33 +00:00
|
|
|
Value Object::getPropertyAtIndex(unsigned int index) const {
|
2016-05-04 02:29:58 +00:00
|
|
|
JSValueRef exn;
|
2016-11-22 14:05:38 +00:00
|
|
|
JSValueRef property = JSC_JSObjectGetPropertyAtIndex(m_context, m_obj, index, &exn);
|
2016-05-04 02:29:58 +00:00
|
|
|
if (!property) {
|
2017-05-30 21:56:11 +00:00
|
|
|
throw JSException(m_context, exn, folly::to<std::string>(
|
|
|
|
"Failed to get property at index ", index).c_str());
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
return Value(m_context, property);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value Object::getProperty(const char *propName) const {
|
2016-11-18 14:25:29 +00:00
|
|
|
return getProperty(String(m_context, propName));
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
2017-05-19 14:05:33 +00:00
|
|
|
void Object::setProperty(const String& propName, const Value& value) {
|
|
|
|
JSValueRef exn = nullptr;
|
2016-11-22 14:05:38 +00:00
|
|
|
JSC_JSObjectSetProperty(m_context, m_obj, propName, value, kJSPropertyAttributeNone, &exn);
|
2016-05-04 02:29:58 +00:00
|
|
|
if (exn) {
|
2017-05-30 21:56:11 +00:00
|
|
|
throw JSException(m_context, exn, folly::to<std::string>(
|
|
|
|
"Failed to set property '", propName.str(), "'").c_str());
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-19 14:05:33 +00:00
|
|
|
void Object::setPropertyAtIndex(unsigned int index, const Value& value) {
|
|
|
|
JSValueRef exn = nullptr;
|
|
|
|
JSC_JSObjectSetPropertyAtIndex(m_context, m_obj, index, value, &exn);
|
|
|
|
if (exn) {
|
2017-05-30 21:56:11 +00:00
|
|
|
throw JSException(m_context, exn, folly::to<std::string>(
|
|
|
|
"Failed to set property at index ", index).c_str());
|
2017-05-19 14:05:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Object::setProperty(const char *propName, const Value& value) {
|
2016-11-18 14:25:29 +00:00
|
|
|
setProperty(String(m_context, propName), value);
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
2016-07-06 13:30:25 +00:00
|
|
|
std::vector<String> Object::getPropertyNames() const {
|
2016-11-22 14:05:38 +00:00
|
|
|
auto namesRef = JSC_JSObjectCopyPropertyNames(m_context, m_obj);
|
|
|
|
size_t count = JSC_JSPropertyNameArrayGetCount(m_context, namesRef);
|
2016-07-06 13:30:25 +00:00
|
|
|
std::vector<String> names;
|
|
|
|
names.reserve(count);
|
2016-05-04 02:29:58 +00:00
|
|
|
for (size_t i = 0; i < count; i++) {
|
2016-11-22 14:05:38 +00:00
|
|
|
names.emplace_back(String::ref(m_context,
|
|
|
|
JSC_JSPropertyNameArrayGetNameAtIndex(m_context, namesRef, i)));
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
2016-11-22 14:05:38 +00:00
|
|
|
JSC_JSPropertyNameArrayRelease(m_context, namesRef);
|
2016-05-04 02:29:58 +00:00
|
|
|
return names;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unordered_map<std::string, std::string> Object::toJSONMap() const {
|
|
|
|
std::unordered_map<std::string, std::string> map;
|
2016-11-22 14:05:38 +00:00
|
|
|
auto namesRef = JSC_JSObjectCopyPropertyNames(m_context, m_obj);
|
|
|
|
size_t count = JSC_JSPropertyNameArrayGetCount(m_context, namesRef);
|
2016-05-04 02:29:58 +00:00
|
|
|
for (size_t i = 0; i < count; i++) {
|
2016-11-22 14:05:38 +00:00
|
|
|
auto key = String::ref(m_context,
|
|
|
|
JSC_JSPropertyNameArrayGetNameAtIndex(m_context, namesRef, i));
|
2016-05-04 02:29:58 +00:00
|
|
|
map.emplace(key.str(), getProperty(key).toJSONString());
|
|
|
|
}
|
2016-11-22 14:05:38 +00:00
|
|
|
JSC_JSPropertyNameArrayRelease(m_context, namesRef);
|
2016-05-04 02:29:58 +00:00
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
Object Object::create(JSContextRef ctx) {
|
2016-11-22 14:05:38 +00:00
|
|
|
JSObjectRef newObj = JSC_JSObjectMake(
|
2016-05-04 02:29:58 +00:00
|
|
|
ctx,
|
|
|
|
NULL, // create instance of default object class
|
|
|
|
NULL); // no private data
|
|
|
|
return Object(ctx, newObj);
|
|
|
|
}
|
|
|
|
|
|
|
|
} }
|