#include "JsiMain.h" #include #include #include #include #include #include #include using namespace facebook; class OnnxruntimeModule : public jni::JavaClass { public: static constexpr auto kJavaDescriptor = "Lai/onnxruntime/reactnative/OnnxruntimeModule;"; static void registerNatives() { javaClassStatic()->registerNatives( {makeNativeMethod("nativeInstall", OnnxruntimeModule::nativeInstall), makeNativeMethod("nativeCleanup", OnnxruntimeModule::nativeCleanup)}); } private: static jlong nativeInstall( jni::alias_ref thiz, jlong jsContextNativePointer, jni::alias_ref jsCallInvokerHolder) { auto runtime = reinterpret_cast(jsContextNativePointer); auto jsCallInvoker = jsCallInvokerHolder->cthis()->getCallInvoker(); auto env = onnxruntimejsi::install(*runtime, jsCallInvoker); return reinterpret_cast( new std::shared_ptr(std::move(env))); } static void nativeCleanup(jni::alias_ref thiz, jlong envHandle) { auto env = std::unique_ptr>( reinterpret_cast*>(envHandle)); // invalidate() runs first because this hook is not guaranteed to run on the JS thread: it // stops dispatching to a runtime that is going away and releases any thread blocked on it. if (env && *env) { (*env)->invalidate(); } } }; JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void*) { return jni::initialize( vm, [] { OnnxruntimeModule::registerNatives(); }); }