xbintsc 0.5.36 → 0.5.38
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/cli/help.d.ts +1 -1
- package/dist/src/codegen/generator/state.js +1 -0
- package/dist/src/codegen/generator/state.js.map +1 -1
- package/dist/src/codegen/generator/tables.js +1 -0
- package/dist/src/codegen/generator/tables.js.map +1 -1
- package/dist/src/driver/bundler/deferred.d.ts +63 -0
- package/dist/src/driver/bundler/deferred.js +279 -0
- package/dist/src/driver/bundler/deferred.js.map +1 -0
- package/dist/src/driver/bundler/merge.js +214 -2
- package/dist/src/driver/bundler/merge.js.map +1 -1
- package/dist/src/driver/runtime-objects.d.ts +1 -1
- package/dist/src/driver/runtime-objects.js +1 -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/gui/html.js +89 -13
- package/dist/src/extensions/gui/html.js.map +1 -1
- package/dist/src/extensions/gui/index.js +13 -0
- package/dist/src/extensions/gui/index.js.map +1 -1
- package/dist/src/extensions/gui/three.d.ts +4 -3
- package/dist/src/extensions/gui/three.js +4 -3
- package/dist/src/extensions/gui/three.js.map +1 -1
- package/dist/tests/e2e/gui-browser.test.js +65 -0
- package/dist/tests/e2e/gui-browser.test.js.map +1 -1
- package/dist/tests/e2e/gui-canvas-args.test.d.ts +6 -0
- package/dist/tests/e2e/gui-canvas-args.test.js +217 -0
- package/dist/tests/e2e/gui-canvas-args.test.js.map +1 -0
- package/dist/tests/e2e/gui-canvas.test.js.map +1 -1
- package/dist/tests/e2e/gui-dom.test.js +215 -0
- package/dist/tests/e2e/gui-dom.test.js.map +1 -1
- package/dist/tests/e2e/gui-events.test.js +112 -0
- package/dist/tests/e2e/gui-events.test.js.map +1 -1
- package/dist/tests/e2e/gui-script-import-order.test.d.ts +10 -0
- package/dist/tests/e2e/gui-script-import-order.test.js +161 -0
- package/dist/tests/e2e/gui-script-import-order.test.js.map +1 -0
- package/dist/tests/e2e/gui-webgl2-shadow.test.d.ts +10 -0
- package/dist/tests/e2e/gui-webgl2-shadow.test.js +108 -0
- package/dist/tests/e2e/gui-webgl2-shadow.test.js.map +1 -0
- package/dist/tests/e2e/url.test.d.ts +6 -0
- package/dist/tests/e2e/url.test.js +114 -0
- package/dist/tests/e2e/url.test.js.map +1 -0
- package/dist/tests/extensions/gui.test.js +22 -10
- package/dist/tests/extensions/gui.test.js.map +1 -1
- package/doc/gui/api.md +38 -9
- package/doc/gui/canvas.md +49 -18
- package/doc/gui/dom.md +51 -4
- package/doc/gui/webgl.md +20 -10
- package/doc/gui-scripts.md +19 -19
- package/doc/gui.md +10 -8
- package/doc/implemented/fetch.md +5 -0
- package/doc/implemented/modules.md +13 -0
- package/doc/implemented/stdlib.md +17 -0
- package/doc/unimplemented/deviations.md +1 -0
- package/doc/zh-CN/gui/api.md +33 -4
- package/doc/zh-CN/gui/canvas.md +44 -19
- package/doc/zh-CN/gui/dom.md +45 -3
- package/doc/zh-CN/gui/webgl.md +3 -3
- package/doc/zh-CN/gui-scripts.md +14 -13
- package/doc/zh-CN/gui.md +7 -6
- package/doc/zh-CN/implemented/fetch.md +4 -0
- package/doc/zh-CN/implemented/modules.md +10 -0
- package/doc/zh-CN/implemented/stdlib.md +15 -0
- package/doc/zh-CN/unimplemented/deviations.md +1 -0
- package/package.json +7 -7
- package/runtime/ext_gui/browser.cpp +34 -49
- package/runtime/ext_gui/browser_abort.cpp +249 -0
- package/runtime/ext_gui/browser_dialogs.cpp +76 -0
- package/runtime/ext_gui/browser_media.cpp +176 -0
- package/runtime/ext_gui/browser_navigation.cpp +130 -0
- package/runtime/ext_gui/canvas.cpp +88 -30
- package/runtime/ext_gui/canvas.h +74 -9
- package/runtime/ext_gui/canvas_image_data.cpp +143 -0
- package/runtime/ext_gui/canvas_internal.h +8 -5
- package/runtime/ext_gui/canvas_pattern.cpp +90 -0
- package/runtime/ext_gui/canvas_raster.cpp +31 -9
- package/runtime/ext_gui/canvas_style.cpp +4 -2
- package/runtime/ext_gui/dom.h +4 -0
- package/runtime/ext_gui/dom_api.cpp +1 -0
- package/runtime/ext_gui/dom_api_canvas.cpp +120 -114
- package/runtime/ext_gui/dom_api_canvas_extra.cpp +57 -2
- package/runtime/ext_gui/dom_api_canvas_image.cpp +239 -0
- package/runtime/ext_gui/dom_api_canvas_paths.cpp +179 -0
- package/runtime/ext_gui/dom_api_computed_style.cpp +124 -0
- package/runtime/ext_gui/dom_api_dataset.cpp +154 -0
- package/runtime/ext_gui/dom_api_document.cpp +58 -5
- package/runtime/ext_gui/dom_api_events.cpp +93 -17
- package/runtime/ext_gui/dom_api_internal.h +55 -2
- package/runtime/ext_gui/dom_api_mutation.cpp +159 -0
- package/runtime/ext_gui/dom_api_node.cpp +40 -19
- package/runtime/ext_gui/dom_api_node_read.cpp +37 -1
- package/runtime/ext_gui/dom_api_path2d.cpp +358 -0
- package/runtime/ext_gui/dom_api_proto.cpp +35 -0
- package/runtime/ext_gui/dom_api_style.cpp +223 -13
- package/runtime/ext_gui/dom_api_webgl.cpp +10 -0
- package/runtime/ext_gui/dom_api_webgl2.cpp +33 -1
- package/runtime/ext_gui/gui.cpp +59 -5
- package/runtime/ext_gui/gui_engine.h +49 -0
- package/runtime/ext_gui/gui_events.cpp +80 -9
- package/runtime/ext_gui/listener_options.h +43 -0
- package/runtime/ext_gui/paint.cpp +28 -0
- package/runtime/ext_gui/paint.h +4 -0
- package/runtime/ext_gui/script.cpp +31 -0
- package/runtime/ext_gui/webgl_glsl.h +3 -0
- package/runtime/ext_gui/webgl_glsl_unroll.cpp +283 -0
- package/runtime/ext_gui/webgl_msl.cpp +4 -0
- package/runtime/ext_gui/webgl_shader.cpp +12 -6
- package/runtime/ext_gui/window.cpp +14 -65
- package/runtime/ext_gui/window_events.cpp +125 -0
- package/runtime/rt.h +29 -0
- package/runtime/rt_internal.h +4 -0
- package/runtime/xt_builtins.c +0 -21
- package/runtime/xt_containers/access.inc +94 -2
- package/runtime/xt_containers/objects.inc +29 -31
- package/runtime/xt_fetch/async.inc +60 -0
- package/runtime/xt_url_object.c +463 -0
- package/src/codegen/generator/state.ts +1 -0
- package/src/codegen/generator/tables.ts +1 -0
- package/src/driver/bundler/deferred.ts +351 -0
- package/src/driver/bundler/merge.ts +219 -2
- package/src/driver/runtime-objects.ts +1 -0
- package/src/driver/version.ts +1 -1
- package/src/extensions/gui/html.ts +91 -15
- package/src/extensions/gui/index.ts +13 -0
- package/src/extensions/gui/three.ts +4 -3
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
#include "gui_engine.h"
|
|
12
|
+
#include "listener_options.h"
|
|
12
13
|
|
|
13
14
|
#include <algorithm>
|
|
14
15
|
|
|
@@ -18,6 +19,12 @@
|
|
|
18
19
|
|
|
19
20
|
static xt_value g_window_proto = XT_UNDEFINED;
|
|
20
21
|
|
|
22
|
+
/** String value of a runtime value, as `std::string` ("" for null/undefined). */
|
|
23
|
+
static std::string window_string(xt_value value) {
|
|
24
|
+
const char *data = xt_string_data(xt_to_string(value));
|
|
25
|
+
return data != nullptr ? data : "";
|
|
26
|
+
}
|
|
27
|
+
|
|
21
28
|
/* -- methods -------------------------------------------------------------- */
|
|
22
29
|
|
|
23
30
|
static xt_value win_set_title(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
|
|
@@ -51,6 +58,9 @@ static xt_value win_load_html(xt_value self, xt_value env, int32_t argc, xt_valu
|
|
|
51
58
|
XtGuiWindow *win = xt_gui_window_from_this(self);
|
|
52
59
|
if (win != nullptr) {
|
|
53
60
|
win->html = html;
|
|
61
|
+
/* A fresh document starts at the top of the page. */
|
|
62
|
+
win->scroll_x = 0.0f;
|
|
63
|
+
win->scroll_y = 0.0f;
|
|
54
64
|
/* Fresh document: invalidate every previously handed-out handle, and drop
|
|
55
65
|
* the decoders that belonged to the old tree. */
|
|
56
66
|
xt_gui_handles_reset(win);
|
|
@@ -371,65 +381,6 @@ static xt_value win_cancel_animation_frame(xt_value self, xt_value env, int32_t
|
|
|
371
381
|
return XT_UNDEFINED;
|
|
372
382
|
}
|
|
373
383
|
|
|
374
|
-
static xt_value win_on(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
|
|
375
|
-
(void)env;
|
|
376
|
-
if (argc >= 2) {
|
|
377
|
-
xt_value name = xt_to_string(xt_arg(argc, argv, 0));
|
|
378
|
-
xt_value fn = xt_arg(argc, argv, 1);
|
|
379
|
-
if (XT_IS_FUNCTION(fn)) {
|
|
380
|
-
xt_value listeners = xt_object_get_cstr(self, "__xt_gui_events");
|
|
381
|
-
if (!XT_IS_OBJECT(listeners)) {
|
|
382
|
-
listeners = xt_object_new();
|
|
383
|
-
xt_object_set(self, xt_string_from_cstr("__xt_gui_events"), listeners);
|
|
384
|
-
}
|
|
385
|
-
xt_value array = xt_object_get(listeners, name);
|
|
386
|
-
if (!XT_IS_ARRAY(array)) {
|
|
387
|
-
array = xt_array_new(0, NULL);
|
|
388
|
-
xt_object_set(listeners, name, array);
|
|
389
|
-
}
|
|
390
|
-
xt_array_push(array, fn);
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
|
-
return self;
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
static xt_value win_off(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
|
|
397
|
-
(void)env;
|
|
398
|
-
if (argc >= 2) {
|
|
399
|
-
xt_value listeners = xt_object_get_cstr(self, "__xt_gui_events");
|
|
400
|
-
if (XT_IS_OBJECT(listeners)) {
|
|
401
|
-
xt_value name = xt_to_string(xt_arg(argc, argv, 0));
|
|
402
|
-
xt_value fn = xt_arg(argc, argv, 1);
|
|
403
|
-
xt_value array = xt_object_get(listeners, name);
|
|
404
|
-
if (XT_IS_ARRAY(array)) {
|
|
405
|
-
xt_value kept = xt_array_new(0, NULL);
|
|
406
|
-
int32_t count = (int32_t)xt_to_number(xt_array_length(array));
|
|
407
|
-
for (int32_t i = 0; i < count; i++) {
|
|
408
|
-
xt_value candidate = xt_array_get(array, xt_number((double)i));
|
|
409
|
-
if (candidate != fn) xt_array_push(kept, candidate);
|
|
410
|
-
}
|
|
411
|
-
xt_object_set(listeners, name, kept);
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
}
|
|
415
|
-
return self;
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
/** Web spelling of 'on': 'window.addEventListener(type, fn, options?)'. The
|
|
419
|
-
* optional 'options' argument ('capture'/'once') is accepted and ignored —
|
|
420
|
-
* window-level delivery has no capture phase — so a document written against
|
|
421
|
-
* the browser API compiles and behaves the same here. */
|
|
422
|
-
static xt_value win_add_event_listener(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
|
|
423
|
-
win_on(self, env, argc, argv);
|
|
424
|
-
return XT_UNDEFINED;
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
/** Web spelling of 'off': 'window.removeEventListener(type, fn, options?)'. */
|
|
428
|
-
static xt_value win_remove_event_listener(xt_value self, xt_value env, int32_t argc,
|
|
429
|
-
xt_value *argv) {
|
|
430
|
-
win_off(self, env, argc, argv);
|
|
431
|
-
return XT_UNDEFINED;
|
|
432
|
-
}
|
|
433
384
|
|
|
434
385
|
/* -- input diagnostics ---------------------------------------------------- */
|
|
435
386
|
|
|
@@ -570,12 +521,10 @@ xt_value xt_gui_window_proto(void) {
|
|
|
570
521
|
define_method(proto, "driver", (void *)win_driver);
|
|
571
522
|
define_method(proto, "requestAnimationFrame", (void *)win_request_animation_frame);
|
|
572
523
|
define_method(proto, "cancelAnimationFrame", (void *)win_cancel_animation_frame);
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
define_method(proto, "addEventListener", (void *)win_add_event_listener);
|
|
578
|
-
define_method(proto, "removeEventListener", (void *)win_remove_event_listener);
|
|
524
|
+
/* `on` / `off` and their Web spellings (`addEventListener` /
|
|
525
|
+
* `removeEventListener` with the listener options bag) live in
|
|
526
|
+
* `window_events.cpp`. */
|
|
527
|
+
xt_gui_install_window_events(proto);
|
|
579
528
|
define_method(proto, "hitTest", (void *)win_hit_test);
|
|
580
529
|
define_method(proto, "sendEvent", (void *)win_send_event);
|
|
581
530
|
define_method(proto, "stats", (void *)win_stats);
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* xbintsc GUI engine — window-level event listeners.
|
|
3
|
+
*
|
|
4
|
+
* `win.on` / `win.off` keep their legacy behaviour (an event name plus a
|
|
5
|
+
* callback, delivered with the input payload); `win.addEventListener` /
|
|
6
|
+
* `win.removeEventListener` take the Web options bag (`capture`, `once`,
|
|
7
|
+
* `passive`, `signal`). Both register in the window's single listener vector, so
|
|
8
|
+
* a listener added through one spelling can be removed through the other, and
|
|
9
|
+
* `gui_events.cpp` delivers one merged batch.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
#include "gui_engine.h"
|
|
13
|
+
#include "listener_options.h"
|
|
14
|
+
|
|
15
|
+
#include <algorithm>
|
|
16
|
+
#include <string>
|
|
17
|
+
#include <vector>
|
|
18
|
+
|
|
19
|
+
namespace {
|
|
20
|
+
|
|
21
|
+
std::string listener_string(xt_value value) {
|
|
22
|
+
const char *data = xt_string_data(xt_to_string(value));
|
|
23
|
+
return data != nullptr ? data : "";
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
void push_listener(XtGuiWindow *win, const std::string &type, xt_value fn, bool once, bool capture,
|
|
27
|
+
bool passive, xt_value signal) {
|
|
28
|
+
if (win == nullptr || !XT_IS_FUNCTION(fn)) return;
|
|
29
|
+
XtGuiWindowListener listener;
|
|
30
|
+
listener.type = type;
|
|
31
|
+
listener.fn = fn;
|
|
32
|
+
listener.once = once;
|
|
33
|
+
listener.capture = capture;
|
|
34
|
+
listener.passive = passive;
|
|
35
|
+
listener.signal = signal;
|
|
36
|
+
win->window_listeners.push_back(listener);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
xt_value win_on(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
|
|
40
|
+
(void)env;
|
|
41
|
+
if (argc >= 2) {
|
|
42
|
+
XtGuiWindow *win = xt_gui_window_from_this(self);
|
|
43
|
+
xt_value fn = xt_arg(argc, argv, 1);
|
|
44
|
+
if (win != nullptr && XT_IS_FUNCTION(fn)) {
|
|
45
|
+
push_listener(win, listener_string(xt_arg(argc, argv, 0)), fn, false, false, false,
|
|
46
|
+
XT_UNDEFINED);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return self;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
xt_value win_off(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
|
|
53
|
+
(void)env;
|
|
54
|
+
if (argc >= 2) {
|
|
55
|
+
XtGuiWindow *win = xt_gui_window_from_this(self);
|
|
56
|
+
if (win != nullptr) {
|
|
57
|
+
std::string type = listener_string(xt_arg(argc, argv, 0));
|
|
58
|
+
xt_value fn = xt_arg(argc, argv, 1);
|
|
59
|
+
std::vector<XtGuiWindowListener> &listeners = win->window_listeners;
|
|
60
|
+
listeners.erase(std::remove_if(listeners.begin(), listeners.end(),
|
|
61
|
+
[&](const XtGuiWindowListener &listener) {
|
|
62
|
+
return listener.type == type &&
|
|
63
|
+
xt_truthy(xt_seq(listener.fn, fn));
|
|
64
|
+
}),
|
|
65
|
+
listeners.end());
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return self;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Web spelling of `on`: `window.addEventListener(type, fn, options?)`. Window
|
|
72
|
+
* delivery has no capture phase, but `capture` is part of listener identity so
|
|
73
|
+
* `removeEventListener(type, fn, true)` still matches the right entry. */
|
|
74
|
+
xt_value win_add_event_listener(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
|
|
75
|
+
(void)env;
|
|
76
|
+
if (argc < 2) return XT_UNDEFINED;
|
|
77
|
+
XtGuiWindow *win = xt_gui_window_from_this(self);
|
|
78
|
+
std::string type = listener_string(xt_arg(argc, argv, 0));
|
|
79
|
+
xt_value fn = xt_arg(argc, argv, 1);
|
|
80
|
+
XtGuiListenerOptions options = xt_gui_listener_options(xt_arg(argc, argv, 2));
|
|
81
|
+
if (win == nullptr || !XT_IS_FUNCTION(fn) || xt_gui_signal_aborted(options.signal)) {
|
|
82
|
+
return XT_UNDEFINED;
|
|
83
|
+
}
|
|
84
|
+
/* The DOM ignores a second registration of the same (type, fn, capture). */
|
|
85
|
+
for (const XtGuiWindowListener &existing : win->window_listeners) {
|
|
86
|
+
if (existing.type == type && existing.capture == options.capture &&
|
|
87
|
+
xt_truthy(xt_seq(existing.fn, fn))) {
|
|
88
|
+
return XT_UNDEFINED;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
push_listener(win, type, fn, options.once, options.capture, options.passive, options.signal);
|
|
92
|
+
return XT_UNDEFINED;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** Web spelling of `off`: `window.removeEventListener(type, fn, options?)`. */
|
|
96
|
+
xt_value win_remove_event_listener(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
|
|
97
|
+
(void)env;
|
|
98
|
+
if (argc < 2) return XT_UNDEFINED;
|
|
99
|
+
XtGuiWindow *win = xt_gui_window_from_this(self);
|
|
100
|
+
if (win == nullptr) return XT_UNDEFINED;
|
|
101
|
+
std::string type = listener_string(xt_arg(argc, argv, 0));
|
|
102
|
+
xt_value fn = xt_arg(argc, argv, 1);
|
|
103
|
+
XtGuiListenerOptions options = xt_gui_listener_options(xt_arg(argc, argv, 2));
|
|
104
|
+
std::vector<XtGuiWindowListener> &listeners = win->window_listeners;
|
|
105
|
+
for (auto it = listeners.begin(); it != listeners.end(); ++it) {
|
|
106
|
+
if (it->type == type && it->capture == options.capture && xt_truthy(xt_seq(it->fn, fn))) {
|
|
107
|
+
listeners.erase(it);
|
|
108
|
+
return XT_UNDEFINED;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return XT_UNDEFINED;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
} // namespace
|
|
115
|
+
|
|
116
|
+
void xt_gui_install_window_events(xt_value proto) {
|
|
117
|
+
xt_object_set(proto, xt_string_from_cstr("on"),
|
|
118
|
+
xt_closure_new((void *)win_on, 0, nullptr));
|
|
119
|
+
xt_object_set(proto, xt_string_from_cstr("off"),
|
|
120
|
+
xt_closure_new((void *)win_off, 0, nullptr));
|
|
121
|
+
xt_object_set(proto, xt_string_from_cstr("addEventListener"),
|
|
122
|
+
xt_closure_new((void *)win_add_event_listener, 0, nullptr));
|
|
123
|
+
xt_object_set(proto, xt_string_from_cstr("removeEventListener"),
|
|
124
|
+
xt_closure_new((void *)win_remove_event_listener, 0, nullptr));
|
|
125
|
+
}
|
package/runtime/rt.h
CHANGED
|
@@ -185,6 +185,32 @@ xt_value xt_not(xt_value a);
|
|
|
185
185
|
/* -- objects -------------------------------------------------------------- */
|
|
186
186
|
xt_value xt_object_new(void);
|
|
187
187
|
xt_value xt_object_new_with_proto(xt_value proto);
|
|
188
|
+
|
|
189
|
+
/*
|
|
190
|
+
* Optional dynamic-property hooks.
|
|
191
|
+
*
|
|
192
|
+
* A host object that represents a *view* over external state — the GUI engine's
|
|
193
|
+
* `element.style`, `element.dataset` and `getComputedStyle()` — cannot know its
|
|
194
|
+
* property names when the prototype is built. Such an object installs these
|
|
195
|
+
* hooks; they are consulted only when ordinary property lookup/assignment finds
|
|
196
|
+
* nothing, so real properties and accessors keep precedence.
|
|
197
|
+
*
|
|
198
|
+
* `get` returns `XT_UNDEFINED` when it does not handle the key. `set`, `has`
|
|
199
|
+
* and `remove` return non-zero when they handled the operation; `keys` returns
|
|
200
|
+
* an array of the dynamically visible string keys (or `XT_UNDEFINED`).
|
|
201
|
+
* `data` is the host pointer handed to `xt_object_set_hooks`. Hooks must not
|
|
202
|
+
* store `xt_value`s outside a runtime object the collector can reach.
|
|
203
|
+
*/
|
|
204
|
+
typedef struct xt_object_hooks {
|
|
205
|
+
xt_value (*get)(xt_value self, xt_value key, void *data);
|
|
206
|
+
int (*set)(xt_value self, xt_value key, xt_value value, void *data);
|
|
207
|
+
int (*has)(xt_value self, xt_value key, void *data);
|
|
208
|
+
int (*remove)(xt_value self, xt_value key, void *data);
|
|
209
|
+
xt_value (*keys)(xt_value self, void *data);
|
|
210
|
+
} xt_object_hooks;
|
|
211
|
+
|
|
212
|
+
/** Install (or clear, with NULL) the dynamic-property hooks of `obj`. */
|
|
213
|
+
xt_value xt_object_set_hooks(xt_value obj, const xt_object_hooks *hooks, void *data);
|
|
188
214
|
/** The shared `Object.prototype` object (root of the ordinary object chain). */
|
|
189
215
|
xt_value xt_object_prototype(void);
|
|
190
216
|
/** The shared `Function.prototype` object. */
|
|
@@ -279,6 +305,9 @@ xt_value xt_date_static(xt_value name, int32_t argc, xt_value *argv);
|
|
|
279
305
|
xt_value xt_regexp_ctor(int32_t argc, xt_value *argv);
|
|
280
306
|
|
|
281
307
|
/* -- node:url ------------------------------------------------------------- */
|
|
308
|
+
xt_value xt_url_ctor(int32_t argc, xt_value *argv);
|
|
309
|
+
/** Resolve a `fetch` reference to a filesystem path (file: URLs and relative paths). */
|
|
310
|
+
int xt_url_file_path(const char *url, char *out, size_t size);
|
|
282
311
|
xt_value xt_url_path_to_file_url(int32_t argc, xt_value *argv);
|
|
283
312
|
xt_value xt_url_file_url_to_path(int32_t argc, xt_value *argv);
|
|
284
313
|
|
package/runtime/rt_internal.h
CHANGED
|
@@ -141,6 +141,10 @@ typedef struct {
|
|
|
141
141
|
* Deliberately not a property: `Object.keys`, `for...in` and JSON.stringify
|
|
142
142
|
* must not see it, while `error.stack` returns it through `xt_get`. */
|
|
143
143
|
xt_value error_stack;
|
|
144
|
+
/* Optional dynamic-property view (host objects such as `element.style`); see
|
|
145
|
+
* `xt_object_hooks` in `rt.h`. NULL for ordinary objects. */
|
|
146
|
+
const xt_object_hooks *hooks;
|
|
147
|
+
void *hook_data;
|
|
144
148
|
} xt_object;
|
|
145
149
|
|
|
146
150
|
typedef struct {
|
package/runtime/xt_builtins.c
CHANGED
|
@@ -406,27 +406,6 @@ xt_value xt_in(xt_value key, xt_value value) {
|
|
|
406
406
|
return XT_FALSE;
|
|
407
407
|
}
|
|
408
408
|
|
|
409
|
-
xt_value xt_delete(xt_value value, xt_value key) {
|
|
410
|
-
if (XT_IS_OBJECT(value)) {
|
|
411
|
-
xt_object *obj = (xt_object *)XT_GET_PTR(value);
|
|
412
|
-
xt_value k = xt_to_property_key(key);
|
|
413
|
-
for (uint32_t i = 0; i < obj->count; i++) {
|
|
414
|
-
if (xt_property_key_equals(obj->properties[i].key, k)) {
|
|
415
|
-
for (uint32_t j = i + 1; j < obj->count; j++) obj->properties[j - 1] = obj->properties[j];
|
|
416
|
-
obj->count--;
|
|
417
|
-
xt_object_shape_changed(obj);
|
|
418
|
-
break;
|
|
419
|
-
}
|
|
420
|
-
}
|
|
421
|
-
return XT_TRUE;
|
|
422
|
-
}
|
|
423
|
-
if (XT_IS_ARRAY(value)) {
|
|
424
|
-
if (!xt_is_symbol(key)) xt_array_set(value, key, XT_UNDEFINED);
|
|
425
|
-
return XT_TRUE;
|
|
426
|
-
}
|
|
427
|
-
return XT_TRUE;
|
|
428
|
-
}
|
|
429
|
-
|
|
430
409
|
xt_value xt_rest_args(int32_t argc, xt_value *argv, int32_t start) {
|
|
431
410
|
if (start < 0) start = 0;
|
|
432
411
|
int32_t count = argc - start;
|
|
@@ -105,8 +105,7 @@ static xt_value xt_array_iterator_method(void) {
|
|
|
105
105
|
return xt_array_iterator_method_value;
|
|
106
106
|
}
|
|
107
107
|
|
|
108
|
-
xt_value xt_get(xt_value target, xt_value key) {
|
|
109
|
-
if (target == XT_UNDEFINED || target == XT_NULL) xt_throw_read_of_nullish(target, key);
|
|
108
|
+
xt_value xt_get(xt_value target, xt_value key) { if (target == XT_UNDEFINED || target == XT_NULL) xt_throw_read_of_nullish(target, key);
|
|
110
109
|
if (XT_IS_ARRAY(target)) {
|
|
111
110
|
/* `arr.length` must read the array's length, never an element. */
|
|
112
111
|
if (XT_IS_STRING(key)) {
|
|
@@ -289,6 +288,8 @@ xt_value xt_get_ic(xt_value target, xt_value key, int64_t *cache) {
|
|
|
289
288
|
if (obj->header.kind != XT_OBJECT_KIND_OBJECT && obj->header.kind != XT_OBJECT_KIND_ERROR) {
|
|
290
289
|
return xt_get(target, key);
|
|
291
290
|
}
|
|
291
|
+
/* A host view answers keys dynamically, outside the shape cache. */
|
|
292
|
+
if (obj->hooks != NULL) return xt_get(target, key);
|
|
292
293
|
if (!XT_IS_STRING(key) && !xt_is_symbol(key)) return xt_get(target, key);
|
|
293
294
|
xt_shape *shape = obj->shape;
|
|
294
295
|
uintptr_t cachedShape = (uintptr_t)cache[0];
|
|
@@ -387,3 +388,94 @@ xt_value xt_set(xt_value target, xt_value key, xt_value value) {
|
|
|
387
388
|
}
|
|
388
389
|
return value;
|
|
389
390
|
}
|
|
391
|
+
|
|
392
|
+
/* ------------------------------------------------------------------------- */
|
|
393
|
+
/* Dynamic host-view properties */
|
|
394
|
+
/* ------------------------------------------------------------------------- */
|
|
395
|
+
|
|
396
|
+
xt_value xt_object_set_hooks(xt_value value, const xt_object_hooks *hooks, void *data) {
|
|
397
|
+
xt_object *obj = xt_as_object(value);
|
|
398
|
+
if (!obj) return value;
|
|
399
|
+
if (obj->header.kind != XT_OBJECT_KIND_OBJECT && obj->header.kind != XT_OBJECT_KIND_ERROR) {
|
|
400
|
+
return value;
|
|
401
|
+
}
|
|
402
|
+
obj->hooks = hooks;
|
|
403
|
+
obj->hook_data = data;
|
|
404
|
+
return value;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
void xt_object_append_dynamic_keys(xt_object *obj, xt_value value, xt_value result) {
|
|
408
|
+
if (obj->hooks == NULL || obj->hooks->keys == NULL) return;
|
|
409
|
+
xt_value dynamic = obj->hooks->keys(value, obj->hook_data);
|
|
410
|
+
xt_array *list = xt_as_array(dynamic);
|
|
411
|
+
if (list == NULL) return;
|
|
412
|
+
for (uint32_t i = 0; i < list->length; i++) {
|
|
413
|
+
xt_value key = list->items[i];
|
|
414
|
+
if (!XT_IS_STRING(key)) continue;
|
|
415
|
+
if (xt_object_find(obj, key) != NULL) continue;
|
|
416
|
+
xt_array_push(result, key);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
/* `delete obj[key]`: remove a real property (arrays clear the element) and let
|
|
420
|
+
* a host view delete its dynamic key. */
|
|
421
|
+
xt_value xt_delete(xt_value value, xt_value key) {
|
|
422
|
+
if (XT_IS_OBJECT(value)) {
|
|
423
|
+
xt_object *obj = (xt_object *)XT_GET_PTR(value);
|
|
424
|
+
xt_value k = xt_to_property_key(key);
|
|
425
|
+
for (uint32_t i = 0; i < obj->count; i++) {
|
|
426
|
+
if (xt_property_key_equals(obj->properties[i].key, k)) {
|
|
427
|
+
for (uint32_t j = i + 1; j < obj->count; j++) obj->properties[j - 1] = obj->properties[j];
|
|
428
|
+
obj->count--;
|
|
429
|
+
xt_object_shape_changed(obj);
|
|
430
|
+
break;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
/* A host view (`element.style`, `element.dataset`) owns no real property for
|
|
434
|
+
* its keys, so `delete` must reach it even when the bag stayed empty. */
|
|
435
|
+
if (obj->hooks != NULL && obj->hooks->remove != NULL) {
|
|
436
|
+
obj->hooks->remove(value, k, obj->hook_data);
|
|
437
|
+
}
|
|
438
|
+
return XT_TRUE;
|
|
439
|
+
}
|
|
440
|
+
if (XT_IS_ARRAY(value)) {
|
|
441
|
+
if (!xt_is_symbol(key)) xt_array_set(value, key, XT_UNDEFINED);
|
|
442
|
+
return XT_TRUE;
|
|
443
|
+
}
|
|
444
|
+
return XT_TRUE;
|
|
445
|
+
}
|
|
446
|
+
xt_value xt_object_has_own(xt_value value, xt_value key) {
|
|
447
|
+
if (XT_IS_ARRAY(value)) {
|
|
448
|
+
xt_array *array = (xt_array *)XT_GET_PTR(value);
|
|
449
|
+
xt_value propertyKey = xt_to_property_key(key);
|
|
450
|
+
if (XT_IS_STRING(propertyKey)) {
|
|
451
|
+
xt_string *k = xt_as_string(propertyKey);
|
|
452
|
+
if (k->length == 6 && memcmp(xt_string_bytes(k), "length", 6) == 0) return XT_TRUE;
|
|
453
|
+
uint32_t index = 0;
|
|
454
|
+
if (xt_key_is_array_index(k, &index)) return xt_bool(index < array->length);
|
|
455
|
+
}
|
|
456
|
+
return xt_bool(XT_IS_OBJECT(array->extra) &&
|
|
457
|
+
xt_object_find((xt_object *)XT_GET_PTR(array->extra), propertyKey));
|
|
458
|
+
}
|
|
459
|
+
if (XT_IS_STRING(value)) {
|
|
460
|
+
xt_string *string = xt_as_string(value);
|
|
461
|
+
if (xt_is_symbol(key)) return XT_FALSE;
|
|
462
|
+
xt_string *k = xt_as_string(xt_to_string(key));
|
|
463
|
+
if (k->length == 6 && memcmp(xt_string_bytes(k), "length", 6) == 0) return XT_TRUE;
|
|
464
|
+
uint32_t index = 0;
|
|
465
|
+
return xt_bool(xt_key_is_array_index(k, &index) && index < xt_string_utf16_length(string));
|
|
466
|
+
}
|
|
467
|
+
xt_object *obj = xt_as_object(value);
|
|
468
|
+
if (!obj) return XT_FALSE;
|
|
469
|
+
/* Map/Set/Date/RegExp/Promise/Symbol use their own struct layout and have no
|
|
470
|
+
* property bag; interpreting them as `xt_object` would read past the end. */
|
|
471
|
+
if (obj->header.kind != XT_OBJECT_KIND_OBJECT && obj->header.kind != XT_OBJECT_KIND_ERROR) {
|
|
472
|
+
return XT_FALSE;
|
|
473
|
+
}
|
|
474
|
+
xt_value propertyKey = xt_to_property_key(key);
|
|
475
|
+
if (xt_object_find(obj, propertyKey) != NULL) return XT_TRUE;
|
|
476
|
+
if (obj->hooks != NULL && obj->hooks->has != NULL &&
|
|
477
|
+
obj->hooks->has(value, propertyKey, obj->hook_data)) {
|
|
478
|
+
return XT_TRUE;
|
|
479
|
+
}
|
|
480
|
+
return XT_FALSE;
|
|
481
|
+
}
|
|
@@ -170,6 +170,9 @@ xt_value xt_object_new_with_proto(xt_value proto) {
|
|
|
170
170
|
obj->prototype = proto;
|
|
171
171
|
obj->frozen = 0;
|
|
172
172
|
obj->is_array_base = 0;
|
|
173
|
+
obj->error_stack = XT_UNDEFINED;
|
|
174
|
+
obj->hooks = NULL;
|
|
175
|
+
obj->hook_data = NULL;
|
|
173
176
|
return XT_FROM_PTR(XT_TAG_OBJECT, obj);
|
|
174
177
|
}
|
|
175
178
|
|
|
@@ -191,6 +194,10 @@ static xt_property *xt_object_find(xt_object *obj, xt_value key) {
|
|
|
191
194
|
return NULL;
|
|
192
195
|
}
|
|
193
196
|
|
|
197
|
+
/* Defined in `access.inc`: append the dynamic keys of a host view whose
|
|
198
|
+
* `xt_object_hooks::keys` returns them. */
|
|
199
|
+
static void xt_object_append_dynamic_keys(xt_object *obj, xt_value value, xt_value result);
|
|
200
|
+
|
|
194
201
|
/*
|
|
195
202
|
* JavaScript enumerates own string keys in a canonical order: array-index
|
|
196
203
|
* keys (the canonical decimal form of an integer in [0, 2^32 - 2]) first, in
|
|
@@ -313,6 +320,13 @@ xt_value xt_object_get(xt_value value, xt_value key) {
|
|
|
313
320
|
if (!XT_IS_OBJECT(cur->prototype)) break;
|
|
314
321
|
cur = (xt_object *)XT_GET_PTR(cur->prototype);
|
|
315
322
|
}
|
|
323
|
+
/* A host view (`element.style`, `element.dataset`, computed style) answers the
|
|
324
|
+
* keys it owns dynamically. Only a miss reaches this point, so prototype
|
|
325
|
+
* methods and real properties win. */
|
|
326
|
+
if (obj->hooks != NULL && obj->hooks->get != NULL) {
|
|
327
|
+
xt_value dynamic = obj->hooks->get(value, k, obj->hook_data);
|
|
328
|
+
if (!XT_IS_UNDEFINED(dynamic)) return dynamic;
|
|
329
|
+
}
|
|
316
330
|
return XT_UNDEFINED;
|
|
317
331
|
}
|
|
318
332
|
|
|
@@ -368,6 +382,11 @@ xt_value xt_object_set(xt_value value, xt_value key, xt_value newValue) {
|
|
|
368
382
|
}
|
|
369
383
|
ancestor = xt_object_prototype_of(ancestor->prototype, ancestor);
|
|
370
384
|
}
|
|
385
|
+
/* A host view handles the assignment itself (e.g. `style.width = "50%"` or
|
|
386
|
+
* `dataset.foo = "1"`); it never stores a shadowing data property. */
|
|
387
|
+
if (obj->hooks != NULL && obj->hooks->set != NULL) {
|
|
388
|
+
if (obj->hooks->set(value, k, newValue, obj->hook_data)) return newValue;
|
|
389
|
+
}
|
|
371
390
|
xt_property *inserted = xt_object_insert_property(obj, k);
|
|
372
391
|
inserted->value = newValue;
|
|
373
392
|
return newValue;
|
|
@@ -446,36 +465,6 @@ int xt_object_is_frozen(xt_value value) {
|
|
|
446
465
|
return obj ? obj->frozen : 0;
|
|
447
466
|
}
|
|
448
467
|
|
|
449
|
-
xt_value xt_object_has_own(xt_value value, xt_value key) {
|
|
450
|
-
if (XT_IS_ARRAY(value)) {
|
|
451
|
-
xt_array *array = (xt_array *)XT_GET_PTR(value);
|
|
452
|
-
xt_value propertyKey = xt_to_property_key(key);
|
|
453
|
-
if (XT_IS_STRING(propertyKey)) {
|
|
454
|
-
xt_string *k = xt_as_string(propertyKey);
|
|
455
|
-
if (k->length == 6 && memcmp(xt_string_bytes(k), "length", 6) == 0) return XT_TRUE;
|
|
456
|
-
uint32_t index = 0;
|
|
457
|
-
if (xt_key_is_array_index(k, &index)) return xt_bool(index < array->length);
|
|
458
|
-
}
|
|
459
|
-
return xt_bool(XT_IS_OBJECT(array->extra) &&
|
|
460
|
-
xt_object_find((xt_object *)XT_GET_PTR(array->extra), propertyKey));
|
|
461
|
-
}
|
|
462
|
-
if (XT_IS_STRING(value)) {
|
|
463
|
-
xt_string *string = xt_as_string(value);
|
|
464
|
-
if (xt_is_symbol(key)) return XT_FALSE;
|
|
465
|
-
xt_string *k = xt_as_string(xt_to_string(key));
|
|
466
|
-
if (k->length == 6 && memcmp(xt_string_bytes(k), "length", 6) == 0) return XT_TRUE;
|
|
467
|
-
uint32_t index = 0;
|
|
468
|
-
return xt_bool(xt_key_is_array_index(k, &index) && index < xt_string_utf16_length(string));
|
|
469
|
-
}
|
|
470
|
-
xt_object *obj = xt_as_object(value);
|
|
471
|
-
if (!obj) return XT_FALSE;
|
|
472
|
-
/* Map/Set/Date/RegExp/Promise/Symbol use their own struct layout and have no
|
|
473
|
-
* property bag; interpreting them as `xt_object` would read past the end. */
|
|
474
|
-
if (obj->header.kind != XT_OBJECT_KIND_OBJECT && obj->header.kind != XT_OBJECT_KIND_ERROR) {
|
|
475
|
-
return XT_FALSE;
|
|
476
|
-
}
|
|
477
|
-
return xt_bool(xt_object_find(obj, xt_to_property_key(key)) != NULL);
|
|
478
|
-
}
|
|
479
468
|
|
|
480
469
|
xt_value xt_object_from_entries(xt_value entries) {
|
|
481
470
|
xt_value result = xt_object_new();
|
|
@@ -495,7 +484,13 @@ xt_value xt_object_has(xt_value value, xt_value key) {
|
|
|
495
484
|
if (obj->header.kind != XT_OBJECT_KIND_OBJECT && obj->header.kind != XT_OBJECT_KIND_ERROR) {
|
|
496
485
|
return XT_FALSE;
|
|
497
486
|
}
|
|
498
|
-
|
|
487
|
+
xt_value propertyKey = xt_to_property_key(key);
|
|
488
|
+
if (xt_object_find(obj, propertyKey) != NULL) return XT_TRUE;
|
|
489
|
+
if (obj->hooks != NULL && obj->hooks->has != NULL &&
|
|
490
|
+
obj->hooks->has(value, propertyKey, obj->hook_data)) {
|
|
491
|
+
return XT_TRUE;
|
|
492
|
+
}
|
|
493
|
+
return XT_FALSE;
|
|
499
494
|
}
|
|
500
495
|
|
|
501
496
|
/* `Object.getOwnPropertySymbols`: the symbol-keyed own properties, in
|
|
@@ -531,6 +526,9 @@ xt_value xt_object_keys(xt_value value) {
|
|
|
531
526
|
for (uint32_t i = 0; i < obj->count; i++) {
|
|
532
527
|
if (XT_IS_STRING(obj->properties[i].key)) xt_array_push(result, obj->properties[i].key);
|
|
533
528
|
}
|
|
529
|
+
/* Dynamic keys of a host view (`element.dataset`, ...), appended without
|
|
530
|
+
* repeating a real property that was just pushed. */
|
|
531
|
+
xt_object_append_dynamic_keys(obj, value, result);
|
|
534
532
|
return result;
|
|
535
533
|
}
|
|
536
534
|
if (XT_IS_ARRAY(value)) {
|
|
@@ -201,6 +201,66 @@ static void xt_fetch_hop_complete(xt_async_job *job) {
|
|
|
201
201
|
|
|
202
202
|
/* Main thread: validate the current URL and queue one exchange. */
|
|
203
203
|
static void xt_fetch_start(xt_fetch_state *state) {
|
|
204
|
+
/* `file:` URLs and references with no scheme (a relative path) read a local
|
|
205
|
+
* file. This is what lets a compiled page load an asset from disk without an
|
|
206
|
+
* HTTP server; the response is built on this thread because a local read is
|
|
207
|
+
* already complete. */
|
|
208
|
+
char path[4096];
|
|
209
|
+
if (xt_url_file_path(state->url, path, sizeof(path))) {
|
|
210
|
+
if (strcmp(state->method, "GET") != 0 && strcmp(state->method, "HEAD") != 0) {
|
|
211
|
+
xt_fetch_state_reject(state, "fetch failed: file URLs only support GET or HEAD");
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
FILE *file = fopen(path, "rb");
|
|
215
|
+
if (!file) {
|
|
216
|
+
xt_fetch_state_reject(state, "fetch failed: cannot read file");
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
size_t capacity = 0;
|
|
220
|
+
size_t length = 0;
|
|
221
|
+
char *body = NULL;
|
|
222
|
+
char chunk[16384];
|
|
223
|
+
for (;;) {
|
|
224
|
+
size_t count = fread(chunk, 1, sizeof(chunk), file);
|
|
225
|
+
if (count > 0) {
|
|
226
|
+
if (length + count + 1 > capacity) {
|
|
227
|
+
capacity = (length + count + 1) * 2;
|
|
228
|
+
char *grown = (char *)realloc(body, capacity);
|
|
229
|
+
if (!grown) {
|
|
230
|
+
free(body);
|
|
231
|
+
fclose(file);
|
|
232
|
+
xt_fetch_state_reject(state, "fetch failed");
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
body = grown;
|
|
236
|
+
}
|
|
237
|
+
memcpy(body + length, chunk, count);
|
|
238
|
+
length += count;
|
|
239
|
+
}
|
|
240
|
+
if (count < sizeof(chunk)) break;
|
|
241
|
+
}
|
|
242
|
+
fclose(file);
|
|
243
|
+
|
|
244
|
+
char href[4300];
|
|
245
|
+
if (path[0] == '/') {
|
|
246
|
+
snprintf(href, sizeof(href), "file://%s", path);
|
|
247
|
+
} else {
|
|
248
|
+
snprintf(href, sizeof(href), "file:///%s", path);
|
|
249
|
+
}
|
|
250
|
+
xt_value headers = xt_headers_new();
|
|
251
|
+
char lengthText[32];
|
|
252
|
+
snprintf(lengthText, sizeof(lengthText), "%zu", length);
|
|
253
|
+
xt_headers_set_raw(headers, "content-length", lengthText);
|
|
254
|
+
xt_headers_set_raw(headers, "content-type", "application/octet-stream");
|
|
255
|
+
size_t bodyLength = strcmp(state->method, "HEAD") == 0 ? 0 : length;
|
|
256
|
+
xt_value response = xt_response_new(headers, 200, "OK", href, body, bodyLength);
|
|
257
|
+
free(body);
|
|
258
|
+
xt_value promise = state->promise;
|
|
259
|
+
xt_fetch_state_free(state);
|
|
260
|
+
xt_promise_fulfill(promise, response);
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
|
|
204
264
|
xt_fetch_url parsed;
|
|
205
265
|
if (xt_fetch_parse_url(state->url, &parsed) != 0) {
|
|
206
266
|
xt_fetch_state_reject(state, "Failed to parse URL");
|