metano-runtime 3.0.1 → 3.1.0

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 (211) hide show
  1. package/dist/system/char-members.d.ts +51 -0
  2. package/dist/system/char-members.js +108 -0
  3. package/dist/system/collections/array-helpers.d.ts +28 -0
  4. package/dist/system/collections/array-helpers.js +105 -0
  5. package/dist/system/collections/hash-core.d.ts +5 -0
  6. package/dist/system/collections/hash-core.js +24 -0
  7. package/dist/system/collections/helpers.d.ts +16 -3
  8. package/dist/system/collections/helpers.js +28 -2
  9. package/dist/system/collections/immutable-list-builder-core.d.ts +7 -2
  10. package/dist/system/collections/immutable-list-builder-core.js +11 -0
  11. package/dist/system/collections/immutable-list-core.d.ts +8 -0
  12. package/dist/system/collections/immutable-list-core.js +19 -1
  13. package/dist/system/collections/immutable-storage.d.ts +2 -0
  14. package/dist/system/collections/immutable-storage.js +6 -1
  15. package/dist/system/collections/index.d.ts +2 -0
  16. package/dist/system/collections/index.js +2 -0
  17. package/dist/system/collections/interfaces.d.ts +13 -0
  18. package/dist/system/collections/list.d.ts +3 -0
  19. package/dist/system/collections/list.js +8 -11
  20. package/dist/system/collections/read-only-list.d.ts +11 -0
  21. package/dist/system/collections/read-only-list.js +26 -1
  22. package/dist/system/collections/sorted-list.d.ts +5 -0
  23. package/dist/system/collections/sorted-list.js +12 -1
  24. package/dist/system/collections/string-comparer.d.ts +14 -0
  25. package/dist/system/collections/string-comparer.js +45 -0
  26. package/dist/system/decimal-parsing.d.ts +12 -0
  27. package/dist/system/decimal-parsing.js +86 -0
  28. package/dist/system/exceptions/index.d.ts +1 -0
  29. package/dist/system/exceptions/index.js +1 -0
  30. package/dist/system/exceptions/throw-error.d.ts +7 -0
  31. package/dist/system/exceptions/throw-error.js +9 -0
  32. package/dist/system/index.d.ts +5 -0
  33. package/dist/system/index.js +5 -0
  34. package/dist/system/int64.d.ts +6 -0
  35. package/dist/system/int64.js +18 -9
  36. package/dist/system/integer-division.d.ts +12 -4
  37. package/dist/system/integer-division.js +19 -2
  38. package/dist/system/json/contract-name.d.ts +2 -1
  39. package/dist/system/json/contract-name.js +3 -2
  40. package/dist/system/json/converters.d.ts +15 -52
  41. package/dist/system/json/converters.js +33 -317
  42. package/dist/system/json/dictionary-keys.js +1 -3
  43. package/dist/system/json/index.d.ts +1 -1
  44. package/dist/system/json/index.js +1 -1
  45. package/dist/system/json/json-serializer.d.ts +12 -1
  46. package/dist/system/json/json-serializer.js +29 -1
  47. package/dist/system/json/json-text-writer.d.ts +17 -0
  48. package/dist/system/json/json-text-writer.js +175 -0
  49. package/dist/system/json/null-reads.d.ts +2 -1
  50. package/dist/system/json/null-reads.js +3 -1
  51. package/dist/system/json/number-from-string.d.ts +2 -0
  52. package/dist/system/json/number-from-string.js +24 -6
  53. package/dist/system/json/numeric-converters.d.ts +4 -4
  54. package/dist/system/json/numeric-converters.js +46 -8
  55. package/dist/system/json/numeric-enum-members.d.ts +3 -3
  56. package/dist/system/json/numeric-enum-members.js +2 -4
  57. package/dist/system/json/spec-plans.d.ts +102 -0
  58. package/dist/system/json/spec-plans.js +312 -0
  59. package/dist/system/json/types.d.ts +15 -9
  60. package/dist/system/lazy.d.ts +15 -0
  61. package/dist/system/lazy.js +43 -0
  62. package/dist/system/linq/grouping.d.ts +12 -0
  63. package/dist/system/linq/grouping.js +42 -0
  64. package/dist/system/linq/index.d.ts +4 -3
  65. package/dist/system/linq/index.js +3 -2
  66. package/dist/system/linq/linq.js +20 -1
  67. package/dist/system/linq/lookup.d.ts +17 -0
  68. package/dist/system/linq/lookup.js +20 -0
  69. package/dist/system/linq/operators.d.ts +35 -5
  70. package/dist/system/linq/operators.js +82 -31
  71. package/dist/system/linq/terminals.d.ts +41 -2
  72. package/dist/system/linq/terminals.js +122 -2
  73. package/dist/system/linq/types.d.ts +48 -4
  74. package/dist/system/math.d.ts +7 -0
  75. package/dist/system/math.js +14 -0
  76. package/dist/system/number-members.d.ts +10 -0
  77. package/dist/system/number-members.js +25 -0
  78. package/dist/system/number-parsing.d.ts +16 -4
  79. package/dist/system/number-parsing.js +21 -4
  80. package/dist/system/string-builder.d.ts +30 -0
  81. package/dist/system/string-builder.js +86 -0
  82. package/dist/system/task-completion-source.d.ts +37 -0
  83. package/dist/system/task-completion-source.js +81 -0
  84. package/dist-unchecked/system/char-members.d.ts +51 -0
  85. package/dist-unchecked/system/char-members.js +108 -0
  86. package/dist-unchecked/system/collections/array-helpers.d.ts +28 -0
  87. package/dist-unchecked/system/collections/array-helpers.js +105 -0
  88. package/dist-unchecked/system/collections/hash-core.d.ts +5 -0
  89. package/dist-unchecked/system/collections/hash-core.js +24 -0
  90. package/dist-unchecked/system/collections/helpers.d.ts +16 -3
  91. package/dist-unchecked/system/collections/helpers.js +28 -2
  92. package/dist-unchecked/system/collections/immutable-list-builder-core.d.ts +7 -2
  93. package/dist-unchecked/system/collections/immutable-list-builder-core.js +11 -0
  94. package/dist-unchecked/system/collections/immutable-list-core.d.ts +8 -0
  95. package/dist-unchecked/system/collections/immutable-list-core.js +19 -1
  96. package/dist-unchecked/system/collections/immutable-storage.d.ts +2 -0
  97. package/dist-unchecked/system/collections/immutable-storage.js +6 -1
  98. package/dist-unchecked/system/collections/index.d.ts +2 -0
  99. package/dist-unchecked/system/collections/index.js +2 -0
  100. package/dist-unchecked/system/collections/interfaces.d.ts +13 -0
  101. package/dist-unchecked/system/collections/list.d.ts +3 -0
  102. package/dist-unchecked/system/collections/list.js +8 -11
  103. package/dist-unchecked/system/collections/read-only-list.d.ts +11 -0
  104. package/dist-unchecked/system/collections/read-only-list.js +26 -1
  105. package/dist-unchecked/system/collections/sorted-list.d.ts +5 -0
  106. package/dist-unchecked/system/collections/sorted-list.js +12 -1
  107. package/dist-unchecked/system/collections/string-comparer.d.ts +14 -0
  108. package/dist-unchecked/system/collections/string-comparer.js +45 -0
  109. package/dist-unchecked/system/decimal-parsing.d.ts +12 -0
  110. package/dist-unchecked/system/decimal-parsing.js +86 -0
  111. package/dist-unchecked/system/exceptions/index.d.ts +1 -0
  112. package/dist-unchecked/system/exceptions/index.js +1 -0
  113. package/dist-unchecked/system/exceptions/throw-error.d.ts +7 -0
  114. package/dist-unchecked/system/exceptions/throw-error.js +9 -0
  115. package/dist-unchecked/system/index.d.ts +5 -0
  116. package/dist-unchecked/system/index.js +5 -0
  117. package/dist-unchecked/system/int64.d.ts +6 -0
  118. package/dist-unchecked/system/int64.js +18 -9
  119. package/dist-unchecked/system/integer-division.d.ts +12 -4
  120. package/dist-unchecked/system/integer-division.js +19 -2
  121. package/dist-unchecked/system/json/contract-name.d.ts +2 -1
  122. package/dist-unchecked/system/json/contract-name.js +3 -2
  123. package/dist-unchecked/system/json/converters.d.ts +15 -52
  124. package/dist-unchecked/system/json/converters.js +33 -317
  125. package/dist-unchecked/system/json/dictionary-keys.js +1 -3
  126. package/dist-unchecked/system/json/index.d.ts +1 -1
  127. package/dist-unchecked/system/json/index.js +1 -1
  128. package/dist-unchecked/system/json/json-serializer.d.ts +12 -1
  129. package/dist-unchecked/system/json/json-serializer.js +29 -1
  130. package/dist-unchecked/system/json/json-text-writer.d.ts +17 -0
  131. package/dist-unchecked/system/json/json-text-writer.js +175 -0
  132. package/dist-unchecked/system/json/null-reads.d.ts +2 -1
  133. package/dist-unchecked/system/json/null-reads.js +3 -1
  134. package/dist-unchecked/system/json/number-from-string.d.ts +2 -0
  135. package/dist-unchecked/system/json/number-from-string.js +24 -6
  136. package/dist-unchecked/system/json/numeric-converters.d.ts +4 -4
  137. package/dist-unchecked/system/json/numeric-converters.js +46 -8
  138. package/dist-unchecked/system/json/numeric-enum-members.d.ts +3 -3
  139. package/dist-unchecked/system/json/numeric-enum-members.js +2 -4
  140. package/dist-unchecked/system/json/spec-plans.d.ts +102 -0
  141. package/dist-unchecked/system/json/spec-plans.js +312 -0
  142. package/dist-unchecked/system/json/types.d.ts +15 -9
  143. package/dist-unchecked/system/lazy.d.ts +15 -0
  144. package/dist-unchecked/system/lazy.js +43 -0
  145. package/dist-unchecked/system/linq/grouping.d.ts +12 -0
  146. package/dist-unchecked/system/linq/grouping.js +42 -0
  147. package/dist-unchecked/system/linq/index.d.ts +4 -3
  148. package/dist-unchecked/system/linq/index.js +3 -2
  149. package/dist-unchecked/system/linq/linq.js +20 -1
  150. package/dist-unchecked/system/linq/lookup.d.ts +17 -0
  151. package/dist-unchecked/system/linq/lookup.js +20 -0
  152. package/dist-unchecked/system/linq/operators.d.ts +35 -5
  153. package/dist-unchecked/system/linq/operators.js +82 -31
  154. package/dist-unchecked/system/linq/terminals.d.ts +41 -2
  155. package/dist-unchecked/system/linq/terminals.js +122 -2
  156. package/dist-unchecked/system/linq/types.d.ts +48 -4
  157. package/dist-unchecked/system/math.d.ts +7 -0
  158. package/dist-unchecked/system/math.js +14 -0
  159. package/dist-unchecked/system/number-members.d.ts +10 -0
  160. package/dist-unchecked/system/number-members.js +25 -0
  161. package/dist-unchecked/system/number-parsing.d.ts +16 -4
  162. package/dist-unchecked/system/number-parsing.js +21 -4
  163. package/dist-unchecked/system/string-builder.d.ts +30 -0
  164. package/dist-unchecked/system/string-builder.js +86 -0
  165. package/dist-unchecked/system/task-completion-source.d.ts +37 -0
  166. package/dist-unchecked/system/task-completion-source.js +81 -0
  167. package/package.json +1 -1
  168. package/src/system/char-members.ts +131 -0
  169. package/src/system/collections/array-helpers.ts +139 -0
  170. package/src/system/collections/hash-core.ts +31 -0
  171. package/src/system/collections/helpers.ts +39 -4
  172. package/src/system/collections/immutable-list-builder-core.ts +16 -2
  173. package/src/system/collections/immutable-list-core.ts +23 -1
  174. package/src/system/collections/immutable-storage.ts +8 -1
  175. package/src/system/collections/index.ts +2 -0
  176. package/src/system/collections/interfaces.ts +16 -0
  177. package/src/system/collections/list.ts +10 -15
  178. package/src/system/collections/read-only-list.ts +35 -1
  179. package/src/system/collections/sorted-list.ts +15 -1
  180. package/src/system/collections/string-comparer.ts +57 -0
  181. package/src/system/decimal-parsing.ts +109 -0
  182. package/src/system/exceptions/index.ts +1 -0
  183. package/src/system/exceptions/throw-error.ts +9 -0
  184. package/src/system/index.ts +5 -0
  185. package/src/system/int64.ts +27 -10
  186. package/src/system/integer-division.ts +43 -6
  187. package/src/system/json/contract-name.ts +3 -2
  188. package/src/system/json/converters.ts +38 -526
  189. package/src/system/json/dictionary-keys.ts +6 -4
  190. package/src/system/json/index.ts +1 -1
  191. package/src/system/json/json-serializer.ts +42 -1
  192. package/src/system/json/json-text-writer.ts +224 -0
  193. package/src/system/json/null-reads.ts +3 -1
  194. package/src/system/json/number-from-string.ts +31 -6
  195. package/src/system/json/numeric-converters.ts +60 -8
  196. package/src/system/json/numeric-enum-members.ts +4 -6
  197. package/src/system/json/spec-plans.ts +524 -0
  198. package/src/system/json/types.ts +15 -9
  199. package/src/system/lazy.ts +62 -0
  200. package/src/system/linq/grouping.ts +77 -0
  201. package/src/system/linq/index.ts +14 -0
  202. package/src/system/linq/linq.ts +23 -1
  203. package/src/system/linq/lookup.ts +34 -0
  204. package/src/system/linq/operators.ts +109 -17
  205. package/src/system/linq/terminals.ts +161 -2
  206. package/src/system/linq/types.ts +66 -3
  207. package/src/system/math.ts +19 -0
  208. package/src/system/number-members.ts +29 -0
  209. package/src/system/number-parsing.ts +24 -4
  210. package/src/system/string-builder.ts +117 -0
  211. package/src/system/task-completion-source.ts +106 -0
