expo-modules-core 58.0.24 → 58.0.27
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.
- package/CHANGELOG.md +34 -0
- package/android/build.gradle +3 -3
- package/android/prebuilt/metadata.json +2 -2
- package/android/prebuilt/native-libs.tar.xz +0 -0
- package/android/src/main/cpp/ArrayBuffer.cpp +7 -1
- package/android/src/main/cpp/ArrayBuffer.h +2 -0
- package/android/src/main/java/expo/modules/interfaces/taskManager/TaskServiceProviderHelper.kt +1 -1
- package/android/src/main/java/expo/modules/kotlin/sharedobjects/SharedObject.kt +5 -0
- package/android/src/main/java/expo/modules/kotlin/sharedobjects/SharedObjectRegistry.kt +14 -0
- package/common/cpp/fabric/ExpoViewShadowNode.h +34 -5
- package/expo-module-gradle-plugin/build.gradle.kts +1 -1
- package/ios/Benchmarks/EventEmitterBenchmarks.swift +73 -0
- package/ios/Benchmarks/FunctionBenchmarks.swift +28 -0
- package/ios/Benchmarks/SharedObjectBenchmarks.swift +97 -3
- package/ios/Core/AppContext.swift +32 -5
- package/ios/Core/ArrayBuffers/ArrayBuffer.swift +44 -6
- package/ios/Core/Classes/ClassDefinition.swift +4 -3
- package/ios/Core/DynamicTypes/DynamicSharedObjectType.swift +1 -1
- package/ios/Core/Events/EventEmitter.swift +68 -26
- package/ios/Core/ExpoModulesMacros.swift +13 -14
- package/ios/Core/ModuleHolder.swift +7 -9
- package/ios/Core/Modules/ModuleDefinition.swift +2 -3
- package/ios/Core/Protocols/AnyModule.swift +12 -4
- package/ios/Core/SharedObjects/SharedObject.swift +30 -0
- package/ios/Core/SharedObjects/SharedObjectNativeState.swift +51 -9
- package/ios/Core/SharedObjects/SharedObjectRegistry.swift +19 -8
- package/ios/JS/JavaScriptBindings.swift +53 -0
- package/ios/Utilities/Mutex.swift +15 -3
- package/package.json +3 -3
- package/prebuilds/output/debug/xcframeworks/ExpoModulesCore.tar.gz +0 -0
- package/prebuilds/output/debug/xcframeworks/ExpoModulesWorklets.tar.gz +0 -0
- package/prebuilds/output/headers/xcframeworks/ExpoModulesCoreHeaders.tar.gz +0 -0
- package/prebuilds/output/release/xcframeworks/ExpoModulesCore.tar.gz +0 -0
- package/prebuilds/output/release/xcframeworks/ExpoModulesWorklets.tar.gz +0 -0
|
@@ -67,12 +67,12 @@ public final class ClassDefinition: ObjectDefinition {
|
|
|
67
67
|
// the prototype chain `new` set up). Prefer the macro-synthesized `_constructSharedObject`,
|
|
68
68
|
// falling back to the DSL `Constructor` body.
|
|
69
69
|
if let sharedObject = try sharedObjectType?._constructSharedObject(this: this, arguments: arguments, in: appContext.runtime) {
|
|
70
|
-
appContext.sharedObjectRegistry.add(native: sharedObject, javaScript: this.getObject())
|
|
70
|
+
appContext.sharedObjectRegistry.add(native: sharedObject, javaScript: this.getObject(), inheritsBaseClass: true)
|
|
71
71
|
} else if let constructor {
|
|
72
72
|
let result = try constructor.runBody(appContext, in: appContext.runtime, this: this, arguments: arguments)
|
|
73
73
|
|
|
74
74
|
if let sharedObject = result as? SharedObject {
|
|
75
|
-
appContext.sharedObjectRegistry.add(native: sharedObject, javaScript: this.getObject())
|
|
75
|
+
appContext.sharedObjectRegistry.add(native: sharedObject, javaScript: this.getObject(), inheritsBaseClass: true)
|
|
76
76
|
}
|
|
77
77
|
} else if sharedObjectType != nil {
|
|
78
78
|
// Nothing built a native instance, so the object would reach JavaScript with no backing
|
|
@@ -89,9 +89,10 @@ public final class ClassDefinition: ObjectDefinition {
|
|
|
89
89
|
|
|
90
90
|
let prototype = klass.getProperty("prototype").getObject()
|
|
91
91
|
|
|
92
|
-
// Bind the macro's `@JS` members onto the prototype.
|
|
92
|
+
// Bind the macro's `@JS` members: instance members onto the prototype, static members onto the constructor.
|
|
93
93
|
if let sharedObjectType {
|
|
94
94
|
try sharedObjectType._decorateSharedObject(prototype: prototype, in: appContext.runtime)
|
|
95
|
+
try sharedObjectType._decorateSharedObject(constructor: klass, in: appContext.runtime)
|
|
95
96
|
}
|
|
96
97
|
|
|
97
98
|
// Register the prototype of the JS class for its associated native type.
|
|
@@ -78,7 +78,7 @@ internal struct DynamicSharedObjectType: AnyDynamicType {
|
|
|
78
78
|
}
|
|
79
79
|
|
|
80
80
|
// Add newly created objects to the registry.
|
|
81
|
-
appContext.sharedObjectRegistry.add(native: sharedObject, javaScript: jsObject)
|
|
81
|
+
appContext.sharedObjectRegistry.add(native: sharedObject, javaScript: jsObject, inheritsBaseClass: true)
|
|
82
82
|
|
|
83
83
|
return jsObject.asValue()
|
|
84
84
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// Copyright 2024-present 650 Industries. All rights reserved.
|
|
2
2
|
|
|
3
|
-
import ExpoModulesJSI
|
|
3
|
+
@_spi(Internal) import ExpoModulesJSI
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
A protocol for native objects that have an associated JavaScript object capable of receiving events.
|
|
@@ -27,6 +27,7 @@ public extension EventEmitter {
|
|
|
27
27
|
/**
|
|
28
28
|
Schedules an event with the given name to be emitted to the associated JavaScript object.
|
|
29
29
|
*/
|
|
30
|
+
@inlinable
|
|
30
31
|
func emit(event: String) {
|
|
31
32
|
emit(event: event, payload: JavaScriptUndefined())
|
|
32
33
|
}
|
|
@@ -71,9 +72,12 @@ public extension EventEmitter {
|
|
|
71
72
|
/**
|
|
72
73
|
Schedules an event with the given name and payload to be emitted to the associated JavaScript object.
|
|
73
74
|
*/
|
|
75
|
+
@inlinable
|
|
74
76
|
func emit<P: JavaScriptEncodable>(event: String, payload: sending P) {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
+
// Inlinable, so `P` and the scheduled closure specialize in the caller's module. Unspecialized, the
|
|
78
|
+
// payload is boxed and `P.encode` goes through the witness table on every event. The non-generic
|
|
79
|
+
// parts are calls into this module.
|
|
80
|
+
guard let runtime = eventRuntime(forEvent: event) else {
|
|
77
81
|
return
|
|
78
82
|
}
|
|
79
83
|
// See the note in `emit(event:payload:)` above - the emitter is captured in a weak, `@unchecked
|
|
@@ -89,16 +93,10 @@ public extension EventEmitter {
|
|
|
89
93
|
do {
|
|
90
94
|
jsPayload = try P.encode(payload, in: runtime)
|
|
91
95
|
} catch {
|
|
92
|
-
|
|
96
|
+
warnPayloadNotEncoded(event: event, payloadType: P.self, error: error)
|
|
93
97
|
return
|
|
94
98
|
}
|
|
95
|
-
|
|
96
|
-
dispatchEvent(event: event, payload: jsPayload, to: target, in: runtime)
|
|
97
|
-
return true
|
|
98
|
-
}
|
|
99
|
-
if dispatched == nil {
|
|
100
|
-
log.warn("Trying to send event '\(event)' to JS, but the JS object is no longer associated with the native instance")
|
|
101
|
-
}
|
|
99
|
+
dispatchEvent(event: event, payload: jsPayload, from: emitter, in: runtime)
|
|
102
100
|
}
|
|
103
101
|
}
|
|
104
102
|
|
|
@@ -140,9 +138,57 @@ public extension EventEmitter {
|
|
|
140
138
|
}
|
|
141
139
|
|
|
142
140
|
/// A weak reference to an event emitter that the scheduled `emit` closures capture by value, so it is stored
|
|
143
|
-
/// in the closure context without an allocation of its own.
|
|
144
|
-
|
|
141
|
+
/// in the closure context without an allocation of its own. Frozen, because with library evolution the
|
|
142
|
+
/// inlinable initializer can't assign the stored property of a struct whose layout isn't fixed.
|
|
143
|
+
@frozen
|
|
144
|
+
@usableFromInline
|
|
145
|
+
internal struct WeakEventEmitterReference: @unchecked Sendable {
|
|
146
|
+
@usableFromInline
|
|
145
147
|
weak var value: (any EventEmitter)?
|
|
148
|
+
|
|
149
|
+
@inlinable
|
|
150
|
+
init(value: any EventEmitter) {
|
|
151
|
+
self.value = value
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
extension EventEmitter {
|
|
156
|
+
/// The runtime to schedule an event on, or `nil` after logging a warning when it has been lost.
|
|
157
|
+
@usableFromInline
|
|
158
|
+
internal func eventRuntime(forEvent event: String) -> JavaScriptRuntime? {
|
|
159
|
+
guard let appContext, let runtime = try? appContext.runtime else {
|
|
160
|
+
log.warn("Trying to send event '\(event)' to \(type(of: self)), but the JS runtime has been lost")
|
|
161
|
+
return nil
|
|
162
|
+
}
|
|
163
|
+
return runtime
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/// Logs that the payload of an event couldn't be converted, so the event is not emitted.
|
|
168
|
+
@usableFromInline
|
|
169
|
+
internal func warnPayloadNotEncoded(event: String, payloadType: Any.Type, error: any Error) {
|
|
170
|
+
log.warn(
|
|
171
|
+
"Failed to convert payload for event '\(event)' on \(payloadType); the event will not be emitted: \(error)"
|
|
172
|
+
)
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/// Sends a pre-converted event payload to the JavaScript object of the given emitter. Logs a warning
|
|
176
|
+
/// when the emitter is no longer associated with a JavaScript object.
|
|
177
|
+
@JavaScriptActor
|
|
178
|
+
@usableFromInline
|
|
179
|
+
internal func dispatchEvent(
|
|
180
|
+
event: String,
|
|
181
|
+
payload: borrowing JavaScriptValue,
|
|
182
|
+
from emitter: any EventEmitter,
|
|
183
|
+
in runtime: JavaScriptRuntime
|
|
184
|
+
) {
|
|
185
|
+
let dispatched = emitter.withEventTarget { target in
|
|
186
|
+
dispatchEvent(event: event, payload: payload, to: target, in: runtime)
|
|
187
|
+
return true
|
|
188
|
+
}
|
|
189
|
+
if dispatched == nil {
|
|
190
|
+
log.warn("Trying to send event '\(event)' to JS, but the JS object is no longer associated with the native instance")
|
|
191
|
+
}
|
|
146
192
|
}
|
|
147
193
|
|
|
148
194
|
/**
|
|
@@ -151,19 +197,15 @@ private struct WeakEventEmitterReference: @unchecked Sendable {
|
|
|
151
197
|
*/
|
|
152
198
|
@JavaScriptActor
|
|
153
199
|
internal func dispatchEvent(event: String, payload: borrowing JavaScriptValue, to target: borrowing JavaScriptObject, in runtime: JavaScriptRuntime) {
|
|
154
|
-
runtime.
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
argumentCount: 1
|
|
164
|
-
)
|
|
165
|
-
}
|
|
166
|
-
}
|
|
200
|
+
runtime.withUnsafePointees(object: target, value: payload) { runtimePtr, objectPtr, payloadPtr in
|
|
201
|
+
event.withCString { eventName in
|
|
202
|
+
emitJSEvent(
|
|
203
|
+
utf8Name: eventName,
|
|
204
|
+
runtimePointer: runtimePtr,
|
|
205
|
+
objectPointer: objectPtr,
|
|
206
|
+
argumentsPointer: payloadPtr,
|
|
207
|
+
argumentCount: 1
|
|
208
|
+
)
|
|
167
209
|
}
|
|
168
210
|
}
|
|
169
211
|
}
|
|
@@ -107,11 +107,9 @@ public macro Event(_ name: String? = nil, sync: Bool = false) =
|
|
|
107
107
|
#externalMacro(module: "ExpoModulesMacros", type: "EventMacro")
|
|
108
108
|
|
|
109
109
|
/// Member macro applied to a `Module` subclass. Scans the class body for declarations
|
|
110
|
-
/// marked with `@JS` and
|
|
111
|
-
/// `
|
|
112
|
-
///
|
|
113
|
-
/// additionally bound directly into the module's JS object by a synthesized
|
|
114
|
-
/// `_decorateModule(object:in:)`.
|
|
110
|
+
/// marked with `@JS` and binds them directly into the module's JS object by a synthesized
|
|
111
|
+
/// `_decorateModule(object:in:)`. The `@SharedObject` classes in the `classes:` argument are
|
|
112
|
+
/// emitted as a `_classTypes` static, and core adds their JavaScript classes to the module's JS object.
|
|
115
113
|
///
|
|
116
114
|
/// The module's JavaScript name is synthesized into a `_jsName` static and read by core, so there
|
|
117
115
|
/// is no `Name(…)` DSL entry. It defaults to the class name; pass `@ExpoModule("CustomName")` to
|
|
@@ -127,7 +125,7 @@ public macro Event(_ name: String? = nil, sync: Bool = false) =
|
|
|
127
125
|
@attached(
|
|
128
126
|
member,
|
|
129
127
|
names:
|
|
130
|
-
named(_jsName), named(
|
|
128
|
+
named(_jsName), named(_classTypes), named(appContext), named(init),
|
|
131
129
|
named(_decorateModule))
|
|
132
130
|
@attached(memberAttribute)
|
|
133
131
|
@attached(extension, conformances: AnyModule)
|
|
@@ -136,15 +134,16 @@ public macro ExpoModule(_ name: String? = nil, classes: [Any.Type] = []) =
|
|
|
136
134
|
|
|
137
135
|
/// Member macro applied to a `SharedObject` subclass. Scans the class body for declarations
|
|
138
136
|
/// marked with `@JS` (including a single `@JS init(...)` for the JS constructor) and
|
|
139
|
-
///
|
|
140
|
-
/// The companion `@ExpoModule(classes: [Foo.self])` wires the
|
|
141
|
-
/// exposed surface.
|
|
137
|
+
/// binds them through overrides of the `SharedObject` hooks below. The class's JavaScript name
|
|
138
|
+
/// is emitted as a `_jsName` override. The companion `@ExpoModule(classes: [Foo.self])` wires the
|
|
139
|
+
/// class into the module's exposed surface.
|
|
142
140
|
///
|
|
143
141
|
/// `@JS` methods and properties are bound directly onto the class prototype by an override of
|
|
144
|
-
/// `_decorateSharedObject(prototype:in:)`, and
|
|
145
|
-
/// of `
|
|
146
|
-
///
|
|
147
|
-
///
|
|
142
|
+
/// `_decorateSharedObject(prototype:in:)`, `static` and `class` ones onto the class itself by an
|
|
143
|
+
/// override of `_decorateSharedObject(constructor:in:)`, and a single `@JS init(...)` becomes an
|
|
144
|
+
/// override of `_constructSharedObject(this:arguments:in:)` that builds the native instance from the
|
|
145
|
+
/// JS arguments. Core calls them when building the class. The class must be a `SharedObject`
|
|
146
|
+
/// subclass, direct or not, or the `_jsName` override doesn't compile.
|
|
148
147
|
///
|
|
149
148
|
/// Usage:
|
|
150
149
|
///
|
|
@@ -162,7 +161,7 @@ public macro ExpoModule(_ name: String? = nil, classes: [Any.Type] = []) =
|
|
|
162
161
|
@attached(
|
|
163
162
|
member,
|
|
164
163
|
names:
|
|
165
|
-
named(
|
|
164
|
+
named(_jsName), named(_decorateSharedObject), named(_constructSharedObject))
|
|
166
165
|
@attached(memberAttribute)
|
|
167
166
|
public macro SharedObject(_ name: String? = nil) =
|
|
168
167
|
#externalMacro(module: "ExpoModulesMacros", type: "SharedObjectMacro")
|
|
@@ -57,15 +57,8 @@ public final class ModuleHolder {
|
|
|
57
57
|
post(event: .moduleCreate)
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
-
///
|
|
61
|
-
/// `@ExpoModule`
|
|
62
|
-
/// `_synthesizedDefinition()` method returning an `[AnyDefinition]` array of the
|
|
63
|
-
/// `Function` / `Property` / `Constructor` entries it generated from `@JS`
|
|
64
|
-
/// members. Those entries are prepended to the user's definitions and the
|
|
65
|
-
/// whole list is fed back through `ModuleDefinition.init` so the merged
|
|
66
|
-
/// result is rebucketed (into `functions`, `properties`, etc.) just like a
|
|
67
|
-
/// hand-written definition. Modules that don't use the macro fall through
|
|
68
|
-
/// the empty-synthesized fast path and return the user's definition unchanged.
|
|
60
|
+
/// Merges the entries from `_synthesizedDefinition()` into the user-authored definition. Only module
|
|
61
|
+
/// binaries built with earlier `@ExpoModule` macros return any, so other modules take the fast path.
|
|
69
62
|
private static func buildDefinition(for module: AnyModule) -> ModuleDefinition {
|
|
70
63
|
let userDefinition = module.definition()
|
|
71
64
|
let synthesized = module._synthesizedDefinition()
|
|
@@ -137,6 +130,11 @@ public final class ModuleHolder {
|
|
|
137
130
|
log.info("Creating JS object for module '\(name)'")
|
|
138
131
|
let object = try definition.build(appContext: appContext)
|
|
139
132
|
|
|
133
|
+
// Install the `@SharedObject` classes the `@ExpoModule` macro registers by type.
|
|
134
|
+
for classType in type(of: module)._classTypes {
|
|
135
|
+
object.setProperty(classType._jsName, value: try JavaScriptBindings.buildSharedObjectClass(classType, appContext: appContext))
|
|
136
|
+
}
|
|
137
|
+
|
|
140
138
|
// Install the `@JS` members the `@ExpoModule` macro binds directly into the JS object
|
|
141
139
|
// (the direct-JSI path). A no-op for modules that don't use the macro.
|
|
142
140
|
try module._decorateModule(object: object, in: appContext.runtime)
|
|
@@ -29,9 +29,8 @@ public final class ModuleDefinition: ObjectDefinition {
|
|
|
29
29
|
|
|
30
30
|
let eventObservers: [AnyEventObservingDefinition]
|
|
31
31
|
|
|
32
|
-
/// The raw list of definitions used to construct this module. Retained so
|
|
33
|
-
///
|
|
34
|
-
/// `@ExpoModule` macro without losing the user-authored ones.
|
|
32
|
+
/// The raw list of definitions used to construct this module. Retained so that `ModuleHolder` can
|
|
33
|
+
/// merge in the entries from `_synthesizedDefinition()` without losing the user-authored ones.
|
|
35
34
|
let rawDefinitions: [AnyDefinition]
|
|
36
35
|
|
|
37
36
|
/**
|
|
@@ -21,10 +21,14 @@ public protocol AnyModule: AnyObject, AnyArgument {
|
|
|
21
21
|
/// Modules that don't use the macro fall back to the default, which is the class name.
|
|
22
22
|
static var _jsName: String { get }
|
|
23
23
|
|
|
24
|
-
///
|
|
25
|
-
///
|
|
26
|
-
///
|
|
27
|
-
|
|
24
|
+
/// The `@SharedObject` classes the module exposes in its JavaScript object, emitted as a static
|
|
25
|
+
/// constant by the `@ExpoModule` macro from its `classes:` argument. Framework-internal (leading
|
|
26
|
+
/// underscore). Modules that don't use the macro fall back to the default empty list.
|
|
27
|
+
static var _classTypes: [SharedObject.Type] { get }
|
|
28
|
+
|
|
29
|
+
/// Returns definitions that core merges into the module's definition. The `@ExpoModule` macro no
|
|
30
|
+
/// longer emits it. It stays so that module binaries built with earlier macros keep loading and
|
|
31
|
+
/// keep their classes. Framework-internal (leading underscore).
|
|
28
32
|
func _synthesizedDefinition() -> [AnyDefinition]
|
|
29
33
|
|
|
30
34
|
/// Binds the module's `@JS` members directly into its JavaScript object, generated by the
|
|
@@ -62,6 +66,10 @@ extension AnyModule {
|
|
|
62
66
|
@ModuleDefinitionBuilder
|
|
63
67
|
public func definition() -> ModuleDefinition {}
|
|
64
68
|
|
|
69
|
+
public static var _classTypes: [SharedObject.Type] {
|
|
70
|
+
return []
|
|
71
|
+
}
|
|
72
|
+
|
|
65
73
|
public func _synthesizedDefinition() -> [AnyDefinition] {
|
|
66
74
|
return []
|
|
67
75
|
}
|
|
@@ -39,9 +39,28 @@ open class SharedObject: AnySharedObject {
|
|
|
39
39
|
/// The value of `nativeRefType` on the JavaScript object, or `nil` when the object does not conform to
|
|
40
40
|
/// `AnySharedRef`.
|
|
41
41
|
internal var sharedRefType: String? {
|
|
42
|
+
guard Self.conformsToAnySharedRef(Swift.type(of: self)) else {
|
|
43
|
+
return nil
|
|
44
|
+
}
|
|
42
45
|
return (self as? AnySharedRef)?.nativeRefType
|
|
43
46
|
}
|
|
44
47
|
|
|
48
|
+
/// Whether each `SharedObject` class conforms to `AnySharedRef`. A failing `as? AnySharedRef` walks the
|
|
49
|
+
/// whole class hierarchy on every call, so the result is looked up once per class.
|
|
50
|
+
private static let anySharedRefConformances = Mutex([ObjectIdentifier: Bool]())
|
|
51
|
+
|
|
52
|
+
private static func conformsToAnySharedRef(_ type: SharedObject.Type) -> Bool {
|
|
53
|
+
let key = ObjectIdentifier(type)
|
|
54
|
+
return anySharedRefConformances.withLock { conformances in
|
|
55
|
+
if let conforms = conformances[key] {
|
|
56
|
+
return conforms
|
|
57
|
+
}
|
|
58
|
+
let conforms = type is AnySharedRef.Type
|
|
59
|
+
conformances[key] = conforms
|
|
60
|
+
return conforms
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
45
64
|
/**
|
|
46
65
|
A function that will be called before the object is removed from the registry.
|
|
47
66
|
*/
|
|
@@ -92,11 +111,22 @@ open class SharedObject: AnySharedObject {
|
|
|
92
111
|
|
|
93
112
|
// MARK: - Macro-synthesized JSI hooks
|
|
94
113
|
|
|
114
|
+
/// The name of the class in JavaScript. Overridden by the `@SharedObject` macro with the class name
|
|
115
|
+
/// or the `@SharedObject("CustomName")` argument. A module registers the class through `_classTypes`.
|
|
116
|
+
open class var _jsName: String {
|
|
117
|
+
return unqualifiedTypeName(self)
|
|
118
|
+
}
|
|
119
|
+
|
|
95
120
|
/// Binds the class's `@JS` members onto its JavaScript prototype. Overridden by the `@SharedObject`
|
|
96
121
|
/// macro; a `class func` (not a protocol requirement) so dispatch resolves per subclass.
|
|
97
122
|
@JavaScriptActor
|
|
98
123
|
open class func _decorateSharedObject(prototype: borrowing JavaScriptObject, in runtime: JavaScriptRuntime) throws {}
|
|
99
124
|
|
|
125
|
+
/// Binds the class's `static` and `class` `@JS` members onto its JavaScript constructor. Overridden by
|
|
126
|
+
/// the `@SharedObject` macro.
|
|
127
|
+
@JavaScriptActor
|
|
128
|
+
open class func _decorateSharedObject(constructor: borrowing JavaScriptObject, in runtime: JavaScriptRuntime) throws {}
|
|
129
|
+
|
|
100
130
|
/// Builds a native instance from the JS constructor arguments, or `nil` to use the DSL `Constructor`
|
|
101
131
|
/// path. Overridden by the `@SharedObject` macro from the class's `@JS init`.
|
|
102
132
|
@JavaScriptActor
|
|
@@ -17,9 +17,14 @@ internal final class SharedObjectNativeState: JavaScriptNativeState {
|
|
|
17
17
|
/// (for example a frozen one, whose native state can't be reset), so lookups check this flag.
|
|
18
18
|
internal private(set) var isReleased = false
|
|
19
19
|
|
|
20
|
-
/// The JS object paired with the native object in
|
|
21
|
-
///
|
|
22
|
-
|
|
20
|
+
/// The JS object paired with the native object in the first runtime it was paired in, held weakly.
|
|
21
|
+
/// Most native objects are only exposed to one runtime, so that pairing skips the dictionary below.
|
|
22
|
+
private var firstRuntimeId: JavaScriptRuntime.ID?
|
|
23
|
+
private var firstPairedObject: JavaScriptWeakObject?
|
|
24
|
+
|
|
25
|
+
/// The JS object paired with the native object in each other runtime, keyed by runtime identity and
|
|
26
|
+
/// held weakly. One native object can be exposed to several runtimes (e.g. the main and UI runtimes,
|
|
27
|
+
/// or worklet contexts), each with its own JS counterpart.
|
|
23
28
|
private var pairedObjects: [JavaScriptRuntime.ID: JavaScriptWeakObject.Ref] = [:]
|
|
24
29
|
|
|
25
30
|
internal init(native: SharedObject, factory: @escaping JavaScriptNativeState.Factory) {
|
|
@@ -35,27 +40,64 @@ internal final class SharedObjectNativeState: JavaScriptNativeState {
|
|
|
35
40
|
/// for having attached this native state to `jsObject` (`jsObject.setNativeState(self)`), which reuses
|
|
36
41
|
/// the shared C++ pointee across runtimes.
|
|
37
42
|
internal func setJavaScriptObject(_ jsObject: borrowing JavaScriptObject, in runtime: JavaScriptRuntime) {
|
|
43
|
+
let runtimeId = runtime.id
|
|
44
|
+
|
|
45
|
+
// Take the first slot when it's free, already belongs to this runtime, or its JS object is gone.
|
|
46
|
+
if firstRuntimeId == nil || firstRuntimeId == runtimeId || !isFirstPairedObjectAlive() {
|
|
47
|
+
firstRuntimeId = runtimeId
|
|
48
|
+
firstPairedObject = jsObject.createWeak()
|
|
49
|
+
if !pairedObjects.isEmpty {
|
|
50
|
+
pairedObjects[runtimeId] = nil
|
|
51
|
+
}
|
|
52
|
+
return
|
|
53
|
+
}
|
|
38
54
|
// Drop entries whose runtime or JS object has gone away before inserting, so the map doesn't
|
|
39
55
|
// accumulate dead keys for runtimes that paired once and were torn down.
|
|
40
56
|
pruneExpired()
|
|
41
|
-
pairedObjects[
|
|
57
|
+
pairedObjects[runtimeId] = jsObject.createWeak().ref()
|
|
42
58
|
}
|
|
43
59
|
|
|
44
60
|
/// Returns the JS object paired with the native object in `runtime`, or `nil` if there is no live
|
|
45
61
|
/// pairing there (none was created, or the JS object has been garbage-collected).
|
|
46
62
|
internal func javaScriptObject(in runtime: JavaScriptRuntime) -> JavaScriptObject? {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
63
|
+
let runtimeId = runtime.id
|
|
64
|
+
|
|
65
|
+
// Unwrap the ref first so the optional doesn't nest with `withValue`'s, which would form a
|
|
66
|
+
// `JavaScriptObject??` that the non-`Copyable` element can't be wrapped in.
|
|
67
|
+
if runtimeId == firstRuntimeId {
|
|
68
|
+
switch firstPairedObject {
|
|
69
|
+
case .some(let weakObject):
|
|
70
|
+
return weakObject.lock()
|
|
71
|
+
case .none:
|
|
72
|
+
return nil
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
guard !pairedObjects.isEmpty, let ref = pairedObjects[runtimeId] else {
|
|
50
76
|
return nil
|
|
51
77
|
}
|
|
52
78
|
return ref.withValue { $0?.lock() }
|
|
53
79
|
}
|
|
54
80
|
|
|
55
81
|
private func pruneExpired() {
|
|
82
|
+
guard !pairedObjects.isEmpty else {
|
|
83
|
+
return
|
|
84
|
+
}
|
|
56
85
|
pairedObjects = pairedObjects.filter { _, ref in
|
|
57
|
-
|
|
58
|
-
return ref.withValue { $0?.lock() != nil } ?? false
|
|
86
|
+
return Self.isAlive(ref)
|
|
59
87
|
}
|
|
60
88
|
}
|
|
89
|
+
|
|
90
|
+
private func isFirstPairedObjectAlive() -> Bool {
|
|
91
|
+
switch firstPairedObject {
|
|
92
|
+
case .some(let weakObject):
|
|
93
|
+
return weakObject.lock() != nil
|
|
94
|
+
case .none:
|
|
95
|
+
return false
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/// Whether the weak ref still resolves to a JS object; an empty or missing ref doesn't.
|
|
100
|
+
private static func isAlive(_ ref: JavaScriptWeakObject.Ref?) -> Bool {
|
|
101
|
+
return ref?.withValue { $0?.lock() != nil } ?? false
|
|
102
|
+
}
|
|
61
103
|
}
|
|
@@ -92,14 +92,23 @@ public final class SharedObjectRegistry: Sendable {
|
|
|
92
92
|
|
|
93
93
|
/**
|
|
94
94
|
Adds a pair of native and JS shared object to the registry. Assigns a new shared object ID to these objects.
|
|
95
|
+
Pass `inheritsBaseClass: true` when `jsObject` inherits from `expo.SharedObject.prototype`, whose getter
|
|
96
|
+
already reads the ID from the native state. A plain object gets the ID as its own property instead.
|
|
95
97
|
*/
|
|
96
98
|
@discardableResult
|
|
97
|
-
internal func add(
|
|
99
|
+
internal func add(
|
|
100
|
+
native nativeObject: SharedObject,
|
|
101
|
+
javaScript jsObject: borrowing JavaScriptObject,
|
|
102
|
+
inheritsBaseClass: Bool = false
|
|
103
|
+
) -> SharedObjectId {
|
|
98
104
|
// Reuse the native state, and with it the ID, only while a live JS object in this runtime holds it.
|
|
99
105
|
// That JS object keeps the C++ native state alive until `setNativeState` attaches it to `jsObject`.
|
|
100
106
|
// Otherwise the C++ native state can be in destruction on another thread, and its release must not
|
|
101
107
|
// apply to `jsObject`, so the native object gets a new native state and ID.
|
|
102
|
-
|
|
108
|
+
// Loaded once: each read of the weak reference to the app context costs an atomic operation.
|
|
109
|
+
let appContext = self.appContext
|
|
110
|
+
let runtime = try? appContext?.runtime
|
|
111
|
+
let pairedObject = pairedObjectHoldingNativeState(of: nativeObject, in: runtime)
|
|
103
112
|
let existingNativeState = pairedObject != nil ? nativeObject.nativeState : nil
|
|
104
113
|
let id = existingNativeState != nil ? nativeObject.sharedObjectId : pullNextId()
|
|
105
114
|
|
|
@@ -107,10 +116,12 @@ public final class SharedObjectRegistry: Sendable {
|
|
|
107
116
|
nativeObject.appContext = appContext
|
|
108
117
|
|
|
109
118
|
// This property should be deprecated, but it's still used when passing as a view prop.
|
|
110
|
-
//
|
|
111
|
-
//
|
|
119
|
+
// The base class prototype defines it as a getter, so only a plain object needs its own property.
|
|
120
|
+
// Defining it calls `Object.defineProperty` in JS, a large part of the cost of pairing.
|
|
112
121
|
// No option set literals here: they build the set through a generic initializer on every call.
|
|
113
|
-
|
|
122
|
+
if !inheritsBaseClass {
|
|
123
|
+
jsObject.defineProperty(sharedObjectIdPropertyName, value: id, options: .writable)
|
|
124
|
+
}
|
|
114
125
|
|
|
115
126
|
if let sharedRefType = nativeObject.sharedRefType {
|
|
116
127
|
jsObject.defineProperty("nativeRefType", value: sharedRefType, options: JavaScriptObject.PropertyOptions())
|
|
@@ -158,7 +169,7 @@ public final class SharedObjectRegistry: Sendable {
|
|
|
158
169
|
// The registry is scoped to its app context, so the JS object being paired here lives in that
|
|
159
170
|
// context's runtime. Record the pairing under that runtime so the native object can recover this
|
|
160
171
|
// JS counterpart via `native.nativeState?.javaScriptObject(in:)`.
|
|
161
|
-
if let runtime
|
|
172
|
+
if let runtime {
|
|
162
173
|
nativeState.setJavaScriptObject(jsObject, in: runtime)
|
|
163
174
|
}
|
|
164
175
|
|
|
@@ -251,8 +262,8 @@ public final class SharedObjectRegistry: Sendable {
|
|
|
251
262
|
|
|
252
263
|
/// Returns the JS object paired with the native object in this registry's runtime, if it is alive and
|
|
253
264
|
/// still holds the native object's native state.
|
|
254
|
-
private func pairedObjectHoldingNativeState(of nativeObject: SharedObject) -> JavaScriptObject? {
|
|
255
|
-
guard let runtime
|
|
265
|
+
private func pairedObjectHoldingNativeState(of nativeObject: SharedObject, in runtime: JavaScriptRuntime?) -> JavaScriptObject? {
|
|
266
|
+
guard let runtime,
|
|
256
267
|
let nativeState = unreleasedNativeState(of: nativeObject),
|
|
257
268
|
let pairedObject = nativeState.javaScriptObject(in: runtime),
|
|
258
269
|
pairedObject.getNativeState(as: SharedObjectNativeState.self) === nativeState
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// Copyright 2026-present 650 Industries. All rights reserved.
|
|
2
|
+
|
|
3
|
+
import ExpoModulesJSI
|
|
4
|
+
|
|
5
|
+
/// Binds native types into the JavaScript runtime: creates their JavaScript classes and objects,
|
|
6
|
+
/// installs them and registers them in the app context.
|
|
7
|
+
internal enum JavaScriptBindings {
|
|
8
|
+
// MARK: - Shared objects
|
|
9
|
+
|
|
10
|
+
/// Builds the JavaScript class of a `@SharedObject` class that a module registers in `_classTypes`,
|
|
11
|
+
/// and registers its prototype for the class in `ClassRegistry`. All members come from the macro
|
|
12
|
+
/// hooks, so no `ClassDefinition` is needed.
|
|
13
|
+
@JavaScriptActor
|
|
14
|
+
static func buildSharedObjectClass(_ type: SharedObject.Type, appContext: AppContext) throws -> JavaScriptObject {
|
|
15
|
+
let name = type._jsName
|
|
16
|
+
let constructor: JavaScriptRuntime.SyncFunctionClosure = { [weak appContext] this, arguments in
|
|
17
|
+
guard let appContext else {
|
|
18
|
+
throw Exceptions.AppContextLost()
|
|
19
|
+
}
|
|
20
|
+
// Pair the native instance with `this` (returning a different object would drop the prototype
|
|
21
|
+
// chain `new` set up). Without a `@JS init`, only the native side can create an instance.
|
|
22
|
+
guard let sharedObject = try type._constructSharedObject(this: this, arguments: arguments, in: appContext.runtime) else {
|
|
23
|
+
throw Exceptions.ConstructorUnavailable(name)
|
|
24
|
+
}
|
|
25
|
+
appContext.sharedObjectRegistry.add(native: sharedObject, javaScript: this.getObject(), inheritsBaseClass: true)
|
|
26
|
+
return this
|
|
27
|
+
}
|
|
28
|
+
let runtime = try appContext.runtime
|
|
29
|
+
let createClass = type is AnySharedRef.Type ? runtime.createSharedRefClass : runtime.createSharedObjectClass
|
|
30
|
+
let klass = try createClass(name, constructor).asObject()
|
|
31
|
+
let prototype = klass.getProperty("prototype").getObject()
|
|
32
|
+
|
|
33
|
+
try type._decorateSharedObject(prototype: prototype, in: runtime)
|
|
34
|
+
try type._decorateSharedObject(constructor: klass, in: runtime)
|
|
35
|
+
|
|
36
|
+
appContext.classRegistry.register(nativeClassId: ObjectIdentifier(type), prototype: prototype)
|
|
37
|
+
return klass
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/// Builds a prototype for a `@SharedObject` class in an alternate runtime (e.g. the worklet runtime),
|
|
41
|
+
/// inheriting from the given base prototype. The counterpart of `ClassDefinition.buildPrototype` for
|
|
42
|
+
/// the classes that a module registers in `_classTypes`.
|
|
43
|
+
@JavaScriptActor
|
|
44
|
+
static func buildSharedObjectPrototype(
|
|
45
|
+
_ type: SharedObject.Type,
|
|
46
|
+
in runtime: JavaScriptRuntime,
|
|
47
|
+
basePrototype: consuming JavaScriptObject
|
|
48
|
+
) throws -> JavaScriptObject {
|
|
49
|
+
let prototype = runtime.createObject(prototype: basePrototype)
|
|
50
|
+
try type._decorateSharedObject(prototype: prototype, in: runtime)
|
|
51
|
+
return prototype
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -8,16 +8,28 @@ import Foundation
|
|
|
8
8
|
@available(tvOS, deprecated: 18.0, message: "Use Mutex from the Synchronization framework")
|
|
9
9
|
public final class Mutex<Value>: @unchecked Sendable {
|
|
10
10
|
private var _value: Value
|
|
11
|
-
|
|
11
|
+
|
|
12
|
+
// An `os_unfair_lock` must not move in memory, so it lives in its own allocation. Under contention it
|
|
13
|
+
// costs much less than `NSLock`, which waits in the kernel.
|
|
14
|
+
private let _lock: os_unfair_lock_t = {
|
|
15
|
+
let lock = os_unfair_lock_t.allocate(capacity: 1)
|
|
16
|
+
lock.initialize(to: os_unfair_lock())
|
|
17
|
+
return lock
|
|
18
|
+
}()
|
|
12
19
|
|
|
13
20
|
public init(_ initialValue: Value) {
|
|
14
21
|
_value = initialValue
|
|
15
22
|
}
|
|
16
23
|
|
|
24
|
+
deinit {
|
|
25
|
+
_lock.deinitialize(count: 1)
|
|
26
|
+
_lock.deallocate()
|
|
27
|
+
}
|
|
28
|
+
|
|
17
29
|
@discardableResult
|
|
18
30
|
public func withLock<T>(_ body: (inout Value) throws -> T) rethrows -> T {
|
|
19
|
-
_lock
|
|
20
|
-
defer { _lock
|
|
31
|
+
os_unfair_lock_lock(_lock)
|
|
32
|
+
defer { os_unfair_lock_unlock(_lock) }
|
|
21
33
|
return try body(&_value)
|
|
22
34
|
}
|
|
23
35
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "expo-modules-core",
|
|
3
|
-
"version": "58.0.
|
|
3
|
+
"version": "58.0.27",
|
|
4
4
|
"description": "The core of Expo Modules architecture",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"expo",
|
|
@@ -66,8 +66,8 @@
|
|
|
66
66
|
"./package.json": "./package.json"
|
|
67
67
|
},
|
|
68
68
|
"dependencies": {
|
|
69
|
-
"expo-modules-jsi": "~58.0.
|
|
70
|
-
"expo-modules-macros": "0.
|
|
69
|
+
"expo-modules-jsi": "~58.0.20",
|
|
70
|
+
"expo-modules-macros": "0.24.0",
|
|
71
71
|
"invariant": "^2.2.4"
|
|
72
72
|
},
|
|
73
73
|
"devDependencies": {
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|