expo-modules-core 0.9.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (167) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/android/CMakeLists.txt +154 -0
  3. package/android/build.gradle +293 -5
  4. package/android/src/main/cpp/Exceptions.cpp +22 -0
  5. package/android/src/main/cpp/Exceptions.h +38 -0
  6. package/android/src/main/cpp/ExpoModulesHostObject.cpp +47 -0
  7. package/android/src/main/cpp/ExpoModulesHostObject.h +32 -0
  8. package/android/src/main/cpp/JNIFunctionBody.cpp +29 -0
  9. package/android/src/main/cpp/JNIFunctionBody.h +50 -0
  10. package/android/src/main/cpp/JNIInjector.cpp +19 -0
  11. package/android/src/main/cpp/JSIInteropModuleRegistry.cpp +122 -0
  12. package/android/src/main/cpp/JSIInteropModuleRegistry.h +96 -0
  13. package/android/src/main/cpp/JSIObjectWrapper.h +33 -0
  14. package/android/src/main/cpp/JSITypeConverter.h +84 -0
  15. package/android/src/main/cpp/JavaScriptModuleObject.cpp +138 -0
  16. package/android/src/main/cpp/JavaScriptModuleObject.h +122 -0
  17. package/android/src/main/cpp/JavaScriptObject.cpp +125 -0
  18. package/android/src/main/cpp/JavaScriptObject.h +131 -0
  19. package/android/src/main/cpp/JavaScriptRuntime.cpp +127 -0
  20. package/android/src/main/cpp/JavaScriptRuntime.h +87 -0
  21. package/android/src/main/cpp/JavaScriptValue.cpp +172 -0
  22. package/android/src/main/cpp/JavaScriptValue.h +78 -0
  23. package/android/src/main/cpp/MethodMetadata.cpp +230 -0
  24. package/android/src/main/cpp/MethodMetadata.h +92 -0
  25. package/android/src/main/java/expo/modules/adapters/react/NativeModulesProxy.java +2 -0
  26. package/android/src/main/java/expo/modules/core/errors/ContextDestroyedException.kt +7 -0
  27. package/android/src/main/java/expo/modules/interfaces/permissions/Permissions.java +30 -0
  28. package/android/src/main/java/expo/modules/kotlin/AppContext.kt +49 -1
  29. package/android/src/main/java/expo/modules/kotlin/ConcatIterator.kt +18 -0
  30. package/android/src/main/java/expo/modules/kotlin/KotlinInteropModuleRegistry.kt +15 -12
  31. package/android/src/main/java/expo/modules/kotlin/ModuleHolder.kt +39 -3
  32. package/android/src/main/java/expo/modules/kotlin/defaultmodules/ErrorManagerModule.kt +2 -2
  33. package/android/src/main/java/expo/modules/kotlin/exception/CodedException.kt +13 -0
  34. package/android/src/main/java/expo/modules/kotlin/exception/ExceptionDecorator.kt +2 -0
  35. package/android/src/main/java/expo/modules/kotlin/functions/AnyFunction.kt +19 -14
  36. package/android/src/main/java/expo/modules/kotlin/functions/AsyncFunction.kt +29 -7
  37. package/android/src/main/java/expo/modules/kotlin/functions/AsyncFunctionBuilder.kt +13 -13
  38. package/android/src/main/java/expo/modules/kotlin/functions/AsyncFunctionComponent.kt +18 -0
  39. package/android/src/main/java/expo/modules/kotlin/functions/AsyncFunctionWithPromiseComponent.kt +18 -0
  40. package/android/src/main/java/expo/modules/kotlin/functions/SuspendFunctionComponent.kt +56 -0
  41. package/android/src/main/java/expo/modules/kotlin/functions/SyncFunctionComponent.kt +28 -0
  42. package/android/src/main/java/expo/modules/kotlin/jni/CppType.kt +18 -0
  43. package/android/src/main/java/expo/modules/kotlin/jni/JNIFunctionBody.kt +39 -0
  44. package/android/src/main/java/expo/modules/kotlin/jni/JSIInteropModuleRegistry.kt +89 -0
  45. package/android/src/main/java/expo/modules/kotlin/jni/JavaScriptModuleObject.kt +44 -0
  46. package/android/src/main/java/expo/modules/kotlin/jni/JavaScriptObject.kt +113 -0
  47. package/android/src/main/java/expo/modules/kotlin/jni/JavaScriptValue.kt +35 -0
  48. package/android/src/main/java/expo/modules/kotlin/modules/Module.kt +15 -5
  49. package/android/src/main/java/expo/modules/kotlin/modules/ModuleDefinitionBuilder.kt +65 -111
  50. package/android/src/main/java/expo/modules/kotlin/modules/ModuleDefinitionData.kt +35 -2
  51. package/android/src/main/java/expo/modules/kotlin/providers/AppContextProvider.kt +14 -0
  52. package/android/src/main/java/expo/modules/kotlin/providers/CurrentActivityProvider.kt +22 -0
  53. package/android/src/main/java/expo/modules/kotlin/records/RecordTypeConverter.kt +19 -2
  54. package/android/src/main/java/expo/modules/kotlin/types/AnyType.kt +3 -2
  55. package/android/src/main/java/expo/modules/kotlin/types/ArrayTypeConverter.kt +7 -2
  56. package/android/src/main/java/expo/modules/kotlin/types/BasicTypeConverters.kt +68 -20
  57. package/android/src/main/java/expo/modules/kotlin/types/EnumTypeConverter.kt +50 -22
  58. package/android/src/main/java/expo/modules/kotlin/types/ListTypeConverter.kt +18 -2
  59. package/android/src/main/java/expo/modules/kotlin/types/MapTypeConverter.kt +18 -2
  60. package/android/src/main/java/expo/modules/kotlin/types/PairTypeConverter.kt +17 -2
  61. package/android/src/main/java/expo/modules/kotlin/types/TypeConverter.kt +43 -3
  62. package/android/src/main/java/expo/modules/kotlin/types/TypeConverterProvider.kt +5 -0
  63. package/build/NativeModulesProxy.native.d.ts.map +1 -1
  64. package/build/NativeModulesProxy.native.js +9 -3
  65. package/build/NativeModulesProxy.native.js.map +1 -1
  66. package/ios/AppDelegates/EXAppDelegatesLoader.m +1 -2
  67. package/ios/ExpoModulesCore.podspec +1 -1
  68. package/ios/JSI/EXJSIConversions.mm +6 -0
  69. package/ios/JSI/EXJSIInstaller.h +15 -21
  70. package/ios/JSI/EXJSIInstaller.mm +39 -3
  71. package/ios/JSI/EXJSIUtils.h +47 -3
  72. package/ios/JSI/EXJSIUtils.mm +88 -4
  73. package/ios/JSI/EXJavaScriptObject.h +11 -18
  74. package/ios/JSI/EXJavaScriptObject.mm +37 -18
  75. package/ios/JSI/EXJavaScriptRuntime.h +43 -9
  76. package/ios/JSI/EXJavaScriptRuntime.mm +70 -27
  77. package/ios/JSI/EXJavaScriptTypedArray.h +30 -0
  78. package/ios/JSI/EXJavaScriptTypedArray.mm +29 -0
  79. package/ios/JSI/EXJavaScriptValue.h +3 -2
  80. package/ios/JSI/EXJavaScriptValue.mm +17 -20
  81. package/ios/JSI/EXJavaScriptWeakObject.h +23 -0
  82. package/ios/JSI/EXJavaScriptWeakObject.mm +53 -0
  83. package/ios/JSI/EXObjectDeallocator.h +27 -0
  84. package/ios/JSI/ExpoModulesHostObject.h +3 -3
  85. package/ios/JSI/ExpoModulesHostObject.mm +4 -4
  86. package/ios/JSI/JavaScriptRuntime.swift +38 -1
  87. package/ios/JSI/JavaScriptValue.swift +7 -0
  88. package/ios/JSI/TypedArray.cpp +67 -0
  89. package/ios/JSI/TypedArray.h +46 -0
  90. package/ios/ModuleRegistryAdapter/EXModuleRegistryAdapter.m +0 -11
  91. package/ios/NativeModulesProxy/EXNativeModulesProxy.h +17 -10
  92. package/ios/NativeModulesProxy/EXNativeModulesProxy.mm +88 -77
  93. package/ios/NativeModulesProxy/NativeModulesProxyModule.swift +17 -0
  94. package/ios/Services/EXReactNativeEventEmitter.h +2 -2
  95. package/ios/Services/EXReactNativeEventEmitter.m +11 -6
  96. package/ios/Swift/AppContext.swift +208 -28
  97. package/ios/Swift/Arguments/AnyArgument.swift +18 -0
  98. package/ios/Swift/Arguments/{Types/EnumArgumentType.swift → EnumArgument.swift} +2 -17
  99. package/ios/Swift/Classes/ClassComponent.swift +95 -0
  100. package/ios/Swift/Classes/ClassComponentElement.swift +33 -0
  101. package/ios/Swift/Classes/ClassComponentElementsBuilder.swift +34 -0
  102. package/ios/Swift/Classes/ClassComponentFactories.swift +96 -0
  103. package/ios/Swift/DynamicTypes/AnyDynamicType.swift +44 -0
  104. package/ios/Swift/DynamicTypes/DynamicArrayType.swift +56 -0
  105. package/ios/Swift/DynamicTypes/DynamicConvertibleType.swift +27 -0
  106. package/ios/Swift/DynamicTypes/DynamicEnumType.swift +27 -0
  107. package/ios/Swift/DynamicTypes/DynamicOptionalType.swift +63 -0
  108. package/ios/Swift/DynamicTypes/DynamicRawType.swift +33 -0
  109. package/ios/Swift/DynamicTypes/DynamicSharedObjectType.swift +37 -0
  110. package/ios/Swift/DynamicTypes/DynamicType.swift +39 -0
  111. package/ios/Swift/DynamicTypes/DynamicTypedArrayType.swift +46 -0
  112. package/ios/Swift/Exceptions/CodedError.swift +1 -1
  113. package/ios/Swift/Exceptions/Exception.swift +8 -6
  114. package/ios/Swift/Exceptions/UnexpectedException.swift +2 -1
  115. package/ios/Swift/ExpoBridgeModule.m +5 -0
  116. package/ios/Swift/ExpoBridgeModule.swift +65 -0
  117. package/ios/Swift/Functions/AnyFunction.swift +33 -31
  118. package/ios/Swift/Functions/AsyncFunctionComponent.swift +196 -59
  119. package/ios/Swift/Functions/SyncFunctionComponent.swift +142 -58
  120. package/ios/Swift/JavaScriptUtils.swift +32 -57
  121. package/ios/Swift/Logging/LogHandlers.swift +39 -0
  122. package/ios/Swift/Logging/LogType.swift +62 -0
  123. package/ios/Swift/Logging/Logger.swift +198 -0
  124. package/ios/Swift/ModuleHolder.swift +19 -54
  125. package/ios/Swift/ModuleRegistry.swift +7 -1
  126. package/ios/Swift/Modules/AnyModule.swift +3 -3
  127. package/ios/Swift/ModulesProvider.swift +2 -0
  128. package/ios/Swift/Objects/JavaScriptObjectBuilder.swift +37 -0
  129. package/ios/Swift/Objects/ObjectDefinition.swift +74 -1
  130. package/ios/Swift/Objects/ObjectDefinitionComponents.swift +77 -68
  131. package/ios/Swift/Objects/PropertyComponent.swift +147 -0
  132. package/ios/Swift/Promise.swift +12 -3
  133. package/ios/Swift/Records/Field.swift +2 -2
  134. package/ios/Swift/SharedObjects/SharedObject.swift +20 -0
  135. package/ios/Swift/SharedObjects/SharedObjectRegistry.swift +129 -0
  136. package/ios/Swift/TypedArrays/AnyTypedArray.swift +11 -0
  137. package/ios/Swift/TypedArrays/ConcreteTypedArrays.swift +56 -0
  138. package/ios/Swift/TypedArrays/GenericTypedArray.swift +49 -0
  139. package/ios/Swift/TypedArrays/TypedArray.swift +80 -0
  140. package/ios/Swift/Utilities.swift +28 -0
  141. package/ios/Swift/Views/ConcreteViewProp.swift +3 -3
  142. package/ios/Swift/Views/ViewManagerDefinitionComponents.swift +2 -2
  143. package/ios/Tests/ClassComponentSpec.swift +210 -0
  144. package/ios/Tests/DynamicTypeSpec.swift +336 -0
  145. package/ios/Tests/EnumArgumentSpec.swift +48 -0
  146. package/ios/Tests/ExpoModulesSpec.swift +17 -3
  147. package/ios/Tests/FunctionSpec.swift +167 -118
  148. package/ios/Tests/Mocks/ModuleMocks.swift +1 -1
  149. package/ios/Tests/PropertyComponentSpec.swift +95 -0
  150. package/ios/Tests/SharedObjectRegistrySpec.swift +109 -0
  151. package/ios/Tests/TypedArraysSpec.swift +136 -0
  152. package/package.json +2 -2
  153. package/src/NativeModulesProxy.native.ts +13 -3
  154. package/src/ts-declarations/ExpoModules.d.ts +7 -0
  155. package/tsconfig.json +1 -1
  156. package/android/src/main/java/expo/modules/kotlin/functions/AsyncFunctionWithPromise.kt +0 -15
  157. package/android/src/main/java/expo/modules/kotlin/functions/AsyncSuspendFunction.kt +0 -36
  158. package/ios/Swift/Arguments/AnyArgumentType.swift +0 -13
  159. package/ios/Swift/Arguments/ArgumentType.swift +0 -28
  160. package/ios/Swift/Arguments/Types/ArrayArgumentType.swift +0 -42
  161. package/ios/Swift/Arguments/Types/ConvertibleArgumentType.swift +0 -16
  162. package/ios/Swift/Arguments/Types/OptionalArgumentType.swift +0 -49
  163. package/ios/Swift/Arguments/Types/PromiseArgumentType.swift +0 -15
  164. package/ios/Swift/Arguments/Types/RawArgumentType.swift +0 -25
  165. package/ios/Swift/Functions/ConcreteFunction.swift +0 -103
  166. package/ios/Swift/SwiftInteropBridge.swift +0 -155
  167. package/ios/Tests/ArgumentTypeSpec.swift +0 -143
