Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Workaround #198

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
4 changes: 4 additions & 0 deletions react-native-pytorch-core/ios/Jsi/PyTorchCoreJSI.mm
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ - (void)install {

torchlive::install(*(facebook::jsi::Runtime *)cxxBridge.runtime, runtimeExecutor);
}
else {
[NSThread sleepForTimeInterval:0.1]
[self install]
}
}

@end
67 changes: 42 additions & 25 deletions react-native-pytorch-core/src/install-jsi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,49 @@ declare global {
var __torchlive__: object | undefined;
}

if (global.__torchlive__ == null) {
const PlayTorchJSIModule = NativeModules.PyTorchCoreJSI;
function TryInstallJSI() {
if (global.__torchlive__ == null) {
const PlayTorchJSIModule = NativeModules.PyTorchCoreJSI;

if (PlayTorchJSIModule == null) {
throw new Error(
'PlayTorchJSIModule not found. Maybe try rebuilding the app.',
);
}
if (PlayTorchJSIModule == null) {
/*
throw new Error(
'PlayTorchJSIModule not found. Maybe try rebuilding the app.',
);
*/
//return instead of throwing error and crashing
return;
}

// Check if we are running on-device (JSI)
if (global.nativeCallSyncHook == null || PlayTorchJSIModule.install == null) {
throw new Error(
'Failed to install react-native-pytorch-core: React Native is not running on-device. The PlayTorchJSIModule can only be used when synchronous method invocations (JSI) are possible. If you are using a remote debugger (e.g. Chrome), switch to an on-device debugger (e.g. Flipper) instead.',
);
}
// Check if we are running on-device (JSI)
if (global.nativeCallSyncHook == null || PlayTorchJSIModule.install == null) {
throw new Error(
'Failed to install react-native-pytorch-core: React Native is not running on-device. The PlayTorchJSIModule can only be used when synchronous method invocations (JSI) are possible. If you are using a remote debugger (e.g. Chrome), switch to an on-device debugger (e.g. Flipper) instead.',
);
}

// Call the synchronous blocking install() function
const result = PlayTorchJSIModule.install();
if (result !== true)
throw new Error(
`Failed to install react-native-pytorch-core: The native PlayTorchJSIModule could not be installed! Looks like something went wrong when installing JSI bindings: ${result}`,
);

// Check again if the constructor now exists. If not, throw an error.
if (global.__torchlive__ == null)
throw new Error(
'Failed to install react-native-pytorch-core, the native initializer function does not exist. Are you trying to use PlayTorchJSIModule from different JS Runtimes?',
);
// Call the synchronous blocking install() function
const result = PlayTorchJSIModule.install();
if (result !== true)
throw new Error(
`Failed to install react-native-pytorch-core: The native PlayTorchJSIModule could not be installed! Looks like something went wrong when installing JSI bindings: ${result}`,
);

// Check again if the constructor now exists. If not, throw an error.
if (global.__torchlive__ == null)
throw new Error(
'Failed to install react-native-pytorch-core, the native initializer function does not exist. Are you trying to use PlayTorchJSIModule from different JS Runtimes?',
);
}
}
TryInstallJSI()

//Use this function to ensure __torchlive__ exists before calling other PyTorch code
global.WaitForTorchLive = async () => { return new Promise( async (resolve, reject) => {
if (global.__torchlive__ != null) {
resolve();
}
else {
setTimeout(async () => {await WaitForTorchLive(); resolve()}, 100)
}
})}