@@ -5,7 +5,7 @@ import {
5
5
  type IEqualityComparer,
6
6
  } from "./default-comparer.ts";
7
7
  import type { IImmutableList } from "./immutable-interfaces.ts";
8
- import { ownsTip } from "./immutable-storage.ts";
8
+ import { detachTip, ownsTip } from "./immutable-storage.ts";
9
9
  import type { IReadOnlyList } from "./interfaces.ts";
10
10
  import { binarySearch } from "./sorted-array.ts";
11
11
 
@@ -43,6 +43,10 @@ export abstract class ImmutableListCore<T, Self extends ImmutableListCore<T, Sel
43
43
  return this._count;
44
44
  }
45
45
 
46
+ get length(): number {
47
+ return this._count;
48
+ }
49
+
46
50
  get isEmpty(): boolean {
47
51
  return this._count === 0;
48
52
  }
@@ -244,6 +248,24 @@ export abstract class ImmutableListCore<T, Self extends ImmutableListCore<T, Sel
244
248
  return this.visible();
245
249
  }
246
250
 
251
+ /**
252
+ * Read-only view over `_items`: zero-copy when this version owns the full array
253
+ * (detaching tip-append ownership so later `add` calls copy rather than mutating
254
+ * the view); falls back to a shallow copy when an older version shares a grown
255
+ * store (values are immutable, though array identity is not stable across calls).
256
+ */
257
+ asReadOnlyJsArray(): readonly T[] {
258
+ if (this._items.length !== this._count) {
259
+ return this.visible();
260
+ }
261
+
262
+ if (this._count > 0) {
263
+ detachTip(this._items);
264
+ }
265
+
266
+ return this._items;
267
+ }
268
+
247
269
  toJSON(): T[] {
248
270
  return this.visible();
249
271
  }
