xbintsc 0.3.25 → 0.3.46

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 (187) hide show
  1. package/README.md +39 -3
  2. package/dist/src/cli/main.js +124 -30
  3. package/dist/src/cli/main.js.map +1 -1
  4. package/dist/src/codegen/generator/calls/access.js +8 -1
  5. package/dist/src/codegen/generator/calls/access.js.map +1 -1
  6. package/dist/src/codegen/generator/module.js +10 -0
  7. package/dist/src/codegen/generator/module.js.map +1 -1
  8. package/dist/src/codegen/generator/state.js +24 -0
  9. package/dist/src/codegen/generator/state.js.map +1 -1
  10. package/dist/src/codegen/generator/tables.d.ts +6 -0
  11. package/dist/src/codegen/generator/tables.js +38 -0
  12. package/dist/src/codegen/generator/tables.js.map +1 -1
  13. package/dist/src/driver/bundler/commonjs.d.ts +52 -0
  14. package/dist/src/driver/bundler/commonjs.js +493 -0
  15. package/dist/src/driver/bundler/commonjs.js.map +1 -0
  16. package/dist/src/driver/bundler/graph.js +14 -0
  17. package/dist/src/driver/bundler/graph.js.map +1 -1
  18. package/dist/src/driver/bundler/merge.js +14 -0
  19. package/dist/src/driver/bundler/merge.js.map +1 -1
  20. package/dist/src/driver/bundler/types.d.ts +8 -0
  21. package/dist/src/driver/compiler.d.ts +8 -1
  22. package/dist/src/driver/compiler.js +73 -5
  23. package/dist/src/driver/compiler.js.map +1 -1
  24. package/dist/src/driver/config.d.ts +53 -0
  25. package/dist/src/driver/config.js +141 -0
  26. package/dist/src/driver/config.js.map +1 -0
  27. package/dist/src/driver/icon.d.ts +62 -0
  28. package/dist/src/driver/icon.js +151 -0
  29. package/dist/src/driver/icon.js.map +1 -0
  30. package/dist/src/driver/index.d.ts +4 -0
  31. package/dist/src/driver/index.js +4 -0
  32. package/dist/src/driver/index.js.map +1 -1
  33. package/dist/src/driver/mac-bundle.d.ts +41 -0
  34. package/dist/src/driver/mac-bundle.js +123 -0
  35. package/dist/src/driver/mac-bundle.js.map +1 -0
  36. package/dist/src/driver/win-icon.d.ts +27 -0
  37. package/dist/src/driver/win-icon.js +80 -0
  38. package/dist/src/driver/win-icon.js.map +1 -0
  39. package/dist/src/parser/statements.js +17 -4
  40. package/dist/src/parser/statements.js.map +1 -1
  41. package/dist/tests/cli/main.test.js +46 -1
  42. package/dist/tests/cli/main.test.js.map +1 -1
  43. package/dist/tests/driver/config.test.d.ts +1 -0
  44. package/dist/tests/driver/config.test.js +124 -0
  45. package/dist/tests/driver/config.test.js.map +1 -0
  46. package/dist/tests/driver/icon.test.d.ts +1 -0
  47. package/dist/tests/driver/icon.test.js +421 -0
  48. package/dist/tests/driver/icon.test.js.map +1 -0
  49. package/dist/tests/driver/modules.test.js +109 -0
  50. package/dist/tests/driver/modules.test.js.map +1 -1
  51. package/dist/tests/e2e/fetch.test.d.ts +12 -0
  52. package/dist/tests/e2e/fetch.test.js +272 -0
  53. package/dist/tests/e2e/fetch.test.js.map +1 -0
  54. package/dist/tests/e2e/gc.test.d.ts +1 -0
  55. package/dist/tests/e2e/gc.test.js +168 -0
  56. package/dist/tests/e2e/gc.test.js.map +1 -0
  57. package/dist/tests/e2e/gui-dom.test.d.ts +5 -0
  58. package/dist/tests/e2e/gui-dom.test.js +102 -0
  59. package/dist/tests/e2e/gui-dom.test.js.map +1 -0
  60. package/dist/tests/e2e/gui-events.test.d.ts +5 -0
  61. package/dist/tests/e2e/gui-events.test.js +60 -0
  62. package/dist/tests/e2e/gui-events.test.js.map +1 -0
  63. package/dist/tests/e2e/gui-helpers.d.ts +31 -0
  64. package/dist/tests/e2e/gui-helpers.js +74 -0
  65. package/dist/tests/e2e/gui-helpers.js.map +1 -0
  66. package/dist/tests/e2e/gui-layout.test.d.ts +4 -0
  67. package/dist/tests/e2e/gui-layout.test.js +88 -0
  68. package/dist/tests/e2e/gui-layout.test.js.map +1 -0
  69. package/dist/tests/e2e/gui-lifecycle.test.d.ts +5 -0
  70. package/dist/tests/e2e/gui-lifecycle.test.js +58 -0
  71. package/dist/tests/e2e/gui-lifecycle.test.js.map +1 -0
  72. package/dist/tests/e2e/gui-paint.test.d.ts +5 -0
  73. package/dist/tests/e2e/gui-paint.test.js +111 -0
  74. package/dist/tests/e2e/gui-paint.test.js.map +1 -0
  75. package/dist/tests/e2e/gui-script.test.d.ts +5 -0
  76. package/dist/tests/e2e/gui-script.test.js +78 -0
  77. package/dist/tests/e2e/gui-script.test.js.map +1 -0
  78. package/dist/tests/e2e/gui-style.test.d.ts +5 -0
  79. package/dist/tests/e2e/gui-style.test.js +175 -0
  80. package/dist/tests/e2e/gui-style.test.js.map +1 -0
  81. package/dist/tests/e2e/harness.d.ts +2 -0
  82. package/dist/tests/e2e/harness.js +1 -0
  83. package/dist/tests/e2e/harness.js.map +1 -1
  84. package/dist/tests/e2e/icon.test.d.ts +7 -0
  85. package/dist/tests/e2e/icon.test.js +80 -0
  86. package/dist/tests/e2e/icon.test.js.map +1 -0
  87. package/dist/tests/e2e/npm-modules.test.d.ts +4 -1
  88. package/dist/tests/e2e/npm-modules.test.js +47 -1
  89. package/dist/tests/e2e/npm-modules.test.js.map +1 -1
  90. package/dist/tests/e2e/runtime-coverage-typed-array.test.d.ts +9 -0
  91. package/dist/tests/e2e/runtime-coverage-typed-array.test.js +140 -0
  92. package/dist/tests/e2e/runtime-coverage-typed-array.test.js.map +1 -0
  93. package/dist/tests/parser/statements.test.js +11 -0
  94. package/dist/tests/parser/statements.test.js.map +1 -1
  95. package/package.json +3 -2
  96. package/runtime/ext_gui/css.cpp +4 -517
  97. package/runtime/ext_gui/css_internal.h +27 -0
  98. package/runtime/ext_gui/css_match.cpp +195 -0
  99. package/runtime/ext_gui/css_selector.cpp +207 -0
  100. package/runtime/ext_gui/css_sheet.cpp +129 -0
  101. package/runtime/ext_gui/dom.cpp +7 -403
  102. package/runtime/ext_gui/dom_api.cpp +63 -1146
  103. package/runtime/ext_gui/dom_api_document.cpp +96 -0
  104. package/runtime/ext_gui/dom_api_events.cpp +123 -0
  105. package/runtime/ext_gui/dom_api_internal.h +135 -0
  106. package/runtime/ext_gui/dom_api_node.cpp +329 -0
  107. package/runtime/ext_gui/dom_api_node_read.cpp +294 -0
  108. package/runtime/ext_gui/dom_api_proto.cpp +135 -0
  109. package/runtime/ext_gui/dom_api_style.cpp +192 -0
  110. package/runtime/ext_gui/dom_internal.h +39 -0
  111. package/runtime/ext_gui/dom_parse.cpp +348 -0
  112. package/runtime/ext_gui/dom_serialize.cpp +54 -0
  113. package/runtime/ext_gui/gui.cpp +56 -286
  114. package/runtime/ext_gui/gui_events.cpp +294 -0
  115. package/runtime/ext_gui/gui_internal.h +29 -0
  116. package/runtime/ext_gui/layout.cpp +6 -597
  117. package/runtime/ext_gui/layout_flex.cpp +200 -0
  118. package/runtime/ext_gui/layout_flow.cpp +191 -0
  119. package/runtime/ext_gui/layout_flow_internal.h +149 -0
  120. package/runtime/ext_gui/layout_inline.cpp +181 -0
  121. package/runtime/ext_gui/layout_internal.h +20 -0
  122. package/runtime/ext_gui/renderer.cpp +15 -679
  123. package/runtime/ext_gui/renderer_image.cpp +233 -0
  124. package/runtime/ext_gui/renderer_internal.h +112 -0
  125. package/runtime/ext_gui/renderer_shaders.h +108 -0
  126. package/runtime/ext_gui/renderer_text.cpp +342 -0
  127. package/runtime/ext_gui/style.cpp +12 -660
  128. package/runtime/ext_gui/style_internal.h +33 -0
  129. package/runtime/ext_gui/style_print.cpp +113 -0
  130. package/runtime/ext_gui/style_ua.cpp +98 -0
  131. package/runtime/ext_gui/style_values.cpp +468 -0
  132. package/runtime/ext_gui/window.cpp +1 -0
  133. package/runtime/ext_node/buffer/parts/prototype.inc +1 -0
  134. package/runtime/ext_node/dgram/dgram.c +1 -0
  135. package/runtime/ext_node/events/events.c +1 -0
  136. package/runtime/ext_node/fs/fs_ops.c +10 -25
  137. package/runtime/ext_node/fs/glob.c +13 -30
  138. package/runtime/ext_node/fs/promises.c +1 -0
  139. package/runtime/ext_node/http/parts/prototypes.inc +5 -0
  140. package/runtime/ext_node/net/parts/prototypes.inc +2 -0
  141. package/runtime/ext_node/process/process.c +9 -7
  142. package/runtime/ext_node/stream/stream.c +1 -0
  143. package/runtime/ext_node/util/util.c +6 -6
  144. package/runtime/rt.h +16 -0
  145. package/runtime/rt_internal.h +67 -2
  146. package/runtime/xt_alloc.c +442 -5
  147. package/runtime/xt_builtins.c +1 -0
  148. package/runtime/xt_containers/access.inc +32 -1
  149. package/runtime/xt_fetch/buffer.inc +64 -0
  150. package/runtime/xt_fetch/headers.inc +312 -0
  151. package/runtime/xt_fetch/request.inc +386 -0
  152. package/runtime/xt_fetch/response.inc +107 -0
  153. package/runtime/xt_fetch/socket.inc +72 -0
  154. package/runtime/xt_fetch/url.inc +108 -0
  155. package/runtime/xt_fetch.c +39 -0
  156. package/runtime/xt_generator.c +95 -1
  157. package/runtime/xt_loop.c +35 -1
  158. package/runtime/xt_promise.c +46 -0
  159. package/runtime/xt_stdlib2/error.inc +1 -0
  160. package/runtime/xt_stdlib2/regexp-match.inc +8 -7
  161. package/runtime/xt_stdlib2/set.inc +1 -1
  162. package/runtime/xt_symbol.c +2 -0
  163. package/runtime/xt_typed_array/construction.inc +142 -0
  164. package/runtime/xt_typed_array/elements.inc +92 -0
  165. package/runtime/xt_typed_array/methods.inc +329 -0
  166. package/runtime/xt_typed_array.c +72 -0
  167. package/runtime/xt_values/number-format.inc +26 -0
  168. package/scripts/check-file-length.ts +85 -0
  169. package/src/cli/main.ts +137 -29
  170. package/src/codegen/generator/calls/access.ts +9 -1
  171. package/src/codegen/generator/module.ts +10 -0
  172. package/src/codegen/generator/state.ts +24 -0
  173. package/src/codegen/generator/tables.ts +40 -0
  174. package/src/driver/bundler/commonjs.ts +553 -0
  175. package/src/driver/bundler/graph.ts +14 -0
  176. package/src/driver/bundler/merge.ts +15 -0
  177. package/src/driver/bundler/types.ts +8 -0
  178. package/src/driver/compiler.ts +83 -5
  179. package/src/driver/config.ts +173 -0
  180. package/src/driver/icon.ts +184 -0
  181. package/src/driver/index.ts +30 -0
  182. package/src/driver/mac-bundle.ts +156 -0
  183. package/src/driver/win-icon.ts +96 -0
  184. package/src/parser/statements.ts +17 -4
  185. package/dist/tests/e2e/gui.test.d.ts +0 -12
  186. package/dist/tests/e2e/gui.test.js +0 -702
  187. package/dist/tests/e2e/gui.test.js.map +0 -1
