Skip to content

Commit

Permalink
Make it possible for a Runtime to provide its own JSON parsing
Browse files Browse the repository at this point in the history
Summary: Make it possible for a Runtime implementation to provide its own JSON parsing, by making the method on Value call through Runtime, which now has the default implementation.

Reviewed By: tmikov

Differential Revision: D17637395

fbshipit-source-id: b8997f7d1721a7790326417f3abfa86c875923c9
  • Loading branch information
kodafb authored and facebook-github-bot committed Sep 30, 2019
1 parent 0a282c4 commit 9e7e178
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
17 changes: 7 additions & 10 deletions ReactCommon/jsi/jsi/jsi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 7 additions & 1 deletion ReactCommon/jsi/jsi/jsi.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<HostObject> ho) = 0;
virtual std::shared_ptr<HostObject> getHostObject(const jsi::Object&) = 0;
Expand Down Expand Up @@ -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
Expand Down

2 comments on commit 9e7e178

@janicduplessis
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is causing a crash in my app, using hermes. Not sure why but I can't repro in RNTester.

*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
Build fingerprint: 'HUAWEI/MHA-L29/HWMHA:7.0/HUAWEIMHA-L29/C567B190:user/release-keys'
Revision: '0'
ABI: 'arm64'
pid: 9097, tid: 9252, name: mqt_js  >>> com.th3rdwave <<<
signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x786d400220
    x0   0000000000000028  x1   000000788ef235e0  x2   000000788e837aa0  x3   0000000000000004
    x4   00000000000001d3  x5   0000000000000040  x6   000000788fccd1c6  x7   6120646574636570
    x8   000000788e837998  x9   000000786d400000  x10  00000000000001d3  x11  0000000000000000
    x12  000000788f885c08  x13  00000000ffffffff  x14  00000000000737d0  x15  ffffffffffffffff
    x16  000000788eba9b70  x17  000000788eb83784  x18  00000078b57343e0  x19  00000078ae03d290
    x20  000000788e837998  x21  00000078ae012eb8  x22  00000078ae076248  x23  000000788e8394e8
    x24  000000788e837c18  x25  00000078ae012eb8  x26  00000078ae03d290  x27  00000078ae012ed8
    x28  00000078ae012ea8  x29  000000788e837980  x30  000000788eb83804
    sp   000000788e837960  pc   000000788ef23600  pstate 0000000020000000
backtrace:
    #00 pc 000000000001f600  /data/data/com.th3rdwave/lib-0/libhermes.so
    #01 pc 000000000003a800  /data/data/com.th3rdwave/lib-0/libhermes-executor-release.so
    #02 pc 0000000000037a30  /data/data/com.th3rdwave/lib-0/libhermes-executor-release.so
    #03 pc 000000000001ddd4  /data/data/com.th3rdwave/lib-0/libhermes-executor-release.so
    #04 pc 00000000000a1100  /data/data/com.th3rdwave/lib-0/libreactnativejni.so
    #05 pc 00000000000999c8  /data/data/com.th3rdwave/lib-0/libreactnativejni.so
    #06 pc 000000000006a9c4  /data/data/com.th3rdwave/lib-0/libreactnativejni.so
    #07 pc 00000000000688f4  /data/data/com.th3rdwave/lib-0/libreactnativejni.so
    #08 pc 0000000000058358  /data/data/com.th3rdwave/lib-0/libreactnativejni.so
    #09 pc 00000000000582d4  /data/data/com.th3rdwave/lib-0/libreactnativejni.so
    #10 pc 0000000000e1f080  /data/app/com.th3rdwave-1/oat/arm64/base.odex (offset 0xdfa000)

Also happens in debug, right after the bundle finishes loading

