1 // Copyright 2018-present 650 Industries. All rights reserved.
2 
3 #import <ExpoModulesCore/EXJavaScriptValue.h>
4 #import <ExpoModulesCore/EXJavaScriptObject.h>
5 #import <React/RCTBridgeModule.h>
6 
7 #ifdef __cplusplus
8 #import <ReactCommon/CallInvoker.h>
9 
10 namespace jsi = facebook::jsi;
11 namespace react = facebook::react;
12 #endif // __cplusplus
13 
14 @class EXJavaScriptValue;
15 @class EXJavaScriptObject;
16 
17 typedef void (^JSAsyncFunctionBlock)(NSArray * _Nonnull, RCTPromiseResolveBlock _Nonnull, RCTPromiseRejectBlock _Nonnull);
18 typedef id _Nullable (^JSSyncFunctionBlock)(NSArray * _Nonnull);
19 
20 #ifdef __cplusplus
21 typedef jsi::Value (^JSHostFunctionBlock)(jsi::Runtime &runtime, std::shared_ptr<react::CallInvoker> callInvoker, NSArray<EXJavaScriptValue *> * _Nonnull arguments);
22 #endif // __cplusplus
23 
24 NS_SWIFT_NAME(JavaScriptRuntime)
25 @interface EXJavaScriptRuntime : NSObject
26 
27 /**
28  Creates a new JavaScript runtime.
29  */
30 - (nonnull instancetype)init;
31 
32 #ifdef __cplusplus
33 - (nonnull instancetype)initWithRuntime:(nonnull jsi::Runtime *)runtime
34                             callInvoker:(std::shared_ptr<react::CallInvoker>)callInvoker;
35 
36 /**
37  Returns the underlying runtime object.
38  */
39 - (nonnull jsi::Runtime *)get;
40 
41 /**
42  Returns the call invoker the runtime was initialized with.
43  */
44 - (std::shared_ptr<react::CallInvoker>)callInvoker;
45 
46 /**
47  Wraps given host object to `EXJavaScriptObject`.
48  */
49 - (nonnull EXJavaScriptObject *)createHostObject:(std::shared_ptr<jsi::HostObject>)jsiHostObjectPtr;
50 
51 #endif // __cplusplus
52 
53 /**
54  Returns the runtime global object for use in Swift.
55  */
56 - (nonnull EXJavaScriptObject *)global;
57 
58 /**
59  Creates a new object for use in Swift.
60  */
61 - (nonnull EXJavaScriptObject *)createObject;
62 
63 /**
64  Creates a synchronous host function that runs given block when it's called.
65  The value returned by the block is synchronously returned to JS.
66  \return A JavaScript function represented as a `JavaScriptObject`.
67  */
68 - (nonnull EXJavaScriptObject *)createSyncFunction:(nonnull NSString *)name
69                                          argsCount:(NSInteger)argsCount
70                                              block:(nonnull JSSyncFunctionBlock)block;
71 
72 /**
73  Creates an asynchronous host function that runs given block when it's called.
74  The block receives a resolver that you should call when the asynchronous operation
75  succeeds and a rejecter to call whenever it fails.
76  \return A JavaScript function represented as a `JavaScriptObject`.
77  */
78 - (nonnull EXJavaScriptObject *)createAsyncFunction:(nonnull NSString *)name
79                                           argsCount:(NSInteger)argsCount
80                                               block:(nonnull JSAsyncFunctionBlock)block;
81 
82 #pragma mark - Script evaluation
83 
84 /**
85  Evaluates given JavaScript source code.
86  */
87 - (nonnull EXJavaScriptValue *)evaluateScript:(nonnull NSString *)scriptSource;
88 
89 @end
90