xbintsc 0.6.10 → 0.6.13

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 (204) hide show
  1. package/dist/src/ast/expressions.d.ts +6 -0
  2. package/dist/src/checker/globals.js +2 -1
  3. package/dist/src/checker/globals.js.map +1 -1
  4. package/dist/src/checker/type-infer.js +1 -1
  5. package/dist/src/checker/type-infer.js.map +1 -1
  6. package/dist/src/cli/help.d.ts +1 -1
  7. package/dist/src/codegen/generator/calls/literals.js +4 -1
  8. package/dist/src/codegen/generator/calls/literals.js.map +1 -1
  9. package/dist/src/codegen/generator/expressions/assignment.js +3 -1
  10. package/dist/src/codegen/generator/expressions/assignment.js.map +1 -1
  11. package/dist/src/codegen/generator/expressions/operators.js +2 -6
  12. package/dist/src/codegen/generator/expressions/operators.js.map +1 -1
  13. package/dist/src/codegen/generator/expressions/primary.d.ts +9 -0
  14. package/dist/src/codegen/generator/expressions/primary.js +44 -7
  15. package/dist/src/codegen/generator/expressions/primary.js.map +1 -1
  16. package/dist/src/codegen/generator/state.js +3 -0
  17. package/dist/src/codegen/generator/state.js.map +1 -1
  18. package/dist/src/codegen/generator/tables.js +3 -0
  19. package/dist/src/codegen/generator/tables.js.map +1 -1
  20. package/dist/src/driver/bundler/commonjs.js +1 -1
  21. package/dist/src/driver/bundler/commonjs.js.map +1 -1
  22. package/dist/src/driver/bundler/symbols.js +6 -0
  23. package/dist/src/driver/bundler/symbols.js.map +1 -1
  24. package/dist/src/driver/compiler.js +1 -1
  25. package/dist/src/driver/compiler.js.map +1 -1
  26. package/dist/src/driver/runtime-objects.d.ts +1 -1
  27. package/dist/src/driver/runtime-objects.js +2 -0
  28. package/dist/src/driver/runtime-objects.js.map +1 -1
  29. package/dist/src/driver/version.d.ts +1 -1
  30. package/dist/src/driver/version.js +1 -1
  31. package/dist/src/extensions/node/crypto/index.js +12 -1
  32. package/dist/src/extensions/node/crypto/index.js.map +1 -1
  33. package/dist/src/extensions/node/index.js +2 -0
  34. package/dist/src/extensions/node/index.js.map +1 -1
  35. package/dist/src/extensions/node/readline/index.d.ts +12 -0
  36. package/dist/src/extensions/node/readline/index.js +27 -0
  37. package/dist/src/extensions/node/readline/index.js.map +1 -0
  38. package/dist/src/parser/declarations/functions.js +6 -0
  39. package/dist/src/parser/declarations/functions.js.map +1 -1
  40. package/dist/src/parser/literals.js +1 -1
  41. package/dist/src/parser/literals.js.map +1 -1
  42. package/dist/tests/e2e/compile-builtins.test.js +26 -0
  43. package/dist/tests/e2e/compile-builtins.test.js.map +1 -1
  44. package/dist/tests/e2e/compile-modules.test.js +22 -0
  45. package/dist/tests/e2e/compile-modules.test.js.map +1 -1
  46. package/dist/tests/e2e/compile-node.test.js +37 -1
  47. package/dist/tests/e2e/compile-node.test.js.map +1 -1
  48. package/dist/tests/e2e/crypto-random.test.d.ts +9 -0
  49. package/dist/tests/e2e/crypto-random.test.js +63 -0
  50. package/dist/tests/e2e/crypto-random.test.js.map +1 -0
  51. package/dist/tests/e2e/date-parse.test.js +44 -1
  52. package/dist/tests/e2e/date-parse.test.js.map +1 -1
  53. package/dist/tests/e2e/gui-browser.test.js +61 -0
  54. package/dist/tests/e2e/gui-browser.test.js.map +1 -1
  55. package/dist/tests/e2e/gui-css-coverage.test.js +40 -1
  56. package/dist/tests/e2e/gui-css-coverage.test.js.map +1 -1
  57. package/dist/tests/e2e/gui-css-features.test.js +100 -0
  58. package/dist/tests/e2e/gui-css-features.test.js.map +1 -1
  59. package/dist/tests/e2e/gui-dom.test.js +63 -0
  60. package/dist/tests/e2e/gui-dom.test.js.map +1 -1
  61. package/dist/tests/e2e/gui-errors.test.d.ts +11 -0
  62. package/dist/tests/e2e/gui-errors.test.js +68 -0
  63. package/dist/tests/e2e/gui-errors.test.js.map +1 -0
  64. package/dist/tests/e2e/gui-form.test.d.ts +9 -0
  65. package/dist/tests/e2e/gui-form.test.js +359 -0
  66. package/dist/tests/e2e/gui-form.test.js.map +1 -0
  67. package/dist/tests/e2e/gui-script-import-order.test.js +6 -1
  68. package/dist/tests/e2e/gui-script-import-order.test.js.map +1 -1
  69. package/dist/tests/e2e/gui-style.test.js +82 -0
  70. package/dist/tests/e2e/gui-style.test.js.map +1 -1
  71. package/dist/tests/e2e/gui-unsupported.test.js +4 -3
  72. package/dist/tests/e2e/gui-unsupported.test.js.map +1 -1
  73. package/dist/tests/e2e/gui-webgl2-shadow-sampler.test.js +7 -1
  74. package/dist/tests/e2e/gui-webgl2-shadow-sampler.test.js.map +1 -1
  75. package/dist/tests/e2e/harness.d.ts +4 -0
  76. package/dist/tests/e2e/harness.js +2 -0
  77. package/dist/tests/e2e/harness.js.map +1 -1
  78. package/dist/tests/e2e/json-parse-errors.test.d.ts +9 -0
  79. package/dist/tests/e2e/json-parse-errors.test.js +48 -0
  80. package/dist/tests/e2e/json-parse-errors.test.js.map +1 -0
  81. package/dist/tests/e2e/readline.test.d.ts +7 -0
  82. package/dist/tests/e2e/readline.test.js +73 -0
  83. package/dist/tests/e2e/readline.test.js.map +1 -0
  84. package/doc/ai/language-support.md +2 -1
  85. package/doc/ai/zh-CN/language-support.md +2 -1
  86. package/doc/gui/api.md +24 -3
  87. package/doc/gui/dom.md +28 -14
  88. package/doc/gui/input.md +74 -3
  89. package/doc/gui/known-issues.md +2 -1
  90. package/doc/gui/layout.md +5 -1
  91. package/doc/implemented/node/crypto.md +8 -0
  92. package/doc/implemented/node/fs.md +1 -1
  93. package/doc/implemented/node/readline.md +34 -0
  94. package/doc/implemented/node.md +1 -0
  95. package/doc/implemented/stdlib.md +10 -2
  96. package/doc/unimplemented/node/modules.md +2 -3
  97. package/doc/unimplemented/node.md +2 -2
  98. package/doc/zh-CN/gui/api.md +20 -3
  99. package/doc/zh-CN/gui/dom.md +23 -13
  100. package/doc/zh-CN/gui/input.md +65 -4
  101. package/doc/zh-CN/gui/known-issues.md +2 -1
  102. package/doc/zh-CN/gui/layout.md +4 -1
  103. package/doc/zh-CN/implemented/node/crypto.md +6 -0
  104. package/doc/zh-CN/implemented/node/fs.md +1 -1
  105. package/doc/zh-CN/implemented/node/readline.md +33 -0
  106. package/doc/zh-CN/implemented/node.md +1 -0
  107. package/doc/zh-CN/implemented/stdlib.md +10 -2
  108. package/doc/zh-CN/unimplemented/node/modules.md +1 -2
  109. package/doc/zh-CN/unimplemented/node.md +2 -2
  110. package/package.json +7 -7
  111. package/runtime/ext_gui/browser_abort.cpp +2 -2
  112. package/runtime/ext_gui/browser_loop.cpp +1 -0
  113. package/runtime/ext_gui/browser_media.cpp +1 -1
  114. package/runtime/ext_gui/css_match.cpp +21 -1
  115. package/runtime/ext_gui/css_selector.cpp +2 -2
  116. package/runtime/ext_gui/document.cpp +12 -0
  117. package/runtime/ext_gui/dom.h +41 -2
  118. package/runtime/ext_gui/dom_api_canvas_image.cpp +2 -1
  119. package/runtime/ext_gui/dom_api_document.cpp +28 -0
  120. package/runtime/ext_gui/dom_api_events.cpp +18 -15
  121. package/runtime/ext_gui/dom_api_form.cpp +567 -0
  122. package/runtime/ext_gui/dom_api_internal.h +48 -214
  123. package/runtime/ext_gui/dom_api_mutation.cpp +1 -1
  124. package/runtime/ext_gui/dom_api_node.cpp +28 -15
  125. package/runtime/ext_gui/dom_api_node_read.cpp +28 -1
  126. package/runtime/ext_gui/dom_api_proto.cpp +47 -0
  127. package/runtime/ext_gui/dom_api_webgl_methods.h +217 -0
  128. package/runtime/ext_gui/dom_parse.cpp +114 -1
  129. package/runtime/ext_gui/error_report.cpp +166 -0
  130. package/runtime/ext_gui/form_focus.cpp +293 -0
  131. package/runtime/ext_gui/form_input.cpp +448 -0
  132. package/runtime/ext_gui/form_internal.h +30 -0
  133. package/runtime/ext_gui/form_shared.cpp +126 -0
  134. package/runtime/ext_gui/form_state.cpp +536 -0
  135. package/runtime/ext_gui/form_state.h +97 -0
  136. package/runtime/ext_gui/gui.cpp +16 -0
  137. package/runtime/ext_gui/gui_engine.h +53 -4
  138. package/runtime/ext_gui/gui_events.cpp +67 -49
  139. package/runtime/ext_gui/layout.cpp +6 -0
  140. package/runtime/ext_gui/layout.h +4 -0
  141. package/runtime/ext_gui/layout_flow.cpp +23 -1
  142. package/runtime/ext_gui/layout_inline.cpp +46 -14
  143. package/runtime/ext_gui/paint.cpp +26 -1
  144. package/runtime/ext_gui/paint_form.cpp +389 -0
  145. package/runtime/ext_gui/paint_internal.h +6 -0
  146. package/runtime/ext_gui/script.cpp +1 -1
  147. package/runtime/ext_gui/style.cpp +1 -0
  148. package/runtime/ext_gui/style.h +13 -0
  149. package/runtime/ext_gui/style_print.cpp +129 -0
  150. package/runtime/ext_gui/style_text.cpp +79 -0
  151. package/runtime/ext_gui/style_ua.cpp +42 -0
  152. package/runtime/ext_gui/warnings.cpp +5 -7
  153. package/runtime/ext_gui/webgl_texture.cpp +0 -8
  154. package/runtime/ext_gui/window.cpp +52 -62
  155. package/runtime/ext_gui/window_diagnostics.cpp +58 -0
  156. package/runtime/ext_gui/window_emit.cpp +51 -0
  157. package/runtime/ext_node/crypto/crypto.c +3 -2
  158. package/runtime/ext_node/crypto/crypto_hash.h +13 -0
  159. package/runtime/ext_node/crypto/crypto_random.c +339 -0
  160. package/runtime/ext_node/fs/async_worker.c +1 -1
  161. package/runtime/ext_node/fs/fs_ops.c +1 -1
  162. package/runtime/ext_node/fs/parts/platform.inc +67 -0
  163. package/runtime/ext_node/readline/readline.c +572 -0
  164. package/runtime/rt.h +42 -0
  165. package/runtime/rt_internal.h +3 -2
  166. package/runtime/xt_crypto.c +132 -0
  167. package/runtime/xt_io/exception.inc +29 -6
  168. package/runtime/xt_loop.c +36 -1
  169. package/runtime/xt_promise.c +26 -2
  170. package/runtime/xt_random.c +143 -0
  171. package/runtime/xt_regex/matcher.inc +9 -28
  172. package/runtime/xt_regex/parser.inc +65 -2
  173. package/runtime/xt_regex/types.inc +11 -1
  174. package/runtime/xt_regex.h +6 -2
  175. package/runtime/xt_stdlib/dispatch.inc +2 -0
  176. package/runtime/xt_stdlib/math.inc +1 -1
  177. package/runtime/xt_stdlib2/date.inc +206 -6
  178. package/runtime/xt_stdlib2/json.inc +4 -546
  179. package/runtime/xt_stdlib2/json_parse.inc +347 -0
  180. package/runtime/xt_stdlib2/json_stringify.inc +368 -0
  181. package/runtime/xt_stdlib2/regexp-match.inc +6 -11
  182. package/runtime/xt_stdlib2.c +0 -4
  183. package/runtime/xt_typed_array/elements.inc +2 -6
  184. package/runtime/xt_url.c +0 -6
  185. package/runtime/xt_url_object.c +1 -1
  186. package/src/ast/expressions.ts +6 -0
  187. package/src/checker/globals.ts +2 -0
  188. package/src/checker/type-infer.ts +1 -1
  189. package/src/codegen/generator/calls/literals.ts +4 -1
  190. package/src/codegen/generator/expressions/assignment.ts +3 -1
  191. package/src/codegen/generator/expressions/operators.ts +2 -6
  192. package/src/codegen/generator/expressions/primary.ts +58 -6
  193. package/src/codegen/generator/state.ts +3 -0
  194. package/src/codegen/generator/tables.ts +3 -0
  195. package/src/driver/bundler/commonjs.ts +1 -1
  196. package/src/driver/bundler/symbols.ts +6 -0
  197. package/src/driver/compiler.ts +1 -1
  198. package/src/driver/runtime-objects.ts +2 -0
  199. package/src/driver/version.ts +1 -1
  200. package/src/extensions/node/crypto/index.ts +12 -1
  201. package/src/extensions/node/index.ts +2 -0
  202. package/src/extensions/node/readline/index.ts +31 -0
  203. package/src/parser/declarations/functions.ts +6 -0
  204. package/src/parser/literals.ts +1 -1
