diff --git a/ReactCommon/jschelpers/JSCWrapper.h b/ReactCommon/jschelpers/JSCWrapper.h index 97fb661f7..aa4053784 100644 --- a/ReactCommon/jschelpers/JSCWrapper.h +++ b/ReactCommon/jschelpers/JSCWrapper.h @@ -101,6 +101,7 @@ struct JSCWrapper { JSC_WRAPPER_METHOD(JSObjectMakeFunctionWithCallback); JSC_WRAPPER_METHOD(JSObjectSetPrivate); JSC_WRAPPER_METHOD(JSObjectSetProperty); + JSC_WRAPPER_METHOD(JSObjectSetPropertyAtIndex); // JSPropertyNameArray JSC_WRAPPER_METHOD(JSObjectCopyPropertyNames); diff --git a/ReactCommon/jschelpers/JavaScriptCore.h b/ReactCommon/jschelpers/JavaScriptCore.h index f0e4423ff..737cf828c 100644 --- a/ReactCommon/jschelpers/JavaScriptCore.h +++ b/ReactCommon/jschelpers/JavaScriptCore.h @@ -146,6 +146,7 @@ jsc_poison(JSClassCreate JSClassRelease JSClassRetain) #define JSC_JSObjectMakeFunctionWithCallback(...) __jsc_wrapper(JSObjectMakeFunctionWithCallback, __VA_ARGS__) #define JSC_JSObjectSetPrivate(...) __jsc_bool_wrapper(JSObjectSetPrivate, __VA_ARGS__) #define JSC_JSObjectSetProperty(...) __jsc_wrapper(JSObjectSetProperty, __VA_ARGS__) +#define JSC_JSObjectSetPropertyAtIndex(...) __jsc_wrapper(JSObjectSetPropertyAtIndex, __VA_ARGS__) jsc_poison(JSObjectCallAsConstructor JSObjectCallAsFunction JSObjectDeleteProperty JSObjectGetPrivate JSObjectGetProperty JSObjectGetPropertyAtIndex diff --git a/ReactCommon/jschelpers/Value.cpp b/ReactCommon/jschelpers/Value.cpp index b228da367..7f00ea973 100644 --- a/ReactCommon/jschelpers/Value.cpp +++ b/ReactCommon/jschelpers/Value.cpp @@ -59,7 +59,7 @@ Value Value::fromJSON(JSContextRef ctx, const String& json) { return Value(ctx, result); } -JSValueRef Value::fromDynamic(JSContextRef ctx, const folly::dynamic& value) { +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. @@ -75,7 +75,7 @@ JSValueRef Value::fromDynamic(JSContextRef ctx, const folly::dynamic& value) { JSValueRef jsVal = Value::fromDynamicInner(ctx, value); JSUnlock(ctx); JSResumeGarbageCollection(ctx, deferGC); - return jsVal; + return Value(ctx, jsVal); #else auto json = folly::toJson(value); return fromJSON(ctx, String(ctx, json.c_str())); @@ -140,9 +140,7 @@ Object Value::asObject() { std::string exceptionText = Value(m_context, exn).toString().str(); throwJSExecutionException("Failed to convert to object: %s", exceptionText.c_str()); } - Object ret = Object(context(), jsObj); - m_value = nullptr; - return ret; + return Object(context(), jsObj); } Value Value::makeError(JSContextRef ctx, const char *error) @@ -207,7 +205,7 @@ Value Object::getProperty(const String& propName) const { return Value(m_context, property); } -Value Object::getPropertyAtIndex(unsigned index) const { +Value Object::getPropertyAtIndex(unsigned int index) const { JSValueRef exn; JSValueRef property = JSC_JSObjectGetPropertyAtIndex(m_context, m_obj, index, &exn); if (!property) { @@ -221,8 +219,8 @@ Value Object::getProperty(const char *propName) const { return getProperty(String(m_context, propName)); } -void Object::setProperty(const String& propName, const Value& value) const { - JSValueRef exn = NULL; +void Object::setProperty(const String& propName, const Value& value) { + JSValueRef exn = nullptr; JSC_JSObjectSetProperty(m_context, m_obj, propName, value, kJSPropertyAttributeNone, &exn); if (exn) { std::string exceptionText = Value(m_context, exn).toString().str(); @@ -230,7 +228,16 @@ void Object::setProperty(const String& propName, const Value& value) const { } } -void Object::setProperty(const char *propName, const Value& value) const { +void Object::setPropertyAtIndex(unsigned int index, const Value& value) { + JSValueRef exn = nullptr; + JSC_JSObjectSetPropertyAtIndex(m_context, m_obj, index, value, &exn); + if (exn) { + std::string exceptionText = Value(m_context, exn).toString().str(); + throwJSExecutionException("Failed to set property: %s", exceptionText.c_str()); + } +} + +void Object::setProperty(const char *propName, const Value& value) { setProperty(String(m_context, propName), value); } diff --git a/ReactCommon/jschelpers/Value.h b/ReactCommon/jschelpers/Value.h index ad9e81e04..d90cedee3 100644 --- a/ReactCommon/jschelpers/Value.h +++ b/ReactCommon/jschelpers/Value.h @@ -201,9 +201,10 @@ public: Value getProperty(const String& propName) const; Value getProperty(const char *propName) const; - Value getPropertyAtIndex(unsigned index) const; - void setProperty(const String& propName, const Value& value) const; - void setProperty(const char *propName, const Value& value) const; + Value getPropertyAtIndex(unsigned int index) const; + void setProperty(const String& propName, const Value& value); + void setProperty(const char *propName, const Value& value); + void setPropertyAtIndex(unsigned int index, const Value& value); std::vector getPropertyNames() const; std::unordered_map toJSONMap() const; @@ -334,7 +335,7 @@ public: __attribute__((visibility("default"))) std::string toJSONString(unsigned indent = 0) const; __attribute__((visibility("default"))) static Value fromJSON(JSContextRef ctx, const String& json); - __attribute__((visibility("default"))) static JSValueRef fromDynamic(JSContextRef ctx, const folly::dynamic& value); + __attribute__((visibility("default"))) static Value fromDynamic(JSContextRef ctx, const folly::dynamic& value); __attribute__((visibility("default"))) JSContextRef context() const; protected: JSContextRef m_context; diff --git a/ReactCommon/jschelpers/systemJSCWrapper.cpp b/ReactCommon/jschelpers/systemJSCWrapper.cpp index a36124838..142726bcd 100644 --- a/ReactCommon/jschelpers/systemJSCWrapper.cpp +++ b/ReactCommon/jschelpers/systemJSCWrapper.cpp @@ -93,6 +93,7 @@ const JSCWrapper* systemJSCWrapper() { .JSObjectMakeFunctionWithCallback = JSObjectMakeFunctionWithCallback, .JSObjectSetPrivate = JSObjectSetPrivate, .JSObjectSetProperty = JSObjectSetProperty, + .JSObjectSetPropertyAtIndex = JSObjectSetPropertyAtIndex, .JSObjectCopyPropertyNames = JSObjectCopyPropertyNames, .JSPropertyNameArrayGetCount = JSPropertyNameArrayGetCount,