@nstudio/nativescript-compiler 9.2.0-alpha.14 → 9.2.0-alpha.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (90) hide show
  1. package/dist/driver/compile.js +4 -3
  2. package/dist/kotlin/arguments.js +2 -1
  3. package/dist/kotlin/builtins.js +14 -3
  4. package/dist/kotlin/class-accessors.js +1 -1
  5. package/dist/kotlin/class-constructor.js +3 -1
  6. package/dist/kotlin/class-declaration.js +3 -2
  7. package/dist/kotlin/class-methods.js +29 -12
  8. package/dist/kotlin/components.js +39 -14
  9. package/dist/kotlin/construction.js +4 -5
  10. package/dist/kotlin/conversion.js +16 -3
  11. package/dist/kotlin/core-api.js +14 -7
  12. package/dist/kotlin/declarations.js +1 -1
  13. package/dist/kotlin/expressions.js +10 -5
  14. package/dist/kotlin/modules.js +7 -3
  15. package/dist/kotlin/names.js +5 -2
  16. package/dist/kotlin/optional-chains.js +24 -5
  17. package/dist/kotlin/overrides.js +7 -2
  18. package/dist/kotlin/placement.js +5 -4
  19. package/dist/kotlin/translator.js +0 -39
  20. package/dist/kotlin/types.js +4 -5
  21. package/dist/kotlin/untyped-values.js +7 -1
  22. package/dist/program/program.js +3 -3
  23. package/dist/semantics/apis/array.js +11 -0
  24. package/dist/semantics/classes.js +24 -0
  25. package/dist/semantics/config.js +2 -0
  26. package/dist/semantics/forms/by-name.js +45 -19
  27. package/dist/semantics/globals.js +8 -2
  28. package/dist/semantics/lang.js +21 -1
  29. package/dist/semantics/ledger.js +0 -24
  30. package/dist/semantics/platform-globals.js +0 -16
  31. package/dist/semantics/refusals.js +1 -1
  32. package/dist/semantics/runtime.js +2 -0
  33. package/dist/semantics/throws.js +7 -0
  34. package/dist/semantics/worker-state.js +41 -1
  35. package/dist/swift/assignment.js +3 -0
  36. package/dist/swift/builtins.js +8 -10
  37. package/dist/swift/class-accessors.js +1 -1
  38. package/dist/swift/class-declaration.js +3 -2
  39. package/dist/swift/class-methods.js +19 -2
  40. package/dist/swift/construction.js +5 -1
  41. package/dist/swift/conversion.js +1 -1
  42. package/dist/swift/expressions.js +2 -4
  43. package/dist/swift/from-any.js +5 -0
  44. package/dist/swift/global-calls.js +8 -1
  45. package/dist/swift/object-classes.js +2 -1
  46. package/dist/swift/placement.js +6 -0
  47. package/dist/swift/protocols.js +30 -20
  48. package/dist/swift/types.js +4 -3
  49. package/dist/targets/ios/build.js +22 -7
  50. package/docs/cli/compile-ios.md +1 -1
  51. package/kit-android/generated/kotlin/org/nativescript/kit/ui_core_properties_index.kt +6 -6
  52. package/kit-android/generated/kotlin/org/nativescript/kit/ui_core_view_view_common.kt +1 -1
  53. package/kit-android/src/main/kotlin/org/nativescript/kit/runtime/Collections.kt +15 -6
  54. package/kit-android/src/main/kotlin/org/nativescript/kit/runtime/Promise.kt +1 -1
  55. package/kit-apple/Overlays/xml-free/Core/ui.core.bindable.index.swift +13 -2
  56. package/kit-apple/Sources/NativeScriptKit/Core/FunctionCells.Arity1.swift +0 -12
  57. package/kit-apple/Sources/NativeScriptKit/Core/FunctionCells.Arity2.swift +0 -39
  58. package/kit-apple/Sources/NativeScriptKit/Core/FunctionCells.Arity3Plus.swift +0 -44
  59. package/kit-apple/Sources/NativeScriptKit/Core/application.application-common.swift +19 -2
  60. package/kit-apple/Sources/NativeScriptKit/Core/color.color-common.swift +17 -0
  61. package/kit-apple/Sources/NativeScriptKit/Core/color.index.ios.swift +60 -0
  62. package/kit-apple/Sources/NativeScriptKit/Core/kit-index.json +90 -25
  63. package/kit-apple/Sources/NativeScriptKit/Core/text.text-common.swift +9 -1
  64. package/kit-apple/Sources/NativeScriptKit/Core/ui.core.bindable.index.swift +13 -2
  65. package/kit-apple/Sources/NativeScriptKit/Core/ui.core.view-base.index.swift +26 -2
  66. package/kit-apple/Sources/NativeScriptKit/Core/ui.gestures.gestures-common.swift +2 -0
  67. package/kit-apple/Sources/NativeScriptKit/Core/ui.layouts.liquid-glass-container.index.ios.swift +12 -2
  68. package/kit-apple/Sources/NativeScriptKit/Core/ui.layouts.root-layout.root-layout-common.swift +46 -6
  69. package/kit-apple/Sources/NativeScriptKit/Core/ui.list-view.index.ios.swift +37 -3
  70. package/kit-apple/Sources/NativeScriptKit/Core/ui.split-view.split-view-common.swift +12 -1
  71. package/kit-apple/Sources/NativeScriptKit/Core/ui.styling.css-selector.swift +50 -6
  72. package/kit-apple/Sources/NativeScriptKit/Core/ui.text-base.text-base-common.swift +13 -1
  73. package/kit-apple/Sources/NativeScriptKit/Runtime/Collections.swift +5 -1
  74. package/kit-apple/Sources/NativeScriptKit/Runtime/Dynamic.swift +13 -2
  75. package/kit-apple/Sources/NativeScriptKit/Runtime/Errors.swift +10 -2
  76. package/kit-apple/Sources/NativeScriptKit/Runtime/Functions.swift +57 -12
  77. package/kit-apple/Sources/NativeScriptKit/Runtime/Lowering.swift +12 -3
  78. package/kit-apple/Sources/NativeScriptKit/Runtime/Promise.swift +56 -29
  79. package/kit-apple/Sources/NativeScriptKit/Runtime/Realm.swift +92 -0
  80. package/kit-apple/Sources/NativeScriptKit/Runtime/Record.swift +25 -5
  81. package/kit-apple/Sources/NativeScriptKit/Runtime/RegExp.swift +13 -4
  82. package/kit-apple/Sources/NativeScriptKit/Runtime/RegExpEngine.swift +14 -5
  83. package/kit-apple/Sources/NativeScriptKit/Runtime/Symbol.swift +28 -6
  84. package/kit-apple/Sources/NativeScriptKit/Runtime/Threads.swift +2 -2
  85. package/kit-apple/Sources/NativeScriptKit/Runtime/Timers.swift +93 -47
  86. package/kit-apple/Sources/NativeScriptKit/Runtime/TypedArrays.swift +37 -9
  87. package/kit-apple/Sources/NativeScriptKit/Runtime/Values.swift +6 -4
  88. package/kit-apple/Sources/NativeScriptKit/Runtime/Views.swift +11 -2
  89. package/kit-apple/Sources/NativeScriptKit/Runtime/Workers.swift +680 -0
  90. package/package.json +1 -1
