expo-modules-core 58.0.24 → 58.0.26
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 +19 -0
- package/android/build.gradle +2 -2
- package/android/prebuilt/metadata.json +1 -1
- package/android/prebuilt/native-libs.tar.xz +0 -0
- 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/ios/Benchmarks/FunctionBenchmarks.swift +28 -0
- package/ios/Benchmarks/SharedObjectBenchmarks.swift +97 -3
- 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/SharedObjects/SharedObject.swift +24 -0
- package/ios/Core/SharedObjects/SharedObjectNativeState.swift +51 -9
- package/ios/Core/SharedObjects/SharedObjectRegistry.swift +19 -8
- package/ios/Utilities/Mutex.swift +15 -3
- package/package.json +2 -2
- 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
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,24 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 58.0.26
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [iOS] Reduce the overhead of creating shared objects in JavaScript and of returning them from native functions. ([#52096](https://github.com/expo/expo/pull/52096) by [@tsapeta](https://github.com/tsapeta))
|
|
8
|
+
- Updated dependencies. ([#52095](https://github.com/expo/expo/pull/52095))
|
|
9
|
+
- expo-modules-jsi@58.0.19
|
|
10
|
+
|
|
11
|
+
## 58.0.25
|
|
12
|
+
|
|
13
|
+
### Patch Changes
|
|
14
|
+
|
|
15
|
+
- [iOS] Support `static` and `class` `@JS` members on `@SharedObject` classes. They are bound onto the JavaScript class itself, for example `Cache.open(name)`. ([#51966](https://github.com/expo/expo/pull/51966) by [@tsapeta](https://github.com/tsapeta))
|
|
16
|
+
- [Android] Fix "Cannot use shared object that was already released" errors when a view with a shared object prop, such as the `@expo/ui` `TextInput`, is remounted right after its first render. ([#51934](https://github.com/expo/expo/pull/51934) by [@expo-tuft](https://github.com/apps/expo-tuft))
|
|
17
|
+
- [iOS] Fix apps running out of memory when native functions return many new `ArrayBuffer` values to JavaScript, because the garbage collector did not count their memory. ([#52124](https://github.com/expo/expo/pull/52124) by [@tsapeta](https://github.com/tsapeta))
|
|
18
|
+
- [iOS] Fix `ArrayBuffer.wrap(dataWithoutCopy:)` keeping the bytes of released buffers alive until the current autorelease pool drains, which could make apps run out of memory when JavaScript calls a native function in a loop. ([#52123](https://github.com/expo/expo/pull/52123) by [@tsapeta](https://github.com/tsapeta))
|
|
19
|
+
- Updated dependencies. ([#52125](https://github.com/expo/expo/pull/52125), [#52031](https://github.com/expo/expo/pull/52031), [#52133](https://github.com/expo/expo/pull/52133), [#52122](https://github.com/expo/expo/pull/52122), [#52055](https://github.com/expo/expo/pull/52055))
|
|
20
|
+
- expo-modules-jsi@58.0.18
|
|
21
|
+
|
|
3
22
|
## 58.0.24
|
|
4
23
|
|
|
5
24
|
### Patch Changes
|
package/android/build.gradle
CHANGED
|
@@ -46,7 +46,7 @@ if (shouldIncludeCompose) {
|
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
group = 'host.exp.exponent'
|
|
49
|
-
version = '58.0.
|
|
49
|
+
version = '58.0.26'
|
|
50
50
|
|
|
51
51
|
def isExpoModulesCoreTests = {
|
|
52
52
|
Gradle gradle = getGradle()
|
|
@@ -175,7 +175,7 @@ android {
|
|
|
175
175
|
defaultConfig {
|
|
176
176
|
consumerProguardFiles 'proguard-rules.pro'
|
|
177
177
|
versionCode 1
|
|
178
|
-
versionName "58.0.
|
|
178
|
+
versionName "58.0.26"
|
|
179
179
|
buildConfigField "String", "EXPO_MODULES_CORE_VERSION", "\"${versionName}\""
|
|
180
180
|
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", "true"
|
|
181
181
|
|
|
Binary file
|
|
@@ -80,6 +80,11 @@ open class SharedObject(runtime: Runtime? = null) {
|
|
|
80
80
|
}
|
|
81
81
|
}
|
|
82
82
|
|
|
83
|
+
/**
|
|
84
|
+
* Delays the release until the queued view updates are applied. Use it for objects passed as view props.
|
|
85
|
+
*/
|
|
86
|
+
open val releasesAfterPendingViewUpdates: Boolean = false
|
|
87
|
+
|
|
83
88
|
open fun onStartListeningToEvent(eventName: String) = Unit
|
|
84
89
|
|
|
85
90
|
open fun onStopListeningToEvent(eventName: String) = Unit
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
package expo.modules.kotlin.sharedobjects
|
|
2
2
|
|
|
3
|
+
import com.facebook.react.modules.core.ReactChoreographer
|
|
3
4
|
import expo.modules.kotlin.runtime.Runtime
|
|
4
5
|
import expo.modules.kotlin.exception.Exceptions
|
|
5
6
|
import expo.modules.kotlin.exception.InvalidSharedObjectIdException
|
|
@@ -85,6 +86,19 @@ class SharedObjectRegistry(runtime: Runtime) {
|
|
|
85
86
|
}
|
|
86
87
|
|
|
87
88
|
internal fun delete(id: SharedObjectId) {
|
|
89
|
+
val native = synchronized(this) { pairs[id]?.first } ?: return
|
|
90
|
+
if (native.releasesAfterPendingViewUpdates) {
|
|
91
|
+
val choreographer = runCatching { ReactChoreographer.getInstance() }.getOrNull()
|
|
92
|
+
if (choreographer != null) {
|
|
93
|
+
// Runs after Fabric's `DISPATCH_UI` callback, which applies the queued view updates.
|
|
94
|
+
choreographer.postFrameCallback(ReactChoreographer.CallbackType.IDLE_EVENT) { remove(id) }
|
|
95
|
+
return
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
remove(id)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
private fun remove(id: SharedObjectId) {
|
|
88
102
|
synchronized(this) {
|
|
89
103
|
pairs.remove(id)
|
|
90
104
|
}?.let { (native, _) ->
|
|
@@ -93,6 +93,34 @@ extension Benchmarks {
|
|
|
93
93
|
}
|
|
94
94
|
}
|
|
95
95
|
|
|
96
|
+
/// Benchmarks for encoding an array of records in Swift, with no JavaScript call around it.
|
|
97
|
+
extension Benchmarks {
|
|
98
|
+
@Test(arguments: [10, 1000])
|
|
99
|
+
func `encode an array of records`(count: Int) async throws {
|
|
100
|
+
try await recordBenchmarkCase { runtime in
|
|
101
|
+
let points = (0..<count).map { BenchmarkPoint(x: Double($0), y: Double($0)) }
|
|
102
|
+
try benchmark("[Record].encode: \(count) records", runtime: runtime) { iterations in
|
|
103
|
+
for _ in 0..<iterations {
|
|
104
|
+
_ = try [BenchmarkPoint].encode(points, in: runtime)
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/// Runs one case with the runtime of a new app context.
|
|
112
|
+
private func recordBenchmarkCase(
|
|
113
|
+
_ measure: @escaping @JavaScriptActor (JavaScriptRuntime) throws -> Void
|
|
114
|
+
) async throws {
|
|
115
|
+
try await benchmarkCase {
|
|
116
|
+
let appContext = AppContext.create()
|
|
117
|
+
let runtime = try appContext.runtime
|
|
118
|
+
try withExtendedLifetime(appContext) {
|
|
119
|
+
try measure(runtime)
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
96
124
|
@Record
|
|
97
125
|
private struct BenchmarkPoint {
|
|
98
126
|
var x: Double = 0
|
|
@@ -5,9 +5,18 @@ import Testing
|
|
|
5
5
|
|
|
6
6
|
@testable import ExpoModulesCore
|
|
7
7
|
|
|
8
|
-
/// Benchmarks for JavaScript using a `@SharedObject` class: creating
|
|
9
|
-
///
|
|
10
|
-
/// against `JS: empty driver loop`.
|
|
8
|
+
/// Benchmarks for JavaScript using a `@SharedObject` class: creating, releasing and collecting instances,
|
|
9
|
+
/// reading their IDs, passing them to and getting them from `@JS` functions, and calling their `@JS` members.
|
|
10
|
+
/// A JavaScript driver runs the measured loop, so read these results against `JS: empty driver loop`.
|
|
11
|
+
///
|
|
12
|
+
/// Notes on reading the results:
|
|
13
|
+
/// - `new instance and garbage collection` adds one full collection to each sample and spreads its cost over
|
|
14
|
+
/// the instances created in the sample. It shows whether collecting shared objects got cheaper, not the
|
|
15
|
+
/// exact cost of finalizing one instance.
|
|
16
|
+
/// - `existing instance returned from a function` includes decoding the argument, which
|
|
17
|
+
/// `instance passed as an argument` measures alone.
|
|
18
|
+
/// - `emit: Int payload from a @SharedObject` emits from Swift in a loop, without the JavaScript driver.
|
|
19
|
+
/// Read it against `emit: Int payload`, which emits the same payload from a module.
|
|
11
20
|
extension Benchmarks {
|
|
12
21
|
@Test
|
|
13
22
|
func `@SharedObject: new instance`() async throws {
|
|
@@ -18,6 +27,25 @@ extension Benchmarks {
|
|
|
18
27
|
}
|
|
19
28
|
}
|
|
20
29
|
|
|
30
|
+
@Test
|
|
31
|
+
func `@SharedObject: new instance and release`() async throws {
|
|
32
|
+
try await sharedObjectBenchmarkCase(body: "new Counter(i).release();") { run, runtime in
|
|
33
|
+
try benchmark("@SharedObject: new instance and release", runtime: runtime) { iterations in
|
|
34
|
+
_ = try run.call(arguments: iterations)
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
@Test
|
|
40
|
+
func `@SharedObject: new instance and garbage collection`() async throws {
|
|
41
|
+
try await sharedObjectBenchmarkCase(body: "new Counter(i);") { run, runtime in
|
|
42
|
+
try benchmark("@SharedObject: new instance and garbage collection", runtime: runtime) { iterations in
|
|
43
|
+
_ = try run.call(arguments: iterations)
|
|
44
|
+
runtime.collectGarbage()
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
21
49
|
@Test
|
|
22
50
|
func `@SharedObject: instance returned from a function`() async throws {
|
|
23
51
|
try await sharedObjectBenchmarkCase(body: "m.makeCounter(i);") { run, runtime in
|
|
@@ -36,6 +64,62 @@ extension Benchmarks {
|
|
|
36
64
|
}
|
|
37
65
|
}
|
|
38
66
|
|
|
67
|
+
@Test
|
|
68
|
+
func `@SharedObject: new instance and ID read`() async throws {
|
|
69
|
+
try await sharedObjectBenchmarkCase(body: "new Counter(i).__expo_shared_object_id__;") { run, runtime in
|
|
70
|
+
try benchmark("@SharedObject: new instance and ID read", runtime: runtime) { iterations in
|
|
71
|
+
_ = try run.call(arguments: iterations)
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
@Test
|
|
77
|
+
func `@SharedObject: ID read`() async throws {
|
|
78
|
+
try await sharedObjectBenchmarkCase(body: "counter.__expo_shared_object_id__;") { run, runtime in
|
|
79
|
+
try benchmark("@SharedObject: ID read", runtime: runtime) { iterations in
|
|
80
|
+
_ = try run.call(arguments: iterations)
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
@Test
|
|
86
|
+
func `@SharedObject: instance passed as an argument`() async throws {
|
|
87
|
+
try await sharedObjectBenchmarkCase(body: "m.readCounter(counter);") { run, runtime in
|
|
88
|
+
try benchmark("@SharedObject: instance passed as an argument", runtime: runtime) { iterations in
|
|
89
|
+
_ = try run.call(arguments: iterations)
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
@Test
|
|
95
|
+
func `@SharedObject: existing instance returned from a function`() async throws {
|
|
96
|
+
try await sharedObjectBenchmarkCase(body: "m.echoCounter(counter);") { run, runtime in
|
|
97
|
+
try benchmark("@SharedObject: existing instance returned from a function", runtime: runtime) { iterations in
|
|
98
|
+
_ = try run.call(arguments: iterations)
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
@Test
|
|
104
|
+
func `emit: Int payload from a @SharedObject`() async throws {
|
|
105
|
+
try await moduleBenchmarkCase { appContext in
|
|
106
|
+
return SharedObjectBenchmarkModule(appContext: appContext)
|
|
107
|
+
} _: { _, runtime in
|
|
108
|
+
let counterObject = try runtime.eval([
|
|
109
|
+
"var counter = new expo.modules.SharedObjectBenchmarkModule.BenchmarkCounter(0)",
|
|
110
|
+
"counter.addListener('count', () => {})",
|
|
111
|
+
"counter"
|
|
112
|
+
]).asObject()
|
|
113
|
+
let counter = try SharedObject.native(from: counterObject)
|
|
114
|
+
|
|
115
|
+
try benchmark("emit: Int payload from a @SharedObject", runtime: runtime) { iterations in
|
|
116
|
+
for index in 0..<iterations {
|
|
117
|
+
counter.emit(event: "count", payload: index)
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
39
123
|
@Test
|
|
40
124
|
func `@SharedObject: method call`() async throws {
|
|
41
125
|
try await sharedObjectBenchmarkCase(body: "counter.increment(1);") { run, runtime in
|
|
@@ -87,6 +171,16 @@ private final class SharedObjectBenchmarkModule: Module {
|
|
|
87
171
|
func makeRef(value: Int) -> SharedRef<Int> {
|
|
88
172
|
return SharedRef(value)
|
|
89
173
|
}
|
|
174
|
+
|
|
175
|
+
@JS
|
|
176
|
+
func readCounter(counter: BenchmarkCounter) -> Int {
|
|
177
|
+
return counter.current
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
@JS
|
|
181
|
+
func echoCounter(counter: BenchmarkCounter) -> BenchmarkCounter {
|
|
182
|
+
return counter
|
|
183
|
+
}
|
|
90
184
|
}
|
|
91
185
|
|
|
92
186
|
/// Runs one case with `SharedObjectBenchmarkModule` registered and a JavaScript driver that runs `body`
|
|
@@ -9,6 +9,9 @@ import ExpoModulesJSI
|
|
|
9
9
|
public final class ArrayBuffer: AnyArrayBuffer, Sendable {
|
|
10
10
|
private let storage: ArrayBufferStorage
|
|
11
11
|
|
|
12
|
+
/// Whether a live JavaScript array buffer reports this buffer's memory to the garbage collector.
|
|
13
|
+
private let isMemoryReported = Mutex(false)
|
|
14
|
+
|
|
12
15
|
private var rawPointer: UnsafeMutableRawPointer {
|
|
13
16
|
return storage.rawPointer
|
|
14
17
|
}
|
|
@@ -86,14 +89,34 @@ public final class ArrayBuffer: AnyArrayBuffer, Sendable {
|
|
|
86
89
|
/// The native buffer is retained for the lifetime of the `JavaScriptArrayBuffer`.
|
|
87
90
|
@usableFromInline
|
|
88
91
|
func asJavaScriptArrayBuffer(runtime: JavaScriptRuntime) -> JavaScriptArrayBuffer {
|
|
89
|
-
|
|
92
|
+
// The garbage collector does not see native memory, so one live JavaScript array buffer reports it.
|
|
93
|
+
// Reporting it for every wrapper would add the full size each time the same buffer is returned.
|
|
94
|
+
// Borrowed memory belongs to another JavaScript array buffer, which accounts for it.
|
|
95
|
+
let reportsMemory =
|
|
96
|
+
storage.ownsMemory && byteLength > 0
|
|
97
|
+
&& isMemoryReported.withLock { isReported in
|
|
98
|
+
if isReported {
|
|
99
|
+
return false
|
|
100
|
+
}
|
|
101
|
+
isReported = true
|
|
102
|
+
return true
|
|
103
|
+
}
|
|
104
|
+
let jsArrayBuffer = runtime.createArrayBuffer(
|
|
90
105
|
data: rawPointer.assumingMemoryBound(to: UInt8.self),
|
|
91
106
|
size: byteLength
|
|
92
107
|
) { [self] in
|
|
93
108
|
// Retain `self` until the JS engine releases the ArrayBuffer. When this
|
|
94
109
|
// closure is dropped, the buffer deinits and deallocates its memory.
|
|
95
|
-
|
|
110
|
+
if reportsMemory {
|
|
111
|
+
isMemoryReported.withLock { isReported in
|
|
112
|
+
isReported = false
|
|
113
|
+
}
|
|
114
|
+
}
|
|
96
115
|
}
|
|
116
|
+
if reportsMemory {
|
|
117
|
+
jsArrayBuffer.asValue().getObject().setExternalMemoryPressure(byteLength)
|
|
118
|
+
}
|
|
119
|
+
return jsArrayBuffer
|
|
97
120
|
}
|
|
98
121
|
|
|
99
122
|
// MARK: - Copy
|
|
@@ -147,10 +170,16 @@ public final class ArrayBuffer: AnyArrayBuffer, Sendable {
|
|
|
147
170
|
/// - Warning: This bypasses Data's copy-on-write capabilities, effectively allowing
|
|
148
171
|
/// mutation of the Data from JavaScript code.
|
|
149
172
|
public static func wrap(dataWithoutCopy data: Data) -> ArrayBuffer {
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
return
|
|
153
|
-
|
|
173
|
+
// `NSData.bytes` returns an inner pointer, so the call retains and autoreleases the `NSData`, which
|
|
174
|
+
// owns the bytes. Without this pool, the bytes stay alive until the caller's pool drains, which never
|
|
175
|
+
// happens while a caller such as a JavaScript loop does not return to its run loop. `retained` keeps
|
|
176
|
+
// the `NSData` alive after this pool drains.
|
|
177
|
+
return autoreleasepool {
|
|
178
|
+
let retained = Unmanaged.passRetained(data as NSData)
|
|
179
|
+
let pointer = UnsafeMutableRawPointer(mutating: retained.takeUnretainedValue().bytes)
|
|
180
|
+
return ArrayBuffer(owning: pointer, count: data.count) {
|
|
181
|
+
retained.release()
|
|
182
|
+
}
|
|
154
183
|
}
|
|
155
184
|
}
|
|
156
185
|
|
|
@@ -305,6 +334,15 @@ private enum ArrayBufferStorage: Sendable {
|
|
|
305
334
|
}
|
|
306
335
|
}
|
|
307
336
|
|
|
337
|
+
var ownsMemory: Bool {
|
|
338
|
+
switch self {
|
|
339
|
+
case .ownedNative:
|
|
340
|
+
return true
|
|
341
|
+
case .nativeBacked:
|
|
342
|
+
return false
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
|
|
308
346
|
func cleanup() {
|
|
309
347
|
switch self {
|
|
310
348
|
case .ownedNative(let storage), .nativeBacked(let storage):
|
|
@@ -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
|
}
|
|
@@ -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
|
*/
|
|
@@ -97,6 +116,11 @@ open class SharedObject: AnySharedObject {
|
|
|
97
116
|
@JavaScriptActor
|
|
98
117
|
open class func _decorateSharedObject(prototype: borrowing JavaScriptObject, in runtime: JavaScriptRuntime) throws {}
|
|
99
118
|
|
|
119
|
+
/// Binds the class's `static` and `class` `@JS` members onto its JavaScript constructor. Overridden by
|
|
120
|
+
/// the `@SharedObject` macro.
|
|
121
|
+
@JavaScriptActor
|
|
122
|
+
open class func _decorateSharedObject(constructor: borrowing JavaScriptObject, in runtime: JavaScriptRuntime) throws {}
|
|
123
|
+
|
|
100
124
|
/// Builds a native instance from the JS constructor arguments, or `nil` to use the DSL `Constructor`
|
|
101
125
|
/// path. Overridden by the `@SharedObject` macro from the class's `@JS init`.
|
|
102
126
|
@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
|
|
@@ -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.26",
|
|
4
4
|
"description": "The core of Expo Modules architecture",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"expo",
|
|
@@ -66,7 +66,7 @@
|
|
|
66
66
|
"./package.json": "./package.json"
|
|
67
67
|
},
|
|
68
68
|
"dependencies": {
|
|
69
|
-
"expo-modules-jsi": "~58.0.
|
|
69
|
+
"expo-modules-jsi": "~58.0.19",
|
|
70
70
|
"expo-modules-macros": "0.22.0",
|
|
71
71
|
"invariant": "^2.2.4"
|
|
72
72
|
},
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|