xbintsc 0.3.35 → 0.3.49
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.
- package/AGENTS.md +95 -0
- package/README.md +31 -3
- package/README.zh-CN.md +23 -0
- package/dist/src/cli/hints.d.ts +54 -0
- package/dist/src/cli/hints.js +165 -0
- package/dist/src/cli/hints.js.map +1 -0
- package/dist/src/cli/main.js +73 -9
- package/dist/src/cli/main.js.map +1 -1
- package/dist/src/codegen/generator/module.js +10 -0
- package/dist/src/codegen/generator/module.js.map +1 -1
- package/dist/src/codegen/generator/state.js +4 -0
- package/dist/src/codegen/generator/state.js.map +1 -1
- package/dist/src/codegen/generator/tables.d.ts +26 -0
- package/dist/src/codegen/generator/tables.js +64 -12
- package/dist/src/codegen/generator/tables.js.map +1 -1
- package/dist/src/diagnostics/source-text.d.ts +22 -0
- package/dist/src/diagnostics/source-text.js +76 -0
- package/dist/src/diagnostics/source-text.js.map +1 -0
- package/dist/src/driver/bundler/graph.js +2 -1
- package/dist/src/driver/bundler/graph.js.map +1 -1
- package/dist/src/driver/compiler.js +3 -2
- package/dist/src/driver/compiler.js.map +1 -1
- package/dist/src/lexer/scanner/strings.js +16 -3
- package/dist/src/lexer/scanner/strings.js.map +1 -1
- package/dist/tests/cli/hints.test.d.ts +9 -0
- package/dist/tests/cli/hints.test.js +143 -0
- package/dist/tests/cli/hints.test.js.map +1 -0
- package/dist/tests/cli/main.test.js +6 -4
- package/dist/tests/cli/main.test.js.map +1 -1
- package/dist/tests/codegen/llvm.test.js +17 -2
- package/dist/tests/codegen/llvm.test.js.map +1 -1
- package/dist/tests/e2e/gc.test.d.ts +1 -0
- package/dist/tests/e2e/gc.test.js +168 -0
- package/dist/tests/e2e/gc.test.js.map +1 -0
- package/dist/tests/e2e/harness.d.ts +2 -0
- package/dist/tests/e2e/harness.js +1 -0
- package/dist/tests/e2e/harness.js.map +1 -1
- package/dist/tests/helpers.js +3 -2
- package/dist/tests/helpers.js.map +1 -1
- package/dist/tests/lexer/strings.test.js +14 -2
- package/dist/tests/lexer/strings.test.js.map +1 -1
- package/doc/DESIGN.md +117 -0
- package/doc/ai/README.md +63 -0
- package/doc/ai/build-recipe.md +137 -0
- package/doc/ai/cli.md +142 -0
- package/doc/ai/contributing.md +196 -0
- package/doc/ai/extensions.md +148 -0
- package/doc/ai/language-support.md +152 -0
- package/doc/ai/troubleshooting.md +163 -0
- package/doc/ai/zh-CN/README.md +56 -0
- package/doc/ai/zh-CN/build-recipe.md +132 -0
- package/doc/ai/zh-CN/cli.md +127 -0
- package/doc/ai/zh-CN/contributing.md +173 -0
- package/doc/ai/zh-CN/extensions.md +139 -0
- package/doc/ai/zh-CN/language-support.md +147 -0
- package/doc/ai/zh-CN/troubleshooting.md +150 -0
- package/doc/gui-scripts.md +350 -0
- package/doc/gui.md +646 -0
- package/doc/icon.md +265 -0
- package/doc/implemented.md +373 -0
- package/doc/node-implemented.md +588 -0
- package/doc/node-unimplemented.md +167 -0
- package/doc/post/announce.md +43 -0
- package/doc/requirements.md +145 -0
- package/doc/unimplemented.md +286 -0
- package/doc/xbintsc.config.schema.json +67 -0
- package/doc/zh-CN/DESIGN.md +104 -0
- package/doc/zh-CN/gui-scripts.md +329 -0
- package/doc/zh-CN/gui.md +588 -0
- package/doc/zh-CN/icon.md +241 -0
- package/doc/zh-CN/implemented.md +365 -0
- package/doc/zh-CN/node-implemented.md +533 -0
- package/doc/zh-CN/node-unimplemented.md +141 -0
- package/doc/zh-CN/plan-require-node-modules.md +284 -0
- package/doc/zh-CN/post/announce.md +47 -0
- package/doc/zh-CN/requirements.md +134 -0
- package/doc/zh-CN/unimplemented.md +247 -0
- package/llms.txt +45 -0
- package/package.json +6 -2
- package/runtime/ext_gui/dom_api_proto.cpp +5 -0
- package/runtime/ext_gui/gui.cpp +3 -1
- package/runtime/ext_gui/renderer.cpp +13 -11
- package/runtime/ext_gui/renderer_image.cpp +12 -8
- package/runtime/ext_gui/renderer_shaders.h +131 -4
- package/runtime/ext_gui/renderer_shaders_data.h +1809 -0
- package/runtime/ext_gui/renderer_text.cpp +12 -8
- package/runtime/ext_gui/shaders.hlsl +98 -0
- package/runtime/ext_gui/spirv/fill.frag +19 -0
- package/runtime/ext_gui/spirv/fill.vert +42 -0
- package/runtime/ext_gui/spirv/image.frag +16 -0
- package/runtime/ext_gui/spirv/quad.vert +30 -0
- package/runtime/ext_gui/spirv/text.frag +16 -0
- package/runtime/ext_gui/window.cpp +1 -0
- package/runtime/ext_node/buffer/parts/prototype.inc +1 -0
- package/runtime/ext_node/dgram/dgram.c +1 -0
- package/runtime/ext_node/events/events.c +1 -0
- package/runtime/ext_node/fs/fs_ops.c +10 -25
- package/runtime/ext_node/fs/glob.c +13 -30
- package/runtime/ext_node/fs/promises.c +1 -0
- package/runtime/ext_node/http/parts/prototypes.inc +5 -0
- package/runtime/ext_node/net/parts/prototypes.inc +2 -0
- package/runtime/ext_node/process/process.c +9 -7
- package/runtime/ext_node/stream/stream.c +1 -0
- package/runtime/ext_node/util/util.c +6 -6
- package/runtime/rt.h +10 -0
- package/runtime/rt_internal.h +63 -2
- package/runtime/xt_alloc.c +442 -5
- package/runtime/xt_generator.c +95 -1
- package/runtime/xt_loop.c +35 -1
- package/runtime/xt_promise.c +46 -0
- package/runtime/xt_stdlib2/error.inc +1 -0
- package/runtime/xt_stdlib2/regexp-match.inc +8 -7
- package/runtime/xt_symbol.c +2 -0
- package/runtime/xt_typed_array/construction.inc +142 -0
- package/runtime/xt_typed_array/elements.inc +92 -0
- package/runtime/xt_typed_array/methods.inc +329 -0
- package/runtime/xt_typed_array.c +6 -548
- package/runtime/xt_values/number-format.inc +26 -0
- package/scripts/build-gui-shaders.mjs +204 -0
- package/scripts/build-gui.ts +35 -0
- package/scripts/check-file-length.ts +89 -0
- package/src/cli/hints.ts +194 -0
- package/src/cli/main.ts +82 -9
- package/src/codegen/generator/module.ts +10 -0
- package/src/codegen/generator/state.ts +4 -0
- package/src/codegen/generator/tables.ts +60 -14
- package/src/diagnostics/source-text.ts +78 -0
- package/src/driver/bundler/graph.ts +2 -1
- package/src/driver/compiler.ts +3 -2
- package/src/lexer/scanner/strings.ts +16 -3
|
@@ -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
|
+
}
|