// Copyright © 2021-present 650 Industries, Inc. (aka Expo) #include "JSIInteropModuleRegistry.h" #include "ExpoModulesHostObject.h" #include #include #include namespace jni = facebook::jni; namespace jsi = facebook::jsi; namespace expo { jni::local_ref JSIInteropModuleRegistry::initHybrid(jni::alias_ref jThis) { return makeCxxInstance(jThis); } void JSIInteropModuleRegistry::registerNatives() { registerHybrid({ makeNativeMethod("initHybrid", JSIInteropModuleRegistry::initHybrid), makeNativeMethod("installJSI", JSIInteropModuleRegistry::installJSI), makeNativeMethod("installJSIForTests", JSIInteropModuleRegistry::installJSIForTests), makeNativeMethod("evaluateScript", JSIInteropModuleRegistry::evaluateScript), makeNativeMethod("global", JSIInteropModuleRegistry::global), }); } JSIInteropModuleRegistry::JSIInteropModuleRegistry(jni::alias_ref jThis) : javaPart_(jni::make_global(jThis)) {} void JSIInteropModuleRegistry::installJSI( jlong jsRuntimePointer, jni::alias_ref jsInvokerHolder, jni::alias_ref nativeInvokerHolder ) { auto runtime = reinterpret_cast(jsRuntimePointer); jsInvoker = jsInvokerHolder->cthis()->getCallInvoker(); nativeInvoker = nativeInvokerHolder->cthis()->getCallInvoker(); runtimeHolder = std::make_shared(runtime, jsInvoker, nativeInvoker); auto expoModules = std::make_shared(this); auto expoModulesObject = jsi::Object::createFromHostObject(*runtime, expoModules); runtime ->global() .setProperty( *runtime, "ExpoModules", std::move(expoModulesObject) ); } void JSIInteropModuleRegistry::installJSIForTests() { runtimeHolder = std::make_shared(); } jni::local_ref JSIInteropModuleRegistry::callGetJavaScriptModuleObjectMethod(const std::string &moduleName) const { const static auto method = expo::JSIInteropModuleRegistry::javaClassLocal() ->getMethod( std::string)>( "getJavaScriptModuleObject" ); return method(javaPart_, moduleName); } jni::local_ref JSIInteropModuleRegistry::getModule(const std::string &moduleName) const { return callGetJavaScriptModuleObjectMethod(moduleName); } jni::local_ref JSIInteropModuleRegistry::evaluateScript( jni::JString script ) { return runtimeHolder->evaluateScript(script.toStdString()); } jni::local_ref JSIInteropModuleRegistry::global() { return runtimeHolder->global(); } } // namespace expo