@@ -1,547 +1,5 @@
1
1
  /* -- JSON ----------------------------------------------------------------- */
2
-
3
- typedef struct { const char *data; size_t length; size_t pos; } xt_json_cursor;
4
-
5
- static void xt_json_skip_ws(xt_json_cursor *c) {
6
- while (c->pos < c->length) {
7
- char ch = c->data[c->pos];
8
- if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r') c->pos++;
9
- else break;
10
- }
11
- }
12
-
13
- static xt_value xt_json_parse_value(xt_json_cursor *c);
14
-
15
- static xt_value xt_json_parse_string(xt_json_cursor *c) {
16
- c->pos++;
17
- size_t capacity = 32, length = 0;
18
- char *out = (char *)malloc(capacity);
19
- if (!out) abort();
20
- while (c->pos < c->length) {
21
- char ch = c->data[c->pos++];
22
- if (ch == '"') break;
23
- if (ch == '\\' && c->pos < c->length) {
24
- char esc = c->data[c->pos++];
25
- if (esc == 'n') ch = '\n';
26
- else if (esc == 't') ch = '\t';
27
- else if (esc == 'r') ch = '\r';
28
- else if (esc == 'b') ch = '\b';
29
- else if (esc == 'f') ch = '\f';
30
- else if (esc == 'u') {
31
- unsigned code = 0;
32
- for (int i = 0; i < 4 && c->pos < c->length; i++) {
33
- char h = c->data[c->pos++];
34
- code <<= 4;
35
- if (h >= '0' && h <= '9') code |= (unsigned)(h - '0');
36
- else if (h >= 'a' && h <= 'f') code |= (unsigned)(h - 'a' + 10);
37
- else if (h >= 'A' && h <= 'F') code |= (unsigned)(h - 'A' + 10);
38
- }
39
- /* A high surrogate followed by `\u` and a low surrogate denotes one
40
- * supplementary code point, exactly like a JavaScript string. */
41
- if (code >= 0xD800 && code <= 0xDBFF && c->pos + 5 < c->length &&
42
- c->data[c->pos] == '\\' && c->data[c->pos + 1] == 'u') {
43
- unsigned low = 0;
44
- int valid = 1;
45
- for (int i = 0; i < 4; i++) {
46
- char h = c->data[c->pos + 2 + (size_t)i];
47
- low <<= 4;
48
- if (h >= '0' && h <= '9') low |= (unsigned)(h - '0');
49
- else if (h >= 'a' && h <= 'f') low |= (unsigned)(h - 'a' + 10);
50
- else if (h >= 'A' && h <= 'F') low |= (unsigned)(h - 'A' + 10);
51
- else valid = 0;
52
- }
53
- if (valid && low >= 0xDC00 && low <= 0xDFFF) {
54
- code = 0x10000 + ((code - 0xD800) << 10) + (low - 0xDC00);
55
- c->pos += 6;
56
- }
57
- }
58
- if (code >= 0x80) {
59
- char tmp[4];
60
- size_t bytes = xt_utf8_encode(code, tmp);
61
- if (length + bytes + 1 > capacity) { capacity = (length + bytes + 1) * 2; out = (char *)realloc(out, capacity); if (!out) abort(); }
62
- memcpy(out + length, tmp, bytes);
63
- length += bytes;
64
- continue;
65
- }
66
- ch = (char)code;
67
- } else ch = esc;
68
- }
69
- if (length + 2 > capacity) { capacity *= 2; out = (char *)realloc(out, capacity); if (!out) abort(); }
70
- out[length++] = ch;
71
- }
72
- xt_value result = xt_string_new(out, length);
73
- free(out);
74
- return result;
75
- }
76
-
77
- static xt_value xt_json_parse_number(xt_json_cursor *c) {
78
- size_t start = c->pos;
79
- if (c->pos < c->length && (c->data[c->pos] == '-' || c->data[c->pos] == '+')) c->pos++;
80
- while (c->pos < c->length && (isdigit((unsigned char)c->data[c->pos]) || c->data[c->pos] == '.' ||
81
- c->data[c->pos] == 'e' || c->data[c->pos] == 'E' ||
82
- c->data[c->pos] == '+' || c->data[c->pos] == '-')) c->pos++;
83
- char buffer[64];
84
- size_t n = c->pos - start;
85
- if (n >= sizeof(buffer)) n = sizeof(buffer) - 1;
86
- memcpy(buffer, c->data + start, n);
87
- buffer[n] = '\0';
88
- return xt_number(strtod(buffer, NULL));
89
- }
90
-
91
- static xt_value xt_json_parse_value(xt_json_cursor *c) {
92
- xt_json_skip_ws(c);
93
- if (c->pos >= c->length) return XT_UNDEFINED;
94
- char ch = c->data[c->pos];
95
- if (ch == '"') return xt_json_parse_string(c);
96
- if (ch == '{') {
97
- c->pos++;
98
- xt_value object = xt_object_new();
99
- xt_json_skip_ws(c);
100
- if (c->pos < c->length && c->data[c->pos] == '}') { c->pos++; return object; }
101
- for (;;) {
102
- xt_json_skip_ws(c);
103
- if (c->pos >= c->length || c->data[c->pos] != '"') break;
104
- xt_value key = xt_json_parse_string(c);
105
- xt_json_skip_ws(c);
106
- if (c->pos < c->length && c->data[c->pos] == ':') c->pos++;
107
- xt_value value = xt_json_parse_value(c);
108
- xt_object_set(object, key, value);
109
- xt_json_skip_ws(c);
110
- if (c->pos < c->length && c->data[c->pos] == ',') { c->pos++; continue; }
111
- if (c->pos < c->length && c->data[c->pos] == '}') c->pos++;
112
- break;
113
- }
114
- return object;
115
- }
116
- if (ch == '[') {
117
- c->pos++;
118
- xt_value array = xt_array_new(0, NULL);
119
- xt_json_skip_ws(c);
120
- if (c->pos < c->length && c->data[c->pos] == ']') { c->pos++; return array; }
121
- for (;;) {
122
- xt_array_push(array, xt_json_parse_value(c));
123
- xt_json_skip_ws(c);
124
- if (c->pos < c->length && c->data[c->pos] == ',') { c->pos++; continue; }
125
- if (c->pos < c->length && c->data[c->pos] == ']') c->pos++;
126
- break;
127
- }
128
- return array;
129
- }
130
- if (c->pos + 4 <= c->length && memcmp(c->data + c->pos, "true", 4) == 0) { c->pos += 4; return XT_TRUE; }
131
- if (c->pos + 5 <= c->length && memcmp(c->data + c->pos, "false", 5) == 0) { c->pos += 5; return XT_FALSE; }
132
- if (c->pos + 4 <= c->length && memcmp(c->data + c->pos, "null", 4) == 0) { c->pos += 4; return XT_NULL; }
133
- return xt_json_parse_number(c);
134
- }
135
-
136
- /* Reviver walk: rebuild each container bottom-up, then pass the value to the
137
- * user callback with its holder as `this`. Returning undefined deletes an
138
- * object property / array element, exactly like the specification. */
139
- static xt_value xt_json_revive(xt_value holder, xt_value key, xt_value reviver) {
140
- xt_value value = xt_get(holder, key);
141
- if (XT_IS_ARRAY(value)) {
142
- xt_array *array = xt_as_array(value);
143
- for (uint32_t i = 0; i < array->length; i++) {
144
- /* Reviver keys are strings even for array indices. */
145
- char keyBuffer[16];
146
- snprintf(keyBuffer, sizeof(keyBuffer), "%u", i);
147
- xt_value index = xt_string_from_cstr(keyBuffer);
148
- xt_value updated = xt_json_revive(value, index, reviver);
149
- if (updated == XT_UNDEFINED) xt_delete(value, index);
150
- else xt_set(value, index, updated);
151
- }
152
- } else if (XT_IS_OBJECT(value)) {
153
- xt_value keys = xt_object_keys(value);
154
- xt_array *list = xt_as_array(keys);
155
- if (list) {
156
- for (uint32_t i = 0; i < list->length; i++) {
157
- xt_value childKey = list->items[i];
158
- xt_value updated = xt_json_revive(value, childKey, reviver);
159
- if (updated == XT_UNDEFINED) xt_delete(value, childKey);
160
- else xt_object_set(value, childKey, updated);
161
- }
162
- }
163
- }
164
- xt_value args[2] = {key, value};
165
- return xt_call_with_this(reviver, holder, 2, args);
166
- }
167
-
168
- xt_value xt_json_parse(int32_t argc, xt_value *argv) {
169
- xt_value input = xt_to_string(xt_arg_at(argc, argv, 0));
170
- xt_string *s = xt_as_string(input);
171
- if (!s) return XT_UNDEFINED;
172
- xt_json_cursor cursor = {xt_string_bytes(s), s->length, 0};
173
- xt_value parsed = xt_json_parse_value(&cursor);
174
- xt_value reviver = xt_arg_at(argc, argv, 1);
175
- if (!XT_IS_FUNCTION(reviver)) return parsed;
176
- xt_value holder = xt_object_new();
177
- xt_object_set(holder, xt_string_from_cstr(""), parsed);
178
- return xt_json_revive(holder, xt_string_from_cstr(""), reviver);
179
- }
180
-
181
- static void xt_json_stringify_value(xt_value value, xt_value indent, int depth, xt_value *out);
182
-
183
- /* The indentation unit for the `space` argument: up to 10 spaces for a
184
- * number, or the first 10 UTF-16 code units of a string (any characters,
185
- * not just spaces), matching JSON.stringify. An empty string means compact. */
186
- static xt_value xt_json_space_unit(xt_value space) {
187
- if (XT_IS_NUMBER(space)) {
188
- int count = xt_to_int32(space);
189
- if (count < 0) count = 0;
190
- if (count > 10) count = 10;
191
- char spaces[10];
192
- memset(spaces, ' ', (size_t)count);
193
- return xt_string_new(spaces, (size_t)count);
194
- }
195
- if (XT_IS_STRING(space)) {
196
- xt_string *s = xt_as_string(space);
197
- const char *bytes = xt_string_bytes(s);
198
- size_t offset = 0;
199
- size_t end = 0;
200
- uint32_t units = 0;
201
- while (offset < s->length && units < 10) {
202
- size_t width = 0;
203
- uint32_t code = xt_utf8_decode(bytes + offset, s->length - offset, &width);
204
- units += code >= 0x10000 ? 2 : 1;
205
- offset += width;
206
- if (units <= 10) end = offset;
207
- }
208
- return xt_string_new(bytes, end);
209
- }
210
- return xt_string_from_cstr("");
211
- }
212
-
213
- static int xt_json_indented(xt_value unit) {
214
- return XT_IS_STRING(unit) && xt_as_string(unit)->length > 0;
215
- }
216
-
217
- static void xt_json_indent(xt_value *out, xt_value unit, int depth) {
218
- if (!xt_json_indented(unit)) return;
219
- *out = xt_add(*out, xt_string_from_cstr("\n"));
220
- for (int i = 0; i < depth; i++) *out = xt_add(*out, unit);
221
- }
222
-
223
- /* Containers on the current serialization path. A repeated pointer is a cycle;
224
- * serializing it would recurse until the native stack overflowed (a hard crash,
225
- * not the TypeError the platform throws). */
226
- static xt_value *xt_json_path = NULL;
227
- static uint32_t xt_json_path_count = 0;
228
- static uint32_t xt_json_path_capacity = 0;
229
-
230
- static void xt_json_enter(xt_value value) {
231
- for (uint32_t i = 0; i < xt_json_path_count; i++) {
232
- if (xt_json_path[i] == value) {
233
- xt_value message = xt_string_from_cstr("Converting circular structure to JSON");
234
- xt_throw(xt_type_error_ctor(1, &message));
235
- }
236
- }
237
- if (xt_json_path_count >= xt_json_path_capacity) {
238
- uint32_t capacity = xt_json_path_capacity == 0 ? 16 : xt_json_path_capacity * 2;
239
- xt_value *path = (xt_value *)realloc(xt_json_path, sizeof(xt_value) * capacity);
240
- if (!path) abort();
241
- xt_json_path = path;
242
- xt_json_path_capacity = capacity;
243
- }
244
- xt_json_path[xt_json_path_count++] = value;
245
- }
246
-
247
- static void xt_json_leave(void) {
248
- if (xt_json_path_count > 0) xt_json_path_count--;
249
- }
250
-
251
- static void xt_json_stringify_array(xt_value value, xt_value indent, int depth, xt_value *out) {
252
- xt_array *array = xt_as_array(value);
253
- if (!array || array->length == 0) { *out = xt_add(*out, xt_string_from_cstr("[]")); return; }
254
- *out = xt_add(*out, xt_string_from_cstr("["));
255
- xt_json_enter(value);
256
- for (uint32_t i = 0; i < array->length; i++) {
257
- if (i > 0) *out = xt_add(*out, xt_string_from_cstr(","));
258
- xt_json_indent(out, indent, depth + 1);
259
- xt_json_stringify_value(array->items[i], indent, depth + 1, out);
260
- }
261
- xt_json_indent(out, indent, depth);
262
- xt_json_leave();
263
- *out = xt_add(*out, xt_string_from_cstr("]"));
264
- }
265
-
266
- static void xt_json_stringify_object(xt_value value, xt_value indent, int depth, xt_value *out) {
267
- xt_value keys = xt_object_keys(value);
268
- xt_array *array = xt_as_array(keys);
269
- if (!array) { *out = xt_add(*out, xt_string_from_cstr("{}")); return; }
270
- int any = 0;
271
- for (uint32_t i = 0; i < array->length; i++) {
272
- xt_value item = xt_get(value, array->items[i]);
273
- if (item != XT_UNDEFINED && !XT_IS_FUNCTION(item) && !xt_is_symbol(item)) { any = 1; break; }
274
- }
275
- if (!any) { *out = xt_add(*out, xt_string_from_cstr("{}")); return; }
276
- *out = xt_add(*out, xt_string_from_cstr("{"));
277
- xt_json_enter(value);
278
- int written = 0;
279
- for (uint32_t i = 0; i < array->length; i++) {
280
- xt_value key = array->items[i];
281
- xt_value item = xt_get(value, key);
282
- /* `undefined`, functions and symbols are omitted from objects. */
283
- if (item == XT_UNDEFINED || XT_IS_FUNCTION(item) || xt_is_symbol(item)) continue;
284
- if (written > 0) *out = xt_add(*out, xt_string_from_cstr(","));
285
- written++;
286
- xt_json_indent(out, indent, depth + 1);
287
- *out = xt_add(*out, xt_string_from_cstr("\""));
288
- *out = xt_add(*out, key);
289
- *out = xt_add(*out, xt_string_from_cstr("\":"));
290
- if (xt_json_indented(indent)) *out = xt_add(*out, xt_string_from_cstr(" "));
291
- xt_json_stringify_value(item, indent, depth + 1, out);
292
- }
293
- xt_json_indent(out, indent, depth);
294
- xt_json_leave();
295
- *out = xt_add(*out, xt_string_from_cstr("}"));
296
- }
297
-
298
- static void xt_json_stringify_value(xt_value value, xt_value indent, int depth, xt_value *out) {
299
- if (value == XT_UNDEFINED || value == XT_NULL || XT_IS_FUNCTION(value) || xt_is_symbol(value)) {
300
- *out = xt_add(*out, xt_string_from_cstr("null"));
301
- return;
302
- }
303
- if (XT_IS_BIGINT(value)) {
304
- xt_throw(xt_error_from_message("TypeError: Do not know how to serialize a BigInt"));
305
- return;
306
- }
307
- if (XT_IS_BOOL(value)) { *out = xt_add(*out, value == XT_TRUE ? xt_string_from_cstr("true") : xt_string_from_cstr("false")); return; }
308
- if (XT_IS_NUMBER(value)) {
309
- double d = xt_to_double(value);
310
- if (!isfinite(d)) *out = xt_add(*out, xt_string_from_cstr("null"));
311
- else *out = xt_add(*out, xt_to_string(value));
312
- return;
313
- }
314
- if (XT_IS_STRING(value)) {
315
- xt_string *s = xt_as_string(value);
316
- const char *bytes = xt_string_bytes(s);
317
- *out = xt_add(*out, xt_string_from_cstr("\""));
318
- size_t offset = 0;
319
- while (offset < s->length) {
320
- size_t width = 0;
321
- uint32_t code = xt_utf8_decode(bytes + offset, s->length - offset, &width);
322
- const char *escape = NULL;
323
- char buffer[8];
324
- if (code == '"') escape = "\\\"";
325
- else if (code == '\\') escape = "\\\\";
326
- else if (code == '\b') escape = "\\b";
327
- else if (code == '\f') escape = "\\f";
328
- else if (code == '\n') escape = "\\n";
329
- else if (code == '\r') escape = "\\r";
330
- else if (code == '\t') escape = "\\t";
331
- else if (code < 0x20 || (code >= 0xD800 && code <= 0xDFFF)) {
332
- snprintf(buffer, sizeof(buffer), "\\u%04x", (unsigned)code);
333
- escape = buffer;
334
- }
335
- if (escape) *out = xt_add(*out, xt_string_from_cstr(escape));
336
- else *out = xt_add(*out, xt_string_new(bytes + offset, width));
337
- offset += width;
338
- }
339
- *out = xt_add(*out, xt_string_from_cstr("\""));
340
- return;
341
- }
342
- if (XT_IS_ARRAY(value)) { xt_json_stringify_array(value, indent, depth, out); return; }
343
- if (XT_IS_OBJECT(value)) {
344
- /* Map/Set/Date/RegExp/Promise/generators are not property-bag objects;
345
- * handing them to xt_json_stringify_object reads their fields as a property
346
- * table and crashed with an access violation. JSON sees no enumerable own
347
- * properties on the first group and calls Date.prototype.toJSON. */
348
- xt_object *object = (xt_object *)XT_GET_PTR(value);
349
- switch (object->header.kind) {
350
- case XT_OBJECT_KIND_MAP:
351
- case XT_OBJECT_KIND_SET:
352
- case XT_OBJECT_KIND_REGEXP:
353
- case XT_OBJECT_KIND_PROMISE:
354
- case XT_OBJECT_KIND_GENERATOR:
355
- case XT_OBJECT_KIND_ITERATOR:
356
- case XT_OBJECT_KIND_ASYNC:
357
- *out = xt_add(*out, xt_string_from_cstr("{}"));
358
- return;
359
- case XT_OBJECT_KIND_DATE: {
360
- xt_value time = xt_call_method(value, xt_string_from_cstr("getTime"), 0, NULL);
361
- if (isnan(xt_to_number(time))) {
362
- *out = xt_add(*out, xt_string_from_cstr("null"));
363
- return;
364
- }
365
- xt_value text = xt_call_method(value, xt_string_from_cstr("toJSON"), 0, NULL);
366
- xt_json_stringify_value(text, indent, depth, out);
367
- return;
368
- }
369
- default:
370
- break;
371
- }
372
- xt_json_stringify_object(value, indent, depth, out);
373
- return;
374
- }
375
- *out = xt_add(*out, xt_string_from_cstr("null"));
376
- }
377
-
378
- /* -- replacer-aware stringify -------------------------------------------- */
379
- /*
380
- * The replacer overload needs the holder and key of every value, so it gets a
381
- * separate walk instead of complicating the fast path. It also calls `toJSON`
382
- * and filters object keys through an array replacer.
383
- */
384
- typedef struct {
385
- xt_value replacer;
386
- xt_value indent;
387
- xt_value *path;
388
- uint32_t path_count;
389
- uint32_t path_capacity;
390
- } xt_json_replacer_context;
391
-
392
- static void xt_json_replacer_enter(xt_json_replacer_context *ctx, xt_value value) {
393
- for (uint32_t i = 0; i < ctx->path_count; i++) {
394
- if (ctx->path[i] == value) {
395
- xt_value message = xt_string_from_cstr("Converting circular structure to JSON");
396
- xt_throw(xt_type_error_ctor(1, &message));
397
- }
398
- }
399
- if (ctx->path_count >= ctx->path_capacity) {
400
- uint32_t capacity = ctx->path_capacity == 0 ? 16 : ctx->path_capacity * 2;
401
- xt_value *path = (xt_value *)realloc(ctx->path, sizeof(xt_value) * capacity);
402
- if (!path) abort();
403
- ctx->path = path;
404
- ctx->path_capacity = capacity;
405
- }
406
- ctx->path[ctx->path_count++] = value;
407
- }
408
-
409
- static void xt_json_replacer_leave(xt_json_replacer_context *ctx) {
410
- if (ctx->path_count > 0) ctx->path_count--;
411
- }
412
-
413
- static void xt_json_replacer_indent(xt_json_replacer_context *ctx, int depth, xt_value *out) {
414
- xt_json_indent(out, ctx->indent, depth);
415
- }
416
-
417
- static int xt_json_replacer_allows(xt_value replacer, xt_value key) {
418
- if (!XT_IS_ARRAY(replacer)) return 1;
419
- xt_array *allowed = xt_as_array(replacer);
420
- for (uint32_t i = 0; i < allowed->length; i++) {
421
- xt_value item = allowed->items[i];
422
- if (!XT_IS_STRING(item) && !XT_IS_NUMBER(item)) continue;
423
- xt_value text = xt_to_string(item);
424
- xt_string *a = xt_as_string(text);
425
- xt_string *b = xt_as_string(key);
426
- if (a && b && xt_string_equals(a, b)) return 1;
427
- }
428
- return 0;
429
- }
430
-
431
- static int xt_json_replacer_write(xt_json_replacer_context *ctx, xt_value holder, xt_value key,
432
- int depth, xt_value *out);
433
-
434
- static int xt_json_replacer_container(xt_json_replacer_context *ctx, xt_value value, int depth,
435
- xt_value *out) {
436
- xt_json_replacer_enter(ctx, value);
437
- if (XT_IS_ARRAY(value)) {
438
- xt_array *array = xt_as_array(value);
439
- *out = xt_add(*out, xt_string_from_cstr("["));
440
- for (uint32_t i = 0; i < array->length; i++) {
441
- if (i > 0) *out = xt_add(*out, xt_string_from_cstr(","));
442
- xt_json_replacer_indent(ctx, depth + 1, out);
443
- char keyBuffer[16];
444
- snprintf(keyBuffer, sizeof(keyBuffer), "%u", i);
445
- xt_value temporary = xt_string_from_cstr("");
446
- int wrote = xt_json_replacer_write(ctx, value, xt_string_from_cstr(keyBuffer), depth + 1,
447
- &temporary);
448
- *out = xt_add(*out, wrote ? temporary : xt_string_from_cstr("null"));
449
- }
450
- xt_json_replacer_indent(ctx, depth, out);
451
- xt_json_replacer_leave(ctx);
452
- *out = xt_add(*out, xt_string_from_cstr("]"));
453
- return 1;
454
- }
455
-
456
- xt_value keys = xt_object_keys(value);
457
- xt_array *list = xt_as_array(keys);
458
- *out = xt_add(*out, xt_string_from_cstr("{"));
459
- int written = 0;
460
- if (list) {
461
- for (uint32_t i = 0; i < list->length; i++) {
462
- xt_value childKey = list->items[i];
463
- if (!xt_json_replacer_allows(ctx->replacer, childKey)) continue;
464
- xt_value temporary = xt_string_from_cstr("");
465
- int wrote = xt_json_replacer_write(ctx, value, childKey, depth + 1, &temporary);
466
- if (!wrote) continue;
467
- if (written > 0) *out = xt_add(*out, xt_string_from_cstr(","));
468
- written++;
469
- xt_json_replacer_indent(ctx, depth + 1, out);
470
- *out = xt_add(*out, xt_string_from_cstr("\""));
471
- *out = xt_add(*out, childKey);
472
- *out = xt_add(*out, xt_string_from_cstr("\":"));
473
- if (xt_json_indented(ctx->indent)) *out = xt_add(*out, xt_string_from_cstr(" "));
474
- *out = xt_add(*out, temporary);
475
- }
476
- }
477
- xt_json_replacer_indent(ctx, depth, out);
478
- xt_json_replacer_leave(ctx);
479
- *out = xt_add(*out, xt_string_from_cstr("}"));
480
- return 1;
481
- }
482
-
483
- static int xt_json_replacer_write(xt_json_replacer_context *ctx, xt_value holder, xt_value key,
484
- int depth, xt_value *out) {
485
- xt_value value = xt_get(holder, key);
486
- if (XT_IS_FUNCTION(ctx->replacer)) {
487
- xt_value args[2] = {key, value};
488
- value = xt_call_with_this(ctx->replacer, holder, 2, args);
489
- }
490
- if (xt_is_date(value)) {
491
- value = xt_call_method(value, xt_string_from_cstr("toJSON"), 1, &key);
492
- } else if (XT_IS_ARRAY(value) ||
493
- (XT_IS_OBJECT(value) &&
494
- (((xt_object *)XT_GET_PTR(value))->header.kind == XT_OBJECT_KIND_OBJECT ||
495
- ((xt_object *)XT_GET_PTR(value))->header.kind == XT_OBJECT_KIND_ERROR))) {
496
- xt_value toJSON = XT_IS_ARRAY(value) ? xt_get(value, xt_string_from_cstr("toJSON"))
497
- : xt_object_get(value, xt_string_from_cstr("toJSON"));
498
- if (XT_IS_FUNCTION(toJSON)) {
499
- xt_value args[1] = {key};
500
- value = xt_call_with_this(toJSON, value, 1, args);
501
- }
502
- }
503
- if (value == XT_UNDEFINED || XT_IS_FUNCTION(value) || xt_is_symbol(value)) return 0;
504
- if (XT_IS_BIGINT(value)) {
505
- xt_value message = xt_string_from_cstr("Do not know how to serialize a BigInt");
506
- xt_throw(xt_type_error_ctor(1, &message));
507
- }
508
- if (XT_IS_ARRAY(value)) return xt_json_replacer_container(ctx, value, depth, out);
509
- if (XT_IS_OBJECT(value)) {
510
- int kind = ((xt_object *)XT_GET_PTR(value))->header.kind;
511
- if (kind == XT_OBJECT_KIND_MAP || kind == XT_OBJECT_KIND_SET ||
512
- kind == XT_OBJECT_KIND_REGEXP || kind == XT_OBJECT_KIND_PROMISE ||
513
- kind == XT_OBJECT_KIND_GENERATOR || kind == XT_OBJECT_KIND_ITERATOR ||
514
- kind == XT_OBJECT_KIND_ASYNC) {
515
- *out = xt_add(*out, xt_string_from_cstr("{}"));
516
- return 1;
517
- }
518
- return xt_json_replacer_container(ctx, value, depth, out);
519
- }
520
- xt_json_stringify_value(value, ctx->indent, depth, out);
521
- return 1;
522
- }
523
-
524
- xt_value xt_json_stringify(int32_t argc, xt_value *argv) {
525
- xt_value value = xt_arg_at(argc, argv, 0);
526
- if (value == XT_UNDEFINED || XT_IS_FUNCTION(value) || xt_is_symbol(value)) return XT_UNDEFINED;
527
- /* Signature: stringify(value, replacer, space). The replacer is argv[1] and
528
- * the indentation is argv[2]; reading argv[1] as `space` silently ignored
529
- * `JSON.stringify(value, null, 2)`. */
530
- xt_value indent = xt_json_space_unit(xt_arg_at(argc, argv, 2));
531
- xt_value replacer = xt_arg_at(argc, argv, 1);
532
- if (XT_IS_FUNCTION(replacer) || XT_IS_ARRAY(replacer)) {
533
- xt_json_replacer_context context = {replacer, indent, NULL, 0, 0};
534
- xt_value wrapper = xt_object_new();
535
- xt_object_set(wrapper, xt_string_from_cstr(""), value);
536
- xt_value out = xt_string_from_cstr("");
537
- int wrote = xt_json_replacer_write(&context, wrapper, xt_string_from_cstr(""), 0, &out);
538
- free(context.path);
539
- return wrote ? out : XT_UNDEFINED;
540
- }
541
- /* A caught circular-structure error longjmps past xt_json_leave; start every
542
- * top-level serialization with a clean path. */
543
- xt_json_path_count = 0;
544
- xt_value out = xt_string_from_cstr("");
545
- xt_json_stringify_value(value, indent, 0, &out);
546
- return out;
547
- }
2
+ /* The parser and the serializer live in their own translation-unit fragments so
3
+ * each stays readable; `xt_stdlib2.c` includes this file. */
4
+ #include "json_parse.inc"
5
+ #include "json_stringify.inc"