xbintsc 0.6.10 → 0.6.12
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/dist/src/checker/globals.js +2 -1
- package/dist/src/checker/globals.js.map +1 -1
- package/dist/src/cli/help.d.ts +1 -1
- package/dist/src/codegen/generator/state.js +3 -0
- package/dist/src/codegen/generator/state.js.map +1 -1
- package/dist/src/codegen/generator/tables.js +3 -0
- package/dist/src/codegen/generator/tables.js.map +1 -1
- package/dist/src/driver/compiler.js +1 -1
- package/dist/src/driver/compiler.js.map +1 -1
- package/dist/src/driver/runtime-objects.d.ts +1 -1
- package/dist/src/driver/runtime-objects.js +2 -0
- package/dist/src/driver/runtime-objects.js.map +1 -1
- package/dist/src/driver/version.d.ts +1 -1
- package/dist/src/driver/version.js +1 -1
- package/dist/src/extensions/node/crypto/index.js +12 -1
- package/dist/src/extensions/node/crypto/index.js.map +1 -1
- package/dist/tests/e2e/compile-builtins.test.js +26 -0
- package/dist/tests/e2e/compile-builtins.test.js.map +1 -1
- package/dist/tests/e2e/compile-node.test.js +37 -1
- package/dist/tests/e2e/compile-node.test.js.map +1 -1
- package/dist/tests/e2e/crypto-random.test.d.ts +9 -0
- package/dist/tests/e2e/crypto-random.test.js +63 -0
- package/dist/tests/e2e/crypto-random.test.js.map +1 -0
- package/dist/tests/e2e/date-parse.test.js +27 -1
- package/dist/tests/e2e/date-parse.test.js.map +1 -1
- package/dist/tests/e2e/gui-css-coverage.test.js +1 -1
- package/dist/tests/e2e/gui-dom.test.js +29 -0
- package/dist/tests/e2e/gui-dom.test.js.map +1 -1
- package/dist/tests/e2e/gui-form.test.d.ts +9 -0
- package/dist/tests/e2e/gui-form.test.js +359 -0
- package/dist/tests/e2e/gui-form.test.js.map +1 -0
- package/dist/tests/e2e/gui-script-import-order.test.js +6 -1
- package/dist/tests/e2e/gui-script-import-order.test.js.map +1 -1
- package/dist/tests/e2e/gui-style.test.js +82 -0
- package/dist/tests/e2e/gui-style.test.js.map +1 -1
- package/dist/tests/e2e/gui-unsupported.test.js +4 -3
- package/dist/tests/e2e/gui-unsupported.test.js.map +1 -1
- package/dist/tests/e2e/json-parse-errors.test.d.ts +9 -0
- package/dist/tests/e2e/json-parse-errors.test.js +48 -0
- package/dist/tests/e2e/json-parse-errors.test.js.map +1 -0
- package/doc/ai/language-support.md +2 -1
- package/doc/ai/zh-CN/language-support.md +2 -1
- package/doc/gui/api.md +8 -2
- package/doc/gui/dom.md +21 -12
- package/doc/gui/input.md +74 -3
- package/doc/implemented/node/crypto.md +8 -0
- package/doc/implemented/node/fs.md +1 -1
- package/doc/implemented/stdlib.md +10 -2
- package/doc/zh-CN/gui/api.md +8 -2
- package/doc/zh-CN/gui/dom.md +17 -11
- package/doc/zh-CN/gui/input.md +65 -4
- package/doc/zh-CN/implemented/node/crypto.md +6 -0
- package/doc/zh-CN/implemented/node/fs.md +1 -1
- package/doc/zh-CN/implemented/stdlib.md +10 -2
- package/package.json +7 -7
- package/runtime/ext_gui/css_match.cpp +21 -1
- package/runtime/ext_gui/css_selector.cpp +2 -2
- package/runtime/ext_gui/document.cpp +12 -0
- package/runtime/ext_gui/dom.h +41 -2
- package/runtime/ext_gui/dom_api_document.cpp +28 -0
- package/runtime/ext_gui/dom_api_events.cpp +17 -14
- package/runtime/ext_gui/dom_api_form.cpp +567 -0
- package/runtime/ext_gui/dom_api_internal.h +46 -214
- package/runtime/ext_gui/dom_api_mutation.cpp +1 -1
- package/runtime/ext_gui/dom_api_node.cpp +10 -3
- package/runtime/ext_gui/dom_api_node_read.cpp +1 -1
- package/runtime/ext_gui/dom_api_proto.cpp +45 -0
- package/runtime/ext_gui/dom_api_webgl_methods.h +217 -0
- package/runtime/ext_gui/dom_parse.cpp +114 -1
- package/runtime/ext_gui/form_focus.cpp +293 -0
- package/runtime/ext_gui/form_input.cpp +448 -0
- package/runtime/ext_gui/form_internal.h +30 -0
- package/runtime/ext_gui/form_shared.cpp +126 -0
- package/runtime/ext_gui/form_state.cpp +536 -0
- package/runtime/ext_gui/form_state.h +97 -0
- package/runtime/ext_gui/gui_engine.h +40 -4
- package/runtime/ext_gui/gui_events.cpp +63 -48
- package/runtime/ext_gui/layout.cpp +6 -0
- package/runtime/ext_gui/layout.h +4 -0
- package/runtime/ext_gui/layout_flow.cpp +23 -1
- package/runtime/ext_gui/paint.cpp +4 -1
- package/runtime/ext_gui/paint_form.cpp +389 -0
- package/runtime/ext_gui/paint_internal.h +6 -0
- package/runtime/ext_gui/style_print.cpp +30 -0
- package/runtime/ext_gui/style_ua.cpp +42 -0
- package/runtime/ext_gui/warnings.cpp +5 -7
- package/runtime/ext_gui/window.cpp +31 -55
- package/runtime/ext_gui/window_diagnostics.cpp +58 -0
- package/runtime/ext_gui/window_emit.cpp +51 -0
- package/runtime/ext_node/crypto/crypto.c +3 -2
- package/runtime/ext_node/crypto/crypto_hash.h +13 -0
- package/runtime/ext_node/crypto/crypto_random.c +339 -0
- package/runtime/ext_node/fs/async_worker.c +1 -1
- package/runtime/ext_node/fs/fs_ops.c +1 -1
- package/runtime/ext_node/fs/parts/platform.inc +65 -0
- package/runtime/rt.h +27 -0
- package/runtime/xt_crypto.c +132 -0
- package/runtime/xt_random.c +143 -0
- package/runtime/xt_regex/matcher.inc +9 -28
- package/runtime/xt_regex/parser.inc +65 -2
- package/runtime/xt_regex/types.inc +11 -1
- package/runtime/xt_regex.h +6 -2
- package/runtime/xt_stdlib/dispatch.inc +2 -0
- package/runtime/xt_stdlib/math.inc +1 -1
- package/runtime/xt_stdlib2/date.inc +204 -4
- package/runtime/xt_stdlib2/json.inc +4 -546
- package/runtime/xt_stdlib2/json_parse.inc +347 -0
- package/runtime/xt_stdlib2/json_stringify.inc +368 -0
- package/runtime/xt_stdlib2/regexp-match.inc +6 -11
- package/runtime/xt_stdlib2.c +0 -4
- package/runtime/xt_typed_array/elements.inc +6 -1
- package/src/checker/globals.ts +2 -0
- package/src/codegen/generator/state.ts +3 -0
- package/src/codegen/generator/tables.ts +3 -0
- package/src/driver/compiler.ts +1 -1
- package/src/driver/runtime-objects.ts +2 -0
- package/src/driver/version.ts +1 -1
- package/src/extensions/node/crypto/index.ts +12 -1
|
@@ -0,0 +1,368 @@
|
|
|
1
|
+
|
|
2
|
+
static void xt_json_stringify_value(xt_value value, xt_value indent, int depth, xt_value *out);
|
|
3
|
+
|
|
4
|
+
/* The indentation unit for the `space` argument: up to 10 spaces for a
|
|
5
|
+
* number, or the first 10 UTF-16 code units of a string (any characters,
|
|
6
|
+
* not just spaces), matching JSON.stringify. An empty string means compact. */
|
|
7
|
+
static xt_value xt_json_space_unit(xt_value space) {
|
|
8
|
+
if (XT_IS_NUMBER(space)) {
|
|
9
|
+
int count = xt_to_int32(space);
|
|
10
|
+
if (count < 0) count = 0;
|
|
11
|
+
if (count > 10) count = 10;
|
|
12
|
+
char spaces[10];
|
|
13
|
+
memset(spaces, ' ', (size_t)count);
|
|
14
|
+
return xt_string_new(spaces, (size_t)count);
|
|
15
|
+
}
|
|
16
|
+
if (XT_IS_STRING(space)) {
|
|
17
|
+
xt_string *s = xt_as_string(space);
|
|
18
|
+
const char *bytes = xt_string_bytes(s);
|
|
19
|
+
size_t offset = 0;
|
|
20
|
+
size_t end = 0;
|
|
21
|
+
uint32_t units = 0;
|
|
22
|
+
while (offset < s->length && units < 10) {
|
|
23
|
+
size_t width = 0;
|
|
24
|
+
uint32_t code = xt_utf8_decode(bytes + offset, s->length - offset, &width);
|
|
25
|
+
units += code >= 0x10000 ? 2 : 1;
|
|
26
|
+
offset += width;
|
|
27
|
+
if (units <= 10) end = offset;
|
|
28
|
+
}
|
|
29
|
+
return xt_string_new(bytes, end);
|
|
30
|
+
}
|
|
31
|
+
return xt_string_from_cstr("");
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
static int xt_json_indented(xt_value unit) {
|
|
35
|
+
return XT_IS_STRING(unit) && xt_as_string(unit)->length > 0;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
static void xt_json_indent(xt_value *out, xt_value unit, int depth) {
|
|
39
|
+
if (!xt_json_indented(unit)) return;
|
|
40
|
+
*out = xt_add(*out, xt_string_from_cstr("\n"));
|
|
41
|
+
for (int i = 0; i < depth; i++) *out = xt_add(*out, unit);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/* Containers on the current serialization path. A repeated pointer is a cycle;
|
|
45
|
+
* serializing it would recurse until the native stack overflowed (a hard crash,
|
|
46
|
+
* not the TypeError the platform throws). */
|
|
47
|
+
static xt_value *xt_json_path = NULL;
|
|
48
|
+
static uint32_t xt_json_path_count = 0;
|
|
49
|
+
static uint32_t xt_json_path_capacity = 0;
|
|
50
|
+
|
|
51
|
+
static void xt_json_enter(xt_value value) {
|
|
52
|
+
for (uint32_t i = 0; i < xt_json_path_count; i++) {
|
|
53
|
+
if (xt_json_path[i] == value) {
|
|
54
|
+
xt_value message = xt_string_from_cstr("Converting circular structure to JSON");
|
|
55
|
+
xt_throw(xt_type_error_ctor(1, &message));
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (xt_json_path_count >= xt_json_path_capacity) {
|
|
59
|
+
uint32_t capacity = xt_json_path_capacity == 0 ? 16 : xt_json_path_capacity * 2;
|
|
60
|
+
xt_value *path = (xt_value *)realloc(xt_json_path, sizeof(xt_value) * capacity);
|
|
61
|
+
if (!path) abort();
|
|
62
|
+
xt_json_path = path;
|
|
63
|
+
xt_json_path_capacity = capacity;
|
|
64
|
+
}
|
|
65
|
+
xt_json_path[xt_json_path_count++] = value;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
static void xt_json_leave(void) {
|
|
69
|
+
if (xt_json_path_count > 0) xt_json_path_count--;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
static void xt_json_stringify_array(xt_value value, xt_value indent, int depth, xt_value *out) {
|
|
73
|
+
xt_array *array = xt_as_array(value);
|
|
74
|
+
if (!array || array->length == 0) { *out = xt_add(*out, xt_string_from_cstr("[]")); return; }
|
|
75
|
+
*out = xt_add(*out, xt_string_from_cstr("["));
|
|
76
|
+
xt_json_enter(value);
|
|
77
|
+
for (uint32_t i = 0; i < array->length; i++) {
|
|
78
|
+
if (i > 0) *out = xt_add(*out, xt_string_from_cstr(","));
|
|
79
|
+
xt_json_indent(out, indent, depth + 1);
|
|
80
|
+
xt_json_stringify_value(array->items[i], indent, depth + 1, out);
|
|
81
|
+
}
|
|
82
|
+
xt_json_indent(out, indent, depth);
|
|
83
|
+
xt_json_leave();
|
|
84
|
+
*out = xt_add(*out, xt_string_from_cstr("]"));
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
static void xt_json_stringify_object(xt_value value, xt_value indent, int depth, xt_value *out) {
|
|
88
|
+
xt_value keys = xt_object_keys(value);
|
|
89
|
+
xt_array *array = xt_as_array(keys);
|
|
90
|
+
if (!array) { *out = xt_add(*out, xt_string_from_cstr("{}")); return; }
|
|
91
|
+
int any = 0;
|
|
92
|
+
for (uint32_t i = 0; i < array->length; i++) {
|
|
93
|
+
xt_value item = xt_get(value, array->items[i]);
|
|
94
|
+
if (item != XT_UNDEFINED && !XT_IS_FUNCTION(item) && !xt_is_symbol(item)) { any = 1; break; }
|
|
95
|
+
}
|
|
96
|
+
if (!any) { *out = xt_add(*out, xt_string_from_cstr("{}")); return; }
|
|
97
|
+
*out = xt_add(*out, xt_string_from_cstr("{"));
|
|
98
|
+
xt_json_enter(value);
|
|
99
|
+
int written = 0;
|
|
100
|
+
for (uint32_t i = 0; i < array->length; i++) {
|
|
101
|
+
xt_value key = array->items[i];
|
|
102
|
+
xt_value item = xt_get(value, key);
|
|
103
|
+
/* `undefined`, functions and symbols are omitted from objects. */
|
|
104
|
+
if (item == XT_UNDEFINED || XT_IS_FUNCTION(item) || xt_is_symbol(item)) continue;
|
|
105
|
+
if (written > 0) *out = xt_add(*out, xt_string_from_cstr(","));
|
|
106
|
+
written++;
|
|
107
|
+
xt_json_indent(out, indent, depth + 1);
|
|
108
|
+
*out = xt_add(*out, xt_string_from_cstr("\""));
|
|
109
|
+
*out = xt_add(*out, key);
|
|
110
|
+
*out = xt_add(*out, xt_string_from_cstr("\":"));
|
|
111
|
+
if (xt_json_indented(indent)) *out = xt_add(*out, xt_string_from_cstr(" "));
|
|
112
|
+
xt_json_stringify_value(item, indent, depth + 1, out);
|
|
113
|
+
}
|
|
114
|
+
xt_json_indent(out, indent, depth);
|
|
115
|
+
xt_json_leave();
|
|
116
|
+
*out = xt_add(*out, xt_string_from_cstr("}"));
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
static void xt_json_stringify_value(xt_value value, xt_value indent, int depth, xt_value *out) {
|
|
120
|
+
if (value == XT_UNDEFINED || value == XT_NULL || XT_IS_FUNCTION(value) || xt_is_symbol(value)) {
|
|
121
|
+
*out = xt_add(*out, xt_string_from_cstr("null"));
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
if (XT_IS_BIGINT(value)) {
|
|
125
|
+
xt_throw(xt_error_from_message("TypeError: Do not know how to serialize a BigInt"));
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
if (XT_IS_BOOL(value)) { *out = xt_add(*out, value == XT_TRUE ? xt_string_from_cstr("true") : xt_string_from_cstr("false")); return; }
|
|
129
|
+
if (XT_IS_NUMBER(value)) {
|
|
130
|
+
double d = xt_to_double(value);
|
|
131
|
+
if (!isfinite(d)) *out = xt_add(*out, xt_string_from_cstr("null"));
|
|
132
|
+
else *out = xt_add(*out, xt_to_string(value));
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
if (XT_IS_STRING(value)) {
|
|
136
|
+
xt_string *s = xt_as_string(value);
|
|
137
|
+
const char *bytes = xt_string_bytes(s);
|
|
138
|
+
*out = xt_add(*out, xt_string_from_cstr("\""));
|
|
139
|
+
size_t offset = 0;
|
|
140
|
+
while (offset < s->length) {
|
|
141
|
+
size_t width = 0;
|
|
142
|
+
uint32_t code = xt_utf8_decode(bytes + offset, s->length - offset, &width);
|
|
143
|
+
const char *escape = NULL;
|
|
144
|
+
char buffer[8];
|
|
145
|
+
if (code == '"') escape = "\\\"";
|
|
146
|
+
else if (code == '\\') escape = "\\\\";
|
|
147
|
+
else if (code == '\b') escape = "\\b";
|
|
148
|
+
else if (code == '\f') escape = "\\f";
|
|
149
|
+
else if (code == '\n') escape = "\\n";
|
|
150
|
+
else if (code == '\r') escape = "\\r";
|
|
151
|
+
else if (code == '\t') escape = "\\t";
|
|
152
|
+
else if (code < 0x20 || (code >= 0xD800 && code <= 0xDFFF)) {
|
|
153
|
+
snprintf(buffer, sizeof(buffer), "\\u%04x", (unsigned)code);
|
|
154
|
+
escape = buffer;
|
|
155
|
+
}
|
|
156
|
+
if (escape) *out = xt_add(*out, xt_string_from_cstr(escape));
|
|
157
|
+
else *out = xt_add(*out, xt_string_new(bytes + offset, width));
|
|
158
|
+
offset += width;
|
|
159
|
+
}
|
|
160
|
+
*out = xt_add(*out, xt_string_from_cstr("\""));
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
if (XT_IS_ARRAY(value)) { xt_json_stringify_array(value, indent, depth, out); return; }
|
|
164
|
+
if (XT_IS_OBJECT(value)) {
|
|
165
|
+
/* Map/Set/Date/RegExp/Promise/generators are not property-bag objects;
|
|
166
|
+
* handing them to xt_json_stringify_object reads their fields as a property
|
|
167
|
+
* table and crashed with an access violation. JSON sees no enumerable own
|
|
168
|
+
* properties on the first group and calls Date.prototype.toJSON. */
|
|
169
|
+
xt_object *object = (xt_object *)XT_GET_PTR(value);
|
|
170
|
+
switch (object->header.kind) {
|
|
171
|
+
case XT_OBJECT_KIND_MAP:
|
|
172
|
+
case XT_OBJECT_KIND_SET:
|
|
173
|
+
case XT_OBJECT_KIND_REGEXP:
|
|
174
|
+
case XT_OBJECT_KIND_PROMISE:
|
|
175
|
+
case XT_OBJECT_KIND_GENERATOR:
|
|
176
|
+
case XT_OBJECT_KIND_ITERATOR:
|
|
177
|
+
case XT_OBJECT_KIND_ASYNC:
|
|
178
|
+
*out = xt_add(*out, xt_string_from_cstr("{}"));
|
|
179
|
+
return;
|
|
180
|
+
case XT_OBJECT_KIND_DATE: {
|
|
181
|
+
xt_value time = xt_call_method(value, xt_string_from_cstr("getTime"), 0, NULL);
|
|
182
|
+
if (isnan(xt_to_number(time))) {
|
|
183
|
+
*out = xt_add(*out, xt_string_from_cstr("null"));
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
xt_value text = xt_call_method(value, xt_string_from_cstr("toJSON"), 0, NULL);
|
|
187
|
+
xt_json_stringify_value(text, indent, depth, out);
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
default:
|
|
191
|
+
break;
|
|
192
|
+
}
|
|
193
|
+
xt_json_stringify_object(value, indent, depth, out);
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
*out = xt_add(*out, xt_string_from_cstr("null"));
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/* -- replacer-aware stringify -------------------------------------------- */
|
|
200
|
+
/*
|
|
201
|
+
* The replacer overload needs the holder and key of every value, so it gets a
|
|
202
|
+
* separate walk instead of complicating the fast path. It also calls `toJSON`
|
|
203
|
+
* and filters object keys through an array replacer.
|
|
204
|
+
*/
|
|
205
|
+
typedef struct {
|
|
206
|
+
xt_value replacer;
|
|
207
|
+
xt_value indent;
|
|
208
|
+
xt_value *path;
|
|
209
|
+
uint32_t path_count;
|
|
210
|
+
uint32_t path_capacity;
|
|
211
|
+
} xt_json_replacer_context;
|
|
212
|
+
|
|
213
|
+
static void xt_json_replacer_enter(xt_json_replacer_context *ctx, xt_value value) {
|
|
214
|
+
for (uint32_t i = 0; i < ctx->path_count; i++) {
|
|
215
|
+
if (ctx->path[i] == value) {
|
|
216
|
+
xt_value message = xt_string_from_cstr("Converting circular structure to JSON");
|
|
217
|
+
xt_throw(xt_type_error_ctor(1, &message));
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
if (ctx->path_count >= ctx->path_capacity) {
|
|
221
|
+
uint32_t capacity = ctx->path_capacity == 0 ? 16 : ctx->path_capacity * 2;
|
|
222
|
+
xt_value *path = (xt_value *)realloc(ctx->path, sizeof(xt_value) * capacity);
|
|
223
|
+
if (!path) abort();
|
|
224
|
+
ctx->path = path;
|
|
225
|
+
ctx->path_capacity = capacity;
|
|
226
|
+
}
|
|
227
|
+
ctx->path[ctx->path_count++] = value;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
static void xt_json_replacer_leave(xt_json_replacer_context *ctx) {
|
|
231
|
+
if (ctx->path_count > 0) ctx->path_count--;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
static void xt_json_replacer_indent(xt_json_replacer_context *ctx, int depth, xt_value *out) {
|
|
235
|
+
xt_json_indent(out, ctx->indent, depth);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
static int xt_json_replacer_allows(xt_value replacer, xt_value key) {
|
|
239
|
+
if (!XT_IS_ARRAY(replacer)) return 1;
|
|
240
|
+
xt_array *allowed = xt_as_array(replacer);
|
|
241
|
+
for (uint32_t i = 0; i < allowed->length; i++) {
|
|
242
|
+
xt_value item = allowed->items[i];
|
|
243
|
+
if (!XT_IS_STRING(item) && !XT_IS_NUMBER(item)) continue;
|
|
244
|
+
xt_value text = xt_to_string(item);
|
|
245
|
+
xt_string *a = xt_as_string(text);
|
|
246
|
+
xt_string *b = xt_as_string(key);
|
|
247
|
+
if (a && b && xt_string_equals(a, b)) return 1;
|
|
248
|
+
}
|
|
249
|
+
return 0;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
static int xt_json_replacer_write(xt_json_replacer_context *ctx, xt_value holder, xt_value key,
|
|
253
|
+
int depth, xt_value *out);
|
|
254
|
+
|
|
255
|
+
static int xt_json_replacer_container(xt_json_replacer_context *ctx, xt_value value, int depth,
|
|
256
|
+
xt_value *out) {
|
|
257
|
+
xt_json_replacer_enter(ctx, value);
|
|
258
|
+
if (XT_IS_ARRAY(value)) {
|
|
259
|
+
xt_array *array = xt_as_array(value);
|
|
260
|
+
*out = xt_add(*out, xt_string_from_cstr("["));
|
|
261
|
+
for (uint32_t i = 0; i < array->length; i++) {
|
|
262
|
+
if (i > 0) *out = xt_add(*out, xt_string_from_cstr(","));
|
|
263
|
+
xt_json_replacer_indent(ctx, depth + 1, out);
|
|
264
|
+
char keyBuffer[16];
|
|
265
|
+
snprintf(keyBuffer, sizeof(keyBuffer), "%u", i);
|
|
266
|
+
xt_value temporary = xt_string_from_cstr("");
|
|
267
|
+
int wrote = xt_json_replacer_write(ctx, value, xt_string_from_cstr(keyBuffer), depth + 1,
|
|
268
|
+
&temporary);
|
|
269
|
+
*out = xt_add(*out, wrote ? temporary : xt_string_from_cstr("null"));
|
|
270
|
+
}
|
|
271
|
+
xt_json_replacer_indent(ctx, depth, out);
|
|
272
|
+
xt_json_replacer_leave(ctx);
|
|
273
|
+
*out = xt_add(*out, xt_string_from_cstr("]"));
|
|
274
|
+
return 1;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
xt_value keys = xt_object_keys(value);
|
|
278
|
+
xt_array *list = xt_as_array(keys);
|
|
279
|
+
*out = xt_add(*out, xt_string_from_cstr("{"));
|
|
280
|
+
int written = 0;
|
|
281
|
+
if (list) {
|
|
282
|
+
for (uint32_t i = 0; i < list->length; i++) {
|
|
283
|
+
xt_value childKey = list->items[i];
|
|
284
|
+
if (!xt_json_replacer_allows(ctx->replacer, childKey)) continue;
|
|
285
|
+
xt_value temporary = xt_string_from_cstr("");
|
|
286
|
+
int wrote = xt_json_replacer_write(ctx, value, childKey, depth + 1, &temporary);
|
|
287
|
+
if (!wrote) continue;
|
|
288
|
+
if (written > 0) *out = xt_add(*out, xt_string_from_cstr(","));
|
|
289
|
+
written++;
|
|
290
|
+
xt_json_replacer_indent(ctx, depth + 1, out);
|
|
291
|
+
*out = xt_add(*out, xt_string_from_cstr("\""));
|
|
292
|
+
*out = xt_add(*out, childKey);
|
|
293
|
+
*out = xt_add(*out, xt_string_from_cstr("\":"));
|
|
294
|
+
if (xt_json_indented(ctx->indent)) *out = xt_add(*out, xt_string_from_cstr(" "));
|
|
295
|
+
*out = xt_add(*out, temporary);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
xt_json_replacer_indent(ctx, depth, out);
|
|
299
|
+
xt_json_replacer_leave(ctx);
|
|
300
|
+
*out = xt_add(*out, xt_string_from_cstr("}"));
|
|
301
|
+
return 1;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
static int xt_json_replacer_write(xt_json_replacer_context *ctx, xt_value holder, xt_value key,
|
|
305
|
+
int depth, xt_value *out) {
|
|
306
|
+
xt_value value = xt_get(holder, key);
|
|
307
|
+
if (XT_IS_FUNCTION(ctx->replacer)) {
|
|
308
|
+
xt_value args[2] = {key, value};
|
|
309
|
+
value = xt_call_with_this(ctx->replacer, holder, 2, args);
|
|
310
|
+
}
|
|
311
|
+
if (xt_is_date(value)) {
|
|
312
|
+
value = xt_call_method(value, xt_string_from_cstr("toJSON"), 1, &key);
|
|
313
|
+
} else if (XT_IS_ARRAY(value) ||
|
|
314
|
+
(XT_IS_OBJECT(value) &&
|
|
315
|
+
(((xt_object *)XT_GET_PTR(value))->header.kind == XT_OBJECT_KIND_OBJECT ||
|
|
316
|
+
((xt_object *)XT_GET_PTR(value))->header.kind == XT_OBJECT_KIND_ERROR))) {
|
|
317
|
+
xt_value toJSON = XT_IS_ARRAY(value) ? xt_get(value, xt_string_from_cstr("toJSON"))
|
|
318
|
+
: xt_object_get(value, xt_string_from_cstr("toJSON"));
|
|
319
|
+
if (XT_IS_FUNCTION(toJSON)) {
|
|
320
|
+
xt_value args[1] = {key};
|
|
321
|
+
value = xt_call_with_this(toJSON, value, 1, args);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
if (value == XT_UNDEFINED || XT_IS_FUNCTION(value) || xt_is_symbol(value)) return 0;
|
|
325
|
+
if (XT_IS_BIGINT(value)) {
|
|
326
|
+
xt_value message = xt_string_from_cstr("Do not know how to serialize a BigInt");
|
|
327
|
+
xt_throw(xt_type_error_ctor(1, &message));
|
|
328
|
+
}
|
|
329
|
+
if (XT_IS_ARRAY(value)) return xt_json_replacer_container(ctx, value, depth, out);
|
|
330
|
+
if (XT_IS_OBJECT(value)) {
|
|
331
|
+
int kind = ((xt_object *)XT_GET_PTR(value))->header.kind;
|
|
332
|
+
if (kind == XT_OBJECT_KIND_MAP || kind == XT_OBJECT_KIND_SET ||
|
|
333
|
+
kind == XT_OBJECT_KIND_REGEXP || kind == XT_OBJECT_KIND_PROMISE ||
|
|
334
|
+
kind == XT_OBJECT_KIND_GENERATOR || kind == XT_OBJECT_KIND_ITERATOR ||
|
|
335
|
+
kind == XT_OBJECT_KIND_ASYNC) {
|
|
336
|
+
*out = xt_add(*out, xt_string_from_cstr("{}"));
|
|
337
|
+
return 1;
|
|
338
|
+
}
|
|
339
|
+
return xt_json_replacer_container(ctx, value, depth, out);
|
|
340
|
+
}
|
|
341
|
+
xt_json_stringify_value(value, ctx->indent, depth, out);
|
|
342
|
+
return 1;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
xt_value xt_json_stringify(int32_t argc, xt_value *argv) {
|
|
346
|
+
xt_value value = xt_arg_at(argc, argv, 0);
|
|
347
|
+
if (value == XT_UNDEFINED || XT_IS_FUNCTION(value) || xt_is_symbol(value)) return XT_UNDEFINED;
|
|
348
|
+
/* Signature: stringify(value, replacer, space). The replacer is argv[1] and
|
|
349
|
+
* the indentation is argv[2]; reading argv[1] as `space` silently ignored
|
|
350
|
+
* `JSON.stringify(value, null, 2)`. */
|
|
351
|
+
xt_value indent = xt_json_space_unit(xt_arg_at(argc, argv, 2));
|
|
352
|
+
xt_value replacer = xt_arg_at(argc, argv, 1);
|
|
353
|
+
if (XT_IS_FUNCTION(replacer) || XT_IS_ARRAY(replacer)) {
|
|
354
|
+
xt_json_replacer_context context = {replacer, indent, NULL, 0, 0};
|
|
355
|
+
xt_value wrapper = xt_object_new();
|
|
356
|
+
xt_object_set(wrapper, xt_string_from_cstr(""), value);
|
|
357
|
+
xt_value out = xt_string_from_cstr("");
|
|
358
|
+
int wrote = xt_json_replacer_write(&context, wrapper, xt_string_from_cstr(""), 0, &out);
|
|
359
|
+
free(context.path);
|
|
360
|
+
return wrote ? out : XT_UNDEFINED;
|
|
361
|
+
}
|
|
362
|
+
/* A caught circular-structure error longjmps past xt_json_leave; start every
|
|
363
|
+
* top-level serialization with a clean path. */
|
|
364
|
+
xt_json_path_count = 0;
|
|
365
|
+
xt_value out = xt_string_from_cstr("");
|
|
366
|
+
xt_json_stringify_value(value, indent, 0, &out);
|
|
367
|
+
return out;
|
|
368
|
+
}
|
|
@@ -1,13 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
/* RegExp support built on POSIX ERE. JavaScript patterns are translated to
|
|
8
|
-
* their POSIX equivalents, capturing groups are tracked so the JS capture
|
|
9
|
-
* indices stay correct even after `(?:` groups are lowered, and the resulting
|
|
10
|
-
* `regmatch_t` offsets are converted back to offsets into the input string. */
|
|
1
|
+
/* RegExp support built on the runtime's self-contained ERE matcher. JavaScript
|
|
2
|
+
* patterns are translated to their ERE equivalents, capturing groups are
|
|
3
|
+
* tracked so the JS capture indices stay correct even after `(?:` groups are
|
|
4
|
+
* lowered, and the resulting `regmatch_t` offsets are converted back to offsets
|
|
5
|
+
* into the input string. */
|
|
11
6
|
|
|
12
7
|
static int xt_regex_hex_digit(char c) {
|
|
13
8
|
if (c >= '0' && c <= '9') return c - '0';
|
|
@@ -135,7 +130,7 @@ static int xt_regexp_run(xt_value regexpValue, xt_value input, int32_t start,
|
|
|
135
130
|
const char *flagBytes = xt_string_bytes(f);
|
|
136
131
|
for (uint32_t i = 0; i < f->length; i++) {
|
|
137
132
|
if (flagBytes[i] == 'i') flags |= REG_ICASE;
|
|
138
|
-
if (flagBytes[i] == 'm') flags |=
|
|
133
|
+
if (flagBytes[i] == 'm') flags |= REG_MULTILINE;
|
|
139
134
|
}
|
|
140
135
|
}
|
|
141
136
|
char *translated = xt_regexp_translate_js(xt_string_bytes(pattern), jsGroups, groupCount);
|
package/runtime/xt_stdlib2.c
CHANGED
|
@@ -257,8 +257,13 @@ static int32_t xt_ta_relative(int32_t index, int32_t length) {
|
|
|
257
257
|
static xt_value xt_ta_append(xt_value array, int kind, xt_value value) {
|
|
258
258
|
uint32_t length = xt_ta_length(array);
|
|
259
259
|
xt_object_set(array, xt_string_from_cstr("length"), xt_number((double)(length + 1)));
|
|
260
|
-
xt_object_set(array, xt_string_from_cstr("byteLength"),
|
|
260
|
+
xt_object_set(array, xt_string_from_cstr("byteLength"),
|
|
261
|
+
xt_number((double)(length + 1) * xt_ta_bytes[kind]));
|
|
261
262
|
xt_ta_store(array, kind, length, xt_to_number(value));
|
|
262
263
|
return array;
|
|
263
264
|
}
|
|
264
265
|
|
|
266
|
+
uint32_t xt_typed_array_length(xt_value value) {
|
|
267
|
+
return xt_typed_array_kind_of(value) >= 0 ? xt_ta_length(value) : 0;
|
|
268
|
+
}
|
|
269
|
+
|
package/src/checker/globals.ts
CHANGED
|
@@ -14,6 +14,7 @@ import {
|
|
|
14
14
|
GLOBAL_FUNCTIONS,
|
|
15
15
|
NAMESPACE_PROPERTIES,
|
|
16
16
|
NAMESPACE_STATICS,
|
|
17
|
+
NAMESPACE_VALUES,
|
|
17
18
|
TYPED_ARRAYS,
|
|
18
19
|
} from "../codegen/generator/tables.js";
|
|
19
20
|
|
|
@@ -25,6 +26,7 @@ const KNOWN_GLOBALS: ReadonlySet<string> = new Set<string>([
|
|
|
25
26
|
...Object.keys(TYPED_ARRAYS),
|
|
26
27
|
...Object.keys(NAMESPACE_STATICS),
|
|
27
28
|
...Object.keys(NAMESPACE_PROPERTIES),
|
|
29
|
+
...Object.keys(NAMESPACE_VALUES),
|
|
28
30
|
...FIRST_CLASS_CONSTRUCTORS,
|
|
29
31
|
// Handled directly in `emitIdentifier` rather than through a table.
|
|
30
32
|
"console",
|
|
@@ -151,6 +151,9 @@ export const RUNTIME_DECLARATIONS: readonly string[] = [
|
|
|
151
151
|
"declare i64 @xt_number_static(i64, i32, i64*)",
|
|
152
152
|
"declare i64 @xt_string_static(i64, i32, i64*)",
|
|
153
153
|
"declare i64 @xt_date_static(i64, i32, i64*)",
|
|
154
|
+
"declare i64 @xt_crypto_static(i64, i32, i64*)",
|
|
155
|
+
"declare i64 @xt_crypto_object(i32, i64*)",
|
|
156
|
+
"declare i64 @xt_crypto_get(i64)",
|
|
154
157
|
"declare i64 @xt_path_static(i64, i32, i64*)",
|
|
155
158
|
"declare i64 @xt_os_static(i64, i32, i64*)",
|
|
156
159
|
"declare i64 @xt_buffer_static(i64, i32, i64*)",
|
|
@@ -243,6 +243,7 @@ export const NAMESPACE_STATICS: Record<string, string> = {
|
|
|
243
243
|
BigInt: "xt_bigint_static",
|
|
244
244
|
Symbol: "xt_symbol_static",
|
|
245
245
|
Date: "xt_date_static",
|
|
246
|
+
crypto: "xt_crypto_static",
|
|
246
247
|
Promise: "xt_promise_static",
|
|
247
248
|
ArrayBuffer: "xt_array_buffer_static",
|
|
248
249
|
path: "xt_path_static",
|
|
@@ -286,6 +287,7 @@ export const SYMBOL_WELL_KNOWN = new Set<string>([
|
|
|
286
287
|
/** Namespaces that also exist as a first-class object value (`Math.cos`). */
|
|
287
288
|
export const NAMESPACE_VALUES: Record<string, string> = {
|
|
288
289
|
Math: "xt_math_object",
|
|
290
|
+
crypto: "xt_crypto_object",
|
|
289
291
|
};
|
|
290
292
|
|
|
291
293
|
/** Namespace identifiers whose property access maps to a runtime getter. */
|
|
@@ -294,6 +296,7 @@ export const NAMESPACE_PROPERTIES: Record<string, string> = {
|
|
|
294
296
|
"import.meta": "xt_import_meta",
|
|
295
297
|
Symbol: "xt_symbol_get",
|
|
296
298
|
performance: "xt_performance_get",
|
|
299
|
+
crypto: "xt_crypto_get",
|
|
297
300
|
};
|
|
298
301
|
|
|
299
302
|
/** Global constructors called as `new X(...)` (all have signature `(i32, i64*)`). */
|
package/src/driver/compiler.ts
CHANGED
|
@@ -354,7 +354,7 @@ export function build(entryPath: string, options: BuildOptions = {}): BuildResul
|
|
|
354
354
|
outputPath,
|
|
355
355
|
linkerFlags: [
|
|
356
356
|
...toolchain.linkerArgs,
|
|
357
|
-
...(process.platform === "win32" ? ["-lws2_32"] : ["-lm", "-pthread"]),
|
|
357
|
+
...(process.platform === "win32" ? ["-lws2_32", "-lbcrypt"] : ["-lm", "-pthread"]),
|
|
358
358
|
...registry.linkerFlags(),
|
|
359
359
|
...linkFeatures.flatMap((feature) => feature.linkerFlags),
|
|
360
360
|
/* A GUI program is a Windows-subsystem executable: no console window is
|
package/src/driver/version.ts
CHANGED
|
@@ -13,11 +13,22 @@ const builtins: Record<string, BuiltinFunction> = {
|
|
|
13
13
|
|
|
14
14
|
const exports: ModuleExports = {
|
|
15
15
|
createHash: { symbol: "xt_crypto_create_hash" },
|
|
16
|
+
randomBytes: { symbol: "xt_crypto_random_bytes" },
|
|
17
|
+
randomUUID: { symbol: "xt_crypto_random_uuid" },
|
|
18
|
+
randomInt: { symbol: "xt_crypto_random_int" },
|
|
19
|
+
randomFillSync: { symbol: "xt_crypto_random_fill_sync" },
|
|
20
|
+
randomFill: { symbol: "xt_crypto_random_fill" },
|
|
21
|
+
pbkdf2Sync: { symbol: "xt_crypto_pbkdf2_sync" },
|
|
22
|
+
pbkdf2: { symbol: "xt_crypto_pbkdf2" },
|
|
23
|
+
webcrypto: { symbol: "xt_crypto_webcrypto", valueSymbol: "xt_crypto_webcrypto" },
|
|
16
24
|
};
|
|
17
25
|
|
|
18
26
|
export const cryptoModule: NodeModule = {
|
|
19
27
|
name: "crypto",
|
|
20
|
-
runtimeSources: () => [
|
|
28
|
+
runtimeSources: () => [
|
|
29
|
+
resolveFrom(import.meta.url, "../../../../runtime/ext_node/crypto/crypto.c"),
|
|
30
|
+
resolveFrom(import.meta.url, "../../../../runtime/ext_node/crypto/crypto_random.c"),
|
|
31
|
+
],
|
|
21
32
|
builtins: () => builtins,
|
|
22
33
|
exports: () => exports,
|
|
23
34
|
};
|