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