expo-modules-core 58.0.15 → 58.0.16

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 (59) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/android/build.gradle +2 -2
  3. package/android/prebuilt/metadata.json +2 -2
  4. package/android/prebuilt/native-libs.tar.xz +0 -0
  5. package/android/src/main/cpp/fabric/AndroidExpoViewComponentDescriptor.cpp +0 -18
  6. package/ios/Core/AppContext.swift +15 -12
  7. package/ios/Core/ArrayBuffers/ArrayBuffer.swift +4 -1
  8. package/ios/Core/Classes/ClassDefinition.swift +1 -1
  9. package/ios/Core/Classes/ClassRegistry.swift +5 -3
  10. package/ios/Core/Conversions.swift +43 -5
  11. package/ios/Core/DynamicTypes/AnyDynamicType.swift +1 -1
  12. package/ios/Core/DynamicTypes/DynamicArrayBufferType.swift +1 -1
  13. package/ios/Core/DynamicTypes/DynamicArrayType.swift +2 -2
  14. package/ios/Core/DynamicTypes/DynamicBoolType.swift +1 -1
  15. package/ios/Core/DynamicTypes/DynamicCodableType.swift +5 -5
  16. package/ios/Core/DynamicTypes/DynamicConvertibleType.swift +1 -7
  17. package/ios/Core/DynamicTypes/DynamicDataType.swift +1 -1
  18. package/ios/Core/DynamicTypes/DynamicDictionaryType.swift +2 -2
  19. package/ios/Core/DynamicTypes/DynamicEitherType.swift +1 -1
  20. package/ios/Core/DynamicTypes/DynamicEnumType.swift +1 -1
  21. package/ios/Core/DynamicTypes/DynamicJavaScriptType.swift +12 -6
  22. package/ios/Core/DynamicTypes/DynamicNumberType.swift +1 -1
  23. package/ios/Core/DynamicTypes/DynamicOptionalType.swift +4 -4
  24. package/ios/Core/DynamicTypes/DynamicRawType.swift +3 -3
  25. package/ios/Core/DynamicTypes/DynamicSharedObjectType.swift +2 -2
  26. package/ios/Core/DynamicTypes/DynamicStringType.swift +1 -1
  27. package/ios/Core/DynamicTypes/DynamicSwiftUIViewType.swift +2 -2
  28. package/ios/Core/DynamicTypes/DynamicTypedArrayType.swift +1 -1
  29. package/ios/Core/DynamicTypes/DynamicValueOrUndefinedType.swift +5 -5
  30. package/ios/Core/DynamicTypes/DynamicViewType.swift +2 -2
  31. package/ios/Core/DynamicTypes/DynamicVoidType.swift +2 -2
  32. package/ios/Core/Events/EventEmitter.swift +9 -3
  33. package/ios/Core/Functions/AnyFunctionDefinition.swift +1 -1
  34. package/ios/Core/Functions/AsyncFunctionDefinition.swift +2 -2
  35. package/ios/Core/Functions/ConcurrentFunctionDefinition.swift +2 -2
  36. package/ios/Core/Functions/OptimizedAsyncFunctionDefinition.swift +1 -1
  37. package/ios/Core/Functions/OptimizedSyncFunctionDefinition.swift +3 -3
  38. package/ios/Core/Functions/SyncFunctionDefinition.swift +5 -5
  39. package/ios/Core/JSValueDecoder.swift +32 -17
  40. package/ios/Core/JSValueEncoder.swift +17 -10
  41. package/ios/Core/MainValueConverter.swift +1 -1
  42. package/ios/Core/ModuleHolder.swift +4 -4
  43. package/ios/Core/Modules/CoreModule.swift +37 -48
  44. package/ios/Core/Objects/ConstantDefinition.swift +5 -3
  45. package/ios/Core/Objects/PropertyDefinition.swift +2 -2
  46. package/ios/Core/Promise.swift +1 -1
  47. package/ios/Core/Records/AnyField.swift +1 -1
  48. package/ios/Core/Records/Field.swift +3 -3
  49. package/ios/Core/Records/Record.swift +1 -4
  50. package/ios/Core/SharedObjects/SharedObject.swift +1 -1
  51. package/ios/Core/SharedObjects/SharedObjectNativeState.swift +1 -1
  52. package/ios/JS/ExpoRuntimeInstaller.swift +1 -1
  53. package/ios/Worklets/Core/DynamicSerializableType.swift +1 -1
  54. package/ios/Worklets/Core/DynamicWorkletType.swift +1 -1
  55. package/package.json +3 -3
  56. package/prebuilds/output/debug/xcframeworks/ExpoModulesCore.tar.gz +0 -0
  57. package/prebuilds/output/debug/xcframeworks/ExpoModulesWorklets.tar.gz +0 -0
  58. package/prebuilds/output/release/xcframeworks/ExpoModulesCore.tar.gz +0 -0
  59. package/prebuilds/output/release/xcframeworks/ExpoModulesWorklets.tar.gz +0 -0
