expo-modules-core 58.0.17 → 58.0.19

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 (53) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/ExpoModulesCore.podspec +10 -1
  3. package/android/build.gradle +3 -3
  4. package/android/prebuilt/metadata.json +2 -2
  5. package/android/prebuilt/native-libs.tar.xz +0 -0
  6. package/android/src/main/java/expo/modules/v2/ExpoModulesV2Host.kt +4 -0
  7. package/build/ts-declarations/global.d.ts +0 -1
  8. package/common/cpp/EventEmitter.cpp +8 -4
  9. package/expo-module-gradle-plugin/build.gradle.kts +1 -1
  10. package/ios/Api/Factories/ClassFactories.swift +1 -1
  11. package/ios/Benchmarks/Benchmarks.swift +15 -0
  12. package/ios/Benchmarks/EventEmitterBenchmarks.swift +140 -0
  13. package/ios/Benchmarks/FunctionBenchmarks.swift +112 -0
  14. package/ios/Benchmarks/SharedObjectBenchmarks.swift +80 -0
  15. package/ios/Core/AppContext.swift +22 -2
  16. package/ios/Core/ArrayBuffers/ArrayBuffer.swift +8 -3
  17. package/ios/Core/Classes/ClassDefinition.swift +15 -5
  18. package/ios/Core/EventListener.swift +4 -0
  19. package/ios/Core/Events/EventEmitter.swift +56 -12
  20. package/ios/Core/Events/EventObservingDefinition.swift +4 -0
  21. package/ios/Core/Functions/AsyncFunctionDefinition.swift +4 -0
  22. package/ios/Core/Functions/ConcurrentFunctionDefinition.swift +4 -0
  23. package/ios/Core/Functions/OptimizedAsyncFunctionDefinition.swift +4 -0
  24. package/ios/Core/Functions/OptimizedSyncFunctionDefinition.swift +4 -0
  25. package/ios/Core/Functions/StaticFunctionDefinition.swift +12 -16
  26. package/ios/Core/Functions/SyncFunctionDefinition.swift +4 -0
  27. package/ios/Core/ModuleHolder.swift +1 -0
  28. package/ios/Core/ModuleRegistry.swift +2 -2
  29. package/ios/Core/Modules/Module.swift +7 -5
  30. package/ios/Core/Modules/ModuleDefinition.swift +43 -16
  31. package/ios/Core/Objects/ConstantDefinition.swift +4 -0
  32. package/ios/Core/Objects/ObjectDefinition.swift +35 -31
  33. package/ios/Core/Objects/PropertyDefinition.swift +4 -0
  34. package/ios/Core/Protocols/AnyDefinition.swift +43 -1
  35. package/ios/Core/Protocols/AnyModule.swift +1 -1
  36. package/ios/Core/TypeName.swift +12 -0
  37. package/ios/Core/TypedArrays/ConcreteTypedArrays.swift +58 -46
  38. package/ios/Core/TypedArrays/TypedArray.swift +11 -5
  39. package/ios/Core/Views/ConcreteViewProp.swift +4 -0
  40. package/ios/Core/Views/SwiftUI/SwiftUIViewDefinition.swift +1 -1
  41. package/ios/Core/Views/ViewDefinition.swift +36 -15
  42. package/ios/Core/Views/ViewLifecycleMethod.swift +4 -0
  43. package/ios/JS/EXJSUtils.h +16 -0
  44. package/ios/JS/EXJSUtils.mm +13 -4
  45. package/ios/Utilities/Utilities.swift +0 -14
  46. package/package.json +4 -4
  47. package/prebuilds/output/debug/xcframeworks/ExpoModulesCore.tar.gz +0 -0
  48. package/prebuilds/output/debug/xcframeworks/ExpoModulesWorklets.tar.gz +0 -0
  49. package/prebuilds/output/headers/xcframeworks/ExpoModulesCoreHeaders.tar.gz +0 -0
  50. package/prebuilds/output/release/xcframeworks/ExpoModulesCore.tar.gz +0 -0
  51. package/prebuilds/output/release/xcframeworks/ExpoModulesWorklets.tar.gz +0 -0
  52. package/spm.config.json +9 -1
  53. package/src/ts-declarations/global.ts +0 -1
