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
@@ -0,0 +1,175 @@
1
+ /**
2
+ * Writes the tree `JsonSerializer.serialize` returns as the JSON text
3
+ * System.Text.Json's `JsonSerializer.Serialize(value, JsonTypeInfo<T>)`
4
+ * writes with its default options (#741, measured on .NET 10):
5
+ *
6
+ * - no whitespace;
7
+ * - strings escaped by `JavaScriptEncoder.Default`: `\\`, `\b`, `\f`, `\n`,
8
+ * `\r` and `\t` keep their short escapes; every other control character,
9
+ * `"`, `&`, `'`, `+`, `<`, `>`, `` ` `` and every character outside
10
+ * printable ASCII is written `\uXXXX` (upper-case hex, one escape per
11
+ * UTF-16 code unit), and a lone surrogate is written `\uFFFD`;
12
+ * - numbers in .NET's shortest round-trip text (`1E+20`, `1E-07`, `-0`),
13
+ * a `JSON.rawJSON` leaf (an exact `decimal` / 64-bit integer) as its text;
14
+ * NaN and the infinities throw `ArgumentException`, as STJ throws for them.
15
+ */
16
+ import { formatDouble } from "../double-format.js";
17
+ import { ArgumentException } from "../exceptions/argument-exception.js";
18
+ import { rawJsonTextOf } from "./exact-json-number.js";
19
+ /** The message STJ throws for a NaN or infinite number written as JSON. */
20
+ const NON_FINITE_NUMBER_MESSAGE = ".NET number values such as positive and negative infinity cannot be written as valid JSON. " +
21
+ "To make it work when using 'JsonSerializer', consider specifying " +
22
+ "'JsonNumberHandling.AllowNamedFloatingPointLiterals' " +
23
+ "(see https://learn.microsoft.com/dotnet/api/system.text.json.serialization.jsonnumberhandling).";
24
+ /** The JSON text of `tree`, as System.Text.Json writes it. */
25
+ export function writeJsonText(tree) {
26
+ const parts = [];
27
+ writeValue(tree, parts);
28
+ return parts.join("");
29
+ }
30
+ function writeValue(value, parts) {
31
+ if (value == null) {
32
+ parts.push("null");
33
+ return;
34
+ }
35
+ switch (typeof value) {
36
+ case "string":
37
+ parts.push(quoted(value));
38
+ return;
39
+ case "number":
40
+ parts.push(numberText(value));
41
+ return;
42
+ case "bigint":
43
+ case "boolean":
44
+ parts.push(String(value));
45
+ return;
46
+ }
47
+ writeObject(value, parts);
48
+ }
49
+ function writeObject(value, parts) {
50
+ const rawText = rawJsonTextOf(value);
51
+ if (rawText !== undefined) {
52
+ parts.push(rawText);
53
+ return;
54
+ }
55
+ const toJson = value.toJSON;
56
+ if (typeof toJson === "function") {
57
+ writeValue(toJson.call(value), parts);
58
+ return;
59
+ }
60
+ if (Array.isArray(value)) {
61
+ writeArray(value, parts);
62
+ return;
63
+ }
64
+ writeProperties(value, parts);
65
+ }
66
+ function writeArray(items, parts) {
67
+ parts.push("[");
68
+ items.forEach((item, index) => {
69
+ if (index > 0) {
70
+ parts.push(",");
71
+ }
72
+ writeValue(item, parts);
73
+ });
74
+ parts.push("]");
75
+ }
76
+ /** An object's own enumerable properties; an `undefined` one is skipped, as `JSON.stringify` skips it. */
77
+ function writeProperties(object, parts) {
78
+ let first = true;
79
+ parts.push("{");
80
+ for (const key of Object.keys(object)) {
81
+ const member = object[key];
82
+ if (member === undefined) {
83
+ continue;
84
+ }
85
+ if (!first) {
86
+ parts.push(",");
87
+ }
88
+ first = false;
89
+ parts.push(quoted(key), ":");
90
+ writeValue(member, parts);
91
+ }
92
+ parts.push("}");
93
+ }
94
+ function numberText(value) {
95
+ if (!Number.isFinite(value)) {
96
+ throw new ArgumentException(NON_FINITE_NUMBER_MESSAGE);
97
+ }
98
+ return formatDouble(value);
99
+ }
100
+ function quoted(text) {
101
+ for (let index = 0; index < text.length; index++) {
102
+ if (isEscaped(text.charCodeAt(index))) {
103
+ return `"${escaped(text)}"`;
104
+ }
105
+ }
106
+ return `"${text}"`;
107
+ }
108
+ /** Whether `JavaScriptEncoder.Default` escapes the UTF-16 code unit `code`. */
109
+ function isEscaped(code) {
110
+ switch (code) {
111
+ case 0x22: // "
112
+ case 0x26: // &
113
+ case 0x27: // '
114
+ case 0x2b: // +
115
+ case 0x3c: // <
116
+ case 0x3e: // >
117
+ case 0x5c: // backslash
118
+ case 0x60: // `
119
+ return true;
120
+ default:
121
+ return code < 0x20 || code >= 0x7f;
122
+ }
123
+ }
124
+ /** The short escape STJ keeps for `code`, or `undefined`. */
125
+ function shortEscapeOf(code) {
126
+ switch (code) {
127
+ case 0x08:
128
+ return "\\b";
129
+ case 0x09:
130
+ return "\\t";
131
+ case 0x0a:
132
+ return "\\n";
133
+ case 0x0c:
134
+ return "\\f";
135
+ case 0x0d:
136
+ return "\\r";
137
+ case 0x5c:
138
+ return "\\\\";
139
+ default:
140
+ return undefined;
141
+ }
142
+ }
143
+ function escaped(text) {
144
+ let result = "";
145
+ for (let index = 0; index < text.length; index++) {
146
+ const code = text.charCodeAt(index);
147
+ if (isHighSurrogate(code) && isLowSurrogate(text.charCodeAt(index + 1))) {
148
+ result += unicodeEscape(code) + unicodeEscape(text.charCodeAt(index + 1));
149
+ index++;
150
+ }
151
+ else {
152
+ result += escapedCodeUnit(code);
153
+ }
154
+ }
155
+ return result;
156
+ }
157
+ function escapedCodeUnit(code) {
158
+ const shortEscape = shortEscapeOf(code);
159
+ if (shortEscape !== undefined) {
160
+ return shortEscape;
161
+ }
162
+ if (isHighSurrogate(code) || isLowSurrogate(code)) {
163
+ return unicodeEscape(0xfffd);
164
+ }
165
+ return isEscaped(code) ? unicodeEscape(code) : String.fromCharCode(code);
166
+ }
167
+ function unicodeEscape(code) {
168
+ return `\\u${code.toString(16).toUpperCase().padStart(4, "0")}`;
169
+ }
170
+ function isHighSurrogate(code) {
171
+ return code >= 0xd800 && code <= 0xdbff;
172
+ }
173
+ function isLowSurrogate(code) {
174
+ return code >= 0xdc00 && code <= 0xdfff;
175
+ }
@@ -15,7 +15,8 @@ export declare function readNull(spec: JsonTypeInfo): null;
15
15
  * `JsonException`, or `undefined` when the descriptor reads `null` (#471,
16
16
  * measured on .NET 10): a `string` primitive, containers, `byte[]`, a
