Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include <rnexecutorch/RnExecutorchInstaller.h>

#include "EmulatorDetection.h"

#include <jni.h>
#include <jsi/jsi.h>

Expand Down Expand Up @@ -64,8 +66,10 @@ void ETInstallerModule::injectJSIBindings() {
return std::vector<std::byte>(dataBytePtr, dataBytePtr + size);
};

auto _isEmulator = isEmulator();

RnExecutorchInstaller::injectJSIBindings(jsiRuntime_, jsCallInvoker_,
fetchDataByUrl);
fetchDataByUrl, _isEmulator);
}
} // namespace rnexecutorch

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once

#include <string>
#include <sys/system_properties.h>

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<std::string *>(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
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
#include <rnexecutorch/models/object_detection/ObjectDetection.h>
#include <rnexecutorch/models/ocr/OCR.h>
#include <rnexecutorch/models/speech_to_text/SpeechToText.h>
#include <rnexecutorch/models/text_to_speech/TextToSpeech.h>
#include <rnexecutorch/models/style_transfer/StyleTransfer.h>
#include <rnexecutorch/models/text_to_image/TextToImage.h>
#include <rnexecutorch/models/text_to_speech/TextToSpeech.h>
#include <rnexecutorch/models/vertical_ocr/VerticalOCR.h>
#include <rnexecutorch/models/voice_activity_detection/VoiceActivityDetection.h>
#include <rnexecutorch/threads/GlobalThreadPool.h>
Expand All @@ -33,9 +33,12 @@ FetchUrlFunc_t fetchUrlFunc;

void RnExecutorchInstaller::injectJSIBindings(
jsi::Runtime *jsiRuntime, std::shared_ptr<react::CallInvoker> 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<models::style_transfer::StyleTransfer>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class RnExecutorchInstaller {
static void
injectJSIBindings(jsi::Runtime *jsiRuntime,
std::shared_ptr<react::CallInvoker> jsCallInvoker,
FetchUrlFunc_t fetchDataFromUrl);
FetchUrlFunc_t fetchDataFromUrl, bool isEmulator);

private:
template <typename ModelT>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#import <React/RCTCallInvoker.h>
#import <ReactCommon/RCTTurboModule.h>
#import <TargetConditionals.h>
#include <rnexecutorch/RnExecutorchInstaller.h>
#include <stdexcept>

Expand Down Expand Up @@ -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;
Expand Down
5 changes: 4 additions & 1 deletion packages/react-native-executorch/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading