diff --git a/change/react-native-windows-858858a8-c4cb-451d-a968-acc43a7d1611.json b/change/react-native-windows-858858a8-c4cb-451d-a968-acc43a7d1611.json new file mode 100644 index 00000000000..0d14a1741ae --- /dev/null +++ b/change/react-native-windows-858858a8-c4cb-451d-a968-acc43a7d1611.json @@ -0,0 +1,7 @@ +{ + "type": "prerelease", + "comment": "TurboModule jsRespresentation_ cache fails to set property", + "packageName": "react-native-windows", + "email": "30809111+acoates-ms@users.noreply.github.com", + "dependentChangeType": "patch" +} diff --git a/vnext/Microsoft.ReactNative/TurboModulesProvider.cpp b/vnext/Microsoft.ReactNative/TurboModulesProvider.cpp index 4958d057187..81cd2681ad4 100644 --- a/vnext/Microsoft.ReactNative/TurboModulesProvider.cpp +++ b/vnext/Microsoft.ReactNative/TurboModulesProvider.cpp @@ -165,7 +165,10 @@ class TurboModuleImpl : public facebook::react::TurboModule { if (m_hostObjectWrapper) { return m_hostObjectWrapper->get(runtime, propName); } + return __super::get(runtime, propName); + } + facebook::jsi::Value create(facebook::jsi::Runtime &runtime, const facebook::jsi::PropNameID &propName) override { // it is not safe to assume that "runtime" never changes, so members are not cached here std::string key = propName.utf8(runtime); diff --git a/vnext/ReactCommon/TEMP_UntilReactCommonUpdate/react/nativemodule/core/ReactCommon/TurboModule.h b/vnext/ReactCommon/TEMP_UntilReactCommonUpdate/react/nativemodule/core/ReactCommon/TurboModule.h deleted file mode 100644 index f9221d83e36..00000000000 --- a/vnext/ReactCommon/TEMP_UntilReactCommonUpdate/react/nativemodule/core/ReactCommon/TurboModule.h +++ /dev/null @@ -1,165 +0,0 @@ -/* - * 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. - */ - -#pragma once - -#include -#include -#include - -#include - -#include -#include - -namespace facebook::react { - -/** - * For now, support the same set of return types as existing impl. - * This can be improved to support richer typed objects. - */ -enum TurboModuleMethodValueKind { - VoidKind, - BooleanKind, - NumberKind, - StringKind, - ObjectKind, - ArrayKind, - FunctionKind, - PromiseKind, -}; - -/** - * Determines TurboModuleMethodValueKind based on the jsi::Value type. - */ -TurboModuleMethodValueKind getTurboModuleMethodValueKind( - jsi::Runtime& rt, - const jsi::Value* value); - -class TurboCxxModule; -class TurboModuleBinding; - -/** - * Base HostObject class for every module to be exposed to JS - */ -class JSI_EXPORT TurboModule : public jsi::HostObject { - public: - TurboModule(std::string name, std::shared_ptr jsInvoker); - - // DO NOT OVERRIDE - it will become final in a future release. - // This method provides automatic caching of properties on the TurboModule's - // JS representation. To customize lookup of properties, override `create`. - // Note: keep this method declared inline to avoid conflicts - // between RTTI and non-RTTI compilation units - jsi::Value get(jsi::Runtime& runtime, const jsi::PropNameID& propName) - override { - auto prop = create(runtime, propName); - // If we have a JS wrapper, cache the result of this lookup - // We don't cache misses, to allow for methodMap_ to dynamically be - // extended - // [Windows] Reenable once https://github.com/microsoft/react-native-windows/issues/14128 is fixed -#ifndef WINAPI_FAMILY - if (jsRepresentation_ && !prop.isUndefined()) { - jsRepresentation_->lock(runtime).asObject(runtime).setProperty( - runtime, propName, prop); - } -#endif - return prop; - } - - std::vector getPropertyNames( - jsi::Runtime& runtime) override { - std::vector result; - result.reserve(methodMap_.size()); - for (auto it = methodMap_.cbegin(); it != methodMap_.cend(); ++it) { - result.push_back(jsi::PropNameID::forUtf8(runtime, it->first)); - } - return result; - } - - protected: - const std::string name_; - std::shared_ptr jsInvoker_; - - struct MethodMetadata { - size_t argCount; - jsi::Value (*invoker)( - jsi::Runtime& rt, - TurboModule& turboModule, - const jsi::Value* args, - size_t count); - }; - std::unordered_map methodMap_; - std::unordered_map> - eventEmitterMap_; - - using ArgFactory = - std::function& args)>; - - /** - * Calls RCTDeviceEventEmitter.emit to JavaScript, with given event name and - * an optional list of arguments. - * If present, argFactory is a callback used to construct extra arguments, - * e.g. - * - * emitDeviceEvent(rt, "myCustomEvent", - * [](jsi::Runtime& rt, std::vector& args) { - * args.emplace_back(jsi::Value(true)); - * args.emplace_back(jsi::Value(42)); - * }); - */ - void emitDeviceEvent( - const std::string& eventName, - ArgFactory&& argFactory = nullptr); - - // Backwards compatibility version - void emitDeviceEvent( - jsi::Runtime& /*runtime*/, - - const std::string& eventName, - ArgFactory&& argFactory = nullptr) { - emitDeviceEvent(eventName, std::move(argFactory)); - } - - virtual jsi::Value create( - jsi::Runtime& runtime, - const jsi::PropNameID& propName) { - std::string propNameUtf8 = propName.utf8(runtime); - if (auto methodIter = methodMap_.find(propNameUtf8); - methodIter != methodMap_.end()) { - const MethodMetadata& meta = methodIter->second; - return jsi::Function::createFromHostFunction( - runtime, - propName, - static_cast(meta.argCount), - [this, meta]( - jsi::Runtime& rt, - [[maybe_unused]] const jsi::Value& thisVal, - const jsi::Value* args, - size_t count) { return meta.invoker(rt, *this, args, count); }); - } else if (auto eventEmitterIter = eventEmitterMap_.find(propNameUtf8); - eventEmitterIter != eventEmitterMap_.end()) { - return eventEmitterIter->second->get(runtime, jsInvoker_); - } else { - // Neither Method nor EventEmitter were found, let JS decide what to do - return jsi::Value::undefined(); - } - } - - private: - friend class TurboModuleBinding; - std::unique_ptr jsRepresentation_; -}; - -/** - * An app/platform-specific provider function to get an instance of a module - * given a name. - */ -using TurboModuleProviderFunctionType = - std::function(const std::string& name)>; - -} // namespace facebook::react diff --git a/vnext/overrides.json b/vnext/overrides.json index efa658f0b0b..77127e35333 100644 --- a/vnext/overrides.json +++ b/vnext/overrides.json @@ -117,13 +117,6 @@ "baseHash": "5678d5ac212112184b7e63b5d58c6ae713e03cd5", "issue": 12195 }, - { - "type": "patch", - "file": "ReactCommon/TEMP_UntilReactCommonUpdate/react/nativemodule/core/ReactCommon/TurboModule.h", - "baseFile": "packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/TurboModule.h", - "baseHash": "a0bc700b3960f771cf6d4e63be30ab45f6ef3683", - "issue": 14128 - }, { "type": "patch", "file": "ReactCommon/TEMP_UntilReactCommonUpdate/react/nativemodule/core/ReactCommon/TurboModuleWithJSIBindings.cpp",