xbintsc 0.3.35 → 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 (42) hide show
  1. package/README.md +6 -3
  2. package/dist/src/codegen/generator/module.js +10 -0
  3. package/dist/src/codegen/generator/module.js.map +1 -1
  4. package/dist/src/codegen/generator/state.js +4 -0
  5. package/dist/src/codegen/generator/state.js.map +1 -1
  6. package/dist/tests/e2e/gc.test.d.ts +1 -0
  7. package/dist/tests/e2e/gc.test.js +168 -0
  8. package/dist/tests/e2e/gc.test.js.map +1 -0
  9. package/dist/tests/e2e/harness.d.ts +2 -0
  10. package/dist/tests/e2e/harness.js +1 -0
  11. package/dist/tests/e2e/harness.js.map +1 -1
  12. package/package.json +3 -2
  13. package/runtime/ext_gui/dom_api_proto.cpp +5 -0
  14. package/runtime/ext_gui/window.cpp +1 -0
  15. package/runtime/ext_node/buffer/parts/prototype.inc +1 -0
  16. package/runtime/ext_node/dgram/dgram.c +1 -0
  17. package/runtime/ext_node/events/events.c +1 -0
  18. package/runtime/ext_node/fs/fs_ops.c +10 -25
  19. package/runtime/ext_node/fs/glob.c +13 -30
  20. package/runtime/ext_node/fs/promises.c +1 -0
  21. package/runtime/ext_node/http/parts/prototypes.inc +5 -0
  22. package/runtime/ext_node/net/parts/prototypes.inc +2 -0
  23. package/runtime/ext_node/process/process.c +9 -7
  24. package/runtime/ext_node/stream/stream.c +1 -0
  25. package/runtime/ext_node/util/util.c +6 -6
  26. package/runtime/rt.h +10 -0
  27. package/runtime/rt_internal.h +63 -2
  28. package/runtime/xt_alloc.c +442 -5
  29. package/runtime/xt_generator.c +95 -1
  30. package/runtime/xt_loop.c +35 -1
  31. package/runtime/xt_promise.c +46 -0
  32. package/runtime/xt_stdlib2/error.inc +1 -0
  33. package/runtime/xt_stdlib2/regexp-match.inc +8 -7
  34. package/runtime/xt_symbol.c +2 -0
  35. package/runtime/xt_typed_array/construction.inc +142 -0
  36. package/runtime/xt_typed_array/elements.inc +92 -0
  37. package/runtime/xt_typed_array/methods.inc +329 -0
  38. package/runtime/xt_typed_array.c +6 -548
  39. package/runtime/xt_values/number-format.inc +26 -0
  40. package/scripts/check-file-length.ts +85 -0
  41. package/src/codegen/generator/module.ts +10 -0
  42. package/src/codegen/generator/state.ts +4 -0
@@ -63,6 +63,8 @@ static xt_value xt_promise_value(xt_promise *promise) {
63
63
  return XT_FROM_PTR(XT_TAG_OBJECT, promise);
64
64
  }
65
65
 