@@ -0,0 +1,30 @@
1
+ // Copyright 2022-present 650 Industries. All rights reserved.
2
+
3
+ #import <Foundation/Foundation.h>
4
+ #import <ExpoModulesCore/EXJavaScriptObject.h>
5
+ #import <ExpoModulesCore/EXJavaScriptRuntime.h>
6
+
7
+ // We need to redefine the C++ enum (see TypedArray.h) in an Objective-C way to expose it to Swift.
8
+ // Please keep them in-sync!
9
+ typedef NS_ENUM(NSInteger, EXTypedArrayKind) {
10
+ Int8Array = 1,
11
+ Int16Array = 2,
12
+ Int32Array = 3,
13
+ Uint8Array = 4,
14
+ Uint8ClampedArray = 5,
15
+ Uint16Array = 6,
16
+ Uint32Array = 7,
17
+ Float32Array = 8,
18
+ Float64Array = 9,
19
+ BigInt64Array = 10,
20
+ BigUint64Array = 11,
21
+ } NS_SWIFT_NAME(TypedArrayKind);
22
+
23
+ NS_SWIFT_NAME(JavaScriptTypedArray)
24
+ @interface EXJavaScriptTypedArray : EXJavaScriptObject
25
+
26
+ @property (nonatomic) EXTypedArrayKind kind;
27
+
28
+ - (nonnull void *)getUnsafeMutableRawPointer;
29
+
30
+ @end
@@ -0,0 +1,29 @@
1
+ // Copyright 2022-present 650 Industries. All rights reserved.
2
+
3
+ #import <ExpoModulesCore/EXJavaScriptTypedArray.h>
4
+ #import <ExpoModulesCore/TypedArray.h>
5
+
6
+ @implementation EXJavaScriptTypedArray {
7
+ __weak EXJavaScriptRuntime *_runtime;
8
+
9
+ std::shared_ptr<expo::TypedArray> _typedArrayPtr;
10
+ }
11
+
12
+ - (nonnull instancetype)initWith:(std::shared_ptr<jsi::Object>)jsObjectPtr
13
+ runtime:(EXJavaScriptRuntime *)runtime
14
+ {
15
+ if (self = [super initWith:jsObjectPtr runtime:runtime]) {
16
+ jsi::Runtime *rt = [runtime get];
17
+ _runtime = runtime;
18
+ _typedArrayPtr = std::make_shared<expo::TypedArray>(*rt, *jsObjectPtr.get());
19
+ _kind = (EXTypedArrayKind)_typedArrayPtr->getKind(*rt);
20
+ }
21
+ return self;
22
+ }
23
+
24
+ - (nonnull void *)getUnsafeMutableRawPointer
25
+ {
26
+ return _typedArrayPtr->getRawPointer(*[_runtime get]);
27
+ }
28
+
29
+ @end
@@ -9,6 +9,7 @@ namespace jsi = facebook::jsi;
9
9
  #endif // __cplusplus