@@ -3,37 +3,27 @@ import Foundation
3
3
  // Promises and the microtask queue, following ECMA-262 §27.2 job for job so that the
4
4
  // interleaving of callbacks matches V8's exactly.
5
5
 
6
- /// The microtask queue (HTML "perform a microtask checkpoint", Node's `runMicrotasks`).
7
- public enum Microtasks {
8
- private static var queue: [(() -> Void)?] = []
9
- private static var head = 0
10
- private static var draining = false
11
- static var pendingRejections: [JSRejection] = []
6
+ /// Per-realm microtask queue state.
7
+ final class JSMicrotasks {
8
+ nonisolated deinit {}
9
+ private var queue: [(() -> Void)?] = []
10
+ private var head = 0
11
+ private var draining = false
12
+ var pendingRejections: [JSRejection] = []
13
+ var kept: [AnyObject] = []
14
+ private var turned = false
15
+ /// Notifies a worker host when work enters an idle queue so its loop can wake and drain.
16
+ var onEnqueue: (() -> Void)?
12
17
 
13
- /// Receives the reason of every promise still rejected without a handler when a checkpoint
14
- /// ends, as Node's `unhandledRejection` event does. The default prints
15
- /// `Uncaught (in promise) <reason>` to standard error, or gives it to `globalThis.__onUncaughtError` as
16
- /// `jsUncaughtHandler` does, and carries on (Node would exit).
17
- public static var onUnhandledRejection: (Any?) -> Void = { reason in
18
- if !jsReportedToScript(reason) { jsWriteStandardError("Uncaught (in promise) \(jsToString(reason))\n") }
18
+ func enqueue(_ job: @escaping () -> Void) {
19
+ let wasEmpty = head >= queue.count
20
+ queue.append(job)
21
+ if wasEmpty && !draining { onEnqueue?() }
19
22
  }
20
23
 
21
- public static func enqueue(_ job: @escaping () -> Void) { queue.append(job) }
22
-
23
- /// The targets of WeakRefs made in the current job, held until the checkpoint after it ends (ECMA-262 KeepDuringJob):
24
- /// under ARC a target nothing else holds would otherwise be gone before the next statement.
25
- static var kept: [AnyObject] = []
24
+ func taskRan() { turned = true }
26
25
 
27
- /// Called when a checkpoint ends after a task (`taskRan`) or a job ran: the app turning stable.
28
- public static var onStable: (() -> Void)?
29
- private static var turned = false
30
-
31
- /// A JavaScript task (an event handler, a timer callback) ran.
32
- public static func taskRan() { turned = true }
33
-
34
- /// Runs queued jobs FIFO, including jobs they queue, then reports unhandled rejections
35
- /// (and drains again if reporting queued more). A call made while draining does nothing.
36
- public static func checkpoint() {
26
+ func checkpoint() {
37
27
  guard !draining else { return }
38
28
  draining = true
39
29
  defer {
@@ -55,7 +45,7 @@ public enum Microtasks {
55
45
  queue.removeAll(keepingCapacity: true)
56
46
  head = 0
57
47
  if pendingRejections.isEmpty {
58
- guard turned, let onStable else { return }
48
+ guard turned, self === JSRealm.main.microtasks, let onStable = Microtasks.onStable else { return }
59
49
  turned = false
60
50
  onStable()
61
51
  continue
@@ -63,12 +53,49 @@ public enum Microtasks {
63
53
  let pending = pendingRejections
64
54
  pendingRejections = []
65
55
  for rejection in pending where !rejection.handled {
66
- onUnhandledRejection(rejection.reason)
56
+ Microtasks.onUnhandledRejection(rejection.reason)
67
57
  }
68
58
  }
69
59
  }
70
60
  }
71
61
 
62
+ /// The microtask queue (HTML "perform a microtask checkpoint", Node's `runMicrotasks`).
63
+ public enum Microtasks {
64
+ private static var current: JSMicrotasks { JSRealm.current().microtasks }
65
+
66
+ static var pendingRejections: [JSRejection] {
67
+ get { current.pendingRejections }
68
+ set { current.pendingRejections = newValue }
69
+ }
70
+
71
+ /// Receives the reason of every promise still rejected without a handler when a checkpoint
72
+ /// ends, as Node's `unhandledRejection` event does. The default prints
73
+ /// `Uncaught (in promise) <reason>` to standard error, or gives it to `globalThis.__onUncaughtError` as
74
+ /// `jsUncaughtHandler` does, and carries on (Node would exit).
75
+ public static var onUnhandledRejection: (Any?) -> Void = { reason in
76
+ if !jsReportedToScript(reason) { jsWriteStandardError("Uncaught (in promise) \(jsToString(reason))\n") }
77
+ }
78
+
79
+ public static func enqueue(_ job: @escaping () -> Void) { current.enqueue(job) }
80
+
81
+ /// The targets of WeakRefs made in the current job, held until the checkpoint after it ends (ECMA-262 KeepDuringJob):
82
+ /// under ARC a target nothing else holds would otherwise be gone before the next statement.
83
+ static var kept: [AnyObject] {
84
+ get { current.kept }
85
+ set { current.kept = newValue }
86
+ }
87
+
88
+ /// Called when a checkpoint ends after a task (`taskRan`) or a job ran: the app turning stable.
89
+ public static var onStable: (() -> Void)?
90
+
91
+ /// A JavaScript task (an event handler, a timer callback) ran.
92
+ public static func taskRan() { current.taskRan() }
93
+
94
+ /// Runs queued jobs FIFO, including jobs they queue, then reports unhandled rejections
95
+ /// (and drains again if reporting queued more). A call made while draining does nothing.
96
+ public static func checkpoint() { current.checkpoint() }
97
+ }
98
+
72
99
  /// A rejection HostPromiseRejectionTracker saw without a handler.
73
100
  final class JSRejection {
74
101
  nonisolated deinit {}
@@ -0,0 +1,92 @@
1
+ import Foundation
2
+
3
+ // A realm's per-thread state: its global object, timers, microtask queue and per-thread runtime scratch state.
4
+ // A worker's thread enters its own realm; every other thread runs in the app's (`main`), as all code did before workers.
5
+
6
+ /// Runs a `@MainActor` closure on a worker thread where the worker owns its own isolated `JSRealm`.
7
+ @usableFromInline nonisolated func jsRunInWorker(_ body: @escaping @MainActor () throws -> Void) throws {
8
+ let fn = unsafeBitCast(body, to: (() throws -> Void).self)
9
+ try fn()
10
+ }
11
+
12
+ /// Runs a non-throwing `@MainActor` closure on a worker thread.
13
+ @usableFromInline nonisolated func jsRunInWorker(_ body: @escaping @MainActor () -> Void) {
14
+ let fn = unsafeBitCast(body, to: (() -> Void).self)
15
+ fn()
16
+ }
17
+
18
+ /// Runs a value-returning `@MainActor` closure on a worker thread.
19
+ @usableFromInline nonisolated func jsRunInWorker<T>(_ body: @escaping @MainActor () -> T) -> T {
20
+ let fn = unsafeBitCast(body, to: (() -> T).self)
21
+ return fn()
22
+ }
23
+
24
+ /// Thread-local pointer to the current thread's `JSRealm` (nil on threads using `JSRealm.main`).
25
+ private nonisolated final class JSRealmTLS: @unchecked Sendable {
26
+ let key: pthread_key_t
27
+ init() {
28
+ var k: pthread_key_t = 0
29
+ pthread_key_create(&k, nil)
30
+ key = k
31
+ }
32
+ }
33
+
34
+ private nonisolated let jsRealmTLS = JSRealmTLS()
35
+
36
+ /// Per-realm script state: global object, event loop, microtask queue, and thread-confined scratch fields.
37
+ public final class JSRealm: @unchecked Sendable {
38
+ nonisolated deinit {}
39
+
40
+ public let global: JSObject
41
+ let loop: JSEventLoopState
42
+ let microtasks: JSMicrotasks
43
+ /// Runs a job on this realm's thread from any thread: the host installs it for the app's realm.
44
+ public nonisolated(unsafe) var post: ((@Sendable @escaping () -> Void) -> Void)?
45
+ /// Where an uncaught error in this realm goes; nil reports it as the app's.
46
+ var uncaught: ((Any?) -> Void)?
47
+
48
+ /// Per-thread cycle guard for `Array.prototype.join`.
49
+ var joining: [ObjectIdentifier] = []
50
+ /// Per-thread refused dynamic write (`jsSet`).
51
+ var refusedWrite: JSException?
52
+ /// Per-thread last RegExp subject cache.
53
+ var lastRegExpSubject: JSRegExpSubject?
54
+ /// Per-thread RegExp VM (matching runs no script, so each realm owns its own VM without locking).
55
+ let regexVM = RegexVM()
56
+
57
+ init(isMain: Bool = false) {
58
+ self.global = JSObject([("DOMException", jsDOMExceptionConstructor), ("performance", jsDateBuiltin)])
59
+ self.loop = JSEventLoopState(isMain: isMain)
60
+ self.microtasks = JSMicrotasks()
61
+ if isMain {
62
+ self.post = { job in
63
+ JSPosted.post {
64
+ job()
65
+ }
66
+ }
67
+ }
68
+ }
69
+
70
+ /// The app's main-thread realm.
71
+ public nonisolated static let main: JSRealm = {
72
+ let make: @MainActor () -> JSRealm = { JSRealm(isMain: true) }
73
+ return unsafeBitCast(make, to: (() -> JSRealm).self)()
74
+ }()
75
+
76
+ /// The realm of the calling thread: a worker's own on a worker thread, else `main`.
77
+ @inline(__always)
78
+ public nonisolated static func current() -> JSRealm {
79
+ if let raw = pthread_getspecific(jsRealmTLS.key) {
80
+ return Unmanaged<JSRealm>.fromOpaque(raw).takeUnretainedValue()
81
+ }
82
+ return main
83
+ }
84
+
85
+ nonisolated static func enter(_ realm: JSRealm) {
86
+ pthread_setspecific(jsRealmTLS.key, Unmanaged.passUnretained(realm).toOpaque())
87
+ }
88
+
89
+ nonisolated static func leave() {
90
+ pthread_setspecific(jsRealmTLS.key, nil)
91
+ }
92
+ }
@@ -6,34 +6,51 @@ import Foundation
6
6
  public final class JSRecord<Value>: JSDynamic, JSReactiveConvertible, ExpressibleByDictionaryLiteral {
7
7
  nonisolated deinit {}
8
8
  public let object: JSObject
9
+ internal var backing: JSDynamic?
9
10
 
10
11
  public init(object: JSObject = JSObject()) {
11
12
  self.object = object
13
+ self.backing = nil
12
14
  }
13
15
 
14
16
  public init(dictionaryLiteral elements: (String, Value)...) {
15
17
  self.object = JSObject()
18
+ self.backing = nil
16
19
  for (key, value) in elements { object[key] = value }
17
20
  }
18
21
 
19
22
  public init(_ entries: [(String, Value)]) {
20
23
  self.object = JSObject()
24
+ self.backing = nil
21
25
  for (key, value) in entries { object[key] = value }
22
26
  }
23
27
 
24
28
  public init(_ dictionary: [String: Value]) {
25
29
  self.object = JSObject()
30
+ self.backing = nil
26
31
  for key in dictionary.keys.sorted() { object[key] = dictionary[key] }
27
32
  }
28
33
 
29
34
  public subscript(key: String) -> Value? {
30
- get { object[key] as? Value }
31
- set { if let newValue { object[key] = newValue } else { _ = object.delete(key) } }
35
+ get {
36
+ if object.has(key) { return object[key] as? Value }
37
+ return backing?[jsKey: key] as? Value
38
+ }
39
+ set {
40
+ if let newValue {
41
+ object[key] = newValue
42
+ backing?[jsKey: key] = newValue
43
+ } else {
44
+ _ = object.delete(key)
45
+ backing?[jsKey: key] = nil
46
+ }
47
+ }
32
48
  }
33
49
 
34
50
  /// The entry of a record whose values may be undefined: nil for a missing key and a stored undefined alike.
35
51
  public subscript<Wrapped>(entry key: String) -> Wrapped? where Value == Wrapped? {
36
- (object[key] as? Value) ?? nil
52
+ if object.has(key) { return (object[key] as? Value) ?? nil }
53
+ return (backing?[jsKey: key] as? Value) ?? nil
37
54
  }
38
55
 
39
56
  @discardableResult public func delete(_ key: String) -> Bool { object.delete(key) }
@@ -46,8 +63,11 @@ public final class JSRecord<Value>: JSDynamic, JSReactiveConvertible, Expressibl
46
63
  public var jsKeys: [String] { object.jsKeys }
47
64
  public var jsClassName: String? { nil }
48
65
  public subscript(jsKey key: String) -> Any? {
49
- get { object[jsKey: key] }
50
- set { object[jsKey: key] = newValue }
66
+ get { object.has(key) ? object[jsKey: key] : backing?[jsKey: key] }
67
+ set {
68
+ object[jsKey: key] = newValue
69
+ backing?[jsKey: key] = newValue
70
+ }
51
71
  }
52
72
 
53
73
  public var jsTracker: JSTracker? {
@@ -164,11 +164,12 @@ public final class JSRegExp: JSDynamic, JSStringConvertible {
164
164
 
165
165
  public func toString() -> String { "/\(source)/\(flags)" }
166
166
 
167
- /// The matcher for every expression: matching runs no script, so it is never re-entered.
168
- private static let machine = RegexVM()
167
+ /// The matcher for every expression: per-realm so worker threads match concurrently without locking.
168
+ private static var machine: RegexVM { JSRealm.current().regexVM }
169
169
 
170
170
  /// Compiled patterns by pattern and flags: a literal evaluated in a loop makes a new RegExp each time, as
171
171
  /// JavaScript's does, without parsing it again. Bounded by emptying it when full.
172
+ private nonisolated static let cacheLock = NSLock()
172
173
  private static var cache: [String: [String: RegexProgram]] = [:]
173
174
  private static var cached = 0
174
175
  private static let cacheLimit = 256
@@ -184,7 +185,10 @@ public final class JSRegExp: JSDynamic, JSStringConvertible {
184
185
  }
185
186
 
186
187
  private static func compile(_ pattern: String, _ flags: String) throws -> RegexProgram {
187
- if let known = cache[pattern]?[flags] { return known }
188
+ cacheLock.lock()
189
+ let known = cache[pattern]?[flags]
190
+ cacheLock.unlock()
191
+ if let known { return known }
188
192
  let unicodeSets = flags.contains("v")
189
193
  let program: RegexProgram
190
194
  do {
@@ -193,12 +197,14 @@ public final class JSRegExp: JSDynamic, JSStringConvertible {
193
197
  } catch let e as RegexSyntaxError {
194
198
  throw JSException(value: JSSyntaxError("Invalid regular expression: /\(pattern)/\(flags): \(e.message)"))
195
199
  }
200
+ cacheLock.lock()
196
201
  if cached >= cacheLimit {
197
202
  cache.removeAll(keepingCapacity: true)
198
203
  cached = 0
199
204
  }
200
205
  cache[pattern, default: [:]][flags] = program
201
206
  cached += 1
207
+ cacheLock.unlock()
202
208
  return program
203
209
  }
204
210
 
@@ -329,7 +335,10 @@ final class JSRegExpSubject {
329
335
  units = Array(string.utf16)
330
336
  }
331
337
 
332
- private static var last: JSRegExpSubject?
338
+ private static var last: JSRegExpSubject? {
339
+ get { JSRealm.current().lastRegExpSubject }
340
+ set { JSRealm.current().lastRegExpSubject = newValue }
341
+ }
333
342
 
334
343
  static func of(_ s: String) -> JSRegExpSubject {
335
344
  if let last, jsSameText(last.string, s) { return last }
@@ -988,6 +988,7 @@ enum UnicodeProperties {
988
988
  }()
989
989
 
990
990
  private static var scripts: [String: [Int]] = [:]
991
+ private static let scriptsLock = JSUnfairLock()
991
992
 
992
993
  private static func codePoint(at offset: Int) -> Int {
993
994
  offset < 0xD800 ? offset : offset < 0xF800 ? offset + 0x800 : 0x10000 + (offset - 0xF800) / 2
@@ -1000,15 +1001,23 @@ enum UnicodeProperties {
1000
1001
  let words = name.split(separator: "_", omittingEmptySubsequences: false)
1001
1002
  guard name.count == 4 || words.allSatisfy({ w in w.first.map { ("A"..."Z").contains($0) } == true && w.dropFirst().allSatisfy { ("a"..."z").contains($0) } }) else { return nil }
1002
1003
  let key = (extensions ? "scx=" : "sc=") + name
1003
- if scripts[key] == nil {
1004
+ scriptsLock.lock()
1005
+ let existing = scripts[key]
1006
+ scriptsLock.unlock()
1007
+ let ranges: [Int]
1008
+ if let existing {
1009
+ ranges = existing
1010
+ } else {
1004
1011
  guard let re = try? NSRegularExpression(pattern: "\\p{\(extensions ? "Script_Extensions" : "Script")=\(name)}+") else { return nil }
1005
- var ranges: [Int] = []
1012
+ var built: [Int] = []
1006
1013
  for m in re.matches(in: everyScalar as String, range: NSRange(location: 0, length: everyScalar.length)) {
1007
- ranges += [codePoint(at: m.range.location), codePoint(at: m.range.location + m.range.length - 1)]
1014
+ built += [codePoint(at: m.range.location), codePoint(at: m.range.location + m.range.length - 1)]
1008
1015
  }
1009
- scripts[key] = ranges
1016
+ scriptsLock.lock()
1017
+ scripts[key] = built
1018
+ scriptsLock.unlock()
1019
+ ranges = built
1010
1020
  }
1011
- let ranges = scripts[key]!
1012
1021
  return { inRanges(ranges, $0) }
1013
1022
  }
1014
1023
 
@@ -9,27 +9,45 @@ public final class JSSymbol: Hashable, CustomStringConvertible {
9
9
 
10
10
  public init(_ description: String?) {
11
11
  jsDescription = description
12
+ JSSymbol.lock.lock()
12
13
  JSSymbol.count += 1
13
- key = "\u{0}@@\(JSSymbol.count)"
14
- JSSymbol.byKey[key] = self
14
+ let k = "\u{0}@@\(JSSymbol.count)"
15
+ key = k
16
+ JSSymbol.byKey[k] = self
17
+ JSSymbol.lock.unlock()
15
18
  }
16
19
 
20
+ private nonisolated static let lock = NSLock()
17
21
  private static var count = 0
18
22
  private static var byKey: [String: JSSymbol] = [:]
19
23
  private static var registry: [String: JSSymbol] = [:]
20
24
 
21
25
  /// `Symbol.for(key)`: one symbol per key, program-wide.
22
26
  public static func `for`(_ key: String) -> JSSymbol {
23
- if let s = registry[key] { return s }
27
+ lock.lock()
28
+ if let s = registry[key] {
29
+ lock.unlock()
30
+ return s
31
+ }
32
+ lock.unlock()
24
33
  let s = JSSymbol(key)
34
+ lock.lock()
35
+ if let existing = registry[key] {
36
+ lock.unlock()
37
+ return existing
38
+ }
25
39
  registry[key] = s
40
+ lock.unlock()
26
41
  return s
27
42
  }
28
43
 
29
44
  /// `Symbol.keyFor(symbol)`: the key of a registered symbol.
30
45
  public static func keyFor(_ symbol: JSSymbol) -> String? {
31
- guard let d = symbol.jsDescription, registry[d] === symbol else { return nil }
32
- return d
46
+ guard let d = symbol.jsDescription else { return nil }
47
+ lock.lock()
48
+ let match = registry[d] === symbol
49
+ lock.unlock()
50
+ return match ? d : nil
33
51
  }
34
52
 
35
53
  public static let iterator = JSSymbol("Symbol.iterator")
@@ -42,7 +60,11 @@ public final class JSSymbol: Hashable, CustomStringConvertible {
42
60
 
43
61
  /// The symbol a property key stands for.
44
62
  public static func of(key: String) -> JSSymbol? {
45
- key.utf8.first == 0 ? byKey[key] : nil
63
+ guard key.utf8.first == 0 else { return nil }
64
+ lock.lock()
65
+ let found = byKey[key]
66
+ lock.unlock()
67
+ return found
46
68
  }
47
69
 
48
70
  public func toString() -> String { "Symbol(\(jsDescription ?? ""))" }
@@ -252,7 +252,7 @@ public enum JSPosted {
252
252
  // Read and written only under `lock`.
253
253
  nonisolated(unsafe) private static var jobs: [@MainActor () -> Void] = []
254
254
  private nonisolated static let lock = NSLock()
255
- nonisolated static let arrived = DispatchSemaphore(value: 0)
255
+ public nonisolated static let arrived = DispatchSemaphore(value: 0)
256
256
 
257
257
  @usableFromInline nonisolated static func post(_ job: @escaping @MainActor () -> Void) {
258
258
  lock.lock()
@@ -264,7 +264,7 @@ public enum JSPosted {
264
264
  }
265
265
 
266
266
  /// Runs every posted job, in order; whether there was one.
267
- @discardableResult static func run() -> Bool {
267
+ @discardableResult public static func run() -> Bool {
268
268
  lock.lock()
269
269
  let ready = jobs
270
270
  jobs = []