66
+ static void xt_promise_gc_register(void);
67
+
66
68
  static xt_promise *xt_promise_new(void) {
67
69
  xt_promise *promise = (xt_promise *)xt_alloc(sizeof(xt_promise), XT_OBJECT_KIND_PROMISE);
68
70
  promise->state = XT_PROMISE_PENDING;
@@ -70,9 +72,52 @@ static xt_promise *xt_promise_new(void) {
70
72
  promise->reactions = NULL;
71
73
  promise->reaction_count = 0;
72
74
  promise->reaction_capacity = 0;
75
+ xt_promise_gc_register();
73
76
  return promise;
74
77
  }
75
78
 
79
+ /* Reactions are plain `malloc` blocks referenced from promises and from the
80
+ * microtask queue, so neither can be found by scanning the heap: the queue is
81
+ * a root provider and a promise traces the reactions it still owns. */
82
+ static void xt_promise_gc_scan_roots(void) {
83
+ for (uint32_t i = xt_microtask_head; i < xt_microtask_count; i++) {
84
+ xt_reaction *reaction = xt_microtasks[i];
85
+ if (!reaction) continue;
86
+ xt_gc_mark_header((xt_header *)reaction->promise);
87
+ xt_gc_mark_value(reaction->on_fulfilled);
88
+ xt_gc_mark_value(reaction->on_rejected);
89
+ xt_gc_mark_header((xt_header *)reaction->result);
90
+ }
91
+ }
92
+
93
+ static void xt_promise_gc_register(void) {
94
+ static int registered = 0;
95
+ if (registered) return;
96
+ registered = 1;
97
+ xt_gc_register_root_provider(xt_promise_gc_scan_roots);
98
+ }
99
+
100
+ void xt_promise_gc_trace(xt_header *header) {
101
+ xt_promise *promise = (xt_promise *)header;
102
+ xt_gc_mark_value(promise->value);
103
+ for (uint32_t i = 0; i < promise->reaction_count; i++) {
104
+ xt_reaction *reaction = promise->reactions[i];
105
+ if (!reaction) continue;
106
+ xt_gc_mark_header((xt_header *)reaction->promise);
107
+ xt_gc_mark_value(reaction->on_fulfilled);
108
+ xt_gc_mark_value(reaction->on_rejected);
109
+ xt_gc_mark_header((xt_header *)reaction->result);
110
+ }
111
+ }
112
+
113
+ void xt_promise_gc_release(xt_header *header) {
114
+ xt_promise *promise = (xt_promise *)header;
115
+ /* Pending reactions are owned solely by the promise; queued ones live in
116
+ * `xt_microtasks` and are freed as they run. */
117
+ for (uint32_t i = 0; i < promise->reaction_count; i++) free(promise->reactions[i]);
118
+ free(promise->reactions);
119
+ }
120
+
76
121
  static void xt_promise_settle(xt_promise *promise, int state, xt_value value);
77
122
 
78
123
  static void xt_promise_resolve_into(xt_promise *result, xt_value value) {
@@ -122,6 +167,7 @@ static xt_value xt_promise_run_reactions(void) {
122
167
  } else {
123
168
  xt_promise_settle(reaction->result, XT_PROMISE_REJECTED, promise->value);
124
169
  }
170
+ free(reaction);
125
171
  }
126
172
  xt_microtask_head = 0;
127
173
  xt_microtask_count = 0;
@@ -19,6 +19,7 @@ static int xt_error_is_family(const char *name) {
19
19
  static xt_value xt_error_root_proto = XT_UNDEFINED;
20
20
 
21
21
  static xt_value xt_error_proto_for(const char *name) {
22
+ xt_gc_add_root(&xt_error_root_proto);
22
23
  if (!XT_IS_OBJECT(xt_error_root_proto)) {
23
24
  xt_error_root_proto = xt_object_new();
24
25
  xt_object_set(xt_error_root_proto, xt_string_from_cstr("name"),
@@ -326,8 +326,10 @@ xt_value xt_regexp_replace(xt_value value, xt_value regexpValue, xt_value replac
326
326
  int32_t end = (int32_t)matches[0].rm_eo;
327
327
  xt_regexp_append(&out, &length, &capacity, text->data + cursor, (size_t)(start - cursor));
328
328
  if (useFunction) {
329
- xt_value *args = (xt_value *)malloc(sizeof(xt_value) * (size_t)(jsTotal + 3));
330
- args[0] = xt_string_new(text->data + start, (int64_t)(end - start));
329
+ /* Accumulate in an `xt_array` so the earlier match/group strings stay
330
+ reachable while later groups (and the callback itself) allocate. */
331
+ xt_value argArray = xt_array_new(0, NULL);
332
+ xt_array_push(argArray, xt_string_new(text->data + start, (int64_t)(end - start)));
331
333
  for (int g = 1; g <= jsTotal; g++) {
332
334
  xt_value groupValue = XT_UNDEFINED;
333
335
  for (int i = 0; i < groupCount; i++) {
@@ -337,12 +339,11 @@ xt_value xt_regexp_replace(xt_value value, xt_value regexpValue, xt_value replac
337
339
  }
338
340
  break;
339
341
  }
340
- args[g] = groupValue;
342
+ xt_array_push(argArray, groupValue);
341
343
  }
342
- args[jsTotal + 1] = xt_number((double)start);
343
- args[jsTotal + 2] = xt_to_string(value);
344
- xt_value produced = xt_closure_call(replacementValue, jsTotal + 3, args);
345
- free(args);
344
+ xt_array_push(argArray, xt_number((double)start));
345
+ xt_array_push(argArray, xt_to_string(value));
346
+ xt_value produced = xt_closure_call(replacementValue, jsTotal + 3, xt_as_array(argArray)->items);
346
347
  xt_string *producedText = xt_as_string(xt_to_string(produced));
347
348
  if (producedText) xt_regexp_append(&out, &length, &capacity, producedText->data, producedText->length);
348
349
  } else {
@@ -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
+ }