10
10
 
11
11
  @class EXJavaScriptRuntime;
12
+ @class EXJavaScriptTypedArray;
12
13
 
13
14
  /**
14
15
  Represents any JavaScript value. Its purpose is to exposes `facebook::jsi::Value` API back to Swift.
@@ -36,8 +37,7 @@ NS_SWIFT_NAME(JavaScriptValue)
36
37
  - (BOOL)isSymbol;
37
38
  - (BOOL)isObject;
38
39
  - (BOOL)isFunction;
39
-
40
- + (nonnull NSString *)kindOf:(nonnull EXJavaScriptValue *)value;
40
+ - (BOOL)isTypedArray;
41
41
 
42
42
  #pragma mark - Type casting
43
43
 
@@ -49,6 +49,7 @@ NS_SWIFT_NAME(JavaScriptValue)
49
49
  - (nonnull NSArray<EXJavaScriptValue *> *)getArray;
50
50
  - (nonnull NSDictionary<NSString *, id> *)getDictionary;
51
51
  - (nonnull EXJavaScriptObject *)getObject;
52
+ - (nullable EXJavaScriptTypedArray *)getTypedArray;
52
53
 
53
54
  #pragma mark - Helpers
54
55
 
@@ -3,6 +3,8 @@
3
3
  #import <ExpoModulesCore/EXJSIConversions.h>
4
4
  #import <ExpoModulesCore/EXJavaScriptValue.h>
5
5
  #import <ExpoModulesCore/EXJavaScriptRuntime.h>
6
+ #import <ExpoModulesCore/EXJavaScriptTypedArray.h>
7
+ #import <ExpoModulesCore/TypedArray.h>
6
8
 
7
9
  @implementation EXJavaScriptValue {
8
10
  __weak EXJavaScriptRuntime *_runtime;
@@ -70,28 +72,13 @@
70
72
  return false;
71
73
  }
72
74
 
73
- + (nonnull NSString *)kindOf:(nonnull EXJavaScriptValue *)value
75
+ - (BOOL)isTypedArray
74
76
  {
75
- if ([value isUndefined]) {
76
- return @"undefined";
77
- }
78
- if ([value isNull]) {
79
- return @"null";
80
- }
81
- if ([value isBool]) {
82
- return @"boolean";
83
- }
84
- if ([value isNumber]) {
85
- return @"number";
86
- }
87
- if ([value isString]) {
88
- return @"string";
89
- }
90
- if ([value isFunction]) {
91
- return @"function";
77
+ if (_value->isObject()) {
78
+ jsi::Runtime *runtime = [_runtime get];
79
+ return expo::isTypedArray(*runtime, _value->getObject(*runtime));
92
80
  }
93
- assert([value isObject] && "Expecting object.");
94
- return @"object";
81
+ return false;
95
82
  }
96
83
 
97
84
  #pragma mark - Type casting
@@ -155,6 +142,16 @@
155
142
  return [[EXJavaScriptObject alloc] initWith:objectPtr runtime:_runtime];
156
143
  }
157
144
 
145
+ - (nullable EXJavaScriptTypedArray *)getTypedArray
146
+ {
147
+ if (![self isTypedArray]) {
148
+ return nil;
149
+ }
150
+ jsi::Runtime *runtime = [_runtime get];
151
+ std::shared_ptr<jsi::Object> objectPtr = std::make_shared<jsi::Object>(_value->asObject(*runtime));
152
+ return [[EXJavaScriptTypedArray alloc] initWith:objectPtr runtime:_runtime];
153
+ }
154
+
158
155
  #pragma mark - Helpers
159
156
 
160
157
  - (nonnull NSString *)toString
@@ -0,0 +1,23 @@
1
+ // Copyright 2022-present 650 Industries. All rights reserved.
2
+
3
+ #import <Foundation/Foundation.h>
4
+ #import <ExpoModulesCore/EXJavaScriptValue.h>
5
+ #import <ExpoModulesCore/EXJavaScriptRuntime.h>
6
+
7
+ #ifdef __cplusplus
8
+ #import <jsi/jsi.h>
9
+
10
+ namespace jsi = facebook::jsi;
11
+ #endif // __cplusplus
12
+
13
+ NS_SWIFT_NAME(JavaScriptWeakObject)
14
+ @interface EXJavaScriptWeakObject : NSObject
15
+
16
+ #ifdef __cplusplus
17
+ - (nonnull instancetype)initWith:(std::shared_ptr<jsi::Object>)jsObject
18
+ runtime:(nonnull EXJavaScriptRuntime *)runtime;
19
+ #endif // __cplusplus
20
+
21
+ - (nullable EXJavaScriptObject *)lock;
22
+
23
+ @end
@@ -0,0 +1,53 @@
1
+ // Copyright 2022-present 650 Industries. All rights reserved.
2
+
3
+ #import <ExpoModulesCore/EXJSIUtils.h>
4
+ #import <ExpoModulesCore/EXJavaScriptWeakObject.h>
5
+
6
+ @implementation EXJavaScriptWeakObject {
7
+ /**
8
+ Pointer to the `EXJavaScriptRuntime` wrapper.
9
+
10
+ \note It must be weak because only then the original runtime can be safely deallocated
11
+ when the JS engine wants to without unsetting it on each created object.
12
+ */
13
+ __weak EXJavaScriptRuntime *_runtime;
14
+
15
+ /**
16
+ Shared pointer to the `WeakRef` JS object.
17
+ */
18
+ std::shared_ptr<jsi::Object> _jsObject;
19
+ }
20
+
21
+ - (nonnull instancetype)initWith:(std::shared_ptr<jsi::Object>)jsObject
22
+ runtime:(nonnull EXJavaScriptRuntime *)runtime
23
+ {
24
+ if (self = [super init]) {
25
+ _runtime = runtime;
26
+
27
+ // Check whether the runtime supports `WeakRef` objects. If it does not,
28
+ // we consciously hold a strong reference to the object and cause memory leaks.
29
+ // This is the case on hermes and JSC prior to iOS 14.5.
30
+ // TODO: (@tsapeta) Use `jsi::WeakObject` on hermes
31
+ if (expo::isWeakRefSupported(*[runtime get])) {
32
+ _jsObject = expo::createWeakRef(*[runtime get], jsObject);
33
+ } else {
34
+ _jsObject = jsObject;
35
+ }
36
+ }
37
+ return self;
38
+ }
39
+
40
+ - (nullable EXJavaScriptObject *)lock
41
+ {
42
+ jsi::Runtime *runtime = [_runtime get];
43
+ std::shared_ptr<jsi::Object> objectPtr = expo::isWeakRefSupported(*runtime)
44
+ ? expo::derefWeakRef(*runtime, _jsObject)
45
+ : _jsObject;
46
+
47
+ if (!objectPtr) {
48
+ return nil;
49
+ }
50
+ return [[EXJavaScriptObject alloc] initWith:objectPtr runtime:_runtime];
51
+ }
52
+
53
+ @end
@@ -0,0 +1,27 @@
1
+ // Copyright 2022-present 650 Industries. All rights reserved.
2
+
3
+ #ifdef __cplusplus
4
+
5
+ #import <jsi/jsi.h>
6
+
7
+ namespace jsi = facebook::jsi;
8
+
9
+ namespace expo {
10
+
11
+ typedef void (^ObjectDeallocatorBlock)();
12
+
13
+ class JSI_EXPORT ObjectDeallocator : public jsi::HostObject {
14
+ public:
15
+ ObjectDeallocator(const ObjectDeallocatorBlock deallocator) : deallocator(deallocator) {};
16
+
17
+ virtual ~ObjectDeallocator() {
18
+ deallocator();
19
+ }
20
+
21
+ const ObjectDeallocatorBlock deallocator;
22
+
23
+ }; // class ObjectDeallocator
24
+
25
+ } // namespace expo
26
+
27
+ #endif
@@ -7,13 +7,13 @@
7
7
 
