expo-modules-core 58.0.14 → 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 (65) hide show
  1. package/CHANGELOG.md +37 -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/proguard-rules.pro +34 -0
  6. package/android/src/main/cpp/fabric/AndroidExpoViewComponentDescriptor.cpp +0 -18
  7. package/android/src/main/java/expo/modules/kotlin/ExpoBridgeModule.kt +20 -4
  8. package/android/src/main/java/expo/modules/kotlin/defaultmodules/NativeModulesProxyModule.kt +7 -3
  9. package/android/src/main/java/expo/modules/kotlin/views/ShadowNodeProxy.kt +21 -3
  10. package/ios/Core/AppContext.swift +15 -12
  11. package/ios/Core/ArrayBuffers/ArrayBuffer.swift +4 -1
  12. package/ios/Core/Classes/ClassDefinition.swift +6 -1
  13. package/ios/Core/Classes/ClassRegistry.swift +5 -3
  14. package/ios/Core/Conversions.swift +43 -5
  15. package/ios/Core/DynamicTypes/AnyDynamicType.swift +1 -1
  16. package/ios/Core/DynamicTypes/DynamicArrayBufferType.swift +1 -1
  17. package/ios/Core/DynamicTypes/DynamicArrayType.swift +2 -2
  18. package/ios/Core/DynamicTypes/DynamicBoolType.swift +1 -1
  19. package/ios/Core/DynamicTypes/DynamicCodableType.swift +5 -5
  20. package/ios/Core/DynamicTypes/DynamicConvertibleType.swift +1 -7
  21. package/ios/Core/DynamicTypes/DynamicDataType.swift +1 -1
  22. package/ios/Core/DynamicTypes/DynamicDictionaryType.swift +2 -2
  23. package/ios/Core/DynamicTypes/DynamicEitherType.swift +1 -1
  24. package/ios/Core/DynamicTypes/DynamicEnumType.swift +1 -1
  25. package/ios/Core/DynamicTypes/DynamicJavaScriptType.swift +12 -6
  26. package/ios/Core/DynamicTypes/DynamicNumberType.swift +1 -1
  27. package/ios/Core/DynamicTypes/DynamicOptionalType.swift +4 -4
  28. package/ios/Core/DynamicTypes/DynamicRawType.swift +3 -3
  29. package/ios/Core/DynamicTypes/DynamicSharedObjectType.swift +2 -2
  30. package/ios/Core/DynamicTypes/DynamicStringType.swift +1 -1
  31. package/ios/Core/DynamicTypes/DynamicSwiftUIViewType.swift +2 -2
  32. package/ios/Core/DynamicTypes/DynamicTypedArrayType.swift +1 -1
  33. package/ios/Core/DynamicTypes/DynamicValueOrUndefinedType.swift +5 -5
  34. package/ios/Core/DynamicTypes/DynamicViewType.swift +2 -2
  35. package/ios/Core/DynamicTypes/DynamicVoidType.swift +2 -2
  36. package/ios/Core/Events/EventEmitter.swift +9 -3
  37. package/ios/Core/Exceptions/CommonExceptions.swift +9 -0
  38. package/ios/Core/Exceptions/Exception.swift +13 -0
  39. package/ios/Core/Functions/AnyFunctionDefinition.swift +1 -1
  40. package/ios/Core/Functions/AsyncFunctionDefinition.swift +2 -2
  41. package/ios/Core/Functions/ConcurrentFunctionDefinition.swift +2 -2
  42. package/ios/Core/Functions/OptimizedAsyncFunctionDefinition.swift +1 -1
  43. package/ios/Core/Functions/OptimizedSyncFunctionDefinition.swift +3 -3
  44. package/ios/Core/Functions/SyncFunctionDefinition.swift +5 -5
  45. package/ios/Core/JSValueDecoder.swift +32 -17
  46. package/ios/Core/JSValueEncoder.swift +17 -10
  47. package/ios/Core/MainValueConverter.swift +1 -1
  48. package/ios/Core/ModuleHolder.swift +4 -4
  49. package/ios/Core/Modules/CoreModule.swift +37 -48
  50. package/ios/Core/Objects/ConstantDefinition.swift +5 -3
  51. package/ios/Core/Objects/PropertyDefinition.swift +2 -2
  52. package/ios/Core/Promise.swift +1 -1
  53. package/ios/Core/Records/AnyField.swift +1 -1
  54. package/ios/Core/Records/Field.swift +3 -3
  55. package/ios/Core/Records/Record.swift +1 -4
  56. package/ios/Core/SharedObjects/SharedObject.swift +1 -1
  57. package/ios/Core/SharedObjects/SharedObjectNativeState.swift +1 -1
  58. package/ios/JS/ExpoRuntimeInstaller.swift +4 -5
  59. package/ios/Worklets/Core/DynamicSerializableType.swift +1 -1
  60. package/ios/Worklets/Core/DynamicWorkletType.swift +1 -1
  61. package/package.json +4 -4
  62. package/prebuilds/output/debug/xcframeworks/ExpoModulesCore.tar.gz +0 -0
  63. package/prebuilds/output/debug/xcframeworks/ExpoModulesWorklets.tar.gz +0 -0
  64. package/prebuilds/output/release/xcframeworks/ExpoModulesCore.tar.gz +0 -0
  65. package/prebuilds/output/release/xcframeworks/ExpoModulesWorklets.tar.gz +0 -0
