From ee16af20d00dffe0c11f4033d3d2eb50fc0f6be9 Mon Sep 17 00:00:00 2001 From: Nathan Walker Date: Fri, 20 Nov 2026 13:19:55 -0800 Subject: [PATCH] fix(ffi/objc): keep bridge objects in their own runtime and survive its teardown react-native-worklets shares jsi::HostObjects between runtimes by reference, so a native object returned from a worklet reached the React Native runtime still bound to the UI runtime's bridge. Using it mixed the two heaps, and finalizing it after the UI runtime was gone crashed on reload. - Bridge host objects throw when used from a runtime other than their bridge's (Hermes), pointing at interop.handleof / interop.object. - NativeScript.scheduleOnUI converts NSObject results to handles inside the worklet and adopts them in the caller's runtime. - On Hermes each bridge is bound to its runtime with setRuntimeData. When the runtime is destroyed, the bridge's caches are cleared and later inserts are no-ops; objects that own a Value abandon it instead of releasing it into the freed heap; blocks and method implementations called afterwards log and return zero. --- NativeScript/ffi/objc/hermes/NativeApiJsi.mm | 36 +++++++ .../ffi/objc/shared/bridge/Callbacks.mm | 30 +++++- .../ffi/objc/shared/bridge/HostObject.mm | 2 + .../ffi/objc/shared/bridge/Install.mm | 6 ++ .../ffi/objc/shared/bridge/ObjCBridge.mm | 84 ++++++++++++++-- .../ffi/objc/shared/bridge/TypeConv.mm | 5 + .../objc/shared/bridge/host_objects/Class.mm | 3 + .../shared/bridge/host_objects/Interop.mm | 10 ++ .../objc/shared/bridge/host_objects/Object.mm | 8 ++ .../shared/bridge/host_objects/Protocol.mm | 2 + .../objc/shared/bridge/host_objects/Struct.mm | 7 ++ packages/react-native/src/index.ts | 99 ++++++++++++++++++- 12 files changed, 283 insertions(+), 9 deletions(-) diff --git a/NativeScript/ffi/objc/hermes/NativeApiJsi.mm b/NativeScript/ffi/objc/hermes/NativeApiJsi.mm index c7db662cb..d4e0f5fde 100644 --- a/NativeScript/ffi/objc/hermes/NativeApiJsi.mm +++ b/NativeScript/ffi/objc/hermes/NativeApiJsi.mm @@ -273,6 +273,42 @@ throw JSError(runtime, }); } +#define NATIVESCRIPT_NATIVE_API_HAS_ENGINE_RUNTIME_TEARDOWN 1 + +// Runtime data is destroyed with the runtime while its Values can still be +// released: Hermes frees it before tearing down its heap, and jsi's default +// implementation frees it from a finalizer of that runtime's own global. +class NativeApiRuntimeBridges final { + public: + ~NativeApiRuntimeBridges() { + for (auto& bridge : bridges_) { + bridge->releaseRuntimeValues(); + } + } + + void add(std::shared_ptr bridge) { + bridges_.push_back(std::move(bridge)); + } + + private: + std::vector> bridges_; +}; + +constexpr facebook::jsi::UUID kNativeApiRuntimeBridgesUUID{ + 0x5f0e2c71, 0x8b3d, 0x4a6e, 0x9c41, 0x2d7a6b13e8f4}; + +void BindNativeApiBridgeToRuntime(Runtime& runtime, + std::shared_ptr bridge) { + bridge->setOwnerRuntime(runtime); + auto bridges = std::static_pointer_cast( + runtime.getRuntimeData(kNativeApiRuntimeBridgesUUID)); + if (bridges == nullptr) { + bridges = std::make_shared(); + runtime.setRuntimeData(kNativeApiRuntimeBridgesUUID, bridges); + } + bridges->add(std::move(bridge)); +} + } // namespace #include "../shared/bridge/Install.mm" diff --git a/NativeScript/ffi/objc/shared/bridge/Callbacks.mm b/NativeScript/ffi/objc/shared/bridge/Callbacks.mm index 8a42d1ec2..1078a19a2 100644 --- a/NativeScript/ffi/objc/shared/bridge/Callbacks.mm +++ b/NativeScript/ffi/objc/shared/bridge/Callbacks.mm @@ -559,6 +559,9 @@ throw JSError(runtime, } ~NativeApiCallback() { + if (bridge_ != nullptr) { + bridge_->releaseOrAbandonValue(function_); + } if (closure_ != nullptr) { ffi_closure_free(closure_); closure_ = nullptr; @@ -587,7 +590,7 @@ void retainBlockCopy(const void* blockPointer) { } auto self = shared_from_this(); if (bridge_ != nullptr && runtime_ != nullptr && function_ != nullptr && - blockPointer != nullptr) { + blockPointer != nullptr && !bridge_->runtimeValuesReleased()) { bridge_->rememberRoundTripValue(*runtime_, blockPointer, Value(*runtime_, *function_), false, roundTripValidationKey_); @@ -657,7 +660,7 @@ bool releaseBlockCopy(const void* blockPointer) { } if (bridge != nullptr && runtime != nullptr && - pointerToForget != nullptr) { + pointerToForget != nullptr && !bridge->runtimeValuesReleased()) { NativeApiRuntimeScope runtimeScope(*runtime); bridge->forgetRoundTripValue(*runtime, pointerToForget); } @@ -681,6 +684,9 @@ bool releaseBlockCopy(const void* blockPointer) { } void invoke(void* ret, void* args[]) { + if (runtimeDestroyed(ret)) { + return; + } if (runtime_ == nullptr || function_ == nullptr || signature_ == nullptr) { throwNativeApiCallbackException("Invalid callback."); } @@ -863,6 +869,23 @@ void invoke(void* ret, void* args[]) { } private: + // A native caller can keep a block or method implementation past its + // runtime (e.g. across a JavaScript reload); it gets a zero result instead. + bool runtimeDestroyed(void* ret) { + if (bridge_ == nullptr || !bridge_->runtimeValuesReleased()) { + return false; + } + std::string name = signature_ != nullptr ? signature_->selectorName : ""; + if (name.empty()) { + name = block_ ? "block" : "native callback"; + } + NSLog(@"NativeScript: %s called after its JavaScript runtime was " + @"destroyed; returning zero/nil.", + name.c_str()); + zeroReturnValue(ret); + return true; + } + // The ObjC class whose implementation `callSuperBeforeCallback`/ // invokeMethodSuper() should dispatch against. A JS-subclass receiver // (ClassBuilder instance) dispatches to its own runtime superclass; @@ -1016,6 +1039,9 @@ void invokeMethodSuper(void* ret, void* args[]) const { } void invokeOnCurrentThread(void* ret, void* args[], std::string* error) { + if (runtimeDestroyed(ret)) { + return; + } try { NativeApiRuntimeScope runtimeScope(*runtime_); size_t nativeArgOffset = signature_->implicitArgumentCount; diff --git a/NativeScript/ffi/objc/shared/bridge/HostObject.mm b/NativeScript/ffi/objc/shared/bridge/HostObject.mm index 6ab4d0493..7e6433b57 100644 --- a/NativeScript/ffi/objc/shared/bridge/HostObject.mm +++ b/NativeScript/ffi/objc/shared/bridge/HostObject.mm @@ -46,6 +46,7 @@ explicit NativeApiHostObject(std::shared_ptr bridge) const std::shared_ptr& bridge() const { return bridge_; } Value get(Runtime& runtime, const PropNameID& name) override { + bridge_->requireOwnerRuntime(runtime); std::string property = name.utf8(runtime); if (property == "runtime") { return makeString(runtime, NATIVESCRIPT_NATIVE_API_RUNTIME_NAME); @@ -618,6 +619,7 @@ throw JSError(runtime, } std::vector getPropertyNames(Runtime& runtime) override { + bridge_->requireOwnerRuntime(runtime); std::vector names; names.reserve(11); addPropertyName(runtime, names, "runtime"); diff --git a/NativeScript/ffi/objc/shared/bridge/Install.mm b/NativeScript/ffi/objc/shared/bridge/Install.mm index 6623f8491..f8a465851 100644 --- a/NativeScript/ffi/objc/shared/bridge/Install.mm +++ b/NativeScript/ffi/objc/shared/bridge/Install.mm @@ -1,5 +1,11 @@ +#ifndef NATIVESCRIPT_NATIVE_API_HAS_ENGINE_RUNTIME_TEARDOWN +inline void BindNativeApiBridgeToRuntime(Runtime&, + std::shared_ptr) {} +#endif + Object CreateNativeApi(Runtime& runtime, const NativeApiConfig& config) { auto bridge = std::make_shared(config); + BindNativeApiBridgeToRuntime(runtime, bridge); return Object::createFromHostObject(runtime, std::make_shared(std::move(bridge))); } diff --git a/NativeScript/ffi/objc/shared/bridge/ObjCBridge.mm b/NativeScript/ffi/objc/shared/bridge/ObjCBridge.mm index 1ae78ad0b..4ab9474e6 100644 --- a/NativeScript/ffi/objc/shared/bridge/ObjCBridge.mm +++ b/NativeScript/ffi/objc/shared/bridge/ObjCBridge.mm @@ -569,6 +569,73 @@ explicit NativeApiBridge(const NativeApiConfig& config) } } + // Called while the bridge's runtime is being destroyed, when its Values can + // still be released. Host objects can be shared into another runtime and + // outlive this one, so from here on the bridge holds no Values. + void releaseRuntimeValues() { + decltype(roundTripValues_) roundTripValues; + decltype(roundTripCacheFramesByThread_) roundTripFrames; + decltype(recentRoundTripValues_) recentRoundTripValues; +#ifdef TARGET_ENGINE_HERMES + decltype(weakRoundTripValues_) weakRoundTripValues; + std::shared_ptr roundTripRoot; +#endif + { + std::lock_guard lock(roundTripValuesMutex_); + runtimeValuesReleased_.store(true, std::memory_order_release); + roundTripValues.swap(roundTripValues_); + roundTripFrames.swap(roundTripCacheFramesByThread_); + recentRoundTripValues.swap(recentRoundTripValues_); + recentRoundTripValueOrder_.clear(); +#ifdef TARGET_ENGINE_HERMES + weakRoundTripValues.swap(weakRoundTripValues_); + rootedRoundTripValues_.clear(); + roundTripRoot.swap(roundTripRootCache_); +#endif + roundTripValuesGeneration_.fetch_add(1, std::memory_order_release); + } + classValues_.clear(); + classPrototypes_.clear(); + pointerValues_.clear(); + decltype(objectExpandos_) objectExpandos; + { + std::lock_guard lock(objectExpandosMutex_); + objectExpandos.swap(objectExpandos_); + objectExpandoOwnerCounts_.clear(); + retiredObjectExpandoKeys_.clear(); + objectExpandosGeneration_.fetch_add(1, std::memory_order_release); + } + } + + bool runtimeValuesReleased() const { + return runtimeValuesReleased_.load(std::memory_order_acquire); + } + + // A destroyed runtime freed its Values with its heap; releasing one would + // touch freed memory. + template + void releaseOrAbandonValue(std::shared_ptr& value) { + if (value != nullptr && runtimeValuesReleased()) { + new std::shared_ptr(std::move(value)); + } + value.reset(); + } + + void setOwnerRuntime(const Runtime& runtime) { ownerRuntime_ = &runtime; } + + // Worklets shares host objects between runtimes by reference. Using one in + // a runtime other than its bridge's would mix the two heaps. + void requireOwnerRuntime(Runtime& runtime) const { + if (ownerRuntime_ != nullptr && ownerRuntime_ != &runtime) { + throw JSError( + runtime, + "This NativeScript object belongs to another JavaScript runtime, " + "such as the Worklets UI runtime. Pass a handle instead: " + "interop.handleof(object).toHexString() in the runtime that owns " + "it, then interop.object(handle) in this one."); + } + } + MDMetadataReader* metadata() const { return metadata_.get(); } void* selfDl() const { return selfDl_; } @@ -800,7 +867,7 @@ void rememberRoundTripValue(Runtime& runtime, const void* native, const Value& value, bool stringLikeNative = false, uintptr_t validationKey = 0) { - if (native == nullptr) { + if (native == nullptr || runtimeValuesReleased()) { return; } uintptr_t key = @@ -846,7 +913,7 @@ void rememberWeakNativeObjectRoundTripValue( Runtime& runtime, id object, const Value& value, bool stringLikeNative = false) { #ifdef TARGET_ENGINE_HERMES - if (object == nil || !value.isObject()) { + if (object == nil || !value.isObject() || runtimeValuesReleased()) { return; } auto weak = @@ -891,7 +958,7 @@ void rememberScopedRoundTripValueWithValidationKey(Runtime& runtime, bool stringLikeNative, bool persistBeyondFrame, uintptr_t validationKey) { - if (native == nullptr) { + if (native == nullptr || runtimeValuesReleased()) { return; } uintptr_t key = @@ -1161,7 +1228,7 @@ void endRoundTripCacheFrame() { } void rememberClassValue(Runtime& runtime, Class cls, const Value& value) { - if (cls == Nil) { + if (cls == Nil || runtimeValuesReleased()) { return; } classValues_[normalizeRuntimePointer(reinterpret_cast(cls))] = @@ -1181,7 +1248,7 @@ Value findClassValue(Runtime& runtime, Class cls) const { } void rememberClassPrototype(Runtime& runtime, Class cls, const Value& value) { - if (cls == Nil) { + if (cls == Nil || runtimeValuesReleased()) { return; } classPrototypes_[normalizeRuntimePointer(reinterpret_cast(cls))] = @@ -1206,7 +1273,7 @@ Value findClassPrototype(Runtime& runtime, Class cls) const { // expando owner count can run on either thread relative to a get/set. void setObjectExpando(Runtime& runtime, const void* native, const std::string& property, const Value& value) { - if (native == nullptr || property.empty()) { + if (native == nullptr || property.empty() || runtimeValuesReleased()) { return; } const uintptr_t key = @@ -1424,6 +1491,9 @@ void cachePropertyGetter(Class cls, const std::string& property, void rememberPointerValue(Runtime& runtime, const void* native, const Value& value) { + if (runtimeValuesReleased()) { + return; + } pointerValues_[reinterpret_cast(native)] = std::make_shared(runtime, value); } @@ -2543,6 +2613,8 @@ void purgeRetiredObjectExpandosLocked(uintptr_t key) { std::thread::id jsThreadId_ = std::this_thread::get_id(); std::mutex retainedLifetimesMutex_; std::vector> retainedLifetimes_; + std::atomic runtimeValuesReleased_{false}; + const Runtime* ownerRuntime_ = nullptr; }; class NativeApiRoundTripCacheFrameGuard final { diff --git a/NativeScript/ffi/objc/shared/bridge/TypeConv.mm b/NativeScript/ffi/objc/shared/bridge/TypeConv.mm index 16df9f094..d9289473e 100644 --- a/NativeScript/ffi/objc/shared/bridge/TypeConv.mm +++ b/NativeScript/ffi/objc/shared/bridge/TypeConv.mm @@ -1311,6 +1311,7 @@ throw JSError(runtime, "This native return type is not supported by " } Value NativeApiReferenceHostObject::get(Runtime& runtime, const PropNameID& name) { + bridge_->requireOwnerRuntime(runtime); std::string property = name.utf8(runtime); if (property == "kind") { return makeString(runtime, "reference"); @@ -1352,6 +1353,7 @@ throw JSError(runtime, "This native return type is not supported by " NativeApiHostSetResult NativeApiReferenceHostObject::set(Runtime& runtime, const PropNameID& name, const Value& value) { + bridge_->requireOwnerRuntime(runtime); std::string property = name.utf8(runtime); auto index = parseArrayIndexProperty(property); if (property != "value" && !index) { @@ -1377,6 +1379,7 @@ throw JSError(runtime, "This native return type is not supported by " } Value NativeApiStructObjectHostObject::get(Runtime& runtime, const PropNameID& name) { + bridge_->requireOwnerRuntime(runtime); std::string property = name.utf8(runtime); if (property == "kind") { return makeString(runtime, info_ != nullptr && info_->isUnion ? "union" : "struct"); @@ -1438,6 +1441,7 @@ throw JSError(runtime, "This native return type is not supported by " NativeApiHostSetResult NativeApiStructObjectHostObject::set(Runtime& runtime, const PropNameID& name, const Value& value) { + bridge_->requireOwnerRuntime(runtime); std::string property = name.utf8(runtime); if (info_ == nullptr || data_ == nullptr) { throw JSError(runtime, "Struct is not initialized."); @@ -1466,6 +1470,7 @@ throw JSError(runtime, "This native return type is not supported by " } std::vector NativeApiStructObjectHostObject::getPropertyNames(Runtime& runtime) { + bridge_->requireOwnerRuntime(runtime); std::vector names; addPropertyName(runtime, names, "kind"); addPropertyName(runtime, names, "name"); diff --git a/NativeScript/ffi/objc/shared/bridge/host_objects/Class.mm b/NativeScript/ffi/objc/shared/bridge/host_objects/Class.mm index 61ffd4953..8f5f6beb7 100644 --- a/NativeScript/ffi/objc/shared/bridge/host_objects/Class.mm +++ b/NativeScript/ffi/objc/shared/bridge/host_objects/Class.mm @@ -19,6 +19,7 @@ static Class classRespondingToClassSelector(Class cls, SEL selector) { } Value get(Runtime& runtime, const PropNameID& name) override { + bridge_->requireOwnerRuntime(runtime); std::string property = name.utf8(runtime); if (property == "kind") { return makeString(runtime, "class"); @@ -300,6 +301,7 @@ throw JSError( } NativeApiHostSetResult set(Runtime& runtime, const PropNameID& name, const Value& value) override { + bridge_->requireOwnerRuntime(runtime); std::string property = name.utf8(runtime); Class cls = objc_lookUpClass(symbol_.runtimeName.c_str()); if (cls == nil) { @@ -335,6 +337,7 @@ throw JSError(runtime, } std::vector getPropertyNames(Runtime& runtime) override { + bridge_->requireOwnerRuntime(runtime); std::vector names; names.reserve(8); addPropertyName(runtime, names, "kind"); diff --git a/NativeScript/ffi/objc/shared/bridge/host_objects/Interop.mm b/NativeScript/ffi/objc/shared/bridge/host_objects/Interop.mm index 0d82e94da..0e700f6d0 100644 --- a/NativeScript/ffi/objc/shared/bridge/host_objects/Interop.mm +++ b/NativeScript/ffi/objc/shared/bridge/host_objects/Interop.mm @@ -13,6 +13,9 @@ backingValue_(std::move(backingValue)) {} ~NativeApiPointerHostObject() override { + if (bridge_ != nullptr) { + bridge_->releaseOrAbandonValue(backingValue_); + } if (adopted_ && pointer_ != nullptr) { if (bridge_ != nullptr) { bridge_->forgetPointerValue(pointer_); @@ -39,6 +42,7 @@ void clearWithoutFree() { } Value get(Runtime& runtime, const PropNameID& name) override { + bridge_->requireOwnerRuntime(runtime); std::string property = name.utf8(runtime); if (property == "kind") { return makeString(runtime, kind_); @@ -144,6 +148,7 @@ Value get(Runtime& runtime, const PropNameID& name) override { } std::vector getPropertyNames(Runtime& runtime) override { + bridge_->requireOwnerRuntime(runtime); std::vector names; names.reserve(3); addPropertyName(runtime, names, "kind"); @@ -186,6 +191,10 @@ Value get(Runtime& runtime, const PropNameID& name) override { backingValue_(std::move(backingValue)) {} ~NativeApiReferenceHostObject() override { + if (bridge_ != nullptr) { + bridge_->releaseOrAbandonValue(pendingValue_); + bridge_->releaseOrAbandonValue(backingValue_); + } for (id object : retainedObjects_) { [object release]; } @@ -208,6 +217,7 @@ void ensureStorage(Runtime& runtime, NativeApiType type, Value get(Runtime& runtime, const PropNameID& name) override; NativeApiHostSetResult set(Runtime& runtime, const PropNameID& name, const Value& value) override; std::vector getPropertyNames(Runtime& runtime) override { + bridge_->requireOwnerRuntime(runtime); std::vector names; addPropertyName(runtime, names, "kind"); addPropertyName(runtime, names, "value"); diff --git a/NativeScript/ffi/objc/shared/bridge/host_objects/Object.mm b/NativeScript/ffi/objc/shared/bridge/host_objects/Object.mm index 703e83f6b..2575dd6f5 100644 --- a/NativeScript/ffi/objc/shared/bridge/host_objects/Object.mm +++ b/NativeScript/ffi/objc/shared/bridge/host_objects/Object.mm @@ -12,6 +12,7 @@ } Value get(Runtime& runtime, const PropNameID& name) override { + bridge_->requireOwnerRuntime(runtime); std::string property = name.utf8(runtime); if (property == "next") { return Function::createFromHostFunction( @@ -24,6 +25,7 @@ Value get(Runtime& runtime, const PropNameID& name) override { } std::vector getPropertyNames(Runtime& runtime) override { + bridge_->requireOwnerRuntime(runtime); std::vector names; addPropertyName(runtime, names, "next"); return names; @@ -239,6 +241,7 @@ bool objectGetPathCanReadRuntimeProperty(id object, } Value get(Runtime& runtime, const PropNameID& name) override { + bridge_->requireOwnerRuntime(runtime); std::string property = name.utf8(runtime); if (property == "kind") { return makeString(runtime, "super"); @@ -301,6 +304,7 @@ throw JSError( } NativeApiHostSetResult set(Runtime& runtime, const PropNameID& name, const Value& value) override { + bridge_->requireOwnerRuntime(runtime); std::string property = name.utf8(runtime); if (receiver_ == nil || dispatchClass_ == Nil) { throw JSError(runtime, "Cannot set property on nil super."); @@ -342,6 +346,7 @@ throw JSError(runtime, } std::vector getPropertyNames(Runtime& runtime) override { + bridge_->requireOwnerRuntime(runtime); std::vector names; addPropertyName(runtime, names, "kind"); addPropertyName(runtime, names, "toString"); @@ -987,6 +992,7 @@ bool invokeEnginePrototypeSetter(Runtime& runtime, const std::string& property, } Value get(Runtime& runtime, const PropNameID& name) override { + bridge_->requireOwnerRuntime(runtime); std::string property = name.utf8(runtime); // Fast path: check expando cache first (hot path for method calls). @@ -1524,6 +1530,7 @@ throw JSError(runtime, } NativeApiHostSetResult set(Runtime& runtime, const PropNameID& name, const Value& value) override { + bridge_->requireOwnerRuntime(runtime); std::string property = name.utf8(runtime); if (object_ == nil) { throw JSError(runtime, "Cannot set property on nil object."); @@ -1664,6 +1671,7 @@ throw JSError( } std::vector getPropertyNames(Runtime& runtime) override { + bridge_->requireOwnerRuntime(runtime); std::vector names; names.reserve(6); addPropertyName(runtime, names, "kind"); diff --git a/NativeScript/ffi/objc/shared/bridge/host_objects/Protocol.mm b/NativeScript/ffi/objc/shared/bridge/host_objects/Protocol.mm index cf480883b..3d4beacc2 100644 --- a/NativeScript/ffi/objc/shared/bridge/host_objects/Protocol.mm +++ b/NativeScript/ffi/objc/shared/bridge/host_objects/Protocol.mm @@ -15,6 +15,7 @@ const NativeApiSymbol& symbol() const { return symbol_; } Value get(Runtime& runtime, const PropNameID& name) override { + bridge_->requireOwnerRuntime(runtime); std::string property = name.utf8(runtime); if (property == "kind") { return makeString(runtime, "protocol"); @@ -91,6 +92,7 @@ Value get(Runtime& runtime, const PropNameID& name) override { } std::vector getPropertyNames(Runtime& runtime) override { + bridge_->requireOwnerRuntime(runtime); std::vector names; addPropertyName(runtime, names, "kind"); addPropertyName(runtime, names, "name"); diff --git a/NativeScript/ffi/objc/shared/bridge/host_objects/Struct.mm b/NativeScript/ffi/objc/shared/bridge/host_objects/Struct.mm index 2087f90c8..0884eb2b8 100644 --- a/NativeScript/ffi/objc/shared/bridge/host_objects/Struct.mm +++ b/NativeScript/ffi/objc/shared/bridge/host_objects/Struct.mm @@ -34,6 +34,13 @@ } } + ~NativeApiStructObjectHostObject() override { + if (bridge_ != nullptr) { + bridge_->releaseOrAbandonValue(backingValue_); + bridge_->releaseOrAbandonValue(pointerBackingValues_); + } + } + void* data() const { return data_; } std::shared_ptr info() const { return info_; } std::shared_ptr> storageOwner() const { diff --git a/packages/react-native/src/index.ts b/packages/react-native/src/index.ts index fb4548c6c..e076cb70d 100644 --- a/packages/react-native/src/index.ts +++ b/packages/react-native/src/index.ts @@ -848,6 +848,101 @@ export function installWorklets( return true; } +const nativeObjectHandleKey = "__nativeScriptObjectHandle"; + +function nativeHandleForNSObject(value: unknown): string | undefined { + "worklet"; + + if (value == null) { + return undefined; + } + const interop = (globalThis as Record).interop; + const pointer = interop?.handleof?.(value); + if (!pointer) { + return undefined; + } + if (typeof pointer.toHexString === "function") { + return pointer.toHexString(); + } + if (typeof pointer.address === "string") { + return pointer.address; + } + if (typeof pointer.address === "number") { + return String(pointer.address); + } + if (typeof pointer.toNumber === "function") { + return String(pointer.toNumber()); + } + return undefined; +} + +// Worklets hands a returned native object to this runtime by reference, still +// bound to the UI runtime. Only its handle crosses; the +1 retained here keeps +// it alive until nativeObjectsFromHandles adopts it. +function nativeObjectsToHandles(value: unknown): unknown { + "worklet"; + + if (value === null || typeof value !== "object") { + return value; + } + if (Array.isArray(value)) { + return value.map(nativeObjectsToHandles); + } + const prototype = Object.getPrototypeOf(value); + if (prototype === Object.prototype || prototype === null) { + const result: Record = {}; + for (const key of Object.keys(value)) { + result[key] = nativeObjectsToHandles( + (value as Record)[key], + ); + } + return result; + } + const { NSObject, CFRetain } = globalThis as Record; + if (typeof NSObject !== "function" || !(value instanceof NSObject)) { + return value; + } + const handle = nativeHandleForNSObject(value); + if (handle === undefined) { + return value; + } + CFRetain(value); + return { [nativeObjectHandleKey]: handle }; +} + +function nativeObjectsFromHandles(value: unknown): unknown { + if (value === null || typeof value !== "object") { + return value; + } + if (Array.isArray(value)) { + return value.map(nativeObjectsFromHandles); + } + const handle = (value as Record)[nativeObjectHandleKey]; + if (typeof handle === "string") { + const interop = (globalThis as Record).interop; + return interop.Pointer(Number(handle)).takeRetainedValue(); + } + if (Object.getPrototypeOf(value) !== Object.prototype) { + return value; + } + const result: Record = {}; + for (const key of Object.keys(value)) { + result[key] = nativeObjectsFromHandles( + (value as Record)[key], + ); + } + return result; +} + +function runWorkletReturningHandles( + callback: (...args: Args) => ReturnValue, + args: Args, +): unknown { + "worklet"; + + return nativeObjectsToHandles(callback(...args)); +} + export function scheduleOnUI( callback: (...args: Args) => ReturnValue | Promise, ...args: Args @@ -865,7 +960,9 @@ export function scheduleOnUI( "NativeScript.scheduleOnUI requires a worklet callback", ); } - return worklets.runOnUIAsync(callback, ...args); + return worklets + .runOnUIAsync(runWorkletReturningHandles, callback, args) + .then(nativeObjectsFromHandles) as Promise; } function callbackInvoker(