8
8
  namespace jsi = facebook::jsi;
9
9
 
10
- @class SwiftInteropBridge;
10
+ @class EXAppContext;
11
11
 
12
12
  namespace expo {
13
13
 
14
14
  class JSI_EXPORT ExpoModulesHostObject : public jsi::HostObject {
15
15
  public:
16
- ExpoModulesHostObject(SwiftInteropBridge *interopBridge);
16
+ ExpoModulesHostObject(EXAppContext *appContext);
17
17
 
18
18
  virtual ~ExpoModulesHostObject();
19
19
 
@@ -24,7 +24,7 @@ public:
24
24
  std::vector<jsi::PropNameID> getPropertyNames(jsi::Runtime &rt) override;
25
25
 
26
26
  private:
27
- SwiftInteropBridge *swiftInterop;
27
+ EXAppContext *appContext;
28
28
 
29
29
  }; // class ExpoModulesHostObject
30
30
 
@@ -6,15 +6,15 @@
6
6
 
7
7
  namespace expo {
8
8
 
9
- ExpoModulesHostObject::ExpoModulesHostObject(SwiftInteropBridge *swiftInterop) : swiftInterop(swiftInterop) {}
9
+ ExpoModulesHostObject::ExpoModulesHostObject(EXAppContext *appContext) : appContext(appContext) {}
10
10
 
11
11
  ExpoModulesHostObject::~ExpoModulesHostObject() {
12
- [swiftInterop setRuntime:nil];
12
+ [appContext setRuntime:nil];
13
13
  }
14
14
 
15
15
  jsi::Value ExpoModulesHostObject::get(jsi::Runtime &runtime, const jsi::PropNameID &name) {
16
16
  NSString *moduleName = [NSString stringWithUTF8String:name.utf8(runtime).c_str()];
17
- EXJavaScriptObject *nativeObject = [swiftInterop getNativeModuleObject:moduleName];
17
+ EXJavaScriptObject *nativeObject = [appContext getNativeModuleObject:moduleName];
18
18
 
19
19
  return nativeObject ? jsi::Value(runtime, *[nativeObject get]) : jsi::Value::undefined();
20
20
  }
@@ -27,7 +27,7 @@ void ExpoModulesHostObject::set(jsi::Runtime &runtime, const jsi::PropNameID &na
27
27
  }
28
28
 
29
29
  std::vector<jsi::PropNameID> ExpoModulesHostObject::getPropertyNames(jsi::Runtime &runtime) {
30
- NSArray<NSString *> *moduleNames = [swiftInterop getModuleNames];
30
+ NSArray<NSString *> *moduleNames = [appContext getModuleNames];
31
31
  std::vector<jsi::PropNameID> propertyNames;
32
32
 
33
33
  propertyNames.reserve([moduleNames count]);
@@ -1,6 +1,11 @@
1
1
  // Copyright 2022-present 650 Industries. All rights reserved.
2
2
 
3
3
  public extension JavaScriptRuntime {
4
+ /**
5
+ A type of the closure that you pass to the `createSyncFunction` function.
6
+ */
7
+ typealias SyncFunctionClosure = (_ this: JavaScriptValue, _ arguments: [JavaScriptValue]) throws -> Any
8
+
4
9
  /**
5
10
  Evaluates JavaScript code represented as a string.
6
11
 
@@ -11,11 +16,12 @@ public extension JavaScriptRuntime {
11
16
  - Throws: `JavaScriptEvalException` when evaluated code has invalid syntax or throws an error.
12
17
  - Note: It wraps the original `evaluateScript` to better handle and rethrow exceptions.
13
18
  */
19
+ @discardableResult
14
20
  func eval(_ source: String) throws -> JavaScriptValue {
15
21
  do {
16
22
  var result: JavaScriptValue?
17
23
  try EXUtilities.catchException {
18
- result = self.evaluateScript(source)
24
+ result = self.__evaluateScript(source)
19
25
  }
20
26
  // There is no risk to force unwrapping as long as the `evaluateScript` returns nonnull value.
21
27
  return result!
@@ -23,6 +29,37 @@ public extension JavaScriptRuntime {
23
29
  throw JavaScriptEvalException(error as NSError)
24
30
  }
25
31
  }
32
+
33
+ /**
34
+ Evaluates the JavaScript code made by joining an array of strings with a newline separator.
35
+ See the other ``eval(_:)`` for more details.
36
+ */
37
+ @discardableResult
38
+ func eval(_ source: [String]) throws -> JavaScriptValue {
39
+ try eval(source.joined(separator: "\n"))
40
+ }
41
+
42
+ /**
43
+ Creates a synchronous host function that runs the given closure when it's called.
44
+ The value returned by the closure is synchronously returned to JS.
45
+ - Returns: A JavaScript function represented as a `JavaScriptObject`.
46
+ - Note: It refines the ObjC implementation from `EXJavaScriptRuntime` to properly catch Swift errors and rethrow them as ObjC `NSError`.
47
+ */
48
+ func createSyncFunction(_ name: String, argsCount: Int = 0, closure: @escaping SyncFunctionClosure) -> JavaScriptObject {
49
+ return __createSyncFunction(name, argsCount: argsCount) { this, args, errorPointer in
50
+ do {
51
+ return try runWithErrorPointer(errorPointer) {
52
+ return try closure(this, args)
53
+ }
54
+ } catch {
55
+ // Nicely log all errors to the console.
56
+ log.error(error)
57
+
58
+ // Can return anything as the error will be caught through the error pointer already.
59
+ return nil
60
+ }
61
+ }
62
+ }
26
63
  }
27
64
 
28
65
  internal final class JavaScriptEvalException: GenericException<NSError> {
@@ -85,6 +85,13 @@ public extension JavaScriptValue {
85
85
  }
86
86
  throw JavaScriptValueConversionException((kind: kind, target: "Object"))
87
87
  }
88
+
89
+ func asTypedArray() throws -> JavaScriptTypedArray {
90
+ if let typedArray = getTypedArray() {
91
+ return typedArray
92
+ }
93
+ throw JavaScriptValueConversionException((kind: kind, target: "TypedArray"))
94
+ }
88
95
  }
89
96
 
90
97
  internal final class JavaScriptValueConversionException: GenericException<(kind: JavaScriptValueKind, target: String)> {
@@ -0,0 +1,67 @@
1
+ // Copyright 2022-present 650 Industries. All rights reserved.
2
+
3
+ #include <unordered_map>
4
+ #include "TypedArray.h"
5
+
6
+ namespace expo {
7
+
8
+ std::unordered_map<std::string, TypedArrayKind> nameToKindMap = {
9
+ {"Int8Array", TypedArrayKind::Int8Array},
10
+ {"Int16Array", TypedArrayKind::Int16Array},
11
+ {"Int32Array", TypedArrayKind::Int32Array},
12
+ {"Uint8Array", TypedArrayKind::Uint8Array},
13
+ {"Uint8ClampedArray", TypedArrayKind::Uint8ClampedArray},
14
+ {"Uint16Array", TypedArrayKind::Uint16Array},
15
+ {"Uint32Array", TypedArrayKind::Uint32Array},
16
+ {"Float32Array", TypedArrayKind::Float32Array},
17
+ {"Float64Array", TypedArrayKind::Float64Array},
18
+ {"BigInt64Array", TypedArrayKind::BigInt64Array},
19
+ {"BigUint64Array", TypedArrayKind::BigUint64Array},
20
+ };
21
+
22
+ TypedArrayKind getTypedArrayKindForName(const std::string &name) {
23
+ return nameToKindMap.at(name);
24
+ }
25
+
26
+ TypedArray::TypedArray(jsi::Runtime &runtime, const jsi::Object &obj)
27
+ : jsi::Object(jsi::Value(runtime, obj).asObject(runtime)) {}
28
+
29
+ TypedArrayKind TypedArray::getKind(jsi::Runtime &runtime) const {
30
+ auto constructorName = this->getPropertyAsObject(runtime, "constructor")
31
+ .getProperty(runtime, "name")
32
+ .asString(runtime)
33
+ .utf8(runtime);
34
+ return getTypedArrayKindForName(constructorName);
35
+ };
36
+
37
+ size_t TypedArray::byteOffset(jsi::Runtime &runtime) const {
38
+ return getProperty(runtime, "byteOffset").asNumber();
39
+ }
40
+
41
+ jsi::ArrayBuffer TypedArray::getBuffer(jsi::Runtime &runtime) const {
42
+ auto buffer = getProperty(runtime, "buffer");
43
+ if (buffer.isObject() && buffer.asObject(runtime).isArrayBuffer(runtime)) {
44
+ return buffer.asObject(runtime).getArrayBuffer(runtime);
45
+ } else {
46
+ throw std::runtime_error("no ArrayBuffer attached");
47
+ }
48
+ }
49
+
50
+ void* TypedArray::getRawPointer(jsi::Runtime &runtime) {
51
+ return reinterpret_cast<void *>(getBuffer(runtime).data(runtime) + byteOffset(runtime));
52
+ }
53
+
54
+ bool isTypedArray(jsi::Runtime &runtime, const jsi::Object &jsObj) {
55
+ jsi::Object ArrayBuffer = runtime
56
+ .global()
57
+ .getPropertyAsObject(runtime, "ArrayBuffer");
58
+
59
+ jsi::Value isViewResult = ArrayBuffer
60
+ .getPropertyAsFunction(runtime, "isView")
61
+ .callWithThis(runtime, ArrayBuffer, {jsi::Value(runtime, jsObj)});
62
+
63
+ assert(isViewResult.isBool());
64
+ return isViewResult.getBool();
65
+ }
66
+
67
+ } // namespace expo
@@ -0,0 +1,46 @@
1
+ // Copyright 2022-present 650 Industries. All rights reserved.
2
+
3
+ #ifdef __cplusplus
4
+
5
+ #include <jsi/jsi.h>
6
+
7
+ namespace jsi = facebook::jsi;
8
+
9
+ namespace expo {
10
+
11
+ // Please keep it in-sync with the `EXTypedArrayKind` in Objective-C.
12
+ // We need to maintain two implementations to expose this enum to Swift.
13
+ enum class TypedArrayKind {
14
+ Int8Array = 1,
15
+ Int16Array = 2,
16
+ Int32Array = 3,
17
+ Uint8Array = 4,
18
+ Uint8ClampedArray = 5,
19
+ Uint16Array = 6,
20
+ Uint32Array = 7,
21
+ Float32Array = 8,
22
+ Float64Array = 9,
23
+ BigInt64Array = 10,
24
+ BigUint64Array = 11,
25
+ };
26
+
27
+ class TypedArray : public jsi::Object {
28
+ public:
29
+ TypedArray(jsi::Runtime &, const jsi::Object &);
30
+ TypedArray(TypedArray &&) = default;
31
+ TypedArray &operator=(TypedArray &&) = default;
32
+
33
+ TypedArrayKind getKind(jsi::Runtime &runtime) const;
34
+
35
+ size_t byteOffset(jsi::Runtime &runtime) const;
36
+
37
+ jsi::ArrayBuffer getBuffer(jsi::Runtime &runtime) const;
38
+
39
+ void* getRawPointer(jsi::Runtime &runtime);
40
+ };
41
+
42
+ bool isTypedArray(jsi::Runtime &runtime, const jsi::Object &jsObj);
43
+
44
+ } // namespace expo
45
+
46
+ #endif // __cplusplus
@@ -7,13 +7,11 @@
7
7
  #import <ExpoModulesCore/EXViewManagerAdapterClassesRegistry.h>
8
8
  #import <ExpoModulesCore/EXModuleRegistryHolderReactModule.h>
9
9
  #import <ExpoModulesCore/EXReactNativeEventEmitter.h>
10
- #import <ExpoModulesCore/Swift.h>
11
10
 
12
11
  @interface EXModuleRegistryAdapter ()
13
12
 
14
13
  @property (nonatomic, strong) EXModuleRegistryProvider *moduleRegistryProvider;
15
14
  @property (nonatomic, strong) EXViewManagerAdapterClassesRegistry *viewManagersClassesRegistry;
16
- @property (nonatomic, strong, nullable) ModulesProvider *swiftModulesProvider;
17
15
 
18
16
  @end
19
17
 
@@ -75,13 +73,4 @@
75
73
  return extraModules;
76
74
  }
77
75
 
78
- - (nullable SwiftInteropBridge *)swiftInteropBridgeModulesRegistry:(EXModuleRegistry *)moduleRegistry
79
- {
80
- if (_swiftModulesProvider) {
81
- return [[SwiftInteropBridge alloc] initWithModulesProvider:_swiftModulesProvider legacyModuleRegistry:moduleRegistry];
82
- } else {
83
- return nil;
84
- }
85
- }
86
-
87
76
  @end
@@ -5,26 +5,33 @@
5
5
  #import <ExpoModulesCore/EXInternalModule.h>
6
6
  #import <ExpoModulesCore/EXModuleRegistry.h>
7
7
 
8
+ // A convenience class, which acts as a store for the native modules proxy config
9
+
10
+ NS_SWIFT_NAME(ModulesProxyConfig)
11
+ @interface EXModulesProxyConfig : NSObject
12
+
13
+ - (instancetype)initWithConstants:(nonnull NSDictionary *)constants
14
+ methodNames:(nonnull NSDictionary *)methodNames
15
+ viewManagers:(nonnull NSDictionary *)viewManagerMetadata;
16
+
17
+ - (void)addEntriesFromConfig:(nonnull const EXModulesProxyConfig *)config;
18
+ - (nonnull NSDictionary<NSString *, id> *)toDictionary;
19
+
20
+ @end
21
+
8
22
  // RCTBridgeModule capable of receiving method calls from JS and forwarding them
9
23
  // to proper exported universal modules. Also, it exports important constants to JS, like
10
24
  // properties of exported methods and modules' constants.
11
25
 
12
- // Swift compatibility headers (e.g. `ExpoModulesCore-Swift.h`) are not available in headers,
13
- // so we use class forward declaration here. Swift header must be imported in the `.m` file.
14
- @class SwiftInteropBridge;
15
- @class ModulesProvider;
16
-
17
- NS_SWIFT_NAME(NativeModulesProxy)
26
+ NS_SWIFT_NAME(LegacyNativeModulesProxy)
18
27
  @interface EXNativeModulesProxy : NSObject <RCTBridgeModule>
19
28
 
20
- @property (nonatomic, strong) SwiftInteropBridge *swiftInteropBridge;
29
+ @property (nonatomic, strong, readonly) EXModulesProxyConfig *nativeModulesConfig;
21
30
 
22
31
  - (nonnull instancetype)init;
23
32
  - (nonnull instancetype)initWithModuleRegistry:(nullable EXModuleRegistry *)moduleRegistry;
33
+ - (nonnull instancetype)initWithCustomModuleRegistry:(nonnull EXModuleRegistry *)moduleRegistry;
24
34
 
25
35
  - (void)callMethod:(NSString *)moduleName methodNameOrKey:(id)methodNameOrKey arguments:(NSArray *)arguments resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject;
26
- - (id)callMethodSync:(NSString *)moduleName methodName:(NSString *)methodName arguments:(NSArray *)arguments;
27
-
28
- + (ModulesProvider *)getExpoModulesProvider;
29
36
 
30
37
  @end