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

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 (186) hide show
  1. package/README.md +3 -0
  2. package/dist/kit/kit-index.js +9 -0
  3. package/dist/kotlin/access.js +14 -15
  4. package/dist/kotlin/arguments.js +2 -2
  5. package/dist/kotlin/assignment.js +22 -13
  6. package/dist/kotlin/builtins.js +67 -0
  7. package/dist/kotlin/calls.js +3 -2
  8. package/dist/kotlin/class-declaration.js +16 -1
  9. package/dist/kotlin/class-methods.js +2 -2
  10. package/dist/kotlin/components.js +2 -7
  11. package/dist/kotlin/core-api.js +3 -22
  12. package/dist/kotlin/declarations.js +86 -9
  13. package/dist/kotlin/expression-statements.js +13 -1
  14. package/dist/kotlin/functions.js +2 -4
  15. package/dist/kotlin/global-calls.js +1 -4
  16. package/dist/kotlin/literals.js +3 -10
  17. package/dist/kotlin/names.js +6 -4
  18. package/dist/kotlin/native/api.js +1 -1
  19. package/dist/kotlin/native/classfiles.js +2 -13
  20. package/dist/kotlin/operators.js +36 -30
  21. package/dist/kotlin/optional-chains.js +3 -10
  22. package/dist/kotlin/overrides.js +5 -17
  23. package/dist/kotlin/placement.js +1 -12
  24. package/dist/kotlin/state-backend.js +6 -2
  25. package/dist/kotlin/statements.js +1 -13
  26. package/dist/kotlin/translator.js +3 -11
  27. package/dist/kotlin/type-tests.js +1 -1
  28. package/dist/kotlin/types.js +6 -23
  29. package/dist/program/core-facts.js +9 -0
  30. package/dist/semantics/classes.js +15 -0
  31. package/dist/semantics/facts.js +38 -2
  32. package/dist/semantics/lang.js +29 -0
  33. package/dist/semantics/lower.js +144 -0
  34. package/dist/semantics/lowering-methods.js +120 -3
  35. package/dist/semantics/marks.js +30 -0
  36. package/dist/semantics/placement.js +9 -3
  37. package/dist/semantics/refusals.js +12 -11
  38. package/dist/semantics/signals.js +11 -0
  39. package/dist/semantics/states/missing.js +7 -0
  40. package/dist/semantics/tdz.js +142 -8
  41. package/dist/semantics/throws.js +1 -1
  42. package/dist/semantics/translator-methods.js +1 -1
  43. package/dist/swift/access.js +13 -17
  44. package/dist/swift/assignment.js +30 -16
  45. package/dist/swift/calls.js +13 -5
  46. package/dist/swift/classes.js +22 -10
  47. package/dist/swift/components.js +3 -7
  48. package/dist/swift/control-flow.js +1 -1
  49. package/dist/swift/conversion.js +5 -2
  50. package/dist/swift/core-api.js +11 -31
  51. package/dist/swift/declarations.js +1 -6
  52. package/dist/swift/expression-statements.js +7 -0
  53. package/dist/swift/expressions.js +6 -2
  54. package/dist/swift/from-any.js +1 -1
  55. package/dist/swift/functions.js +5 -6
  56. package/dist/swift/global-calls.js +1 -5
  57. package/dist/swift/lenient.js +0 -7
  58. package/dist/swift/literals.js +8 -3
  59. package/dist/swift/method-values.js +1 -19
  60. package/dist/swift/modules.js +5 -3
  61. package/dist/swift/names.js +3 -12
  62. package/dist/swift/native/api.js +3 -1
  63. package/dist/swift/native/calls.js +1 -1
  64. package/dist/swift/native/dispatch.js +2 -8
  65. package/dist/swift/object-classes.js +84 -1
  66. package/dist/swift/operators.js +37 -34
  67. package/dist/swift/optional-chains.js +1 -8
  68. package/dist/swift/parameters.js +0 -4
  69. package/dist/swift/placement.js +5 -15
  70. package/dist/swift/results.js +0 -4
  71. package/dist/swift/state-backend.js +3 -2
  72. package/dist/swift/statements.js +1 -13
  73. package/dist/swift/static-calls.js +2 -3
  74. package/dist/swift/syntax.js +9 -5
  75. package/dist/swift/translator.js +1 -11
  76. package/dist/swift/type-facts.js +3 -3
  77. package/dist/swift/types.js +3 -3
  78. package/dist/targets/ios/build.js +1 -1
  79. package/dist/types/adapter-api.d.ts +3 -3
  80. package/kit-android/generated/kotlin/org/nativescript/kit/__Modules.kt +1 -0
  81. package/kit-android/generated/kotlin/org/nativescript/kit/__Objects.kt +525 -99
  82. package/kit-android/generated/kotlin/org/nativescript/kit/accessibility_accessibility_common.kt +2 -2
  83. package/kit-android/generated/kotlin/org/nativescript/kit/application_application_common.kt +29 -6
  84. package/kit-android/generated/kotlin/org/nativescript/kit/application_helpers_common.kt +14 -0
  85. package/kit-android/generated/kotlin/org/nativescript/kit/color_color_utils.kt +3 -3
  86. package/kit-android/generated/kotlin/org/nativescript/kit/css_mediaquery_index.kt +49 -1
  87. package/kit-android/generated/kotlin/org/nativescript/kit/data_observable_array_index.kt +0 -8
  88. package/kit-android/generated/kotlin/org/nativescript/kit/data_observable_index.kt +0 -16
  89. package/kit-android/generated/kotlin/org/nativescript/kit/data_virtual_array_index.kt +6 -4
  90. package/kit-android/generated/kotlin/org/nativescript/kit/http_http_request_index.kt +19 -7
  91. package/kit-android/generated/kotlin/org/nativescript/kit/http_http_request_internal_http_request_internal_common.kt +14 -0
  92. package/kit-android/generated/kotlin/org/nativescript/kit/http_http_request_internal_index.kt +19 -10
  93. package/kit-android/generated/kotlin/org/nativescript/kit/matrix_index.kt +17 -29
  94. package/kit-android/generated/kotlin/org/nativescript/kit/media_query_list_index.kt +144 -22
  95. package/kit-android/generated/kotlin/org/nativescript/kit/npm_css_what_src_parse.kt +8 -3
  96. package/kit-android/generated/kotlin/org/nativescript/kit/org.nativescript.NativeScriptLifecycleCallbacks.kt +2 -10
  97. package/kit-android/generated/kotlin/org/nativescript/kit/ui_builder_index.kt +0 -2
  98. package/kit-android/generated/kotlin/org/nativescript/kit/ui_core_bindable_expression_parser.kt +6 -10
  99. package/kit-android/generated/kotlin/org/nativescript/kit/ui_core_properties_index.kt +531 -8
  100. package/kit-android/generated/kotlin/org/nativescript/kit/ui_core_view_base_index.kt +0 -9
  101. package/kit-android/generated/kotlin/org/nativescript/kit/ui_core_weak_event_listener_index.kt +3 -3
  102. package/kit-android/generated/kotlin/org/nativescript/kit/ui_styling_style_index.kt +19 -0
  103. package/kit-android/generated/kotlin/org/nativescript/kit/ui_styling_style_properties.kt +5 -2
  104. package/kit-android/generated/kotlin/org/nativescript/kit/ui_styling_style_scope.kt +232 -174
  105. package/kit-android/generated/kotlin/org/nativescript/kit/ui_text_view_text_view_common.kt +1 -1
  106. package/kit-android/generated/kotlin/org/nativescript/kit/utils_android_runtime_version.kt +75 -0
  107. package/kit-android/generated/manifest.json +21 -18
  108. package/kit-android/generated/source-lines.json +1 -1
  109. package/kit-android/src/main/kotlin/org/nativescript/kit/runtime/Array.kt +547 -51
  110. package/kit-android/src/main/kotlin/org/nativescript/kit/runtime/Intl.kt +1 -1
  111. package/kit-android/src/main/kotlin/org/nativescript/kit/runtime/JSON.kt +52 -6
  112. package/kit-android/src/main/kotlin/org/nativescript/kit/runtime/Language.kt +33 -0
  113. package/kit-android/src/main/kotlin/org/nativescript/kit/runtime/Prototypes.kt +9 -1
  114. package/kit-android/src/main/kotlin/org/nativescript/kit/runtime/Strings.kt +32 -18
  115. package/kit-android/src/main/kotlin/org/nativescript/kit/runtime/Values.kt +137 -28
  116. package/kit-apple/Overlays/xml-free/Core/ui.builder.index.swift +1 -1
  117. package/kit-apple/Sources/NativeScriptKit/Core/FunctionCells.Arity1.swift +14 -10
  118. package/kit-apple/Sources/NativeScriptKit/Core/FunctionCells.Arity2.swift +3 -9
  119. package/kit-apple/Sources/NativeScriptKit/Core/FunctionCells.Arity3Plus.swift +20 -21
  120. package/kit-apple/Sources/NativeScriptKit/Core/Object_eventName_object_index_section_view_ios_android.swift +5 -7
  121. package/kit-apple/Sources/NativeScriptKit/Core/Object_eventName_object_ios_android_.swift +130 -0
  122. package/kit-apple/Sources/NativeScriptKit/Core/Object_eventName_object_oldIndex_newIndex.swift +2 -4
  123. package/kit-apple/Sources/NativeScriptKit/Core/Object_specified_substituted_reads.swift +115 -0
  124. package/kit-apple/Sources/NativeScriptKit/Core/Object_substituted_values_reads.swift +113 -0
  125. package/kit-apple/Sources/NativeScriptKit/Core/Object_substituted_values_reads_.swift +113 -0
  126. package/kit-apple/Sources/NativeScriptKit/Core/application.application-common.swift +348 -330
  127. package/kit-apple/Sources/NativeScriptKit/Core/application.application.ios.swift +358 -359
  128. package/kit-apple/Sources/NativeScriptKit/Core/application.helpers-common.swift +47 -29
  129. package/kit-apple/Sources/NativeScriptKit/Core/connectivity.index.ios.swift +3 -1
  130. package/kit-apple/Sources/NativeScriptKit/Core/css-mediaquery.index.swift +95 -53
  131. package/kit-apple/Sources/NativeScriptKit/Core/css.CSS3Parser.swift +1 -1
  132. package/kit-apple/Sources/NativeScriptKit/Core/css.parser.swift +1 -1
  133. package/kit-apple/Sources/NativeScriptKit/Core/data.observable-array.index.swift +2 -2
  134. package/kit-apple/Sources/NativeScriptKit/Core/file-system.file-system-access.ios.swift +52 -43
  135. package/kit-apple/Sources/NativeScriptKit/Core/http.http-request-internal.http-request-internal-common.swift +18 -0
  136. package/kit-apple/Sources/NativeScriptKit/Core/http.http-request-internal.index.ios.swift +66 -51
  137. package/kit-apple/Sources/NativeScriptKit/Core/image-source.index.ios.swift +67 -51
  138. package/kit-apple/Sources/NativeScriptKit/Core/js-libs.easysax.easysax.swift +24 -6
  139. package/kit-apple/Sources/NativeScriptKit/Core/kit-index.json +143 -32
  140. package/kit-apple/Sources/NativeScriptKit/Core/manifest.json +30 -19
  141. package/kit-apple/Sources/NativeScriptKit/Core/media-query-list.index.swift +190 -78
  142. package/kit-apple/Sources/NativeScriptKit/Core/native-window.native-window.ios.swift +1 -1
  143. package/kit-apple/Sources/NativeScriptKit/Core/npm.css-what.src.parse.swift +205 -201
  144. package/kit-apple/Sources/NativeScriptKit/Core/npm.css-what.src.stringify.swift +41 -42
  145. package/kit-apple/Sources/NativeScriptKit/Core/npm.css-what.src.types.swift +1 -1
  146. package/kit-apple/Sources/NativeScriptKit/Core/trace.index.swift +2 -2
  147. package/kit-apple/Sources/NativeScriptKit/Core/ui.animation.index.ios.swift +77 -65
  148. package/kit-apple/Sources/NativeScriptKit/Core/ui.builder.index.swift +4 -4
  149. package/kit-apple/Sources/NativeScriptKit/Core/ui.core.properties.index.swift +1272 -824
  150. package/kit-apple/Sources/NativeScriptKit/Core/ui.core.view-base.index.swift +2 -2
  151. package/kit-apple/Sources/NativeScriptKit/Core/ui.core.view.index.ios.swift +8 -8
  152. package/kit-apple/Sources/NativeScriptKit/Core/ui.core.view.view-common.swift +1 -1
  153. package/kit-apple/Sources/NativeScriptKit/Core/ui.frame.frame-common.swift +2 -1
  154. package/kit-apple/Sources/NativeScriptKit/Core/ui.frame.index.ios.swift +6 -6
  155. package/kit-apple/Sources/NativeScriptKit/Core/ui.layouts.flexbox-layout.index.ios.swift +22 -21
  156. package/kit-apple/Sources/NativeScriptKit/Core/ui.page.index.ios.swift +202 -188
  157. package/kit-apple/Sources/NativeScriptKit/Core/ui.repeater.index.swift +2 -6
  158. package/kit-apple/Sources/NativeScriptKit/Core/ui.split-view.index.ios.swift +3 -3
  159. package/kit-apple/Sources/NativeScriptKit/Core/ui.styling.background.ios.swift +2 -2
  160. package/kit-apple/Sources/NativeScriptKit/Core/ui.styling.clip-path-function.swift +1 -1
  161. package/kit-apple/Sources/NativeScriptKit/Core/ui.styling.css-animation-parser.swift +1 -1
  162. package/kit-apple/Sources/NativeScriptKit/Core/ui.styling.css-selector.swift +2 -2
  163. package/kit-apple/Sources/NativeScriptKit/Core/ui.styling.font-common.swift +2 -2
  164. package/kit-apple/Sources/NativeScriptKit/Core/ui.styling.style-scope.swift +869 -838
  165. package/kit-apple/Sources/NativeScriptKit/Core/ui.styling.style.index.swift +46 -31
  166. package/kit-apple/Sources/NativeScriptKit/Core/ui.text-base.formatted-string.swift +1 -2
  167. package/kit-apple/Sources/NativeScriptKit/Core/ui.text-view.text-view-common.swift +1 -1
  168. package/kit-apple/Sources/NativeScriptKit/Core/ui.transition.index.ios.swift +2 -2
  169. package/kit-apple/Sources/NativeScriptKit/Core/ui.transition.modal-transition.ios.swift +4 -4
  170. package/kit-apple/Sources/NativeScriptKit/Core/ui.transition.page-transition.ios.swift +2 -2
  171. package/kit-apple/Sources/NativeScriptKit/Core/ui.transition.shared-transition-helper.ios.swift +17 -17
  172. package/kit-apple/Sources/NativeScriptKit/Core/utils.android-runtime-version.swift +77 -0
  173. package/kit-apple/Sources/NativeScriptKit/Core/utils.debug-source.swift +2 -2
  174. package/kit-apple/Sources/NativeScriptKit/Core/xhr.index.swift +62 -61
  175. package/kit-apple/Sources/NativeScriptKit/Runtime/Array.swift +4 -0
  176. package/kit-apple/Sources/NativeScriptKit/Runtime/Dynamic.swift +2 -0
  177. package/kit-apple/Sources/NativeScriptKit/Runtime/Interop.swift +1 -1
  178. package/kit-apple/Sources/NativeScriptKit/Runtime/Intl.swift +1 -1
  179. package/kit-apple/Sources/NativeScriptKit/Runtime/Iterators.swift +1 -1
  180. package/kit-apple/Sources/NativeScriptKit/Runtime/JSON.swift +413 -73
  181. package/kit-apple/Sources/NativeScriptKit/Runtime/Lowering.swift +3 -2
  182. package/kit-apple/Sources/NativeScriptKit/Runtime/Prototypes.swift +14 -1
  183. package/kit-apple/Sources/NativeScriptKit/Runtime/Symbol.swift +1 -0
  184. package/kit-apple/Sources/NativeScriptKit/Runtime/Values.swift +1 -0
  185. package/kit-apple/Sources/NativeScriptKit/Runtime/Views.swift +2 -0
  186. package/package.json +2 -2
