diff --git a/packages/react-native-executorch/android/src/main/cpp/ETInstallerModule.cpp b/packages/react-native-executorch/android/src/main/cpp/ETInstallerModule.cpp index d69af8634b..1a637b15d1 100644 --- a/packages/react-native-executorch/android/src/main/cpp/ETInstallerModule.cpp +++ b/packages/react-native-executorch/android/src/main/cpp/ETInstallerModule.cpp @@ -2,6 +2,8 @@ #include +#include "EmulatorDetection.h" + #include #include @@ -64,8 +66,10 @@ void ETInstallerModule::injectJSIBindings() { return std::vector(dataBytePtr, dataBytePtr + size); }; + auto _isEmulator = isEmulator(); + RnExecutorchInstaller::injectJSIBindings(jsiRuntime_, jsCallInvoker_, - fetchDataByUrl); + fetchDataByUrl, _isEmulator); } } // namespace rnexecutorch diff --git a/packages/react-native-executorch/android/src/main/cpp/EmulatorDetection.h b/packages/react-native-executorch/android/src/main/cpp/EmulatorDetection.h new file mode 100644 index 0000000000..63cc17e398 --- /dev/null +++ b/packages/react-native-executorch/android/src/main/cpp/EmulatorDetection.h @@ -0,0 +1,36 @@ +#pragma once + +#include +#include + +namespace rnexecutorch { + +inline bool isEmulator() { + auto readProp = [](const char *key) -> std::string { +#if __ANDROID_API__ >= 26 + const prop_info *pi = __system_property_find(key); + if (pi == nullptr) { + return ""; + } + std::string result; + __system_property_read_callback( + pi, + [](void *cookie, const char * /*__name*/, const char *value, + uint32_t /*__serial*/) { + *static_cast(cookie) = value; + }, + &result); + return result; +#else + char value[PROP_VALUE_MAX] = {0}; + __system_property_get(key, value); + return {value}; +#endif + }; + + std::string fp = readProp("ro.build.fingerprint"); + std::string hw = readProp("ro.hardware"); + return fp.find("generic") == 0 || hw == "goldfish" || hw == "ranchu"; +} + +} // namespace rnexecutorch diff --git a/packages/react-native-executorch/common/rnexecutorch/RnExecutorchInstaller.cpp b/packages/react-native-executorch/common/rnexecutorch/RnExecutorchInstaller.cpp index 7a4426e06f..2d2bdd3d48 100644 --- a/packages/react-native-executorch/common/rnexecutorch/RnExecutorchInstaller.cpp +++ b/packages/react-native-executorch/common/rnexecutorch/RnExecutorchInstaller.cpp @@ -10,9 +10,9 @@ #include #include #include -#include #include #include +#include #include #include #include @@ -33,9 +33,12 @@ FetchUrlFunc_t fetchUrlFunc; void RnExecutorchInstaller::injectJSIBindings( jsi::Runtime *jsiRuntime, std::shared_ptr jsCallInvoker, - FetchUrlFunc_t fetchDataFromUrl) { + FetchUrlFunc_t fetchDataFromUrl, bool isEmulator) { fetchUrlFunc = fetchDataFromUrl; + jsiRuntime->global().setProperty(*jsiRuntime, "__rne_isEmulator", + jsi::Value(isEmulator)); + jsiRuntime->global().setProperty( *jsiRuntime, "loadStyleTransfer", RnExecutorchInstaller::loadModel( diff --git a/packages/react-native-executorch/common/rnexecutorch/RnExecutorchInstaller.h b/packages/react-native-executorch/common/rnexecutorch/RnExecutorchInstaller.h index d5c98763d0..c6bb72ed73 100644 --- a/packages/react-native-executorch/common/rnexecutorch/RnExecutorchInstaller.h +++ b/packages/react-native-executorch/common/rnexecutorch/RnExecutorchInstaller.h @@ -22,7 +22,7 @@ class RnExecutorchInstaller { static void injectJSIBindings(jsi::Runtime *jsiRuntime, std::shared_ptr jsCallInvoker, - FetchUrlFunc_t fetchDataFromUrl); + FetchUrlFunc_t fetchDataFromUrl, bool isEmulator); private: template diff --git a/packages/react-native-executorch/ios/RnExecutorch/ETInstaller.mm b/packages/react-native-executorch/ios/RnExecutorch/ETInstaller.mm index dcb40b29d8..2a8bc519ba 100644 --- a/packages/react-native-executorch/ios/RnExecutorch/ETInstaller.mm +++ b/packages/react-native-executorch/ios/RnExecutorch/ETInstaller.mm @@ -4,6 +4,7 @@ #import #import +#import #include #include @@ -41,8 +42,9 @@ @implementation ETInstaller throw std::runtime_error("Error fetching data from a url"); } }; + bool isEmulator = TARGET_OS_SIMULATOR; rnexecutorch::RnExecutorchInstaller::injectJSIBindings( - jsiRuntime, jsCallInvoker, fetchUrl); + jsiRuntime, jsCallInvoker, fetchUrl, isEmulator); NSLog(@"Successfully installed JSI bindings for react-native-executorch!"); return @true; diff --git a/packages/react-native-executorch/src/index.ts b/packages/react-native-executorch/src/index.ts index a42881f450..b17ce9016e 100644 --- a/packages/react-native-executorch/src/index.ts +++ b/packages/react-native-executorch/src/index.ts @@ -46,6 +46,8 @@ declare global { symbols: string, independentCharacters?: boolean ) => any; + // eslint-disable-next-line camelcase + var __rne_isEmulator: boolean; } // eslint-disable no-var if ( @@ -63,7 +65,8 @@ if ( global.loadSpeechToText == null || global.loadTextToSpeechKokoro == null || global.loadOCR == null || - global.loadVerticalOCR == null + global.loadVerticalOCR == null || + global.__rne_isEmulator == null ) { if (!ETInstallerNativeModule) { throw new Error( diff --git a/packages/react-native-executorch/src/utils/ResourceFetcherUtils.ts b/packages/react-native-executorch/src/utils/ResourceFetcherUtils.ts index 207adce9ee..7e2cda6c7a 100644 --- a/packages/react-native-executorch/src/utils/ResourceFetcherUtils.ts +++ b/packages/react-native-executorch/src/utils/ResourceFetcherUtils.ts @@ -171,6 +171,10 @@ export namespace ResourceFetcherUtils { } } + export function isEmulator(): boolean { + return global.__rne_isEmulator; + } + export async function checkFileExists(fileUri: string) { const fileInfo = await getInfoAsync(fileUri); return fileInfo.exists;