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 @class EXJavaScriptValue;
18 
19 /**
20  The property descriptor options for the property being defined or modified.
21  */
22 typedef NS_OPTIONS(NSInteger, EXJavaScriptObjectPropertyDescriptor) {
23   /**
24    If set, the type of this property descriptor may be changed and if the property may be deleted from the corresponding object.
25    */
26   EXJavaScriptObjectPropertyDescriptorConfigurable = 1 << 0,
27   /**
28    If set, the property shows up during enumeration of the properties on the corresponding object.
29    */
30   EXJavaScriptObjectPropertyDescriptorEnumerable = 1 << 1,
31   /**
32    If set, the value associated with the property may be changed with an assignment operator.
33    */
34   EXJavaScriptObjectPropertyDescriptorWritable = 1 << 2,
35 } NS_SWIFT_NAME(JavaScriptObjectPropertyDescriptor);
36 
37 NS_SWIFT_NAME(JavaScriptObject)
38 @interface EXJavaScriptObject : NSObject
39 
40 // Some parts of the interface must be hidden for Swift – it can't import any C++ code.
41 #ifdef __cplusplus
42 - (nonnull instancetype)initWith:(std::shared_ptr<jsi::Object>)jsObjectPtr
43                          runtime:(nonnull EXJavaScriptRuntime *)runtime;
44 
45 /**
46  Returns the pointer to the underlying object.
47  */
48 - (nonnull jsi::Object *)get;
49 #endif // __cplusplus
50 
51 #pragma mark - Accessing object properties
52 
53 /**
54  \return a bool whether the object has a property with the given name.
55  */
56 - (BOOL)hasProperty:(nonnull NSString *)name;
57 
58 /**
59  \return the property of the object with the given name.
60  If the name isn't a property on the object, returns the `undefined` value.
61  */
62 - (nonnull EXJavaScriptValue *)getProperty:(nonnull NSString *)name;
63 
64 /**
65  \return an array consisting of all enumerable property names in the object and its prototype chain.
66  */
67 - (nonnull NSArray<NSString *> *)getPropertyNames;
68 
69 #pragma mark - Modifying object properties
70 
71 /**
72  Sets the value for the property with the given name.
73  */
74 - (void)setProperty:(nonnull NSString *)name value:(nullable id)value;
75 
76 /**
77  Defines a new property or modifies an existing property on the object. Calls `Object.defineProperty` under the hood.
78  */
79 - (void)defineProperty:(nonnull NSString *)name value:(nullable id)value options:(EXJavaScriptObjectPropertyDescriptor)options;
80 
81 #pragma mark - Functions
82 
83 /**
84  Sets given function block on the object as a host function returning a promise.
85  */
86 - (void)setAsyncFunction:(nonnull NSString *)key
87                argsCount:(NSInteger)argsCount
88                    block:(nonnull JSAsyncFunctionBlock)block;
89 
90 /**
91  Sets given synchronous function block as a host function on the object.
92  */
93 - (void)setSyncFunction:(nonnull NSString *)name
94               argsCount:(NSInteger)argsCount
95                   block:(nonnull JSSyncFunctionBlock)block;
96 
97 @end
98