From 7081391c3d443891045374a3a827fc8c2dd9eafc Mon Sep 17 00:00:00 2001 From: Pieter De Baets Date: Tue, 6 Jun 2017 03:04:56 -0700 Subject: [PATCH] Simplify Value.fromJSON API Reviewed By: mhorowitz Differential Revision: D5154478 fbshipit-source-id: 3f9528a6401d89df7e170d55da9c0327db5f4f3e --- ReactCommon/cxxreact/JSCExecutor.cpp | 3 +-- ReactCommon/cxxreact/tests/value.cpp | 4 ++-- ReactCommon/jschelpers/Value.cpp | 5 +++-- ReactCommon/jschelpers/Value.h | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/ReactCommon/cxxreact/JSCExecutor.cpp b/ReactCommon/cxxreact/JSCExecutor.cpp index c08dab694..be014ff4d 100644 --- a/ReactCommon/cxxreact/JSCExecutor.cpp +++ b/ReactCommon/cxxreact/JSCExecutor.cpp @@ -521,8 +521,7 @@ Value JSCExecutor::callFunctionSyncWithValue( void JSCExecutor::setGlobalVariable(std::string propName, std::unique_ptr jsonValue) { try { SystraceSection s("JSCExecutor::setGlobalVariable", "propName", propName); - - auto valueToInject = Value::fromJSON(m_context, adoptString(std::move(jsonValue))); + auto valueToInject = Value::fromJSON(adoptString(std::move(jsonValue))); Object::getGlobalObject(m_context).setProperty(propName.c_str(), valueToInject); } catch (...) { std::throw_with_nested(std::runtime_error("Error setting global variable: " + propName)); diff --git a/ReactCommon/cxxreact/tests/value.cpp b/ReactCommon/cxxreact/tests/value.cpp index 4965b2345..8f445b0e1 100644 --- a/ReactCommon/cxxreact/tests/value.cpp +++ b/ReactCommon/cxxreact/tests/value.cpp @@ -36,7 +36,7 @@ TEST(Value, FromJSON) { prepare(); JSGlobalContextRef ctx = JSC_JSGlobalContextCreateInGroup(false, nullptr, nullptr); String s(ctx, "{\"a\": 4}"); - Value v(Value::fromJSON(ctx, s)); + Value v(Value::fromJSON(s)); EXPECT_TRUE(v.isObject()); JSC_JSGlobalContextRelease(ctx); } @@ -45,7 +45,7 @@ TEST(Value, ToJSONString) { prepare(); JSGlobalContextRef ctx = JSC_JSGlobalContextCreateInGroup(false, nullptr, nullptr); String s(ctx, "{\"a\": 4}"); - Value v(Value::fromJSON(ctx, s)); + Value v(Value::fromJSON(s)); folly::dynamic dyn = folly::parseJson(v.toJSONString()); ASSERT_NE(nullptr, dyn); EXPECT_TRUE(dyn.isObject()); diff --git a/ReactCommon/jschelpers/Value.cpp b/ReactCommon/jschelpers/Value.cpp index 7ba8ff0c9..ab46577d9 100644 --- a/ReactCommon/jschelpers/Value.cpp +++ b/ReactCommon/jschelpers/Value.cpp @@ -38,7 +38,8 @@ std::string Value::toJSONString(unsigned indent) const { } /* static */ -Value Value::fromJSON(JSContextRef ctx, const String& json) { +Value Value::fromJSON(const String& json) { + JSContextRef ctx = json.context(); auto result = JSC_JSValueMakeFromJSONString(ctx, json); if (!result) { throw JSException(folly::to( @@ -66,7 +67,7 @@ Value Value::fromDynamic(JSContextRef ctx, const folly::dynamic& value) { return Value(ctx, jsVal); #else auto json = folly::toJson(value); - return fromJSON(ctx, String(ctx, json.c_str())); + return fromJSON(String(ctx, json.c_str())); #endif } diff --git a/ReactCommon/jschelpers/Value.h b/ReactCommon/jschelpers/Value.h index 0cc58418e..b8f5603d2 100644 --- a/ReactCommon/jschelpers/Value.h +++ b/ReactCommon/jschelpers/Value.h @@ -330,7 +330,7 @@ public: } RN_EXPORT std::string toJSONString(unsigned indent = 0) const; - RN_EXPORT static Value fromJSON(JSContextRef ctx, const String& json); + RN_EXPORT static Value fromJSON(const String& json); RN_EXPORT static Value fromDynamic(JSContextRef ctx, const folly::dynamic& value); RN_EXPORT JSContextRef context() const;