Add Object.getPropertyNames() and Object.toJSONMap APIs
Summary: Adds APIs to get all the enumerable property names of an object and to get an object as a map of property names to JSON values. public Reviewed By: lexs Differential Revision: D2916238 fb-gh-sync-id: 0d9ee1eb4886d58fba8241537f6a0dad6024bd0e shipit-source-id: 0d9ee1eb4886d58fba8241537f6a0dad6024bd0e
This commit is contained in:
parent
e6b6aedd86
commit
bab48182d9
|
@ -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<std::string> Object::getPropertyNames() const {
|
||||
std::vector<std::string> 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<std::string, std::string> Object::toJSONMap() const {
|
||||
std::unordered_map<std::string, std::string> 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(
|
||||
|
|
|
@ -4,6 +4,9 @@
|
|||
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include <JavaScriptCore/JSObjectRef.h>
|
||||
#include <JavaScriptCore/JSRetainPtr.h>
|
||||
#include <JavaScriptCore/JSStringRef.h>
|
||||
|
@ -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<std::string> getPropertyNames() const;
|
||||
std::unordered_map<std::string, std::string> toJSONMap() const;
|
||||
|
||||
void makeProtected() {
|
||||
if (!m_isProtected && m_obj) {
|
||||
|
|
Loading…
Reference in New Issue