@@ -15,10 +15,17 @@
15
15
  import { defaultEqualityComparer, type IEqualityComparer } from "./default-comparer.ts";
16
16
  import { HashCore } from "./hash-core.ts";
17
17
 
18
+ let detachedTips: WeakSet<object> | undefined;
19
+
20
+ /** Marks `store` as exposed to a read-only view so later appends copy instead of mutating it. */
21
+ export function detachTip(store: object): void {
22
+ (detachedTips ??= new WeakSet()).add(store);
23
+ }
24
+
18
25
  /** W4-11's ownership rule: a version of `count` entries may append to `store` in place. */
19
26
  export function ownsTip(store: { readonly length: number }, count: number): boolean {
20
27
  // Empty versions can be canonical singletons: never mutate their backing store.
21
- return count > 0 && store.length === count;
28
+ return count > 0 && store.length === count && !detachedTips?.has(store);
22
29
  }
23
30
 
24
31
  /** `null` for the default equality comparer, so comparing comparers sees one default. */
@@ -1,4 +1,5 @@
1
1
  export type { MetanoChecks } from "./checks.ts";
2
+ export * from "./array-helpers.ts";
2
3
  export * from "./default-comparer.ts";
3
4
  export * from "./descriptors.ts";
4
5
  export * from "./concurrent-dictionary.ts";