17
17
  * `nullable` and a `ref` read `null`; a numeric or `bool`/`char` primitive,
18
- * an enum, a branded value, a temporal value and a `decimal` reject it,
18
+ * an enum, a branded value, a temporal value, a `decimal` and an
19
+ * `ImmutableArray<T>` (the one value-type collection, #713) reject it,
19
20
  * naming the type the descriptor carries (the element type, wherever the
20
21
  * value sits; the declaring contract names itself at the property).
21
22
  */
@@ -20,7 +20,8 @@ export function readNull(spec) {
20
20
  * `JsonException`, or `undefined` when the descriptor reads `null` (#471,
21
21
  * measured on .NET 10): a `string` primitive, containers, `byte[]`, a
22
22
  * `nullable` and a `ref` read `null`; a numeric or `bool`/`char` primitive,
23
- * an enum, a branded value, a temporal value and a `decimal` reject it,
23
+ * an enum, a branded value, a temporal value, a `decimal` and an
24
+ * `ImmutableArray<T>` (the one value-type collection, #713) reject it,
24
25
  * naming the type the descriptor carries (the element type, wherever the
25
26
  * value sits; the declaring contract names itself at the property).
26
27
  */
@@ -36,6 +37,7 @@ export function valueTypeNameOf(descriptor) {
36
37
  case "enum":
37
38
  case "numericEnum":
38
39
  case "branded":
40
+ case "immutableArray":
39
41
  return descriptor.typeName;
40
42
  default:
41
43
  return undefined;
@@ -15,6 +15,8 @@ export interface SignificandParts {
15
15
  }
16
16
  /** Whether `value` fits `numberType`'s integer range. */
17
17
  export declare function isWithinIntegerRange(value: bigint, numberType: IntegerTypeName): boolean;
18
+ /** Whether `numberType` is an unsigned integer type, which takes no sign. */
19
+ export declare function isUnsignedIntegerType(numberType: IntegerTypeName): boolean;
18
20
  /**
19
21
  * The number `text` spells as a `numberType`, or `undefined` when System.Text.Json
20
22
  * rejects it. The 64-bit types answer a `bigint` in range (spec 012 FR-054);
@@ -75,10 +75,10 @@ function isWithinRange(value, min, max) {
75
75
  }
76
76
  /**
77
77
  * Reads the floating-point grammar; `staysFinite` rejects a value that
78
- * overflows the target type. The value keeps double precision, as a JSON
79
- * number read into a `Single` does.
78
+ * overflows the target type, and `round` brings the double to the target's
79
+ * precision (a `Single` rounds to single precision, #695).
80
80
  */
81
- function floatingPointReader(staysFinite) {
81
+ function floatingPointReader(staysFinite, round) {
82
82
  return (text) => {
83
83
  const literal = FLOATING_POINT_LITERALS.get(text);
84
84
  if (literal !== undefined) {
@@ -89,12 +89,26 @@ function floatingPointReader(staysFinite) {
89
89
  return undefined;
90
90
  }
91
91
  const value = Number(text);
92
- return staysFinite(parts, value) ? value : undefined;
92
+ return staysFinite(parts, value) ? round(value) : undefined;
93
93
  };
94
94
  }
95
95
  function fitsDouble(_, value) {
96
96
  return Number.isFinite(value);
97
97
  }
98
+ /** The largest `Single`, `(2 − 2^-23) × 2^127`. */
99
+ const SINGLE_MAX_VALUE = 3.4028234663852886e38;
100
+ /**
101
+ * A finite double that {@link fitsSingle} accepted, rounded to single
102
+ * precision (#695). Only the threshold double itself would round to an
103
+ * infinity (a tie to even); its text was below the threshold, so it is
104
+ * `±Single.MaxValue`.
105
+ */
106
+ function roundToFiniteSingle(value) {
107
+ const single = Math.fround(value);
108
+ return Number.isFinite(single) || !Number.isFinite(value)
109
+ ? single
110
+ : Math.sign(single) * SINGLE_MAX_VALUE;
111
+ }
98
112
  /**
99
113
  * Whether the text stays below the `Single` overflow threshold. The double
100
114
  * is already rounded, so it decides every case but one: a double equal to
@@ -130,6 +144,10 @@ const UNSIGNED_INTEGER_TYPES = /* @__PURE__ */ new Set([
130
144
  "System.UInt32",
131
145
  "System.UInt64",
132
146
  ]);
147
+ /** Whether `numberType` is an unsigned integer type, which takes no sign. */
148
+ export function isUnsignedIntegerType(numberType) {
149
+ return UNSIGNED_INTEGER_TYPES.has(numberType);
150
+ }
133
151
  /** The integers that read exactly, as the `bigint` they compute (spec 012 FR-054). */
134
152
  const EXACT_INTEGER_TYPES = /* @__PURE__ */ new Set([
135
153
  "System.Int64",
@@ -169,8 +187,8 @@ const INTEGER_TOKEN_READERS = /* @__PURE__ */ lazyValue(() => integerReadersOf((
169
187
  : SIGNED_INTEGER_TOKEN_PATTERN, () => (value) => value));
170
188
  const NUMBER_TEXT_READERS = /* @__PURE__ */ lazyValue(() => ({
171
189
  ...STRICT_INTEGER_READERS(),
172
- "System.Single": floatingPointReader(fitsSingle),
173
- "System.Double": floatingPointReader(fitsDouble),
190
+ "System.Single": floatingPointReader(fitsSingle, roundToFiniteSingle),
191
+ "System.Double": floatingPointReader(fitsDouble, (value) => value),
174
192
  }));
175
193
  /**
176
194
  * The number `text` spells as a `numberType`, or `undefined` when System.Text.Json
@@ -28,8 +28,8 @@ export declare function primitiveConverterOf(descriptor: PrimitiveDescriptor, re
28
28
  * range-checks: a safe-integer `number` from untyped code writes its
29
29
  * digits too, and any value outside the 64-bit range — a negative one for
30
30
  * a `ulong` — or of any other kind throws `JsonException` (FR-051). A
31
- * bigint has no negative zero (`-0n === 0n`), so a `-0` write puts `0`
32
- * into the slot.
31
+ * `-0` write, a `number` or a `bigint` (`-0n === 0n`), puts `0` into the
32
+ * slot, a ulong's included (#545); a `-0` read into a ulong still throws.
33
33
  * - Read as a `bigint` in range from: a `bigint`; a `rawJSON` leaf or
34
34
  * `JsonNumberText` whose text is a JSON integer token (`-0` reads `0`,
35
35
  * `1.0` / `1e3` are not tokens, R7 — the exact text the
@@ -41,8 +41,8 @@ export declare function primitiveConverterOf(descriptor: PrimitiveDescriptor, re
41
41
  * Anything else throws System.Text.Json's conversion error (FR-052),
42
42
  * a `JsonNonIntegerNumber` (`1.0`, `1e3` read from text) included.
43
43
  * - The descriptor's `create` — a `[Branded]` type's companion factory —
44
- * builds the read value, as the branded converter does. FR-050 does not
45
- * describe that factory case; the review of slice 012e (#450) added it.
44
+ * builds the read value, as the branded converter does (added by the
45
+ * review of slice 012e, #450).
46
46
  */
47
47
  export declare function int64ConverterOf(descriptor: Int64Descriptor, readOptions: JsonReadOptions | undefined, errorTypeName?: ErrorTypeName): ValueConverter;
48
48
  /**
@@ -1,3 +1,4 @@
1
+ import { formatSingle } from "../double-format.js";
1
2
  import { JsonException } from "./errors.js";
2
3
  import { isDecimalText, readDecimalText } from "./decimal-text.js";
3
4
  import { exactBigIntLeafOf, exactNumberTextOf, JsonNonIntegerNumber, numberOfTreeValue, writeExactNumber, } from "./exact-json-number.js";
@@ -27,6 +28,8 @@ const passthroughConverter = {
27
28
  };
28
29
  /** The CLR name of a C# `char`, which reads as a one-code-unit `string`. */
29
30
  const CHAR_TYPE_NAME = "System.Char";
31
+ /** The CLR name of a C# `float`, whose values read rounded to single precision. */
32
+ const SINGLE_TYPE_NAME = "System.Single";
30
33
  /**
31
34
  * A `primitive`: values pass through both ways, except that a JSON string
32
35
  * for a numeric `numberType` is read by System.Text.Json's grammar when the
@@ -39,6 +42,9 @@ export function primitiveConverterOf(descriptor, readOptions, errorTypeName = de
39
42
  if (descriptor.typeName === CHAR_TYPE_NAME) {
40
43
  return charConverterOf(errorTypeName);
41
44
  }
45
+ if (numberType === SINGLE_TYPE_NAME) {
46
+ return singleConverterOf(readOptions, errorTypeName);
47
+ }
42
48
  if (numberType === undefined || !readsNumbersFromStrings(readOptions)) {
43
49
  return passthroughConverter;
44
50
  }
@@ -56,6 +62,41 @@ export function primitiveConverterOf(descriptor, readOptions, errorTypeName = de
56
62
  },
57
63
  };
58
64
  }
65
+ /**
66
+ * A `float`: a JSON number reads rounded to single precision, as
67
+ * System.Text.Json parses it into a `Single` (#695), and a string reads
68
+ * through the number grammar (which rounds too) when the context reads
69
+ * numbers from strings. A finite value writes the shortest text that reads
70
+ * back as the same `float`, as System.Text.Json writes it (`0.1`, not the
71
+ * double `0.10000000149011612`), through the exact-number leaf. A
72
+ * non-finite value passes through: System.Text.Json throws for it without
73
+ * `AllowNamedFloatingPointLiterals`, which Metano does not mirror.
74
+ */
75
+ function singleConverterOf(readOptions, errorTypeName) {
76
+ const readsStrings = readsNumbersFromStrings(readOptions);
77
+ return {
78
+ serialize: (value) => typeof value === "number" && Number.isFinite(value)
79
+ ? writeExactNumber(formatSingle(value), () => Math.fround(value))
80
+ : passthroughConverter.serialize(value),
81
+ deserialize: (value) => {
82
+ if (typeof value === "number") {
83
+ return Math.fround(value);
84
+ }
85
+ const exact = exactNumberTextOf(value);
86
+ if (exact !== undefined) {
87
+ return Math.fround(Number(exact));
88
+ }
89
+ if (typeof value !== "string" || !readsStrings) {
90
+ return value;
91
+ }
92
+ const number = readNumberTextAsNumber(value, SINGLE_TYPE_NAME);
93
+ if (number === undefined) {
94
+ throw cannotConvert(SINGLE_TYPE_NAME, errorTypeName);
95
+ }
96
+ return number;
97
+ },
98
+ };
99
+ }
59
100
  /**
60
101
  * A `long` / `ulong` — the `int64` descriptor (spec 012 FR-050), whatever
61
102
  * the read options:
@@ -65,8 +106,8 @@ export function primitiveConverterOf(descriptor, readOptions, errorTypeName = de
65
106
  * range-checks: a safe-integer `number` from untyped code writes its
66
107
  * digits too, and any value outside the 64-bit range — a negative one for
67
108
  * a `ulong` — or of any other kind throws `JsonException` (FR-051). A
68
- * bigint has no negative zero (`-0n === 0n`), so a `-0` write puts `0`
69
- * into the slot.
109
+ * `-0` write, a `number` or a `bigint` (`-0n === 0n`), puts `0` into the
110
+ * slot, a ulong's included (#545); a `-0` read into a ulong still throws.
70
111
  * - Read as a `bigint` in range from: a `bigint`; a `rawJSON` leaf or
71
112
  * `JsonNumberText` whose text is a JSON integer token (`-0` reads `0`,
72
113
  * `1.0` / `1e3` are not tokens, R7 — the exact text the
@@ -78,8 +119,8 @@ export function primitiveConverterOf(descriptor, readOptions, errorTypeName = de
78
119
  * Anything else throws System.Text.Json's conversion error (FR-052),
79
120
  * a `JsonNonIntegerNumber` (`1.0`, `1e3` read from text) included.
80
121
  * - The descriptor's `create` — a `[Branded]` type's companion factory —
81
- * builds the read value, as the branded converter does. FR-050 does not
82
- * describe that factory case; the review of slice 012e (#450) added it.
122
+ * builds the read value, as the branded converter does (added by the
123
+ * review of slice 012e, #450).
83
124
  */
84
125
  export function int64ConverterOf(descriptor, readOptions, errorTypeName = declaredTypeName) {
85
126
  const { numberType, create } = descriptor;
@@ -92,10 +133,7 @@ export function int64ConverterOf(descriptor, readOptions, errorTypeName = declar
92
133
  return isWithinIntegerRange(value, numberType) ? value : undefined;
93
134
  }
94
135
  if (typeof value === "number" && Number.isSafeInteger(value)) {
95
- // `-0` is a safe integer whose bigint is in a ulong's range, but a
96
- // ulong takes no sign (measured: STJ throws for `-0`).
97
- if (Object.is(value, -0) && numberType === "System.UInt64")
98
- return undefined;
136
+ // `-0` writes `0`, in a ulong slot too: `BigInt(-0)` is `0n` (#545).
99
137
  const read = BigInt(value);
100
138
  return isWithinIntegerRange(read, numberType) ? read : undefined;
101
139
  }
@@ -1,4 +1,6 @@
1
- import type { Int64Descriptor, NumericEnumDescriptor } from "./types.ts";
1
+ import type { Int64Descriptor, IntegerTypeName, NumericEnumDescriptor } from "./types.ts";
2
+ /** The underlying type of a numeric enum whose descriptor names none, the C# default. */
3
+ export declare const DEFAULT_ENUM_INTEGER_TYPE: IntegerTypeName;
2
4
  /** A numeric enum member's value: a `number`, or a `bigint` for a 64-bit enum (#701). */
3
5
  export type NumericEnumValue = number | bigint;
4
6
  /** A numeric enum descriptor of a 64-bit enum, which names its underlying type (#701). */
@@ -7,8 +9,6 @@ export type Int64EnumDescriptor = NumericEnumDescriptor & {
7
9
  };
8
10
  /** Whether a numeric enum's values are `bigint`s: its underlying type is 64-bit (#701). */
9
11
  export declare function holdsBigInts(descriptor: NumericEnumDescriptor): descriptor is Int64EnumDescriptor;
10
- /** Whether `value` is of a numeric enum's representation, a `number` or a `bigint`. */
11
- export declare function isNumericEnumValue(value: unknown): value is NumericEnumValue;
12
12
  /**
13
13
  * The declared members of a numeric enum, as `[name, value]` pairs: the
14
14
  * entries of its `as const` object whose value is a number of the enum's
@@ -1,11 +1,9 @@
1
+ /** The underlying type of a numeric enum whose descriptor names none, the C# default. */
2
+ export const DEFAULT_ENUM_INTEGER_TYPE = "System.Int32";
1
3
  /** Whether a numeric enum's values are `bigint`s: its underlying type is 64-bit (#701). */
2
4
  export function holdsBigInts(descriptor) {
3
5
  return descriptor.numberType === "System.Int64" || descriptor.numberType === "System.UInt64";
4
6
  }
5
- /** Whether `value` is of a numeric enum's representation, a `number` or a `bigint`. */
6
- export function isNumericEnumValue(value) {
7
- return typeof value === "number" || typeof value === "bigint";
8
- }
9
7
  /**
10
8
  * The declared members of a numeric enum, as `[name, value]` pairs: the
11
9
  * entries of its `as const` object whose value is a number of the enum's
@@ -0,0 +1,102 @@
1
+ /**
2
+ * The spec plans (#456, split out by #462): each contract's wire properties
3
+ * and converters compiled once per set of custom converters and kind of tree
4
+ * numbers, and the `serializeWithSpec` / `deserializeWithSpec` /
5
+ * `*WithValueSpec` entry points that run them. Converters receive the plans
6
+ * as their context (`converters.ts` imports only their type), so the two
7
+ * modules import each other in one direction at run time.
8
+ */
9
+ import type { TypeSpec, PropertySpec, JsonConverter, TypeDiscriminator, ValueSpec } from "./types.ts";
10
+ import type { ValueConverter } from "./value-converter.ts";
11
+ /**
12
+ * Collects all properties from a spec chain, walking the base hierarchy.
13
+ * Base properties come first, own properties last: the declaration order a
14
+ * factory reads, **not** the wire order (see `collectWireProperties`). An
15
+ * override appears once per spec that declares it.
16
+ */
17
+ export declare function collectProperties(spec: TypeSpec): readonly PropertySpec[];
18
+ /**
19
+ * The properties a spec writes and reads, in System.Text.Json's wire order
20
+ * (FR-009): the spec's own properties, then its base's, recursively. A JSON
21
+ * name appears once, from its most-derived declaration, and a base
22
+ * declaration whose `ts` name a more-derived spec lists in
23
+ * `ignoredProperties` (a `[JsonIgnore]` override) is left out. When any
24
+ * property has an `order` (`[JsonPropertyOrder]`), the list is then sorted by
25
+ * it, stably, across the whole chain (measured: `{"Der2":20,"Base3":3,"Der1":10,
26
+ * "Base1":1,"Der3":30,"Base2":2}` for orders -5, -5, 0, 0, 1, 5).
27
+ */
28
+ export declare function collectWireProperties(spec: TypeSpec): readonly PropertySpec[];
29
+ /**
30
+ * Everything the reads and writes of one spec share, compiled on its first
31
+ * use (#456): the wire properties (`collectWireProperties`, with ignored
32
+ * overrides), their converters resolved under the spec's read options
33
+ * (numbers from strings included), and the case-insensitive key index. A
34
+ * polymorphic base dispatches per value through its own cached dispatch
35
+ * index (`polymorphism.ts`), then runs the chosen contract's plan.
36
+ *
37
+ * Writes use the converters resolved for reads: read options never change
38
+ * how a value is written (FR-048 item 5).
39
+ */
40
+ export interface SpecPlan {
41
+ /** Writes through the spec, dispatching a polymorphic base (FR-005, FR-006). */
42
+ readonly serialize: (value: unknown) => Record<string, unknown>;
43
+ /** Reads through the spec, dispatching a polymorphic base (FR-007). */
44
+ readonly deserialize: (data: unknown) => unknown;
45
+ /** Writes `value` with exactly the spec's properties, after the tag when one is given. */
46
+ readonly writeContract: (value: unknown, typeMetadata?: TypeMetadata) => Record<string, unknown>;
47
+ /**
48
+ * Reads `raw` with exactly the spec's properties and builds it with the
49
+ * spec's factory. `metadataKey` is the discriminator a polymorphic read
50
+ * consumed.
51
+ */
52
+ readonly readContract: (raw: Record<string, unknown>, metadataKey?: string) => unknown;
53
+ }
54
+ /**
55
+ * How the tree being read holds its numbers: `parsed`, each a `number`, as
56
+ * `JSON.parse` builds it; `exact`, some as leaves, as `readJsonText` builds
57
+ * it (#457): a `JsonNumberText` for those with more significant digits than
58
+ * a double holds and, for a spec that reads a 64-bit integer, a
59
+ * `JsonNonIntegerNumber` for the other ones with a fraction or an exponent
60
+ * (`NumberTextLeaves`).
61
+ */
62
+ export type TreeNumbers = "parsed" | "exact";
63
+ /**
64
+ * The plans compiled under one set of custom converters for one kind of
65
+ * tree numbers, each built once.
66
+ */
67
+ export interface SpecPlans {
68
+ readonly customConverters: readonly JsonConverter[];
69
+ readonly treeNumbers: TreeNumbers;
70
+ planOf(spec: TypeSpec): SpecPlan;
71
+ valueConverterOf(spec: ValueSpec): ValueConverter;
72
+ }
73
+ /**
74
+ * Serialize a TS object to a JSON-safe plain object using the given spec. A
75
+ * polymorphic spec first chooses the derived contract and writes its tag
76
+ * before every property (FR-005); the chosen contract is never dispatched
77
+ * again (FR-006).
78
+ */
79
+ export declare function serializeWithSpec(value: unknown, spec: TypeSpec, customConverters?: readonly JsonConverter[]): Record<string, unknown>;
80
+ /** The type discriminator a polymorphic write puts first. */
81
+ interface TypeMetadata {
82
+ readonly name: string;
83
+ readonly tag: TypeDiscriminator;
84
+ }
85
+ /**
86
+ * Deserialize unknown data (from JSON.parse) into a typed instance using the
87
+ * spec. A polymorphic spec reads the derived contract its tag selects
88
+ * (FR-007). `treeNumbers` says how `data` holds its numbers; a root number
89
+ * leaf reads as the number it is, which no contract accepts.
90
+ */
91
+ export declare function deserializeWithSpec<T>(data: unknown, spec: TypeSpec<T>, customConverters?: readonly JsonConverter[], treeNumbers?: TreeNumbers): T;
92
+ /**
93
+ * Serializes a non-object root through its descriptor, exactly as a property
94
+ * of that type would be (custom converters still match by kind).
95
+ */
96
+ export declare function serializeWithValueSpec(value: unknown, spec: ValueSpec, customConverters?: readonly JsonConverter[]): unknown;
97
+ /**
98
+ * Deserializes a non-object root through its descriptor, under the spec's
99
+ * read options. `treeNumbers` says how `data` holds its numbers.
100
+ */
101
+ export declare function deserializeWithValueSpec(data: unknown, spec: ValueSpec, customConverters?: readonly JsonConverter[], treeNumbers?: TreeNumbers): unknown;
102
+ export {};