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 #endif // __cplusplus
46 
47 #pragma mark - Accessing object properties
48 
49 /**
50  \return a bool whether the object has a property with the given name.
51  */
52 - (BOOL)hasProperty:(nonnull NSString *)name;
53 
54 /**
55  \return the property of the object with the given name.
56  If the name isn't a property on the object, returns the `undefined` value.
57  */
58 - (nonnull EXJavaScriptValue *)getProperty:(nonnull NSString *)name;
59 
60 /**
61  \return an array consisting of all enumerable property names in the object and its prototype chain.
62  */
63 - (nonnull NSArray<NSString *> *)getPropertyNames;
64 
65 #pragma mark - Modifying object properties
66 
67 /**
68  Sets the value for the property with the given name.
69  */
70 - (void)setProperty:(nonnull NSString *)name value:(nullable id)value;
71 
72 /**
73  Defines a new property or modifies an existing property on the object using the property descriptor.
74  */
75 - (void)defineProperty:(nonnull NSString *)name descriptor:(nonnull EXJavaScriptObject *)descriptor;
76 
77 /**
78  Defines a new property or modifies an existing property on the object. Calls `Object.defineProperty` under the hood.
79  */
80 - (void)defineProperty:(nonnull NSString *)name value:(nullable id)value options:(EXJavaScriptObjectPropertyDescriptor)options;
81 
82 #pragma mark - WeakObject
83 
84 - (nonnull EXJavaScriptWeakObject *)createWeak;
85 
86 #pragma mark - Deallocator
87 
88 - (void)setObjectDeallocator:(void (^ _Nonnull)(void))deallocatorBlock;
89 
90 @end
91