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,143 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* xbintsc runtime — OS entropy and the process-wide PRNG.
|
|
3
|
+
*
|
|
4
|
+
* `Math.random` used to be `rand() / (RAND_MAX + 1)`, which on the MSVC CRT has
|
|
5
|
+
* only 32 767 output states: salts and nonces derived from it collided and were
|
|
6
|
+
* predictable. This unit reads OS entropy (`BCryptGenRandom` on Windows,
|
|
7
|
+
* `/dev/urandom` elsewhere) for the CSPRNG surface and seeds a xoshiro256**
|
|
8
|
+
* generator for `Math.random`, so every run produces a different, well-mixed
|
|
9
|
+
* stream.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
#include "rt.h"
|
|
13
|
+
|
|
14
|
+
#include <stdint.h>
|
|
15
|
+
#include <string.h>
|
|
16
|
+
#include <time.h>
|
|
17
|
+
|
|
18
|
+
#if defined(_WIN32)
|
|
19
|
+
#include <windows.h>
|
|
20
|
+
#include <bcrypt.h>
|
|
21
|
+
#else
|
|
22
|
+
#include <errno.h>
|
|
23
|
+
#include <fcntl.h>
|
|
24
|
+
#include <unistd.h>
|
|
25
|
+
#if defined(__APPLE__)
|
|
26
|
+
#include <stdlib.h>
|
|
27
|
+
#endif
|
|
28
|
+
#endif
|
|
29
|
+
|
|
30
|
+
static uint64_t xt_random_state[4];
|
|
31
|
+
static volatile int xt_random_seeded = 0;
|
|
32
|
+
|
|
33
|
+
/** SplitMix64, used to expand the seed material and to avoid an all-zero
|
|
34
|
+
* xoshiro state. */
|
|
35
|
+
static uint64_t xt_splitmix64(uint64_t *state) {
|
|
36
|
+
uint64_t z = (*state += 0x9E3779B97F4A7C15ULL);
|
|
37
|
+
z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ULL;
|
|
38
|
+
z = (z ^ (z >> 27)) * 0x94D049BB133111EBULL;
|
|
39
|
+
return z ^ (z >> 31);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
static uint64_t xt_rotate_left(uint64_t value, int bits) {
|
|
43
|
+
return (value << bits) | (value >> (64 - bits));
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
int xt_secure_random(uint8_t *out, size_t length) {
|
|
47
|
+
if (length == 0) return 0;
|
|
48
|
+
if (out == NULL) return -1;
|
|
49
|
+
#if defined(_WIN32)
|
|
50
|
+
NTSTATUS status = BCryptGenRandom(NULL, (PUCHAR)out, (ULONG)length,
|
|
51
|
+
BCRYPT_USE_SYSTEM_PREFERRED_RNG);
|
|
52
|
+
return status == 0 ? 0 : -1;
|
|
53
|
+
#elif defined(__APPLE__)
|
|
54
|
+
arc4random_buf(out, length);
|
|
55
|
+
return 0;
|
|
56
|
+
#else
|
|
57
|
+
int fd = open("/dev/urandom", O_RDONLY);
|
|
58
|
+
if (fd < 0) return -1;
|
|
59
|
+
size_t offset = 0;
|
|
60
|
+
while (offset < length) {
|
|
61
|
+
ssize_t got = read(fd, out + offset, length - offset);
|
|
62
|
+
if (got <= 0) {
|
|
63
|
+
if (got < 0 && errno == EINTR) continue;
|
|
64
|
+
close(fd);
|
|
65
|
+
return -1;
|
|
66
|
+
}
|
|
67
|
+
offset += (size_t)got;
|
|
68
|
+
}
|
|
69
|
+
close(fd);
|
|
70
|
+
return 0;
|
|
71
|
+
#endif
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Fill four 64-bit words with seed material. The clock, the process id, the
|
|
75
|
+
* address of a static and a counter are mixed in even when the OS CSPRNG
|
|
76
|
+
* works, so two processes started in the same nanosecond cannot share a
|
|
77
|
+
* stream. */
|
|
78
|
+
static void xt_random_fill_seed(uint64_t seed[4]) {
|
|
79
|
+
uint8_t bytes[32];
|
|
80
|
+
uint64_t material[4];
|
|
81
|
+
if (xt_secure_random(bytes, sizeof(bytes)) == 0) {
|
|
82
|
+
memcpy(material, bytes, sizeof(material));
|
|
83
|
+
} else {
|
|
84
|
+
material[0] = (uint64_t)time(NULL);
|
|
85
|
+
material[1] = (uint64_t)clock();
|
|
86
|
+
#if defined(_WIN32)
|
|
87
|
+
LARGE_INTEGER counter;
|
|
88
|
+
QueryPerformanceCounter(&counter);
|
|
89
|
+
material[2] = (uint64_t)counter.QuadPart;
|
|
90
|
+
material[3] = (uint64_t)GetCurrentProcessId();
|
|
91
|
+
#else
|
|
92
|
+
struct timespec ts;
|
|
93
|
+
clock_gettime(CLOCK_MONOTONIC, &ts);
|
|
94
|
+
material[2] = (uint64_t)ts.tv_sec * 1000000000ULL + (uint64_t)ts.tv_nsec;
|
|
95
|
+
material[3] = (uint64_t)getpid();
|
|
96
|
+
#endif
|
|
97
|
+
}
|
|
98
|
+
material[0] ^= (uint64_t)(uintptr_t)&xt_random_state;
|
|
99
|
+
material[1] ^= (uint64_t)(uintptr_t)&seed;
|
|
100
|
+
uint64_t state = material[0] ^ xt_rotate_left(material[1], 17);
|
|
101
|
+
for (int i = 0; i < 4; i++) {
|
|
102
|
+
seed[i] = xt_splitmix64(&state);
|
|
103
|
+
seed[i] ^= material[i];
|
|
104
|
+
seed[i] = xt_splitmix64(&seed[i]);
|
|
105
|
+
}
|
|
106
|
+
if ((seed[0] | seed[1] | seed[2] | seed[3]) == 0) seed[0] = 0x9E3779B97F4A7C15ULL;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
static void xt_random_seed(void) {
|
|
110
|
+
uint64_t seed[4];
|
|
111
|
+
xt_random_fill_seed(seed);
|
|
112
|
+
memcpy(xt_random_state, seed, sizeof(seed));
|
|
113
|
+
xt_random_seeded = 1;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
uint64_t xt_random_u64(void) {
|
|
117
|
+
if (!xt_random_seeded) xt_random_seed();
|
|
118
|
+
uint64_t *s = xt_random_state;
|
|
119
|
+
uint64_t result = xt_rotate_left(s[1] * 5, 7) * 9;
|
|
120
|
+
uint64_t t = s[1] << 17;
|
|
121
|
+
s[2] ^= s[0];
|
|
122
|
+
s[3] ^= s[1];
|
|
123
|
+
s[1] ^= s[2];
|
|
124
|
+
s[0] ^= s[3];
|
|
125
|
+
s[2] ^= t;
|
|
126
|
+
s[3] = xt_rotate_left(s[3], 45);
|
|
127
|
+
return result;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
double xt_random_double(void) {
|
|
131
|
+
/* 53 random bits: every double in [0, 1) that JavaScript can represent. */
|
|
132
|
+
return (double)(xt_random_u64() >> 11) * (1.0 / 9007199254740992.0);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
uint64_t xt_random_below(uint64_t bound) {
|
|
136
|
+
if (bound == 0) return 0;
|
|
137
|
+
/* Rejection sampling removes the modulo bias of `% bound`. */
|
|
138
|
+
uint64_t threshold = (uint64_t)(-bound) % bound;
|
|
139
|
+
for (;;) {
|
|
140
|
+
uint64_t value = xt_random_u64();
|
|
141
|
+
if (value >= threshold) return value % bound;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
@@ -160,35 +160,16 @@ static int xt_re_repeat_try(xt_re_repeat *rep, size_t pos, int count, const xt_r
|
|
|
160
160
|
static int xt_re_match_piece(xt_re_piece *piece, const xt_re_input *in, size_t pos,
|
|
161
161
|
xt_re_cont cont, void *ctx, const xt_re_cap *caps) {
|
|
162
162
|
xt_re_repeat rep;
|
|
163
|
+
if (piece->quant == XT_RE_Q_NONE)
|
|
164
|
+
return xt_re_match_atom(piece->node, in, pos, cont, ctx, caps);
|
|
165
|
+
rep.atom = piece->node;
|
|
166
|
+
rep.in = in;
|
|
167
|
+
rep.min = piece->min;
|
|
168
|
+
rep.max = piece->max;
|
|
163
169
|
rep.lazy = piece->lazy;
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
rep.in = in;
|
|
168
|
-
rep.min = 0;
|
|
169
|
-
rep.max = -1;
|
|
170
|
-
rep.cont = cont;
|
|
171
|
-
rep.ctx = ctx;
|
|
172
|
-
return xt_re_repeat_try(&rep, pos, 0, caps);
|
|
173
|
-
case XT_RE_Q_PLUS:
|
|
174
|
-
rep.atom = piece->node;
|
|
175
|
-
rep.in = in;
|
|
176
|
-
rep.min = 1;
|
|
177
|
-
rep.max = -1;
|
|
178
|
-
rep.cont = cont;
|
|
179
|
-
rep.ctx = ctx;
|
|
180
|
-
return xt_re_repeat_try(&rep, pos, 0, caps);
|
|
181
|
-
case XT_RE_Q_QUEST:
|
|
182
|
-
rep.atom = piece->node;
|
|
183
|
-
rep.in = in;
|
|
184
|
-
rep.min = 0;
|
|
185
|
-
rep.max = 1;
|
|
186
|
-
rep.cont = cont;
|
|
187
|
-
rep.ctx = ctx;
|
|
188
|
-
return xt_re_repeat_try(&rep, pos, 0, caps);
|
|
189
|
-
default:
|
|
190
|
-
return xt_re_match_atom(piece->node, in, pos, cont, ctx, caps);
|
|
191
|
-
}
|
|
170
|
+
rep.cont = cont;
|
|
171
|
+
rep.ctx = ctx;
|
|
172
|
+
return xt_re_repeat_try(&rep, pos, 0, caps);
|
|
192
173
|
}
|
|
193
174
|
|
|
194
175
|
typedef struct {
|
|
@@ -33,7 +33,8 @@ static xt_re_node *xt_re_new_class(const unsigned char *bits) {
|
|
|
33
33
|
return n;
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
static void xt_re_push_piece(xt_re_concat *c, xt_re_node *node, int quant, int lazy
|
|
36
|
+
static void xt_re_push_piece(xt_re_concat *c, xt_re_node *node, int quant, int lazy, int min,
|
|
37
|
+
int max) {
|
|
37
38
|
if (c->count == c->capacity) {
|
|
38
39
|
int next = c->capacity ? c->capacity * 2 : 4;
|
|
39
40
|
xt_re_piece *grown = (xt_re_piece *)realloc(c->pieces, (size_t)next * sizeof(xt_re_piece));
|
|
@@ -44,6 +45,8 @@ static void xt_re_push_piece(xt_re_concat *c, xt_re_node *node, int quant, int l
|
|
|
44
45
|
c->pieces[c->count].node = node;
|
|
45
46
|
c->pieces[c->count].quant = quant;
|
|
46
47
|
c->pieces[c->count].lazy = lazy;
|
|
48
|
+
c->pieces[c->count].min = min;
|
|
49
|
+
c->pieces[c->count].max = max;
|
|
47
50
|
c->count++;
|
|
48
51
|
}
|
|
49
52
|
|
|
@@ -188,30 +191,90 @@ static xt_re_node *xt_re_parse_atom(xt_re_parser *p) {
|
|
|
188
191
|
return xt_re_new_char((unsigned char)c);
|
|
189
192
|
}
|
|
190
193
|
|
|
194
|
+
/** Parse `{n}`, `{n,}` or `{n,m}` starting at `p->i`. On success advances past
|
|
195
|
+
* the closing brace and reports the bounds. A malformed group (or one whose
|
|
196
|
+
* bound is unreasonable) is left untouched so its `{` is matched as a literal,
|
|
197
|
+
* which is what JavaScript does for `a{` and `a{x}`; an inverted range such as
|
|
198
|
+
* `a{2,1}` is treated as literal too rather than rejected. */
|
|
199
|
+
static int xt_re_parse_brace(xt_re_parser *p, int *min, int *max) {
|
|
200
|
+
size_t i = p->i;
|
|
201
|
+
long low = 0;
|
|
202
|
+
long high = 0;
|
|
203
|
+
int have_low = 0;
|
|
204
|
+
int have_high = 0;
|
|
205
|
+
if (p->s[i] != '{') return 0;
|
|
206
|
+
i++;
|
|
207
|
+
while (p->s[i] >= '0' && p->s[i] <= '9') {
|
|
208
|
+
low = low * 10 + (p->s[i] - '0');
|
|
209
|
+
if (low > XT_RE_MAX_REPEAT) return 0;
|
|
210
|
+
have_low = 1;
|
|
211
|
+
i++;
|
|
212
|
+
}
|
|
213
|
+
if (!have_low) return 0;
|
|
214
|
+
if (p->s[i] == '}') {
|
|
215
|
+
i++;
|
|
216
|
+
*min = (int)low;
|
|
217
|
+
*max = (int)low;
|
|
218
|
+
p->i = i;
|
|
219
|
+
return 1;
|
|
220
|
+
}
|
|
221
|
+
if (p->s[i] != ',') return 0;
|
|
222
|
+
i++;
|
|
223
|
+
while (p->s[i] >= '0' && p->s[i] <= '9') {
|
|
224
|
+
high = high * 10 + (p->s[i] - '0');
|
|
225
|
+
if (high > XT_RE_MAX_REPEAT) return 0;
|
|
226
|
+
have_high = 1;
|
|
227
|
+
i++;
|
|
228
|
+
}
|
|
229
|
+
if (p->s[i] != '}') return 0;
|
|
230
|
+
i++;
|
|
231
|
+
if (have_high && high < low) return 0;
|
|
232
|
+
*min = (int)low;
|
|
233
|
+
*max = have_high ? (int)high : -1;
|
|
234
|
+
p->i = i;
|
|
235
|
+
return 1;
|
|
236
|
+
}
|
|
237
|
+
|
|
191
238
|
static void xt_re_parse_concat(xt_re_parser *p, xt_re_concat *out) {
|
|
192
239
|
while (p->s[p->i] && p->s[p->i] != '|' && p->s[p->i] != ')') {
|
|
193
240
|
xt_re_node *node = xt_re_parse_atom(p);
|
|
194
241
|
int quant = XT_RE_Q_NONE;
|
|
195
242
|
int lazy = 0;
|
|
243
|
+
int min = 0;
|
|
244
|
+
int max = 0;
|
|
196
245
|
if (p->error || !node) {
|
|
197
246
|
p->error = 1;
|
|
198
247
|
return;
|
|
199
248
|
}
|
|
200
249
|
if (p->s[p->i] == '*') {
|
|
201
250
|
quant = XT_RE_Q_STAR;
|
|
251
|
+
min = 0;
|
|
252
|
+
max = -1;
|
|
202
253
|
p->i++;
|
|
203
254
|
} else if (p->s[p->i] == '+') {
|
|
204
255
|
quant = XT_RE_Q_PLUS;
|
|
256
|
+
min = 1;
|
|
257
|
+
max = -1;
|
|
205
258
|
p->i++;
|
|
206
259
|
} else if (p->s[p->i] == '?') {
|
|
207
260
|
quant = XT_RE_Q_QUEST;
|
|
261
|
+
min = 0;
|
|
262
|
+
max = 1;
|
|
208
263
|
p->i++;
|
|
264
|
+
} else if (p->s[p->i] == '{') {
|
|
265
|
+
int range_min = 0;
|
|
266
|
+
int range_max = 0;
|
|
267
|
+
if (xt_re_parse_brace(p, &range_min, &range_max)) {
|
|
268
|
+
quant = XT_RE_Q_RANGE;
|
|
269
|
+
min = range_min;
|
|
270
|
+
max = range_max;
|
|
271
|
+
}
|
|
209
272
|
}
|
|
210
273
|
if (quant != XT_RE_Q_NONE && p->s[p->i] == '?') {
|
|
211
274
|
lazy = 1;
|
|
212
275
|
p->i++;
|
|
213
276
|
}
|
|
214
|
-
xt_re_push_piece(out, node, quant, lazy);
|
|
277
|
+
xt_re_push_piece(out, node, quant, lazy, min, max);
|
|
215
278
|
}
|
|
216
279
|
}
|
|
217
280
|
|
|
@@ -23,9 +23,16 @@ enum {
|
|
|
23
23
|
XT_RE_Q_NONE = 0,
|
|
24
24
|
XT_RE_Q_STAR,
|
|
25
25
|
XT_RE_Q_PLUS,
|
|
26
|
-
XT_RE_Q_QUEST
|
|
26
|
+
XT_RE_Q_QUEST,
|
|
27
|
+
XT_RE_Q_RANGE
|
|
27
28
|
};
|
|
28
29
|
|
|
30
|
+
/** Upper bound for a counted quantifier (`a{0,20000}`): the matcher recurses
|
|
31
|
+
* once per repetition, so an unbounded authored bound could overflow the C
|
|
32
|
+
* stack. A brace group whose count exceeds this is treated as literal text,
|
|
33
|
+
* exactly like any other malformed `{...}`. */
|
|
34
|
+
#define XT_RE_MAX_REPEAT 4096
|
|
35
|
+
|
|
29
36
|
typedef struct xt_re_alt xt_re_alt;
|
|
30
37
|
|
|
31
38
|
typedef struct {
|
|
@@ -40,6 +47,9 @@ typedef struct {
|
|
|
40
47
|
xt_re_node *node;
|
|
41
48
|
int quant;
|
|
42
49
|
int lazy;
|
|
50
|
+
/** Repetition bounds for the quantifier (`max == -1` means unbounded). */
|
|
51
|
+
int min;
|
|
52
|
+
int max;
|
|
43
53
|
} xt_re_piece;
|
|
44
54
|
|
|
45
55
|
typedef struct {
|
package/runtime/xt_regex.h
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* xbintsc runtime — minimal POSIX ERE-compatible regular expressions.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
4
|
+
* The runtime uses this self-contained matcher on every host instead of the C
|
|
5
|
+
* library's <regex.h>. Host implementations disagree on JavaScript-visible
|
|
6
|
+
* details (lazy quantifiers, counted-quantifier limits, malformed `{...}`
|
|
7
|
+
* groups), so one engine keeps a program's output identical everywhere. It
|
|
8
|
+
* implements the subset the runtime uses:
|
|
6
9
|
*
|
|
7
10
|
* literals, '.', '^', '$', '[abc]', '[^abc]', '[a-z]', '*', '+', '?',
|
|
8
11
|
* alternation '|', groups '(...)', and escapes ('\d', '\w', '\s', ...).
|
|
12
|
+
* Counted quantifiers `{n}`, `{n,}` and `{n,m}` accept a lazy `?` suffix.
|
|
9
13
|
*
|
|
10
14
|
* Capturing groups are reported through `regmatch_t` the same way POSIX does:
|
|
11
15
|
* `pmatch[0]` is the whole match and `pmatch[k]` the k-th group in pattern
|
|
@@ -158,6 +158,8 @@ static const xt_method_spec XT_DATE_METHODS[] = {
|
|
|
158
158
|
{"getUTCFullYear", 0}, {"getUTCMonth", 0}, {"getUTCDate", 0}, {"getUTCDay", 0},
|
|
159
159
|
{"getUTCHours", 0}, {"getUTCMinutes", 0}, {"getUTCSeconds", 0}, {"getUTCMilliseconds", 0},
|
|
160
160
|
{"toISOString", 0}, {"toJSON", 1}, {"toString", 0}, {"valueOf", 0},
|
|
161
|
+
{"toLocaleDateString", 0}, {"toLocaleTimeString", 0}, {"toLocaleString", 0},
|
|
162
|
+
{"toDateString", 0}, {"toTimeString", 0}, {"toUTCString", 0},
|
|
161
163
|
};
|
|
162
164
|
|
|
163
165
|
static const xt_method_spec XT_MAP_METHODS[] = {
|
|
@@ -48,7 +48,7 @@ xt_value xt_math_call(xt_value name, int32_t argc, xt_value *argv) {
|
|
|
48
48
|
if (isnan(a)) return xt_number(NAN);
|
|
49
49
|
return xt_number(a > 0 ? 1.0 : a < 0 ? -1.0 : a);
|
|
50
50
|
}
|
|
51
|
-
if (strcmp(fn, "random") == 0) return xt_number((
|
|
51
|
+
if (strcmp(fn, "random") == 0) return xt_number(xt_random_double());
|
|
52
52
|
if (strcmp(fn, "min") == 0) {
|
|
53
53
|
double result = INFINITY;
|
|
54
54
|
for (int32_t i = 0; i < argc; i++) {
|
|
@@ -149,9 +149,172 @@ xt_value xt_date_static(xt_value name, int32_t argc, xt_value *argv) {
|
|
|
149
149
|
return XT_UNDEFINED;
|
|
150
150
|
}
|
|
151
151
|
|
|
152
|
+
static const char *const xt_date_month_long[12] = {
|
|
153
|
+
"January", "February", "March", "April", "May", "June",
|
|
154
|
+
"July", "August", "September", "October", "November", "December",
|
|
155
|
+
};
|
|
156
|
+
static const char *const xt_date_month_short[12] = {
|
|
157
|
+
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
|
|
158
|
+
};
|
|
159
|
+
static const char *const xt_date_weekday_long[7] = {
|
|
160
|
+
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday",
|
|
161
|
+
};
|
|
162
|
+
static const char *const xt_date_weekday_short[7] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
|
|
163
|
+
|
|
164
|
+
static void xt_date_append(char *out, size_t capacity, size_t *length, const char *text) {
|
|
165
|
+
if (text == NULL) return;
|
|
166
|
+
size_t count = strlen(text);
|
|
167
|
+
if (*length + count + 1 > capacity) count = capacity > *length + 1 ? capacity - *length - 1 : 0;
|
|
168
|
+
memcpy(out + *length, text, count);
|
|
169
|
+
*length += count;
|
|
170
|
+
out[*length] = '\0';
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
static void xt_date_append_int(char *out, size_t capacity, size_t *length, int value, int width) {
|
|
174
|
+
char buffer[24];
|
|
175
|
+
if (width == 2) {
|
|
176
|
+
snprintf(buffer, sizeof(buffer), "%02d", value);
|
|
177
|
+
} else {
|
|
178
|
+
snprintf(buffer, sizeof(buffer), "%d", value);
|
|
179
|
+
}
|
|
180
|
+
xt_date_append(out, capacity, length, buffer);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** Read a string option (`{ month: "long" }`); null when absent. */
|
|
184
|
+
static const char *xt_date_option(xt_value options, const char *name) {
|
|
185
|
+
if (!XT_IS_OBJECT(options)) return NULL;
|
|
186
|
+
xt_value value = xt_object_get_cstr(options, name);
|
|
187
|
+
if (XT_IS_UNDEFINED(value) || XT_IS_NULL(value)) return NULL;
|
|
188
|
+
return xt_string_data(xt_to_string(value));
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
static int xt_date_option_width(const char *value) {
|
|
192
|
+
return value != NULL && strcmp(value, "2-digit") == 0 ? 2 : 1;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
static int xt_date_has_option(xt_value options, const char *const *names, int count) {
|
|
196
|
+
for (int i = 0; i < count; i++) {
|
|
197
|
+
if (xt_date_option(options, names[i]) != NULL) return 1;
|
|
198
|
+
}
|
|
199
|
+
return 0;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** `toLocaleDateString` with a small, deterministic en-US-style subset of the
|
|
203
|
+
* options object. No locale database is linked, so unknown locales still use
|
|
204
|
+
* the same month/day order. */
|
|
205
|
+
static xt_value xt_date_locale_date(const struct tm *tm, xt_value options) {
|
|
206
|
+
static const char *const names[] = {"weekday", "year", "month", "day"};
|
|
207
|
+
int custom = xt_date_has_option(options, names, 4);
|
|
208
|
+
char buffer[128];
|
|
209
|
+
size_t length = 0;
|
|
210
|
+
buffer[0] = '\0';
|
|
211
|
+
if (!custom) {
|
|
212
|
+
xt_date_append_int(buffer, sizeof(buffer), &length, tm->tm_mon + 1, 1);
|
|
213
|
+
xt_date_append(buffer, sizeof(buffer), &length, "/");
|
|
214
|
+
xt_date_append_int(buffer, sizeof(buffer), &length, tm->tm_mday, 1);
|
|
215
|
+
xt_date_append(buffer, sizeof(buffer), &length, "/");
|
|
216
|
+
xt_date_append_int(buffer, sizeof(buffer), &length, tm->tm_year + 1900, 4);
|
|
217
|
+
return xt_string_from_cstr(buffer);
|
|
218
|
+
}
|
|
219
|
+
const char *weekday = xt_date_option(options, "weekday");
|
|
220
|
+
if (weekday != NULL) {
|
|
221
|
+
if (strcmp(weekday, "long") == 0) xt_date_append(buffer, sizeof(buffer), &length,
|
|
222
|
+
xt_date_weekday_long[tm->tm_wday]);
|
|
223
|
+
else xt_date_append(buffer, sizeof(buffer), &length, xt_date_weekday_short[tm->tm_wday]);
|
|
224
|
+
xt_date_append(buffer, sizeof(buffer), &length, ", ");
|
|
225
|
+
}
|
|
226
|
+
const char *month = xt_date_option(options, "month");
|
|
227
|
+
const char *day = xt_date_option(options, "day");
|
|
228
|
+
const char *year = xt_date_option(options, "year");
|
|
229
|
+
if (month != NULL && (strcmp(month, "long") == 0 || strcmp(month, "short") == 0 ||
|
|
230
|
+
strcmp(month, "narrow") == 0)) {
|
|
231
|
+
if (strcmp(month, "long") == 0) {
|
|
232
|
+
xt_date_append(buffer, sizeof(buffer), &length, xt_date_month_long[tm->tm_mon]);
|
|
233
|
+
} else {
|
|
234
|
+
xt_date_append(buffer, sizeof(buffer), &length, xt_date_month_short[tm->tm_mon]);
|
|
235
|
+
}
|
|
236
|
+
if (day != NULL) {
|
|
237
|
+
xt_date_append(buffer, sizeof(buffer), &length, " ");
|
|
238
|
+
xt_date_append_int(buffer, sizeof(buffer), &length, tm->tm_mday,
|
|
239
|
+
xt_date_option_width(day));
|
|
240
|
+
}
|
|
241
|
+
if (year != NULL) {
|
|
242
|
+
xt_date_append(buffer, sizeof(buffer), &length, ", ");
|
|
243
|
+
xt_date_append_int(buffer, sizeof(buffer), &length,
|
|
244
|
+
xt_date_option_width(year) == 2 ? (tm->tm_year + 1900) % 100
|
|
245
|
+
: tm->tm_year + 1900,
|
|
246
|
+
xt_date_option_width(year));
|
|
247
|
+
}
|
|
248
|
+
return xt_string_from_cstr(buffer);
|
|
249
|
+
}
|
|
250
|
+
if (month != NULL) {
|
|
251
|
+
xt_date_append_int(buffer, sizeof(buffer), &length, tm->tm_mon + 1,
|
|
252
|
+
xt_date_option_width(month));
|
|
253
|
+
}
|
|
254
|
+
if (day != NULL) {
|
|
255
|
+
if (month != NULL) xt_date_append(buffer, sizeof(buffer), &length, "/");
|
|
256
|
+
xt_date_append_int(buffer, sizeof(buffer), &length, tm->tm_mday, xt_date_option_width(day));
|
|
257
|
+
}
|
|
258
|
+
if (year != NULL) {
|
|
259
|
+
if (month != NULL || day != NULL) xt_date_append(buffer, sizeof(buffer), &length, "/");
|
|
260
|
+
xt_date_append_int(buffer, sizeof(buffer), &length,
|
|
261
|
+
xt_date_option_width(year) == 2 ? (tm->tm_year + 1900) % 100
|
|
262
|
+
: tm->tm_year + 1900,
|
|
263
|
+
xt_date_option_width(year));
|
|
264
|
+
}
|
|
265
|
+
return xt_string_from_cstr(buffer);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
static xt_value xt_date_locale_time(const struct tm *tm, xt_value options) {
|
|
269
|
+
static const char *const names[] = {"hour", "minute", "second"};
|
|
270
|
+
int custom = xt_date_has_option(options, names, 3);
|
|
271
|
+
const char *hour12_option = xt_date_option(options, "hour12");
|
|
272
|
+
int hour12 = 1;
|
|
273
|
+
if (hour12_option != NULL) hour12 = strcmp(hour12_option, "false") != 0;
|
|
274
|
+
int hour = tm->tm_hour;
|
|
275
|
+
const char *suffix = hour >= 12 ? "PM" : "AM";
|
|
276
|
+
if (hour12) hour = hour % 12 == 0 ? 12 : hour % 12;
|
|
277
|
+
char buffer[64];
|
|
278
|
+
size_t length = 0;
|
|
279
|
+
buffer[0] = '\0';
|
|
280
|
+
const char *hour_option = xt_date_option(options, "hour");
|
|
281
|
+
const char *minute_option = xt_date_option(options, "minute");
|
|
282
|
+
const char *second_option = xt_date_option(options, "second");
|
|
283
|
+
if (!custom) {
|
|
284
|
+
xt_date_append_int(buffer, sizeof(buffer), &length, hour, 1);
|
|
285
|
+
xt_date_append(buffer, sizeof(buffer), &length, ":");
|
|
286
|
+
xt_date_append_int(buffer, sizeof(buffer), &length, tm->tm_min, 2);
|
|
287
|
+
xt_date_append(buffer, sizeof(buffer), &length, ":");
|
|
288
|
+
xt_date_append_int(buffer, sizeof(buffer), &length, tm->tm_sec, 2);
|
|
289
|
+
if (hour12) {
|
|
290
|
+
xt_date_append(buffer, sizeof(buffer), &length, " ");
|
|
291
|
+
xt_date_append(buffer, sizeof(buffer), &length, suffix);
|
|
292
|
+
}
|
|
293
|
+
return xt_string_from_cstr(buffer);
|
|
294
|
+
}
|
|
295
|
+
if (hour_option != NULL) {
|
|
296
|
+
xt_date_append_int(buffer, sizeof(buffer), &length, hour, xt_date_option_width(hour_option));
|
|
297
|
+
} else if (minute_option != NULL || second_option != NULL) {
|
|
298
|
+
xt_date_append_int(buffer, sizeof(buffer), &length, hour, 1);
|
|
299
|
+
}
|
|
300
|
+
if (minute_option != NULL) {
|
|
301
|
+
xt_date_append(buffer, sizeof(buffer), &length, ":");
|
|
302
|
+
xt_date_append_int(buffer, sizeof(buffer), &length, tm->tm_min,
|
|
303
|
+
xt_date_option_width(minute_option));
|
|
304
|
+
}
|
|
305
|
+
if (second_option != NULL) {
|
|
306
|
+
xt_date_append(buffer, sizeof(buffer), &length, ":");
|
|
307
|
+
xt_date_append_int(buffer, sizeof(buffer), &length, tm->tm_sec,
|
|
308
|
+
xt_date_option_width(second_option));
|
|
309
|
+
}
|
|
310
|
+
if (hour12) {
|
|
311
|
+
xt_date_append(buffer, sizeof(buffer), &length, " ");
|
|
312
|
+
xt_date_append(buffer, sizeof(buffer), &length, suffix);
|
|
313
|
+
}
|
|
314
|
+
return xt_string_from_cstr(buffer);
|
|
315
|
+
}
|
|
316
|
+
|
|
152
317
|
static xt_value xt_date_method(xt_value target, const char *method, int32_t argc, xt_value *argv, int *handled) {
|
|
153
|
-
(void)argc;
|
|
154
|
-
(void)argv;
|
|
155
318
|
*handled = 1;
|
|
156
319
|
xt_date *date = (xt_date *)XT_GET_PTR(target);
|
|
157
320
|
if (strcmp(method, "getTime") == 0 || strcmp(method, "valueOf") == 0) return xt_number(date->time);
|
|
@@ -167,8 +330,45 @@ static xt_value xt_date_method(xt_value target, const char *method, int32_t argc
|
|
|
167
330
|
}
|
|
168
331
|
/* Bare getters and `toString` report local time; `getUTC*` and
|
|
169
332
|
* `toISOString` report UTC. */
|
|
170
|
-
if (isUtc || strcmp(method, "toISOString") == 0
|
|
171
|
-
|
|
333
|
+
if (isUtc || strcmp(method, "toISOString") == 0 || strcmp(method, "toUTCString") == 0) {
|
|
334
|
+
xt_gmtime_r(&seconds, &tm);
|
|
335
|
+
} else {
|
|
336
|
+
xt_localtime_r(&seconds, &tm);
|
|
337
|
+
}
|
|
338
|
+
/* Locale/time-string methods answer "Invalid Date" for NaN time values, as
|
|
339
|
+
* browsers do; `toISOString` is the one that throws. */
|
|
340
|
+
if (strcmp(method, "toLocaleDateString") == 0 || strcmp(method, "toLocaleTimeString") == 0 ||
|
|
341
|
+
strcmp(method, "toLocaleString") == 0 || strcmp(method, "toDateString") == 0 ||
|
|
342
|
+
strcmp(method, "toTimeString") == 0 || strcmp(method, "toUTCString") == 0) {
|
|
343
|
+
if (isnan(date->time)) return xt_string_from_cstr("Invalid Date");
|
|
344
|
+
xt_value options = XT_UNDEFINED;
|
|
345
|
+
if (argc >= 2 && XT_IS_OBJECT(argv[1])) options = argv[1];
|
|
346
|
+
else if (argc >= 1 && XT_IS_OBJECT(argv[0])) options = argv[0];
|
|
347
|
+
if (strcmp(method, "toDateString") == 0) {
|
|
348
|
+
char buffer[48];
|
|
349
|
+
snprintf(buffer, sizeof(buffer), "%s %s %02d %04d", xt_date_weekday_short[tm.tm_wday],
|
|
350
|
+
xt_date_month_short[tm.tm_mon], tm.tm_mday, tm.tm_year + 1900);
|
|
351
|
+
return xt_string_from_cstr(buffer);
|
|
352
|
+
}
|
|
353
|
+
if (strcmp(method, "toTimeString") == 0) {
|
|
354
|
+
char buffer[32];
|
|
355
|
+
snprintf(buffer, sizeof(buffer), "%02d:%02d:%02d", tm.tm_hour, tm.tm_min, tm.tm_sec);
|
|
356
|
+
return xt_string_from_cstr(buffer);
|
|
357
|
+
}
|
|
358
|
+
if (strcmp(method, "toUTCString") == 0) {
|
|
359
|
+
char buffer[64];
|
|
360
|
+
snprintf(buffer, sizeof(buffer), "%s, %02d %s %04d %02d:%02d:%02d GMT",
|
|
361
|
+
xt_date_weekday_short[tm.tm_wday], tm.tm_mday, xt_date_month_short[tm.tm_mon],
|
|
362
|
+
tm.tm_year + 1900, tm.tm_hour, tm.tm_min, tm.tm_sec);
|
|
363
|
+
return xt_string_from_cstr(buffer);
|
|
364
|
+
}
|
|
365
|
+
xt_value date_part = xt_date_locale_date(&tm, options);
|
|
366
|
+
if (strcmp(method, "toLocaleDateString") == 0) return date_part;
|
|
367
|
+
xt_value time_part = xt_date_locale_time(&tm, options);
|
|
368
|
+
if (strcmp(method, "toLocaleTimeString") == 0) return time_part;
|
|
369
|
+
xt_value comma = xt_string_from_cstr(", ");
|
|
370
|
+
return xt_add(xt_add(date_part, comma), time_part);
|
|
371
|
+
}
|
|
172
372
|
if (strcmp(name, "getFullYear") == 0) return xt_number(tm.tm_year + 1900);
|
|
173
373
|
if (strcmp(name, "getMonth") == 0) return xt_number(tm.tm_mon);
|
|
174
374
|
if (strcmp(name, "getDate") == 0) return xt_number(tm.tm_mday);
|