expo-modules-core 58.0.21 → 58.0.23

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 (27) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/android/build.gradle +2 -2
  3. package/android/prebuilt/metadata.json +2 -2
  4. package/android/prebuilt/native-libs.tar.xz +0 -0
  5. package/android/src/compose/expo/modules/kotlin/views/ExpoComposeView.kt +11 -0
  6. package/android/src/main/cpp/worklets/Serializable.h +1 -1
  7. package/android/src/main/cpp/worklets/Worklet.cpp +1 -1
  8. package/common/cpp/JSI/JSIUtils.cpp +43 -13
  9. package/common/cpp/JSI/JSIUtils.h +5 -0
  10. package/common/cpp/SharedObject.cpp +21 -0
  11. package/ios/Benchmarks/FunctionBenchmarks.swift +41 -0
  12. package/ios/Benchmarks/SharedObjectBenchmarks.swift +34 -6
  13. package/ios/Core/AppContext.swift +15 -15
  14. package/ios/Core/Classes/ClassDefinition.swift +4 -3
  15. package/ios/Core/Classes/ClassRegistry.swift +14 -18
  16. package/ios/Core/Coding/JavaScriptCodable+Record.swift +3 -12
  17. package/ios/Core/Conversions.swift +3 -15
  18. package/ios/Core/DynamicTypes/DynamicSharedObjectType.swift +2 -8
  19. package/ios/Core/ExpoModulesMacros.swift +10 -5
  20. package/ios/Core/ExpoRuntime.swift +18 -0
  21. package/ios/WorkletsAdapter/ExpoWorkletsBridgeProvider.mm +4 -3
  22. package/package.json +3 -3
  23. package/prebuilds/output/debug/xcframeworks/ExpoModulesCore.tar.gz +0 -0
  24. package/prebuilds/output/debug/xcframeworks/ExpoModulesWorklets.tar.gz +0 -0
  25. package/prebuilds/output/headers/xcframeworks/ExpoModulesCoreHeaders.tar.gz +0 -0
  26. package/prebuilds/output/release/xcframeworks/ExpoModulesCore.tar.gz +0 -0
  27. package/prebuilds/output/release/xcframeworks/ExpoModulesWorklets.tar.gz +0 -0
package/CHANGELOG.md CHANGED
@@ -1,5 +1,26 @@
1
1
  # Changelog
2
2
 
