1 // Copyright 2018-present 650 Industries. All rights reserved. 2 3 #import <ExpoModulesCore/EXJavaScriptObject.h> 4 5 #ifdef __cplusplus 6 #import <ReactCommon/CallInvoker.h> 7 8 namespace jsi = facebook::jsi; 9 namespace react = facebook::react; 10 #endif // __cplusplus 11 12 NS_SWIFT_NAME(JavaScriptRuntime) 13 @interface EXJavaScriptRuntime : NSObject 14 15 /** 16 Creates a new JavaScript runtime. 17 */ 18 - (nonnull instancetype)init; 19 20 #ifdef __cplusplus 21 typedef jsi::Value (^JSHostFunctionBlock)(jsi::Runtime &runtime, std::shared_ptr<react::CallInvoker> callInvoker, NSArray * _Nonnull arguments); 22 23 - (nonnull instancetype)initWithRuntime:(jsi::Runtime *)runtime 24 callInvoker:(std::shared_ptr<react::CallInvoker>)callInvoker; 25 26 /** 27 Returns the underlying runtime object. 28 */ 29 - (nonnull jsi::Runtime *)get; 30 31 /** 32 Returns the call invoker the runtime was initialized with. 33 */ 34 - (std::shared_ptr<react::CallInvoker>)callInvoker; 35 36 /** 37 Wraps given host object to `EXJavaScriptObject`. 38 */ 39 - (nonnull EXJavaScriptObject *)createHostObject:(std::shared_ptr<jsi::HostObject>)jsiHostObjectPtr; 40 41 - (jsi::Function)createSyncFunction:(nonnull NSString *)name 42 argsCount:(NSInteger)argsCount 43 block:(nonnull JSSyncFunctionBlock)block; 44 45 - (jsi::Function)createAsyncFunction:(nonnull NSString *)name 46 argsCount:(NSInteger)argsCount 47 block:(nonnull JSAsyncFunctionBlock)block; 48 #endif // __cplusplus 49 50 /** 51 Returns the runtime global object for use in Swift. 52 */ 53 - (nonnull EXJavaScriptObject *)global; 54 55 /** 56 Creates a new object for use in Swift. 57 */ 58 - (nonnull EXJavaScriptObject *)createObject; 59 60 #pragma mark - Script evaluation 61 62 /** 63 Evaluates given JavaScript source code. 64 */ 65 - (nullable id)evaluateScript:(nonnull NSString *)scriptSource; 66 67 @end 68