@@ -23,7 +23,7 @@ internal struct DynamicDictionaryType: AnyDynamicType {
23
23
  return false
24
24
  }
25
25
 
26
- func cast(jsValue: JavaScriptValue, appContext: AppContext) throws -> Any {
26
+ func cast(jsValue: borrowing JavaScriptValue, appContext: AppContext) throws -> Any {
27
27
  if let jsObject = try? jsValue.asObject() {
28
28
  var result: [AnyHashable: Any] = [:]
29
29
  for key in jsObject.getPropertyNames() {
@@ -58,7 +58,7 @@ internal struct DynamicDictionaryType: AnyDynamicType {
58
58
  /**
59
59
  Type-aware conversion for dictionaries that were already normalized by `convertResult`.
60
60
  Values must use `castToJS` here to avoid re-entering `convertResult` for values that
61
- are already in their post-conversion shape, such as `JavaScriptValue.undefined`.
61
+ are already in their post-conversion shape, such as `JavaScriptValue.undefined()`.
62
62
  */
63
63
  func castToJS<ValueType>(_ value: ValueType, appContext: AppContext) throws -> JavaScriptValue {
64
64
  return try castToJS(value, appContext: appContext, in: try appContext.runtime)
@@ -15,7 +15,7 @@ internal struct DynamicEitherType<EitherType: AnyEither>: AnyDynamicType {
15
15
  return type is Self
16
16
  }
17
17
 
18
- func cast(jsValue: JavaScriptValue, appContext: AppContext) throws -> Any {
18
+ func cast(jsValue: borrowing JavaScriptValue, appContext: AppContext) throws -> Any {
19
19
  let types = eitherType.dynamicTypes()
20
20
 
21
21
  for type in types {
@@ -19,7 +19,7 @@ internal struct DynamicEnumType: AnyDynamicType {
19
19
  return false
20
20
  }
21
21
 
22
- func cast(jsValue: JavaScriptValue, appContext: AppContext) throws -> Any {
22
+ func cast(jsValue: borrowing JavaScriptValue, appContext: AppContext) throws -> Any {
23
23
  let rawValue = try innerType.getRawValueDynamicType().cast(jsValue: jsValue, appContext: appContext)
24
24
  return try innerType.create(fromRawValue: rawValue)
25
25
  }
@@ -16,18 +16,24 @@ internal struct DynamicJavaScriptType: AnyDynamicType {
16
16
  return type is Self
17
17
  }
18
18
 
19
- func cast(jsValue: JavaScriptValue, appContext: AppContext) throws -> Any {
20
- return jsValue
19
+ // `JavaScriptValue` is non-copyable, so it can neither be boxed in `Any` nor appear in a definition
20
+ // closure's argument tuple or result. The compiler rejects such a definition, so these paths are
21
+ // unreachable from the DSL; they throw for completeness.
22
+ func cast(jsValue: borrowing JavaScriptValue, appContext: AppContext) throws -> Any {
23
+ throw NonCopyableValueException()
21
24
  }
22
25
 
23
26
  func castToJS<ValueType>(_ value: ValueType, appContext: AppContext) throws -> JavaScriptValue {
24
- if let value = value as? JavaScriptValue {
25
- return value
26
- }
27
- throw Conversions.ConversionToJSFailedException((kind: .undefined, nativeType: ValueType.self))
27
+ throw NonCopyableValueException()
28
28
  }
29
29
 
30
30
  var description: String {
31
31
  return "JavaScriptValue"
32
32
  }
33
33
  }
34
+
35
+ internal final class NonCopyableValueException: Exception, @unchecked Sendable {
36
+ override var reason: String {
37
+ return "JavaScriptValue is non-copyable and cannot be passed through the module definition as a native value"
38
+ }
39
+ }
@@ -13,7 +13,7 @@ internal struct DynamicNumberType<NumberType>: 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
  if jsValue.isNumber() {
18
18
  // Fast paths for common types avoid expensive `as? any BinaryFloatingPoint` conformance lookups.
19
19
  let double = jsValue.getDouble()
@@ -23,7 +23,7 @@ internal struct DynamicOptionalType: AnyDynamicType {
23
23
  return false
24
24
  }
25
25
 
26
- func cast(jsValue: JavaScriptValue, appContext: AppContext) throws -> Any {
26
+ func cast(jsValue: borrowing JavaScriptValue, appContext: AppContext) throws -> Any {
27
27
  if jsValue.isUndefined() || jsValue.isNull() {
28
28
  return Optional<Any>.none as Any
29
29
  }
@@ -39,21 +39,21 @@ internal struct DynamicOptionalType: AnyDynamicType {
39
39
 
40
40
  func castToJS<ValueType>(_ value: ValueType, appContext: AppContext) throws -> JavaScriptValue {
41
41
  if Optional.isNil(value) || value is NSNull {
42
- return .null
42
+ return .null()
43
43
  }
44
44
  return try wrappedType.castToJS(value, appContext: appContext)
45
45
  }
46
46
 
47
47
  func convertToJS<ValueType>(_ value: ValueType, appContext: AppContext) throws -> JavaScriptValue {
48
48
  if Optional.isNil(value) || value is NSNull {
49
- return .null
49
+ return .null()
50
50
  }
51
51
  return try wrappedType.convertToJS(value, appContext: appContext)
52
52
  }
53
53
 
54
54
  func convertToJS<ValueType>(_ value: ValueType, appContext: AppContext, in runtime: JavaScriptRuntime) throws -> JavaScriptValue {
55
55
  if Optional.isNil(value) || value is NSNull {
56
- return .null
56
+ return .null()
57
57
  }
58
58
  return try wrappedType.convertToJS(value, appContext: appContext, in: runtime)
59
59
  }
@@ -17,7 +17,7 @@ internal struct DynamicRawType<InnerType>: AnyDynamicType {
17
17
  return type is Self
18
18
  }
19
19
 
20
- func cast(jsValue: JavaScriptValue, appContext: AppContext) throws -> Any {
20
+ func cast(jsValue: borrowing JavaScriptValue, appContext: AppContext) throws -> Any {
21
21
  return try cast(jsValue.getAny(), appContext: appContext)
22
22
  }
23
23
 
@@ -43,7 +43,7 @@ internal struct DynamicRawType<InnerType>: AnyDynamicType {
43
43
 
44
44
  func castToJS<ValueType>(_ value: ValueType, appContext: AppContext, in runtime: JavaScriptRuntime) throws -> JavaScriptValue {
45
45
  if Optional.isNil(value) {
46
- return .null
46
+ return .null()
47
47
  }
48
48
  // When `InnerType` is `Any` (e.g. `[String: Any]` value slot), the runtime type of the
49
49
  // value is more specific than the static type, so we dispatch on it to reach proper
@@ -60,7 +60,7 @@ internal struct DynamicRawType<InnerType>: AnyDynamicType {
60
60
  // We use `DynamicRawType` for this only temporarily.
61
61
  if let objectBuilder = result as? JavaScriptObjectBuilder {
62
62
  return try JavaScriptActor.assumeIsolated {
63
- return try objectBuilder.build(appContext: appContext).asValue()
63
+ return ConvertedJavaScriptValue(try objectBuilder.build(appContext: appContext).asValue())
64
64
  }
65
65
  }
66
66
  return result
@@ -44,7 +44,7 @@ internal struct DynamicSharedObjectType: AnyDynamicType {
44
44
  throw SharedObject.NotFoundException()
45
45
  }
46
46
 
47
- func cast(jsValue: JavaScriptValue, appContext: AppContext) throws -> Any {
47
+ func cast(jsValue: borrowing JavaScriptValue, appContext: AppContext) throws -> Any {
48
48
  if jsValue.kind == .number {
49
49
  let sharedObjectId = jsValue.getInt() as SharedObjectId
50
50
 
@@ -74,7 +74,7 @@ internal struct DynamicSharedObjectType: AnyDynamicType {
74
74
  guard let jsObject = (try? appContext.newObject(nativeClassId: typeIdentifier)) ?? (try? newBaseSharedObject(appContext, nativeType: innerType)) else {
75
75
  // Throwing is not possible here due to swift-objC interop.
76
76
  log.warn("Unable to create a JS object for \(description)")
77
- return JavaScriptValue.undefined
77
+ return JavaScriptValue.undefined()
78
78
  }
79
79
 
80
80
  // Add newly created objects to the registry.
@@ -20,7 +20,7 @@ internal struct DynamicStringType: AnyDynamicType {
20
20
  throw Conversions.CastingException<String>(value)
21
21
  }
22
22
 
23
- func cast(jsValue: JavaScriptValue, appContext: AppContext) throws -> Any {
23
+ func cast(jsValue: borrowing JavaScriptValue, appContext: AppContext) throws -> Any {
24
24
  if jsValue.kind == .string {
25
25
  return jsValue.getString()
26
26
  }
@@ -19,7 +19,7 @@ internal struct DynamicSwiftUIViewType<ViewType: ExpoSwiftUIView>: AnyDynamicTyp
19
19
  /**
20
20
  Resolves the React component instance to the native SwiftUI 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
  }
@@ -69,7 +69,7 @@ internal struct DynamicSwiftUIViewType<ViewType: ExpoSwiftUIView>: AnyDynamicTyp
69
69
  }
70
70
  }
71
71
 
72
- private func findViewTag(_ value: JavaScriptValue) -> Int? {
72
+ private func findViewTag(_ value: borrowing JavaScriptValue) -> Int? {
73
73
  if value.isNumber() {
74
74
  return value.getInt()
75
75
  }
@@ -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
@@ -99,6 +99,15 @@ public struct Exceptions {
99
99
  }
100
100
  }
101
101
 
102
+ /// An exception thrown when JavaScript calls `new` on a class whose native instance only the
103
+ /// native side can build. The class exposes no `@JS init` and no DSL `Constructor`, so there is
104
+ /// nothing to construct it with.
105
+ public final class ConstructorUnavailable: GenericException<String> {
106
+ override public var reason: String {
107
+ "'\(param)' cannot be constructed from JavaScript because its native instance is created by the module that owns it. Get an instance from that module's API instead of calling `new \(param)()`."
108
+ }
109
+ }
110
+
102
111
  /**
103
112
  An exception to throw when there is no module implementing the `EXPermissionsInterface` interface.
104
113
  - Note: This should never happen since the module is a part of `expo-modules-core`, but for compatibility reasons
@@ -63,6 +63,19 @@ open class Exception: CodedError, ChainableException, CustomStringConvertible, C
63
63
  let debugDescription = "\(name): \(reason) (at \(origin.file):\(origin.line))"
64
64
  return concatDescription(debugDescription, withCause: cause, debug: true)
65
65
  }
66
+
67
+ // MARK: - JavaScriptThrowable
68
+
69
+ /**
70
+ The message of the JavaScript error this exception is converted to. Defaults to `description`
71
+ (the reason and its cause chain), matching Android and what JavaScript received before the
72
+ JSI rewrite, instead of the protocol's default `debugDescription`, which also leaks the type
73
+ name and the native `file:line`. It is declared on the class, rather than inherited from the
74
+ protocol extension, so that subclasses can override it.
75
+ */
76
+ open var message: String {
77
+ description
78
+ }
66
79
  }
67
80
 
68
81
  /**
@@ -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