Add setPropertyAtIndex

Reviewed By: mhorowitz

Differential Revision: D5069708

fbshipit-source-id: f81542f9d752ff53acf3fb9d96bf71e60acecac3
This commit is contained in:
Pieter De Baets 2017-05-19 07:05:33 -07:00 committed by Facebook Github Bot
parent eb9e4d4d11
commit f98518c5f9
5 changed files with 24 additions and 13 deletions

View File

@ -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);

View File

@ -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

View File

@ -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);
}

View File

@ -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<String> getPropertyNames() const;
std::unordered_map<std::string, std::string> 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;

View File

@ -93,6 +93,7 @@ const JSCWrapper* systemJSCWrapper() {
.JSObjectMakeFunctionWithCallback = JSObjectMakeFunctionWithCallback,
.JSObjectSetPrivate = JSObjectSetPrivate,
.JSObjectSetProperty = JSObjectSetProperty,
.JSObjectSetPropertyAtIndex = JSObjectSetPropertyAtIndex,
.JSObjectCopyPropertyNames = JSObjectCopyPropertyNames,
.JSPropertyNameArrayGetCount = JSPropertyNameArrayGetCount,