xbintsc 0.7.0 → 0.7.6
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/ast/common.d.ts +10 -0
- package/dist/src/checker/source.js +4 -0
- package/dist/src/checker/source.js.map +1 -1
- package/dist/src/cli/help.d.ts +1 -1
- package/dist/src/cli/main.js +15 -3
- package/dist/src/cli/main.js.map +1 -1
- package/dist/src/codegen/generator/calls/access.js +4 -11
- package/dist/src/codegen/generator/calls/access.js.map +1 -1
- package/dist/src/codegen/generator/calls/element-inline.d.ts +42 -0
- package/dist/src/codegen/generator/calls/element-inline.js +388 -0
- package/dist/src/codegen/generator/calls/element-inline.js.map +1 -0
- package/dist/src/codegen/generator/calls/invocation.js +82 -1
- package/dist/src/codegen/generator/calls/invocation.js.map +1 -1
- package/dist/src/codegen/generator/calls.d.ts +3 -1
- package/dist/src/codegen/generator/calls.js +3 -1
- package/dist/src/codegen/generator/calls.js.map +1 -1
- package/dist/src/codegen/generator/debug.js +20 -3
- package/dist/src/codegen/generator/debug.js.map +1 -1
- package/dist/src/codegen/generator/expressions/assignment.js +10 -2
- package/dist/src/codegen/generator/expressions/assignment.js.map +1 -1
- package/dist/src/codegen/generator/expressions/global-values.d.ts +19 -0
- package/dist/src/codegen/generator/expressions/global-values.js +49 -0
- package/dist/src/codegen/generator/expressions/global-values.js.map +1 -0
- package/dist/src/codegen/generator/expressions/primary.d.ts +0 -2
- package/dist/src/codegen/generator/expressions/primary.js +4 -24
- package/dist/src/codegen/generator/expressions/primary.js.map +1 -1
- package/dist/src/codegen/generator/expressions.d.ts +2 -1
- package/dist/src/codegen/generator/expressions.js +2 -1
- package/dist/src/codegen/generator/expressions.js.map +1 -1
- package/dist/src/codegen/generator/layout.d.ts +65 -0
- package/dist/src/codegen/generator/layout.js +50 -0
- package/dist/src/codegen/generator/layout.js.map +1 -0
- package/dist/src/codegen/generator/state.js +4 -0
- package/dist/src/codegen/generator/state.js.map +1 -1
- package/dist/src/diagnostics/diagnostic.js +1 -1
- package/dist/src/diagnostics/diagnostic.js.map +1 -1
- package/dist/src/diagnostics/source-text.d.ts +37 -9
- package/dist/src/diagnostics/source-text.js +124 -23
- package/dist/src/diagnostics/source-text.js.map +1 -1
- package/dist/src/diagnostics/source.d.ts +16 -6
- package/dist/src/diagnostics/source.js +25 -4
- package/dist/src/diagnostics/source.js.map +1 -1
- package/dist/src/driver/build-key.d.ts +14 -0
- package/dist/src/driver/build-key.js +6 -1
- package/dist/src/driver/build-key.js.map +1 -1
- package/dist/src/driver/bundler/concatenate.js +11 -4
- package/dist/src/driver/bundler/concatenate.js.map +1 -1
- package/dist/src/driver/bundler/graph.js +2 -2
- package/dist/src/driver/bundler/graph.js.map +1 -1
- package/dist/src/driver/bundler/merge.js +11 -5
- package/dist/src/driver/bundler/merge.js.map +1 -1
- package/dist/src/driver/bundler/reexport-resolution.js +3 -0
- package/dist/src/driver/bundler/reexport-resolution.js.map +1 -1
- package/dist/src/driver/compiler.js +13 -3
- package/dist/src/driver/compiler.js.map +1 -1
- package/dist/src/driver/frontend.js +3 -4
- package/dist/src/driver/frontend.js.map +1 -1
- package/dist/src/driver/runtime-objects.d.ts +1 -1
- package/dist/src/driver/runtime-objects.js +4 -0
- package/dist/src/driver/runtime-objects.js.map +1 -1
- package/dist/src/driver/toolchain.js +28 -5
- package/dist/src/driver/toolchain.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 +42 -10
- package/dist/src/extensions/gui/html.js.map +1 -1
- package/dist/src/extensions/gui/index.js +48 -0
- package/dist/src/extensions/gui/index.js.map +1 -1
- package/dist/src/extensions/registry.d.ts +20 -0
- package/dist/src/extensions/registry.js.map +1 -1
- package/dist/src/parser/expressions.js +2 -2
- package/dist/src/parser/expressions.js.map +1 -1
- package/dist/src/parser/helpers.d.ts +8 -0
- package/dist/src/parser/helpers.js +21 -0
- package/dist/src/parser/helpers.js.map +1 -1
- package/dist/tests/cli/main.test.js +9 -0
- package/dist/tests/cli/main.test.js.map +1 -1
- package/dist/tests/codegen/element-layout.test.d.ts +19 -0
- package/dist/tests/codegen/element-layout.test.js +80 -0
- package/dist/tests/codegen/element-layout.test.js.map +1 -0
- package/dist/tests/codegen/expressions.test.js +25 -0
- package/dist/tests/codegen/expressions.test.js.map +1 -1
- package/dist/tests/codegen/llvm.test.js +13 -0
- package/dist/tests/codegen/llvm.test.js.map +1 -1
- package/dist/tests/diagnostics/diagnostic.test.js +9 -0
- package/dist/tests/diagnostics/diagnostic.test.js.map +1 -1
- package/dist/tests/diagnostics/source-text.test.d.ts +1 -0
- package/dist/tests/diagnostics/source-text.test.js +50 -0
- package/dist/tests/diagnostics/source-text.test.js.map +1 -0
- package/dist/tests/diagnostics/source.test.js +17 -0
- package/dist/tests/diagnostics/source.test.js.map +1 -1
- package/dist/tests/driver/build-key.test.d.ts +1 -0
- package/dist/tests/driver/build-key.test.js +49 -0
- package/dist/tests/driver/build-key.test.js.map +1 -0
- package/dist/tests/driver/toolchain.test.js +8 -1
- package/dist/tests/driver/toolchain.test.js.map +1 -1
- package/dist/tests/driver/usage.test.js +2 -1
- package/dist/tests/driver/usage.test.js.map +1 -1
- package/dist/tests/e2e/async-await.test.js +16 -0
- package/dist/tests/e2e/async-await.test.js.map +1 -1
- package/dist/tests/e2e/compile-builtins.test.js +29 -0
- package/dist/tests/e2e/compile-builtins.test.js.map +1 -1
- package/dist/tests/e2e/compile-modules.test.js +13 -0
- package/dist/tests/e2e/compile-modules.test.js.map +1 -1
- package/dist/tests/e2e/compile-objects-classes.test.js.map +1 -1
- package/dist/tests/e2e/compile-prototype-chain.test.d.ts +8 -0
- package/dist/tests/e2e/compile-prototype-chain.test.js +24 -0
- package/dist/tests/e2e/compile-prototype-chain.test.js.map +1 -0
- package/dist/tests/e2e/differential-language.test.js +25 -0
- package/dist/tests/e2e/differential-language.test.js.map +1 -1
- package/dist/tests/e2e/differential-values.test.js +59 -0
- package/dist/tests/e2e/differential-values.test.js.map +1 -1
- package/dist/tests/e2e/gc.test.js +22 -0
- package/dist/tests/e2e/gc.test.js.map +1 -1
- package/dist/tests/e2e/gui-audio-extra.test.d.ts +12 -0
- package/dist/tests/e2e/gui-audio-extra.test.js +185 -0
- package/dist/tests/e2e/gui-audio-extra.test.js.map +1 -0
- package/dist/tests/e2e/gui-browser.test.js +35 -2
- package/dist/tests/e2e/gui-browser.test.js.map +1 -1
- package/dist/tests/e2e/gui-dom.test.js +88 -0
- package/dist/tests/e2e/gui-dom.test.js.map +1 -1
- package/dist/tests/e2e/gui-webgl-declarations.test.d.ts +13 -0
- package/dist/tests/e2e/gui-webgl-declarations.test.js +136 -0
- package/dist/tests/e2e/gui-webgl-declarations.test.js.map +1 -0
- package/dist/tests/e2e/gui-webgl-texture-units.test.d.ts +12 -0
- package/dist/tests/e2e/gui-webgl-texture-units.test.js +86 -0
- package/dist/tests/e2e/gui-webgl-texture-units.test.js.map +1 -0
- package/dist/tests/e2e/gui-webgl2-blit.test.js +6 -2
- package/dist/tests/e2e/gui-webgl2-blit.test.js.map +1 -1
- package/dist/tests/e2e/gui-webgl2-msaa-float.test.d.ts +12 -0
- package/dist/tests/e2e/gui-webgl2-msaa-float.test.js +157 -0
- package/dist/tests/e2e/gui-webgl2-msaa-float.test.js.map +1 -0
- package/dist/tests/e2e/gui-webgl2-msaa.test.d.ts +10 -0
- package/dist/tests/e2e/gui-webgl2-msaa.test.js +344 -0
- package/dist/tests/e2e/gui-webgl2-msaa.test.js.map +1 -0
- package/dist/tests/e2e/gui-webgl2-target-orientation.test.d.ts +15 -0
- package/dist/tests/e2e/gui-webgl2-target-orientation.test.js +287 -0
- package/dist/tests/e2e/gui-webgl2-target-orientation.test.js.map +1 -0
- package/dist/tests/e2e/gui-webgl2.test.js +59 -1
- package/dist/tests/e2e/gui-webgl2.test.js.map +1 -1
- package/dist/tests/e2e/hang-watchdog.test.d.ts +10 -0
- package/dist/tests/e2e/hang-watchdog.test.js +50 -0
- package/dist/tests/e2e/hang-watchdog.test.js.map +1 -0
- package/dist/tests/e2e/method-call-cache.test.d.ts +5 -0
- package/dist/tests/e2e/method-call-cache.test.js +65 -0
- package/dist/tests/e2e/method-call-cache.test.js.map +1 -0
- package/dist/tests/e2e/performance-now.test.d.ts +9 -0
- package/dist/tests/e2e/performance-now.test.js +36 -0
- package/dist/tests/e2e/performance-now.test.js.map +1 -0
- package/dist/tests/e2e/property-ic.test.js +33 -0
- package/dist/tests/e2e/property-ic.test.js.map +1 -1
- package/dist/tests/e2e/shape-interning.test.d.ts +16 -0
- package/dist/tests/e2e/shape-interning.test.js +55 -0
- package/dist/tests/e2e/shape-interning.test.js.map +1 -0
- package/dist/tests/e2e/string-utf16.test.js +9 -0
- package/dist/tests/e2e/string-utf16.test.js.map +1 -1
- package/dist/tests/e2e/typed-array-views.test.js +26 -0
- package/dist/tests/e2e/typed-array-views.test.js.map +1 -1
- package/dist/tests/e2e/url-search-params.test.js +19 -0
- package/dist/tests/e2e/url-search-params.test.js.map +1 -1
- package/dist/tests/extensions/gui.test.js +16 -1
- package/dist/tests/extensions/gui.test.js.map +1 -1
- package/dist/tests/helpers.js +1 -0
- package/dist/tests/helpers.js.map +1 -1
- package/dist/tests/parser/expressions.test.js +36 -0
- package/dist/tests/parser/expressions.test.js.map +1 -1
- package/doc/ai/contributing.md +8 -5
- package/doc/ai/extensions.md +12 -3
- package/doc/ai/language-support.md +2 -1
- package/doc/ai/troubleshooting.md +14 -0
- package/doc/ai/zh-CN/contributing.md +6 -3
- package/doc/ai/zh-CN/extensions.md +10 -2
- package/doc/ai/zh-CN/language-support.md +2 -1
- package/doc/ai/zh-CN/troubleshooting.md +12 -0
- package/doc/gui/api.md +26 -4
- package/doc/gui/dom.md +8 -0
- package/doc/gui/known-issues.md +35 -6
- package/doc/gui/runtime.md +16 -1
- package/doc/gui/webgl.md +97 -26
- package/doc/gui-scripts.md +2 -1
- package/doc/gui.md +8 -5
- package/doc/implemented/checker.md +9 -0
- package/doc/implemented/codegen.md +7 -0
- package/doc/implemented/parser.md +2 -1
- package/doc/implemented/runtime-core.md +9 -2
- package/doc/implemented/stdlib.md +10 -2
- package/doc/implemented/typed-arrays.md +18 -2
- package/doc/zh-CN/gui/api.md +19 -5
- package/doc/zh-CN/gui/dom.md +6 -0
- package/doc/zh-CN/gui/known-issues.md +24 -5
- package/doc/zh-CN/gui/runtime.md +13 -1
- package/doc/zh-CN/gui/webgl.md +20 -7
- package/doc/zh-CN/gui-scripts.md +2 -1
- package/doc/zh-CN/gui.md +4 -1
- package/doc/zh-CN/implemented/checker.md +7 -0
- package/doc/zh-CN/implemented/codegen.md +7 -0
- package/doc/zh-CN/implemented/parser.md +2 -1
- package/doc/zh-CN/implemented/runtime-core.md +9 -2
- package/doc/zh-CN/implemented/stdlib.md +6 -2
- package/doc/zh-CN/implemented/typed-arrays.md +14 -2
- package/package.json +7 -7
- package/runtime/ext_gui/audio.cpp +39 -3
- package/runtime/ext_gui/audio_extra.cpp +353 -0
- package/runtime/ext_gui/audio_extra.h +47 -0
- package/runtime/ext_gui/audio_fft.h +76 -0
- package/runtime/ext_gui/audio_internal.h +21 -0
- package/runtime/ext_gui/audio_mixer.cpp +46 -1
- package/runtime/ext_gui/audio_mixer.h +89 -1
- package/runtime/ext_gui/audio_mixer_extra.cpp +126 -0
- package/runtime/ext_gui/audio_nodes.cpp +242 -0
- package/runtime/ext_gui/audio_nodes_internal.h +81 -0
- package/runtime/ext_gui/audio_nodes_spatial.cpp +375 -0
- package/runtime/ext_gui/browser.cpp +9 -0
- package/runtime/ext_gui/browser_screen.cpp +73 -0
- package/runtime/ext_gui/dom_api_canvas.cpp +9 -1
- package/runtime/ext_gui/dom_api_internal.h +3 -0
- package/runtime/ext_gui/dom_api_node.cpp +21 -0
- package/runtime/ext_gui/dom_api_node_read.cpp +29 -0
- package/runtime/ext_gui/dom_api_proto.cpp +3 -0
- package/runtime/ext_gui/dom_api_webgl2.cpp +16 -11
- package/runtime/ext_gui/dom_api_webgl2_msaa.cpp +64 -0
- package/runtime/ext_gui/dom_api_webgl_compressed.cpp +2 -2
- package/runtime/ext_gui/dom_api_webgl_framebuffer.cpp +68 -11
- package/runtime/ext_gui/dom_api_webgl_methods.h +3 -0
- package/runtime/ext_gui/dom_api_webgl_state.cpp +4 -2
- package/runtime/ext_gui/dom_api_webgl_texture.cpp +6 -6
- package/runtime/ext_gui/gui.cpp +6 -1
- package/runtime/ext_gui/gui_engine.h +2 -0
- package/runtime/ext_gui/gui_events.cpp +13 -8
- package/runtime/ext_gui/gui_webgl.cpp +3 -2
- package/runtime/ext_gui/style_ua.cpp +4 -0
- package/runtime/ext_gui/warnings.cpp +1 -1
- package/runtime/ext_gui/webgl.h +102 -7
- package/runtime/ext_gui/webgl_api.h +3 -2
- package/runtime/ext_gui/webgl_blit.cpp +109 -15
- package/runtime/ext_gui/webgl_blit_color.cpp +264 -0
- package/runtime/ext_gui/webgl_blit_depth.cpp +26 -8
- package/runtime/ext_gui/webgl_buffer.cpp +20 -170
- package/runtime/ext_gui/webgl_context.cpp +117 -169
- package/runtime/ext_gui/webgl_draw.cpp +116 -5
- package/runtime/ext_gui/webgl_draw_buffers.cpp +21 -6
- package/runtime/ext_gui/webgl_draw_setup.cpp +1 -0
- package/runtime/ext_gui/webgl_draw_state.h +2 -0
- package/runtime/ext_gui/webgl_feature.cpp +2 -2
- package/runtime/ext_gui/webgl_feature.h +4 -3
- package/runtime/ext_gui/webgl_framebuffer.cpp +346 -0
- package/runtime/ext_gui/webgl_glsl_rewrite.cpp +78 -47
- package/runtime/ext_gui/webgl_objects.h +16 -2
- package/runtime/ext_gui/webgl_pipeline.cpp +1 -1
- package/runtime/ext_gui/webgl_state.cpp +45 -36
- package/runtime/ext_gui/webgl_targets.cpp +440 -0
- package/runtime/ext_gui/webgl_texture.cpp +81 -25
- package/runtime/ext_gui/window.cpp +8 -0
- package/runtime/rt.h +27 -27
- package/runtime/rt_binary.h +44 -0
- package/runtime/rt_internal.h +52 -60
- package/runtime/rt_object.h +83 -0
- package/runtime/rt_profile.h +45 -0
- package/runtime/xt_alloc.c +88 -133
- package/runtime/xt_array_buffer.c +99 -71
- package/runtime/xt_builtins.c +3 -1
- package/runtime/xt_constructors.c +1 -0
- package/runtime/xt_containers/access.inc +96 -11
- package/runtime/xt_containers/arrays.inc +2 -1
- package/runtime/xt_containers/cstr_cache.inc +56 -0
- package/runtime/xt_containers/method_cache.inc +121 -0
- package/runtime/xt_containers/object_keys.inc +58 -0
- package/runtime/xt_containers/objects.inc +47 -79
- package/runtime/xt_containers/shapes.inc +153 -8
- package/runtime/xt_containers.c +4 -0
- package/runtime/xt_gc_release.c +70 -0
- package/runtime/xt_gc_stack.c +83 -0
- package/runtime/xt_gc_verify.c +1 -0
- package/runtime/xt_io/console.inc +66 -0
- package/runtime/xt_io/exception.inc +32 -6
- package/runtime/xt_io/watchdog.inc +101 -0
- package/runtime/xt_io.c +1 -0
- package/runtime/xt_loop.c +23 -3
- package/runtime/xt_pool.c +151 -0
- package/runtime/xt_profile.c +126 -0
- package/runtime/xt_stdlib/dispatch.inc +7 -1
- package/runtime/xt_stdlib/math.inc +272 -57
- package/runtime/xt_stdlib2/json_stringify.inc +1 -2
- package/runtime/xt_stdlib2/regexp-match.inc +17 -0
- package/runtime/xt_storage/indexed_db.c +4 -1
- package/runtime/xt_structured_clone.c +2 -1
- package/runtime/xt_typed_array/construction.inc +10 -0
- package/runtime/xt_typed_array/elements.inc +43 -23
- package/runtime/xt_url.c +50 -0
- package/runtime/xt_values/constructors.inc +4 -1
- package/runtime/xt_values/strings.inc +3 -1
- package/src/ast/common.ts +10 -0
- package/src/checker/source.ts +3 -0
- package/src/cli/main.ts +14 -3
- package/src/codegen/generator/calls/access.ts +5 -10
- package/src/codegen/generator/calls/element-inline.ts +433 -0
- package/src/codegen/generator/calls/invocation.ts +87 -1
- package/src/codegen/generator/calls.ts +5 -0
- package/src/codegen/generator/debug.ts +21 -3
- package/src/codegen/generator/expressions/assignment.ts +11 -2
- package/src/codegen/generator/expressions/global-values.ts +57 -0
- package/src/codegen/generator/expressions/primary.ts +4 -27
- package/src/codegen/generator/expressions.ts +3 -0
- package/src/codegen/generator/layout.ts +54 -0
- package/src/codegen/generator/state.ts +5 -0
- package/src/diagnostics/diagnostic.ts +1 -1
- package/src/diagnostics/source-text.ts +118 -22
- package/src/diagnostics/source.ts +29 -7
- package/src/driver/build-key.ts +21 -1
- package/src/driver/bundler/concatenate.ts +12 -5
- package/src/driver/bundler/graph.ts +2 -2
- package/src/driver/bundler/merge.ts +11 -4
- package/src/driver/bundler/reexport-resolution.ts +2 -0
- package/src/driver/compiler.ts +14 -3
- package/src/driver/frontend.ts +3 -4
- package/src/driver/runtime-objects.ts +4 -0
- package/src/driver/toolchain.ts +28 -5
- package/src/driver/version.ts +1 -1
- package/src/extensions/gui/html.ts +41 -11
- package/src/extensions/gui/index.ts +50 -1
- package/src/extensions/registry.ts +20 -0
- package/src/parser/expressions.ts +2 -2
- package/src/parser/helpers.ts +22 -0
package/runtime/xt_alloc.c
CHANGED
|
@@ -54,21 +54,11 @@ static size_t g_root_provider_capacity = 0;
|
|
|
54
54
|
|
|
55
55
|
/* ------------------------------------------------------------------------- */
|
|
56
56
|
/* Active stack regions */
|
|
57
|
+
/* */
|
|
58
|
+
/* The region table lives in `xt_gc_stack.c`; it grows on demand because an */
|
|
59
|
+
/* async call chain can be deeper than any fixed limit. */
|
|
57
60
|
/* ------------------------------------------------------------------------- */
|
|
58
61
|
|
|
59
|
-
#define XT_GC_MAX_STACKS 64
|
|
60
|
-
typedef struct {
|
|
61
|
-
uintptr_t low;
|
|
62
|
-
uintptr_t high;
|
|
63
|
-
/* Lower bound captured from the stack below this region when a deeper region
|
|
64
|
-
* was entered on top of it. `UINTPTR_MAX` means "this is the top region"; the
|
|
65
|
-
* collector then scans from the current stack pointer. */
|
|
66
|
-
uintptr_t scan_low;
|
|
67
|
-
} xt_stack_region;
|
|
68
|
-
|
|
69
|
-
static xt_stack_region g_stack_regions[XT_GC_MAX_STACKS];
|
|
70
|
-
static int g_stack_depth = 0;
|
|
71
|
-
|
|
72
62
|
/* ------------------------------------------------------------------------- */
|
|
73
63
|
/* Mark stack and address set */
|
|
74
64
|
/* ------------------------------------------------------------------------- */
|
|
@@ -144,6 +134,8 @@ static void xt_heap_link(xt_header *header) {
|
|
|
144
134
|
}
|
|
145
135
|
|
|
146
136
|
void *xt_alloc(size_t size, int kind) {
|
|
137
|
+
XT_PROFILE_TICK(XT_PROFILE_ALLOC);
|
|
138
|
+
if (xt_profile_on) xt_profile_note_alloc(kind);
|
|
147
139
|
if (!g_gc_initialised) xt_gc_init();
|
|
148
140
|
if (xt_gc_worker_thread) {
|
|
149
141
|
void *block = calloc(1, size);
|
|
@@ -162,7 +154,15 @@ void *xt_alloc(size_t size, int kind) {
|
|
|
162
154
|
* the conservative stack scan. */
|
|
163
155
|
if (g_gc_armed && !g_gc_running && g_heap_bytes >= g_gc_threshold) xt_gc_collect();
|
|
164
156
|
|
|
165
|
-
xt_header *header = (xt_header *)
|
|
157
|
+
xt_header *header = (xt_header *)xt_pool_alloc(size);
|
|
158
|
+
if (header != NULL) {
|
|
159
|
+
header->kind = (uint8_t)kind;
|
|
160
|
+
header->flags = XT_GC_POOLED;
|
|
161
|
+
header->size = (uint32_t)size;
|
|
162
|
+
xt_heap_link(header);
|
|
163
|
+
return header;
|
|
164
|
+
}
|
|
165
|
+
header = (xt_header *)calloc(1, size);
|
|
166
166
|
if (!header) {
|
|
167
167
|
fprintf(stderr, "xbintsc: out of memory allocating %zu bytes\n", size);
|
|
168
168
|
abort();
|
|
@@ -170,7 +170,6 @@ void *xt_alloc(size_t size, int kind) {
|
|
|
170
170
|
header->kind = (uint8_t)kind;
|
|
171
171
|
header->flags = 0;
|
|
172
172
|
header->size = (uint32_t)size;
|
|
173
|
-
header->size = (uint32_t)size;
|
|
174
173
|
xt_heap_link(header);
|
|
175
174
|
return header;
|
|
176
175
|
}
|
|
@@ -185,30 +184,20 @@ size_t xt_heap_allocations(void) { return g_heap_allocations; }
|
|
|
185
184
|
void xt_gc_set_stack_base(void *base) {
|
|
186
185
|
uintptr_t high = (uintptr_t)base;
|
|
187
186
|
if (high == 0) return;
|
|
188
|
-
|
|
189
|
-
g_stack_regions[0].low = 0;
|
|
190
|
-
g_stack_regions[0].high = high;
|
|
191
|
-
g_stack_regions[0].scan_low = UINTPTR_MAX;
|
|
192
|
-
g_stack_depth = 1;
|
|
193
|
-
} else if (high > g_stack_regions[0].high) {
|
|
194
|
-
g_stack_regions[0].high = high;
|
|
195
|
-
}
|
|
187
|
+
xt_gc_init_main_stack(high);
|
|
196
188
|
}
|
|
197
189
|
|
|
198
190
|
void xt_gc_init(void) {
|
|
199
191
|
if (g_gc_initialised) return;
|
|
200
192
|
g_gc_initialised = 1;
|
|
193
|
+
/* The generated `main` calls this first, before any module code, so the
|
|
194
|
+
* `performance.now()` origin is the start of the program. */
|
|
195
|
+
xt_time_init();
|
|
196
|
+
xt_profile_init();
|
|
201
197
|
uintptr_t high = (uintptr_t)&high;
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
g_stack_regions[0].scan_low = UINTPTR_MAX;
|
|
206
|
-
g_stack_depth = 1;
|
|
207
|
-
} else if (high > g_stack_regions[0].high) {
|
|
208
|
-
/* `xt_gc_set_stack_base` normally installed a bound above the caller's
|
|
209
|
-
* frame already; keep the higher of the two. */
|
|
210
|
-
g_stack_regions[0].high = high;
|
|
211
|
-
}
|
|
198
|
+
/* `xt_gc_set_stack_base` normally installed a bound above the caller's frame
|
|
199
|
+
* already; the helper keeps the higher of the two. */
|
|
200
|
+
xt_gc_init_main_stack(high);
|
|
212
201
|
const char *env = getenv("XT_GC_THRESHOLD");
|
|
213
202
|
if (env && *env) {
|
|
214
203
|
long long value = atoll(env);
|
|
@@ -269,24 +258,6 @@ void xt_gc_register_root_provider(void (*provider)(void)) {
|
|
|
269
258
|
}
|
|
270
259
|
g_root_providers[g_root_provider_count++] = provider;
|
|
271
260
|
}
|
|
272
|
-
void xt_gc_push_stack(uintptr_t low, uintptr_t high) {
|
|
273
|
-
if (g_stack_depth >= XT_GC_MAX_STACKS) return;
|
|
274
|
-
g_stack_regions[g_stack_depth].low = low;
|
|
275
|
-
g_stack_regions[g_stack_depth].high = high;
|
|
276
|
-
g_stack_regions[g_stack_depth].scan_low = low;
|
|
277
|
-
g_stack_depth++;
|
|
278
|
-
}
|
|
279
|
-
void xt_gc_suspend(uintptr_t sp) {
|
|
280
|
-
if (g_stack_depth <= 0) return;
|
|
281
|
-
uintptr_t *scan = &g_stack_regions[g_stack_depth - 1].scan_low;
|
|
282
|
-
if (*scan == UINTPTR_MAX || sp < *scan) *scan = sp;
|
|
283
|
-
}
|
|
284
|
-
void xt_gc_pop_stack(void) {
|
|
285
|
-
if (g_stack_depth <= 1) return;
|
|
286
|
-
g_stack_depth--;
|
|
287
|
-
/* Back on the underlying stack: resume scanning it from the current frame. */
|
|
288
|
-
if (g_stack_depth > 0) g_stack_regions[g_stack_depth - 1].scan_low = UINTPTR_MAX;
|
|
289
|
-
}
|
|
290
261
|
/* ------------------------------------------------------------------------- */
|
|
291
262
|
/* Marking */
|
|
292
263
|
/* ------------------------------------------------------------------------- */
|
|
@@ -319,10 +290,12 @@ static void xt_gc_gray_push_checked(xt_header *header) {
|
|
|
319
290
|
void xt_gc_mark_header(xt_header *header) { xt_gc_gray_push_checked(header); }
|
|
320
291
|
|
|
321
292
|
void xt_gc_mark_value(xt_value value) {
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
293
|
+
/* One tag compare rejects every number (and undefined/null/booleans), which
|
|
294
|
+
* is most of a scene's property slots; the five individual `XT_IS_*` tests
|
|
295
|
+
* made marking a large live set spend most of its time in predictable
|
|
296
|
+
* branches. */
|
|
297
|
+
if ((value & XT_TAG_MASK) < XT_TAG_BIGINT) return;
|
|
298
|
+
xt_gc_gray_push((xt_header *)XT_GET_PTR(value));
|
|
326
299
|
}
|
|
327
300
|
int xt_gc_value_is_marked(xt_value value) {
|
|
328
301
|
if (!(XT_IS_STRING(value) || XT_IS_OBJECT(value) || XT_IS_ARRAY(value) ||
|
|
@@ -377,41 +350,59 @@ static void xt_gc_process(xt_header *header) {
|
|
|
377
350
|
/* Numbers/bytes only: no references to follow. */
|
|
378
351
|
return;
|
|
379
352
|
case XT_OBJECT_KIND_OBJECT:
|
|
380
|
-
case XT_OBJECT_KIND_ERROR:
|
|
353
|
+
case XT_OBJECT_KIND_ERROR:
|
|
354
|
+
case XT_OBJECT_KIND_ARRAY_BUFFER: {
|
|
355
|
+
/* Precise trace: every reference in an ordinary object is an `xt_value`
|
|
356
|
+
* (a property key/value/accessor, the prototype and the captured error
|
|
357
|
+
* stack). The old conservative payload scan also looked up the raw
|
|
358
|
+
* `properties`/`shape`/`hooks` pointers and every number in the property
|
|
359
|
+
* table, which made marking a large scene spend most of its time in the
|
|
360
|
+
* heap address table. */
|
|
381
361
|
xt_object *object = (xt_object *)header;
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
362
|
+
xt_gc_mark_value(object->prototype);
|
|
363
|
+
xt_gc_mark_value(object->error_stack);
|
|
364
|
+
for (uint32_t i = 0; i < object->count; i++) {
|
|
365
|
+
xt_gc_mark_value(object->properties[i].key);
|
|
366
|
+
xt_gc_mark_value(object->properties[i].value);
|
|
367
|
+
xt_gc_mark_value(object->properties[i].getter);
|
|
368
|
+
xt_gc_mark_value(object->properties[i].setter);
|
|
385
369
|
}
|
|
386
370
|
return;
|
|
387
371
|
}
|
|
388
372
|
case XT_OBJECT_KIND_ARRAY: {
|
|
389
373
|
xt_array *array = (xt_array *)header;
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
}
|
|
374
|
+
xt_gc_mark_value(array->prototype);
|
|
375
|
+
xt_gc_mark_value(array->extra);
|
|
376
|
+
for (uint32_t i = 0; i < array->length; i++) xt_gc_mark_value(array->items[i]);
|
|
394
377
|
return;
|
|
395
378
|
}
|
|
396
379
|
case XT_OBJECT_KIND_FUNCTION: {
|
|
397
380
|
xt_function *function = (xt_function *)header;
|
|
398
|
-
|
|
399
|
-
if (function->
|
|
400
|
-
|
|
381
|
+
if (function->name != NULL) xt_gc_gray_push_checked(&function->name->header);
|
|
382
|
+
if (function->properties != NULL) xt_gc_gray_push_checked(&function->properties->header);
|
|
383
|
+
xt_gc_mark_value(function->prototype);
|
|
384
|
+
for (int32_t i = 0; i < function->environment_count; i++) {
|
|
385
|
+
xt_gc_mark_value(function->environment[i]);
|
|
401
386
|
}
|
|
402
387
|
return;
|
|
403
388
|
}
|
|
404
389
|
case XT_OBJECT_KIND_MAP: {
|
|
405
390
|
xt_map *map = (xt_map *)header;
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
391
|
+
for (uint32_t i = 0; i < map->count; i++) {
|
|
392
|
+
xt_gc_mark_value(map->keys[i]);
|
|
393
|
+
xt_gc_mark_value(map->values[i]);
|
|
394
|
+
}
|
|
409
395
|
return;
|
|
410
396
|
}
|
|
411
397
|
case XT_OBJECT_KIND_SET: {
|
|
412
398
|
xt_set_object *set = (xt_set_object *)header;
|
|
413
|
-
|
|
414
|
-
|
|
399
|
+
for (uint32_t i = 0; i < set->count; i++) xt_gc_mark_value(set->values[i]);
|
|
400
|
+
return;
|
|
401
|
+
}
|
|
402
|
+
case XT_OBJECT_KIND_REGEXP: {
|
|
403
|
+
xt_regexp *regexp = (xt_regexp *)header;
|
|
404
|
+
xt_gc_mark_value(regexp->source);
|
|
405
|
+
xt_gc_mark_value(regexp->flags);
|
|
415
406
|
return;
|
|
416
407
|
}
|
|
417
408
|
case XT_OBJECT_KIND_PROMISE:
|
|
@@ -442,52 +433,6 @@ static void xt_gc_process(xt_header *header) {
|
|
|
442
433
|
/* ------------------------------------------------------------------------- */
|
|
443
434
|
/* Release */
|
|
444
435
|
/* ------------------------------------------------------------------------- */
|
|
445
|
-
static void xt_gc_release(xt_header *header) {
|
|
446
|
-
switch (header->kind) {
|
|
447
|
-
case XT_OBJECT_KIND_STRING:
|
|
448
|
-
free(((xt_string *)header)->flat);
|
|
449
|
-
break;
|
|
450
|
-
case XT_OBJECT_KIND_BIGINT:
|
|
451
|
-
free(((xt_bigint *)header)->limbs);
|
|
452
|
-
break;
|
|
453
|
-
case XT_OBJECT_KIND_OBJECT:
|
|
454
|
-
case XT_OBJECT_KIND_ERROR:
|
|
455
|
-
free(((xt_object *)header)->properties);
|
|
456
|
-
break;
|
|
457
|
-
case XT_OBJECT_KIND_ARRAY:
|
|
458
|
-
free(((xt_array *)header)->items);
|
|
459
|
-
break;
|
|
460
|
-
case XT_OBJECT_KIND_FUNCTION:
|
|
461
|
-
free(((xt_function *)header)->environment);
|
|
462
|
-
break;
|
|
463
|
-
case XT_OBJECT_KIND_MAP:
|
|
464
|
-
free(((xt_map *)header)->keys);
|
|
465
|
-
free(((xt_map *)header)->values);
|
|
466
|
-
break;
|
|
467
|
-
case XT_OBJECT_KIND_SET:
|
|
468
|
-
free(((xt_set_object *)header)->values);
|
|
469
|
-
break;
|
|
470
|
-
case XT_OBJECT_KIND_WEAK_MAP:
|
|
471
|
-
free(((xt_weak_map *)header)->keys);
|
|
472
|
-
free(((xt_weak_map *)header)->values);
|
|
473
|
-
break;
|
|
474
|
-
case XT_OBJECT_KIND_WEAK_SET:
|
|
475
|
-
free(((xt_weak_set *)header)->values);
|
|
476
|
-
break;
|
|
477
|
-
case XT_OBJECT_KIND_PROMISE:
|
|
478
|
-
xt_promise_gc_release(header);
|
|
479
|
-
break;
|
|
480
|
-
case XT_OBJECT_KIND_GENERATOR:
|
|
481
|
-
xt_generator_gc_release(header);
|
|
482
|
-
break;
|
|
483
|
-
case XT_OBJECT_KIND_ASYNC:
|
|
484
|
-
xt_async_gc_release(header);
|
|
485
|
-
break;
|
|
486
|
-
default:
|
|
487
|
-
break;
|
|
488
|
-
}
|
|
489
|
-
free(header);
|
|
490
|
-
}
|
|
491
436
|
/* ------------------------------------------------------------------------- */
|
|
492
437
|
/* Collection */
|
|
493
438
|
/* ------------------------------------------------------------------------- */
|
|
@@ -504,22 +449,29 @@ static void xt_gc_mark_roots(void) {
|
|
|
504
449
|
_setjmp(registers);
|
|
505
450
|
xt_gc_scan_region(®isters, sizeof(registers));
|
|
506
451
|
uintptr_t stack_pointer = (uintptr_t)®isters;
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
uintptr_t
|
|
452
|
+
size_t regionCount = xt_gc_stack_region_count();
|
|
453
|
+
for (size_t i = 0; i < regionCount; i++) {
|
|
454
|
+
uintptr_t high = 0;
|
|
455
|
+
uintptr_t low = 0;
|
|
456
|
+
xt_gc_stack_region_bounds(i, stack_pointer, &low, &high);
|
|
510
457
|
if (high > low) xt_gc_scan_region((const void *)low, high - low);
|
|
511
458
|
}
|
|
512
459
|
}
|
|
513
460
|
void xt_gc_collect(void) {
|
|
461
|
+
XT_PROFILE_TICK(XT_PROFILE_GC);
|
|
514
462
|
if (g_gc_running) return;
|
|
515
463
|
if (!g_gc_initialised) xt_gc_init();
|
|
516
464
|
g_gc_running = 1;
|
|
465
|
+
const double gc_started_ms = xt_monotonic_ms();
|
|
517
466
|
const int trace = xt_gc_trace_enabled();
|
|
518
467
|
const int leak = getenv("XT_GC_LEAK") != NULL;
|
|
519
468
|
const size_t heap_before = g_heap_bytes;
|
|
520
469
|
const size_t allocations_before = g_heap_allocations;
|
|
521
470
|
|
|
471
|
+
const double phase_started = xt_monotonic_ms();
|
|
522
472
|
xt_gc_table_build();
|
|
473
|
+
const double phase_table = xt_monotonic_ms() - phase_started;
|
|
474
|
+
const double mark_started = xt_monotonic_ms();
|
|
523
475
|
xt_gc_mark_roots();
|
|
524
476
|
/* Ephemeron fixed point: a WeakMap value is reachable only when its key is
|
|
525
477
|
* reachable. Marking a value can reach further keys, so repeat until a pass
|
|
@@ -535,6 +487,8 @@ void xt_gc_collect(void) {
|
|
|
535
487
|
xt_weak_gc_clear();
|
|
536
488
|
xt_weak_gc_compact();
|
|
537
489
|
if (getenv("XT_GC_VERIFY") != NULL) xt_gc_verify_payloads();
|
|
490
|
+
const double phase_mark = xt_monotonic_ms() - mark_started;
|
|
491
|
+
const double sweep_started = xt_monotonic_ms();
|
|
538
492
|
|
|
539
493
|
xt_header **link = &g_heap_head;
|
|
540
494
|
xt_header *header = g_heap_head;
|
|
@@ -565,20 +519,19 @@ void xt_gc_collect(void) {
|
|
|
565
519
|
}
|
|
566
520
|
|
|
567
521
|
xt_gc_table_free();
|
|
522
|
+
const double phase_sweep = xt_monotonic_ms() - sweep_started;
|
|
568
523
|
g_gc_running = 0;
|
|
569
524
|
|
|
570
525
|
if (!g_gc_threshold_pinned) {
|
|
571
|
-
/* Follow the live set:
|
|
572
|
-
* mark-sweep wants between collections
|
|
573
|
-
*
|
|
574
|
-
*
|
|
575
|
-
*
|
|
576
|
-
* threshold
|
|
577
|
-
*
|
|
578
|
-
*
|
|
579
|
-
|
|
580
|
-
* alive for one frame is enough to start that spiral. */
|
|
581
|
-
size_t next = g_heap_bytes * 2;
|
|
526
|
+
/* Follow the live set: four times the surviving heap is the headroom a
|
|
527
|
+
* mark-sweep wants between collections for an interactive program. Two
|
|
528
|
+
* times collected too often: a three.js scene that keeps ~380k objects live
|
|
529
|
+
* spent a ~300 ms pause every ~1.2 s, and half of the frame budget went to
|
|
530
|
+
* collections. Four times halves that overhead at a bounded memory cost
|
|
531
|
+
* (the threshold still shrinks with the live set, so it cannot ratchet
|
|
532
|
+
* upwards). The floor keeps a tiny heap from collecting on nearly every
|
|
533
|
+
* allocation. */
|
|
534
|
+
size_t next = g_heap_bytes * 4;
|
|
582
535
|
if (next < XT_GC_MIN_THRESHOLD) next = XT_GC_MIN_THRESHOLD;
|
|
583
536
|
g_gc_threshold = next;
|
|
584
537
|
}
|
|
@@ -586,9 +539,11 @@ void xt_gc_collect(void) {
|
|
|
586
539
|
g_gc_collections++;
|
|
587
540
|
if (trace) {
|
|
588
541
|
fprintf(stderr,
|
|
589
|
-
"xt_gc: #%zu heap %zu -> %zu bytes, allocations %zu -> %zu, next trigger %zu
|
|
542
|
+
"xt_gc: #%zu heap %zu -> %zu bytes, allocations %zu -> %zu, next trigger %zu, took %.1fms"
|
|
543
|
+
" (table %.1f, mark %.1f, sweep %.1f)\n",
|
|
590
544
|
g_gc_collections, heap_before, g_heap_bytes, allocations_before, g_heap_allocations,
|
|
591
|
-
g_gc_threshold)
|
|
545
|
+
g_gc_threshold, xt_monotonic_ms() - gc_started_ms, phase_table, phase_mark,
|
|
546
|
+
phase_sweep);
|
|
592
547
|
fprintf(stderr, "xt_gc: live kinds");
|
|
593
548
|
for (int i = 1; i < 16; i++) {
|
|
594
549
|
if (live_by_kind[i] != 0) {
|
|
@@ -20,44 +20,69 @@
|
|
|
20
20
|
#include <stdlib.h>
|
|
21
21
|
#include <string.h>
|
|
22
22
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
static xt_value xt_ab_data(xt_value buffer) { if (!XT_IS_OBJECT(buffer)) return XT_UNDEFINED;
|
|
36
|
-
return xt_object_get(buffer, xt_ab_data_key());
|
|
37
|
-
}
|
|
23
|
+
/*
|
|
24
|
+
* Storage: an ArrayBuffer is an ordinary object of kind
|
|
25
|
+
* `XT_OBJECT_KIND_ARRAY_BUFFER` whose bytes live in one `calloc`ed block
|
|
26
|
+
* (`buffer_bytes`). Typed arrays and DataViews index that block directly, so a
|
|
27
|
+
* geometry loop reads memory instead of paying a property lookup per byte.
|
|
28
|
+
*
|
|
29
|
+
* The block belongs to the buffer object and the collector frees it when the
|
|
30
|
+
* buffer is swept (see `xt_gc_release`). `byteLength` and `slice` are served by
|
|
31
|
+
* `xt_get` from this kind instead of being own properties, so an ArrayBuffer has
|
|
32
|
+
* no enumerable keys (`Object.keys(buffer)` is empty) like on the platform.
|
|
33
|
+
*/
|
|
38
34
|
|
|
39
35
|
static int xt_ab_is_buffer(xt_value value) {
|
|
40
|
-
return
|
|
36
|
+
return XT_IS_OBJECT(value) &&
|
|
37
|
+
((xt_object *)XT_GET_PTR(value))->header.kind == XT_OBJECT_KIND_ARRAY_BUFFER;
|
|
41
38
|
}
|
|
42
39
|
|
|
43
40
|
static xt_value xt_ab_new(uint32_t length) {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
41
|
+
/* Instances sit on `ArrayBuffer.prototype`, so `buffer instanceof ArrayBuffer`
|
|
42
|
+
* and `buffer.constructor` behave like the platform (the properties are still
|
|
43
|
+
* served from the kind, not from the prototype). */
|
|
44
|
+
xt_value proto = xt_builtin_prototype_for_kind(XT_OBJECT_KIND_ARRAY_BUFFER);
|
|
45
|
+
xt_value buffer = XT_IS_OBJECT(proto) ? xt_object_new_with_proto(proto) : xt_object_new();
|
|
46
|
+
xt_object *object = (xt_object *)XT_GET_PTR(buffer);
|
|
47
|
+
object->header.kind = (uint8_t)XT_OBJECT_KIND_ARRAY_BUFFER;
|
|
48
|
+
/* A zero-length buffer keeps a NULL block: nothing ever indexes it. */
|
|
49
|
+
object->buffer_bytes = length > 0 ? (uint8_t *)calloc(1, length) : NULL;
|
|
50
|
+
object->buffer_length = object->buffer_bytes != NULL ? length : 0;
|
|
49
51
|
return buffer;
|
|
50
52
|
}
|
|
51
53
|
|
|
52
54
|
static xt_value xt_ab_slice(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv);
|
|
53
|
-
|
|
55
|
+
|
|
56
|
+
/** The single `slice` method value `xt_get` hands out for an ArrayBuffer. */
|
|
57
|
+
xt_value xt_array_buffer_slice_value(void) {
|
|
58
|
+
static xt_value cached = XT_UNDEFINED;
|
|
59
|
+
if (XT_IS_FUNCTION(cached)) return cached;
|
|
60
|
+
cached = xt_closure_new((void *)xt_ab_slice, 0, NULL);
|
|
61
|
+
xt_gc_add_root(&cached);
|
|
62
|
+
return cached;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** The properties an ArrayBuffer serves from its kind rather than from a
|
|
66
|
+
* property bag: `byteLength` and `slice`. `xt_get` and `xt_object_get` both
|
|
67
|
+
* consult this, so a C helper and JavaScript code agree. */
|
|
68
|
+
xt_value xt_array_buffer_served_property(xt_value buffer, xt_value key) {
|
|
69
|
+
if (!XT_IS_STRING(key)) return XT_UNDEFINED;
|
|
70
|
+
xt_object *object = (xt_object *)XT_GET_PTR(buffer);
|
|
71
|
+
xt_string *name = xt_as_string(key);
|
|
72
|
+
if (name == NULL) return XT_UNDEFINED;
|
|
73
|
+
if (name->length == 10 && memcmp(xt_string_bytes(name), "byteLength", 10) == 0) {
|
|
74
|
+
return xt_number((double)object->buffer_length);
|
|
75
|
+
}
|
|
76
|
+
if (name->length == 5 && memcmp(xt_string_bytes(name), "slice", 5) == 0) {
|
|
77
|
+
return xt_array_buffer_slice_value();
|
|
78
|
+
}
|
|
79
|
+
return XT_UNDEFINED;
|
|
80
|
+
}
|
|
54
81
|
|
|
55
82
|
xt_value xt_array_buffer_ctor(int32_t argc, xt_value *argv) {
|
|
56
83
|
double length = xt_to_number(xt_arg_at(argc, argv, 0));
|
|
57
84
|
if (!(length >= 0) || length > 4294967295.0) length = 0;
|
|
58
|
-
|
|
59
|
-
xt_ab_install(buffer, "slice", (void *)xt_ab_slice);
|
|
60
|
-
return buffer;
|
|
85
|
+
return xt_ab_new((uint32_t)length);
|
|
61
86
|
}
|
|
62
87
|
|
|
63
88
|
xt_value xt_array_buffer_alloc(uint32_t byte_length) { return xt_ab_new(byte_length); }
|
|
@@ -65,55 +90,61 @@ xt_value xt_array_buffer_alloc(uint32_t byte_length) { return xt_ab_new(byte_len
|
|
|
65
90
|
int xt_is_array_buffer(xt_value value) { return xt_ab_is_buffer(value); }
|
|
66
91
|
|
|
67
92
|
uint32_t xt_array_buffer_byte_length(xt_value value) {
|
|
68
|
-
if (!
|
|
69
|
-
|
|
93
|
+
if (!XT_IS_OBJECT(value)) return 0;
|
|
94
|
+
xt_object *object = (xt_object *)XT_GET_PTR(value);
|
|
95
|
+
if (object->header.kind != XT_OBJECT_KIND_ARRAY_BUFFER) return 0;
|
|
96
|
+
return object->buffer_length;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
uint8_t *xt_array_buffer_bytes(xt_value buffer, uint32_t *byte_length) {
|
|
100
|
+
if (byte_length != NULL) *byte_length = 0;
|
|
101
|
+
if (!XT_IS_OBJECT(buffer)) return NULL;
|
|
102
|
+
xt_object *object = (xt_object *)XT_GET_PTR(buffer);
|
|
103
|
+
if (object->header.kind != XT_OBJECT_KIND_ARRAY_BUFFER) return NULL;
|
|
104
|
+
if (byte_length != NULL) *byte_length = object->buffer_length;
|
|
105
|
+
return object->buffer_bytes;
|
|
70
106
|
}
|
|
71
107
|
|
|
72
108
|
uint8_t xt_array_buffer_get_byte(xt_value buffer, uint32_t index) {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
109
|
+
uint32_t length = 0;
|
|
110
|
+
uint8_t *bytes = xt_array_buffer_bytes(buffer, &length);
|
|
111
|
+
if (bytes == NULL || index >= length) return 0;
|
|
112
|
+
return bytes[index];
|
|
76
113
|
}
|
|
77
114
|
|
|
78
115
|
void xt_array_buffer_set_byte(xt_value buffer, uint32_t index, uint8_t value) {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
116
|
+
uint32_t length = 0;
|
|
117
|
+
uint8_t *bytes = xt_array_buffer_bytes(buffer, &length);
|
|
118
|
+
if (bytes == NULL || index >= length) return;
|
|
119
|
+
bytes[index] = value;
|
|
82
120
|
}
|
|
83
121
|
|
|
84
|
-
/* Bulk byte access
|
|
85
|
-
* property read per byte, which is far too slow for a multi-megabyte file. The
|
|
86
|
-
* backing store is an `xt_array` of numbers; reading its `items` directly is
|
|
87
|
-
* the same representation `xt_ab_new` writes. */
|
|
122
|
+
/* Bulk byte access for decoders and wasm memories. */
|
|
88
123
|
uint32_t xt_array_buffer_copy_bytes_out(xt_value buffer, uint32_t offset, uint8_t *dst,
|
|
89
124
|
uint32_t count) {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
if (
|
|
93
|
-
if (
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
}
|
|
125
|
+
uint32_t length = 0;
|
|
126
|
+
uint8_t *bytes = xt_array_buffer_bytes(buffer, &length);
|
|
127
|
+
if (bytes == NULL || dst == NULL) return 0;
|
|
128
|
+
if (offset >= length) return 0;
|
|
129
|
+
if (count > length - offset) count = length - offset;
|
|
130
|
+
memcpy(dst, bytes + offset, count);
|
|
97
131
|
return count;
|
|
98
132
|
}
|
|
99
133
|
|
|
100
134
|
uint32_t xt_array_buffer_copy_bytes_in(xt_value buffer, uint32_t offset, const uint8_t *src,
|
|
101
135
|
uint32_t count) {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
if (
|
|
105
|
-
if (
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
}
|
|
136
|
+
uint32_t length = 0;
|
|
137
|
+
uint8_t *bytes = xt_array_buffer_bytes(buffer, &length);
|
|
138
|
+
if (bytes == NULL || src == NULL) return 0;
|
|
139
|
+
if (offset >= length) return 0;
|
|
140
|
+
if (count > length - offset) count = length - offset;
|
|
141
|
+
memcpy(bytes + offset, src, count);
|
|
109
142
|
return count;
|
|
110
143
|
}
|
|
111
144
|
|
|
112
145
|
static xt_value xt_ab_slice(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
|
|
113
146
|
(void)env;
|
|
114
|
-
|
|
115
|
-
if (!XT_IS_ARRAY(data)) return xt_ab_new(0);
|
|
116
|
-
int32_t length = xt_to_int32(xt_array_length(data));
|
|
147
|
+
int32_t length = (int32_t)xt_array_buffer_byte_length(thisValue);
|
|
117
148
|
int32_t start = argc > 0 && argv[0] != XT_UNDEFINED ? xt_to_int32(argv[0]) : 0;
|
|
118
149
|
int32_t end = argc > 1 && argv[1] != XT_UNDEFINED ? xt_to_int32(argv[1]) : length;
|
|
119
150
|
if (start < 0) start += length;
|
|
@@ -121,18 +152,12 @@ static xt_value xt_ab_slice(xt_value thisValue, xt_value env, int32_t argc, xt_v
|
|
|
121
152
|
if (start < 0) start = 0;
|
|
122
153
|
if (end > length) end = length;
|
|
123
154
|
if (end < start) end = start;
|
|
124
|
-
xt_value result = xt_ab_new(
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
for (int32_t i = start; i < end; i++) xt_array_push(source, xt_get(data, xt_number((double)i)));
|
|
128
|
-
xt_object_set(result, xt_string_from_cstr("byteLength"), xt_number((double)(end - start)));
|
|
155
|
+
xt_value result = xt_ab_new((uint32_t)(end - start));
|
|
156
|
+
xt_array_buffer_copy_bytes_out(thisValue, (uint32_t)start,
|
|
157
|
+
xt_array_buffer_bytes(result, NULL), (uint32_t)(end - start));
|
|
129
158
|
return result;
|
|
130
159
|
}
|
|
131
160
|
|
|
132
|
-
static void xt_ab_install(xt_value object, const char *name, void *fn) {
|
|
133
|
-
xt_object_set(object, xt_string_from_cstr(name), xt_closure_new(fn, 0, NULL));
|
|
134
|
-
}
|
|
135
|
-
|
|
136
161
|
xt_value xt_array_buffer_static(xt_value name, int32_t argc, xt_value *argv) {
|
|
137
162
|
const char *method = xt_string_data(name);
|
|
138
163
|
if (!method) return XT_UNDEFINED;
|
|
@@ -182,8 +207,8 @@ static int xt_dv_signed(int kind) {
|
|
|
182
207
|
return kind == XT_DV_I8 || kind == XT_DV_I16 || kind == XT_DV_I32;
|
|
183
208
|
}
|
|
184
209
|
|
|
185
|
-
static xt_value
|
|
186
|
-
return
|
|
210
|
+
static xt_value xt_dv_buffer(xt_value view) {
|
|
211
|
+
return xt_get(view, xt_string_from_cstr("buffer"));
|
|
187
212
|
}
|
|
188
213
|
|
|
189
214
|
static uint32_t xt_dv_offset(xt_value view) {
|
|
@@ -207,23 +232,25 @@ static int xt_dv_check(xt_value view, xt_value indexValue, uint32_t width, uint3
|
|
|
207
232
|
}
|
|
208
233
|
|
|
209
234
|
static uint64_t xt_dv_read_bits(xt_value view, uint32_t offset, uint32_t width, int little) {
|
|
210
|
-
|
|
235
|
+
uint32_t length = 0;
|
|
236
|
+
uint8_t *bytes = xt_array_buffer_bytes(xt_dv_buffer(view), &length);
|
|
211
237
|
uint64_t value = 0;
|
|
238
|
+
if (bytes == NULL || offset + width > length) return 0;
|
|
212
239
|
for (uint32_t i = 0; i < width; i++) {
|
|
213
240
|
uint32_t index = little ? offset + width - 1 - i : offset + i;
|
|
214
|
-
|
|
215
|
-
value = (value << 8) | (byte & 0xff);
|
|
241
|
+
value = (value << 8) | (uint64_t)bytes[index];
|
|
216
242
|
}
|
|
217
243
|
return value;
|
|
218
244
|
}
|
|
219
245
|
|
|
220
246
|
static void xt_dv_write_bits(xt_value view, uint32_t offset, uint32_t width, uint64_t value,
|
|
221
247
|
int little) {
|
|
222
|
-
|
|
248
|
+
uint32_t length = 0;
|
|
249
|
+
uint8_t *bytes = xt_array_buffer_bytes(xt_dv_buffer(view), &length);
|
|
250
|
+
if (bytes == NULL || offset + width > length) return;
|
|
223
251
|
for (uint32_t i = 0; i < width; i++) {
|
|
224
252
|
uint32_t shift = little ? i * 8 : (width - 1 - i) * 8;
|
|
225
|
-
|
|
226
|
-
xt_set(data, xt_number((double)index), xt_number((double)((value >> shift) & 0xff)));
|
|
253
|
+
bytes[offset + i] = (uint8_t)((value >> shift) & 0xff);
|
|
227
254
|
}
|
|
228
255
|
}
|
|
229
256
|
|
|
@@ -312,3 +339,4 @@ xt_value xt_data_view_ctor(int32_t argc, xt_value *argv) {
|
|
|
312
339
|
}
|
|
313
340
|
return view;
|
|
314
341
|
}
|
|
342
|
+
|
package/runtime/xt_builtins.c
CHANGED
|
@@ -117,7 +117,7 @@ xt_value xt_object_rest(xt_value source, int32_t argc, xt_value *argv) {
|
|
|
117
117
|
if (!XT_IS_OBJECT(source) && !XT_IS_FUNCTION(source)) return result;
|
|
118
118
|
xt_object *object = xt_as_object(source);
|
|
119
119
|
if (!object) return result;
|
|
120
|
-
if (object->header.kind
|
|
120
|
+
if (!xt_kind_is_object(object->header.kind)) {
|
|
121
121
|
/* Map/Set/Date/RegExp/Promise have no own enumerable string properties. */
|
|
122
122
|
return result;
|
|
123
123
|
}
|
|
@@ -421,6 +421,7 @@ xt_value xt_closure_new(void *fn, int32_t env_count, xt_value *env) {
|
|
|
421
421
|
}
|
|
422
422
|
|
|
423
423
|
xt_value xt_closure_call(xt_value value, int32_t argc, xt_value *argv) {
|
|
424
|
+
XT_PROFILE_TICK(XT_PROFILE_CLOSURE_CALL);
|
|
424
425
|
return xt_call_with_this(value, XT_UNDEFINED, argc, argv);
|
|
425
426
|
}
|
|
426
427
|
|
|
@@ -595,3 +596,4 @@ xt_value xt_function_set_metadata(xt_value value, xt_value name, int32_t arity)
|
|
|
595
596
|
return value;
|
|
596
597
|
}
|
|
597
598
|
|
|
599
|
+
|
|
@@ -487,6 +487,7 @@ xt_value xt_builtin_prototype_for_kind(int32_t kind) {
|
|
|
487
487
|
case XT_OBJECT_KIND_REGEXP: name = "RegExp"; break;
|
|
488
488
|
case XT_OBJECT_KIND_PROMISE: name = "Promise"; break;
|
|
489
489
|
case XT_OBJECT_KIND_SYMBOL: name = "Symbol"; break;
|
|
490
|
+
case XT_OBJECT_KIND_ARRAY_BUFFER: name = "ArrayBuffer"; break;
|
|
490
491
|
default: return XT_UNDEFINED;
|
|
491
492
|
}
|
|
492
493
|
xt_value constructor = xt_builtin_constructor(xt_string_from_cstr(name));
|