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