/* * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ #import "RCTRuntimeExecutorFromBridge.h" #import #import #import #import using namespace facebook::react; @interface RCTBridge () - (std::shared_ptr)jsMessageThread; - (void)invokeAsync:(std::function &&)func; @end RuntimeExecutor RCTRuntimeExecutorFromBridge(RCTBridge *bridge) { RCTAssert(bridge, @"RCTRuntimeExecutorFromBridge: Bridge must not be nil."); auto bridgeWeakWrapper = wrapManagedObjectWeakly([bridge batchedBridge] ?: bridge); RuntimeExecutor runtimeExecutor = [bridgeWeakWrapper]( std::function &&callback) { RCTBridge *bridge = unwrapManagedObjectWeakly(bridgeWeakWrapper); RCTAssert(bridge, @"RCTRuntimeExecutorFromBridge: Bridge must not be nil at the moment of scheduling a call."); [bridge invokeAsync:[bridgeWeakWrapper, callback = std::move(callback)]() { RCTCxxBridge *batchedBridge = (RCTCxxBridge *)unwrapManagedObjectWeakly(bridgeWeakWrapper); RCTAssert(batchedBridge, @"RCTRuntimeExecutorFromBridge: Bridge must not be nil at the moment of invocation."); if (!batchedBridge) { return; } auto runtime = (facebook::jsi::Runtime *)(batchedBridge.runtime); RCTAssert( runtime, @"RCTRuntimeExecutorFromBridge: Bridge must have a valid jsi::Runtime at the moment of invocation."); if (!runtime) { return; } callback(*runtime); }]; }; return runtimeExecutor; }