@@ -57,6 +57,7 @@ static int g_well_known_ready = 0;
57
57
  static void xt_symbol_init_well_known(void) {
58
58
  if (g_well_known_ready) return;
59
59
  g_well_known_ready = 1;
60
+ xt_gc_add_root_range(g_well_known, XT_WELL_KNOWN_COUNT);
60
61
  for (int i = 0; i < XT_WELL_KNOWN_COUNT; i++) {
61
62
  char buffer[64];
62
63
  snprintf(buffer, sizeof(buffer), "Symbol.%s", XT_WELL_KNOWN_NAMES[i]);
@@ -116,6 +117,7 @@ xt_value xt_symbol(int32_t argc, xt_value *argv) {
116
117
  static xt_value g_symbol_registry = XT_UNDEFINED;
117
118
 
118
119
  xt_value xt_symbol_for(xt_value key) {
120
+ xt_gc_add_root(&g_symbol_registry);
119
121
  xt_value keyString = xt_to_string(key);
120
122
  if (g_symbol_registry == XT_UNDEFINED) g_symbol_registry = xt_object_new();
121
123
  xt_value existing = xt_object_get(g_symbol_registry, keyString);
@@ -0,0 +1,142 @@
1
+ /* xbintsc runtime - typed array constructors, prototypes and statics.
2
+ * Included by `xt_typed_array.c` as part of a single translation unit. */
3
+
4
+ /* -- construction --------------------------------------------------------- */
5
+
6
+ static void xt_ta_define(xt_value proto, const char *name, void *fn) {
7
+ xt_object_set(proto, xt_string_from_cstr(name), xt_closure_new(fn, 0, NULL));
8
+ }
9
+
10
+ static xt_value xt_ta_proto(int kind) {
11
+ static int registered = 0;
12
+ if (!registered) {
13
+ registered = 1;
14
+ xt_gc_add_root_range(xt_ta_protos, XT_TA_KIND_COUNT);
15
+ }
16
+ if (kind < 0 || kind >= XT_TA_KIND_COUNT) return XT_UNDEFINED;
17
+ if (xt_ta_protos[kind]) return xt_ta_protos[kind];
18
+ xt_value proto = xt_object_new();
19
+ xt_ta_protos[kind] = proto;
20
+ xt_object_set(proto, xt_string_from_cstr("BYTES_PER_ELEMENT"), xt_number((double)xt_ta_bytes[kind]));
21
+ xt_ta_define(proto, "fill", (void *)xt_ta_method_fill);
22
+ xt_ta_define(proto, "set", (void *)xt_ta_method_set);
23
+ xt_ta_define(proto, "slice", (void *)xt_ta_method_slice);
24
+ xt_ta_define(proto, "subarray", (void *)xt_ta_method_slice);
25
+ xt_ta_define(proto, "join", (void *)xt_ta_method_join);
26
+ xt_ta_define(proto, "toString", (void *)xt_ta_method_to_string);
27
+ xt_ta_define(proto, "toLocaleString", (void *)xt_ta_method_to_string);
28
+ xt_ta_define(proto, "indexOf", (void *)xt_ta_method_index_of);
29
+ xt_ta_define(proto, "lastIndexOf", (void *)xt_ta_method_last_index_of);
30
+ xt_ta_define(proto, "includes", (void *)xt_ta_method_includes);
31
+ xt_ta_define(proto, "forEach", (void *)xt_ta_method_for_each);
32
+ xt_ta_define(proto, "map", (void *)xt_ta_method_map);
33
+ xt_ta_define(proto, "filter", (void *)xt_ta_method_filter);
34
+ xt_ta_define(proto, "every", (void *)xt_ta_method_every);
35
+ xt_ta_define(proto, "some", (void *)xt_ta_method_some);
36
+ xt_ta_define(proto, "find", (void *)xt_ta_method_find);
37
+ xt_ta_define(proto, "findIndex", (void *)xt_ta_method_find_index);
38
+ xt_ta_define(proto, "reduce", (void *)xt_ta_method_reduce);
39
+ xt_ta_define(proto, "reverse", (void *)xt_ta_method_reverse);
40
+ xt_ta_define(proto, "sort", (void *)xt_ta_method_sort);
41
+ xt_ta_define(proto, "copyWithin", (void *)xt_ta_method_copy_within);
42
+ xt_ta_define(proto, "at", (void *)xt_ta_method_at);
43
+ xt_ta_define(proto, "keys", (void *)xt_ta_method_keys);
44
+ xt_ta_define(proto, "values", (void *)xt_ta_method_values);
45
+ xt_ta_define(proto, "entries", (void *)xt_ta_method_entries);
46
+ return proto;
47
+ }
48
+
49
+ static xt_value xt_ta_new(int kind, uint32_t length) {
50
+ xt_value array = xt_object_new_with_proto(xt_ta_proto(kind));
51
+ xt_object_set(array, xt_string_from_cstr("length"), xt_number((double)length));
52
+ xt_object_set(array, xt_string_from_cstr("byteLength"), xt_number((double)length * xt_ta_bytes[kind]));
53
+ xt_object_set(array, xt_string_from_cstr("byteOffset"), xt_number(0));
54
+ for (uint32_t i = 0; i < length; i++) xt_ta_store(array, kind, i, 0);
55
+ return array;
56
+ }
57
+
58
+ /* Length of any value a typed array can be built from, or 0 when it is not
59
+ * array-like (strings/iterables are intentionally not treated as element
60
+ * sources yet). */
61
+ static int xt_ta_length_of(xt_value value, uint32_t *out) {
62
+ if (XT_IS_ARRAY(value)) {
63
+ *out = (uint32_t)xt_to_number(xt_array_length(value));
64
+ return 1;
65
+ }
66
+ if (XT_IS_STRING(value)) {
67
+ *out = xt_as_string(value)->length;
68
+ return 1;
69
+ }
70
+ if (xt_typed_array_kind_of(value) >= 0) {
71
+ *out = xt_ta_length(value);
72
+ return 1;
73
+ }
74
+ if (XT_IS_OBJECT(value)) {
75
+ xt_value length = xt_object_get_cstr(value, "length");
76
+ if (length != XT_UNDEFINED) {
77
+ double size = xt_to_number(length);
78
+ *out = size > 0 ? (uint32_t)size : 0;
79
+ return 1;
80
+ }
81
+ }
82
+ return 0;
83
+ }
84
+
85
+ static xt_value xt_ta_from_value(int kind, xt_value value, xt_value mapFn, xt_value thisArg, int haveMap) {
86
+ uint32_t length = 0;
87
+ if (!xt_ta_length_of(value, &length)) length = 0;
88
+ xt_value array = xt_ta_new(kind, length);
89
+ for (uint32_t i = 0; i < length; i++) {
90
+ xt_value item = xt_get(value, xt_number((double)i));
91
+ if (haveMap) {
92
+ xt_value args[3] = {item, xt_number((double)i), array};
93
+ item = xt_call_with_this(mapFn, thisArg, 3, args);
94
+ }
95
+ xt_ta_store(array, kind, i, xt_to_number(item));
96
+ }
97
+ return array;
98
+ }
99
+
100
+ static xt_value xt_ta_ctor_impl(int kind, int32_t argc, xt_value *argv) {
101
+ if (argc == 0) return xt_ta_new(kind, 0);
102
+ xt_value argument = argv[0];
103
+ /* Objects (arrays / typed arrays / array-likes / iterables) are copied
104
+ * element-wise; every other argument is a length via `ToIndex`. */
105
+ if (XT_IS_ARRAY(argument) || XT_IS_OBJECT(argument)) {
106
+ return xt_ta_from_value(kind, argument, XT_UNDEFINED, XT_UNDEFINED, 0);
107
+ }
108
+ double size = xt_to_number(argument);
109
+ return xt_ta_new(kind, size > 0 ? (uint32_t)trunc(size) : 0);
110
+ }
111
+
112
+ static xt_value xt_ta_static_impl(int kind, xt_value name, int32_t argc, xt_value *argv) {
113
+ const char *method = xt_string_data(name);
114
+ if (!method) return XT_UNDEFINED;
115
+ if (strcmp(method, "from") == 0) {
116
+ int haveMap = argc > 1 && XT_IS_FUNCTION(argv[1]);
117
+ return xt_ta_from_value(kind, xt_arg_at(argc, argv, 0), xt_arg_at(argc, argv, 1), xt_arg_at(argc, argv, 2), haveMap);
118
+ }
119
+ if (strcmp(method, "of") == 0) {
120
+ xt_value array = xt_ta_new(kind, (uint32_t)argc);
121
+ for (int32_t i = 0; i < argc; i++) xt_ta_store(array, kind, (uint32_t)i, xt_to_number(argv[i]));
122
+ return array;
123
+ }
124
+ return XT_UNDEFINED;
125
+ }
126
+
127
+ #define XT_TA_CTOR(suffix, kind) \
128
+ xt_value xt_ta_##suffix##_ctor(int32_t argc, xt_value *argv) { return xt_ta_ctor_impl(kind, argc, argv); } \
129
+ xt_value xt_ta_##suffix##_static(xt_value name, int32_t argc, xt_value *argv) { \
130
+ return xt_ta_static_impl(kind, name, argc, argv); \
131
+ }
132
+
133
+ XT_TA_CTOR(u8, XT_TA_U8)
134
+ XT_TA_CTOR(i8, XT_TA_I8)
135
+ XT_TA_CTOR(u8c, XT_TA_U8C)
136
+ XT_TA_CTOR(u16, XT_TA_U16)
137
+ XT_TA_CTOR(i16, XT_TA_I16)
138
+ XT_TA_CTOR(u32, XT_TA_U32)
139
+ XT_TA_CTOR(i32, XT_TA_I32)
140
+ XT_TA_CTOR(f32, XT_TA_F32)
141
+ XT_TA_CTOR(f64, XT_TA_F64)
142
+
@@ -0,0 +1,92 @@
1
+ /* xbintsc runtime - typed array element conversion, storage and helpers.
2
+ * Included by `xt_typed_array.c` as part of a single translation unit. */
3
+
4
+ /* -- element conversion --------------------------------------------------- */
5
+
6
+ /* ECMAScript `ToUintN` / `ToIntN`: truncate, then wrap modulo 2^N. */
7
+ static double xt_ta_wrap(double value, double modulus) {
8
+ if (isnan(value) || isinf(value)) return 0;
9
+ double result = fmod(trunc(value), modulus);
10
+ if (result < 0) result += modulus;
11
+ return result;
12
+ }
13
+
14
+ double xt_typed_array_coerce(int kind, double value) {
15
+ switch (kind) {
16
+ case XT_TA_U8:
17
+ return xt_ta_wrap(value, 256.0);
18
+ case XT_TA_I8: {
19
+ double wrapped = xt_ta_wrap(value, 256.0);
20
+ return wrapped >= 128.0 ? wrapped - 256.0 : wrapped;
21
+ }
22
+ case XT_TA_U8C:
23
+ if (isnan(value) || value <= 0) return 0;
24
+ if (value >= 255) return 255;
25
+ return rint(value);
26
+ case XT_TA_U16:
27
+ return xt_ta_wrap(value, 65536.0);
28
+ case XT_TA_I16: {
29
+ double wrapped = xt_ta_wrap(value, 65536.0);
30
+ return wrapped >= 32768.0 ? wrapped - 65536.0 : wrapped;
31
+ }
32
+ case XT_TA_U32:
33
+ return xt_ta_wrap(value, 4294967296.0);
34
+ case XT_TA_I32: {
35
+ double wrapped = xt_ta_wrap(value, 4294967296.0);
36
+ return wrapped >= 2147483648.0 ? wrapped - 4294967296.0 : wrapped;
37
+ }
38
+ case XT_TA_F32:
39
+ return (double)(float)value;
40
+ case XT_TA_F64:
41
+ default:
42
+ return value;
43
+ }
44
+ }
45
+
46
+ int xt_typed_array_kind_of(xt_value value) {
47
+ if (!XT_IS_OBJECT(value)) return -1;
48
+ xt_object *object = (xt_object *)XT_GET_PTR(value);
49
+ if (object->header.kind != XT_OBJECT_KIND_OBJECT) return -1;
50
+ for (int kind = 0; kind < XT_TA_KIND_COUNT; kind++) {
51
+ if (xt_ta_protos[kind] && object->prototype == xt_ta_protos[kind]) return kind;
52
+ }
53
+ return -1;
54
+ }
55
+
56
+ /* -- element storage ------------------------------------------------------ */
57
+
58
+ static uint32_t xt_ta_length(xt_value array) {
59
+ double length = xt_to_number(xt_object_get_cstr(array, "length"));
60
+ if (!(length > 0)) return 0;
61
+ return (uint32_t)length;
62
+ }
63
+
64
+ static xt_value xt_ta_element(xt_value array, uint32_t index) {
65
+ char key[24];
66
+ snprintf(key, sizeof(key), "%u", index);
67
+ return xt_object_get_cstr(array, key);
68
+ }
69
+
70
+ static void xt_ta_store(xt_value array, int kind, uint32_t index, double value) {
71
+ char key[24];
72
+ snprintf(key, sizeof(key), "%u", index);
73
+ xt_object_set(array, xt_string_from_cstr(key), xt_number(xt_typed_array_coerce(kind, value)));
74
+ }
75
+
76
+ /* -- helpers -------------------------------------------------------------- */
77
+
78
+ static int32_t xt_ta_relative(int32_t index, int32_t length) {
79
+ if (index < 0) index += length;
80
+ if (index < 0) index = 0;
81
+ if (index > length) index = length;
82
+ return index;
83
+ }
84
+
85
+ static xt_value xt_ta_append(xt_value array, int kind, xt_value value) {
86
+ uint32_t length = xt_ta_length(array);
87
+ xt_object_set(array, xt_string_from_cstr("length"), xt_number((double)(length + 1)));
88
+ xt_object_set(array, xt_string_from_cstr("byteLength"), xt_number((double)(length + 1) * xt_ta_bytes[kind]));
89
+ xt_ta_store(array, kind, length, xt_to_number(value));
90
+ return array;
91
+ }
92
+
@@ -0,0 +1,329 @@
1
+ /* xbintsc runtime - typed array instance methods.
2
+ * Included by `xt_typed_array.c` as part of a single translation unit. */
3
+
4
+ /* -- methods -------------------------------------------------------------- */
5
+
6
+ static xt_value xt_ta_method_fill(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
7
+ (void)env;
8
+ int kind = xt_typed_array_kind_of(self);
9
+ if (kind < 0) return self;
10
+ int32_t length = (int32_t)xt_ta_length(self);
11
+ int32_t start = xt_ta_relative(argc > 1 ? xt_to_int32(argv[1]) : 0, length);
12
+ int32_t end = xt_ta_relative(argc > 2 && argv[2] != XT_UNDEFINED ? xt_to_int32(argv[2]) : length, length);
13
+ double value = xt_to_number(xt_arg_at(argc, argv, 0));
14
+ for (int32_t i = start; i < end; i++) xt_ta_store(self, kind, (uint32_t)i, value);
15
+ return self;
16
+ }
17
+
18
+ static xt_value xt_ta_method_set(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
19
+ (void)env;
20
+ int kind = xt_typed_array_kind_of(self);
21
+ if (kind < 0) return XT_UNDEFINED;
22
+ xt_value source = xt_arg_at(argc, argv, 0);
23
+ uint32_t sourceLength = 0;
24
+ if (!xt_ta_length_of(source, &sourceLength)) return XT_UNDEFINED;
25
+ int32_t offset = argc > 1 ? xt_to_int32(argv[1]) : 0;
26
+ if (offset < 0 || offset + (int32_t)sourceLength > (int32_t)xt_ta_length(self)) {
27
+ xt_throw(xt_string_from_cstr("RangeError: offset is out of bounds"));
28
+ return XT_UNDEFINED;
29
+ }
30
+ for (uint32_t i = 0; i < sourceLength; i++) {
31
+ xt_value item = xt_get(source, xt_number((double)i));
32
+ xt_ta_store(self, kind, (uint32_t)(offset + (int32_t)i), xt_to_number(item));
33
+ }
34
+ return XT_UNDEFINED;
35
+ }
36
+
37
+ static xt_value xt_ta_method_slice(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
38
+ (void)env;
39
+ int kind = xt_typed_array_kind_of(self);
40
+ if (kind < 0) return XT_UNDEFINED;
41
+ int32_t length = (int32_t)xt_ta_length(self);
42
+ int32_t start = xt_ta_relative(argc > 0 && argv[0] != XT_UNDEFINED ? xt_to_int32(argv[0]) : 0, length);
43
+ int32_t end = xt_ta_relative(argc > 1 && argv[1] != XT_UNDEFINED ? xt_to_int32(argv[1]) : length, length);
44
+ if (end < start) end = start;
45
+ xt_value out = xt_ta_new(kind, (uint32_t)(end - start));
46
+ for (int32_t i = start; i < end; i++) xt_ta_store(out, kind, (uint32_t)(i - start), xt_to_number(xt_ta_element(self, (uint32_t)i)));
47
+ return out;
48
+ }
49
+
50
+ static xt_value xt_ta_method_join(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
51
+ (void)env;
52
+ xt_value separator = argc > 0 && argv[0] != XT_UNDEFINED ? xt_to_string(argv[0]) : xt_string_from_cstr(",");
53
+ int32_t length = (int32_t)xt_ta_length(self);
54
+ xt_value result = xt_string_from_cstr("");
55
+ for (int32_t i = 0; i < length; i++) {
56
+ if (i > 0) result = xt_add(result, separator);
57
+ result = xt_add(result, xt_to_string(xt_ta_element(self, (uint32_t)i)));
58
+ }
59
+ return result;
60
+ }
61
+
62
+ static xt_value xt_ta_method_to_string(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
63
+ (void)argc;
64
+ return xt_ta_method_join(self, env, 0, argv);
65
+ }
66
+
67
+ static xt_value xt_ta_method_index_of(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
68
+ (void)env;
69
+ int32_t length = (int32_t)xt_ta_length(self);
70
+ xt_value needle = xt_arg_at(argc, argv, 0);
71
+ int32_t from = argc > 1 && argv[1] != XT_UNDEFINED ? xt_to_int32(argv[1]) : 0;
72
+ if (from < 0) from += length;
73
+ if (from < 0) from = 0;
74
+ for (int32_t i = from; i < length; i++) {
75
+ if (xt_value_equals(xt_ta_element(self, (uint32_t)i), needle)) return xt_number((double)i);
76
+ }
77
+ return xt_number(-1);
78
+ }
79
+
80
+ static xt_value xt_ta_method_last_index_of(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
81
+ (void)env;
82
+ int32_t length = (int32_t)xt_ta_length(self);
83
+ xt_value needle = xt_arg_at(argc, argv, 0);
84
+ int32_t from = argc > 1 && argv[1] != XT_UNDEFINED ? xt_to_int32(argv[1]) : length - 1;
85
+ if (from < 0) from += length;
86
+ if (from >= length) from = length - 1;
87
+ for (int32_t i = from; i >= 0; i--) {
88
+ if (xt_value_equals(xt_ta_element(self, (uint32_t)i), needle)) return xt_number((double)i);
89
+ }
90
+ return xt_number(-1);
91
+ }
92
+
93
+ static xt_value xt_ta_method_includes(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
94
+ (void)env;
95
+ int32_t length = (int32_t)xt_ta_length(self);
96
+ xt_value needle = xt_arg_at(argc, argv, 0);
97
+ int32_t from = argc > 1 && argv[1] != XT_UNDEFINED ? xt_to_int32(argv[1]) : 0;
98
+ if (from < 0) from += length;
99
+ if (from < 0) from = 0;
100
+ for (int32_t i = from; i < length; i++) {
101
+ if (xt_value_equals(xt_ta_element(self, (uint32_t)i), needle)) return XT_TRUE;
102
+ }
103
+ return XT_FALSE;
104
+ }
105
+
106
+ static xt_value xt_ta_method_for_each(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
107
+ (void)env;
108
+ xt_value callback = xt_arg_at(argc, argv, 0);
109
+ xt_value thisArg = xt_arg_at(argc, argv, 1);
110
+ int32_t length = (int32_t)xt_ta_length(self);
111
+ for (int32_t i = 0; i < length; i++) {
112
+ xt_value args[3] = {xt_ta_element(self, (uint32_t)i), xt_number((double)i), self};
113
+ xt_call_with_this(callback, thisArg, 3, args);
114
+ }
115
+ return XT_UNDEFINED;
116
+ }
117
+
118
+ static xt_value xt_ta_method_map(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
119
+ (void)env;
120
+ int kind = xt_typed_array_kind_of(self);
121
+ if (kind < 0) return XT_UNDEFINED;
122
+ xt_value callback = xt_arg_at(argc, argv, 0);
123
+ xt_value thisArg = xt_arg_at(argc, argv, 1);
124
+ int32_t length = (int32_t)xt_ta_length(self);
125
+ xt_value out = xt_ta_new(kind, (uint32_t)length);
126
+ for (int32_t i = 0; i < length; i++) {
127
+ xt_value args[3] = {xt_ta_element(self, (uint32_t)i), xt_number((double)i), self};
128
+ xt_ta_store(out, kind, (uint32_t)i, xt_to_number(xt_call_with_this(callback, thisArg, 3, args)));
129
+ }
130
+ return out;
131
+ }
132
+
133
+ static xt_value xt_ta_method_filter(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
134
+ (void)env;
135
+ int kind = xt_typed_array_kind_of(self);
136
+ if (kind < 0) return XT_UNDEFINED;
137
+ xt_value callback = xt_arg_at(argc, argv, 0);
138
+ xt_value thisArg = xt_arg_at(argc, argv, 1);
139
+ int32_t length = (int32_t)xt_ta_length(self);
140
+ xt_value out = xt_ta_new(kind, 0);
141
+ for (int32_t i = 0; i < length; i++) {
142
+ xt_value args[3] = {xt_ta_element(self, (uint32_t)i), xt_number((double)i), self};
143
+ if (xt_truthy(xt_call_with_this(callback, thisArg, 3, args))) {
144
+ xt_ta_append(out, kind, xt_ta_element(self, (uint32_t)i));
145
+ }
146
+ }
147
+ return out;
148
+ }
149
+
150
+ static xt_value xt_ta_method_every(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
151
+ (void)env;
152
+ xt_value callback = xt_arg_at(argc, argv, 0);
153
+ xt_value thisArg = xt_arg_at(argc, argv, 1);
154
+ int32_t length = (int32_t)xt_ta_length(self);
155
+ for (int32_t i = 0; i < length; i++) {
156
+ xt_value args[3] = {xt_ta_element(self, (uint32_t)i), xt_number((double)i), self};
157
+ if (!xt_truthy(xt_call_with_this(callback, thisArg, 3, args))) return XT_FALSE;
158
+ }
159
+ return XT_TRUE;
160
+ }
161
+
162
+ static xt_value xt_ta_method_some(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
163
+ (void)env;
164
+ xt_value callback = xt_arg_at(argc, argv, 0);
165
+ xt_value thisArg = xt_arg_at(argc, argv, 1);
166
+ int32_t length = (int32_t)xt_ta_length(self);
167
+ for (int32_t i = 0; i < length; i++) {
168
+ xt_value args[3] = {xt_ta_element(self, (uint32_t)i), xt_number((double)i), self};
169
+ if (xt_truthy(xt_call_with_this(callback, thisArg, 3, args))) return XT_TRUE;
170
+ }
171
+ return XT_FALSE;
172
+ }
173
+
174
+ static xt_value xt_ta_method_find(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
175
+ (void)env;
176
+ xt_value callback = xt_arg_at(argc, argv, 0);
177
+ xt_value thisArg = xt_arg_at(argc, argv, 1);
178
+ int32_t length = (int32_t)xt_ta_length(self);
179
+ for (int32_t i = 0; i < length; i++) {
180
+ xt_value element = xt_ta_element(self, (uint32_t)i);
181
+ xt_value args[3] = {element, xt_number((double)i), self};
182
+ if (xt_truthy(xt_call_with_this(callback, thisArg, 3, args))) return element;
183
+ }
184
+ return XT_UNDEFINED;
185
+ }
186
+
187
+ static xt_value xt_ta_method_find_index(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
188
+ (void)env;
189
+ xt_value callback = xt_arg_at(argc, argv, 0);
190
+ xt_value thisArg = xt_arg_at(argc, argv, 1);
191
+ int32_t length = (int32_t)xt_ta_length(self);
192
+ for (int32_t i = 0; i < length; i++) {
193
+ xt_value args[3] = {xt_ta_element(self, (uint32_t)i), xt_number((double)i), self};
194
+ if (xt_truthy(xt_call_with_this(callback, thisArg, 3, args))) return xt_number((double)i);
195
+ }
196
+ return xt_number(-1);
197
+ }
198
+
199
+ static xt_value xt_ta_method_reduce(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
200
+ (void)env;
201
+ xt_value callback = xt_arg_at(argc, argv, 0);
202
+ int32_t length = (int32_t)xt_ta_length(self);
203
+ int32_t index = 0;
204
+ xt_value accumulator;
205
+ if (argc > 1) {
206
+ accumulator = argv[1];
207
+ } else if (length > 0) {
208
+ accumulator = xt_ta_element(self, 0);
209
+ index = 1;
210
+ } else {
211
+ xt_throw(xt_string_from_cstr("TypeError: Reduce of empty array with no initial value"));
212
+ return XT_UNDEFINED;
213
+ }
214
+ for (; index < length; index++) {
215
+ xt_value args[4] = {accumulator, xt_ta_element(self, (uint32_t)index), xt_number((double)index), self};
216
+ accumulator = xt_call_with_this(callback, XT_UNDEFINED, 4, args);
217
+ }
218
+ return accumulator;
219
+ }
220
+
221
+ static xt_value xt_ta_method_reverse(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
222
+ (void)env;
223
+ (void)argc;
224
+ (void)argv;
225
+ int kind = xt_typed_array_kind_of(self);
226
+ if (kind < 0) return self;
227
+ int32_t length = (int32_t)xt_ta_length(self);
228
+ for (int32_t i = 0; i < length / 2; i++) {
229
+ double left = xt_to_number(xt_ta_element(self, (uint32_t)i));
230
+ double right = xt_to_number(xt_ta_element(self, (uint32_t)(length - 1 - i)));
231
+ xt_ta_store(self, kind, (uint32_t)i, right);
232
+ xt_ta_store(self, kind, (uint32_t)(length - 1 - i), left);
233
+ }
234
+ return self;
235
+ }
236
+
237
+ static int xt_ta_compare(xt_value comparator, xt_value a, xt_value b) {
238
+ if (XT_IS_FUNCTION(comparator)) {
239
+ xt_value args[2] = {a, b};
240
+ double result = xt_to_number(xt_call_with_this(comparator, XT_UNDEFINED, 2, args));
241
+ return result < 0 ? -1 : result > 0 ? 1 : 0;
242
+ }
243
+ double x = xt_to_number(a);
244
+ double y = xt_to_number(b);
245
+ if (isnan(x)) return isnan(y) ? 0 : 1;
246
+ if (isnan(y)) return -1;
247
+ return x < y ? -1 : x > y ? 1 : 0;
248
+ }
249
+
250
+ static xt_value xt_ta_method_sort(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
251
+ (void)env;
252
+ int kind = xt_typed_array_kind_of(self);
253
+ if (kind < 0) return self;
254
+ xt_value comparator = xt_arg_at(argc, argv, 0);
255
+ int32_t length = (int32_t)xt_ta_length(self);
256
+ for (int32_t i = 1; i < length; i++) {
257
+ xt_value key = xt_ta_element(self, (uint32_t)i);
258
+ int32_t j = i - 1;
259
+ while (j >= 0 && xt_ta_compare(comparator, xt_ta_element(self, (uint32_t)j), key) > 0) {
260
+ xt_ta_store(self, kind, (uint32_t)(j + 1), xt_to_number(xt_ta_element(self, (uint32_t)j)));
261
+ j--;
262
+ }
263
+ xt_ta_store(self, kind, (uint32_t)(j + 1), xt_to_number(key));
264
+ }
265
+ return self;
266
+ }
267
+
268
+ static xt_value xt_ta_method_copy_within(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
269
+ (void)env;
270
+ int kind = xt_typed_array_kind_of(self);
271
+ if (kind < 0) return self;
272
+ int32_t length = (int32_t)xt_ta_length(self);
273
+ int32_t target = xt_ta_relative(xt_to_int32(xt_arg_at(argc, argv, 0)), length);
274
+ int32_t start = xt_ta_relative(argc > 1 && argv[1] != XT_UNDEFINED ? xt_to_int32(argv[1]) : 0, length);
275
+ int32_t end = xt_ta_relative(argc > 2 && argv[2] != XT_UNDEFINED ? xt_to_int32(argv[2]) : length, length);
276
+ int32_t count = end - start;
277
+ if (count > length - target) count = length - target;
278
+ if (count <= 0 || target == start) return self;
279
+ xt_value *temporary = (xt_value *)malloc(sizeof(xt_value) * (size_t)count);
280
+ if (!temporary) return self;
281
+ for (int32_t i = 0; i < count; i++) temporary[i] = xt_ta_element(self, (uint32_t)(start + i));
282
+ for (int32_t i = 0; i < count; i++) xt_ta_store(self, kind, (uint32_t)(target + i), xt_to_number(temporary[i]));
283
+ free(temporary);
284
+ return self;
285
+ }
286
+
287
+ static xt_value xt_ta_method_at(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
288
+ (void)env;
289
+ int32_t length = (int32_t)xt_ta_length(self);
290
+ int32_t index = xt_to_int32(xt_arg_at(argc, argv, 0));
291
+ if (index < 0) index += length;
292
+ if (index < 0 || index >= length) return XT_UNDEFINED;
293
+ return xt_ta_element(self, (uint32_t)index);
294
+ }
295
+
296
+ static xt_value xt_ta_method_keys(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
297
+ (void)env;
298
+ (void)argc;
299
+ (void)argv;
300
+ int32_t length = (int32_t)xt_ta_length(self);
301
+ xt_value out = xt_array_new(0, NULL);
302
+ for (int32_t i = 0; i < length; i++) xt_array_push(out, xt_number((double)i));
303
+ return out;
304
+ }
305
+
306
+ static xt_value xt_ta_method_values(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
307
+ (void)env;
308
+ (void)argc;
309
+ (void)argv;
310
+ int32_t length = (int32_t)xt_ta_length(self);
311
+ xt_value out = xt_array_new(0, NULL);
312
+ for (int32_t i = 0; i < length; i++) xt_array_push(out, xt_ta_element(self, (uint32_t)i));
313
+ return out;
314
+ }
315
+
316
+ static xt_value xt_ta_method_entries(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
317
+ (void)env;
318
+ (void)argc;
319
+ (void)argv;
320
+ int32_t length = (int32_t)xt_ta_length(self);
321
+ xt_value out = xt_array_new(0, NULL);
322
+ for (int32_t i = 0; i < length; i++) {
323
+ xt_value pair = xt_array_new(0, NULL);
324
+ xt_array_push(pair, xt_number((double)i));
325
+ xt_array_push(pair, xt_ta_element(self, (uint32_t)i));
326
+ xt_array_push(out, pair);
327
+ }
328
+ return out;
329
+ }
@@ -0,0 +1,72 @@
1
+ /*
2
+ * xbintsc runtime - typed arrays.
3
+ *
4
+ * JavaScript TypedArrays are modelled as plain property-bag objects with one
5
+ * numbered property per element plus `length`, `byteLength` and `byteOffset`.
6
+ * Every write is coerced to the array's element type, so
7
+ * `new Uint8Array(2)[0] = 300` stores 44 exactly like ECMAScript and a
8
+ * `Float32Array` round-trips through single precision.
9
+ *
10
+ * There is no shared `ArrayBuffer` backing store: `slice` / `subarray` return
11
+ * independent copies and the `.buffer` property is not exposed. That matches
12
+ * xbintsc's runtime, which has no distinct binary value type (strings are
13
+ * UTF-8 byte arrays and `Buffer` is likewise a property bag).
14
+ */
15
+
16
+ #include "rt_internal.h"
17
+
18
+ #include <math.h>
19
+ #include <stdio.h>
20
+ #include <stdlib.h>
21
+ #include <string.h>
22
+
23
+ enum {
24
+ XT_TA_U8,
25
+ XT_TA_I8,
26
+ XT_TA_U8C,
27
+ XT_TA_U16,
28
+ XT_TA_I16,
29
+ XT_TA_U32,
30
+ XT_TA_I32,
31
+ XT_TA_F32,
32
+ XT_TA_F64,
33
+ XT_TA_KIND_COUNT
34
+ };
35
+
36
+ static const int xt_ta_bytes[XT_TA_KIND_COUNT] = {1, 1, 1, 2, 2, 4, 4, 4, 8};
37
+
38
+ static xt_value xt_ta_protos[XT_TA_KIND_COUNT];
39
+
40
+ /* -- forward declarations ------------------------------------------------- */
41
+
42
+ static xt_value xt_ta_method_fill(xt_value, xt_value, int32_t, xt_value *);
43
+ static xt_value xt_ta_method_set(xt_value, xt_value, int32_t, xt_value *);
44
+ static xt_value xt_ta_method_slice(xt_value, xt_value, int32_t, xt_value *);
45
+ static xt_value xt_ta_method_join(xt_value, xt_value, int32_t, xt_value *);
46
+ static xt_value xt_ta_method_to_string(xt_value, xt_value, int32_t, xt_value *);
47
+ static xt_value xt_ta_method_index_of(xt_value, xt_value, int32_t, xt_value *);
48
+ static xt_value xt_ta_method_last_index_of(xt_value, xt_value, int32_t, xt_value *);
49
+ static xt_value xt_ta_method_includes(xt_value, xt_value, int32_t, xt_value *);
50
+ static xt_value xt_ta_method_for_each(xt_value, xt_value, int32_t, xt_value *);
51
+ static xt_value xt_ta_method_map(xt_value, xt_value, int32_t, xt_value *);
52
+ static xt_value xt_ta_method_filter(xt_value, xt_value, int32_t, xt_value *);
53
+ static xt_value xt_ta_method_every(xt_value, xt_value, int32_t, xt_value *);
54
+ static xt_value xt_ta_method_some(xt_value, xt_value, int32_t, xt_value *);
55
+ static xt_value xt_ta_method_find(xt_value, xt_value, int32_t, xt_value *);
56
+ static xt_value xt_ta_method_find_index(xt_value, xt_value, int32_t, xt_value *);
57
+ static xt_value xt_ta_method_reduce(xt_value, xt_value, int32_t, xt_value *);
58
+ static xt_value xt_ta_method_reverse(xt_value, xt_value, int32_t, xt_value *);
59
+ static xt_value xt_ta_method_sort(xt_value, xt_value, int32_t, xt_value *);
60
+ static xt_value xt_ta_method_copy_within(xt_value, xt_value, int32_t, xt_value *);
61
+ static xt_value xt_ta_method_at(xt_value, xt_value, int32_t, xt_value *);
62
+ static xt_value xt_ta_method_keys(xt_value, xt_value, int32_t, xt_value *);
63
+ static xt_value xt_ta_method_values(xt_value, xt_value, int32_t, xt_value *);
64
+ static xt_value xt_ta_method_entries(xt_value, xt_value, int32_t, xt_value *);
65
+
66
+ /* The implementation is grouped by functional area under `xt_typed_array/`
67
+ * and `#include`d here as a single translation unit, so the internal helpers
68
+ * stay `static` while each file stays focused on one area. */
69
+
70
+ #include "xt_typed_array/elements.inc"
71
+ #include "xt_typed_array/construction.inc"
72
+ #include "xt_typed_array/methods.inc"
@@ -64,6 +64,32 @@ void xt_number_to_js_string(double d, char *buffer, size_t size) {
64
64
  }
65
65
  int negative = d < 0;
66
66
  double x = negative ? -d : d;
67
+
68
+ /* Integers up to 2^53 have a unique shortest round-tripping representation:
69
+ * themselves. Emit them with pure integer arithmetic so we never touch the
70
+ * `%e` + `strtod` loop below, whose libc conversion is mis-rounded for some
71
+ * inputs on the Windows ARM64 UCRT (12502500 used to come out as
72
+ * 12502500.000000002). Values above 2^53 can have a shorter neighbour, so
73
+ * they keep using the general path. */
74
+ if (x <= 9007199254740992.0) {
75
+ uint64_t n = (uint64_t)x;
76
+ if ((double)n == x) {
77
+ char digits[20];
78
+ int nd = 0;
79
+ do {
80
+ digits[nd++] = (char)('0' + (int)(n % 10u));
81
+ n /= 10u;
82
+ } while (n > 0);
83
+ for (int i = 0; i < nd / 2; i++) {
84
+ char tmp = digits[i];
85
+ digits[i] = digits[nd - 1 - i];
86
+ digits[nd - 1 - i] = tmp;
87
+ }
88
+ xt_render_decimal(negative, digits, nd, nd - 1, 0, buffer, size);
89
+ return;
90
+ }
91
+ }
92
+
67
93
  char sci[64];
68
94
  for (int p = 1; p <= 17; p++) {
69
95
  snprintf(sci, sizeof(sci), "%.*e", p - 1, x);