1 // Copyright 2018-present 650 Industries. All rights reserved. 2 3 #import <Foundation/Foundation.h> 4 #import <ExpoModulesCore/EXJavaScriptValue.h> 5 #import <ExpoModulesCore/EXJavaScriptObject.h> 6 #import <React/RCTBridgeModule.h> 7 8 #ifdef __cplusplus 9 #import <ReactCommon/CallInvoker.h> 10 11 namespace jsi = facebook::jsi; 12 namespace react = facebook::react; 13 #endif // __cplusplus 14 15 @class EXJavaScriptValue; 16 @class EXJavaScriptObject; 17 18 typedef void (^JSAsyncFunctionBlock)(EXJavaScriptValue * _Nonnull thisValue, 19 NSArray<EXJavaScriptValue *> * _Nonnull arguments, 20 RCTPromiseResolveBlock _Nonnull resolve, 21 RCTPromiseRejectBlock _Nonnull reject); 22 23 typedef id _Nullable (^JSSyncFunctionBlock)(EXJavaScriptValue * _Nonnull thisValue, 24 NSArray<EXJavaScriptValue *> * _Nonnull arguments, 25 NSError * _Nullable __autoreleasing * _Nullable error); 26 27 #ifdef __cplusplus 28 typedef jsi::Value (^JSHostFunctionBlock)(jsi::Runtime &runtime, 29 std::shared_ptr<react::CallInvoker> callInvoker, 30 EXJavaScriptValue * _Nonnull thisValue, 31 NSArray<EXJavaScriptValue *> * _Nonnull arguments); 32 #endif // __cplusplus 33 34 NS_SWIFT_NAME(JavaScriptRuntime) 35 @interface EXJavaScriptRuntime : NSObject 36 37 /** 38 Creates a new JavaScript runtime. 39 */ 40 - (nonnull instancetype)init; 41 42 #ifdef __cplusplus 43 44 - (nonnull instancetype)initWithRuntime:(nonnull jsi::Runtime *)runtime 45 callInvoker:(std::shared_ptr<react::CallInvoker>)callInvoker; 46 47 /** 48 Returns the underlying runtime object. 49 */ 50 - (nonnull jsi::Runtime *)get; 51 52 /** 53 Returns the call invoker the runtime was initialized with. 54 */ 55 - (std::shared_ptr<react::CallInvoker>)callInvoker; 56 57 /** 58 Wraps given host object to `EXJavaScriptObject`. 59 */ 60 - (nonnull EXJavaScriptObject *)createHostObject:(std::shared_ptr<jsi::HostObject>)jsiHostObjectPtr; 61 62 #endif // __cplusplus 63 64 /** 65 Returns the runtime global object for use in Swift. 66 */ 67 - (nonnull EXJavaScriptObject *)global; 68 69 /** 70 The main object of the Expo runtime that is used to scope native Expo-specific functionalities. 71 It gets installed into the runtime as the `global.expo` object. 72 */ 73 - (nonnull EXJavaScriptObject *)mainObject; 74 75 /** 76 Creates a new object for use in Swift. 77 */ 78 - (nonnull EXJavaScriptObject *)createObject; 79 80 /** 81 Creates a synchronous host function that runs given block when it's called. 82 The value returned by the block is synchronously returned to JS. 83 \return A JavaScript function represented as a `JavaScriptObject`. 84 */ 85 - (nonnull EXJavaScriptObject *)createSyncFunction:(nonnull NSString *)name 86 argsCount:(NSInteger)argsCount 87 block:(nonnull JSSyncFunctionBlock)block NS_REFINED_FOR_SWIFT; 88 89 /** 90 Creates an asynchronous host function that runs given block when it's called. 91 The block receives a resolver that you should call when the asynchronous operation 92 succeeds and a rejecter to call whenever it fails. 93 \return A JavaScript function represented as a `JavaScriptObject`. 94 */ 95 - (nonnull EXJavaScriptObject *)createAsyncFunction:(nonnull NSString *)name 96 argsCount:(NSInteger)argsCount 97 block:(nonnull JSAsyncFunctionBlock)block; 98 99 #pragma mark - Classes 100 101 typedef void (^ClassConstructorBlock)(EXJavaScriptObject * _Nonnull thisValue, NSArray<EXJavaScriptValue *> * _Nonnull arguments); 102 103 - (nonnull EXJavaScriptObject *)createClass:(nonnull NSString *)name 104 constructor:(nonnull ClassConstructorBlock)constructor; 105 106 /** 107 Creates a new object, using the provided object as the prototype. 108 */ 109 - (nullable EXJavaScriptObject *)createObjectWithPrototype:(nonnull EXJavaScriptObject *)prototype; 110 111 #pragma mark - Script evaluation 112 113 /** 114 Evaluates given JavaScript source code. 115 */ 116 - (nonnull EXJavaScriptValue *)evaluateScript:(nonnull NSString *)scriptSource NS_REFINED_FOR_SWIFT; 117 118 @end 119