@@ -24,4 +25,5 @@ export * from "./sorted-dictionary.ts";
24
25
  export * from "./sorted-list.ts";
25
26
  export * from "./sorted-set.ts";
26
27
  export * from "./stack.ts";
28
+ export * from "./string-comparer.ts";
27
29
  export { throwModified } from "./throw-helpers.ts";
@@ -7,7 +7,23 @@ export interface IReadOnlyCollection<T> extends Iterable<T> {
7
7
  readonly count: number;
8
8
  }
9
9
  export interface IReadOnlyList<T> extends IReadOnlyCollection<T> {
10
+ /** Mirrors `count` for JavaScript/TypeScript array ergonomics. */
11
+ readonly length: number;
12
+
10
13
  get(index: number): T; // ArgumentOutOfRangeException
14
+
15
+ /** Detached shallow copy of the list's elements (spec 011 FR-055). */
16
+ toArray(): T[];
17
+
18
+ /**
19
+ * Read-only view over the backing JS array (spec 011 FR-056): zero-copy and
20
+ * live for runtime array-backed list kinds (reflects later mutations of a
21
+ * mutable source; array identity is not stable for older immutable versions
22
+ * sharing a grown store), and a shallow snapshot copy when `ReadOnlyList`
23
+ * wraps a custom `IList` without `asReadOnlyJsArray()`. Read-only for the
24
+ * consumer.
25
+ */
26
+ asReadOnlyJsArray(): readonly T[];
11
27
  }
