expo-modules-core 1.2.6 → 1.3.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 (180) hide show
  1. package/CHANGELOG.md +33 -4
  2. package/ExpoModulesCore.podspec +1 -0
  3. package/README.md +1 -1
  4. package/android/ExpoModulesCorePlugin.gradle +16 -0
  5. package/android/build.gradle +3 -2
  6. package/android/src/main/cpp/Exceptions.cpp +8 -0
  7. package/android/src/main/cpp/Exceptions.h +11 -0
  8. package/android/src/main/cpp/ExpoModulesHostObject.cpp +22 -5
  9. package/android/src/main/cpp/ExpoModulesHostObject.h +5 -0
  10. package/android/src/main/cpp/JNIInjector.cpp +2 -0
  11. package/android/src/main/cpp/JSIInteropModuleRegistry.cpp +25 -1
  12. package/android/src/main/cpp/JSIInteropModuleRegistry.h +14 -0
  13. package/android/src/main/cpp/JSIObjectWrapper.h +15 -4
  14. package/android/src/main/cpp/JSITypeConverter.h +3 -2
  15. package/android/src/main/cpp/JavaReferencesCache.cpp +2 -0
  16. package/android/src/main/cpp/JavaScriptFunction.cpp +56 -0
  17. package/android/src/main/cpp/JavaScriptFunction.h +54 -0
  18. package/android/src/main/cpp/JavaScriptModuleObject.cpp +225 -105
  19. package/android/src/main/cpp/JavaScriptModuleObject.h +67 -34
  20. package/android/src/main/cpp/JavaScriptObject.cpp +55 -1
  21. package/android/src/main/cpp/JavaScriptObject.h +17 -13
  22. package/android/src/main/cpp/JavaScriptRuntime.cpp +12 -3
  23. package/android/src/main/cpp/JavaScriptRuntime.h +9 -1
  24. package/android/src/main/cpp/JavaScriptValue.cpp +9 -0
  25. package/android/src/main/cpp/JavaScriptValue.h +4 -0
  26. package/android/src/main/cpp/MethodMetadata.cpp +66 -87
  27. package/android/src/main/cpp/MethodMetadata.h +18 -16
  28. package/android/src/main/cpp/ObjectDeallocator.h +25 -0
  29. package/android/src/main/cpp/WeakRuntimeHolder.cpp +7 -0
  30. package/android/src/main/cpp/WeakRuntimeHolder.h +4 -0
  31. package/android/src/main/cpp/types/CppType.h +4 -1
  32. package/android/src/main/cpp/types/FrontendConverter.cpp +58 -0
  33. package/android/src/main/cpp/types/FrontendConverter.h +45 -0
  34. package/android/src/main/cpp/types/FrontendConverterProvider.cpp +3 -0
  35. package/android/src/main/cpp/types/JNIToJSIConverter.cpp +88 -0
  36. package/android/src/main/cpp/types/JNIToJSIConverter.h +22 -0
  37. package/android/src/main/java/com/facebook/react/uimanager/ReactStylesDiffMapHelper.kt +10 -0
  38. package/android/src/main/java/expo/modules/kotlin/AppContext.kt +18 -25
  39. package/android/src/main/java/expo/modules/kotlin/FilteredIterator.kt +37 -0
  40. package/android/src/main/java/expo/modules/kotlin/KotlinInteropModuleRegistry.kt +1 -1
  41. package/android/src/main/java/expo/modules/kotlin/ModuleHolder.kt +34 -21
  42. package/android/src/main/java/expo/modules/kotlin/ModuleRegistry.kt +13 -3
  43. package/android/src/main/java/expo/modules/kotlin/Utils.kt +21 -0
  44. package/android/src/main/java/expo/modules/kotlin/activityresult/AppContextActivityResultCaller.kt +21 -1
  45. package/android/src/main/java/expo/modules/kotlin/classcomponent/ClassComponentBuilder.kt +112 -0
  46. package/android/src/main/java/expo/modules/kotlin/classcomponent/ClassDefinitionData.kt +10 -0
  47. package/android/src/main/java/expo/modules/kotlin/exception/CodedException.kt +21 -0
  48. package/android/src/main/java/expo/modules/kotlin/exception/CommonExceptions.kt +15 -0
  49. package/android/src/main/java/expo/modules/kotlin/functions/AnyFunction.kt +17 -4
  50. package/android/src/main/java/expo/modules/kotlin/functions/AsyncFunction.kt +38 -8
  51. package/android/src/main/java/expo/modules/kotlin/functions/AsyncFunctionComponent.kt +3 -2
  52. package/android/src/main/java/expo/modules/kotlin/functions/AsyncFunctionWithPromiseComponent.kt +3 -2
  53. package/android/src/main/java/expo/modules/kotlin/functions/SuspendFunctionComponent.kt +1 -0
  54. package/android/src/main/java/expo/modules/kotlin/functions/SyncFunctionComponent.kt +18 -11
  55. package/android/src/main/java/expo/modules/kotlin/jni/CppType.kt +4 -1
  56. package/android/src/main/java/expo/modules/kotlin/jni/JNIDeallocator.kt +73 -0
  57. package/android/src/main/java/expo/modules/kotlin/jni/JSIInteropModuleRegistry.kt +28 -2
  58. package/android/src/main/java/expo/modules/kotlin/jni/JavaCallback.kt +8 -1
  59. package/android/src/main/java/expo/modules/kotlin/jni/JavaScriptFunction.kt +48 -0
  60. package/android/src/main/java/expo/modules/kotlin/jni/JavaScriptModuleObject.kt +40 -3
  61. package/android/src/main/java/expo/modules/kotlin/jni/JavaScriptObject.kt +23 -3
  62. package/android/src/main/java/expo/modules/kotlin/jni/JavaScriptValue.kt +26 -1
  63. package/android/src/main/java/expo/modules/kotlin/modules/Module.kt +0 -11
  64. package/android/src/main/java/expo/modules/kotlin/modules/ModuleDefinitionBuilder.kt +26 -16
  65. package/android/src/main/java/expo/modules/kotlin/modules/ModuleDefinitionData.kt +3 -1
  66. package/android/src/main/java/expo/modules/kotlin/sharedobjects/SharedObject.kt +12 -0
  67. package/android/src/main/java/expo/modules/kotlin/sharedobjects/SharedObjectRegistry.kt +62 -0
  68. package/android/src/main/java/expo/modules/kotlin/sharedobjects/SharedObjectTypeConverter.kt +27 -0
  69. package/android/src/main/java/expo/modules/kotlin/types/AnyType.kt +2 -1
  70. package/android/src/main/java/expo/modules/kotlin/types/EitherTypeConverter.kt +7 -6
  71. package/android/src/main/java/expo/modules/kotlin/types/JavaScriptFunctionTypeConverter.kt +22 -0
  72. package/android/src/main/java/expo/modules/kotlin/types/TypeConverter.kt +30 -24
  73. package/android/src/main/java/expo/modules/kotlin/types/TypeConverterProvider.kt +45 -1
  74. package/android/src/main/java/expo/modules/kotlin/types/TypedArrayTypeConverter.kt +3 -2
  75. package/android/src/main/java/expo/modules/kotlin/views/AnyViewProp.kt +3 -1
  76. package/android/src/main/java/expo/modules/kotlin/views/ConcreteViewProp.kt +3 -3
  77. package/android/src/main/java/expo/modules/kotlin/views/FilteredReadableMap.kt +53 -0
  78. package/android/src/main/java/expo/modules/kotlin/views/GroupViewManagerWrapper.kt +25 -5
  79. package/android/src/main/java/expo/modules/kotlin/views/SimpleViewManagerWrapper.kt +25 -5
  80. package/android/src/main/java/expo/modules/kotlin/views/ViewDefinitionBuilder.kt +161 -10
  81. package/android/src/main/java/expo/modules/kotlin/views/ViewGroupDefinitionBuilder.kt +0 -67
  82. package/android/src/main/java/expo/modules/kotlin/views/ViewManagerDefinition.kt +7 -8
  83. package/android/src/main/java/expo/modules/kotlin/views/ViewManagerWrapperDelegate.kt +40 -3
  84. package/android/src/main/java/expo/modules/kotlin/views/ViewTypeConverter.kt +44 -0
  85. package/android-annotation/build.gradle +45 -0
  86. package/android-annotation/src/main/java/expo/modules/annotation/Config.kt +7 -0
  87. package/android-annotation/src/main/java/expo/modules/annotation/ConverterBinder.kt +7 -0
  88. package/android-annotation-processor/build.gradle +51 -0
  89. package/android-annotation-processor/src/main/java/expo/modules/annotationprocessor/ExpoSymbolProcessor.kt +175 -0
  90. package/android-annotation-processor/src/main/java/expo/modules/annotationprocessor/ExpoSymbolProcessorProvider.kt +10 -0
  91. package/android-annotation-processor/src/main/resources/META-INF/services/com.google.devtools.ksp.processing.SymbolProcessorProvider +1 -0
  92. package/build/NativeViewManagerAdapter.native.d.ts.map +1 -1
  93. package/build/NativeViewManagerAdapter.native.js +36 -23
  94. package/build/NativeViewManagerAdapter.native.js.map +1 -1
  95. package/build/requireNativeModule.js +2 -2
  96. package/build/requireNativeModule.js.map +1 -1
  97. package/common/cpp/fabric/ExpoViewProps.cpp +18 -3
  98. package/common/cpp/fabric/ExpoViewProps.h +4 -1
  99. package/ios/Fabric/ExpoFabricView.swift +10 -10
  100. package/ios/Fabric/ExpoFabricViewObjC.h +2 -0
  101. package/ios/Fabric/ExpoFabricViewObjC.mm +17 -2
  102. package/ios/JSI/EXJSIInstaller.mm +1 -1
  103. package/ios/JSI/EXJSIUtils.h +5 -0
  104. package/ios/JSI/EXJSIUtils.mm +17 -0
  105. package/ios/JSI/EXJavaScriptRuntime.h +5 -0
  106. package/ios/JSI/EXJavaScriptRuntime.mm +6 -0
  107. package/ios/JSI/EXJavaScriptValue.h +2 -0
  108. package/ios/JSI/EXJavaScriptValue.mm +8 -0
  109. package/ios/JSI/EXJavaScriptWeakObject.mm +29 -8
  110. package/ios/JSI/EXRawJavaScriptFunction.h +24 -0
  111. package/ios/JSI/EXRawJavaScriptFunction.mm +52 -0
  112. package/ios/JSI/ExpoModulesHostObject.mm +1 -1
  113. package/ios/JSI/JavaScriptValue.swift +28 -1
  114. package/ios/ModuleRegistry/EXModuleRegistry.h +0 -4
  115. package/ios/ModuleRegistry/EXModuleRegistry.m +0 -23
  116. package/ios/ModuleRegistryAdapter/EXModuleRegistryAdapter.m +0 -16
  117. package/ios/ModuleRegistryProvider/EXModuleRegistryProvider.m +0 -6
  118. package/ios/NativeModulesProxy/EXNativeModulesProxy.mm +1 -31
  119. package/ios/Swift/AppContext.swift +46 -6
  120. package/ios/Swift/Arguments/Convertible.swift +3 -3
  121. package/ios/Swift/Arguments/Convertibles.swift +5 -5
  122. package/ios/Swift/Classes/ClassComponent.swift +18 -12
  123. package/ios/Swift/Classes/ClassRegistry.swift +31 -0
  124. package/ios/Swift/Conversions.swift +19 -3
  125. package/ios/Swift/Convertibles/Convertibles+Color.swift +3 -3
  126. package/ios/Swift/Convertibles/Either.swift +6 -4
  127. package/ios/Swift/DynamicTypes/AnyDynamicType.swift +18 -2
  128. package/ios/Swift/DynamicTypes/DynamicArrayType.swift +3 -3
  129. package/ios/Swift/DynamicTypes/DynamicConvertibleType.swift +2 -2
  130. package/ios/Swift/DynamicTypes/DynamicEnumType.swift +1 -1
  131. package/ios/Swift/DynamicTypes/DynamicJavaScriptType.swift +27 -0
  132. package/ios/Swift/DynamicTypes/DynamicOptionalType.swift +9 -2
  133. package/ios/Swift/DynamicTypes/DynamicRawType.swift +1 -1
  134. package/ios/Swift/DynamicTypes/DynamicSharedObjectType.swift +16 -2
  135. package/ios/Swift/DynamicTypes/DynamicType.swift +6 -0
  136. package/ios/Swift/DynamicTypes/DynamicTypedArrayType.swift +15 -4
  137. package/ios/Swift/DynamicTypes/DynamicViewType.swift +68 -0
  138. package/ios/Swift/ExpoBridgeModule.swift +1 -1
  139. package/ios/Swift/Functions/AnyFunction.swift +5 -4
  140. package/ios/Swift/Functions/AsyncFunctionComponent.swift +22 -19
  141. package/ios/Swift/Functions/ConcurrentFunctionDefinition.swift +29 -13
  142. package/ios/Swift/Functions/SyncFunctionComponent.swift +26 -15
  143. package/ios/Swift/JavaScriptFunction.swift +68 -0
  144. package/ios/Swift/JavaScriptUtils.swift +57 -18
  145. package/ios/Swift/ModuleHolder.swift +22 -10
  146. package/ios/Swift/Modules/ModuleDefinition.swift +8 -2
  147. package/ios/Swift/Objects/JavaScriptObjectBuilder.swift +8 -8
  148. package/ios/Swift/Objects/ObjectDefinition.swift +17 -15
  149. package/ios/Swift/Objects/PropertyComponent.swift +23 -17
  150. package/ios/Swift/Records/AnyField.swift +1 -1
  151. package/ios/Swift/Records/Field.swift +2 -2
  152. package/ios/Swift/Records/Record.swift +5 -5
  153. package/ios/Swift/SharedObjects/SharedObjectRegistry.swift +4 -0
  154. package/ios/Swift/Views/AnyViewProp.swift +1 -1
  155. package/ios/Swift/Views/ComponentData.swift +37 -2
  156. package/ios/Swift/Views/ConcreteViewProp.swift +2 -2
  157. package/ios/Swift/Views/ViewDefinition.swift +39 -0
  158. package/ios/Swift/Views/ViewModuleWrapper.swift +0 -29
  159. package/ios/Tests/ClassComponentSpec.swift +39 -27
  160. package/ios/Tests/ConvertiblesSpec.swift +75 -49
  161. package/ios/Tests/DynamicTypeSpec.swift +29 -27
  162. package/ios/Tests/EitherSpec.swift +9 -7
  163. package/ios/Tests/ExpoModulesSpec.swift +13 -13
  164. package/ios/Tests/FunctionSpec.swift +38 -22
  165. package/ios/Tests/JavaScriptRuntimeSpec.swift +4 -0
  166. package/ios/Tests/PropertyComponentSpec.swift +33 -30
  167. package/ios/Tests/RecordSpec.swift +7 -5
  168. package/ios/Tests/SharedObjectRegistrySpec.swift +12 -12
  169. package/ios/Tests/TypedArraysSpec.swift +1 -1
  170. package/ios/Tests/ViewDefinitionSpec.swift +4 -2
  171. package/package.json +2 -2
  172. package/src/NativeViewManagerAdapter.native.tsx +33 -29
  173. package/src/requireNativeModule.ts +2 -2
  174. package/android/src/main/java/expo/modules/kotlin/views/ViewManagerDefinitionBuilder.kt +0 -132
  175. package/ios/EXViewManager.h +0 -21
  176. package/ios/EXViewManager.m +0 -128
  177. package/ios/ModuleRegistryAdapter/EXViewManagerAdapterClassesRegistry.h +0 -17
  178. package/ios/ModuleRegistryAdapter/EXViewManagerAdapterClassesRegistry.m +0 -67
  179. package/ios/ViewManagerAdapter/EXViewManagerAdapter.h +0 -17
  180. package/ios/ViewManagerAdapter/EXViewManagerAdapter.m +0 -45
