Skip to content

Commit 238a7ba

Browse files
committed
added emulator detection
1 parent 46ea659 commit 238a7ba

6 files changed

Lines changed: 83 additions & 63 deletions

File tree

packages/react-native-executorch/android/src/main/cpp/ETInstallerModule.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <jni.h>
66
#include <jsi/jsi.h>
7+
#include <sys/system_properties.h>
78

89
namespace rnexecutorch {
910
JavaVM *java_machine;
@@ -64,8 +65,18 @@ void ETInstallerModule::injectJSIBindings() {
6465
return std::vector<std::byte>(dataBytePtr, dataBytePtr + size);
6566
};
6667

68+
auto isEmulator = []() {
69+
char fingerprint[PROP_VALUE_MAX] = {0};
70+
char hardware[PROP_VALUE_MAX] = {0};
71+
__system_property_get("ro.build.fingerprint", fingerprint);
72+
__system_property_get("ro.hardware", hardware);
73+
std::string fp(fingerprint);
74+
std::string hw(hardware);
75+
return fp.find("generic") == 0 || hw == "goldfish" || hw == "ranchu";
76+
}();
77+
6778
RnExecutorchInstaller::injectJSIBindings(jsiRuntime_, jsCallInvoker_,
68-
fetchDataByUrl);
79+
fetchDataByUrl, isEmulator);
6980
}
7081
} // namespace rnexecutorch
7182

packages/react-native-executorch/common/rnexecutorch/RnExecutorchInstaller.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,12 @@ FetchUrlFunc_t fetchUrlFunc;
3434