@@ -19,7 +19,7 @@ internal struct DynamicTypedArrayType: AnyDynamicType {
19
19
  /**
20
20
  Converts JS typed array to its native representation.
21
21
  */
22
- func cast(jsValue: JavaScriptValue, appContext: AppContext) throws -> Any {
22
+ func cast(jsValue: borrowing JavaScriptValue, appContext: AppContext) throws -> Any {
23
23
  guard jsValue.isTypedArray() else {
24
24
  throw NotTypedArrayException(innerType)
25
25
  }
@@ -14,7 +14,7 @@ internal struct DynamicValueOrUndefinedType<InnerType: AnyArgument>: AnyDynamicT
14
14
  return type is Self
15
15
  }
16
16
 
17
- func cast(jsValue: JavaScriptValue, appContext: AppContext) throws -> Any {
17
+ func cast(jsValue: borrowing JavaScriptValue, appContext: AppContext) throws -> Any {
18
18
  if jsValue.isUndefined() {
19
19
  return ValueOrUndefined<InnerType>.undefined
20
20
  }
@@ -33,15 +33,15 @@ internal struct DynamicValueOrUndefinedType<InnerType: AnyArgument>: AnyDynamicT
33
33
  func convertResult<ResultType>(_ result: ResultType, appContext: AppContext) throws -> Any {
34
34
  let value = result as! ValueOrUndefined<InnerType>
35
35
  if case .undefined = value {
36
- // JavaScriptValue.undefined is not runtime specific, so it's safe to return here, even if it's not on the JS thread.
37
- return JavaScriptValue.undefined
36
+ // The shared box holds no engine value and creates `undefined` when read, so it is safe to return here, even off the JS thread.
37
+ return ConvertedJavaScriptValue.undefined
38
38
  }
39
39
  return try dynamicInnerType.convertResult(value.optional, appContext: appContext)
40
40
  }
41
41
 
42
42
  func castToJS<ValueType>(_ value: ValueType, appContext: AppContext) throws -> JavaScriptValue {
43
- if let jsValue = value as? JavaScriptValue {
44
- return jsValue
43
+ if let converted = value as? ConvertedJavaScriptValue {
44
+ return converted.takeValue()
45
45
  }
46
46
  return try dynamicInnerType.castToJS(value, appContext: appContext)
47
47
  }
@@ -19,7 +19,7 @@ internal struct DynamicViewType: AnyDynamicType {
19
19
  /**
20
20
  Resolves the React component instance to the native view via its tag.
21
21
  */
22
- func cast(jsValue: JavaScriptValue, appContext: AppContext) throws -> Any {
22
+ func cast(jsValue: borrowing JavaScriptValue, appContext: AppContext) throws -> Any {
23
23
  guard let viewTag = findViewTag(jsValue) else {
24
24
  throw InvalidViewTagException()
25
25
  }
@@ -44,7 +44,7 @@ internal struct DynamicViewType: AnyDynamicType {
44
44
  }
45
45
  }
46
46
 
47
- private func findViewTag(_ value: JavaScriptValue) -> Int? {
47
+ private func findViewTag(_ value: borrowing JavaScriptValue) -> Int? {
48
48
  if value.isNumber() {
49
49
  return value.getInt()
50
50
  }
@@ -13,7 +13,7 @@ internal struct DynamicVoidType: AnyDynamicType {
13
13
  return type is Self
14
14
  }
15
15
 
16
- func cast(jsValue: JavaScriptValue, appContext: AppContext) throws -> Any {
16
+ func cast(jsValue: borrowing JavaScriptValue, appContext: AppContext) throws -> Any {
17
17
  return Optional<Any>.none as Any
18
18
  }
19
19
 
@@ -22,7 +22,7 @@ internal struct DynamicVoidType: AnyDynamicType {
22
22
  }
23
23
 
24
24
  func castToJS<ValueType>(_ value: ValueType, appContext: AppContext) throws -> JavaScriptValue {
25
- return .undefined
25
+ return .undefined()
26
26
  }
27
27
 
28
28
  var description: String {
@@ -28,7 +28,7 @@ public extension EventEmitter {
28
28
  Schedules an event with the given name to be emitted to the associated JavaScript object.
29
29
  */
30
30
  func emit(event: String) {
31
- emit(event: event, payload: JavaScriptValue.undefined)
31
+ emit(event: event, payload: JavaScriptValue.undefined())
32
32
  }
33
33
 
34
34
  /**
@@ -36,11 +36,13 @@ public extension EventEmitter {
36
36
  to the associated JavaScript object. This is the lowest-level emit overload - use it when the
37
37
  value is already a `JavaScriptValue` to skip the native-to-JS conversion step.
38
38
  */
39
- func emit(event: String, payload: JavaScriptValue) {
39
+ func emit(event: String, payload: consuming JavaScriptValue) {
40
40
  guard let appContext, let runtime = try? appContext.runtime else {
41
41
  log.warn("Trying to send event '\(event)' to \(type(of: self)), but the JS runtime has been lost")
42
42
  return
43
43
  }
44
+ // The payload is moved into a `JavaScriptRef` so the scheduled closure can capture it.
45
+ let payload = payload.ref()
44
46
  // The emitter is not necessarily `Sendable` - some modules hold non-sendable state — so we can't let
45
47
  // the compiler send `self` into the `@JavaScriptActor` region. Wrapping it in a weak, `@unchecked
46
48
  // Sendable` box is safe here because the scheduled closure only calls `withEventTarget`, which touches
@@ -52,6 +54,10 @@ public extension EventEmitter {
52
54
  guard let emitter = emitter.value else {
53
55
  return
54
56
  }
57
+ // The event is dispatched once, so the payload can be moved out of the ref.
58
+ guard let payload = payload.take() else {
59
+ return
60
+ }
55
61
  let dispatched = emitter.withEventTarget { target in
56
62
  dispatchEvent(event: event, payload: payload, to: target, in: runtime)
57
63
  return true
@@ -102,7 +108,7 @@ public extension EventEmitter {
102
108
  Must run on the JS thread; the public `emit` overloads schedule onto the runtime before calling in.
103
109
  */
104
110
  @JavaScriptActor
105
- internal func dispatchEvent(event: String, payload: JavaScriptValue, to target: borrowing JavaScriptObject, in runtime: JavaScriptRuntime) {
111
+ internal func dispatchEvent(event: String, payload: borrowing JavaScriptValue, to target: borrowing JavaScriptObject, in runtime: JavaScriptRuntime) {
106
112
  runtime.withUnsafePointee { runtimePtr in
107
113
  target.withUnsafePointee { objectPtr in
108
114
  payload.withUnsafePointee { payloadPtr in
@@ -1,7 +1,7 @@
1
1
  import ExpoModulesJSI
2
2
 
3
3
  /**
4
- An alias to `Result<JavaScriptRef<JavaScriptValue>, Exception>` which can be passed to the function callback.
4
+ An alias to `Result<JavaScriptValue, Exception>` which can be passed to the function callback.
5
5
  */
6
6
  public typealias FunctionCallResult = Result<JavaScriptValue, Exception>
7
7
 
@@ -86,7 +86,7 @@ public class AsyncFunctionDefinition<Args, FirstArgType, ReturnType>: AnyAsyncFu
86
86
  var takesOwner: Bool = false
87
87
 
88
88
  @JavaScriptActor
89
- func call(_ appContext: AppContext, this: JavaScriptValue, arguments: consuming JavaScriptValuesBuffer, callback: @Sendable @escaping (consuming FunctionCallResult) -> Void) {
89
+ func call(_ appContext: AppContext, this: borrowing JavaScriptValue, arguments: consuming JavaScriptValuesBuffer, callback: @Sendable @escaping (consuming FunctionCallResult) -> Void) {
90
90
  let promise = Promise(appContext: appContext) { value in
91
91
  callback(.success(value))
92
92
  } rejecter: { exception in
@@ -190,7 +190,7 @@ public class AsyncFunctionDefinition<Args, FirstArgType, ReturnType>: AnyAsyncFu
190
190
  let promiseValue = promise.asValue()
191
191
 
192
192
  self.call(appContext, this: this, arguments: arguments) { [promise] result in
193
- switch result {
193
+ switch consume result {
194
194
  case .success(let value):
195
195
  promise.resolve(value)
196
196
  case .failure(let error as any Error):
@@ -97,7 +97,7 @@ public class ConcurrentFunctionDefinition<Args, FirstArgType, ReturnType>: AnyCo
97
97
  do {
98
98
  // Promote the borrowed `this` only when the function converts it to its owner; the owning
99
99
  // value stays within the decode phase.
100
- let owner: JavaScriptValue = self.takesOwner ? this.copied(in: try appContext.runtime) : .undefined
100
+ let owner: JavaScriptValue = self.takesOwner ? this.copied(in: try appContext.runtime) : .undefined()
101
101
  argumentsTuple = try self.decodeArguments(appContext: appContext, this: owner, arguments: arguments)
102
102
  } catch let error as Exception {
103
103
  throw FunctionCallException(self.name).causedBy(error)
@@ -116,7 +116,7 @@ public class ConcurrentFunctionDefinition<Args, FirstArgType, ReturnType>: AnyCo
116
116
  @JavaScriptActor
117
117
  private func decodeArguments(
118
118
  appContext: AppContext,
119
- this: JavaScriptValue,
119
+ this: borrowing JavaScriptValue,
120
120
  arguments: borrowing JavaScriptValuesBuffer
121
121
  ) throws -> Args {
122
122
  try validateArgumentsNumber(function: self, received: arguments.count)
@@ -42,7 +42,7 @@ public struct OptimizedAsyncFunctionDefinition: AnyAsyncFunctionDefinition, @unc
42
42
 
43
43
  public var takesOwner: Bool = false
44
44
 
45
- public func call(by owner: AnyObject?, withArguments args: [Any], appContext: AppContext, callback: @escaping (FunctionCallResult) -> ()) {
45
+ public func call(by owner: AnyObject?, withArguments args: [Any], appContext: AppContext, callback: @escaping (consuming FunctionCallResult) -> ()) {
46
46
  callback(.failure(GenericException("OptimizedAsyncFunctionDefinition cannot be called from native code")))
47
47
  }
48
48
 
@@ -40,17 +40,17 @@ public struct OptimizedSyncFunctionDefinition: AnySyncFunctionDefinition, @unche
40
40
 
41
41
  public var takesOwner: Bool = false
42
42
 
43
- public func call(by owner: AnyObject?, withArguments args: [Any], appContext: AppContext, callback: @escaping (FunctionCallResult) -> ()) {
43
+ public func call(by owner: AnyObject?, withArguments args: [Any], appContext: AppContext, callback: @escaping (consuming FunctionCallResult) -> ()) {
44
44
  callback(.failure(GenericException("OptimizedSyncFunctionDefinition cannot be called from native code")))
45
45
  }
46
46
 
47
47
  // MARK: - AnySyncFunctionDefinition
48
48
 
49
- public func call(_ appContext: AppContext, in runtime: JavaScriptRuntime, this: JavaScriptValue, arguments: consuming JavaScriptValuesBuffer) throws(Exception) -> ExpoModulesJSI.JavaScriptValue {
49
+ public func call(_ appContext: AppContext, in runtime: JavaScriptRuntime, this: borrowing JavaScriptValue, arguments: consuming JavaScriptValuesBuffer) throws(Exception) -> ExpoModulesJSI.JavaScriptValue {
50
50
  throw GenericException("OptimizedSyncFunctionDefinition cannot be called directly")
51
51
  }
52
52
 
53
- public func runBody(_ appContext: AppContext, in runtime: JavaScriptRuntime, this: JavaScriptValue, arguments: consuming JavaScriptValuesBuffer) throws(Exception) -> Any {
53
+ public func runBody(_ appContext: AppContext, in runtime: JavaScriptRuntime, this: borrowing JavaScriptValue, arguments: consuming JavaScriptValuesBuffer) throws(Exception) -> Any {
54
54
  throw GenericException("OptimizedSyncFunctionDefinition cannot be called directly")
55
55
  }
56
56
 
@@ -12,7 +12,7 @@ internal protocol AnySyncFunctionDefinition: AnyFunctionDefinition, ~Copyable {
12
12
  */
13
13
  @discardableResult
14
14
  @JavaScriptActor
15
- func call(_ appContext: AppContext, in runtime: JavaScriptRuntime, this: JavaScriptValue, arguments: consuming JavaScriptValuesBuffer) throws(Exception) -> JavaScriptValue
15
+ func call(_ appContext: AppContext, in runtime: JavaScriptRuntime, this: borrowing JavaScriptValue, arguments: consuming JavaScriptValuesBuffer) throws(Exception) -> JavaScriptValue
16
16
 
17
17
  /**
18
18
  Runs the underlying body with the given arguments and returns the raw native result,
@@ -20,7 +20,7 @@ internal protocol AnySyncFunctionDefinition: AnyFunctionDefinition, ~Copyable {
20
20
  native result (e.g. a `SharedObject`) to pair with the JS `this` instance directly.
21
21
  */
22
22
  @JavaScriptActor
23
- func runBody(_ appContext: AppContext, in runtime: JavaScriptRuntime, this: JavaScriptValue, arguments: consuming JavaScriptValuesBuffer) throws(Exception) -> Any
23
+ func runBody(_ appContext: AppContext, in runtime: JavaScriptRuntime, this: borrowing JavaScriptValue, arguments: consuming JavaScriptValuesBuffer) throws(Exception) -> Any
24
24
 
25
25
  /**
26
26
  Builds the sync function in a specific runtime.
@@ -82,7 +82,7 @@ public class SyncFunctionDefinition<Args, FirstArgType, ReturnType>: AnySyncFunc
82
82
 
83
83
  @JavaScriptActor
84
84
  @discardableResult
85
- func runBody(_ appContext: AppContext, in runtime: JavaScriptRuntime, this: JavaScriptValue, arguments: consuming JavaScriptValuesBuffer) throws(Exception) -> Any {
85
+ func runBody(_ appContext: AppContext, in runtime: JavaScriptRuntime, this: borrowing JavaScriptValue, arguments: consuming JavaScriptValuesBuffer) throws(Exception) -> Any {
86
86
  do {
87
87
  try validateArgumentsNumber(function: self, received: arguments.count)
88
88
  let nativeArguments = try toNativeClosureArguments(converter: appContext.converter, fn: self, this: this, arguments: arguments)
@@ -98,7 +98,7 @@ public class SyncFunctionDefinition<Args, FirstArgType, ReturnType>: AnySyncFunc
98
98
  }
99
99
 
100
100
  @JavaScriptActor
101
- func call(_ appContext: AppContext, in runtime: JavaScriptRuntime, this: JavaScriptValue, arguments: consuming JavaScriptValuesBuffer) throws(Exception) -> JavaScriptValue {
101
+ func call(_ appContext: AppContext, in runtime: JavaScriptRuntime, this: borrowing JavaScriptValue, arguments: consuming JavaScriptValuesBuffer) throws(Exception) -> JavaScriptValue {
102
102
  let result = try runBody(appContext, in: runtime, this: this, arguments: arguments)
103
103
  do {
104
104
  return try appContext.converter.toJS(result, returnType, in: runtime)
@@ -140,7 +140,7 @@ public class SyncFunctionDefinition<Args, FirstArgType, ReturnType>: AnySyncFunc
140
140
  internal func toNativeClosureArguments<F: AnyFunctionDefinition>(
141
141
  converter: borrowing MainValueConverter,
142
142
  fn: borrowing F,
143
- this: JavaScriptValue,
143
+ this: borrowing JavaScriptValue,
144
144
  arguments: borrowing JavaScriptValuesBuffer
145
145
  ) throws -> [Any] {
146
146
  if !fn.takesOwner, fn.argumentsCount == 0 {
@@ -17,13 +17,15 @@ import ExpoModulesJSI
17
17
  internal final class JSValueDecoder: Decoder {
18
18
  private let appContext: AppContext
19
19
  private let runtime: JavaScriptRuntime
20
- private let value: JavaScriptValue
20
+ // Behind a ref, so a single value container can share it instead of cloning the value. Nothing
21
+ // takes the value out of the reference, so it is always there.
22
+ private let value: JavaScriptValue.Ref
21
23
 
22
24
  /**
23
25
  Initializes the decoder with the given JS value and app context.
24
26
  Throws if the runtime has been lost.
25
27
  */
26
- convenience init(value: JavaScriptValue, appContext: AppContext) throws {
28
+ convenience init(value: consuming JavaScriptValue, appContext: AppContext) throws {
27
29
  try self.init(value: value, appContext: appContext, runtime: appContext.runtime)
28
30
  }
29
31
 
@@ -32,17 +34,17 @@ internal final class JSValueDecoder: Decoder {
32
34
  Use this when the source JS value lives in a runtime other than the one currently
33
35
  held by the app context.
34
36
  */
35
- convenience init(value: JavaScriptValue, appContext: AppContext, runtime: JavaScriptRuntime) {
37
+ convenience init(value: consuming JavaScriptValue, appContext: AppContext, runtime: JavaScriptRuntime) {
36
38
  self.init(value: value, appContext: appContext, runtime: runtime, codingPath: [])
37
39
  }
38
40
 
39
41
  fileprivate init(
40
- value: JavaScriptValue,
42
+ value: consuming JavaScriptValue,
41
43
  appContext: AppContext,
42
44
  runtime: JavaScriptRuntime,
43
45
  codingPath: [any CodingKey]
44
46
  ) {
45
- self.value = value
47
+ self.value = value.ref()
46
48
  self.appContext = appContext
47
49
  self.runtime = runtime
48
50
  self.codingPath = codingPath
@@ -54,6 +56,15 @@ internal final class JSValueDecoder: Decoder {
54
56
  let userInfo: [CodingUserInfoKey: Any] = [:]
55
57
 
56
58
  func container<Key>(keyedBy type: Key.Type) throws -> KeyedDecodingContainer<Key> where Key: CodingKey {
59
+ return try value.withUnwrappedValue { (value: borrowing JavaScriptValue) in
60
+ try keyedContainer(of: value, keyedBy: type)
61
+ }!
62
+ }
63
+
64
+ private func keyedContainer<Key>(
65
+ of value: borrowing JavaScriptValue,
66
+ keyedBy type: Key.Type
67
+ ) throws -> KeyedDecodingContainer<Key> where Key: CodingKey {
57
68
  guard value.isObject() else {
58
69
  throw DecodingError.typeMismatch(
59
70
  [String: Any].self,
@@ -73,6 +84,12 @@ internal final class JSValueDecoder: Decoder {
73
84
  }
74
85
 
75
86
  func unkeyedContainer() throws -> any UnkeyedDecodingContainer {
87
+ return try value.withUnwrappedValue { (value: borrowing JavaScriptValue) in
88
+ try unkeyedContainer(of: value)
89
+ }!
90
+ }
91
+
92
+ private func unkeyedContainer(of value: borrowing JavaScriptValue) throws -> any UnkeyedDecodingContainer {
76
93
  guard value.isArray() else {
77
94
  throw DecodingError.typeMismatch(
78
95
  [Any].self,
@@ -109,7 +126,7 @@ internal final class JSValueDecoder: Decoder {
109
126
  */
110
127
  private func decodeUsingDynamicType<ValueType: Decodable>(
111
128
  _ type: ValueType.Type,
112
- from jsValue: JavaScriptValue,
129
+ from jsValue: borrowing JavaScriptValue,
113
130
  appContext: AppContext,
114
131
  runtime: JavaScriptRuntime,
115
132
  codingPath: [any CodingKey]
@@ -145,7 +162,7 @@ private func decodeUsingDynamicType<ValueType: Decodable>(
145
162
  }
146
163
 
147
164
  // Plain Decodable type the registry doesn't recognize — recurse via Decodable.
148
- let decoder = JSValueDecoder(value: jsValue, appContext: appContext, runtime: runtime, codingPath: codingPath)
165
+ let decoder = JSValueDecoder(value: jsValue.copy(), appContext: appContext, runtime: runtime, codingPath: codingPath)
149
166
  return try ValueType(from: decoder)
150
167
  }
151
168
 
@@ -157,10 +174,12 @@ private func decodeUsingDynamicType<ValueType: Decodable>(
157
174
  private struct JSValueDecodingContainer: SingleValueDecodingContainer {
158
175
  private let appContext: AppContext
159
176
  private let runtime: JavaScriptRuntime
160
- private let value: JavaScriptValue
177
+ // Behind a ref: `SingleValueDecodingContainer` is a copyable protocol, so the struct cannot store a
178
+ // non-copyable value directly.
179
+ private let value: JavaScriptValue.Ref
161
180
  let codingPath: [any CodingKey]
162
181
 
163
- init(value: JavaScriptValue, appContext: AppContext, runtime: JavaScriptRuntime, codingPath: [any CodingKey]) {
182
+ init(value: JavaScriptValue.Ref, appContext: AppContext, runtime: JavaScriptRuntime, codingPath: [any CodingKey]) {
164
183
  self.value = value
165
184
  self.appContext = appContext
166
185
  self.runtime = runtime
@@ -168,17 +187,13 @@ private struct JSValueDecodingContainer: SingleValueDecodingContainer {
168
187
  }
169
188
 
170
189
  func decodeNil() -> Bool {
171
- return value.isNull() || value.isUndefined()
190
+ return value.withUnwrappedValue { (value: borrowing JavaScriptValue) in value.isNull() || value.isUndefined() }!
172
191
  }
173
192
 
174
193
  func decode<ValueType: Decodable>(_ type: ValueType.Type) throws -> ValueType {
175
- return try decodeUsingDynamicType(
176
- type,
177
- from: value,
178
- appContext: appContext,
179
- runtime: runtime,
180
- codingPath: codingPath
181
- )
194
+ return try value.withUnwrappedValue { (value: borrowing JavaScriptValue) in
195
+ try decodeUsingDynamicType(type, from: value, appContext: appContext, runtime: runtime, codingPath: codingPath)
196
+ }!
182
197
  }
183
198
  }
184
199
 
@@ -23,7 +23,7 @@ internal final class JSValueEncoder: Encoder {
23
23
  The result of encoding to `JavaScriptValue`. Use this property after running `encode(to:)` on the encodable.
24
24
  */
25
25
  var value: JavaScriptValue {
26
- return valueHolder.value
26
+ return valueHolder.take()
27
27
  }
28
28
 
29
29
  /**
@@ -120,7 +120,7 @@ private func encodeUsingDynamicType<ValueType: Encodable>(
120
120
  valueHolder: holder
121
121
  )
122
122
  try value.encode(to: encoder)
123
- return holder.value
123
+ return holder.take()
124
124
  }
125
125
 
126
126
  // MARK: - Containers
@@ -129,7 +129,14 @@ private func encodeUsingDynamicType<ValueType: Encodable>(
129
129
  An object that holds a JS value, mutated by an encoding container as it makes progress.
130
130
  */
131
131
  private final class JSValueHolder {
132
- var value: JavaScriptValue = .undefined
132
+ // Optional so the value can be moved out: a non-copyable stored property of a class cannot be
133
+ // consumed, but `Optional.take()` can run on it.
134
+ var value: JavaScriptValue?
135
+
136
+ /// Moves the encoded value out, leaving the holder empty. An empty holder yields `undefined`.
137
+ func take() -> JavaScriptValue {
138
+ return value.take() ?? .undefined()
139
+ }
133
140
  }
134
141
 
135
142
  /**
@@ -149,7 +156,7 @@ private struct JSValueEncodingContainer: SingleValueEncodingContainer {
149
156
  }
150
157
 
151
158
  mutating func encodeNil() throws {
152
- valueHolder.value = .null
159
+ valueHolder.value = .null()
153
160
  }
154
161
 
155
162
  mutating func encode<ValueType: Encodable>(_ value: ValueType) throws {
@@ -190,7 +197,7 @@ private final class JSObjectEncodingContainer<Key: CodingKey>: KeyedEncodingCont
190
197
  }
191
198
 
192
199
  func encodeNil(forKey key: Key) throws {
193
- object.setProperty(key.stringValue, value: JavaScriptValue.null)
200
+ object.setProperty(key.stringValue, value: JavaScriptValue.null())
194
201
  }
195
202
 
196
203
  func encode<ValueType: Encodable>(_ value: ValueType, forKey key: Key) throws {
@@ -247,7 +254,7 @@ private final class JSObjectEncodingContainer<Key: CodingKey>: KeyedEncodingCont
247
254
  runtime: runtime,
248
255
  codingPath: codingPath + [key]
249
256
  )
250
- object.setProperty(key.stringValue, value: holder.value)
257
+ object.setProperty(key.stringValue, value: holder.take())
251
258
  return KeyedEncodingContainer(container)
252
259
  }
253
260
 
@@ -259,7 +266,7 @@ private final class JSObjectEncodingContainer<Key: CodingKey>: KeyedEncodingCont
259
266
  runtime: runtime,
260
267
  codingPath: codingPath + [key]
261
268
  )
262
- object.setProperty(key.stringValue, value: holder.value)
269
+ object.setProperty(key.stringValue, value: holder.take())
263
270
  return container
264
271
  }
265
272
 
@@ -299,7 +306,7 @@ private final class JSArrayEncodingContainer: UnkeyedEncodingContainer {
299
306
  }
300
307
 
301
308
  func encodeNil() throws {
302
- array[count] = JavaScriptValue.null
309
+ array[count] = JavaScriptValue.null()
303
310
  count += 1
304
311
  }
305
312
 
@@ -322,7 +329,7 @@ private final class JSArrayEncodingContainer: UnkeyedEncodingContainer {
322
329
  runtime: runtime,
323
330
  codingPath: codingPath + [AnyCodingKey(intValue: count)]
324
331
  )
325
- array[count] = holder.value
332
+ array[count] = holder.take()
326
333
  count += 1
327
334
  return KeyedEncodingContainer(container)
328
335
  }
@@ -335,7 +342,7 @@ private final class JSArrayEncodingContainer: UnkeyedEncodingContainer {
335
342
  runtime: runtime,
336
343
  codingPath: codingPath + [AnyCodingKey(intValue: count)]
337
344
  )
338
- array[count] = holder.value
345
+ array[count] = holder.take()
339
346
  count += 1
340
347
  return container
341
348
  }
@@ -14,7 +14,7 @@ public struct MainValueConverter: ~Copyable {
14
14
  It **must** be run on the thread used by the JavaScript runtime.
15
15
  */
16
16
  @JavaScriptActor
17
- public func toNative(_ value: JavaScriptValue, _ type: AnyDynamicType) throws -> Any {
17
+ public func toNative(_ value: borrowing JavaScriptValue, _ type: AnyDynamicType) throws -> Any {
18
18
  return try type.cast(jsValue: value, appContext: appContext)
19
19
  }
20
20
 
@@ -186,18 +186,18 @@ public final class ModuleHolder {
186
186
  let startListening = runtime.createFunction("__expo_onStartListeningToEvent") {
187
187
  [weak module = self.module] _, arguments in
188
188
  guard let module, arguments.count > 0 else {
189
- return .undefined
189
+ return .undefined()
190
190
  }
191
191
  module.didStartListening(event: try arguments[0].asString())
192
- return .undefined
192
+ return .undefined()
193
193
  }
194
194
  let stopListening = runtime.createFunction("__expo_onStopListeningToEvent") {
195
195
  [weak module = self.module] _, arguments in
196
196
  guard let module, arguments.count > 0 else {
197
- return .undefined
197
+ return .undefined()
198
198
  }
199
199
  module.didStopListening(event: try arguments[0].asString())
200
- return .undefined
200
+ return .undefined()
201
201
  }
202
202
  object.defineProperty("__expo_onStartListeningToEvent", value: startListening.asValue(), options: [])
203
203
  object.defineProperty("__expo_onStopListeningToEvent", value: stopListening.asValue(), options: [])
@@ -5,6 +5,7 @@ import Foundation
5
5
  import ExpoModulesJSI
6
6
 
7
7
  // The core module that describes the `global.expo` object.
8
+ @ExpoModule
8
9
  internal final class CoreModule: Module {
9
10
  internal func definition() -> ModuleDefinition {
10
11
  Constant("expoModulesCoreVersion") {
@@ -31,53 +32,6 @@ internal final class CoreModule: Module {
31
32
  false
32
33
  }
33
34
 
34
- Function("installOnUIRuntime") { (uiRuntimeHolder: JavaScriptValue) in
35
- guard let appContext else {
36
- throw Exceptions.AppContextLost()
37
- }
38
-
39
- // Check if was already installed
40
- if appContext._uiRuntime != nil {
41
- return
42
- }
43
-
44
- let runtime = try appContext.runtime
45
- guard uiRuntimeHolder.isObject() else {
46
- throw WorkletUIRuntimeException()
47
- }
48
-
49
- guard let factory = AppContext.uiRuntimeFactory else {
50
- throw WorkletUIRuntimeException()
51
- }
52
-
53
- // Use a Sendable box to safely capture error across isolation boundaries
54
- final class ErrorHolder: @unchecked Sendable {
55
- var error: (any Error)?
56
- }
57
- let errorHolder = ErrorHolder()
58
-
59
- let block = {
60
- do {
61
- let uiRuntime = try factory(appContext, uiRuntimeHolder, runtime)
62
- appContext._uiRuntime = uiRuntime
63
- } catch {
64
- errorHolder.error = error
65
- }
66
- }
67
-
68
- if Thread.isMainThread {
69
- block()
70
- } else {
71
- DispatchQueue.main.sync {
72
- block()
73
- }
74
- }
75
-
76
- if let error = errorHolder.error {
77
- throw error
78
- }
79
- }
80
-
81
35
  // Expose some common classes and maybe even the `modules` host object in the future.
82
36
  Function("uuidv4") { () -> String in
83
37
  return UUID().uuidString.lowercased()
@@ -130,6 +84,42 @@ internal final class CoreModule: Module {
130
84
 
131
85
  return viewName
132
86
  }
87
+
88
+ /**
89
+ Installs the worklet UI runtime from the holder object that the worklets JS code passes to
90
+ `global.expo.installOnUIRuntime`.
91
+ */
92
+ @JS
93
+ nonisolated internal func installOnUIRuntime(_ uiRuntimeHolder: borrowing JavaScriptValue) throws {
94
+ guard let appContext else {
95
+ throw Exceptions.AppContextLost()
96
+ }
97
+
98
+ // Check if was already installed
99
+ if appContext._uiRuntime != nil {
100
+ return
101
+ }
102
+
103
+ let runtime = try appContext.runtime
104
+ guard uiRuntimeHolder.isObject() else {
105
+ throw WorkletUIRuntimeException()
106
+ }
107
+
108
+ guard let factory = AppContext.uiRuntimeFactory else {
109
+ throw WorkletUIRuntimeException()
110
+ }
111
+
112
+ // The runtime is created and stored on the main thread: setting `_uiRuntime` prepares it there.
113
+ // `DispatchQueue.main.sync` doesn't escape its closure and rethrows, so the closure can borrow the
114
+ // holder and the error reaches the caller directly.
115
+ if Thread.isMainThread {
116
+ appContext._uiRuntime = try factory(appContext, uiRuntimeHolder, runtime)
117
+ } else {
118
+ try DispatchQueue.main.sync {
119
+ appContext._uiRuntime = try factory(appContext, uiRuntimeHolder, runtime)
120
+ }
121
+ }
122
+ }
133
123
  }
134
124
 
135
125
  internal final class WorkletUIRuntimeException: Exception, @unchecked Sendable {
@@ -137,4 +127,3 @@ internal final class WorkletUIRuntimeException: Exception, @unchecked Sendable {
137
127
  "Cannot find UI worklet runtime"
138
128
  }
139
129
  }
140
-
@@ -76,7 +76,9 @@ public final class ConstantDefinition<ReturnType: AnyArgument>: AnyDefinition, A
76
76
  */
77
77
  @JavaScriptActor
78
78
  internal func buildGetter(appContext: AppContext) throws -> JavaScriptFunction {
79
- var savedValue: JavaScriptValue?
79
+ // The converted value is cached behind a `JavaScriptRef` so the closure can keep it across calls:
80
+ // the ref gives the value reference semantics, which an escaping closure capture needs.
81
+ let savedValue = JavaScriptValue.Ref()
80
82
  return try appContext.runtime.createFunction(name) { [weak appContext, weak self, name] _, _ in
81
83
  guard let appContext else {
82
84
  throw Exceptions.AppContextLost()
@@ -87,11 +89,11 @@ public final class ConstantDefinition<ReturnType: AnyArgument>: AnyDefinition, A
87
89
  guard let getter = self.getter else {
88
90
  throw NativeConstantWithoutGetterException(name)
89
91
  }
90
- if let value = savedValue {
92
+ if let value = savedValue.withUnwrappedValue({ (value: borrowing JavaScriptValue) in value.copy() }) {
91
93
  return value
92
94
  }
93
95
  let value = try appContext.converter.toJS(try getter(), ~ReturnType.self)
94
- savedValue = value
96
+ savedValue.reset(value.copy())
95
97
  return value
96
98
  }
97
99
  }
@@ -146,7 +146,7 @@ public final class PropertyDefinition<OwnerType>: AnyDefinition, AnyPropertyDefi
146
146
  throw NativePropertyUnavailableException(name)
147
147
  }
148
148
  guard let getter = self.getter else {
149
- return .undefined
149
+ return .undefined()
150
150
  }
151
151
  return try getter.call(appContext, in: runtime, this: this, arguments: arguments)
152
152
  }
@@ -165,7 +165,7 @@ public final class PropertyDefinition<OwnerType>: AnyDefinition, AnyPropertyDefi
165
165
  throw NativePropertyUnavailableException(name)
166
166
  }
167
167
  guard let setter = self.setter else {
168
- return .undefined
168
+ return .undefined()
169
169
  }
170
170
  return try setter.call(appContext, in: runtime, this: this, arguments: arguments)
171
171
  }
@@ -3,7 +3,7 @@
3
3
  import ExpoModulesJSI
4
4
 
5
5
  public struct Promise: AnyArgument, Sendable {
6
- public typealias ResolveClosure = @Sendable (_ value: JavaScriptValue) -> Void
6
+ public typealias ResolveClosure = @Sendable (_ value: consuming JavaScriptValue) -> Void
7
7
  public typealias RejectClosure = @Sendable (Exception) -> Void
8
8
 
9
9
  internal weak var appContext: AppContext?