@@ -440,20 +440,66 @@ public protocol JSToJSONConvertible: AnyObject {
440
440
  func jsToJSON(_ key: String) throws -> Any?
441
441
  }
442
442
 
443
+ /// Generated typed object shapes implement this to write directly into `JSUTF8Buffer` on the fast path
444
+ /// (no replacer, no indent, no `jsOrder`/`jsExtra`/`jsNulls`/`jsUnset`, depth < 64), bypassing `jsKeys` and `subscript[jsKey:]`.
445
+ public protocol JSJSONFastSerializable: AnyObject {
446
+ var jsCanFastJSON: Bool { get }
447
+ func jsWriteJSONFast(to out: JSUTF8Buffer, writeField: (StaticString, String, Any?) throws -> Void) throws -> Bool
448
+ }
449
+
443
450
  /// What JSON.stringify does with an instance of a class, found once per class: casts to the runtime's
444
451
  /// protocols look up conformances, and a value cast to one only to fail costs as much as one that succeeds.
445
452
  private enum JSJSONClassKind {
446
- case null, plainObject, numbers, strings, values, array, match, matchIndices, toJSON, date, function, other
453
+ case null, plainObject, fastObject, numbers, strings, values, array, match, matchIndices, toJSON, date, function, other
447
454
  /// A dynamic object whose `toJSON` is looked up; an error's is not.
448
455
  case dynamic, error
449
456
  }
