diff --git a/ReactCommon/jsi/jsi/jsi.cpp b/ReactCommon/jsi/jsi/jsi.cpp index 0b8cb80b388..0435ee062b5 100644 --- a/ReactCommon/jsi/jsi/jsi.cpp +++ b/ReactCommon/jsi/jsi/jsi.cpp @@ -102,6 +102,13 @@ Instrumentation& Runtime::instrumentation() { return sharedInstance; } +Value Runtime::createValueFromJsonUtf8(const uint8_t* json, size_t length) { + Function parseJson = global() + .getPropertyAsObject(*this, "JSON") + .getPropertyAsFunction(*this, "parse"); + return parseJson.call(*this, String::createFromUtf8(*this, json, length)); +} + Pointer& Pointer::operator=(Pointer&& other) { if (ptr_) { ptr_->invalidate(); @@ -214,16 +221,6 @@ Value::~Value() { } } -Value Value::createFromJsonUtf8( - Runtime& runtime, - const uint8_t* json, - size_t length) { - Function parseJson = runtime.global() - .getPropertyAsObject(runtime, "JSON") - .getPropertyAsFunction(runtime, "parse"); - return parseJson.call(runtime, String::createFromUtf8(runtime, json, length)); -} - bool Value::strictEquals(Runtime& runtime, const Value& a, const Value& b) { if (a.kind_ != b.kind_) { return false; diff --git a/ReactCommon/jsi/jsi/jsi.h b/ReactCommon/jsi/jsi/jsi.h index 0baa5e55641..0370fed9730 100644 --- a/ReactCommon/jsi/jsi/jsi.h +++ b/ReactCommon/jsi/jsi/jsi.h @@ -252,6 +252,10 @@ class Runtime { virtual String createStringFromUtf8(const uint8_t* utf8, size_t length) = 0; virtual std::string utf8(const String&) = 0; + // \return a \c Value created from a utf8-encoded JSON string. The default + // implementation creates a \c String and invokes JSON.parse. + virtual Value createValueFromJsonUtf8(const uint8_t* json, size_t length); + virtual Object createObject() = 0; virtual Object createObject(std::shared_ptr ho) = 0; virtual std::shared_ptr getHostObject(const jsi::Object&) = 0; @@ -974,7 +978,9 @@ class Value { // \return a \c Value created from a utf8-encoded JSON string. static Value - createFromJsonUtf8(Runtime& runtime, const uint8_t* json, size_t length); + createFromJsonUtf8(Runtime& runtime, const uint8_t* json, size_t length) { + return runtime.createValueFromJsonUtf8(json, length); + } /// \return according to the SameValue algorithm see more here: // https://www.ecma-international.org/ecma-262/5.1/#sec-11.9.4