12
28
  export interface ICollection<T> extends Iterable<T> {
13
29
  readonly count: number;
@@ -13,6 +13,7 @@ import type { CollectionType, RuntimeType } from "../reflection/runtime-type.ts"
13
13
  import { closeCollectionFor } from "./close-collection.ts";
14
14
  import { collectionTypeName } from "./collection-type-name.ts";
15
15
  import { copyInto } from "./copy-to.ts";
16
+ import { isIdentityComparable, sortInPlace } from "./array-helpers.ts";
16
17
  import { type ComparisonArgument, comparisonOf, elementEquals } from "./default-comparer.ts";
17
18
  import type { IList, IReadOnlyList, Span } from "./interfaces.ts";
18
19
  import { binarySearch } from "./sorted-array.ts";
@@ -134,6 +135,10 @@ export class List<T> implements IList<T>, IReadOnlyList<T> {
134
135
  return this._items.length;
135
136
  }
136
137
 
138
+ get length(): number {
139
+ return this._items.length;
140
+ }
141
+
137
142
  get isReadOnly(): boolean {
138
143
  return false;
139
144
  }
@@ -483,6 +488,11 @@ export class List<T> implements IList<T>, IReadOnlyList<T> {
483
488
  return this._items;
484
489
  }
485
490
 
491
+ /** The backing array itself, typed read-only (FR-056). */
492
+ asReadOnlyJsArray(): readonly T[] {
493
+ return this._items;
494
+ }
495
+
486
496
  /** The backing array itself, typed as a length-keeping view (FR-056). */
487
497
  asSpan(): Span<T> {
488
498
  return this._items;
@@ -630,11 +640,6 @@ class ListIterator<T> implements IterableIterator<T> {
630
640
  }
631
641
  }
632
642
 
633
- /** Primitives other than `NaN` compare by `===`, which `Array#indexOf` uses. */
634
- function isIdentityComparable(value: unknown): boolean {
635
- return typeof value !== "object" && !Number.isNaN(value);
636
- }
637
-
638
643
  function requireDelegate(delegate: unknown, paramName: string): void {
639
644
  if (delegate === null || delegate === undefined) {
640
645
  throwArgumentNull(paramName);
@@ -668,13 +673,3 @@ function insertAll<T>(storage: T[], index: number, added: readonly T[]): void {
668
673
  storage.push(item);
669
674
  }
670
675
  }
671
-
672
- function sortInPlace<T>(items: T[], comparison: ComparisonArgument<T>): void {
673
- const compare = comparisonOf(comparison);
674
-
675
- try {
676
- items.sort(compare);
677
- } catch (error) {
678
- throwCompareFailed(error);
679
- }
680
- }
@@ -92,6 +92,10 @@ export class ReadOnlyList<T> implements IList<T>, IReadOnlyList<T> {
92
92
  return isArraySource(source) ? source.length : source.count;
93
93
  }
94
94
 
95
+ get length(): number {
96
+ return this.count;
97
+ }
98
+
95
99
  get isReadOnly(): boolean {
96
100
  return true;
97
101
  }
@@ -175,8 +179,32 @@ export class ReadOnlyList<T> implements IList<T>, IReadOnlyList<T> {
175
179
  return throwReadOnly();
176
180
  }
177
181
 
182
+ /** Detached shallow copy of the source's elements (FR-055). */
183
+ toArray(): T[] {
184
+ const source = this._source;
185
+
186
+ return isArraySource(source) ? source.slice() : Array.from(source);
187
+ }
188
+
189
+ /**
190
+ * Read-only view over the source's backing array (spec 011 FR-056):
191
+ * zero-copy and live ONLY for runtime array-backed sources (a native array,
192
+ * a `List`, or a source exposing `asReadOnlyJsArray()`). For a custom `IList`
193
+ * source without `asReadOnlyJsArray()`, returns a **shallow snapshot copy**
194
+ * (not live; allocates a new array on each call).
195
+ */
196
+ asReadOnlyJsArray(): readonly T[] {
197
+ const source = this._source;
198
+
199
+ if (isArraySource(source)) {
200
+ return source;
201
+ }
202
+
203
+ return hasAsReadOnlyJsArray(source) ? source.asReadOnlyJsArray() : Array.from(source);
204
+ }
205
+
178
206
  toJSON(): T[] {
179
- return [...this];
207
+ return this.toArray();
180
208
  }
181
209
 
182
210
  /** .NET's ``System.Collections.ObjectModel.ReadOnlyCollection`1[T]`` text. */
@@ -208,3 +236,9 @@ export function asReadOnly<T>(
208
236
  function isArraySource<T>(source: IList<T> | readonly T[]): source is readonly T[] {
209
237
  return Array.isArray(source);
210
238
  }
239
+
240
+ function hasAsReadOnlyJsArray<T>(
241
+ source: IList<T>,
242
+ ): source is IList<T> & { asReadOnlyJsArray(): readonly T[] } {
243
+ return typeof (source as { asReadOnlyJsArray?: unknown }).asReadOnlyJsArray === "function";
244
+ }
@@ -459,6 +459,10 @@ export class SortedListView<T> implements IList<T>, IReadOnlyList<T> {
459
459
  return this._list.count;
460
460
  }
461
461
 
462
+ get length(): number {
463
+ return this._list.count;
464
+ }
465
+
462
466
  get isReadOnly(): true {
463
467
  return true;
464
468
  }
@@ -512,8 +516,18 @@ export class SortedListView<T> implements IList<T>, IReadOnlyList<T> {
512
516
  return throwSortedListNested();
513
517
  }
514
518
 
519
+ /** Detached shallow copy (FR-055). */
520
+ toArray(): T[] {
521
+ return this.items().slice();
522
+ }
523
+
524
+ /** Zero-copy live read-only view over the list's backing key/value array (FR-056). */
525
+ asReadOnlyJsArray(): readonly T[] {
526
+ return this.items();
527
+ }
528
+
515
529
  toJSON(): T[] {
516
- return [...this.items()];
530
+ return this.toArray();
517
531
  }
518
532
 
519
533
  [Symbol.iterator](): Iterator<T> {
@@ -0,0 +1,57 @@
1
+ /**
2
+ * `System.StringComparer.Ordinal` and `OrdinalIgnoreCase` as the runtime's comparers
3
+ * (#687): the argument a `Dictionary<string, V>`, a `HashSet<string>` or a sort takes. The
4
+ * culture-sensitive comparers have no counterpart and stay unmapped.
5
+ */
6
+ import { ordinalIgnoreCaseKeyOf } from "../json/ordinal-ignore-case.ts";
7
+ import { elementHashCode, type IComparer, type IEqualityComparer } from "./default-comparer.ts";
8
+
9
+ /* The singletons are built on first read, so importing the module runs no code (tree shaking). */
10
+ let ordinal: StringComparer | undefined;
11
+ let ordinalIgnoreCase: StringComparer | undefined;
12
+
13
+ export class StringComparer implements IEqualityComparer<string | null>, IComparer<string | null> {
14
+ private readonly ignoreCase: boolean;
15
+
16
+ private constructor(ignoreCase: boolean) {
17
+ this.ignoreCase = ignoreCase;
18
+ }
19
+
20
+ /** Every code unit compared as is. */
21
+ static get ordinal(): StringComparer {
22
+ return (ordinal ??= new StringComparer(false));
23
+ }
24
+
25
+ /** Strings that differ only by the simple uppercase mapping of their code points are equal. */
26
+ static get ordinalIgnoreCase(): StringComparer {
27
+ return (ordinalIgnoreCase ??= new StringComparer(true));
28
+ }
29
+
30
+ equals(a: string | null, b: string | null): boolean {
31
+ return this.keyOf(a) === this.keyOf(b);
32
+ }
33
+
34
+ getHashCode(value: string | null): number {
35
+ return elementHashCode(this.keyOf(value));
36
+ }
37
+
38
+ /** `null` sorts first; the rest by UTF-16 code unit order (of the uppercased text when ignoring case). */
39
+ compare(a: string | null, b: string | null): number {
40
+ const left = this.keyOf(a);
41
+ const right = this.keyOf(b);
42
+
43
+ if (left === right) {
44
+ return 0;
45
+ }
46
+
47
+ if (left === null) {
48
+ return -1;
49
+ }
50
+
51
+ return right === null || left > right ? 1 : -1;
52
+ }
53
+
54
+ private keyOf(value: string | null): string | null {
55
+ return this.ignoreCase && value !== null ? ordinalIgnoreCaseKeyOf(value) : value;
56
+ }
57
+ }
@@ -63,6 +63,115 @@ export function parseDecimalText(text: string | null | undefined): string {
63
63
  return `${sign}${integer || "0"}${fraction ? `.${fraction}` : ""}`;
64
64
  }
65
65
 
66
+ /** The largest scale of a `decimal`. */
67
+ const DECIMAL_SCALE_MAX = 28;
68
+
69
+ /** `1e0` … `1e28`, parsed so each is the exact double .NET's table holds. */
70
+ const DOUBLE_POWERS_OF_TEN = /* @__PURE__ */ lazyValue(() =>
71
+ Array.from({ length: DECIMAL_SCALE_MAX + 1 }, (_, power) => Number(`1e${power}`)),
72
+ );
73
+
74
+ /** A scratch view that reads the biased binary exponent of a `number`. */
75
+ const EXPONENT_VIEW = /* @__PURE__ */ lazyValue(() => new DataView(new ArrayBuffer(8)));
76
+
77
+ /**
78
+ * The digits of `(decimal)d` (`digits` 15) or `(decimal)f` (`digits` 7), for
79
+ * `new Decimal(…)`, ported from .NET's `VarDecFromR8` / `VarDecFromR4`
80
+ * (#695): the value is scaled by a power of ten to `digits` significant
81
+ * digits, the scale capped at 28, rounded half to even, and trailing zeros
82
+ * are dropped from the scale. So `(decimal)(0.1 + 0.2)` is `0.3`,
83
+ * `(decimal)1000000000000005.0` is `1000000000000000` and `(decimal)1e-30`
84
+ * is `0`. The scaling is done in double arithmetic, as .NET does.
85
+ *
86
+ * @throws OverflowException for NaN, ±Infinity and a magnitude of 2^96 or more.
87
+ */
88
+ export function decimalTextFromDouble(value: number, digits: 7 | 15): string {
89
+ const exponent = binaryExponentOf(value);
90
+
91
+ if (exponent < -94) {
92
+ return "0";
93
+ }
94
+
95
+ if (exponent > 96) {
96
+ throw new OverflowException(DECIMAL_RANGE_MESSAGE);
97
+ }
98
+
99
+ const lowest = DOUBLE_POWERS_OF_TEN()[digits - 1]!;
100
+ let power = digits - 1 - ((exponent * 19728) >> 16);
101
+ let scaled = Math.abs(value);
102
+
103
+ if (power >= 0) {
104
+ power = Math.min(power, DECIMAL_SCALE_MAX);
105
+ scaled *= DOUBLE_POWERS_OF_TEN()[power]!;
106
+ } else if (power !== -1 || scaled >= lowest * 10) {
107
+ scaled /= DOUBLE_POWERS_OF_TEN()[-power]!;
108
+ } else {
109
+ power = 0;
110
+ }
111
+
112
+ if (scaled < lowest && power < DECIMAL_SCALE_MAX) {
113
+ scaled *= 10;
114
+ power++;
115
+ }
116
+
117
+ const mantissa = roundHalfToEven(scaled);
118
+
119
+ if (mantissa === 0) {
120
+ return "0";
121
+ }
122
+
123
+ const sign = value < 0 ? "-" : "";
124
+
125
+ return power < 0
126
+ ? `${sign}${mantissa}${"0".repeat(-power)}`
127
+ : `${sign}${scaledText(String(mantissa), power, digits - 1)}`;
128
+ }
129
+
130
+ /**
131
+ * The exponent .NET reads from a binary floating-point value: the biased
132
+ * exponent of the double minus 1022, so `2^e ≤ |value| < 2^(e+1)` gives
133
+ * `e + 1`. A `float` held as a double reads the same exponent.
134
+ */
135
+ function binaryExponentOf(value: number): number {
136
+ const view = EXPONENT_VIEW();
137
+
138
+ view.setFloat64(0, value);
139
+
140
+ return ((view.getUint16(0) >> 4) & 0x7ff) - 1022;
141
+ }
142
+
143
+ /** `value` (below 2^53) rounded to an integer, a tie to the even one. */
144
+ function roundHalfToEven(value: number): number {
145
+ const floor = Math.floor(value);
146
+ const rest = value - floor;
147
+
148
+ return rest > 0.5 || (rest === 0.5 && floor % 2 === 1) ? floor + 1 : floor;
149
+ }
150
+
151
+ /**
152
+ * `digitsText` divided by `10^scale`, written out after up to `maxStrip`
153
+ * trailing zeros are dropped from the scale.
154
+ */
155
+ function scaledText(digitsText: string, scale: number, maxStrip: number): string {
156
+ let text = digitsText;
157
+ let remaining = scale;
158
+ let strippable = Math.min(scale, maxStrip);
159
+
160
+ while (strippable > 0 && text.endsWith("0")) {
161
+ text = text.slice(0, -1);
162
+ remaining--;
163
+ strippable--;
164
+ }
165
+
166
+ if (remaining === 0) {
167
+ return text;
168
+ }
169
+
170
+ const padded = text.padStart(remaining + 1, "0");
171
+
172
+ return `${padded.slice(0, -remaining)}.${padded.slice(-remaining)}`;
173
+ }
174
+
66
175
  function formatException(text: string): FormatException {
67
176
  return new FormatException(`The input string '${text}' was not in a correct format.`);
68
177
  }
@@ -24,3 +24,4 @@ export * from "./switch-expression-exception.ts";
24
24
  export * from "./named-exception.ts";
25
25
  export * from "./type-initialization-exception.ts";
26
26
  export * from "./to-exception.ts";
27
+ export * from "./throw-error.ts";
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Throws `error` from an expression position: the tail of a lowered C#
3
+ * `switch` expression no arm matched, or an arm whose result is a throw
4
+ * expression (`_ => throw new X()`). Returning `never` lets the conditional
5
+ * chain keep the type of its other arms, with no closure allocated.
6
+ */
7
+ export function throwError(error: unknown): never {
8
+ throw error;
9
+ }
@@ -33,3 +33,8 @@ export * from "./array-length.ts";
33
33
  export * from "./argument-guards.ts";
34
34
  export * from "./reflection/index.ts";
35
35
  export * from "./dynamic-conversion.ts";
36
+ export * from "./number-members.ts";
37
+ export * from "./char-members.ts";
38
+ export * from "./string-builder.ts";
39
+ export * from "./lazy.ts";
40
+ export * from "./task-completion-source.ts";
@@ -210,20 +210,37 @@ export function uint64FromDecimal(value: DecimalLike): bigint {
210
210
 
211
211
  /** Decimal to an integer of 32 bits or fewer: truncate exactly, then check even when unchecked. */
212
212
  export function narrowFromDecimal(value: DecimalLike, bits: 8 | 16 | 32, signed: boolean): number {
213
- const message = () => {
214
- const type =
215
- bits === 8
216
- ? signed
217
- ? "a signed byte"
218
- : "an unsigned byte"
219
- : `${signed ? "an Int" : "a UInt"}${bits}`;
220
- return `Value was either too large or too small for ${type}.`;
221
- };
222
- const integer = truncatedDecimal(value, message);
213
+ const type =
214
+ bits === 8
215
+ ? signed
216
+ ? "a signed byte"
217
+ : "an unsigned byte"
218
+ : `${signed ? "an Int" : "a UInt"}${bits}`;
223
219
  const width = BigInt(bits);
224
220
  const min = signed ? -(1n << (width - 1n)) : 0n;
225
221
  const max = signed ? (1n << (width - 1n)) - 1n : (1n << width) - 1n;
226
222
 
223
+ return truncatedDecimalInRange(value, min, max, type);
224
+ }
225
+
226
+ /**
227
+ * `(char)m`: the UTF-16 code of the truncated `decimal` (#695).
228
+ *
229
+ * @throws OverflowException("Value was either too large or too small for a character.") outside 0–65535.
230
+ */
231
+ export function charCodeFromDecimal(value: DecimalLike): number {
232
+ return truncatedDecimalInRange(value, 0n, 0xffffn, "a character");
233
+ }
234
+
235
+ function truncatedDecimalInRange(
236
+ value: DecimalLike,
237
+ min: bigint,
238
+ max: bigint,
239
+ type: string,
240
+ ): number {
241
+ const message = () => `Value was either too large or too small for ${type}.`;
242
+ const integer = truncatedDecimal(value, message);
243
+
227
244
  return Number(requireRange(integer, min, max, message));
228
245
  }
229
246
 
@@ -6,18 +6,18 @@
6
6
  */
7
7
 
8
8
  import { DivideByZeroException } from "./exceptions/divide-by-zero-exception.ts";
9
+ import { OverflowException } from "./exceptions/overflow-exception.ts";
10
+
11
+ const INT32_MIN = -2147483648;
9
12
 
10
13
  /**
11
14
  * Divides `dividend` by `divisor` and truncates the quotient toward zero, as C# does
12
15
  * for integral operands (`-7 / 2` is `-3`). A `null` (or `undefined`) operand yields
13
16
  * `null`, matching C#'s lifted `int?` division.
14
17
  *
15
- * Overflow is not modelled: `int.MinValue / -1` returns `2147483648` instead of
16
- * throwing `OverflowException`, and a `long` quotient beyond `Number.MAX_SAFE_INTEGER`
17
- * loses precision, like every other `long` value mapped to `number`.
18
- *
19
18
  * @throws DivideByZeroException, with .NET's message, when `divisor` is zero,
20
19
  * where a bare `Math.trunc(dividend / divisor)` would yield `Infinity` or `NaN`.
20
+ * @throws OverflowException for `int.MinValue / -1`, whose quotient does not fit `int`.
21
21
  */
22
22
  export function intDiv(dividend: number, divisor: number): number;
23
23
  export function intDiv(
@@ -32,10 +32,47 @@ export function intDiv(
32
32
  return null;
33
33
  }
34
34
 
35
+ requireInt32Division(dividend, divisor);
36
+
37
+ // C# integers have no negative zero: `+ 0` turns the `-0` of `-1 / 3` into `0`.
38
+ return Math.trunc(dividend / divisor) + 0;
39
+ }
40
+
41
+ /**
42
+ * `a % b` over `int` or `uint` operands with a divisor that is not a constant:
43
+ * the remainder with the dividend's sign, as JavaScript's `%` computes it, but
44
+ * throwing where .NET throws instead of yielding `NaN` or `0` (#695). A `null`
45
+ * (or `undefined`) operand yields `null`, matching C#'s lifted `int?` remainder.
46
+ *
47
+ * @throws DivideByZeroException, with .NET's message, when `divisor` is zero.
48
+ * @throws OverflowException for `int.MinValue % -1`, as .NET does.
49
+ */
50
+ export function intRem(dividend: number, divisor: number): number;
51
+ export function intRem(
52
+ dividend: number | null | undefined,
53
+ divisor: number | null | undefined,
54
+ ): number | null;
55
+ export function intRem(
56
+ dividend: number | null | undefined,
57
+ divisor: number | null | undefined,
58
+ ): number | null {
59
+ if (dividend == null || divisor == null) {
60
+ return null;
61
+ }
62
+
63
+ requireInt32Division(dividend, divisor);
64
+
65
+ // `+ 0` turns the `-0` of `-4 % 2` into `0`.
66
+ return (dividend % divisor) + 0;
67
+ }
68
+
69
+ /** Throws what .NET throws for an `int` `/` or `%`: a zero divisor, or `int.MinValue` by `-1`. */
70
+ function requireInt32Division(dividend: number, divisor: number): void {
35
71
  if (divisor === 0) {
36
72
  throw new DivideByZeroException();
37
73
  }
38
74
 
39
- // C# integers have no negative zero: `+ 0` turns the `-0` of `-1 / 3` into `0`.
40
- return Math.trunc(dividend / divisor) + 0;
75
+ if (dividend === INT32_MIN && divisor === -1) {
76
+ throw new OverflowException();
77
+ }
41
78
  }
@@ -5,10 +5,11 @@ import { isValueSpec } from "./spec-kind.ts";
5
5
  /**
6
6
  * The C# type name a contract reports in errors: its constructor's name, the
7
7
  * `typeName` of a polymorphic base without a constructor (an interface or a
8
- * `[PlainObject]` base), and `Object` for any other `[PlainObject]` contract.
8
+ * `[PlainObject]` base), the CLR `typeName` of any other `[PlainObject]`
9
+ * contract (#512), and `Object` for a hand-written spec with none of them.
9
10
  */
10
11
  export function contractNameOf(spec: TypeSpec): string {
11
- return spec.type?.name ?? spec.polymorphism?.typeName ?? "Object";
12
+ return spec.type?.name ?? spec.polymorphism?.typeName ?? spec.typeName ?? "Object";
12
13
  }
13
14
 
14
15
  /**