diff --git a/ReactAndroid/src/main/jni/react/Value.cpp b/ReactAndroid/src/main/jni/react/Value.cpp index ac07c486f..6c1c393de 100644 --- a/ReactAndroid/src/main/jni/react/Value.cpp +++ b/ReactAndroid/src/main/jni/react/Value.cpp @@ -79,6 +79,16 @@ Value Object::getProperty(const String& propName) const { return Value(m_context, property); } +Value Object::getPropertyAtIndex(unsigned index) const { + JSValueRef exn; + JSValueRef property = JSObjectGetPropertyAtIndex(m_context, m_obj, index, &exn); + if (!property) { + std::string exceptionText = Value(m_context, exn).toString().str(); + throwJSExecutionException("Failed to get property at index %u: %s", index, exceptionText.c_str()); + } + return Value(m_context, property); +} + Value Object::getProperty(const char *propName) const { return getProperty(String(propName)); } @@ -96,6 +106,30 @@ void Object::setProperty(const char *propName, const Value& value) const { setProperty(String(propName), value); } +std::vector Object::getPropertyNames() const { + std::vector names; + auto namesRef = JSObjectCopyPropertyNames(m_context, m_obj); + size_t count = JSPropertyNameArrayGetCount(namesRef); + for (size_t i = 0; i < count; i++) { + auto string = String::ref(JSPropertyNameArrayGetNameAtIndex(namesRef, i)); + names.emplace_back(string.str()); + } + JSPropertyNameArrayRelease(namesRef); + return names; +} + +std::unordered_map Object::toJSONMap() const { + std::unordered_map map; + auto namesRef = JSObjectCopyPropertyNames(m_context, m_obj); + size_t count = JSPropertyNameArrayGetCount(namesRef); + for (size_t i = 0; i < count; i++) { + auto key = String::ref(JSPropertyNameArrayGetNameAtIndex(namesRef, i)); + map.emplace(key.str(), getProperty(key).toJSONString()); + } + JSPropertyNameArrayRelease(namesRef); + return map; +} + /* static */ Object Object::create(JSContextRef ctx) { JSObjectRef newObj = JSObjectMake( diff --git a/ReactAndroid/src/main/jni/react/Value.h b/ReactAndroid/src/main/jni/react/Value.h index 1cee8cc9f..8549c7f1b 100644 --- a/ReactAndroid/src/main/jni/react/Value.h +++ b/ReactAndroid/src/main/jni/react/Value.h @@ -4,6 +4,9 @@ #include #include +#include +#include + #include #include #include @@ -123,8 +126,11 @@ 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; + std::vector getPropertyNames() const; + std::unordered_map toJSONMap() const; void makeProtected() { if (!m_isProtected && m_obj) {