Skip to content

Commit f98518c

Browse files
javachefacebook-github-bot
authored andcommitted
Add setPropertyAtIndex
Reviewed By: mhorowitz Differential Revision: D5069708 fbshipit-source-id: f81542f9d752ff53acf3fb9d96bf71e60acecac3
1 parent eb9e4d4 commit f98518c

File tree

5 files changed

+24
-13
lines changed

5 files changed

+24
-13
lines changed

ReactCommon/jschelpers/JSCWrapper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ struct JSCWrapper {
101101
JSC_WRAPPER_METHOD(JSObjectMakeFunctionWithCallback);
102102
JSC_WRAPPER_METHOD(JSObjectSetPrivate);
103103
JSC_WRAPPER_METHOD(JSObjectSetProperty);
104+
JSC_WRAPPER_METHOD(JSObjectSetPropertyAtIndex);
104105

105106
// JSPropertyNameArray
106107
JSC_WRAPPER_METHOD(JSObjectCopyPropertyNames);

ReactCommon/jschelpers/JavaScriptCore.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ jsc_poison(JSClassCreate JSClassRelease JSClassRetain)
146146
#define JSC_JSObjectMakeFunctionWithCallback(...) __jsc_wrapper(JSObjectMakeFunctionWithCallback, __VA_ARGS__)
147147
#define JSC_JSObjectSetPrivate(...) __jsc_bool_wrapper(JSObjectSetPrivate, __VA_ARGS__)
148148
#define JSC_JSObjectSetProperty(...) __jsc_wrapper(JSObjectSetProperty, __VA_ARGS__)
149+
#define JSC_JSObjectSetPropertyAtIndex(...) __jsc_wrapper(JSObjectSetPropertyAtIndex, __VA_ARGS__)
149150

150151
jsc_poison(JSObjectCallAsConstructor JSObjectCallAsFunction JSObjectDeleteProperty
151152
JSObjectGetPrivate JSObjectGetProperty JSObjectGetPropertyAtIndex

ReactCommon/jschelpers/Value.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Value Value::fromJSON(JSContextRef ctx, const String& json) {
5959
return Value(ctx, result);
6060
}
6161

62-
JSValueRef Value::fromDynamic(JSContextRef ctx, const folly::dynamic& value) {
62+
Value Value::fromDynamic(JSContextRef ctx, const folly::dynamic& value) {
6363
// JavaScriptCore's iOS APIs have their own version of this direct conversion.
6464
// In addition, using this requires exposing some of JSC's private APIs,
6565
// 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) {
7575
JSValueRef jsVal = Value::fromDynamicInner(ctx, value);
7676
JSUnlock(ctx);
7777
JSResumeGarbageCollection(ctx, deferGC);
78-
return jsVal;
78+
return Value(ctx, jsVal);
7979
#else
8080
auto json = folly::toJson(value);
8181
return fromJSON(ctx, String(ctx, json.c_str()));
@@ -140,9 +140,7 @@ Object Value::asObject() {
140140
std::string exceptionText = Value(m_context, exn).toString().str();
141141
throwJSExecutionException("Failed to convert to object: %s", exceptionText.c_str());
142142
}
143-
Object ret = Object(context(), jsObj);
144-
m_value = nullptr;
145-
return ret;
143+
return Object(context(), jsObj);
146144
}
147145

148146
Value Value::makeError(JSContextRef ctx, const char *error)
@@ -207,7 +205,7 @@ Value Object::getProperty(const String& propName) const {
207205
return Value(m_context, property);
208206
}
209207

210-
Value Object::getPropertyAtIndex(unsigned index) const {
208+
Value Object::getPropertyAtIndex(unsigned int index) const {
211209
JSValueRef exn;
212210
JSValueRef property = JSC_JSObjectGetPropertyAtIndex(m_context, m_obj, index, &exn);
213211
if (!property) {
@@ -221,16 +219,25 @@ Value Object::getProperty(const char *propName) const {
221219
return getProperty(String(m_context, propName));
222220
}
223221

224-
void Object::setProperty(const String& propName, const Value& value) const {
225-
JSValueRef exn = NULL;
222+
void Object::setProperty(const String& propName, const Value& value) {
223+
JSValueRef exn = nullptr;
226224
JSC_JSObjectSetProperty(m_context, m_obj, propName, value, kJSPropertyAttributeNone, &exn);
227225
if (exn) {
228226
std::string exceptionText = Value(m_context, exn).toString().str();
229227
throwJSExecutionException("Failed to set property: %s", exceptionText.c_str());
230228
}
231229
}
232230

233-
void Object::setProperty(const char *propName, const Value& value) const {
231+
void Object::setPropertyAtIndex(unsigned int index, const Value& value) {
232+
JSValueRef exn = nullptr;
233+
JSC_JSObjectSetPropertyAtIndex(m_context, m_obj, index, value, &exn);
234+
if (exn) {
235+
std::string exceptionText = Value(m_context, exn).toString().str();
236+
throwJSExecutionException("Failed to set property: %s", exceptionText.c_str());
237+
}
238+
}
239+
240+
void Object::setProperty(const char *propName, const Value& value) {
234241
setProperty(String(m_context, propName), value);
235242
}
236243

ReactCommon/jschelpers/Value.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,10 @@ class Object : public noncopyable {
201201

202202
Value getProperty(const String& propName) const;
203203
Value getProperty(const char *propName) const;
204-
Value getPropertyAtIndex(unsigned index) const;
205-
void setProperty(const String& propName, const Value& value) const;
206-
void setProperty(const char *propName, const Value& value) const;
204+
Value getPropertyAtIndex(unsigned int index) const;
205+
void setProperty(const String& propName, const Value& value);
206+
void setProperty(const char *propName, const Value& value);
207+
void setPropertyAtIndex(unsigned int index, const Value& value);
207208
std::vector<String> getPropertyNames() const;
208209
std::unordered_map<std::string, std::string> toJSONMap() const;
209210

@@ -334,7 +335,7 @@ class Value : public noncopyable {
334335

335336
__attribute__((visibility("default"))) std::string toJSONString(unsigned indent = 0) const;
336337
__attribute__((visibility("default"))) static Value fromJSON(JSContextRef ctx, const String& json);
337-
__attribute__((visibility("default"))) static JSValueRef fromDynamic(JSContextRef ctx, const folly::dynamic& value);
338+
__attribute__((visibility("default"))) static Value fromDynamic(JSContextRef ctx, const folly::dynamic& value);
338339
__attribute__((visibility("default"))) JSContextRef context() const;
339340
protected:
340341
JSContextRef m_context;

ReactCommon/jschelpers/systemJSCWrapper.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ const JSCWrapper* systemJSCWrapper() {
9393
.JSObjectMakeFunctionWithCallback = JSObjectMakeFunctionWithCallback,
9494
.JSObjectSetPrivate = JSObjectSetPrivate,
9595
.JSObjectSetProperty = JSObjectSetProperty,
96+
.JSObjectSetPropertyAtIndex = JSObjectSetPropertyAtIndex,
9697

9798
.JSObjectCopyPropertyNames = JSObjectCopyPropertyNames,
9899
.JSPropertyNameArrayGetCount = JSPropertyNameArrayGetCount,

0 commit comments

Comments
 (0)