10-01 12:47:04.649 24781 24920 F libc    : Fatal signal 11 (SIGSEGV), code 1, fault addr 0xcb329f9b in tid 24920 (mqt_js)
10-01 12:47:04.755 24927 24927 F DEBUG   : *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
10-01 12:47:04.755 24927 24927 F DEBUG   : Build fingerprint: 'google/sdk_gphone_x86/generic_x86:8.0.0/OSR1.170901.043/4456315:user/release-keys'
10-01 12:47:04.756 24927 24927 F DEBUG   : Revision: '0'
10-01 12:47:04.756 24927 24927 F DEBUG   : ABI: 'x86'
10-01 12:47:04.756 24927 24927 F DEBUG   : pid: 24781, tid: 24920, name: mqt_js  >>> com.th3rdwave <<<
10-01 12:47:04.756 24927 24927 F DEBUG   : signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0xcb329f9b
10-01 12:47:04.756 24927 24927 F DEBUG   :     eax 8e1fc040  ebx 88408a50  ecx cb329f9b  edx 884047e4
10-01 12:47:04.757 24927 24927 F DEBUG   :     esi 8e1fc040  edi 87e8b210
10-01 12:47:04.757 24927 24927 F DEBUG   :     xcs 00000073  xds 0000007b  xes 0000007b  xfs 0000003b  xss 0000007b
10-01 12:47:04.757 24927 24927 F DEBUG   :     eip 881176b3  ebp 87e8b1d8  esp 87e8b0d0  flags 00010296
10-01 12:47:04.760 24927 24927 F DEBUG   :
10-01 12:47:04.760 24927 24927 F DEBUG   : backtrace:
10-01 12:47:04.760 24927 24927 F DEBUG   :     #00 pc 000166b3  /data/app/com.th3rdwave-TlJljyrlRG7qldhw8ZaTww==/lib/x86/libhermes.so (_ZN8facebook6hermes17HermesRuntimeImpl12createObjectENSt6__ndk110shared_ptrINS_3jsi10HostObjectEEE+195)
10-01 12:47:04.760 24927 24927 F DEBUG   :     #01 pc 00060469  /data/app/com.th3rdwave-TlJljyrlRG7qldhw8ZaTww==/lib/x86/libhermes-inspector.so (_ZN8facebook6hermes9inspector9Inspector17installLogHandlerEv+73)
10-01 12:47:04.760 24927 24927 F DEBUG   :     #02 pc 00092f19  /data/app/com.th3rdwave-TlJljyrlRG7qldhw8ZaTww==/lib/x86/libhermes-inspector.so (_ZN8facebook6hermes9inspector6chrome10Connection4ImplC1ENSt6__ndk110unique_ptrINS1_14RuntimeAdapterENS5_14default_deleteIS7_EEEERKNS5_12basic_stringIcNS5_11char_traitsIcEENS5_9allocatorIcEEEEb+585)
10-01 12:47:04.761 24927 24927 F DEBUG   :     #03 pc 0009b463  /data/app/com.th3rdwave-TlJljyrlRG7qldhw8ZaTww==/lib/x86/libhermes-inspector.so (_ZN8facebook6hermes9inspector6chrome10ConnectionC1ENSt6__ndk110unique_ptrINS1_14RuntimeAdapterENS4_14default_deleteIS6_EEEERKNS4_12basic_stringIcNS4_11char_traitsIcEENS4_9allocatorIcEEEEb+99)
10-01 12:47:04.761 24927 24927 F DEBUG   :     #04 pc 000b39e3  /data/app/com.th3rdwave-TlJljyrlRG7qldhw8ZaTww==/lib/x86/libhermes-inspector.so (_ZNSt6__ndk110shared_ptrIN8facebook6hermes9inspector6chrome10ConnectionEE11make_sharedIJNS_10unique_ptrINS3_14RuntimeAdapterENS_14default_deleteIS9_EEEERKNS_12basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEERbEEES6_DpOT_+131)
10-01 12:47:04.761 24927 24927 F DEBUG   :     #05 pc 000b2c17  /data/app/com.th3rdwave-TlJljyrlRG7qldhw8ZaTww==/lib/x86/libhermes-inspector.so (_ZN8facebook6hermes9inspector6chrome15ConnectionDemux15enableDebuggingENSt6__ndk110unique_ptrINS1_14RuntimeAdapterENS4_14default_deleteIS6_EEEERKNS4_12basic_stringIcNS4_11char_traitsIcEENS4_9allocatorIcEEEE+823)
10-01 12:47:04.761 24927 24927 F DEBUG   :     #06 pc 000d5544  /data/app/com.th3rdwave-TlJljyrlRG7qldhw8ZaTww==/lib/x86/libhermes-inspector.so (_ZN8facebook6hermes9inspector6chrome15enableDebuggingENSt6__ndk110unique_ptrINS1_14RuntimeAdapterENS3_14default_deleteIS5_EEEERKNS3_12basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEE+84)
10-01 12:47:04.762 24927 24927 F DEBUG   :     #07 pc 0001924a  /data/app/com.th3rdwave-TlJljyrlRG7qldhw8ZaTww==/lib/x86/libhermes-executor-debug.so (_ZN8facebook5react21HermesExecutorFactory16createJSExecutorENSt6__ndk110shared_ptrINS0_16ExecutorDelegateEEENS3_INS0_18MessageQueueThreadEEE+554)
10-01 12:47:04.762 24927 24927 F DEBUG   :     #08 pc 0009d0cb  /data/app/com.th3rdwave-TlJljyrlRG7qldhw8ZaTww==/lib/x86/libreactnativejni.so (_ZN8facebook5react16NativeToJsBridgeC1EPNS0_17JSExecutorFactoryENSt6__ndk110shared_ptrINS0_14ModuleRegistryEEENS5_INS0_18MessageQueueThreadEEENS5_INS0_16InstanceCallbackEEE+235)
10-01 12:47:04.762 24927 24927 F DEBUG   :     #09 pc 00094b2f  /data/app/com.th3rdwave-TlJljyrlRG7qldhw8ZaTww==/lib/x86/libreactnativejni.so
10-01 12:47:04.762 24927 24927 F DEBUG   :     #10 pc 00061089  /data/app/com.th3rdwave-TlJljyrlRG7qldhw8ZaTww==/lib/x86/libreactnativejni.so
10-01 12:47:04.762 24927 24927 F DEBUG   :     #11 pc 0005ec67  /data/app/com.th3rdwave-TlJljyrlRG7qldhw8ZaTww==/lib/x86/libreactnativejni.so
10-01 12:47:04.762 24927 24927 F DEBUG   :     #12 pc 0004ca62  /data/app/com.th3rdwave-TlJljyrlRG7qldhw8ZaTww==/lib/x86/libreactnativejni.so (_ZN8facebook3jni6detail13MethodWrapperIMNS_5react15JNativeRunnableEFvvEXadL_ZNS4_3runEvEES4_vJEE8dispatchENS0_9alias_refIPNS1_8JTypeForINS0_11HybridClassIS4_NS3_8RunnableEE8JavaPartESB_vE11_javaobjectEEE+50)
10-01 12:47:04.762 24927 24927 F DEBUG   :     #13 pc 0004c9c9  /data/app/com.th3rdwave-TlJljyrlRG7qldhw8ZaTww==/lib/x86/libreactnativejni.so (_ZN8facebook3jni6detail15FunctionWrapperIPFvNS0_9alias_refIPNS1_8JTypeForINS0_11HybridClassINS_5react15JNativeRunnableENS6_8RunnableEE8JavaPartES8_vE11_javaobjectEEEEXadL_ZNS1_13MethodWrapperIMS7_FvvEXadL_ZNS7_3runEvEES7_vJEE8dispatchESE_EESD_vJEE4callEP7_JNIEnvP8_jobject+73)
10-01 12:47:04.762 24927 24927 F DEBUG   :     #14 pc 0004c973  /data/app/com.th3rdwave-TlJljyrlRG7qldhw8ZaTww==/lib/x86/libreactnativejni.so (_ZN8facebook3jni6detail13MethodWrapperIMNS_5react15JNativeRunnableEFvvEXadL_ZNS4_3runEvEES4_vJEE4callEP7_JNIEnvP8_jobject+35)
10-01 12:47:04.763 24927 24927 F DEBUG   :     #15 pc 000767d8  /data/app/com.th3rdwave-TlJljyrlRG7qldhw8ZaTww==/oat/x86/base.odex (offset 0x73000)
10-01 12:47:06.094  1536  1536 E /system/bin/tombstoned: Tombstone written to: /data/tombstones//tombstone_09
``

@rickhanlonii
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kodafb did you see this comment?

Please sign in to comment.