3
+ ## 58.0.23
4
+
5
+ ### Patch Changes
6
+
7
+ - [iOS] Reduce the overhead of returning `Data` and shared objects to JavaScript. ([#51728](https://github.com/expo/expo/pull/51728) by [@tsapeta](https://github.com/tsapeta))
8
+ - [Android] Fix a Jetpack Compose `Host` that stays blank when it mounts in a visible list row with `removeClippedSubviews` enabled, for example in a `FlatList`. The `Host` now measures its content when it is attached for the first time. ([#51780](https://github.com/expo/expo/pull/51780) by [@expo-bot](https://github.com/expo-bot))
9
+ - [iOS] Update `expo-modules-macros` to 0.21.0. `@Record` types now convert between JavaScript and native code with less overhead and no longer need an app context. `@ExpoModule` and `@SharedObject` now report a build error when two `@JS` members of one object have the same JavaScript name, because JavaScript could reach only one of them. ([#51716](https://github.com/expo/expo/pull/51716) by [@tsapeta](https://github.com/tsapeta))
10
+ - Shared objects now report their class name from `Object.prototype.toString`, for example `[object VideoPlayer]`. React in development builds no longer deep-compares shared objects in props, which threw `ERR_NATIVE_SHARED_OBJECT_NOT_FOUND` when the previous props held a released shared object. `Blob` from `expo-blob` and `File` from `expo-file-system` now report `[object Blob]` and `[object File]`, as on the web. Libraries that detect these types by this tag now treat them as a `Blob` and a `File`. ([#51743](https://github.com/expo/expo/pull/51743) by [@tsapeta](https://github.com/tsapeta))
11
+ - Updated dependencies. ([#51728](https://github.com/expo/expo/pull/51728), [#51739](https://github.com/expo/expo/pull/51739), [#51577](https://github.com/expo/expo/pull/51577))
12
+ - expo-modules-jsi@58.0.16
13
+
14
+ ## 58.0.22
15
+
16
+ ### Patch Changes
17
+
18
+ - [Android] Fix native memory that grew with each module class under Hermes V1, by about 0.57 MiB per class on React Native 0.86 and 0.87 and about 93 KiB on 0.88. `createClass` evaluated a new JavaScript source for each class, and Hermes keeps memory for every evaluated source until the runtime is destroyed. It now evaluates one class factory for each runtime and calls it for each class. On iOS, this code creates only the four base classes (`SharedObject`, `SharedRef`, `EventEmitter` and `NativeModule`). ([#51582](https://github.com/expo/expo/pull/51582) by [@amillez](https://github.com/amillez))
19
+ - Fix an `-Wunused-result` warning in `Worklet.cpp` on Android when building with `react-native-worklets@0.13`. Running a worklet synchronously now discards its return value while the worklet runtime lock is still held, on Android and iOS. ([#51656](https://github.com/expo/expo/pull/51656) by [@tomekzaw](https://github.com/tomekzaw))
20
+ - Fix the `Do not include this file directly` warnings from `react-native-worklets@0.13` on Android and iOS. The worklets integration now includes the specific `Serializable` headers instead of the deprecated `worklets/SharedItems/Serializable.h`. ([#51664](https://github.com/expo/expo/pull/51664) by [@tomekzaw](https://github.com/tomekzaw))
21
+ - Updated dependencies. ([#51583](https://github.com/expo/expo/pull/51583))
22
+ - expo-modules-jsi@58.0.15
23
+
3
24
  ## 58.0.21
4
25
 
5
26
  ### Patch Changes
@@ -46,7 +46,7 @@ if (shouldIncludeCompose) {
46
46
  }
47
47
 
48
48
  group = 'host.exp.exponent'
49
- version = '58.0.21'
49
+ version = '58.0.23'
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.21"
178
+ versionName "58.0.23"
179
179
  buildConfigField "String", "EXPO_MODULES_CORE_VERSION", "\"${versionName}\""
180
180
  buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", "true"
181
181
 
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "reactNativeVersion": "0.88.0",
3
- "sourceHash": "0b72456880c81b38b6a81f2e69eb4cf4",
3
+ "sourceHash": "d5785b6fa4de4ea64f18bdac451d0308",
4
4
  "variants": [
5
5
  "debug",
6
6
  "release"
@@ -16,5 +16,5 @@
16
16
  "libexpo-modules-jsi.so"
17
17
  ],
18
18
  "stripped": true,
19
- "createdAt": "2026-10-16T15:16:23.131056350Z"
19
+ "createdAt": "2026-10-19T12:37:31.850362445Z"
20
20
  }
Binary file
@@ -82,9 +82,16 @@ abstract class ExpoComposeView<T : ComposeProps>(
82
82
 
83
83
  override val shouldUseAndroidLayout = withHostingView
84
84
 
85
+ // A measure pass skipped while detached is cached by Android, so the same specs will not measure
86
+ // the Compose content again. Set when that happens, so the first attach can request a new pass.
87
+ // React Native can attach this view from inside its own `layout()`, which clears the forced-layout
88
+ // flag when it returns, so the request is posted.
89
+ private var needsLayoutOnAttach = false
90
+
85
91
  override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
86
92
  // In case of issues there's an alternative solution in previous commits at https://github.com/expo/expo/pull/33759
87
93
  if (shouldUseAndroidLayout && !isAttachedToWindow) {
94
+ needsLayoutOnAttach = true
88
95
  setMeasuredDimension(widthMeasureSpec, heightMeasureSpec)
89
96
  return
90
97
  }
@@ -112,6 +119,10 @@ abstract class ExpoComposeView<T : ComposeProps>(
112
119
 
113
120
  override fun onAttachedToWindow() {
114
121
  super.onAttachedToWindow()
122
+ if (needsLayoutOnAttach) {
123
+ needsLayoutOnAttach = false
124
+ post { requestLayout() }
125
+ }
115
126
  if (!withHostingView) {
116
127
  validateHostingAncestor()
117
128
  }
@@ -4,7 +4,7 @@
4
4
  #include "../JSIContext.h"
5
5
  #include "../JNIDeallocator.h"
6
6
 
7
- #include <worklets/SharedItems/Serializable.h>
7
+ #include <worklets/SharedItems/Serializable/Serializable.h>
8
8
 
9
9
  namespace jni = facebook::jni;
10
10
 
@@ -36,7 +36,7 @@ void Worklet::execute(
36
36
  synchronizable->cthis()->getSerializable()
37
37
  );
38
38
 
39
- workletRuntime->runSync(worklet);
39
+ workletRuntime->runSyncAndDiscard(worklet);
40
40
  }
41
41
 
42
42
  void Worklet::scheduleWithArgs(
@@ -1,21 +1,39 @@
1
1
  // Copyright 2022-present 650 Industries. All rights reserved.
2
2
 
3
- #include <sstream>
4
3
  #include <utility>
5
4
  #include "JSIUtils.h"
6
5
 
7
6
  namespace expo::common {
8
7
 
8
+ static constexpr jsi::UUID classFactoryUUID{0xa1f49c57, 0x6f0a, 0x4775, 0x9f44, 0xcb0673675b4c};
9
+
10
+ // Hermes keeps memory for every evaluated source until the runtime is destroyed, so the
11
+ // constructor source is evaluated once per runtime and the factory is called for each class.
12
+ // The runtime data releases the factory when the runtime is destroyed, and JS code can't reach it.
13
+ static std::shared_ptr<jsi::Function> getClassFactory(jsi::Runtime &runtime) {
14
+ if (std::shared_ptr<void> cached = runtime.getRuntimeData(classFactoryUUID)) {
15
+ return std::static_pointer_cast<jsi::Function>(cached);
16
+ }
17
+ std::shared_ptr<jsi::StringBuffer> sourceBuffer = std::make_shared<jsi::StringBuffer>(
18
+ "(function (name) {"
19
+ " const klass = function (...args) { return this.__native_constructor__(...args); };"
20
+ " Object.defineProperty(klass, 'name', { value: name });"
21
+ " return klass;"
22
+ "})"
23
+ );
24
+ auto factory = std::make_shared<jsi::Function>(
25
+ runtime.evaluateJavaScript(sourceBuffer, "expo-modules-core/class-factory.js").asObject(runtime).asFunction(runtime)
26
+ );
27
+ runtime.setRuntimeData(classFactoryUUID, factory);
28
+ return factory;
29
+ }
30
+
9
31
  jsi::Function createClass(jsi::Runtime &runtime, const char *name, ClassConstructor constructor) {
10
32
  std::string nativeConstructorKey("__native_constructor__");
11
33
 
12
- // Create a string buffer of the source code to evaluate.
13
- std::stringstream source;
14
- source << "(function " << name << "(...args) { return this." << nativeConstructorKey << "(...args); })";
15
- std::shared_ptr<jsi::StringBuffer> sourceBuffer = std::make_shared<jsi::StringBuffer>(source.str());
16
-
17
- // Evaluate the code and obtain the returned value (the constructor function).
18
- jsi::Object klass = runtime.evaluateJavaScript(sourceBuffer, "").asObject(runtime);
34
+ jsi::Object klass = getClassFactory(runtime)
35
+ ->call(runtime, jsi::String::createFromUtf8(runtime, name))
36
+ .asObject(runtime);
19
37
 
20
38
  // Set the native constructor in the prototype.
21
39
  jsi::Object prototype = klass.getPropertyAsObject(runtime, "prototype");
@@ -84,7 +102,7 @@ std::vector<jsi::PropNameID> jsiArrayToPropNameIdsVector(jsi::Runtime &runtime,
84
102
  return vector;
85
103
  }
86
104
 
87
- void defineProperty(jsi::Runtime &runtime, jsi::Object *object, const char *name, const PropertyDescriptor& descriptor) {
105
+ static jsi::Object createDescriptorObject(jsi::Runtime &runtime, const PropertyDescriptor& descriptor) {
88
106
  jsi::Object jsDescriptor(runtime);
89
107
 
90
108
  // These three flags are all `false` by default, so set the property only when `true`.
@@ -128,20 +146,32 @@ void defineProperty(jsi::Runtime &runtime, jsi::Object *object, const char *name
128
146
  jsDescriptor.setProperty(runtime, valuePropName, descriptor.value);
129
147
  }
130
148
 
131
- defineProperty(runtime, object, name, std::move(jsDescriptor));
149
+ return jsDescriptor;
132
150
  }
133
151
 
134
- void defineProperty(jsi::Runtime &runtime, jsi::Object *object, const char *name, jsi::Object descriptor) {
152
+ static void callDefineProperty(jsi::Runtime &runtime, jsi::Object *object, jsi::Value key, jsi::Object descriptor) {
135
153
  jsi::Object global = runtime.global();
136
154
  jsi::Object objectClass = global.getPropertyAsObject(runtime, "Object");
137
155
  jsi::Function definePropertyFunction = objectClass.getPropertyAsFunction(runtime, "defineProperty");
138
156
 
139
- // This call is basically the same as `Object.defineProperty(object, name, descriptor)` in JS
157
+ // This call is basically the same as `Object.defineProperty(object, key, descriptor)` in JS
140
158
  definePropertyFunction.callWithThis(runtime, objectClass, {
141
159
  jsi::Value(runtime, *object),
142
- jsi::String::createFromUtf8(runtime, name),
160
+ std::move(key),
143
161
  std::move(descriptor),
144
162
  });
145
163
  }
146
164
 
165
+ void defineProperty(jsi::Runtime &runtime, jsi::Object *object, const char *name, const PropertyDescriptor& descriptor) {
166
+ callDefineProperty(runtime, object, jsi::String::createFromUtf8(runtime, name), createDescriptorObject(runtime, descriptor));
167
+ }
168
+
169
+ void defineProperty(jsi::Runtime &runtime, jsi::Object *object, const jsi::Symbol &key, const PropertyDescriptor& descriptor) {
170
+ callDefineProperty(runtime, object, jsi::Value(runtime, key), createDescriptorObject(runtime, descriptor));
171
+ }
172
+
173
+ void defineProperty(jsi::Runtime &runtime, jsi::Object *object, const char *name, jsi::Object descriptor) {
174
+ callDefineProperty(runtime, object, jsi::String::createFromUtf8(runtime, name), std::move(descriptor));
175
+ }
176
+
147
177
  } // namespace expo::common
@@ -66,6 +66,11 @@ struct PropertyDescriptor {
66
66
  */
67
67
  void defineProperty(jsi::Runtime &runtime, jsi::Object *object, const char *name, const PropertyDescriptor& descriptor);
68
68
 
69
+ /**
70
+ Defines the property under a symbol key, such as `Symbol.toStringTag`, with the provided descriptor options.
71
+ */
72
+ void defineProperty(jsi::Runtime &runtime, jsi::Object *object, const jsi::Symbol &key, const PropertyDescriptor& descriptor);
73
+
69
74
  /**
70
75
  Calls `Object.defineProperty(object, name, descriptor)`.
71
76
  */
@@ -74,6 +74,27 @@ void installBaseClass(jsi::Runtime &runtime, const ObjectReleaser& releaser) {
74
74
  prototype.setProperty(runtime, "release", releaseFunction);
75
75
  prototype.setProperty(runtime, "toJSON", toJSONFunction);
76
76
 
77
+ // React in development deep-compares props whose `Object.prototype.toString` is `[object Object]`.
78
+ // That reads every property getter, and a getter throws when its shared object was released.
79
+ // The tag is the name of the object's class, so subclasses, also those defined in JS, report their own.
80
+ jsi::Symbol toStringTag = runtime.global()
81
+ .getPropertyAsObject(runtime, "Symbol")
82
+ .getProperty(runtime, "toStringTag")
83
+ .getSymbol(runtime);
84
+ defineProperty(runtime, &prototype, toStringTag, common::PropertyDescriptor {
85
+ .configurable = true,
86
+ .get = [](jsi::Runtime &runtime, jsi::Object thisObject) {
87
+ jsi::Value constructor = thisObject.getProperty(runtime, "constructor");
88
+ if (constructor.isObject()) {
89
+ jsi::Value name = constructor.getObject(runtime).getProperty(runtime, "name");
90
+ if (name.isString() && !name.getString(runtime).utf8(runtime).empty()) {
91
+ return name;
92
+ }
93
+ }
94
+ return jsi::Value(jsi::String::createFromAscii(runtime, "SharedObject"));
95
+ }
96
+ });
97
+
77
98
  // This property should be deprecated, but it's still used when passing as a view prop.
78
99
  defineProperty(runtime, &prototype, "__expo_shared_object_id__", common::PropertyDescriptor {
79
100
  .get = [](jsi::Runtime &runtime, jsi::Object thisObject) {
@@ -55,6 +55,34 @@ extension Benchmarks {
55
55
  }
56
56
  }
57
57
 
58
+ @Test
59
+ func `@JS: record argument`() async throws {
60
+ try await functionBenchmarkCase(setup: "var point = { x: 1, y: 2 };", body: "m.take(point);") { run, runtime in
61
+ try benchmark("@JS: record argument", runtime: runtime) { iterations in
62
+ _ = try run.call(arguments: iterations)
63
+ }
64
+ }
65
+ }
66
+
67
+ @Test
68
+ func `@JS: record result`() async throws {
69
+ try await functionBenchmarkCase(body: "m.makePoint();") { run, runtime in
70
+ try benchmark("@JS: record result", runtime: runtime) { iterations in
71
+ _ = try run.call(arguments: iterations)
72
+ }
73
+ }
74
+ }
75
+
76
+ @Test
77
+ func `@JS: array of 10 records argument and result`() async throws {
78
+ let setup = "var points = []; for (var k = 0; k < 10; k++) { points.push({ x: k, y: k }); }"
79
+ try await functionBenchmarkCase(setup: setup, body: "m.mirrorAll(points);") { run, runtime in
80
+ try benchmark("@JS: array of 10 records argument and result", runtime: runtime) { iterations in
81
+ _ = try run.call(arguments: iterations)
82
+ }
83
+ }
84
+ }
85
+
58
86
  @Test
59
87
  func `@JS: async function with no arguments`() async throws {
60
88
  try await functionBenchmarkCase(body: "m.noopAsync();") { run, runtime in
@@ -91,6 +119,19 @@ private final class FunctionBenchmarkModule: Module {
91
119
  return BenchmarkPoint(x: point.y, y: point.x)
92
120
  }
93
121
 
122
+ @JS
123
+ func take(point: BenchmarkPoint) {}
124
+
125
+ @JS
126
+ func makePoint() -> BenchmarkPoint {
127
+ return BenchmarkPoint(x: 1, y: 2)
128
+ }
129
+
130
+ @JS
131
+ func mirrorAll(points: [BenchmarkPoint]) -> [BenchmarkPoint] {
132
+ return points
133
+ }
134
+
94
135
  @JS
95
136
  @JavaScriptActor
96
137
  func noopAsync() async {}
@@ -5,9 +5,9 @@ import Testing
5
5
 
6
6
  @testable import ExpoModulesCore
7
7
 
8
- /// Benchmarks for JavaScript using a `@SharedObject` class: creating instances and calling their `@JS`
9
- /// members. A JavaScript driver runs the measured loop. Read every result against
10
- /// `JS: empty driver loop`.
8
+ /// Benchmarks for JavaScript using a `@SharedObject` class: creating instances, getting them from a `@JS`
9
+ /// function and calling their `@JS` members. A JavaScript driver runs the measured loop. Read every result
10
+ /// against `JS: empty driver loop`.
11
11
  extension Benchmarks {
12
12
  @Test
13
13
  func `@SharedObject: new instance`() async throws {
@@ -18,6 +18,24 @@ extension Benchmarks {
18
18
  }
19
19
  }
20
20
 
21
+ @Test
22
+ func `@SharedObject: instance returned from a function`() async throws {
23
+ try await sharedObjectBenchmarkCase(body: "m.makeCounter(i);") { run, runtime in
24
+ try benchmark("@SharedObject: instance returned from a function", runtime: runtime) { iterations in
25
+ _ = try run.call(arguments: iterations)
26
+ }
27
+ }
28
+ }
29
+
30
+ @Test
31
+ func `SharedRef without a class: returned from a function`() async throws {
32
+ try await sharedObjectBenchmarkCase(body: "m.makeRef(i);") { run, runtime in
33
+ try benchmark("SharedRef without a class: returned from a function", runtime: runtime) { iterations in
34
+ _ = try run.call(arguments: iterations)
35
+ }
36
+ }
37
+ }
38
+
21
39
  @Test
22
40
  func `@SharedObject: method call`() async throws {
23
41
  try await sharedObjectBenchmarkCase(body: "counter.increment(1);") { run, runtime in
@@ -59,10 +77,20 @@ private final class BenchmarkCounter: SharedObject {
59
77
  }
60
78
 
61
79
  @ExpoModule(classes: [BenchmarkCounter.self])
62
- private final class SharedObjectBenchmarkModule: Module {}
80
+ private final class SharedObjectBenchmarkModule: Module {
81
+ @JS
82
+ func makeCounter(value: Int) -> BenchmarkCounter {
83
+ return BenchmarkCounter(value: value)
84
+ }
85
+
86
+ @JS
87
+ func makeRef(value: Int) -> SharedRef<Int> {
88
+ return SharedRef(value)
89
+ }
90
+ }
63
91
 
64
92
  /// Runs one case with `SharedObjectBenchmarkModule` registered and a JavaScript driver that runs `body`
65
- /// in a loop, with the class in `Counter` and one instance in `counter`.
93
+ /// in a loop, with the module in `m`, the class in `Counter` and one instance in `counter`.
66
94
  private func sharedObjectBenchmarkCase(
67
95
  body: String,
68
96
  _ measure: @escaping @JavaScriptActor (borrowing JavaScriptFunction, JavaScriptRuntime) throws -> Void
@@ -72,7 +100,7 @@ private func sharedObjectBenchmarkCase(
72
100
  } _: { _, runtime in
73
101
  let run = try javaScriptDriver(
74
102
  runtime,
75
- setup: "var Counter = expo.modules.SharedObjectBenchmarkModule.BenchmarkCounter; var counter = new Counter(0);",
103
+ setup: "var m = expo.modules.SharedObjectBenchmarkModule; var Counter = m.BenchmarkCounter; var counter = new Counter(0);",
76
104
  body: body
77
105
  )
78
106
  try measure(run, runtime)
@@ -230,11 +230,9 @@ public class AppContext: NSObject, EXAppContextProtocol, @unchecked Sendable {
230
230
 
231
231
  internal lazy var sharedObjectRegistry = SharedObjectRegistry(appContext: self)
232
232
 
233
- /**
234
- A registry containing references to JavaScript classes.
235
- - ToDo: Make one registry per module, not the entire app context.
236
- Perhaps it should be kept by the `ModuleHolder`.
237
- */
233
+ /// A registry containing references to the prototypes of JavaScript classes.
234
+ /// - ToDo: Make one registry per module, not the entire app context.
235
+ /// Perhaps it should be kept by the `ModuleHolder`.
238
236
  internal let classRegistry = ClassRegistry()
239
237
 
240
238
  /**
@@ -242,13 +240,14 @@ public class AppContext: NSObject, EXAppContextProtocol, @unchecked Sendable {
242
240
  - ToDo: Move this to `ModuleHolder` along the `classRegistry` property.
243
241
  */
244
242
  internal func newObject(nativeClassId: ObjectIdentifier) throws -> JavaScriptObject? {
245
- guard let jsClass = classRegistry.getJavaScriptClass(nativeClassId: nativeClassId) else {
246
- throw JavaScriptClassNotFoundException()
243
+ let runtime = try self.runtime
244
+ let object = classRegistry.withPrototype(nativeClassId: nativeClassId) { (prototype: borrowing JavaScriptObject) in
245
+ return runtime.createObject(prototype: prototype)
247
246
  }
248
- return try JavaScriptActor.assumeIsolated {
249
- let prototype = try jsClass.getProperty("prototype").asObject()
250
- return try runtime.createObject(prototype: prototype)
247
+ guard let object else {
248
+ throw JavaScriptClassNotFoundException()
251
249
  }
250
+ return object
252
251
  }
253
252
 
254
253
  // MARK: - Legacy modules
@@ -691,11 +690,12 @@ public class AppContext: NSObject, EXAppContextProtocol, @unchecked Sendable {
691
690
  guard let nativeState = nativeState as? NativeState, nativeState.ownsLifecycle else {
692
691
  return
693
692
  }
694
- // `destroy()` asserts it runs on the JS thread (`JavaScriptActor.assumeIsolated`). That
695
- // holds for the intended path, where the native state dies as the runtime finalizes on
696
- // its own thread. A future cross-runtime sharing path that could drop this state's last
697
- // JSI slot from another thread (see `JavaScriptNativeState.acquireShared`) would need to
698
- // hop back to the JS thread here first.
693
+ // Hermes runs this on its finalizer thread while the JS thread waits in the runtime's
694
+ // destructor, so nothing else uses the runtime and `destroy()` can release JS objects
695
+ // here. Don't send that work to another thread: it would run after Hermes frees its
696
+ // values. A future cross-runtime sharing path that could drop this state's last JSI slot
697
+ // while the runtime still runs JavaScript (see `JavaScriptNativeState.acquireShared`)
698
+ // would need to hop to the JS thread here first.
699
699
  nativeState.appContext.destroy()
700
700
  }
701
701
  }
@@ -87,15 +87,16 @@ public final class ClassDefinition: ObjectDefinition {
87
87
 
88
88
  try decorate(object: klass, appContext: appContext)
89
89
 
90
+ let prototype = klass.getProperty("prototype").getObject()
91
+
90
92
  // Bind the macro's `@JS` members onto the prototype.
91
93
  if let sharedObjectType {
92
- let prototype = klass.getProperty("prototype").getObject()
93
94
  try sharedObjectType._decorateSharedObject(prototype: prototype, in: appContext.runtime)
94
95
  }
95
96
 
96
- // Register the JS class and its associated native type.
97
+ // Register the prototype of the JS class for its associated native type.
97
98
  if let sharedObjectType = associatedType as? DynamicSharedObjectType {
98
- appContext.classRegistry.register(nativeClassId: sharedObjectType.typeIdentifier, javaScriptClass: klass)
99
+ appContext.classRegistry.register(nativeClassId: sharedObjectType.typeIdentifier, prototype: prototype)
99
100
  }
100
101
 
101
102
  return klass
@@ -3,33 +3,29 @@
3
3
  import ExpoModulesJSI
4
4
 
5
5
  internal final class ClassRegistry {
6
- // Classes are kept behind `JavaScriptRef` because `JavaScriptObject` is ~Copyable and cannot be a
7
- // Dictionary value.
8
- var nativeToJS = [ObjectIdentifier: JavaScriptValue.Ref]()
6
+ // Prototypes are kept behind `JavaScriptRef` because `JavaScriptObject` is ~Copyable and cannot be a
7
+ // Dictionary value. Only the prototype is kept: creating an instance needs nothing else from the class,
8
+ // and reading the prototype from the class would cost a property lookup on every instance.
9
+ var nativeToPrototype = [ObjectIdentifier: JavaScriptObject.Ref]()
9
10
 
10
11
  // MARK: - Accessing
11
12
 
12
- func getJavaScriptClass(nativeClassId: ObjectIdentifier) -> JavaScriptObject? {
13
- return nativeToJS[nativeClassId]?.withUnwrappedValue { (value: borrowing JavaScriptValue) in value.getObject() }
14
- }
15
-
16
- func getJavaScriptClass(nativeClass: SharedObject.Type) -> JavaScriptObject? {
17
- let nativeClassId = ObjectIdentifier(nativeClass)
18
- return getJavaScriptClass(nativeClassId: nativeClassId)
13
+ /// Calls `body` with the prototype of the JavaScript class registered for the native class.
14
+ /// Returns `nil` when no class is registered.
15
+ func withPrototype<R: ~Copyable>(
16
+ nativeClassId: ObjectIdentifier,
17
+ _ body: (borrowing JavaScriptObject) throws -> R
18
+ ) rethrows -> R? {
19
+ return try nativeToPrototype[nativeClassId]?.withUnwrappedValue(body)
19
20
  }
20
21
 
21
22
  // MARK: - Registration
22
23
 
23
- func register(nativeClassId: ObjectIdentifier, javaScriptClass: borrowing JavaScriptObject) {
24
- nativeToJS[nativeClassId] = javaScriptClass.refToValue()
25
- }
26
-
27
- func register(nativeClass: SharedObject.Type, javaScriptClass: borrowing JavaScriptObject) {
28
- let nativeClassId = ObjectIdentifier(nativeClass)
29
- register(nativeClassId: nativeClassId, javaScriptClass: javaScriptClass)
24
+ func register(nativeClassId: ObjectIdentifier, prototype: consuming JavaScriptObject) {
25
+ nativeToPrototype[nativeClassId] = prototype.ref()
30
26
  }
31
27
 
32
28
  internal func clear() {
33
- nativeToJS.removeAll()
29
+ nativeToPrototype.removeAll()
34
30
  }
35
31
  }
@@ -14,8 +14,9 @@ import ExpoModulesJSI
14
14
  // context never prepared has no such state, so recovery returns `nil` and the conversion throws
15
15
  // `Exceptions.AppContextNotFound`.
16
16
  //
17
- // Both `decode` overloads read the object and hand it to `from(object:appContext:)`. The unowned one
18
- // reads it straight from the borrowed value, so a record argument isn't copied first.
17
+ // `decode` reads the object straight from the borrowed value and hands it to
18
+ // `from(object:appContext:)`, so a record argument isn't copied first. An owning value takes the
19
+ // `JavaScriptDecodable` default, which borrows it and calls this `decode`.
19
20
 
20
21
  extension Record {
21
22
  // A record decodes only from an object.
@@ -24,16 +25,6 @@ extension Record {
24
25
  return .object
25
26
  }
26
27
 
27
- @JavaScriptActor
28
- @inlinable
29
- public static func decode(_ value: borrowing JavaScriptValue, in runtime: borrowing JavaScriptRuntime) throws -> Self
30
- {
31
- guard let appContext = AppContext.from(runtime: runtime) else {
32
- throw Exceptions.AppContextNotFound()
33
- }
34
- return try from(object: value.asObject(), appContext: appContext)
35
- }
36
-
37
28
  @JavaScriptActor
38
29
  @inlinable
39
30
  public static func decode(_ value: borrowing JavaScriptUnownedValue, in runtime: borrowing JavaScriptRuntime) throws
@@ -208,7 +208,9 @@ public struct Conversions {
208
208
  case let string as NSString:
209
209
  return .representing(value: string as String, in: runtime)
210
210
  case let data as Data:
211
- return try dataToUint8Array(data, runtime: runtime)
211
+ return try JavaScriptActor.assumeIsolated {
212
+ return try Data.encode(data, in: runtime)
213
+ }
212
214
  case let array as NSArray:
213
215
  let jsArray = runtime.createArray(length: array.count)
214
216
  for (index, element) in array.enumerated() {
@@ -228,20 +230,6 @@ public struct Conversions {
228
230
  }
229
231
  }
230
232
 
231
- /**
232
- Copies the given `Data` into a new JS `ArrayBuffer` and wraps it in a `Uint8Array`.
233
- */
234
- private static func dataToUint8Array(_ data: Data, runtime: JavaScriptRuntime) throws -> JavaScriptValue {
235
- let arrayBuffer = runtime.createArrayBuffer(size: data.count)
236
- data.withUnsafeBytes { rawBuffer in
237
- if let baseAddress = rawBuffer.baseAddress, data.count > 0 {
238
- memcpy(arrayBuffer.data(), baseAddress, data.count)
239
- }
240
- }
241
- let uint8ArrayCtor = try runtime.global().getPropertyAsFunction("Uint8Array")
242
- return try uint8ArrayCtor.callAsConstructor(arrayBuffer.asValue().ref())
243
- }
244
-
245
233
  // MARK: - Exceptions
246
234
 
247
235
  /**
@@ -86,15 +86,9 @@ internal struct DynamicSharedObjectType: AnyDynamicType {
86
86
  }
87
87
  }
88
88
 
89
- private func getBaseSharedType(_ appContext: AppContext, nativeType: AnySharedObject.Type) throws -> JavaScriptFunction {
89
+ private func newBaseSharedObject(_ appContext: AppContext, nativeType: AnySharedObject.Type) throws -> JavaScriptObject? {
90
90
  let isSharedRef = nativeType is AnySharedRef.Type
91
91
  return try JavaScriptActor.assumeIsolated {
92
- return try isSharedRef ? appContext.runtime.getSharedRefClass() : appContext.runtime.getSharedObjectClass()
92
+ return try appContext.runtime.createBaseSharedObject(isSharedRef: isSharedRef)
93
93
  }
94
94
  }
95
-
96
- private func newBaseSharedObject(_ appContext: AppContext, nativeType: AnySharedObject.Type) throws -> JavaScriptObject? {
97
- let jsClass = try getBaseSharedType(appContext, nativeType: nativeType)
98
- let prototype = try jsClass.asObject().getPropertyAsObject("prototype")
99
- return try appContext.runtime.createObject(prototype: prototype)
100
- }
@@ -158,10 +158,15 @@ public macro SharedObject(_ name: String? = nil) =
158
158
  /// non-`private`/`fileprivate`, non-`lazy`, non-computed stored property is part of the record — no
159
159
  /// `@Field` wrapper needed — and the macro synthesizes the whole conversion surface from each
160
160
  /// property's static type: a memberwise `init`, the `from(object:appContext:)` /
161
- /// `from(dictionary:appContext:)` factories, and the `toDictionary(appContext:)` /
162
- /// `toObject(appContext:)` write side. The type is auto-conformed to `Record`; the synthesized
163
- /// methods override `Record`'s reflection-based defaults, so it stays usable anywhere a `Record`
164
- /// argument is expected.
161
+ /// `from(dictionary:appContext:)` factories, the `toDictionary(appContext:)` /
162
+ /// `toObject(appContext:)` write side, and the `JavaScriptDecodable` / `JavaScriptEncodable`
163
+ /// conversions `decode(_:in:)` and `encode(_:in:)`. The type is auto-conformed to `Record`; the
164
+ /// synthesized methods override `Record`'s reflection-based defaults, so it stays usable anywhere a
165
+ /// `Record` argument is expected.
166
+ ///
167
+ /// `decode(_:in:)` and `encode(_:in:)` use the runtime they receive and don't need an app context, so
168
+ /// the record converts on any runtime. A class with an inheritance clause doesn't get them, so it
169
+ /// converts only on a runtime that an app context prepared.
165
170
  ///
166
171
  /// Requiredness is inferred from each property: a default value makes it optional, an optional type
167
172
  /// makes it nullable and optional, and a non-optional property without a default is required (the
@@ -177,7 +182,7 @@ public macro SharedObject(_ name: String? = nil) =
177
182
  /// }
178
183
  @attached(
179
184
  member,
180
- names: named(init), named(from), named(toDictionary), named(toObject),
185
+ names: named(init), named(from), named(toDictionary), named(toObject), named(decode), named(encode),
181
186
  named(_assertTypesConformance))
182
187
  @attached(extension, conformances: Record)
183
188
  public macro Record() =
@@ -22,6 +22,20 @@ public class ExpoRuntime: JavaScriptRuntime, @unchecked Sendable {
22
22
  return try getCoreObject().getPropertyAsFunction("SharedRef")
23
23
  }
24
24
 
25
+ /// Creates an instance of the base `SharedObject` class, or of the base `SharedRef` class if `isSharedRef` is true.
26
+ /// The prototypes of both classes are looked up once per runtime and cached.
27
+ @JavaScriptActor
28
+ internal func createBaseSharedObject(isSharedRef: Bool) throws -> JavaScriptObject {
29
+ let key = isSharedRef ? sharedRefPrototypeKey : sharedObjectPrototypeKey
30
+ let makePrototype = {
31
+ let baseClass = try isSharedRef ? self.getSharedRefClass() : self.getSharedObjectClass()
32
+ return try baseClass.asObject().getPropertyAsObject("prototype")
33
+ }
34
+ return try withCached(key, makePrototype) { (prototype: borrowing JavaScriptObject) in
35
+ return createObject(prototype: prototype)
36
+ }
37
+ }
38
+
25
39
  @JavaScriptActor
26
40
  internal func createNativeModuleObject() throws -> JavaScriptObject {
27
41
  return try getCoreObject()
@@ -42,3 +56,7 @@ public class ExpoRuntime: JavaScriptRuntime, @unchecked Sendable {
42
56
  return try createClass(name: name, inheriting: sharedRefBaseClass, constructor)
43
57
  }
44
58
  }
59
+
60
+ /// Keys of the base `SharedObject` and `SharedRef` prototypes in each runtime's cache.
61
+ private let sharedObjectPrototypeKey = JavaScriptRuntime.Cache.Key<JavaScriptObject.Ref>()
62
+ private let sharedRefPrototypeKey = JavaScriptRuntime.Cache.Key<JavaScriptObject.Ref>()
@@ -8,7 +8,8 @@
8
8
  #include <memory>
9
9
  #include <vector>
10
10
  #include <worklets/Compat/StableApi.h>
11
- #include <worklets/SharedItems/Serializable.h>
11
+ #include <worklets/SharedItems/Serializable/Serializable.h>
12
+ #include <worklets/SharedItems/Serializable/SerializableWorklet.h>
12
13
  #include <worklets/WorkletRuntime/WorkletRuntime.h>
13
14
 
14
15
  namespace jsi = facebook::jsi;
@@ -233,8 +234,8 @@ static jsi::Value callWorklet(jsi::Runtime &rt, std::shared_ptr<worklets::Serial
233
234
  return;
234
235
  }
235
236
 
236
- workletRuntime->runSync([worklet, arguments](jsi::Runtime &rt) -> jsi::Value {
237
- return callWorklet(rt, worklet, arguments);
237
+ workletRuntime->runSync([worklet, arguments](jsi::Runtime &rt) {
238
+ callWorklet(rt, worklet, arguments);
238
239
  });
239
240
  }
240
241
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "expo-modules-core",
3
- "version": "58.0.21",
3
+ "version": "58.0.23",
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.14",
70
- "expo-modules-macros": "0.20.0",
69
+ "expo-modules-jsi": "~58.0.16",
70
+ "expo-modules-macros": "0.21.0",
71
71
  "invariant": "^2.2.4"
72
72
  },
73
73
  "devDependencies": {