@@ -28,7 +28,7 @@ public extension EventEmitter {
28
28
  Schedules an event with the given name to be emitted to the associated JavaScript object.
29
29
  */
30
30
  func emit(event: String) {
31
- emit(event: event, payload: JavaScriptValue.undefined())
31
+ emit(event: event, payload: JavaScriptUndefined())
32
32
  }
33
33
 
34
34
  /**
@@ -45,10 +45,10 @@ public extension EventEmitter {
45
45
  let payload = payload.ref()
46
46
  // The emitter is not necessarily `Sendable` - some modules hold non-sendable state — so we can't let
47
47
  // the compiler send `self` into the `@JavaScriptActor` region. Wrapping it in a weak, `@unchecked
48
- // Sendable` box is safe here because the scheduled closure only calls `withEventTarget`, which touches
48
+ // Sendable` reference is safe here because the scheduled closure only calls `withEventTarget`, which touches
49
49
  // `@JavaScriptActor`-isolated or `Sendable` state (the JS object, the registry, `appContext`) and the
50
50
  // emitter's identity - never the module's own mutable state.
51
- let emitter = NonisolatedUnsafeWeakVar(self)
51
+ let emitter = WeakEventEmitterReference(value: self)
52
52
 
53
53
  runtime.schedule {
54
54
  guard let emitter = emitter.value else {
@@ -71,15 +71,51 @@ public extension EventEmitter {
71
71
  /**
72
72
  Schedules an event with the given name and payload to be emitted to the associated JavaScript object.
73
73
  */
74
+ func emit<P: JavaScriptEncodable>(event: String, payload: sending P) {
75
+ guard let appContext, let runtime = try? appContext.runtime else {
76
+ log.warn("Trying to send event '\(event)' to \(type(of: self)), but the JS runtime has been lost")
77
+ return
78
+ }
79
+ // See the note in `emit(event:payload:)` above - the emitter is captured in a weak, `@unchecked
80
+ // Sendable` reference because it isn't necessarily `Sendable`, and the scheduled closure only reaches
81
+ // `@JavaScriptActor`-isolated or `Sendable` state through it.
82
+ let emitter = WeakEventEmitterReference(value: self)
83
+
84
+ runtime.schedule {
85
+ guard let emitter = emitter.value else {
86
+ return
87
+ }
88
+ let jsPayload: JavaScriptValue
89
+ do {
90
+ jsPayload = try P.encode(payload, in: runtime)
91
+ } catch {
92
+ log.warn("Failed to convert payload for event '\(event)' on \(P.self); the event will not be emitted: \(error)")
93
+ return
94
+ }
95
+ let dispatched = emitter.withEventTarget { target in
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
+ }
102
+ }
103
+ }
104
+
105
+ /**
106
+ Schedules an event with the given name and payload to be emitted to the associated JavaScript object.
107
+ Payloads that are `JavaScriptEncodable` use the overload above, which converts them statically.
108
+ */
109
+ @_disfavoredOverload
74
110
  func emit<P: AnyArgument>(event: String, payload: sending P) {
75
111
  guard let appContext, let runtime = try? appContext.runtime else {
76
112
  log.warn("Trying to send event '\(event)' to \(type(of: self)), but the JS runtime has been lost")
77
113
  return
78
114
  }
79
115
  // See the note in `emit(event:payload:)` above - the emitter is captured in a weak, `@unchecked
80
- // Sendable` box because it isn't necessarily `Sendable`, and the scheduled closure only reaches
116
+ // Sendable` reference because it isn't necessarily `Sendable`, and the scheduled closure only reaches
81
117
  // `@JavaScriptActor`-isolated or `Sendable` state through it.
82
- let emitter = NonisolatedUnsafeWeakVar(self)
118
+ let emitter = WeakEventEmitterReference(value: self)
83
119
 
84
120
  runtime.schedule { [weak appContext] in
85
121
  guard let emitter = emitter.value, let appContext else {
@@ -103,6 +139,12 @@ public extension EventEmitter {
103
139
  }
104
140
  }
105
141
 
142
+ /// 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
+ private struct WeakEventEmitterReference: @unchecked Sendable {
145
+ weak var value: (any EventEmitter)?
146
+ }
147
+
106
148
  /**
107
149
  Sends a pre-converted event payload to the given JavaScript object via the JSI emitter helper.
108
150
  Must run on the JS thread; the public `emit` overloads schedule onto the runtime before calling in.
@@ -112,13 +154,15 @@ internal func dispatchEvent(event: String, payload: borrowing JavaScriptValue, t
112
154
  runtime.withUnsafePointee { runtimePtr in
113
155
  target.withUnsafePointee { objectPtr in
114
156
  payload.withUnsafePointee { payloadPtr in
115
- JSUtils.emitEvent(
116
- event,
117
- runtimePointer: runtimePtr,
118
- objectPointer: objectPtr,
119
- argumentsPointer: payloadPtr,
120
- argumentCount: 1
121
- )
157
+ event.withCString { eventName in
158
+ emitJSEvent(
159
+ utf8Name: eventName,
160
+ runtimePointer: runtimePtr,
161
+ objectPointer: objectPtr,
162
+ argumentsPointer: payloadPtr,
163
+ argumentCount: 1
164
+ )
165
+ }
122
166
  }
123
167
  }
124
168
  }
@@ -16,6 +16,10 @@ internal protocol AnyEventObservingDefinition: AnyDefinition, Sendable {
16
16
  }
17
17
 
18
18
  public final class EventObservingDefinition: AnyEventObservingDefinition, @unchecked Sendable {
19
+ public var _definitionClassification: DefinitionClassification {
20
+ return DefinitionClassification(kind: .eventObserver(self))
21
+ }
22
+
19
23
  public typealias ClosureType = () -> Void
20
24
 
21
25
  let type: EventObservingType
@@ -23,6 +23,10 @@ private let defaultQueue = DispatchQueue(label: "expo.modules.AsyncFunctionQueue
23
23
  Represents a function that can only be called asynchronously, thus its JavaScript equivalent returns a Promise.
24
24
  */
25
25
  public class AsyncFunctionDefinition<Args, FirstArgType, ReturnType>: AnyAsyncFunctionDefinition, @unchecked Sendable {
26
+ public var _definitionClassification: DefinitionClassification {
27
+ return DefinitionClassification(kind: .function(self))
28
+ }
29
+
26
30
  typealias ClosureType = (Args) throws -> ReturnType
27
31
 
28
32
  /**
@@ -17,6 +17,10 @@ internal protocol AnyConcurrentFunctionDefinition: AnyFunctionDefinition {
17
17
  As opposed to `AsyncFunctionDefinition`, it can leverage the new Swift's concurrency model and take the async/await closure.
18
18
  */
19
19
  public class ConcurrentFunctionDefinition<Args, FirstArgType, ReturnType>: AnyConcurrentFunctionDefinition, @unchecked Sendable {
20
+ public var _definitionClassification: DefinitionClassification {
21
+ return DefinitionClassification(kind: .function(self))
22
+ }
23
+
20
24
  typealias ClosureType = @Sendable (Args) async throws -> sending ReturnType
21
25
 
22
26
  let body: ClosureType
@@ -9,6 +9,10 @@ import ExpoModulesJSI
9
9
  The async variant creates a JS Promise and dispatches the block invocation to a background queue.
10
10
  */
11
11
  public struct OptimizedAsyncFunctionDefinition: AnyAsyncFunctionDefinition, @unchecked Sendable {
12
+ public var _definitionClassification: DefinitionClassification {
13
+ return DefinitionClassification(kind: .function(self))
14
+ }
15
+
12
16
  public let name: String
13
17
  public let typeEncoding: String
14
18
  public let argsCount: Int
@@ -7,6 +7,10 @@ import ExpoModulesJSI
7
7
  This is used by the `@OptimizedFunction` macro with specific type signatures.
8
8
  */
9
9
  public struct OptimizedSyncFunctionDefinition: AnySyncFunctionDefinition, @unchecked Sendable {
10
+ public var _definitionClassification: DefinitionClassification {
11
+ return DefinitionClassification(kind: .function(self))
12
+ }
13
+
10
14
  public let name: String
11
15
  public let typeEncoding: String
12
16
  public let argsCount: Int
@@ -1,35 +1,31 @@
1
1
  // Copyright 2025-present 650 Industries. All rights reserved.
2
2
 
3
- /**
4
- A protocol for static any type-erased function.
5
- */
6
- internal protocol AnyStaticFunctionDefinition: AnyFunctionDefinition {
7
- /**
8
- Indicates whether the function is static.
9
- */
10
- var isStatic: Bool { get }
11
- }
12
-
13
3
  /**
14
4
  Represents a static function that can only be called synchronously.
15
5
  */
16
6
  public final class StaticSyncFunctionDefinition<Args, FirstArgType, ReturnType>:
17
- SyncFunctionDefinition<Args, FirstArgType, ReturnType>, AnyStaticFunctionDefinition, @unchecked Sendable {
18
- let isStatic = true
7
+ SyncFunctionDefinition<Args, FirstArgType, ReturnType>, @unchecked Sendable {
8
+ public override var _definitionClassification: DefinitionClassification {
9
+ return DefinitionClassification(kind: .staticFunction(self))
10
+ }
19
11
  }
20
12
 
21
13
  /**
22
14
  Represents a static function that can only be called asynchronously, thus its JavaScript equivalent returns a Promise.
23
15
  */
24
16
  public final class StaticAsyncFunctionDefinition<Args, FirstArgType, ReturnType>:
25
- AsyncFunctionDefinition<Args, FirstArgType, ReturnType>, AnyStaticFunctionDefinition, @unchecked Sendable {
26
- let isStatic = true
17
+ AsyncFunctionDefinition<Args, FirstArgType, ReturnType>, @unchecked Sendable {
18
+ public override var _definitionClassification: DefinitionClassification {
19
+ return DefinitionClassification(kind: .staticFunction(self))
20
+ }
27
21
  }
28
22
 
29
23
  /**
30
24
  Represents a static function that can only be called asynchronously, thus its JavaScript equivalent returns a Promise.
31
25
  */
32
26
  public final class StaticConcurrentFunctionDefinition<Args, FirstArgType, ReturnType>:
33
- ConcurrentFunctionDefinition<Args, FirstArgType, ReturnType>, AnyStaticFunctionDefinition, @unchecked Sendable {
34
- let isStatic = true
27
+ ConcurrentFunctionDefinition<Args, FirstArgType, ReturnType>, @unchecked Sendable {
28
+ public override var _definitionClassification: DefinitionClassification {
29
+ return DefinitionClassification(kind: .staticFunction(self))
30
+ }
35
31
  }
@@ -34,6 +34,10 @@ internal protocol AnySyncFunctionDefinition: AnyFunctionDefinition, ~Copyable {
34
34
  Represents a function that can only be called synchronously.
35
35
  */
36
36
  public class SyncFunctionDefinition<Args, FirstArgType, ReturnType>: AnySyncFunctionDefinition, @unchecked Sendable {
37
+ public var _definitionClassification: DefinitionClassification {
38
+ return DefinitionClassification(kind: .function(self))
39
+ }
40
+
37
41
  typealias ClosureType = (Args) throws -> ReturnType
38
42
 
39
43
  /**
@@ -53,6 +53,7 @@ public final class ModuleHolder {
53
53
  self._name = name
54
54
  self.module = module
55
55
  self.definition = ModuleHolder.buildDefinition(for: module)
56
+ (module as? BaseModule)?.holder = self
56
57
  post(event: .moduleCreate)
57
58
  }
58
59
 
@@ -31,8 +31,8 @@ public final class ModuleRegistry: Sequence {
31
31
  return true
32
32
  }
33
33
  if didRegister {
34
- // Call the `didCreate` lifecycle hook only after the holder is in the registry, so the module
35
- // can already look itself up (e.g. through `withEventTarget`). Rejected registrations don't get it.
34
+ // Call the `didCreate` lifecycle hook only after the holder is in the registry, so lookups such as
35
+ // `get(moduleWithName:)` can already find the module. Rejected registrations don't get it.
36
36
  holder.module.didCreate()
37
37
  }
38
38
  }
@@ -8,6 +8,10 @@ import ExpoModulesJSI
8
8
  open class BaseModule {
9
9
  public private(set) weak var appContext: AppContext?
10
10
 
11
+ /// The holder that registered this module. Emitting events reaches the module's JavaScript object
12
+ /// through it, without searching the module registry.
13
+ internal weak var holder: ModuleHolder?
14
+
11
15
  @available(
12
16
  *, unavailable,
13
17
  message: "Module's initializer cannot be overridden, override the \"didCreate\" lifecycle hook instead."
@@ -51,16 +55,14 @@ open class BaseModule {
51
55
 
52
56
  extension BaseModule: EventEmitter {
53
57
  /**
54
- Lends the module's JavaScript object, looked up from the module registry, for the duration of `body`.
58
+ Lends the module's JavaScript object for the duration of `body`.
55
59
  */
56
60
  @JavaScriptActor
57
61
  public func withEventTarget<R>(_ body: (borrowing JavaScriptObject) throws -> R) rethrows -> R? {
58
- guard let appContext else {
62
+ guard let holder else {
59
63
  return nil
60
64
  }
61
- let holder = appContext.moduleRegistry.first { $0.module === self }
62
-
63
- return try holder?.withEventTarget(body) ?? nil
65
+ return try holder.withEventTarget(body)
64
66
  }
65
67
  }
66
68
 
@@ -40,26 +40,41 @@ public final class ModuleDefinition: ObjectDefinition {
40
40
  override init(definitions: [AnyDefinition]) {
41
41
  self.rawDefinitions = definitions
42
42
 
43
- self.name = definitions
44
- .compactMap { $0 as? ModuleNameDefinition }
45
- .last?
46
- .name ?? ""
47
-
48
- self.eventListeners = definitions.compactMap { $0 as? EventListener }
43
+ var name: String?
44
+ var eventListeners = [EventListener]()
45
+ var viewDefinitions = [AnyViewDefinition]()
46
+ var eventNames = [String]()
47
+ var eventObservers = [AnyEventObservingDefinition]()
48
+
49
+ for definition in definitions {
50
+ switch definition._definitionClassification.kind {
51
+ case .moduleName(let nameDefinition):
52
+ name = nameDefinition.name
53
+ case .eventListener(let listener):
54
+ eventListeners.append(listener)
55
+ case .view(let view):
56
+ viewDefinitions.append(view)
57
+ case .events(let events):
58
+ eventNames.append(contentsOf: events.names)
59
+ case .eventObserver(let observer):
60
+ eventObservers.append(observer)
61
+ case .unknown:
62
+ // `AnyViewDefinition` is the only element protocol collected here that a type from another module can conform to.
63
+ if let view = definition as? AnyViewDefinition {
64
+ viewDefinitions.append(view)
65
+ }
66
+ case .function, .staticFunction, .legacyConstants, .constant, .property, .klass, .viewProp, .viewName, .viewLifecycle:
67
+ break
68
+ }
69
+ }
49
70
 
50
- let viewDefinitions: [AnyViewDefinition] = definitions
51
- .compactMap { $0 as? AnyViewDefinition }
71
+ self.name = name ?? ""
72
+ self.eventListeners = eventListeners
52
73
  var viewsDict = Dictionary(uniqueKeysWithValues: viewDefinitions.map { ($0.name, $0) })
53
74
  viewsDict[DEFAULT_MODULE_VIEW] = viewDefinitions.first
54
75
  self.views = viewsDict
55
- self.eventNames = Array(
56
- definitions
57
- .compactMap { ($0 as? EventsDefinition)?.names }
58
- .joined()
59
- )
60
-
61
- self.eventObservers = definitions
62
- .compactMap { $0 as? AnyEventObservingDefinition }
76
+ self.eventNames = eventNames
77
+ self.eventObservers = eventObservers
63
78
 
64
79
  super.init(definitions: definitions)
65
80
  }
@@ -109,6 +124,10 @@ public final class ModuleDefinition: ObjectDefinition {
109
124
  Module's name definition. Returned by `name()` in module's definition.
110
125
  */
111
126
  internal struct ModuleNameDefinition: AnyDefinition {
127
+ var _definitionClassification: DefinitionClassification {
128
+ return DefinitionClassification(kind: .moduleName(self))
129
+ }
130
+
112
131
  let name: String
113
132
  }
114
133
 
@@ -116,6 +135,10 @@ internal struct ModuleNameDefinition: AnyDefinition {
116
135
  A definition for module's constants. Returned by `constants(() -> SomeType)` in module's definition.
117
136
  */
118
137
  internal struct ConstantsDefinition: AnyDefinition {
138
+ var _definitionClassification: DefinitionClassification {
139
+ return DefinitionClassification(kind: .legacyConstants(self))
140
+ }
141
+
119
142
  let body: () -> [String: Any?]
120
143
  }
121
144
 
@@ -123,5 +146,9 @@ internal struct ConstantsDefinition: AnyDefinition {
123
146
  A definition for module's events that can be sent to JavaScript.
124
147
  */
125
148
  public struct EventsDefinition: AnyDefinition {
149
+ public var _definitionClassification: DefinitionClassification {
150
+ return DefinitionClassification(kind: .events(self))
151
+ }
152
+
126
153
  let names: [String]
127
154
  }
@@ -21,6 +21,10 @@ protocol AnyConstantDefinition {
21
21
  }
22
22
 
23
23
  public final class ConstantDefinition<ReturnType: AnyArgument>: AnyDefinition, AnyConstantDefinition {
24
+ public var _definitionClassification: DefinitionClassification {
25
+ return DefinitionClassification(kind: .constant(self))
26
+ }
27
+
24
28
  typealias ClosureType = () throws -> ReturnType
25
29
 
26
30
  /**
@@ -6,6 +6,10 @@ import ExpoModulesJSI
6
6
  Base class for other definitions representing an object, such as `ModuleDefinition`.
7
7
  */
8
8
  public class ObjectDefinition: AnyDefinition, JavaScriptObjectBuilder {
9
+ public var _definitionClassification: DefinitionClassification {
10
+ return .unknown
11
+ }
12
+
9
13
  /**
10
14
  A dictionary of functions defined by the object.
11
15
  */
@@ -40,39 +44,39 @@ public class ObjectDefinition: AnyDefinition, JavaScriptObjectBuilder {
40
44
  Default initializer receiving children definitions from the result builder.
41
45
  */
42
46
  init(definitions: [AnyDefinition]) {
43
- self.functions = definitions
44
- .compactMap { $0 as? AnyFunctionDefinition }
45
- .filter { !($0 is AnyStaticFunctionDefinition) }
46
- .reduce(into: [String: AnyFunctionDefinition]()) { dict, function in
47
- dict[function.name] = function
48
- }
49
-
50
- self.staticFunctions = definitions
51
- .compactMap { $0 as? AnyStaticFunctionDefinition }
52
- .reduce(into: [String: AnyFunctionDefinition]()) { dict, function in
53
- dict[function.name] = function
54
- }
55
-
56
- self.legacyConstants = definitions
57
- .compactMap { $0 as? ConstantsDefinition }
58
-
59
- self.constants = definitions
60
- .compactMap { $0 as? AnyConstantDefinition }
61
- .reduce(into: [String: AnyConstantDefinition]()) { dict, constant in
62
- dict[constant.name] = constant
63
- }
64
-
65
- self.properties = definitions
66
- .compactMap { $0 as? AnyPropertyDefinition }
67
- .reduce(into: [String: AnyPropertyDefinition]()) { dict, property in
68
- dict[property.name] = property
47
+ var functions = [String: AnyFunctionDefinition]()
48
+ var staticFunctions = [String: AnyFunctionDefinition]()
49
+ var legacyConstants = [ConstantsDefinition]()
50
+ var constants = [String: AnyConstantDefinition]()
51
+ var properties = [String: AnyPropertyDefinition]()
52
+ var classes = [String: ClassDefinition]()
53
+
54
+ for definition in definitions {
55
+ switch definition._definitionClassification.kind {
56
+ case .function(let function):
57
+ functions[function.name] = function
58
+ case .staticFunction(let function):
59
+ staticFunctions[function.name] = function
60
+ case .legacyConstants(let constantsDefinition):
61
+ legacyConstants.append(constantsDefinition)
62
+ case .constant(let constant):
63
+ constants[constant.name] = constant
64
+ case .property(let property):
65
+ properties[property.name] = property
66
+ case .klass(let klass):
67
+ classes[klass.name] = klass
68
+ case .unknown, .moduleName, .eventListener, .events, .eventObserver, .view, .viewProp, .viewName, .viewLifecycle:
69
+ // Every protocol collected here is internal, so an unclassified element from another module can't match any of them.
70
+ break
69
71
  }
72
+ }
70
73
 
71
- self.classes = definitions
72
- .compactMap { $0 as? ClassDefinition }
73
- .reduce(into: [String: ClassDefinition]()) { dict, klass in
74
- dict[klass.name] = klass
75
- }
74
+ self.functions = functions
75
+ self.staticFunctions = staticFunctions
76
+ self.legacyConstants = legacyConstants
77
+ self.constants = constants
78
+ self.properties = properties
79
+ self.classes = classes
76
80
  }
77
81
 
78
82
  /**
@@ -23,6 +23,10 @@ protocol AnyPropertyDefinition {
23
23
  }
24
24
 
25
25
  public final class PropertyDefinition<OwnerType>: AnyDefinition, AnyPropertyDefinition, @unchecked Sendable {
26
+ public var _definitionClassification: DefinitionClassification {
27
+ return DefinitionClassification(kind: .property(self))
28
+ }
29
+
26
30
  /**
27
31
  Name of the property.
28
32
  */
@@ -1,4 +1,46 @@
1
1
  /**
2
2
  A protocol that must be implemented to be a part of module's definition and the module definition itself.
3
3
  */
4
- public protocol AnyDefinition: ~Copyable {}
4
+ public protocol AnyDefinition: ~Copyable {
5
+ /**
6
+ The kind of definition element this is, so containers can switch on it instead of casting to each element protocol.
7
+ A protocol cast that misses the runtime cache walks every conformance record.
8
+ A type that does not override this returns `.unknown`.
9
+ */
10
+ var _definitionClassification: DefinitionClassification { get }
11
+ }
12
+
13
+ extension AnyDefinition where Self: ~Copyable {
14
+ public var _definitionClassification: DefinitionClassification { .unknown }
15
+ }
16
+
17
+ /**
18
+ The kind of a definition element together with the element itself.
19
+ Only `.unknown` is public, so a conformer outside of this module cannot classify itself.
20
+ When a kind is added, every container switch must handle it, and the `.unknown` fallbacks must stay in sync.
21
+ */
22
+ public struct DefinitionClassification {
23
+ internal enum Kind {
24
+ case unknown
25
+ case function(AnyFunctionDefinition)
26
+ case staticFunction(AnyFunctionDefinition)
27
+ case legacyConstants(ConstantsDefinition)
28
+ case constant(AnyConstantDefinition)
29
+ case property(AnyPropertyDefinition)
30
+ case klass(ClassDefinition)
31
+ case moduleName(ModuleNameDefinition)
32
+ case eventListener(EventListener)
33
+ case events(EventsDefinition)
34
+ case eventObserver(AnyEventObservingDefinition)
35
+ case view(AnyViewDefinition)
36
+ case viewProp(AnyViewProp)
37
+ case viewName(ViewNameDefinition)
38
+ case viewLifecycle(AnyViewLifecycleMethod)
39
+ }
40
+
41
+ internal let kind: Kind
42
+
43
+ public static var unknown: DefinitionClassification {
44
+ return DefinitionClassification(kind: .unknown)
45
+ }
46
+ }
@@ -53,7 +53,7 @@ public protocol AnyModule: AnyObject, AnyArgument {
53
53
 
54
54
  extension AnyModule {
55
55
  public static var _jsName: String {
56
- return String(describing: self)
56
+ return unqualifiedTypeName(self)
57
57
  }
58
58
 
59
59
  /// An empty definition by default, so `@ExpoModule`-macro modules that describe their whole surface
@@ -0,0 +1,12 @@
1
+ // Copyright 2026-present 650 Industries. All rights reserved.
2
+
3
+ /**
4
+ The name `String(describing: type)` gives for a metatype, without its three protocol conformance probes and the `Mirror`.
5
+ It takes an opaque `Any.Type` and is never inlined on purpose.
6
+ With a statically known metatype the optimizer folds `_typeName` into a constant that keeps the file discriminator
7
+ of a `private` type, e.g. `(Foo in _ABC123…)`, where `String(describing:)` yields `Foo`.
8
+ */
9
+ @inline(never)
10
+ internal func unqualifiedTypeName(_ type: Any.Type) -> String {
11
+ return _typeName(type, qualified: false)
12
+ }