@@ -15,7 +15,7 @@ public protocol Record: Convertible {
15
15
  /**
16
16
  Initializes a record from given dictionary. Only members wrapped by `@Field` will be set in the object.
17
17
  */
18
- init(from: Dict) throws
18
+ init(from: Dict, appContext: AppContext) throws
19
19
 
20
20
  /**
21
21
  Converts the record back to the dictionary. Only members wrapped by `@Field` will be set in the dictionary.
@@ -27,14 +27,14 @@ public protocol Record: Convertible {
27
27
  Provides the default implementation of `Record` protocol.
28
28
  */
29
29
  public extension Record {
30
- static func convert(from value: Any?) throws -> Self {
30
+ static func convert(from value: Any?, appContext: AppContext) throws -> Self {
31
31
  if let value = value as? Dict {
32
- return try Self(from: value)
32
+ return try Self(from: value, appContext: appContext)
33
33
  }
34
34
  throw Conversions.ConvertingException<Self>(value)
35
35
  }
36
36
 
37
- init(from dict: Dict) throws {
37
+ init(from dict: Dict, appContext: AppContext) throws {
38
38
  self.init()
39
39
 
40
40
  let dictKeys = dict.keys
@@ -45,7 +45,7 @@ public extension Record {
45
45
  return
46
46
  }
47
47
  if dictKeys.contains(key) || field.isRequired {
48
- try field.set(dict[key])
48
+ try field.set(dict[key], appContext: appContext)
49
49
  }
50
50
  }
51
51
  }
@@ -126,4 +126,8 @@ public final class SharedObjectRegistry {
126
126
  }
127
127
  return createSharedJavaScriptObject(runtime: runtime, nativeObject: nativeObject)
128
128
  }
129
+
130
+ internal static func clear() {
131
+ pairs.removeAll()
132
+ }
129
133
  }
@@ -12,5 +12,5 @@ public protocol AnyViewProp: ViewManagerDefinitionComponent {
12
12
  /**
13
13
  Function that sets the underlying prop value for given view.
14
14
  */
15
- func set(value: Any, onView: UIView) throws
15
+ func set(value: Any, onView: UIView, appContext: AppContext) throws
16
16
  }
@@ -43,6 +43,37 @@ public final class ComponentData: RCTComponentData {
43
43
  return super.createPropBlock(propName, isShadowView: isShadowView)
44
44
  }
45
45
 
46
+ public override func setProps(_ props: [String: Any], forView view: RCTComponent) {
47
+ guard let view = view as? UIView else {
48
+ log.warn("Given view is not an UIView")
49
+ return
50
+ }
51
+ guard let viewManager = moduleHolder?.viewManager else {
52
+ log.warn("View manager '\(self.name)' not found")
53
+ return
54
+ }
55
+ guard let appContext = moduleHolder?.appContext else {
56
+ log.warn("App context has been lost")
57
+ return
58
+ }
59
+ let propsDict = viewManager.propsDict()
60
+ var remainingProps = props
61
+
62
+ for (key, prop) in propsDict {
63
+ let newValue = props[key] as Any
64
+
65
+ // TODO: @tsapeta: Figure out better way to rethrow errors from here.
66
+ try? prop.set(value: Conversions.fromNSObject(newValue), onView: view, appContext: appContext)
67
+
68
+ remainingProps.removeValue(forKey: key)
69
+ }
70
+
71
+ // Let the base class `RCTComponentData` handle all remaining props.
72
+ super.setProps(remainingProps, forView: view)
73
+
74
+ viewManager.callLifecycleMethods(withType: .didUpdateProps, forView: view)
75
+ }
76
+
46
77
  /**
47
78
  The base `RCTComponentData` class does some Objective-C dynamic calls in this function, but we don't
48
79
  need to do these slow operations since the Sweet API gives us necessary details without reflections.
@@ -52,8 +83,12 @@ public final class ComponentData: RCTComponentData {
52
83
  var directEvents: [String] = []
53
84
  let superClass: AnyClass? = managerClass.superclass()
54
85
 
55
- if let eventNames = moduleHolder?.viewManager?.eventNames {
56
- for eventName in eventNames {
86
+ if let viewManager = moduleHolder?.viewManager {
87
+ for prop in viewManager.props {
88
+ // `id` allows every type to be passed in
89
+ propTypes[prop.name] = "id"
90
+ }
91
+ for eventName in viewManager.eventNames {
57
92
  directEvents.append(RCTNormalizeInputEventName(eventName))
58
93
  propTypes[eventName] = "BOOL"
59
94
  }
@@ -32,13 +32,13 @@ public final class ConcreteViewProp<ViewType: UIView, PropType: AnyArgument>: An
32
32
  /**
33
33
  Function that sets the underlying prop value for given view.
34
34
  */
35
- public func set(value: Any, onView view: UIView) throws {
35
+ public func set(value: Any, onView view: UIView, appContext: AppContext) throws {
36
36
  // Method's signature must be type-erased to conform to `AnyViewProp` protocol.
37
37
  // Given view must be castable to the generic `ViewType` type.
38
38
  guard let view = view as? ViewType else {
39
39
  throw IncompatibleViewException((propName: name, viewType: ViewType.self))
40
40
  }
41
- guard let value = try propType.cast(value) as? PropType else {
41
+ guard let value = try propType.cast(value, appContext: appContext) as? PropType else {
42
42
  throw Conversions.CastingException<PropType>(value)
43
43
  }
44
44
  setter(view, value)
@@ -52,14 +52,53 @@ public final class ViewDefinition<ViewType: UIView>: ViewManagerDefinition {
52
52
  public static func buildExpression(_ element: ViewLifecycleMethod<ViewType>) -> AnyViewDefinitionElement {
53
53
  return element
54
54
  }
55
+
56
+ /**
57
+ Accepts functions as a view definition elements.
58
+ */
59
+ public static func buildExpression<ElementType: ViewDefinitionFunctionElement>(
60
+ _ element: ElementType
61
+ ) -> AnyViewDefinitionElement {
62
+ return element
63
+ }
64
+
65
+ /**
66
+ Accepts functions that take the owner as a view definition elements.
67
+ */
68
+ public static func buildExpression<ElementType: ViewDefinitionFunctionElement>(
69
+ _ element: ElementType
70
+ ) -> AnyViewDefinitionElement where ElementType.ViewType == ViewType {
71
+ // Enforce async functions to run on the main queue
72
+ if var function = element as? AnyAsyncFunctionComponent {
73
+ function.runOnQueue(.main)
74
+ function.takesOwner = true
75
+ }
76
+ return element
77
+ }
55
78
  }
56
79
  }
57
80
 
81
+ // MARK: - AnyViewDefinitionElement
82
+
58
83
  public protocol AnyViewDefinitionElement: AnyDefinition {}
59
84
  extension ConcreteViewProp: AnyViewDefinitionElement {}
60
85
  extension EventsDefinition: AnyViewDefinitionElement {}
61
86
  extension ViewLifecycleMethod: AnyViewDefinitionElement {}
62
87
 
88
+ // MARK: - ViewDefinitionFunctionElement
89
+
90
+ public protocol ViewDefinitionFunctionElement: AnyViewDefinitionElement {
91
+ associatedtype ViewType
92
+ }
93
+ extension AsyncFunctionComponent: ViewDefinitionFunctionElement {
94
+ public typealias ViewType = FirstArgType
95
+ }
96
+ extension ConcurrentFunctionDefinition: ViewDefinitionFunctionElement {
97
+ public typealias ViewType = FirstArgType
98
+ }
99
+
100
+ extension UIView: AnyArgument {}
101
+
63
102
  /**
64
103
  Creates a view definition describing the native view exported to React.
65
104
  */
@@ -91,35 +91,6 @@ public final class ViewModuleWrapper: RCTViewManager, DynamicModuleWrapperProtoc
91
91
  return view
92
92
  }
93
93
 
94
- /**
95
- The config for `proxiedProperties` prop. In Objective-C style, this function is generated by `RCT_CUSTOM_VIEW_PROPERTY` macro.
96
- */
97
- @objc
98
- public class func propConfig_proxiedProperties() -> [String] {
99
- return ["NSDictionary", "__custom__"]
100
- }
101
-
102
- /**
103
- The setter for `proxiedProperties` prop. In Objective-C style, this function is generated by `RCT_CUSTOM_VIEW_PROPERTY` macro.
104
- */
105
- @objc
106
- public func set_proxiedProperties(_ json: Any, forView view: UIView, withDefaultView defaultView: UIView) {
107
- guard let json = json as? [String: Any], let viewManager = wrappedModuleHolder.definition.viewManager else {
108
- return
109
- }
110
- let props = viewManager.propsDict()
111
-
112
- for (key, prop) in props {
113
- let newValue = json[key] as Any
114
-
115
- // TODO: @tsapeta: Figure out better way to rethrow errors from here.
116
- // Adding `throws` keyword to the function results in different
117
- // method signature in Objective-C. Maybe just call `RCTLogError`?
118
- try? prop.set(value: Conversions.fromNSObject(newValue), onView: view)
119
- }
120
- viewManager.callLifecycleMethods(withType: .didUpdateProps, forView: view)
121
- }
122
-
123
94
  public static let viewManagerAdapterPrefix = "ViewManagerAdapter_"
124
95
 
125
96
  /**
@@ -32,9 +32,9 @@ class ClassComponentSpec: ExpoSpec {
32
32
  }
33
33
 
34
34
  it("builds a class") {
35
- let runtime = JavaScriptRuntime()
35
+ let appContext = AppContext.create()
36
36
  let klass = Class("") {}
37
- let object = klass.build(inRuntime: runtime)
37
+ let object = try klass.build(appContext: appContext)
38
38
 
39
39
  expect(object.hasProperty("prototype")) == true
40
40
  expect(object.getProperty("prototype").kind) == .object
@@ -43,7 +43,7 @@ class ClassComponentSpec: ExpoSpec {
43
43
 
44
44
  describe("module") {
45
45
  let appContext = AppContext.create()
46
- let runtime = appContext.runtime
46
+ let runtime = try! appContext.runtime
47
47
 
48
48
  beforeSuite {
49
49
  class ClassTestModule: Module {
@@ -67,72 +67,72 @@ class ClassComponentSpec: ExpoSpec {
67
67
  }
68
68
 
69
69
  it("is a function") {
70
- let klass = try runtime?.eval("expo.modules.ClassTest.MyClass")
71
- expect(klass?.isFunction()) == true
70
+ let klass = try runtime.eval("expo.modules.ClassTest.MyClass")
71
+ expect(klass.isFunction()) == true
72
72
  }
73
73
 
74
74
  it("has a name") {
75
- let klass = try runtime?.eval("expo.modules.ClassTest.MyClass.name")
76
- expect(klass?.getString()) == "MyClass"
75
+ let klass = try runtime.eval("expo.modules.ClassTest.MyClass.name")
76
+ expect(klass.getString()) == "MyClass"
77
77
  }
78
78
 
79
79
  it("has a prototype") {
80
- let prototype = try runtime?.eval("expo.modules.ClassTest.MyClass.prototype")
81
- expect(prototype?.isObject()) == true
80
+ let prototype = try runtime.eval("expo.modules.ClassTest.MyClass.prototype")
81
+ expect(prototype.isObject()) == true
82
82
  }
83
83
 
84
84
  it("has keys in prototype") {
85
- let prototypeKeys = try runtime?.eval("Object.keys(expo.modules.ClassTest.MyClass.prototype)")
85
+ let prototypeKeys = try runtime.eval("Object.keys(expo.modules.ClassTest.MyClass.prototype)")
86
86
  .getArray()
87
- .map { $0.getString() } ?? []
87
+ .map { $0.getString() }
88
88
 
89
89
  expect(prototypeKeys).to(contain("myFunction"))
90
90
  expect(prototypeKeys).notTo(contain("__native_constructor__"))
91
91
  }
92
92
 
93
93
  it("is an instance of") {
94
- let isInstanceOf = try runtime?.eval([
94
+ let isInstanceOf = try runtime.eval([
95
95
  "myObject = new expo.modules.ClassTest.MyClass()",
96
96
  "myObject instanceof expo.modules.ClassTest.MyClass",
97
97
  ])
98
98
 
99
- expect(isInstanceOf?.getBool()) == true
99
+ expect(isInstanceOf.getBool()) == true
100
100
  }
101
101
 
102
102
  it("defines properties on initialization") {
103
103
  // The properties are not specified in the prototype, but defined during initialization.
104
- let object = try runtime?.eval("new expo.modules.ClassTest.MyClass()").asObject()
105
- expect(object?.getPropertyNames()).to(contain("foo"))
106
- expect(object?.getProperty("foo").getString()) == "bar"
104
+ let object = try runtime.eval("new expo.modules.ClassTest.MyClass()").asObject()
105
+ expect(object.getPropertyNames()).to(contain("foo"))
106
+ expect(object.getProperty("foo").getString()) == "bar"
107
107
  }
108
108
  }
109
109
 
110
110
  describe("class with associated type") {
111
111
  let appContext = AppContext.create()
112
- let runtime = appContext.runtime
112
+ let runtime = try! appContext.runtime
113
113
 
114
114
  beforeSuite {
115
115
  appContext.moduleRegistry.register(moduleType: ModuleWithCounterClass.self)
116
116
  }
117
117
  it("is defined") {
118
- let isDefined = try! runtime!.eval("'Counter' in expo.modules.TestModule")
118
+ let isDefined = try runtime.eval("'Counter' in expo.modules.TestModule")
119
119
 
120
120
  expect(isDefined.getBool()) == true
121
121
  }
122
122
  it("creates shared object") {
123
- let jsObject = try! runtime!.eval("new expo.modules.TestModule.Counter(0)").getObject()
123
+ let jsObject = try runtime.eval("new expo.modules.TestModule.Counter(0)").getObject()
124
124
  let nativeObject = SharedObjectRegistry.toNativeObject(jsObject)
125
125
 
126
126
  expect(nativeObject).notTo(beNil())
127
127
  }
128
128
  it("registers shared object") {
129
129
  let oldSize = SharedObjectRegistry.size
130
- try! runtime?.eval("object = new expo.modules.TestModule.Counter(0)")
130
+ try runtime.eval("object = new expo.modules.TestModule.Counter(0)")
131
131
 
132
132
  expect(SharedObjectRegistry.size) == oldSize + 1
133
133
  }
134
134
  it("calls function with owner") {
135
- try runtime?.eval([
135
+ try runtime.eval([
136
136
  "object = new expo.modules.TestModule.Counter(0)",
137
137
  "object.increment(1)",
138
138
  ])
@@ -140,7 +140,7 @@ class ClassComponentSpec: ExpoSpec {
140
140
  }
141
141
  it("creates with initial value") {
142
142
  let initialValue = Int.random(in: 1..<100)
143
- let value = try runtime!.eval([
143
+ let value = try runtime.eval([
144
144
  "object = new expo.modules.TestModule.Counter(\(initialValue))",
145
145
  "object.getValue()",
146
146
  ])
@@ -149,7 +149,7 @@ class ClassComponentSpec: ExpoSpec {
149
149
  expect(value.getInt()) == initialValue
150
150
  }
151
151
  it("gets shared object value") {
152
- let value = try runtime!.eval([
152
+ let value = try runtime.eval([
153
153
  "object = new expo.modules.TestModule.Counter(0)",
154
154
  "object.getValue()",
155
155
  ])
@@ -158,10 +158,10 @@ class ClassComponentSpec: ExpoSpec {
158
158
  expect(value.isNumber()) == true
159
159
  }
160
160
  it("changes shared object") {
161
- try! runtime?.eval("object = new expo.modules.TestModule.Counter(0)")
161
+ try runtime.eval("object = new expo.modules.TestModule.Counter(0)")
162
162
  let incrementBy = Int.random(in: 1..<100)
163
- let value = try runtime!.eval("object.getValue()").asInt()
164
- let newValue = try runtime!.eval([
163
+ let value = try runtime.eval("object.getValue()").asInt()
164
+ let newValue = try runtime.eval([
165
165
  "object.increment(\(incrementBy))",
166
166
  "object.getValue()",
167
167
  ])
@@ -172,7 +172,7 @@ class ClassComponentSpec: ExpoSpec {
172
172
 
173
173
  it("gets value from the dynamic property") {
174
174
  let initialValue = Int.random(in: 1..<100)
175
- let value = try runtime!.eval([
175
+ let value = try runtime.eval([
176
176
  "object = new expo.modules.TestModule.Counter(\(initialValue))",
177
177
  "object.currentValue"
178
178
  ])
@@ -180,6 +180,14 @@ class ClassComponentSpec: ExpoSpec {
180
180
  expect(value.kind) == .number
181
181
  expect(value.getInt()) == initialValue
182
182
  }
183
+
184
+ it("initializes the shared object from native") {
185
+ let initialValue = Int.random(in: 1..<100)
186
+ let value = try runtime.eval("expo.modules.TestModule.newCounter(\(initialValue))")
187
+
188
+ expect(value.kind) == .object
189
+ expect(value.getObject().getProperty("currentValue").getInt()) == initialValue
190
+ }
183
191
  }
184
192
  }
185
193
  }
@@ -191,6 +199,10 @@ fileprivate final class ModuleWithCounterClass: Module {
191
199
  func definition() -> ModuleDefinition {
192
200
  Name("TestModule")
193
201
 
202
+ Function("newCounter") { (initialValue: Int) in
203
+ return Counter(initialValue: initialValue)
204
+ }
205
+
194
206
  Class(Counter.self) {
195
207
  Constructor { (initialValue: Int) in
196
208
  return Counter(initialValue: initialValue)