450
457
 
451
- /// SerializeJSONProperty over one output buffer of UTF-8.
458
+ /// SerializeJSONProperty over one output buffer of UTF-8, using an explicit heap stack so deep nesting never overflows the call stack.
452
459
  private struct JSJSONWriter {
460
+ private enum ArraySource {
461
+ case numbers(JSArray<Double>)
462
+ case strings(JSArray<String>)
463
+ case values(JSArray<Any?>)
464
+ case boxed([Any?])
465
+
466
+ func element(at i: Int) -> Any? {
467
+ switch self {
468
+ case .numbers(let array):
469
+ let d = array.storage[i]
470
+ return d.isNaN ? JSArray<Double>.untyped(d) : d
471
+ case .strings(let array):
472
+ return jsIsHoleString(array.storage[i]) ? nil : array.storage[i]
473
+ case .values(let array):
474
+ return array.storage[i]
475
+ case .boxed(let list):
476
+ return list[i]
477
+ }
478
+ }
479
+ }
480
+
481
+ private enum ObjectSource {
482
+ case plain(JSObject)
483
+ case dynamic(JSDynamic)
484
+
485
+ func value(for key: String) -> Any? {
486
+ switch self {
487
+ case .plain(let o): return o[key]
488
+ case .dynamic(let d): return d[jsKey: key]
489
+ }
490
+ }
491
+ }
492
+
493
+ private enum Frame {
494
+ case array(object: AnyObject?, source: ArraySource, count: Int, index: Int)
495
+ case object(object: AnyObject, source: ObjectSource, keys: [String], sourceKeys: [String]?, index: Int, empty: Bool)
496
+ }
497
+
453
498
  private let gap: [UInt8]
454
499
  private let output = JSUTF8Buffer()
455
500
  private var depth = 0
456
501
  private var stack: [AnyObject] = []
502
+ private var frames: [Frame] = []
457
503
  private var kinds: [ObjectIdentifier: JSJSONClassKind] = [:]
458
504
  var replacer: Any?
459
505
  var allowed: [String]?
@@ -463,8 +509,15 @@ private struct JSJSONWriter {
463
509
  var text: String { output.string }
464
510
 
465
511
  /// `value`, the property `key` of `holder`, after its `toJSON` and the replacer; false, writing nothing, when
466
- /// it serializes to undefined.
512
+ /// it serializes to undefined. Uses direct recursion below depth 64 for speed, spilling to `frames` past that
513
+ /// so arbitrarily deep structures (e.g. 5000+ nested arrays) never overflow the call stack.
467
514
  mutating func property(_ holder: AnyObject?, _ key: @autoclosure () -> String, _ value: Any?) throws -> Bool {
515
+ if depth >= 64 {
516
+ guard let resolved = try resolve(holder, key(), value) else { return false }
517
+ guard try emitOrPush(resolved) else { return false }
518
+ try drain()
519
+ return true
520
+ }
468
521
  var v = jsFlat(value)
469
522
  if let value = v, jsIsClassInstance(value) {
470
523
  let object = jsObjectReference(value)
@@ -475,25 +528,289 @@ private struct JSJSONWriter {
475
528
  if let f = jsFlat(plain["toJSON"]), jsIsFunction(f) {
476
529
  v = try jsInvoke(f, plain, [key()])
477
530
  } else if replacer == nil {
478
- try members(plain, plain.keys) { plain[$0] }
531
+ try membersDirect(plain, plain.keys) { plain[$0] }
479
532
  return true
480
533
  }
534
+ case .fastObject:
535
+ let fast = object as! JSJSONFastSerializable
536
+ if fast.jsCanFastJSON {
537
+ if replacer == nil && allowed == nil && gap.isEmpty {
538
+ if try writeFastDirect(fast) { return true }
539
+ }
540
+ if replacer == nil {
541
+ let dynamic = object as! JSDynamic
542
+ try membersDirect(dynamic, dynamic.jsKeys) { dynamic[jsKey: $0] }
543
+ return true
544
+ }
545
+ } else {
546
+ let dynamic = object as! JSDynamic
547
+ if let f = jsFlat(dynamic[jsKey: "toJSON"]), jsIsFunction(f) {
548
+ v = try jsInvoke(f, dynamic, [key()])
549
+ } else if replacer == nil {
550
+ try membersDirect(dynamic, dynamic.jsKeys) { dynamic[jsKey: $0] }
551
+ return true
552
+ }
553
+ }
481
554
  case .dynamic:
482
555
  let dynamic = object as! JSDynamic
483
556
  if let f = jsFlat(dynamic[jsKey: "toJSON"]), jsIsFunction(f) {
484
557
  v = try jsInvoke(f, dynamic, [key()])
485
558
  } else if replacer == nil {
486
- try members(dynamic, dynamic.jsKeys) { dynamic[jsKey: $0] }
559
+ try membersDirect(dynamic, dynamic.jsKeys) { dynamic[jsKey: $0] }
487
560
  return true
488
561
  }
489
562
  case .toJSON: v = try (object as! JSToJSONConvertible).jsToJSON(key())
490
563
  case .date: v = unsafeDowncast(object, to: JSDate.self).toJSON() ?? jsNull
491
- default: if replacer == nil { return try write(object: object, kind) }
564
+ default: if replacer == nil { return try writeDirect(object: object, kind) }
492
565
  }
493
566
  }
494
567
  if let replacer { v = try jsFlat(jsInvoke(replacer, holder, [key(), v])) }
495
568
  guard let v else { return false }
496
- return try write(v)
569
+ return try writeDirect(v)
570
+ }
571
+
572
+ private mutating func writeDirect(_ value: Any) throws -> Bool {
573
+ if jsIsClassInstance(value) {
574
+ let object = jsObjectReference(value)
575
+ return try writeDirect(object: object, kind(of: object))
576
+ }
577
+ let output = self.output
578
+ func scalar<T>(_ value: T) -> Bool {
579
+ if T.self == String.self { output.appendJSONString(unsafeBitCast(value, to: String.self)) }
580
+ else if T.self == Double.self { output.appendJSONNumber(unsafeBitCast(value, to: Double.self)) }
581
+ else if T.self == Bool.self { output.append(unsafeBitCast(value, to: Bool.self) ? "true" : "false") }
582
+ else { return false }
583
+ return true
584
+ }
585
+ if _openExistential(value, do: scalar) { return true }
586
+ if value is JSBigInt { throw JSException(JSTypeError("Do not know how to serialize a BigInt")) }
587
+ if let n = jsNumeric(value) {
588
+ output.appendJSONNumber(n)
589
+ } else if jsIsFunction(value) {
590
+ return false
591
+ } else if jsIsTuple(value) {
592
+ let list = Mirror(reflecting: value).children.map { jsFlat($0.value) }
593
+ try elementsDirect(nil, list.count) { list[$0] }
594
+ } else {
595
+ return try writeBridgedDirect(value)
596
+ }
597
+ return true
598
+ }
599
+
600
+ private mutating func writeDirect(object: AnyObject, _ kind: JSJSONClassKind) throws -> Bool {
601
+ switch kind {
602
+ case .null:
603
+ output.append("null")
604
+ case .plainObject:
605
+ let plain = unsafeDowncast(object, to: JSObject.self)
606
+ try membersDirect(plain, plain.keys) { plain[$0] }
607
+ case .fastObject:
608
+ if replacer == nil && allowed == nil && gap.isEmpty {
609
+ let fast = object as! JSJSONFastSerializable
610
+ if try writeFastDirect(fast) { break }
611
+ }
612
+ let dynamic = object as! JSDynamic
613
+ try membersDirect(dynamic, dynamic.jsKeys) { dynamic[jsKey: $0] }
614
+ case .dynamic, .error:
615
+ let dynamic = object as! JSDynamic
616
+ try membersDirect(dynamic, dynamic.jsKeys) { dynamic[jsKey: $0] }
617
+ case .numbers:
618
+ let array = unsafeDowncast(object, to: JSArray<Double>.self)
619
+ array.track()
620
+ if replacer == nil && gap.isEmpty && array.shared == nil && jsSideProperties(array, create: false) == nil {
621
+ output.appendJSONNumbers(array)
622
+ } else {
623
+ try elementsDirect(array, array.storage.count) {
624
+ let d = array.storage[$0]
625
+ return d.isNaN ? JSArray<Double>.untyped(d) : d
626
+ }
627
+ }
628
+ case .strings:
629
+ let array = unsafeDowncast(object, to: JSArray<String>.self)
630
+ array.track()
631
+ if replacer == nil && gap.isEmpty && array.shared == nil && jsSideProperties(array, create: false) == nil {
632
+ output.appendJSONStrings(array)
633
+ } else {
634
+ try elementsDirect(array, array.storage.count) { jsIsHoleString(array.storage[$0]) ? nil : array.storage[$0] }
635
+ }
636
+ case .values where unsafeDowncast(object, to: JSArray<Any?>.self).jsTracker == nil && unsafeDowncast(object, to: JSArray<Any?>.self).shared == nil:
637
+ let array = unsafeDowncast(object, to: JSArray<Any?>.self)
638
+ try elementsDirect(array, array.storage.count) { array.storage[$0] }
639
+ case .match:
640
+ let values = unsafeDowncast(object, to: JSMatch.self).values
641
+ try elementsDirect(values, values.storage.count) { values.storage[$0] }
642
+ case .matchIndices:
643
+ let values = unsafeDowncast(object, to: JSMatchIndices.self).values
644
+ try elementsDirect(values, values.storage.count) { values.storage[$0] }
645
+ case .values, .array:
646
+ let array = object as! JSArrayProtocol
647
+ let values = array.jsAnyElements
648
+ try elementsDirect(array, values.count) { values[$0] }
649
+ case .date:
650
+ guard let iso = unsafeDowncast(object, to: JSDate.self).toJSON() else { output.append("null"); break }
651
+ output.appendJSONString(iso)
652
+ case .function:
653
+ return false
654
+ case .toJSON, .other:
655
+ return try writeBridgedDirect(object)
656
+ }
657
+ return true
658
+ }
659
+
660
+ private mutating func writeBridgedDirect(_ value: Any) throws -> Bool {
661
+ switch value {
662
+ case let s as String: output.appendJSONString(s)
663
+ case let b as Bool: output.append(b ? "true" : "false")
664
+ case let n where jsNumeric(n) != nil: output.appendJSONNumber(jsNumeric(n)!)
665
+ case let array as JSArrayProtocol:
666
+ let values = array.jsAnyElements
667
+ try elementsDirect(array, values.count) { values[$0] }
668
+ case let dynamic as JSDynamic:
669
+ try membersDirect(dynamic, dynamic.jsKeys) { dynamic[jsKey: $0] }
670
+ default: output.append("{}")
671
+ }
672
+ return true
673
+ }
674
+
675
+ private mutating func elementsDirect(_ object: AnyObject?, _ count: Int, _ element: (Int) -> Any?) throws {
676
+ try enter(object)
677
+ defer { leave(object) }
678
+ guard count > 0 else { return output.append("[]") }
679
+ output.append(0x5B)
680
+ depth += 1
681
+ for i in 0..<count {
682
+ if i > 0 { output.append(0x2C) }
683
+ newline()
684
+ if try !property(object, String(i), element(i)) { output.append("null") }
685
+ }
686
+ depth -= 1
687
+ newline()
688
+ output.append(0x5D)
689
+ }
690
+
691
+ private mutating func writeFastDirect(_ fast: JSJSONFastSerializable) throws -> Bool {
692
+ try enter(fast)
693
+ defer { leave(fast) }
694
+ depth += 1
695
+ defer { depth -= 1 }
696
+ let mark = output.count
697
+ if try fast.jsWriteJSONFast(to: output, writeField: { prefix, key, value in
698
+ let fieldMark = self.output.count
699
+ self.output.append(prefix)
700
+ if try !self.property(fast, key, value) { self.output.truncate(to: fieldMark) }
701
+ }) {
702
+ return true
703
+ }
704
+ output.truncate(to: mark)
705
+ return false
706
+ }
707
+
708
+ private mutating func membersDirect(_ object: AnyObject, _ keys: [String], _ member: (String) -> Any?) throws {
709
+ try enter(object)
710
+ defer { leave(object) }
711
+ output.append(0x7B)
712
+ depth += 1
713
+ var empty = true
714
+ for key in allowed ?? keys {
715
+ if allowed != nil, !keys.contains(key) { continue }
716
+ let start = output.count
717
+ if !empty { output.append(0x2C) }
718
+ newline()
719
+ output.appendJSONString(key)
720
+ output.append(0x3A)
721
+ if !gap.isEmpty { output.append(0x20) }
722
+ if try property(object, key, member(key)) { empty = false } else { output.truncate(to: start) }
723
+ }
724
+ depth -= 1
725
+ if !empty { newline() }
726
+ output.append(0x7D)
727
+ }
728
+
729
+ /// Runs `toJSON` and `replacer`, returning `nil` if the property is omitted (undefined or function).
730
+ private mutating func resolve(_ holder: AnyObject?, _ key: @autoclosure () -> String, _ value: Any?) throws -> Any? {
731
+ var v = jsFlat(value)
732
+ if let value = v, jsIsClassInstance(value) {
733
+ let object = jsObjectReference(value)
734
+ switch kind(of: object) {
735
+ case .plainObject:
736
+ let plain = unsafeDowncast(object, to: JSObject.self)
737
+ if let f = jsFlat(plain["toJSON"]), jsIsFunction(f) { v = try jsInvoke(f, plain, [key()]) }
738
+ case .fastObject:
739
+ let fast = object as! JSJSONFastSerializable
740
+ if !fast.jsCanFastJSON {
741
+ let dynamic = object as! JSDynamic
742
+ if let f = jsFlat(dynamic[jsKey: "toJSON"]), jsIsFunction(f) { v = try jsInvoke(f, dynamic, [key()]) }
743
+ }
744
+ case .dynamic:
745
+ let dynamic = object as! JSDynamic
746
+ if let f = jsFlat(dynamic[jsKey: "toJSON"]), jsIsFunction(f) { v = try jsInvoke(f, dynamic, [key()]) }
747
+ case .toJSON: v = try (object as! JSToJSONConvertible).jsToJSON(key())
748
+ case .date: v = unsafeDowncast(object, to: JSDate.self).toJSON() ?? jsNull
749
+ default: break
750
+ }
751
+ }
752
+ if let replacer { v = try jsFlat(jsInvoke(replacer, holder, [key(), v])) }
753
+ guard let v else { return nil }
754
+ if jsIsClassInstance(v) {
755
+ if kind(of: jsObjectReference(v)) == .function { return nil }
756
+ return v
757
+ }
758
+ if jsIsFunction(v) { return nil }
759
+ return v
760
+ }
761
+
762
+ private mutating func drain() throws {
763
+ while !frames.isEmpty {
764
+ let top = frames.count - 1
765
+ switch frames[top] {
766
+ case .array(let object, let source, let count, let index):
767
+ if index < count {
768
+ frames[top] = .array(object: object, source: source, count: count, index: index + 1)
769
+ if index > 0 { output.append(0x2C) }
770
+ newline()
771
+ if let resolved = try resolve(object, String(index), source.element(at: index)) {
772
+ if try !emitOrPush(resolved) { output.append("null") }
773
+ } else {
774
+ output.append("null")
775
+ }
776
+ } else {
777
+ frames.removeLast()
778
+ leave(object)
779
+ depth -= 1
780
+ newline()
781
+ output.append(0x5D)
782
+ }
783
+ case .object(let object, let source, let keys, let sourceKeys, var index, let empty):
784
+ var pushed = false
785
+ var nowEmpty = empty
786
+ while index < keys.count {
787
+ let key = keys[index]
788
+ index += 1
789
+ if let sourceKeys, !sourceKeys.contains(key) { continue }
790
+ guard let resolved = try resolve(object, key, source.value(for: key)) else { continue }
791
+ if !nowEmpty { output.append(0x2C) }
792
+ newline()
793
+ output.appendJSONString(key)
794
+ output.append(0x3A)
795
+ if !gap.isEmpty { output.append(0x20) }
796
+ nowEmpty = false
797
+ frames[top] = .object(object: object, source: source, keys: keys, sourceKeys: sourceKeys, index: index, empty: false)
798
+ let before = frames.count
799
+ _ = try emitOrPush(resolved)
800
+ if frames.count > before {
801
+ pushed = true
802
+ break
803
+ }
804
+ }
805
+ if !pushed {
806
+ frames.removeLast()
807
+ leave(object)
808
+ depth -= 1
809
+ if !nowEmpty { newline() }
810
+ output.append(0x7D)
811
+ }
812
+ }
813
+ }
497
814
  }
498
815
 
499
816
  private mutating func kind(of object: AnyObject) -> JSJSONClassKind {
@@ -512,6 +829,7 @@ private struct JSJSONWriter {
512
829
  case is JSDate: .date
513
830
  case let f where jsIsFunction(f): .function
514
831
  case is JSError: .error
832
+ case is JSJSONFastSerializable: .fastObject
515
833
  case is JSDynamic: .dynamic
516
834
  default: .other
517
835
  }
@@ -519,11 +837,11 @@ private struct JSJSONWriter {
519
837
  return kind
520
838
  }
521
839
 
522
- /// A value after `toJSON` and the replacer; false, writing nothing, when it serializes to undefined.
523
- private mutating func write(_ value: Any) throws -> Bool {
840
+ /// Writes a scalar immediately or pushes a container frame onto `frames`.
841
+ private mutating func emitOrPush(_ value: Any) throws -> Bool {
524
842
  if jsIsClassInstance(value) {
525
843
  let object = jsObjectReference(value)
526
- return try write(object: object, kind(of: object))
844
+ return try emitOrPush(object: object, kind(of: object))
527
845
  }
528
846
  let output = self.output
529
847
  /// The value opened: its type tested against String's and Double's is a comparison of metadata,
@@ -542,47 +860,45 @@ private struct JSJSONWriter {
542
860
  } else if jsIsFunction(value) {
543
861
  return false
544
862
  } else if jsIsTuple(value) {
545
- try array(nil, Mirror(reflecting: value).children.map { jsFlat($0.value) })
863
+ let list = Mirror(reflecting: value).children.map { jsFlat($0.value) }
864
+ try pushArray(nil, .boxed(list), list.count)
546
865
  } else {
547
866
  return try writeBridged(value)
548
867
  }
549
868
  return true
550
869
  }
551
870
 
552
- private mutating func write(object: AnyObject, _ kind: JSJSONClassKind) throws -> Bool {
871
+ private mutating func emitOrPush(object: AnyObject, _ kind: JSJSONClassKind) throws -> Bool {
553
872
  switch kind {
554
873
  case .null:
555
874
  output.append("null")
556
875
  case .plainObject:
557
876
  let plain = unsafeDowncast(object, to: JSObject.self)
558
- try members(plain, plain.keys) { plain[$0] }
559
- case .dynamic, .error:
877
+ try pushObject(plain, .plain(plain), plain.keys)
878
+ case .dynamic, .fastObject, .error:
560
879
  let dynamic = object as! JSDynamic
561
- try members(dynamic, dynamic.jsKeys) { dynamic[jsKey: $0] }
880
+ try pushObject(dynamic, .dynamic(dynamic), dynamic.jsKeys)
562
881
  case .numbers:
563
882
  let array = unsafeDowncast(object, to: JSArray<Double>.self)
564
883
  array.track()
565
- try elements(array, array.storage.count) {
566
- let d = array.storage[$0]
567
- return d.isNaN ? JSArray<Double>.untyped(d) : d
568
- }
884
+ try pushArray(array, .numbers(array), array.storage.count)
569
885
  case .strings:
570
886
  let array = unsafeDowncast(object, to: JSArray<String>.self)
571
887
  array.track()
572
- try elements(array, array.storage.count) { jsIsHoleString(array.storage[$0]) ? nil : array.storage[$0] }
888
+ try pushArray(array, .strings(array), array.storage.count)
573
889
  case .values where unsafeDowncast(object, to: JSArray<Any?>.self).jsTracker == nil && unsafeDowncast(object, to: JSArray<Any?>.self).shared == nil:
574
890
  let array = unsafeDowncast(object, to: JSArray<Any?>.self)
575
- try elements(array, array.storage.count) { array.storage[$0] }
891
+ try pushArray(array, .values(array), array.storage.count)
576
892
  case .match:
577
893
  let values = unsafeDowncast(object, to: JSMatch.self).values
578
- try elements(values, values.storage.count) { values.storage[$0] }
894
+ try pushArray(values, .boxed(values.storage.map { $0 }), values.storage.count)
579
895
  case .matchIndices:
580
896
  let values = unsafeDowncast(object, to: JSMatchIndices.self).values
581
- try elements(values, values.storage.count) { values.storage[$0] }
897
+ try pushArray(values, .boxed(values.storage.map { $0 }), values.storage.count)
582
898
  case .values, .array:
583
899
  let array = object as! JSArrayProtocol
584
900
  let values = array.jsAnyElements
585
- try elements(array, values.count) { values[$0] }
901
+ try pushArray(array, .boxed(values), values.count)
586
902
  case .date:
587
903
  guard let iso = unsafeDowncast(object, to: JSDate.self).toJSON() else { output.append("null"); break }
588
904
  output.appendJSONString(iso)
@@ -603,17 +919,37 @@ private struct JSJSONWriter {
603
919
  case let n where jsNumeric(n) != nil: output.appendJSONNumber(jsNumeric(n)!)
604
920
  case let array as JSArrayProtocol:
605
921
  let values = array.jsAnyElements
606
- try elements(array, values.count) { values[$0] }
922
+ try pushArray(array, .boxed(values), values.count)
607
923
  case let dynamic as JSDynamic:
608
- try members(dynamic, dynamic.jsKeys) { dynamic[jsKey: $0] }
924
+ try pushObject(dynamic, .dynamic(dynamic), dynamic.jsKeys)
609
925
  default: output.append("{}")
610
926
  }
611
927
  return true
612
928
  }
613
929
 
930
+ private mutating func pushArray(_ object: AnyObject?, _ source: ArraySource, _ count: Int) throws {
931
+ try enter(object)
932
+ guard count > 0 else {
933
+ leave(object)
934
+ output.append("[]")
935
+ return
936
+ }
937
+ output.append(0x5B)
938
+ depth += 1
939
+ frames.append(.array(object: object, source: source, count: count, index: 0))
940
+ }
941
+
942
+ private mutating func pushObject(_ object: AnyObject, _ source: ObjectSource, _ ownKeys: [String]) throws {
943
+ try enter(object)
944
+ output.append(0x7B)
945
+ depth += 1
946
+ let iterKeys = allowed ?? ownKeys
947
+ let filterKeys: [String]? = allowed != nil ? ownKeys : nil
948
+ frames.append(.object(object: object, source: source, keys: iterKeys, sourceKeys: filterKeys, index: 0, empty: true))
949
+ }
950
+
614
951
  private mutating func enter(_ object: AnyObject?) throws {
615
952
  guard let object else { return }
616
- try jsCheckStack()
617
953
  if let start = stack.firstIndex(where: { $0 === object }) { throw JSException(JSTypeError(circularMessage(start))) }
618
954
  stack.append(object)
619
955
  }
@@ -651,49 +987,6 @@ private struct JSJSONWriter {
651
987
  return text + "\n --- \(link(stack.count - 1, to: stack[start])) closes the circle"
652
988
  }
653
989
 
654
- private mutating func array(_ object: AnyObject?, _ values: [Any?]) throws {
655
- try elements(object, values.count) { values[$0] }
656
- }
657
-
658
- /// SerializeJSONArray: an element that serializes to undefined is `null`.
659
- private mutating func elements(_ object: AnyObject?, _ count: Int, _ element: (Int) -> Any?) throws {
660
- try enter(object)
661
- defer { leave(object) }
662
- guard count > 0 else { return output.append("[]") }
663
- output.append(0x5B)
664
- depth += 1
665
- for i in 0..<count {
666
- if i > 0 { output.append(0x2C) }
667
- newline()
668
- if try !property(object, String(i), element(i)) { output.append("null") }
669
- }
670
- depth -= 1
671
- newline()
672
- output.append(0x5D)
673
- }
674
-
675
- /// SerializeJSONObject: each key's value read as it is serialized; one that serializes to undefined is left out.
676
- private mutating func members(_ object: AnyObject, _ keys: [String], _ member: (String) -> Any?) throws {
677
- try enter(object)
678
- defer { leave(object) }
679
- output.append(0x7B)
680
- depth += 1
681
- var empty = true
682
- for key in allowed ?? keys {
683
- if allowed != nil, !keys.contains(key) { continue }
684
- let start = output.count
685
- if !empty { output.append(0x2C) }
686
- newline()
687
- output.appendJSONString(key)
688
- output.append(0x3A)
689
- if !gap.isEmpty { output.append(0x20) }
690
- if try property(object, key, member(key)) { empty = false } else { output.truncate(to: start) }
691
- }
692
- depth -= 1
693
- if !empty { newline() }
694
- output.append(0x7D)
695
- }
696
-
697
990
  @inline(__always) private func newline() {
698
991
  guard !gap.isEmpty else { return }
699
992
  output.append(0x0A)
@@ -763,7 +1056,7 @@ func jsJSONQuote(_ s: String) -> String {
763
1056
  return output.string
764
1057
  }
765
1058
 
766
- extension JSUTF8Buffer {
1059
+ public extension JSUTF8Buffer {
767
1060
  /// Number::toString. Integers below 2^53 are written digit by digit, and a number with at most three decimals
768
1061
  /// is found as the integer of its hundredths (say) whose quotient is the same double: what parsing those digits
769
1062
  /// would give, so that they are its shortest round-trip form. Other fractions take Swift's shortest digits,
@@ -798,14 +1091,61 @@ extension JSUTF8Buffer {
798
1091
  }
799
1092
  append(0x22)
800
1093
  }
1094
+
1095
+ @inline(__always) func appendJSONBool(_ b: Bool) {
1096
+ append(b ? "true" : "false")
1097
+ }
1098
+
1099
+ @inline(__always) func appendJSONLiteral(_ text: StaticString) {
1100
+ append(text)
1101
+ }
1102
+
1103
+ func appendJSONNumbers(_ array: JSArray<Double>) {
1104
+ array.track()
1105
+ let items = array.held
1106
+ guard !items.isEmpty else { return append("[]") }
1107
+ append(0x5B)
1108
+ for i in 0..<items.count {
1109
+ if i > 0 { append(0x2C) }
1110
+ let d = items[i]
1111
+ if d.isFinite { appendJSONNumber(d) } else { append("null") }
1112
+ }
1113
+ append(0x5D)
1114
+ }
1115
+
1116
+ func appendJSONStrings(_ array: JSArray<String>) {
1117
+ array.track()
1118
+ let items = array.held
1119
+ guard !items.isEmpty else { return append("[]") }
1120
+ append(0x5B)
1121
+ for i in 0..<items.count {
1122
+ if i > 0 { append(0x2C) }
1123
+ let s = items[i]
1124
+ if jsIsHoleString(s) { append("null") } else { appendJSONString(s) }
1125
+ }
1126
+ append(0x5D)
1127
+ }
1128
+ func appendJSONNumbersIfPlain(_ prefix: StaticString, _ array: JSArray<Double>?) -> Bool {
1129
+ guard let array, array.shared == nil, jsSideProperties(array, create: false) == nil else { return false }
1130
+ append(prefix)
1131
+ appendJSONNumbers(array)
1132
+ return true
1133
+ }
1134
+
1135
+ func appendJSONStringsIfPlain(_ prefix: StaticString, _ array: JSArray<String>?) -> Bool {
1136
+ guard let array, array.shared == nil, jsSideProperties(array, create: false) == nil else { return false }
1137
+ append(prefix)
1138
+ appendJSONStrings(array)
1139
+ return true
1140
+ }
801
1141
  }
802
1142
 
803
1143
  /// UTF-8 text written a byte or a run at a time, into storage that doubles as it fills: an array would check
804
1144
  /// that its storage is unshared on every append.
805
- private nonisolated final class JSUTF8Buffer {
1145
+ public nonisolated final class JSUTF8Buffer {
806
1146
  private var base: UnsafeMutablePointer<UInt8>
807
1147
  private var capacity: Int
808
- private(set) var count = 0
1148
+ public private(set) var count = 0
809
1149
 
810
1150
  init(capacity: Int = 256) {
811
1151
  self.capacity = capacity
@@ -11,11 +11,11 @@ public final class JSAsyncLoop {
11
11
  nonisolated deinit {}
12
12
  private var running = false
13
13
  private var pending = false
14
- private var iteration: ((@escaping () -> Void) -> Void)?
14
+ private var iteration: ((@escaping @MainActor () -> Void) -> Void)?
15
15
 
16
16
  public init() {}
17
17
 
18
- public func run(_ body: @escaping (@escaping () -> Void) -> Void) {
18
+ public func run(_ body: @escaping (@escaping @MainActor () -> Void) -> Void) {
19
19
  iteration = body
20
20
  step()
21
21
  }
@@ -39,6 +39,7 @@ public func jsReport(_ body: () throws -> Void) {
39
39
 
40
40
  /// Without NativeScript's `Application` (a plain program, the differential tests), an uncaught error goes to
41
41
  /// `globalThis.__onUncaughtError` when script sets one, where core's handler is; else to standard error.
42
+ @MainActor
42
43
  public var jsUncaughtHandler: (Any?) -> Void = { value in
43
44
  if !jsReportedToScript(value) { jsError("Uncaught", value) }
44
45
  }
@@ -136,6 +136,18 @@ public enum JSPrototypes {
136
136
  return objectPrototype
137
137
  }
138
138
 
139
+ /// `Object.getPrototypeOf` of a class constructor: its superclass, else `Function.prototype`.
140
+ static func prototypeOf(class cls: AnyClass) -> Any? {
141
+ tableLock.lock()
142
+ if let custom = customPrototypes[ObjectIdentifier(cls)] {
143
+ tableLock.unlock()
144
+ return custom
145
+ }
146
+ tableLock.unlock()
147
+ if let superclass = class_getSuperclass(cls), !isRoot(superclass) { return superclass }
148
+ return builtin("Function")
149
+ }
150
+
139
151
  /// The class Swift's own root classes extend, which no script class corresponds to.
140
152
  private static func isRoot(_ cls: AnyClass) -> Bool { NSStringFromClass(cls) == "_TtCs12_SwiftObject" }
141
153
 
@@ -307,7 +319,7 @@ public func jsGetPrototypeOf(_ value: Any?) throws -> Any? {
307
319
  guard let v = jsFlat(value), !(v is JSNull) else { throw JSException(JSTypeError("Cannot convert undefined or null to object")) }
308
320
  switch v {
309
321
  case let o as JSObject: return JSPrototypes.prototypeOf(object: o)
310
- case is AnyClass: return JSPrototypes.builtin("Function")
322
+ case let cls as AnyClass: return JSPrototypes.prototypeOf(class: cls)
311
323
  case is JSArrayProtocol: return JSPrototypes.builtin("Array")
312
324
  case is String: return JSPrototypes.builtin("String")
313
325
  case is Double: return JSPrototypes.builtin("Number")
@@ -368,6 +380,7 @@ public func jsDecorate(_ cls: AnyClass, _ decorators: [Any?]) throws {
368
380
  /// An instance of a class generated from core: the properties script gives it beyond the
369
381
  /// fields it declares (`this[key] = v`, symbol-keyed ones included), its methods named by
370
382
  /// symbols (`[prop.setNative](value)`), and what its class's prototype defines.
383
+ @MainActor
371
384
  public protocol JSExpando: JSDynamic, JSSymbolKeyed, JSDeletable {
372
385
  var jsExpando: JSObject? { get set }
373
386
  /// The method a class declares under the symbol whose key this is; the method takes its receiver as `this`.