xbintsc 0.6.28 → 0.7.0
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/binder/declarations.d.ts +1 -0
- package/dist/src/binder/declarations.js +37 -0
- package/dist/src/binder/declarations.js.map +1 -1
- package/dist/src/binder/references.js +12 -0
- package/dist/src/binder/references.js.map +1 -1
- package/dist/src/checker/globals.js +1 -0
- package/dist/src/checker/globals.js.map +1 -1
- package/dist/src/checker/type-system.d.ts +5 -0
- package/dist/src/checker/type-system.js +22 -3
- package/dist/src/checker/type-system.js.map +1 -1
- package/dist/src/cli/help.d.ts +1 -1
- package/dist/src/codegen/generator/calls/access.js +25 -0
- package/dist/src/codegen/generator/calls/access.js.map +1 -1
- package/dist/src/codegen/generator/calls/invocation.js +13 -0
- package/dist/src/codegen/generator/calls/invocation.js.map +1 -1
- package/dist/src/codegen/generator/calls/super.js +3 -4
- package/dist/src/codegen/generator/calls/super.js.map +1 -1
- package/dist/src/codegen/generator/calls/unwrap.d.ts +18 -0
- package/dist/src/codegen/generator/calls/unwrap.js +45 -0
- package/dist/src/codegen/generator/calls/unwrap.js.map +1 -0
- package/dist/src/codegen/generator/expressions/assignment.js +14 -0
- package/dist/src/codegen/generator/expressions/assignment.js.map +1 -1
- package/dist/src/codegen/generator/expressions/module-values.d.ts +6 -0
- package/dist/src/codegen/generator/expressions/module-values.js +51 -17
- package/dist/src/codegen/generator/expressions/module-values.js.map +1 -1
- package/dist/src/codegen/generator/expressions/primary.js +9 -5
- package/dist/src/codegen/generator/expressions/primary.js.map +1 -1
- package/dist/src/codegen/generator/generator.js +4 -0
- package/dist/src/codegen/generator/generator.js.map +1 -1
- package/dist/src/codegen/generator/lookup-tables.d.ts +22 -0
- package/dist/src/codegen/generator/lookup-tables.js +48 -0
- package/dist/src/codegen/generator/lookup-tables.js.map +1 -0
- package/dist/src/codegen/generator/module.js +38 -7
- package/dist/src/codegen/generator/module.js.map +1 -1
- package/dist/src/codegen/generator/state.js +17 -0
- package/dist/src/codegen/generator/state.js.map +1 -1
- package/dist/src/codegen/generator/tables.d.ts +2 -6
- package/dist/src/codegen/generator/tables.js +18 -12
- package/dist/src/codegen/generator/tables.js.map +1 -1
- package/dist/src/driver/bundler/commonjs.d.ts +9 -2
- package/dist/src/driver/bundler/commonjs.js +11 -4
- package/dist/src/driver/bundler/commonjs.js.map +1 -1
- package/dist/src/driver/bundler/concatenate.d.ts +20 -0
- package/dist/src/driver/bundler/concatenate.js +68 -4
- package/dist/src/driver/bundler/concatenate.js.map +1 -1
- package/dist/src/driver/bundler/deferred.d.ts +29 -1
- package/dist/src/driver/bundler/deferred.js +114 -5
- package/dist/src/driver/bundler/deferred.js.map +1 -1
- package/dist/src/driver/bundler/dynamic-import.d.ts +6 -0
- package/dist/src/driver/bundler/dynamic-import.js +59 -3
- package/dist/src/driver/bundler/dynamic-import.js.map +1 -1
- package/dist/src/driver/bundler/graph.d.ts +13 -0
- 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 +32 -8
- package/dist/src/driver/bundler/merge.js.map +1 -1
- package/dist/src/driver/bundler/namespace-object.d.ts +11 -0
- package/dist/src/driver/bundler/namespace-object.js +32 -0
- package/dist/src/driver/bundler/namespace-object.js.map +1 -1
- package/dist/src/driver/bundler/reexport-resolution.js +8 -2
- package/dist/src/driver/bundler/reexport-resolution.js.map +1 -1
- package/dist/src/driver/bundler/symbols.d.ts +4 -0
- package/dist/src/driver/bundler/symbols.js +14 -1
- package/dist/src/driver/bundler/symbols.js.map +1 -1
- package/dist/src/driver/config.d.ts +1 -1
- package/dist/src/driver/options.d.ts +1 -4
- package/dist/src/driver/runtime-lib.d.ts +27 -0
- package/dist/src/driver/runtime-lib.js +78 -2
- package/dist/src/driver/runtime-lib.js.map +1 -1
- package/dist/src/driver/runtime-objects.d.ts +1 -1
- package/dist/src/driver/runtime-objects.js +24 -5
- 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/driver/version.js.map +1 -1
- package/dist/src/extensions/node/buffer/index.d.ts +5 -4
- package/dist/src/extensions/node/buffer/index.js +5 -4
- package/dist/src/extensions/node/buffer/index.js.map +1 -1
- package/dist/src/extensions/node/events/index.js +8 -1
- package/dist/src/extensions/node/events/index.js.map +1 -1
- package/dist/src/extensions/node/fs/callbacks.d.ts +9 -0
- package/dist/src/extensions/node/fs/callbacks.js +42 -0
- package/dist/src/extensions/node/fs/callbacks.js.map +1 -0
- package/dist/src/extensions/node/fs/index.d.ts +2 -3
- package/dist/src/extensions/node/fs/index.js +11 -4
- package/dist/src/extensions/node/fs/index.js.map +1 -1
- package/dist/src/extensions/node/process/index.d.ts +10 -3
- package/dist/src/extensions/node/process/index.js +30 -5
- package/dist/src/extensions/node/process/index.js.map +1 -1
- package/dist/src/extensions/registry.d.ts +7 -0
- package/dist/src/extensions/registry.js +13 -4
- package/dist/src/extensions/registry.js.map +1 -1
- package/dist/src/lexer/scanner.d.ts +7 -1
- package/dist/src/lexer/scanner.js +15 -0
- package/dist/src/lexer/scanner.js.map +1 -1
- package/dist/src/parser/context.d.ts +10 -0
- package/dist/src/parser/context.js +42 -3
- package/dist/src/parser/context.js.map +1 -1
- package/dist/src/parser/literals.js +6 -2
- package/dist/src/parser/literals.js.map +1 -1
- package/dist/tests/binder/binder.test.js +43 -0
- package/dist/tests/binder/binder.test.js.map +1 -1
- package/dist/tests/codegen/llvm.test.js +15 -0
- package/dist/tests/codegen/llvm.test.js.map +1 -1
- package/dist/tests/driver/bundler-resolve.test.js +12 -0
- package/dist/tests/driver/bundler-resolve.test.js.map +1 -1
- package/dist/tests/driver/commonjs-prelude.test.d.ts +11 -0
- package/dist/tests/driver/commonjs-prelude.test.js +69 -0
- package/dist/tests/driver/commonjs-prelude.test.js.map +1 -0
- package/dist/tests/driver/compiler.test.js +42 -1
- package/dist/tests/driver/compiler.test.js.map +1 -1
- package/dist/tests/driver/html-deferred-cycles.test.d.ts +10 -0
- package/dist/tests/driver/html-deferred-cycles.test.js +75 -0
- package/dist/tests/driver/html-deferred-cycles.test.js.map +1 -0
- package/dist/tests/driver/namespace-type-exports.test.d.ts +9 -0
- package/dist/tests/driver/namespace-type-exports.test.js +59 -0
- package/dist/tests/driver/namespace-type-exports.test.js.map +1 -0
- package/dist/tests/driver/runtime-lib.test.js +49 -1
- package/dist/tests/driver/runtime-lib.test.js.map +1 -1
- package/dist/tests/e2e/compile-basics.test.js +53 -0
- package/dist/tests/e2e/compile-basics.test.js.map +1 -1
- package/dist/tests/e2e/compile-deferred-marker.test.d.ts +14 -0
- package/dist/tests/e2e/compile-deferred-marker.test.js +68 -0
- package/dist/tests/e2e/compile-deferred-marker.test.js.map +1 -0
- package/dist/tests/e2e/compile-deferred-reexport.test.d.ts +12 -0
- package/dist/tests/e2e/compile-deferred-reexport.test.js +89 -0
- package/dist/tests/e2e/compile-deferred-reexport.test.js.map +1 -0
- package/dist/tests/e2e/compile-error-module.test.d.ts +9 -0
- package/dist/tests/e2e/compile-error-module.test.js +29 -0
- package/dist/tests/e2e/compile-error-module.test.js.map +1 -0
- package/dist/tests/e2e/compile-gc-promises.test.d.ts +11 -0
- package/dist/tests/e2e/compile-gc-promises.test.js +52 -0
- package/dist/tests/e2e/compile-gc-promises.test.js.map +1 -0
- package/dist/tests/e2e/compile-modules.test.js +40 -0
- package/dist/tests/e2e/compile-modules.test.js.map +1 -1
- package/dist/tests/e2e/compile-node-fs-callbacks.test.d.ts +6 -0
- package/dist/tests/e2e/compile-node-fs-callbacks.test.js +46 -0
- package/dist/tests/e2e/compile-node-fs-callbacks.test.js.map +1 -0
- package/dist/tests/e2e/compile-node-inheritance.test.d.ts +6 -0
- package/dist/tests/e2e/compile-node-inheritance.test.js +48 -0
- package/dist/tests/e2e/compile-node-inheritance.test.js.map +1 -0
- package/dist/tests/e2e/compile-node-require.test.d.ts +9 -0
- package/dist/tests/e2e/compile-node-require.test.js +29 -0
- package/dist/tests/e2e/compile-node-require.test.js.map +1 -0
- package/dist/tests/e2e/compile-node-stats.test.d.ts +8 -0
- package/dist/tests/e2e/compile-node-stats.test.js +26 -0
- package/dist/tests/e2e/compile-node-stats.test.js.map +1 -0
- package/dist/tests/e2e/compile-proxy.test.d.ts +6 -0
- package/dist/tests/e2e/compile-proxy.test.js +66 -0
- package/dist/tests/e2e/compile-proxy.test.js.map +1 -0
- package/dist/tests/e2e/compile-web-globals.test.d.ts +6 -0
- package/dist/tests/e2e/compile-web-globals.test.js +94 -0
- package/dist/tests/e2e/compile-web-globals.test.js.map +1 -0
- package/dist/tests/e2e/fetch.test.js +1 -1
- package/dist/tests/e2e/gui-script-import-order.test.js +24 -0
- package/dist/tests/e2e/gui-script-import-order.test.js.map +1 -1
- package/dist/tests/e2e/json-stringify-to-json.test.d.ts +10 -0
- package/dist/tests/e2e/json-stringify-to-json.test.js +61 -0
- package/dist/tests/e2e/json-stringify-to-json.test.js.map +1 -0
- package/dist/tests/e2e/node-process.test.d.ts +7 -0
- package/dist/tests/e2e/node-process.test.js +50 -0
- package/dist/tests/e2e/node-process.test.js.map +1 -0
- package/dist/tests/e2e/runtime-coverage-buffer.test.js +31 -0
- package/dist/tests/e2e/runtime-coverage-buffer.test.js.map +1 -1
- package/dist/tests/e2e/web-streams.test.js +44 -29
- package/dist/tests/e2e/web-streams.test.js.map +1 -1
- package/dist/tests/parser/literals.test.js +19 -0
- package/dist/tests/parser/literals.test.js.map +1 -1
- package/dist/tests/parser/statements.test.js +13 -0
- package/dist/tests/parser/statements.test.js.map +1 -1
- package/doc/ai/cli.md +1 -0
- package/doc/ai/zh-CN/cli.md +1 -0
- package/doc/implemented/binder.md +2 -1
- package/doc/implemented/checker.md +5 -0
- package/doc/implemented/lexer.md +1 -1
- package/doc/implemented/modules.md +23 -1
- package/doc/implemented/node/buffer.md +18 -5
- package/doc/implemented/node/fs.md +22 -1
- package/doc/implemented/node/process.md +65 -5
- package/doc/implemented/node.md +7 -1
- package/doc/implemented/operators.md +3 -1
- package/doc/implemented/parser.md +1 -1
- package/doc/implemented/runtime-core.md +6 -2
- package/doc/implemented/stdlib.md +69 -2
- package/doc/unimplemented/classes.md +1 -0
- package/doc/unimplemented/modules.md +1 -0
- package/doc/unimplemented/stdlib.md +2 -1
- package/doc/xbintsc.config.schema.json +5 -0
- package/doc/zh-CN/implemented/binder.md +2 -1
- package/doc/zh-CN/implemented/checker.md +3 -0
- package/doc/zh-CN/implemented/lexer.md +1 -1
- package/doc/zh-CN/implemented/modules.md +17 -1
- package/doc/zh-CN/implemented/node/buffer.md +3 -1
- package/doc/zh-CN/implemented/node/fs.md +19 -1
- package/doc/zh-CN/implemented/node/process.md +59 -5
- package/doc/zh-CN/implemented/node.md +1 -1
- package/doc/zh-CN/implemented/operators.md +2 -1
- package/doc/zh-CN/implemented/parser.md +1 -1
- package/doc/zh-CN/implemented/runtime-core.md +3 -2
- package/doc/zh-CN/implemented/stdlib.md +58 -2
- package/doc/zh-CN/unimplemented/classes.md +1 -0
- package/doc/zh-CN/unimplemented/modules.md +1 -0
- package/doc/zh-CN/unimplemented/stdlib.md +2 -1
- package/package.json +7 -7
- package/runtime/ext_node/buffer/buffer.c +5 -4
- package/runtime/ext_node/buffer/parts/byte-access.inc +115 -11
- package/runtime/ext_node/buffer/parts/prototype.inc +4 -1
- package/runtime/ext_node/events/events.c +9 -0
- package/runtime/ext_node/fs/callbacks.c +138 -0
- package/runtime/ext_node/fs/fs_ops.c +28 -15
- package/runtime/ext_node/module/module.c +18 -9
- package/runtime/ext_node/process/process.c +117 -32
- package/runtime/ext_node/process/streams.c +370 -0
- package/runtime/ext_node/process/streams.h +20 -0
- package/runtime/rt.h +36 -0
- package/runtime/rt_internal.h +10 -10
- package/runtime/xt_alloc.c +67 -49
- package/runtime/xt_async_io.c +3 -0
- package/runtime/xt_builtins.c +26 -23
- package/runtime/xt_constructors.c +101 -2
- package/runtime/xt_containers/access.inc +29 -1
- package/runtime/xt_containers/function_prototypes.inc +58 -0
- package/runtime/xt_containers/objects.inc +18 -2
- package/runtime/xt_containers.c +1 -0
- package/runtime/xt_exit.c +34 -0
- package/runtime/xt_fetch/headers.inc +1 -0
- package/runtime/xt_fetch/response.inc +28 -8
- package/runtime/xt_fetch/webapi.inc +82 -0
- package/runtime/xt_fetch.c +5 -0
- package/runtime/xt_gc_verify.c +135 -0
- package/runtime/xt_intl.c +224 -0
- package/runtime/xt_io/exception.inc +40 -1
- package/runtime/xt_module_values.c +82 -0
- package/runtime/xt_proxy.c +140 -0
- package/runtime/xt_stdlib/dispatch.inc +98 -9
- package/runtime/xt_stdlib2/date.inc +7 -1
- package/runtime/xt_stdlib2/json.inc +28 -0
- package/runtime/xt_stdlib2/json_stringify.inc +70 -53
- package/runtime/xt_web.h +27 -0
- package/runtime/xt_web_abort.c +373 -0
- package/scripts/build-runtime.ts +8 -0
- package/src/binder/declarations.ts +34 -0
- package/src/binder/references.ts +12 -0
- package/src/checker/globals.ts +1 -0
- package/src/checker/type-system.ts +23 -3
- package/src/codegen/generator/calls/access.ts +23 -0
- package/src/codegen/generator/calls/invocation.ts +12 -0
- package/src/codegen/generator/calls/super.ts +5 -4
- package/src/codegen/generator/calls/unwrap.ts +47 -0
- package/src/codegen/generator/expressions/assignment.ts +15 -0
- package/src/codegen/generator/expressions/module-values.ts +57 -19
- package/src/codegen/generator/expressions/primary.ts +9 -9
- package/src/codegen/generator/generator.ts +4 -0
- package/src/codegen/generator/lookup-tables.ts +63 -0
- package/src/codegen/generator/module.ts +38 -7
- package/src/codegen/generator/state.ts +17 -0
- package/src/codegen/generator/tables.ts +18 -12
- package/src/driver/bundler/commonjs.ts +12 -5
- package/src/driver/bundler/concatenate.ts +84 -7
- package/src/driver/bundler/deferred.ts +122 -6
- package/src/driver/bundler/dynamic-import.ts +75 -2
- package/src/driver/bundler/graph.ts +2 -2
- package/src/driver/bundler/merge.ts +31 -5
- package/src/driver/bundler/namespace-object.ts +44 -0
- package/src/driver/bundler/reexport-resolution.ts +12 -2
- package/src/driver/bundler/symbols.ts +17 -0
- package/src/driver/config.ts +1 -1
- package/src/driver/options.ts +1 -4
- package/src/driver/runtime-lib.ts +87 -2
- package/src/driver/runtime-objects.ts +24 -5
- package/src/driver/version.ts +1 -1
- package/src/extensions/node/buffer/index.ts +5 -4
- package/src/extensions/node/events/index.ts +8 -1
- package/src/extensions/node/fs/callbacks.ts +45 -0
- package/src/extensions/node/fs/index.ts +11 -4
- package/src/extensions/node/process/index.ts +30 -5
- package/src/extensions/registry.ts +21 -4
- package/src/lexer/scanner.ts +16 -1
- package/src/parser/context.ts +39 -3
- package/src/parser/literals.ts +6 -2
|
@@ -27,3 +27,85 @@ xt_value xt_method_trampoline(xt_value thisValue, xt_value env, int32_t argc, xt
|
|
|
27
27
|
(xt_value (*)(xt_value, int32_t, xt_value *))(uintptr_t)raw;
|
|
28
28
|
return dispatcher(name, argc, argv);
|
|
29
29
|
}
|
|
30
|
+
|
|
31
|
+
/* `import(<expression>)` in a compiled binary.
|
|
32
|
+
*
|
|
33
|
+
* The build links a fixed set of modules; `table` maps every enabled extension
|
|
34
|
+
* module name — bare and with the `node:` prefix — to its namespace object. A
|
|
35
|
+
* specifier that names one of them resolves to that namespace (this is how
|
|
36
|
+
* `import("node:" + "fs")`, the common "load Node built-ins lazily" idiom,
|
|
37
|
+
* works); anything else rejects with a message that names it. */
|
|
38
|
+
xt_value xt_dynamic_import(xt_value table, xt_value specifier) {
|
|
39
|
+
if (XT_IS_STRING(specifier)) {
|
|
40
|
+
xt_value found = xt_get(table, specifier);
|
|
41
|
+
if (found != XT_UNDEFINED && found != XT_NULL) return xt_promise_resolve(found);
|
|
42
|
+
}
|
|
43
|
+
xt_value message = xt_add(
|
|
44
|
+
xt_string_from_cstr("dynamic import of '"),
|
|
45
|
+
xt_add(xt_to_string(specifier),
|
|
46
|
+
xt_string_from_cstr("' is not supported in compiled binaries: "
|
|
47
|
+
"only modules linked into this build can be loaded")));
|
|
48
|
+
xt_value arguments[1];
|
|
49
|
+
arguments[0] = message;
|
|
50
|
+
return xt_promise_reject(xt_error_ctor_by_name(xt_string_from_cstr("Error"), 1, arguments));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/* Like `xt_fnptr_trampoline`, for a binding declared `void` (the console
|
|
54
|
+
* output methods): call it for its effect and answer `undefined`. */
|
|
55
|
+
xt_value xt_voidptr_trampoline(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
|
|
56
|
+
(void)thisValue;
|
|
57
|
+
xt_value raw = xt_closure_env(env, 0);
|
|
58
|
+
void (*fn)(int32_t, xt_value *) = (void (*)(int32_t, xt_value *))(uintptr_t)raw;
|
|
59
|
+
fn(argc, argv);
|
|
60
|
+
return XT_UNDEFINED;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/* `__xt_defer(id, init)`: evaluate `init` once and return its exports.
|
|
64
|
+
*
|
|
65
|
+
* The bundler wraps a module that is only reached through a dynamic import in
|
|
66
|
+
* such a function, so the body runs on the first `import(...)` instead of at
|
|
67
|
+
* startup. The placeholder is published before `init` runs, so a cycle among
|
|
68
|
+
* deferred modules sees the same object rather than recursing. */
|
|
69
|
+
xt_value xt_defer_module(int32_t argc, xt_value *argv) {
|
|
70
|
+
if (argc < 1) return XT_UNDEFINED;
|
|
71
|
+
static xt_value registry = 0;
|
|
72
|
+
if (registry == 0) {
|
|
73
|
+
registry = xt_object_new();
|
|
74
|
+
xt_gc_add_root(®istry);
|
|
75
|
+
}
|
|
76
|
+
xt_value key = xt_to_string(xt_arg(argc, argv, 0));
|
|
77
|
+
|
|
78
|
+
xt_value found = xt_object_get(registry, key);
|
|
79
|
+
if (found != XT_UNDEFINED) return found;
|
|
80
|
+
xt_value exports = xt_object_new();
|
|
81
|
+
xt_object_set(registry, key, exports);
|
|
82
|
+
xt_value init = xt_arg(argc, argv, 1);
|
|
83
|
+
if (XT_IS_FUNCTION(init)) {
|
|
84
|
+
exports = xt_call_with_this(init, XT_UNDEFINED, 0, NULL);
|
|
85
|
+
xt_object_set(registry, key, exports);
|
|
86
|
+
}
|
|
87
|
+
return exports;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/* -- linked-module registry ------------------------------------------------ *
|
|
91
|
+
*
|
|
92
|
+
* A compiled program has no module loader, but the modules linked into it are
|
|
93
|
+
* known: the code generator builds a table mapping every enabled extension
|
|
94
|
+
* module name (bare and `node:`-prefixed) to its namespace object and registers
|
|
95
|
+
* it before the first `createRequire(...)` / computed `import(...)` runs. Both
|
|
96
|
+
* entry points answer from that table instead of failing outright. */
|
|
97
|
+
static xt_value g_module_registry = 0;
|
|
98
|
+
static int g_module_registry_rooted = 0;
|
|
99
|
+
|
|
100
|
+
void xt_module_registry_set(xt_value table) {
|
|
101
|
+
g_module_registry = table;
|
|
102
|
+
if (!g_module_registry_rooted) {
|
|
103
|
+
xt_gc_add_root(&g_module_registry);
|
|
104
|
+
g_module_registry_rooted = 1;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
xt_value xt_module_registry_lookup(xt_value specifier) {
|
|
109
|
+
if (g_module_registry == 0 || !XT_IS_STRING(specifier)) return XT_UNDEFINED;
|
|
110
|
+
return xt_object_get(g_module_registry, specifier);
|
|
111
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* xbintsc runtime — a subset of `Proxy`.
|
|
3
|
+
*
|
|
4
|
+
* A proxy is an ordinary host view (`xt_object` with hooks): its target and
|
|
5
|
+
* handler live in hidden symbol-keyed properties, so the collector traces them
|
|
6
|
+
* like any other property, and the existing hook dispatch in `xt_object_get`,
|
|
7
|
+
* `xt_object_set`, `xt_object_has`, `xt_delete` and `xt_object_keys` runs the
|
|
8
|
+
* traps. Own and prototype lookups on the proxy object itself stay empty, so
|
|
9
|
+
* every string-keyed read and write misses and reaches the hooks.
|
|
10
|
+
*
|
|
11
|
+
* Supported traps: `get`, `set`, `has`, `deleteProperty`, `ownKeys`. Without a
|
|
12
|
+
* trap the operation is forwarded to the target. Not supported (and documented
|
|
13
|
+
* in doc/unimplemented/expressions.md): function targets (`apply`/`construct`),
|
|
14
|
+
* `Object.getPrototypeOf`/`setPrototypeOf` traps, property-descriptor traps
|
|
15
|
+
* (`getOwnPropertyDescriptor`, `defineProperty`, `isExtensible`, …) and
|
|
16
|
+
* `Proxy.revocable`.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
#include "rt_internal.h"
|
|
20
|
+
|
|
21
|
+
#include <string.h>
|
|
22
|
+
|
|
23
|
+
/* Hidden own properties; symbol keys keep them out of `Object.keys`, JSON and
|
|
24
|
+
* ordinary string-keyed reads. */
|
|
25
|
+
static xt_value xt_proxy_target_key = XT_UNDEFINED;
|
|
26
|
+
static xt_value xt_proxy_handler_key = XT_UNDEFINED;
|
|
27
|
+
|
|
28
|
+
static xt_value xt_proxy_state_key(const char *name, xt_value *slot) {
|
|
29
|
+
if (!xt_is_symbol(*slot)) {
|
|
30
|
+
*slot = xt_symbol_new(xt_string_from_cstr(name));
|
|
31
|
+
xt_gc_add_root(slot);
|
|
32
|
+
}
|
|
33
|
+
return *slot;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
static xt_value xt_proxy_target(xt_value proxy) {
|
|
37
|
+
return xt_object_get(proxy, xt_proxy_state_key("__xt_proxy_target", &xt_proxy_target_key));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
static xt_value xt_proxy_handler(xt_value proxy) {
|
|
41
|
+
return xt_object_get(proxy, xt_proxy_state_key("__xt_proxy_handler", &xt_proxy_handler_key));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/* The handler's function for `name`, or 0 when the trap is absent. */
|
|
45
|
+
static int xt_proxy_trap(xt_value proxy, const char *name, xt_value *trap) {
|
|
46
|
+
xt_value handler = xt_proxy_handler(proxy);
|
|
47
|
+
if (!XT_IS_OBJECT(handler)) return 0;
|
|
48
|
+
xt_value value = xt_object_get(handler, xt_string_from_cstr(name));
|
|
49
|
+
if (!XT_IS_FUNCTION(value)) return 0;
|
|
50
|
+
*trap = value;
|
|
51
|
+
return 1;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/* -- hooks ---------------------------------------------------------------- */
|
|
55
|
+
|
|
56
|
+
static xt_value xt_proxy_hook_get(xt_value self, xt_value key, void *data) {
|
|
57
|
+
(void)data;
|
|
58
|
+
xt_value target = xt_proxy_target(self);
|
|
59
|
+
xt_value trap;
|
|
60
|
+
if (!xt_proxy_trap(self, "get", &trap)) return xt_object_get(target, key);
|
|
61
|
+
xt_value arguments[3] = {target, key, self};
|
|
62
|
+
return xt_call_with_this(trap, xt_proxy_handler(self), 3, arguments);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
static int xt_proxy_hook_set(xt_value self, xt_value key, xt_value value, void *data) {
|
|
66
|
+
(void)data;
|
|
67
|
+
xt_value target = xt_proxy_target(self);
|
|
68
|
+
xt_value trap;
|
|
69
|
+
if (!xt_proxy_trap(self, "set", &trap)) {
|
|
70
|
+
xt_object_set(target, key, value);
|
|
71
|
+
return 1;
|
|
72
|
+
}
|
|
73
|
+
xt_value arguments[4] = {target, key, value, self};
|
|
74
|
+
xt_call_with_this(trap, xt_proxy_handler(self), 4, arguments);
|
|
75
|
+
/* A falsy trap result would fail the assignment in strict mode; the runtime
|
|
76
|
+
* has no strict/sloppy distinction, so a handled write always succeeds. */
|
|
77
|
+
return 1;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
static int xt_proxy_hook_has(xt_value self, xt_value key, void *data) {
|
|
81
|
+
(void)data;
|
|
82
|
+
xt_value target = xt_proxy_target(self);
|
|
83
|
+
xt_value trap;
|
|
84
|
+
if (!xt_proxy_trap(self, "has", &trap)) return xt_truthy(xt_object_has(target, key)) ? 1 : 0;
|
|
85
|
+
xt_value arguments[2] = {target, key};
|
|
86
|
+
return xt_truthy(xt_call_with_this(trap, xt_proxy_handler(self), 2, arguments)) ? 1 : 0;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
static int xt_proxy_hook_remove(xt_value self, xt_value key, void *data) {
|
|
90
|
+
(void)data;
|
|
91
|
+
xt_value target = xt_proxy_target(self);
|
|
92
|
+
xt_value trap;
|
|
93
|
+
if (!xt_proxy_trap(self, "deleteProperty", &trap)) {
|
|
94
|
+
xt_delete(target, key);
|
|
95
|
+
return 1;
|
|
96
|
+
}
|
|
97
|
+
xt_value arguments[2] = {target, key};
|
|
98
|
+
xt_call_with_this(trap, xt_proxy_handler(self), 2, arguments);
|
|
99
|
+
return 1;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
static xt_value xt_proxy_hook_keys(xt_value self, void *data) {
|
|
103
|
+
(void)data;
|
|
104
|
+
xt_value target = xt_proxy_target(self);
|
|
105
|
+
xt_value trap;
|
|
106
|
+
if (!xt_proxy_trap(self, "ownKeys", &trap)) return xt_object_keys(target);
|
|
107
|
+
xt_value arguments[1] = {target};
|
|
108
|
+
return xt_call_with_this(trap, xt_proxy_handler(self), 1, arguments);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
static const xt_object_hooks xt_proxy_hooks = {
|
|
112
|
+
xt_proxy_hook_get,
|
|
113
|
+
xt_proxy_hook_set,
|
|
114
|
+
xt_proxy_hook_has,
|
|
115
|
+
xt_proxy_hook_remove,
|
|
116
|
+
xt_proxy_hook_keys,
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
/* -- construction --------------------------------------------------------- */
|
|
120
|
+
|
|
121
|
+
xt_value xt_proxy_ctor(int32_t argc, xt_value *argv) {
|
|
122
|
+
xt_value target = xt_arg(argc, argv, 0);
|
|
123
|
+
xt_value handler = xt_arg(argc, argv, 1);
|
|
124
|
+
if (!XT_IS_OBJECT(target) && !XT_IS_FUNCTION(target)) {
|
|
125
|
+
xt_throw(xt_error_from_message("TypeError: Cannot create proxy with a non-object as target"));
|
|
126
|
+
return XT_UNDEFINED;
|
|
127
|
+
}
|
|
128
|
+
if (!XT_IS_OBJECT(handler)) {
|
|
129
|
+
xt_throw(xt_error_from_message("TypeError: Cannot create proxy with a non-object as handler"));
|
|
130
|
+
return XT_UNDEFINED;
|
|
131
|
+
}
|
|
132
|
+
xt_value proxy = xt_object_new();
|
|
133
|
+
/* The proxy inherits the target's prototype: `instanceof` and inherited
|
|
134
|
+
* methods behave like the target's. A `getPrototypeOf` trap is not modelled. */
|
|
135
|
+
xt_object_set_prototype(proxy, xt_object_get_prototype(target));
|
|
136
|
+
xt_object_set(proxy, xt_proxy_state_key("__xt_proxy_target", &xt_proxy_target_key), target);
|
|
137
|
+
xt_object_set(proxy, xt_proxy_state_key("__xt_proxy_handler", &xt_proxy_handler_key), handler);
|
|
138
|
+
xt_object_set_hooks(proxy, &xt_proxy_hooks, NULL);
|
|
139
|
+
return proxy;
|
|
140
|
+
}
|
|
@@ -266,6 +266,92 @@ xt_value xt_method_value(xt_value target, xt_value name) {
|
|
|
266
266
|
return closure;
|
|
267
267
|
}
|
|
268
268
|
|
|
269
|
+
/* -- prototype method values ---------------------------------------------- */
|
|
270
|
+
|
|
271
|
+
/*
|
|
272
|
+
* `Map.prototype.get`, `String.prototype.slice`, `Promise.prototype.then` are
|
|
273
|
+
* ordinary values in JavaScript, and libraries uncurry them:
|
|
274
|
+
*
|
|
275
|
+
* const get = Map.prototype.get.bind(cache);
|
|
276
|
+
* const toLower = Function.call.bind(String.prototype.toLowerCase);
|
|
277
|
+
*
|
|
278
|
+
* Instances already expose their methods through `xt_method_value` (keyed by
|
|
279
|
+
* the receiver's kind), but a prototype object is a plain object, so the lookup
|
|
280
|
+
* found nothing and `.bind` was read from `undefined`. Install the same table
|
|
281
|
+
* on the prototype as unbound closures; the receiver then arrives through
|
|
282
|
+
* `this`, exactly like the specification.
|
|
283
|
+
*
|
|
284
|
+
* `kind` is the constructor name the prototype belongs to.
|
|
285
|
+
*/
|
|
286
|
+
static const xt_method_spec *xt_proto_method_specs(const char *kind, size_t *count) {
|
|
287
|
+
if (!kind) return NULL;
|
|
288
|
+
#define XT_PROTO_SPECS(name, table) \
|
|
289
|
+
if (strcmp(kind, name) == 0) { \
|
|
290
|
+
*count = XT_NAME_COUNT(table); \
|
|
291
|
+
return table; \
|
|
292
|
+
}
|
|
293
|
+
XT_PROTO_SPECS("Array", XT_ARRAY_METHODS)
|
|
294
|
+
XT_PROTO_SPECS("String", XT_STRING_METHODS)
|
|
295
|
+
XT_PROTO_SPECS("Function", XT_FUNCTION_METHODS)
|
|
296
|
+
XT_PROTO_SPECS("BigInt", XT_BIGINT_METHODS)
|
|
297
|
+
XT_PROTO_SPECS("Number", XT_NUMBER_METHODS)
|
|
298
|
+
XT_PROTO_SPECS("Boolean", XT_BOOLEAN_METHODS)
|
|
299
|
+
XT_PROTO_SPECS("Date", XT_DATE_METHODS)
|
|
300
|
+
XT_PROTO_SPECS("Map", XT_MAP_METHODS)
|
|
301
|
+
XT_PROTO_SPECS("Set", XT_SET_METHODS)
|
|
302
|
+
XT_PROTO_SPECS("WeakMap", XT_WEAKMAP_METHODS)
|
|
303
|
+
XT_PROTO_SPECS("WeakSet", XT_WEAKSET_METHODS)
|
|
304
|
+
XT_PROTO_SPECS("WeakRef", XT_WEAKREF_METHODS)
|
|
305
|
+
XT_PROTO_SPECS("RegExp", XT_REGEXP_METHODS)
|
|
306
|
+
XT_PROTO_SPECS("Promise", XT_PROMISE_METHODS)
|
|
307
|
+
XT_PROTO_SPECS("Error", XT_ERROR_METHODS)
|
|
308
|
+
XT_PROTO_SPECS("Symbol", XT_SYMBOL_METHODS)
|
|
309
|
+
XT_PROTO_SPECS("Generator", XT_GENERATOR_METHODS)
|
|
310
|
+
#undef XT_PROTO_SPECS
|
|
311
|
+
return NULL;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
void xt_install_proto_methods(xt_value proto, const char *kind) {
|
|
315
|
+
size_t count = 0;
|
|
316
|
+
const xt_method_spec *specs = xt_proto_method_specs(kind, &count);
|
|
317
|
+
if (!specs || !XT_IS_OBJECT(proto)) return;
|
|
318
|
+
for (size_t i = 0; i < count; i++) {
|
|
319
|
+
xt_value name = xt_string_from_cstr(specs[i].name);
|
|
320
|
+
xt_value closure = xt_closure_new((void *)xt_method_value_invoke, 1, &name);
|
|
321
|
+
xt_function_set_metadata(closure, name, specs[i].arity);
|
|
322
|
+
xt_object_set(proto, name, closure);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/* `Function.prototype[Symbol.hasInstance]`: `ctor[Symbol.hasInstance](value)`
|
|
327
|
+
* and `value instanceof ctor` share one implementation. */
|
|
328
|
+
static xt_value xt_function_has_instance(xt_value thisValue, xt_value env, int32_t argc,
|
|
329
|
+
xt_value *argv) {
|
|
330
|
+
(void)env;
|
|
331
|
+
if (argc < 1) return xt_bool(0);
|
|
332
|
+
return xt_instance_of(argv[0], thisValue);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
xt_value xt_function_has_instance_method(void) {
|
|
336
|
+
static xt_value cached = 0;
|
|
337
|
+
if (cached == 0) {
|
|
338
|
+
cached = xt_closure_new((void *)xt_function_has_instance, 0, NULL);
|
|
339
|
+
xt_function_set_metadata(cached, xt_string_from_cstr("[Symbol.hasInstance]"), 1);
|
|
340
|
+
xt_gc_add_root(&cached);
|
|
341
|
+
}
|
|
342
|
+
return cached;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/* Source text is not retained, so a function's `toString()` reports a
|
|
346
|
+
* native-style representation of its name. */
|
|
347
|
+
static xt_value xt_function_source_text(xt_value target) {
|
|
348
|
+
xt_function *self = (xt_function *)XT_GET_PTR(target);
|
|
349
|
+
const char *selfName = self->name ? xt_string_bytes(self->name) : "";
|
|
350
|
+
char buffer[256];
|
|
351
|
+
snprintf(buffer, sizeof(buffer), "function %s() { [native code] }", selfName);
|
|
352
|
+
return xt_string_from_cstr(buffer);
|
|
353
|
+
}
|
|
354
|
+
|
|
269
355
|
xt_value xt_call_method(xt_value target, xt_value name, int32_t argc, xt_value *argv) {
|
|
270
356
|
/* A symbol-named method (`obj[Symbol.iterator]()`) is never a built-in, so
|
|
271
357
|
* resolve it as an ordinary property and call it with `target` as `this`. */
|
|
@@ -372,7 +458,17 @@ xt_value xt_call_method(xt_value target, xt_value name, int32_t argc, xt_value *
|
|
|
372
458
|
return bound;
|
|
373
459
|
}
|
|
374
460
|
xt_value fn = xt_object_get(target, name);
|
|
375
|
-
if (XT_IS_FUNCTION(fn))
|
|
461
|
+
if (XT_IS_FUNCTION(fn)) {
|
|
462
|
+
/* `fn.toString()` resolves to `Function.prototype.toString`, whose body
|
|
463
|
+
* dispatches back through this function (`xt_method_value_invoke` →
|
|
464
|
+
* `xt_call_method`) — calling it would recurse until the stack dies.
|
|
465
|
+
* Recognise the prototype's own method and answer the native-style
|
|
466
|
+
* source here; a function's own `toString` still takes the normal path. */
|
|
467
|
+
if (strcmp(method, "toString") == 0 && fn == xt_object_get(xt_function_prototype(), name)) {
|
|
468
|
+
return xt_function_source_text(target);
|
|
469
|
+
}
|
|
470
|
+
return xt_call_with_this(fn, target, argc, argv);
|
|
471
|
+
}
|
|
376
472
|
/* Statics of a first-class built-in constructor (`const A = Array;
|
|
377
473
|
* A.isArray(x)`). */
|
|
378
474
|
xt_value builtinStatic = xt_builtin_static_value(target, name);
|
|
@@ -380,14 +476,7 @@ xt_value xt_call_method(xt_value target, xt_value name, int32_t argc, xt_value *
|
|
|
380
476
|
if (XT_IS_FUNCTION(builtinStatic)) return xt_call_with_this(builtinStatic, target, argc, argv);
|
|
381
477
|
return builtinStatic;
|
|
382
478
|
}
|
|
383
|
-
if (strcmp(method, "toString") == 0)
|
|
384
|
-
/* Source text is not retained; report a native-style representation. */
|
|
385
|
-
xt_function *self = (xt_function *)XT_GET_PTR(target);
|
|
386
|
-
const char *selfName = self->name ? xt_string_bytes(self->name) : "";
|
|
387
|
-
char buffer[256];
|
|
388
|
-
snprintf(buffer, sizeof(buffer), "function %s() { [native code] }", selfName);
|
|
389
|
-
return xt_string_from_cstr(buffer);
|
|
390
|
-
}
|
|
479
|
+
if (strcmp(method, "toString") == 0) return xt_function_source_text(target);
|
|
391
480
|
{
|
|
392
481
|
char message[256];
|
|
393
482
|
snprintf(message, sizeof(message), "TypeError: function has no callable method '%s'", method);
|
|
@@ -387,7 +387,13 @@ static xt_value xt_date_method(xt_value target, const char *method, int32_t argc
|
|
|
387
387
|
tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)fmod(date->time, 1000.0));
|
|
388
388
|
return xt_string_from_cstr(buffer);
|
|
389
389
|
}
|
|
390
|
-
|
|
390
|
+
/* `toJSON` is the JSON hook: an invalid date serializes as `null` instead of
|
|
391
|
+
* throwing the way `toISOString` does. */
|
|
392
|
+
if (strcmp(method, "toJSON") == 0) {
|
|
393
|
+
if (isnan(date->time)) return XT_NULL;
|
|
394
|
+
return xt_call_method(target, xt_string_from_cstr("toISOString"), 0, NULL);
|
|
395
|
+
}
|
|
396
|
+
if (strcmp(method, "toString") == 0) return xt_call_method(target, xt_string_from_cstr("toISOString"), 0, NULL);
|
|
391
397
|
*handled = 0;
|
|
392
398
|
return XT_UNDEFINED;
|
|
393
399
|
}
|
|
@@ -3,3 +3,31 @@
|
|
|
3
3
|
* each stays readable; `xt_stdlib2.c` includes this file. */
|
|
4
4
|
#include "json_parse.inc"
|
|
5
5
|
#include "json_stringify.inc"
|
|
6
|
+
|
|
7
|
+
/* The `JSON` namespace as a first-class value: `const stringify = JSON.stringify`.
|
|
8
|
+
* Call sites lower straight to `xt_json_parse` / `xt_json_stringify`; this
|
|
9
|
+
* object exists for programs that read the namespace or destructure it. */
|
|
10
|
+
static xt_value xt_json_bridge(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
|
|
11
|
+
(void)thisValue;
|
|
12
|
+
const char *method = xt_string_data(xt_closure_env(env, 0));
|
|
13
|
+
if (method && strcmp(method, "parse") == 0) return xt_json_parse(argc, argv);
|
|
14
|
+
if (method && strcmp(method, "stringify") == 0) return xt_json_stringify(argc, argv);
|
|
15
|
+
return XT_UNDEFINED;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
xt_value xt_json_object(int32_t argc, xt_value *argv) {
|
|
19
|
+
(void)argc;
|
|
20
|
+
(void)argv;
|
|
21
|
+
static xt_value object = 0;
|
|
22
|
+
if (object != 0) return object;
|
|
23
|
+
object = xt_object_new();
|
|
24
|
+
xt_gc_add_root(&object);
|
|
25
|
+
static const char *const methods[] = {"parse", "stringify"};
|
|
26
|
+
for (size_t i = 0; i < sizeof(methods) / sizeof(methods[0]); i++) {
|
|
27
|
+
xt_value name = xt_string_from_cstr(methods[i]);
|
|
28
|
+
xt_value closure = xt_closure_new((void *)xt_json_bridge, 1, &name);
|
|
29
|
+
xt_function_set_metadata(closure, name, methods[i][0] == 'p' ? 1 : 1);
|
|
30
|
+
xt_object_set(object, name, closure);
|
|
31
|
+
}
|
|
32
|
+
return object;
|
|
33
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
|
|
2
|
-
static
|
|
2
|
+
static int xt_json_stringify_value(xt_value value, xt_value key, xt_value indent, int depth,
|
|
3
|
+
xt_value *out);
|
|
3
4
|
|
|
4
5
|
/* The indentation unit for the `space` argument: up to 10 spaces for a
|
|
5
6
|
* number, or the first 10 UTF-16 code units of a string (any characters,
|
|
@@ -69,15 +70,35 @@ static void xt_json_leave(void) {
|
|
|
69
70
|
if (xt_json_path_count > 0) xt_json_path_count--;
|
|
70
71
|
}
|
|
71
72
|
|
|
73
|
+
/* SerializeJSONProperty step: when the value is an object it may define
|
|
74
|
+
* `toJSON`, which runs with the property key before anything else is inspected.
|
|
75
|
+
* Every object with a JSON form relies on this (Date included — its
|
|
76
|
+
* `toJSON` is what turns it into a string), so the fast path no longer has to
|
|
77
|
+
* special-case individual kinds. */
|
|
78
|
+
static void xt_json_apply_to_json(xt_value *value, xt_value key) {
|
|
79
|
+
if (!XT_IS_OBJECT(*value) && !XT_IS_FUNCTION(*value)) return;
|
|
80
|
+
xt_value toJSON = xt_get(*value, xt_string_from_cstr("toJSON"));
|
|
81
|
+
if (!XT_IS_FUNCTION(toJSON)) return;
|
|
82
|
+
xt_value args[1] = {key};
|
|
83
|
+
*value = xt_call_with_this(toJSON, *value, 1, args);
|
|
84
|
+
}
|
|
85
|
+
|
|
72
86
|
static void xt_json_stringify_array(xt_value value, xt_value indent, int depth, xt_value *out) {
|
|
73
87
|
xt_array *array = xt_as_array(value);
|
|
74
88
|
if (!array || array->length == 0) { *out = xt_add(*out, xt_string_from_cstr("[]")); return; }
|
|
75
|
-
*out = xt_add(*out, xt_string_from_cstr("["));
|
|
76
89
|
xt_json_enter(value);
|
|
90
|
+
*out = xt_add(*out, xt_string_from_cstr("["));
|
|
77
91
|
for (uint32_t i = 0; i < array->length; i++) {
|
|
78
92
|
if (i > 0) *out = xt_add(*out, xt_string_from_cstr(","));
|
|
79
93
|
xt_json_indent(out, indent, depth + 1);
|
|
80
|
-
|
|
94
|
+
/* An array index is the key `toJSON` sees, and the element that serializes
|
|
95
|
+
* to nothing (`undefined`, a function, a symbol) becomes `null`. */
|
|
96
|
+
char keyBuffer[16];
|
|
97
|
+
snprintf(keyBuffer, sizeof(keyBuffer), "%u", i);
|
|
98
|
+
xt_value item = xt_string_from_cstr("");
|
|
99
|
+
int wrote = xt_json_stringify_value(array->items[i], xt_string_from_cstr(keyBuffer), indent,
|
|
100
|
+
depth + 1, &item);
|
|
101
|
+
*out = xt_add(*out, wrote ? item : xt_string_from_cstr("null"));
|
|
81
102
|
}
|
|
82
103
|
xt_json_indent(out, indent, depth);
|
|
83
104
|
xt_json_leave();
|
|
@@ -87,50 +108,50 @@ static void xt_json_stringify_array(xt_value value, xt_value indent, int depth,
|
|
|
87
108
|
static void xt_json_stringify_object(xt_value value, xt_value indent, int depth, xt_value *out) {
|
|
88
109
|
xt_value keys = xt_object_keys(value);
|
|
89
110
|
xt_array *array = xt_as_array(keys);
|
|
90
|
-
|
|
91
|
-
int any = 0;
|
|
92
|
-
for (uint32_t i = 0; i < array->length; i++) {
|
|
93
|
-
xt_value item = xt_get(value, array->items[i]);
|
|
94
|
-
if (item != XT_UNDEFINED && !XT_IS_FUNCTION(item) && !xt_is_symbol(item)) { any = 1; break; }
|
|
95
|
-
}
|
|
96
|
-
if (!any) { *out = xt_add(*out, xt_string_from_cstr("{}")); return; }
|
|
97
|
-
*out = xt_add(*out, xt_string_from_cstr("{"));
|
|
98
|
-
xt_json_enter(value);
|
|
111
|
+
xt_value body = xt_string_from_cstr("");
|
|
99
112
|
int written = 0;
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
+
xt_json_enter(value);
|
|
114
|
+
if (array) {
|
|
115
|
+
for (uint32_t i = 0; i < array->length; i++) {
|
|
116
|
+
xt_value key = array->items[i];
|
|
117
|
+
xt_value item = xt_string_from_cstr("");
|
|
118
|
+
/* `undefined`, functions and symbols are omitted from objects - including
|
|
119
|
+
* a value whose `toJSON` returned one, which is why the decision is made
|
|
120
|
+
* after serializing the child. */
|
|
121
|
+
if (!xt_json_stringify_value(xt_get(value, key), key, indent, depth + 1, &item)) continue;
|
|
122
|
+
if (written > 0) body = xt_add(body, xt_string_from_cstr(","));
|
|
123
|
+
written++;
|
|
124
|
+
xt_json_indent(&body, indent, depth + 1);
|
|
125
|
+
body = xt_add(body, xt_string_from_cstr("\""));
|
|
126
|
+
body = xt_add(body, key);
|
|
127
|
+
body = xt_add(body, xt_string_from_cstr("\":"));
|
|
128
|
+
if (xt_json_indented(indent)) body = xt_add(body, xt_string_from_cstr(" "));
|
|
129
|
+
body = xt_add(body, item);
|
|
130
|
+
}
|
|
113
131
|
}
|
|
114
|
-
xt_json_indent(out, indent, depth);
|
|
115
132
|
xt_json_leave();
|
|
133
|
+
if (written == 0) { *out = xt_add(*out, xt_string_from_cstr("{}")); return; }
|
|
134
|
+
*out = xt_add(*out, xt_string_from_cstr("{"));
|
|
135
|
+
*out = xt_add(*out, body);
|
|
136
|
+
xt_json_indent(out, indent, depth);
|
|
116
137
|
*out = xt_add(*out, xt_string_from_cstr("}"));
|
|
117
138
|
}
|
|
118
139
|
|
|
119
|
-
static
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
}
|
|
140
|
+
static int xt_json_stringify_value(xt_value value, xt_value key, xt_value indent, int depth,
|
|
141
|
+
xt_value *out) {
|
|
142
|
+
xt_json_apply_to_json(&value, key);
|
|
143
|
+
if (value == XT_UNDEFINED || XT_IS_FUNCTION(value) || xt_is_symbol(value)) return 0;
|
|
144
|
+
if (value == XT_NULL) { *out = xt_add(*out, xt_string_from_cstr("null")); return 1; }
|
|
124
145
|
if (XT_IS_BIGINT(value)) {
|
|
125
146
|
xt_throw(xt_error_from_message("TypeError: Do not know how to serialize a BigInt"));
|
|
126
|
-
return;
|
|
147
|
+
return 0;
|
|
127
148
|
}
|
|
128
|
-
if (XT_IS_BOOL(value)) { *out = xt_add(*out, value == XT_TRUE ? xt_string_from_cstr("true") : xt_string_from_cstr("false")); return; }
|
|
149
|
+
if (XT_IS_BOOL(value)) { *out = xt_add(*out, value == XT_TRUE ? xt_string_from_cstr("true") : xt_string_from_cstr("false")); return 1; }
|
|
129
150
|
if (XT_IS_NUMBER(value)) {
|
|
130
151
|
double d = xt_to_double(value);
|
|
131
152
|
if (!isfinite(d)) *out = xt_add(*out, xt_string_from_cstr("null"));
|
|
132
153
|
else *out = xt_add(*out, xt_to_string(value));
|
|
133
|
-
return;
|
|
154
|
+
return 1;
|
|
134
155
|
}
|
|
135
156
|
if (XT_IS_STRING(value)) {
|
|
136
157
|
xt_string *s = xt_as_string(value);
|
|
@@ -158,14 +179,14 @@ static void xt_json_stringify_value(xt_value value, xt_value indent, int depth,
|
|
|
158
179
|
offset += width;
|
|
159
180
|
}
|
|
160
181
|
*out = xt_add(*out, xt_string_from_cstr("\""));
|
|
161
|
-
return;
|
|
182
|
+
return 1;
|
|
162
183
|
}
|
|
163
|
-
if (XT_IS_ARRAY(value)) { xt_json_stringify_array(value, indent, depth, out); return; }
|
|
184
|
+
if (XT_IS_ARRAY(value)) { xt_json_stringify_array(value, indent, depth, out); return 1; }
|
|
164
185
|
if (XT_IS_OBJECT(value)) {
|
|
165
|
-
/* Map/Set/
|
|
186
|
+
/* Map/Set/RegExp/Promise/generators are not property-bag objects;
|
|
166
187
|
* handing them to xt_json_stringify_object reads their fields as a property
|
|
167
188
|
* table and crashed with an access violation. JSON sees no enumerable own
|
|
168
|
-
* properties
|
|
189
|
+
* properties, so they serialize as `{}` unless they define `toJSON`. */
|
|
169
190
|
xt_object *object = (xt_object *)XT_GET_PTR(value);
|
|
170
191
|
switch (object->header.kind) {
|
|
171
192
|
case XT_OBJECT_KIND_MAP:
|
|
@@ -176,24 +197,21 @@ static void xt_json_stringify_value(xt_value value, xt_value indent, int depth,
|
|
|
176
197
|
case XT_OBJECT_KIND_ITERATOR:
|
|
177
198
|
case XT_OBJECT_KIND_ASYNC:
|
|
178
199
|
*out = xt_add(*out, xt_string_from_cstr("{}"));
|
|
179
|
-
return;
|
|
180
|
-
case XT_OBJECT_KIND_DATE:
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
xt_value text = xt_call_method(value, xt_string_from_cstr("toJSON"), 0, NULL);
|
|
187
|
-
xt_json_stringify_value(text, indent, depth, out);
|
|
188
|
-
return;
|
|
189
|
-
}
|
|
200
|
+
return 1;
|
|
201
|
+
case XT_OBJECT_KIND_DATE:
|
|
202
|
+
/* Only reachable when `toJSON` was deleted or shadowed: a Date has no
|
|
203
|
+
* enumerable own properties, and reading its struct as a property table
|
|
204
|
+
* is unsafe. */
|
|
205
|
+
*out = xt_add(*out, xt_string_from_cstr("{}"));
|
|
206
|
+
return 1;
|
|
190
207
|
default:
|
|
191
208
|
break;
|
|
192
209
|
}
|
|
193
210
|
xt_json_stringify_object(value, indent, depth, out);
|
|
194
|
-
return;
|
|
211
|
+
return 1;
|
|
195
212
|
}
|
|
196
213
|
*out = xt_add(*out, xt_string_from_cstr("null"));
|
|
214
|
+
return 1;
|
|
197
215
|
}
|
|
198
216
|
|
|
199
217
|
/* -- replacer-aware stringify -------------------------------------------- */
|
|
@@ -338,8 +356,7 @@ static int xt_json_replacer_write(xt_json_replacer_context *ctx, xt_value holder
|
|
|
338
356
|
}
|
|
339
357
|
return xt_json_replacer_container(ctx, value, depth, out);
|
|
340
358
|
}
|
|
341
|
-
xt_json_stringify_value(value, ctx->indent, depth, out);
|
|
342
|
-
return 1;
|
|
359
|
+
return xt_json_stringify_value(value, key, ctx->indent, depth, out);
|
|
343
360
|
}
|
|
344
361
|
|
|
345
362
|
xt_value xt_json_stringify(int32_t argc, xt_value *argv) {
|
|
@@ -363,6 +380,6 @@ xt_value xt_json_stringify(int32_t argc, xt_value *argv) {
|
|
|
363
380
|
* top-level serialization with a clean path. */
|
|
364
381
|
xt_json_path_count = 0;
|
|
365
382
|
xt_value out = xt_string_from_cstr("");
|
|
366
|
-
xt_json_stringify_value(value, indent, 0, &out);
|
|
383
|
+
if (!xt_json_stringify_value(value, xt_string_from_cstr(""), indent, 0, &out)) return XT_UNDEFINED;
|
|
367
384
|
return out;
|
|
368
385
|
}
|
package/runtime/xt_web.h
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* xbintsc runtime — Web platform globals contributed by `xt_web_*.c`.
|
|
3
|
+
*
|
|
4
|
+
* Declared here rather than in `rt.h`: generated code calls the constructor and
|
|
5
|
+
* static entry points through IR declarations, while these prototypes are only
|
|
6
|
+
* needed by the runtime units that build constructor values from them.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
#ifndef XT_WEB_H
|
|
10
|
+
#define XT_WEB_H
|
|
11
|
+
|
|
12
|
+
#include "rt.h"
|
|
13
|
+
|
|
14
|
+
/* AbortController / AbortSignal (xt_web_abort.c). */
|
|
15
|
+
xt_value xt_abort_controller_ctor(int32_t argc, xt_value *argv);
|
|
16
|
+
xt_value xt_abort_signal_ctor(int32_t argc, xt_value *argv);
|
|
17
|
+
xt_value xt_abort_signal_static(xt_value method, int32_t argc, xt_value *argv);
|
|
18
|
+
xt_value xt_web_abort_signal_proto(void);
|
|
19
|
+
xt_value xt_web_abort_controller_proto(void);
|
|
20
|
+
|
|
21
|
+
/* Headers / Response (xt_fetch/webapi.inc). */
|
|
22
|
+
xt_value xt_headers_ctor(int32_t argc, xt_value *argv);
|
|
23
|
+
xt_value xt_response_ctor(int32_t argc, xt_value *argv);
|
|
24
|
+
xt_value xt_web_headers_proto(void);
|
|
25
|
+
xt_value xt_web_response_proto(void);
|
|
26
|
+
|
|
27
|
+
#endif /* XT_WEB_H */
|