1 // Copyright 2022-present 650 Industries. All rights reserved.
2 
3 #import <Foundation/Foundation.h>
4 #import <React/RCTBridgeModule.h>
5 
6 #ifdef __cplusplus
7 #import <jsi/jsi.h>
8 #import <ReactCommon/CallInvoker.h>
9 
10 namespace jsi = facebook::jsi;
11 #endif // __cplusplus
12 
13 typedef void (^JSAsyncFunctionBlock)(NSArray * _Nonnull, RCTPromiseResolveBlock _Nonnull, RCTPromiseRejectBlock _Nonnull);
14 typedef id _Nullable (^JSSyncFunctionBlock)(NSArray * _Nonnull);
15 
16 @class EXJavaScriptRuntime;
17 
18 NS_SWIFT_NAME(JavaScriptObject)
19 @interface EXJavaScriptObject : NSObject
20 
21 // Some parts of the interface must be hidden for Swift – it can't import any C++ code.
22 #ifdef __cplusplus
23 - (nonnull instancetype)initWith:(std::shared_ptr<jsi::Object>)jsObjectPtr
24                          runtime:(nonnull EXJavaScriptRuntime *)runtime;
25 
26 /**
27  Returns the pointer to the underlying object.
28  */
29 - (nonnull jsi::Object *)get;
30 #endif // __cplusplus
31 
32 #pragma mark - Subscripting
33 
34 /**
35  Subscript getter. Supports only values convertible to Foundation types, otherwise `nil` is returned.
36  */
37 - (nullable id)objectForKeyedSubscript:(nonnull NSString *)key;
38 
39 /**
40  Subscript setter. Only `EXJavaScriptObject` and Foundation object convertible to JSI values can be used as a value,
41  otherwise the property is set to `undefined`.
42  */
43 - (void)setObject:(nullable id)obj forKeyedSubscript:(nonnull NSString *)key;
44 
45 #pragma mark - Functions
46 
47 /**
48  Sets given function block on the object as a host function returning a promise.
49  */
50 - (void)setAsyncFunction:(nonnull NSString *)key
51                argsCount:(NSInteger)argsCount
52                    block:(nonnull JSAsyncFunctionBlock)block;
53 
54 /**
55  Sets given synchronous function block as a host function on the object.
56  */
57 - (void)setSyncFunction:(nonnull NSString *)name
58               argsCount:(NSInteger)argsCount
59                   block:(nonnull JSSyncFunctionBlock)block;
60 
61 @end
62