Skip to content

Commit 34af43e

Browse files
committed
fix: crash when copying TurboModules between runtimes
1 parent 37bf285 commit 34af43e

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/TurboModule.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#pragma once
99

10+
#include <cstdint>
1011
#include <memory>
1112
#include <string>
1213
#include <unordered_map>
@@ -58,7 +59,7 @@ class JSI_EXPORT TurboModule : public jsi::HostObject {
5859
// If we have a JS wrapper, cache the result of this lookup
5960
// We don't cache misses, to allow for methodMap_ to dynamically be
6061
// extended
61-
if (jsRepresentation_ && !prop.isUndefined()) {
62+
if (jsRepresentation_ && reinterpret_cast<std::uintptr_t>(&runtime) == representationRuntimeAddress_ && !prop.isUndefined()) {
6263
jsRepresentation_->lock(runtime).asObject(runtime).setProperty(runtime, propName, prop);
6364
}
6465
return prop;
@@ -138,6 +139,9 @@ class JSI_EXPORT TurboModule : public jsi::HostObject {
138139
private:
139140
friend class TurboModuleBinding;
140141
std::unique_ptr<jsi::WeakObject> jsRepresentation_;
142+
/** This address is used only for identifying the runtime associated with the
143+
* JS representation. Do not dereference or use for any other purpose. */
144+
std::uintptr_t representationRuntimeAddress_;
141145
};
142146

143147
/**

packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/TurboModuleBinding.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <ReactCommon/TurboModuleWithJSIBindings.h>
1111
#include <cxxreact/TraceSection.h>
1212
#include <react/utils/jsi-utils.h>
13+
#include <cstdint>
1314
#include <stdexcept>
1415
#include <string>
1516

@@ -177,6 +178,8 @@ jsi::Value TurboModuleBinding::getModule(
177178
jsi::Object jsRepresentation(runtime);
178179
weakJsRepresentation =
179180
std::make_unique<jsi::WeakObject>(runtime, jsRepresentation);
181+
module->representationRuntimeAddress_ =
182+
reinterpret_cast<std::uintptr_t>(&runtime);
180183

181184
// Lazily populate the jsRepresentation, on property access.
182185
//

0 commit comments

Comments
 (0)