3535
void RnExecutorchInstaller::injectJSIBindings(
3636
jsi::Runtime *jsiRuntime, std::shared_ptr<react::CallInvoker> jsCallInvoker,
37-
FetchUrlFunc_t fetchDataFromUrl) {
37+
FetchUrlFunc_t fetchDataFromUrl, bool isEmulator) {
3838
fetchUrlFunc = fetchDataFromUrl;
3939

40+
jsiRuntime->global().setProperty(*jsiRuntime, "__rne_isEmulator",
41+
jsi::Value(isEmulator));
42+
4043
jsiRuntime->global().setProperty(
4144
*jsiRuntime, "loadStyleTransfer",
4245
RnExecutorchInstaller::loadModel<models::style_transfer::StyleTransfer>(

packages/react-native-executorch/common/rnexecutorch/RnExecutorchInstaller.h

Lines changed: 54 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class RnExecutorchInstaller {
2424
static void
2525
injectJSIBindings(jsi::Runtime *jsiRuntime,
2626
std::shared_ptr<react::CallInvoker> jsCallInvoker,
27-
FetchUrlFunc_t fetchDataFromUrl);
27+
FetchUrlFunc_t fetchDataFromUrl, bool isEmulator);
2828

2929
private:
3030
template <typename ModelT>
@@ -56,72 +56,67 @@ class RnExecutorchInstaller {
5656
// access), then dispatch the heavy model construction to a background
5757
// thread and return a Promise.
5858
auto constructorArgs =
59-
meta::createConstructorArgsWithCallInvoker<ModelT>(
60-
args, runtime, jsCallInvoker);
59+
meta::createConstructorArgsWithCallInvoker<ModelT>(args, runtime,
60+
jsCallInvoker);
6161

6262
return Promise::createPromise(
6363
runtime, jsCallInvoker,
64-
[jsCallInvoker,
65-
constructorArgs =
66-
std::move(constructorArgs)](std::shared_ptr<Promise> promise) {
67-
threads::GlobalThreadPool::detach(
68-
[jsCallInvoker, promise,
69-
constructorArgs = std::move(constructorArgs)]() {
70-
try {
71-
auto modelImplementationPtr = std::apply(
72-
[](auto &&...unpackedArgs) {
73-
return std::make_shared<ModelT>(
74-
std::forward<decltype(unpackedArgs)>(
75-
unpackedArgs)...);
76-
},
77-
std::move(constructorArgs));
64+
[jsCallInvoker, constructorArgs = std::move(constructorArgs)](
65+
std::shared_ptr<Promise> promise) {
66+
threads::GlobalThreadPool::detach([jsCallInvoker, promise,
67+
constructorArgs = std::move(
68+
constructorArgs)]() {
69+
try {
70+
auto modelImplementationPtr = std::apply(
71+
[](auto &&...unpackedArgs) {
72+
return std::make_shared<ModelT>(
73+
std::forward<decltype(unpackedArgs)>(
74+
unpackedArgs)...);
75+
},
76+
std::move(constructorArgs));
7877

79-
auto modelHostObject =
80-
std::make_shared<ModelHostObject<ModelT>>(
81-
modelImplementationPtr, jsCallInvoker);
78+
auto modelHostObject =
79+
std::make_shared<ModelHostObject<ModelT>>(
80+
modelImplementationPtr, jsCallInvoker);
8281

83-
auto memoryLowerBound =
84-
modelImplementationPtr->getMemoryLowerBound();
82+
auto memoryLowerBound =
83+
modelImplementationPtr->getMemoryLowerBound();
8584

86-
jsCallInvoker->invokeAsync(
87-
[promise, modelHostObject,
88-
memoryLowerBound](jsi::Runtime &rt) {
89-
auto jsiObject =
90-
jsi::Object::createFromHostObject(
91-
rt, modelHostObject);
92-
jsiObject.setExternalMemoryPressure(
93-
rt, memoryLowerBound);
94-
promise->resolve(std::move(jsiObject));
95-
});
96-
} catch (const rnexecutorch::RnExecutorchError &e) {
97-
auto code = e.getNumericCode();
98-
auto msg = std::string(e.what());
99-
jsCallInvoker->invokeAsync(
100-
[promise, code, msg](jsi::Runtime &rt) {
101-
jsi::Object errorData(rt);
102-
errorData.setProperty(rt, "code", code);
103-
errorData.setProperty(
104-
rt, "message",
105-
jsi::String::createFromUtf8(rt, msg));
106-
promise->reject(
107-
jsi::Value(rt, std::move(errorData)));
108-
});
109-
} catch (const std::runtime_error &e) {
110-
jsCallInvoker->invokeAsync(
111-
[promise, msg = std::string(e.what())]() {
112-
promise->reject(msg);
113-
});
114-
} catch (const std::exception &e) {
115-
jsCallInvoker->invokeAsync(
116-
[promise, msg = std::string(e.what())]() {
117-
promise->reject(msg);
118-
});
119-
} catch (...) {
120-
jsCallInvoker->invokeAsync([promise]() {
121-
promise->reject(std::string("Unknown error"));
85+
jsCallInvoker->invokeAsync([promise, modelHostObject,
86+
memoryLowerBound](
87+
jsi::Runtime &rt) {
88+
auto jsiObject = jsi::Object::createFromHostObject(
89+
rt, modelHostObject);
90+
jsiObject.setExternalMemoryPressure(rt, memoryLowerBound);
91+
promise->resolve(std::move(jsiObject));
92+
});
93+
} catch (const rnexecutorch::RnExecutorchError &e) {
94+
auto code = e.getNumericCode();
95+
auto msg = std::string(e.what());
96+
jsCallInvoker->invokeAsync([promise, code,
97+
msg](jsi::Runtime &rt) {
98+
jsi::Object errorData(rt);
99+
errorData.setProperty(rt, "code", code);
100+
errorData.setProperty(
101+
rt, "message", jsi::String::createFromUtf8(rt, msg));
102+
promise->reject(jsi::Value(rt, std::move(errorData)));
103+
});
104+
} catch (const std::runtime_error &e) {
105+
jsCallInvoker->invokeAsync(
106+
[promise, msg = std::string(e.what())]() {
107+
promise->reject(msg);
108+
});
109+
} catch (const std::exception &e) {
110+
jsCallInvoker->invokeAsync(
111+
[promise, msg = std::string(e.what())]() {
112+
promise->reject(msg);
122113
});
123-
}
114+
} catch (...) {
115+
jsCallInvoker->invokeAsync([promise]() {
116+
promise->reject(std::string("Unknown error"));
124117
});
118+
}
119+
});
125120
});
126121
});
127122
}

packages/react-native-executorch/ios/RnExecutorch/ETInstaller.mm

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#import <React/RCTCallInvoker.h>
66
#import <ReactCommon/RCTTurboModule.h>
7+
#import <TargetConditionals.h>
78
#include <rnexecutorch/RnExecutorchInstaller.h>
89
#include <stdexcept>
910

@@ -41,8 +42,9 @@ @implementation ETInstaller
4142
throw std::runtime_error("Error fetching data from a url");
4243
}
4344
};
45+
bool isEmulator = TARGET_OS_SIMULATOR;
4446
rnexecutorch::RnExecutorchInstaller::injectJSIBindings(
45-
jsiRuntime, jsCallInvoker, fetchUrl);
47+
jsiRuntime, jsCallInvoker, fetchUrl, isEmulator);
4648

4749
NSLog(@"Successfully installed JSI bindings for react-native-executorch!");
4850
return @true;

packages/react-native-executorch/src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ declare global {
105105
symbols: string,
106106
independentCharacters?: boolean
107107
) => Promise<any>;
108+
var __rne_isEmulator: () => boolean;
108109
}
109110
// eslint-disable no-var
110111

@@ -124,7 +125,8 @@ if (
124125
global.loadSpeechToText == null ||
125126
global.loadTextToSpeechKokoro == null ||
126127
global.loadOCR == null ||
127-
global.loadVerticalOCR == null
128+
global.loadVerticalOCR == null ||
129+
global.__rne_isEmulator == null
128130
) {
129131
if (!ETInstallerNativeModule) {
130132
throw new Error(

packages/react-native-executorch/src/utils/ResourceFetcherUtils.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,11 @@ export namespace ResourceFetcherUtils {
206206
return 'UNKNOWN';
207207
}
208208

209+
export function isEmulator(): boolean {
210+
// eslint-disable-next-line camelcase
211+
return !!(global as any).__rne_isEmulator;
212+
}
213+
209214
function getModelNameFromUri(uri: string): string {
210215
const knownName = getModelNameForUrl(uri);
211216
if (knownName) {
@@ -228,6 +233,8 @@ export namespace ResourceFetcherUtils {
228233
body: JSON.stringify({
229234
modelName: getModelNameFromUri(uri),
230235
countryCode: getCountryCode(),
236+
isEmulator: isEmulator(),
237+
libVersion: LIB_VERSION,
231238
}),
232239
});
233240
} catch (e) {}

0 commit comments

Comments
 (0)