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
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The callback-style `fs` API (`fs.realpath(path, cb)`, `fs.stat(...)`, …).
|
|
3
|
+
*
|
|
4
|
+
* Node's callback forms run on the same asynchronous I/O pool as
|
|
5
|
+
* `fs.promises`: each one forwards its arguments to the promise-returning
|
|
6
|
+
* implementation and attaches two closures, which call `callback(null, value)`
|
|
7
|
+
* on success and `callback(error)` on failure. Programs written against
|
|
8
|
+
* `require("fs")` callbacks — `proper-lockfile`, most of npm's tooling — work
|
|
9
|
+
* without a promise in sight.
|
|
10
|
+
*
|
|
11
|
+
* `exists` is the exception Node makes: its callback receives a boolean and no
|
|
12
|
+
* error argument.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
#include "async_job.h"
|
|
16
|
+
|
|
17
|
+
/* The promise-returning implementations (async_api.c). */
|
|
18
|
+
xt_value xt_node_p_read_file(int32_t argc, xt_value *argv);
|
|
19
|
+
xt_value xt_node_p_write_file(int32_t argc, xt_value *argv);
|
|
20
|
+
xt_value xt_node_p_append_file(int32_t argc, xt_value *argv);
|
|
21
|
+
xt_value xt_node_p_mkdir(int32_t argc, xt_value *argv);
|
|
22
|
+
xt_value xt_node_p_readdir(int32_t argc, xt_value *argv);
|
|
23
|
+
xt_value xt_node_p_rm(int32_t argc, xt_value *argv);
|
|
24
|
+
xt_value xt_node_p_unlink(int32_t argc, xt_value *argv);
|
|
25
|
+
xt_value xt_node_p_rmdir(int32_t argc, xt_value *argv);
|
|
26
|
+
xt_value xt_node_p_rename(int32_t argc, xt_value *argv);
|
|
27
|
+
xt_value xt_node_p_copy_file(int32_t argc, xt_value *argv);
|
|
28
|
+
xt_value xt_node_p_cp(int32_t argc, xt_value *argv);
|
|
29
|
+
xt_value xt_node_p_realpath(int32_t argc, xt_value *argv);
|
|
30
|
+
xt_value xt_node_p_stat(int32_t argc, xt_value *argv);
|
|
31
|
+
xt_value xt_node_p_lstat(int32_t argc, xt_value *argv);
|
|
32
|
+
xt_value xt_node_p_statfs(int32_t argc, xt_value *argv);
|
|
33
|
+
xt_value xt_node_p_access(int32_t argc, xt_value *argv);
|
|
34
|
+
xt_value xt_node_p_open(int32_t argc, xt_value *argv);
|
|
35
|
+
xt_value xt_node_p_chmod(int32_t argc, xt_value *argv);
|
|
36
|
+
xt_value xt_node_p_lchmod(int32_t argc, xt_value *argv);
|
|
37
|
+
xt_value xt_node_p_chown(int32_t argc, xt_value *argv);
|
|
38
|
+
xt_value xt_node_p_lchown(int32_t argc, xt_value *argv);
|
|
39
|
+
xt_value xt_node_p_truncate(int32_t argc, xt_value *argv);
|
|
40
|
+
xt_value xt_node_p_utimes(int32_t argc, xt_value *argv);
|
|
41
|
+
xt_value xt_node_p_lutimes(int32_t argc, xt_value *argv);
|
|
42
|
+
xt_value xt_node_p_link(int32_t argc, xt_value *argv);
|
|
43
|
+
xt_value xt_node_p_symlink(int32_t argc, xt_value *argv);
|
|
44
|
+
xt_value xt_node_p_readlink(int32_t argc, xt_value *argv);
|
|
45
|
+
xt_value xt_node_p_mkdtemp(int32_t argc, xt_value *argv);
|
|
46
|
+
xt_value xt_node_p_opendir(int32_t argc, xt_value *argv);
|
|
47
|
+
xt_value xt_node_p_glob(int32_t argc, xt_value *argv);
|
|
48
|
+
|
|
49
|
+
/* `existsSync` from fs_ops.c, for the one callback with a different shape. */
|
|
50
|
+
xt_value xt_node_exists(int32_t argc, xt_value *argv);
|
|
51
|
+
|
|
52
|
+
/* -- the two handlers ----------------------------------------------------- */
|
|
53
|
+
|
|
54
|
+
static xt_value xt_fs_cb_done(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
|
|
55
|
+
(void)thisValue;
|
|
56
|
+
xt_value callback = xt_closure_env(env, 0);
|
|
57
|
+
xt_value arguments[2] = {XT_NULL, xt_arg(argc, argv, 0)};
|
|
58
|
+
xt_call_with_this(callback, XT_UNDEFINED, 2, arguments);
|
|
59
|
+
return XT_UNDEFINED;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
static xt_value xt_fs_cb_failed(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
|
|
63
|
+
(void)thisValue;
|
|
64
|
+
xt_value callback = xt_closure_env(env, 0);
|
|
65
|
+
xt_value arguments[1] = {xt_arg(argc, argv, 0)};
|
|
66
|
+
xt_call_with_this(callback, XT_UNDEFINED, 1, arguments);
|
|
67
|
+
return XT_UNDEFINED;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
static xt_value xt_fs_callbackify(xt_value promise, xt_value callback) {
|
|
71
|
+
xt_value captured[1] = {callback};
|
|
72
|
+
xt_value handlers[2] = {
|
|
73
|
+
xt_closure_new((void *)xt_fs_cb_done, 1, captured),
|
|
74
|
+
xt_closure_new((void *)xt_fs_cb_failed, 1, captured),
|
|
75
|
+
};
|
|
76
|
+
if (!xt_is_promise(promise)) {
|
|
77
|
+
/* A rejected promise would have skipped the handler; nothing to attach. */
|
|
78
|
+
return XT_UNDEFINED;
|
|
79
|
+
}
|
|
80
|
+
int handled = 0;
|
|
81
|
+
xt_value chained = xt_promise_method(promise, "then", 2, handlers, &handled);
|
|
82
|
+
/* The callback consumed the result, so the derivative is not an unhandled
|
|
83
|
+
* rejection the program has to observe. */
|
|
84
|
+
if (xt_is_promise(chained)) xt_promise_observe_value(chained);
|
|
85
|
+
return XT_UNDEFINED;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Forward every argument but the trailing callback to `target`. */
|
|
89
|
+
static xt_value xt_fs_call_with_callback(xt_value (*target)(int32_t, xt_value *), int32_t argc,
|
|
90
|
+
xt_value *argv) {
|
|
91
|
+
if (argc < 1 || !XT_IS_FUNCTION(argv[argc - 1])) return target(argc, argv);
|
|
92
|
+
xt_value callback = argv[argc - 1];
|
|
93
|
+
return xt_fs_callbackify(target(argc - 1, argv), callback);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
#define XT_FS_CALLBACK(name) \
|
|
97
|
+
xt_value xt_node_##name##_cb(int32_t argc, xt_value *argv) { \
|
|
98
|
+
return xt_fs_call_with_callback(xt_node_p_##name, argc, argv); \
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
XT_FS_CALLBACK(read_file)
|
|
102
|
+
XT_FS_CALLBACK(write_file)
|
|
103
|
+
XT_FS_CALLBACK(append_file)
|
|
104
|
+
XT_FS_CALLBACK(mkdir)
|
|
105
|
+
XT_FS_CALLBACK(readdir)
|
|
106
|
+
XT_FS_CALLBACK(rm)
|
|
107
|
+
XT_FS_CALLBACK(unlink)
|
|
108
|
+
XT_FS_CALLBACK(rmdir)
|
|
109
|
+
XT_FS_CALLBACK(rename)
|
|
110
|
+
XT_FS_CALLBACK(copy_file)
|
|
111
|
+
XT_FS_CALLBACK(cp)
|
|
112
|
+
XT_FS_CALLBACK(realpath)
|
|
113
|
+
XT_FS_CALLBACK(stat)
|
|
114
|
+
XT_FS_CALLBACK(lstat)
|
|
115
|
+
XT_FS_CALLBACK(statfs)
|
|
116
|
+
XT_FS_CALLBACK(access)
|
|
117
|
+
XT_FS_CALLBACK(open)
|
|
118
|
+
XT_FS_CALLBACK(chmod)
|
|
119
|
+
XT_FS_CALLBACK(lchmod)
|
|
120
|
+
XT_FS_CALLBACK(chown)
|
|
121
|
+
XT_FS_CALLBACK(lchown)
|
|
122
|
+
XT_FS_CALLBACK(truncate)
|
|
123
|
+
XT_FS_CALLBACK(utimes)
|
|
124
|
+
XT_FS_CALLBACK(lutimes)
|
|
125
|
+
XT_FS_CALLBACK(link)
|
|
126
|
+
XT_FS_CALLBACK(symlink)
|
|
127
|
+
XT_FS_CALLBACK(readlink)
|
|
128
|
+
XT_FS_CALLBACK(mkdtemp)
|
|
129
|
+
XT_FS_CALLBACK(opendir)
|
|
130
|
+
XT_FS_CALLBACK(glob)
|
|
131
|
+
|
|
132
|
+
/* `fs.exists(path, cb)`: `cb(boolean)`, never an error. */
|
|
133
|
+
xt_value xt_node_exists_cb(int32_t argc, xt_value *argv) {
|
|
134
|
+
if (argc < 1 || !XT_IS_FUNCTION(argv[argc - 1])) return xt_node_exists(argc, argv);
|
|
135
|
+
xt_value callback = argv[argc - 1];
|
|
136
|
+
xt_value arguments[1] = {xt_node_exists(1, argv)};
|
|
137
|
+
return xt_call_with_this(callback, XT_UNDEFINED, 1, arguments);
|
|
138
|
+
}
|
|
@@ -335,6 +335,12 @@ static double xt_fs_seconds_ms(long seconds, long nanoseconds) {
|
|
|
335
335
|
return (double)seconds * 1000.0 + (double)nanoseconds / 1000000.0;
|
|
336
336
|
}
|
|
337
337
|
|
|
338
|
+
/** A `Date` value for a millisecond timestamp (`stat.mtime`, ...). */
|
|
339
|
+
static xt_value xt_fs_date_value(double milliseconds) {
|
|
340
|
+
xt_value argument = xt_number(milliseconds);
|
|
341
|
+
return xt_date_ctor(1, &argument);
|
|
342
|
+
}
|
|
343
|
+
|
|
338
344
|
xt_value xt_node_stat_result(const xt_fs_stat_t *info) {
|
|
339
345
|
xt_value object = xt_object_new();
|
|
340
346
|
xt_object_set(object, xt_string_from_cstr("size"), xt_number((double)info->st_size));
|
|
@@ -345,29 +351,36 @@ xt_value xt_node_stat_result(const xt_fs_stat_t *info) {
|
|
|
345
351
|
xt_object_set(object, xt_string_from_cstr("ino"), xt_number((double)info->st_ino));
|
|
346
352
|
xt_object_set(object, xt_string_from_cstr("nlink"), xt_number((double)info->st_nlink));
|
|
347
353
|
xt_object_set(object, xt_string_from_cstr("rdev"), xt_number((double)info->st_rdev));
|
|
354
|
+
double mtimeMs;
|
|
355
|
+
double atimeMs;
|
|
356
|
+
double ctimeMs;
|
|
348
357
|
#if defined(__APPLE__)
|
|
349
358
|
xt_object_set(object, xt_string_from_cstr("blocks"), xt_number((double)info->st_blocks));
|
|
350
359
|
xt_object_set(object, xt_string_from_cstr("blksize"), xt_number((double)info->st_blksize));
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
xt_number(xt_fs_seconds_ms(info->st_atimespec.tv_sec, info->st_atimespec.tv_nsec)));
|
|
355
|
-
xt_object_set(object, xt_string_from_cstr("ctimeMs"),
|
|
356
|
-
xt_number(xt_fs_seconds_ms(info->st_ctimespec.tv_sec, info->st_ctimespec.tv_nsec)));
|
|
360
|
+
mtimeMs = xt_fs_seconds_ms(info->st_mtimespec.tv_sec, info->st_mtimespec.tv_nsec);
|
|
361
|
+
atimeMs = xt_fs_seconds_ms(info->st_atimespec.tv_sec, info->st_atimespec.tv_nsec);
|
|
362
|
+
ctimeMs = xt_fs_seconds_ms(info->st_ctimespec.tv_sec, info->st_ctimespec.tv_nsec);
|
|
357
363
|
#elif defined(_WIN32)
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
364
|
+
mtimeMs = (double)info->st_mtime * 1000.0;
|
|
365
|
+
atimeMs = (double)info->st_atime * 1000.0;
|
|
366
|
+
ctimeMs = (double)info->st_ctime * 1000.0;
|
|
361
367
|
#else
|
|
362
368
|
xt_object_set(object, xt_string_from_cstr("blocks"), xt_number((double)info->st_blocks));
|
|
363
369
|
xt_object_set(object, xt_string_from_cstr("blksize"), xt_number((double)info->st_blksize));
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
xt_number(xt_fs_seconds_ms(info->st_atim.tv_sec, info->st_atim.tv_nsec)));
|
|
368
|
-
xt_object_set(object, xt_string_from_cstr("ctimeMs"),
|
|
369
|
-
xt_number(xt_fs_seconds_ms(info->st_ctim.tv_sec, info->st_ctim.tv_nsec)));
|
|
370
|
+
mtimeMs = xt_fs_seconds_ms(info->st_mtim.tv_sec, info->st_mtim.tv_nsec);
|
|
371
|
+
atimeMs = xt_fs_seconds_ms(info->st_atim.tv_sec, info->st_atim.tv_nsec);
|
|
372
|
+
ctimeMs = xt_fs_seconds_ms(info->st_ctim.tv_sec, info->st_ctim.tv_nsec);
|
|
370
373
|
#endif
|
|
374
|
+
xt_object_set(object, xt_string_from_cstr("mtimeMs"), xt_number(mtimeMs));
|
|
375
|
+
xt_object_set(object, xt_string_from_cstr("atimeMs"), xt_number(atimeMs));
|
|
376
|
+
xt_object_set(object, xt_string_from_cstr("ctimeMs"), xt_number(ctimeMs));
|
|
377
|
+
/* Node reports timestamps as `Date` objects too, and libraries call
|
|
378
|
+
* `stat.mtime.getTime()` (`proper-lockfile` probes the filesystem this way). */
|
|
379
|
+
xt_object_set(object, xt_string_from_cstr("mtime"), xt_fs_date_value(mtimeMs));
|
|
380
|
+
xt_object_set(object, xt_string_from_cstr("atime"), xt_fs_date_value(atimeMs));
|
|
381
|
+
xt_object_set(object, xt_string_from_cstr("ctime"), xt_fs_date_value(ctimeMs));
|
|
382
|
+
xt_object_set(object, xt_string_from_cstr("birthtime"), xt_fs_date_value(ctimeMs));
|
|
383
|
+
xt_object_set(object, xt_string_from_cstr("birthtimeMs"), xt_number(ctimeMs));
|
|
371
384
|
|
|
372
385
|
unsigned int mode = (unsigned int)info->st_mode;
|
|
373
386
|
xt_fs_define_stat_method(object, "isFile", mode);
|
|
@@ -1,27 +1,36 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* Node.js `module` module for xbintsc: `createRequire` and `isBuiltin`.
|
|
3
3
|
*
|
|
4
|
-
* A compiled binary has no
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
4
|
+
* A compiled binary has no module loader for arbitrary files, but the modules
|
|
5
|
+
* linked into it are known: the code generator registers a table of every
|
|
6
|
+
* enabled extension module's namespace object, so `require("fs")` (and
|
|
7
|
+
* `require("node:fs")`) answer from it. Anything else throws a clear error.
|
|
8
|
+
* `isBuiltin` reports whether a specifier is a known Node built-in, but since
|
|
9
|
+
* the binary already bundles every reachable module it conservatively returns
|
|
10
|
+
* false.
|
|
8
11
|
*/
|
|
9
12
|
|
|
10
13
|
#include "../node_common.h"
|
|
11
14
|
|
|
12
|
-
|
|
15
|
+
/* The linked-module table the code generator registers before the first
|
|
16
|
+
* `createRequire(...)` (core runtime, `xt_module_values.c`). */
|
|
17
|
+
xt_value xt_module_registry_lookup(xt_value specifier);
|
|
18
|
+
|
|
19
|
+
static xt_value module_require(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
|
|
13
20
|
(void)thisValue;
|
|
14
21
|
(void)env;
|
|
15
|
-
(
|
|
16
|
-
(
|
|
17
|
-
|
|
22
|
+
xt_value specifier = xt_arg_at(argc, argv, 0);
|
|
23
|
+
xt_value found = xt_module_registry_lookup(xt_to_string(specifier));
|
|
24
|
+
if (found != XT_UNDEFINED) return found;
|
|
25
|
+
xt_throw(xt_error_from_message(
|
|
26
|
+
"Error: require() of a module that is not linked into this binary is not supported"));
|
|
18
27
|
return XT_UNDEFINED;
|
|
19
28
|
}
|
|
20
29
|
|
|
21
30
|
xt_value xt_module_create_require(int32_t argc, xt_value *argv) {
|
|
22
31
|
(void)argc;
|
|
23
32
|
(void)argv;
|
|
24
|
-
return xt_closure_new((void *)
|
|
33
|
+
return xt_closure_new((void *)module_require, 0, NULL);
|
|
25
34
|
}
|
|
26
35
|
|
|
27
36
|
xt_value xt_module_is_builtin(int32_t argc, xt_value *argv) {
|
|
@@ -2,12 +2,19 @@
|
|
|
2
2
|
* Node.js `process` object for xbintsc.
|
|
3
3
|
*
|
|
4
4
|
* The compiler lowers `process.<method>(...)` to
|
|
5
|
-
* `xt_process_call(<method>, argc, argv)
|
|
6
|
-
* `xt_process_get(<property>)
|
|
7
|
-
*
|
|
5
|
+
* `xt_process_call(<method>, argc, argv)`, `process.<property>` to
|
|
6
|
+
* `xt_process_get(<property>)` and `process.<property> = value` to
|
|
7
|
+
* `xt_process_set(<property>, value)`.
|
|
8
|
+
*
|
|
9
|
+
* Besides the original cwd/exit/uptime/hrtime/getuid methods and the
|
|
10
|
+
* platform/arch/pid/argv/env properties, the object is an EventEmitter (so the
|
|
11
|
+
* TUI can subscribe to "exit") and owns the three standard streams. `xt_finish`
|
|
12
|
+
* runs the "exit" listeners once, after the event loop drains, and returns the
|
|
13
|
+
* status `process.exitCode`/`process.exit(n)` asked for.
|
|
8
14
|
*/
|
|
9
15
|
|
|
10
|
-
#include "
|
|
16
|
+
#include "../node_common.h"
|
|
17
|
+
#include "streams.h"
|
|
11
18
|
|
|
12
19
|
#include <stdio.h>
|
|
13
20
|
#include <stdlib.h>
|
|
@@ -30,6 +37,12 @@ extern char **environ;
|
|
|
30
37
|
#define xt_process_getcwd getcwd
|
|
31
38
|
#endif
|
|
32
39
|
|
|
40
|
+
static xt_value xt_process_emitter_value = 0;
|
|
41
|
+
static xt_value xt_process_versions_value = 0;
|
|
42
|
+
/* Copied into a static buffer: the string's bytes are collected with it. */
|
|
43
|
+
static char xt_process_title_value[256] = "xbintsc";
|
|
44
|
+
static int xt_process_status_set = 0;
|
|
45
|
+
|
|
33
46
|
static const char *xt_process_platform(void) {
|
|
34
47
|
#if defined(_WIN32)
|
|
35
48
|
return "win32";
|
|
@@ -85,18 +98,79 @@ static xt_value xt_process_argv(void) {
|
|
|
85
98
|
return result;
|
|
86
99
|
}
|
|
87
100
|
|
|
101
|
+
/* -- emitter / termination ------------------------------------------------ */
|
|
102
|
+
|
|
103
|
+
static void xt_process_run_exit_listeners(void) {
|
|
104
|
+
if (!xt_process_emitter_value) return;
|
|
105
|
+
xt_value code = xt_number((double)xt_get_exit_status());
|
|
106
|
+
xt_node_emit(xt_process_emitter_value, "exit", 1, &code);
|
|
107
|
+
/* Exit handlers may queue further work (`process.nextTick`); Node drops it,
|
|
108
|
+
but draining keeps a handler that prints through a promise visible. */
|
|
109
|
+
xt_drain_microtasks();
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
static xt_value xt_process_emitter(void) {
|
|
113
|
+
xt_gc_add_root(&xt_process_emitter_value);
|
|
114
|
+
if (!xt_process_emitter_value) {
|
|
115
|
+
xt_value emitter = xt_object_new();
|
|
116
|
+
xt_node_install_emitter(emitter);
|
|
117
|
+
xt_process_emitter_value = emitter;
|
|
118
|
+
xt_at_exit(xt_process_run_exit_listeners);
|
|
119
|
+
}
|
|
120
|
+
return xt_process_emitter_value;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
static xt_value xt_process_versions(void) {
|
|
124
|
+
xt_gc_add_root(&xt_process_versions_value);
|
|
125
|
+
if (!xt_process_versions_value) {
|
|
126
|
+
xt_value versions = xt_object_new();
|
|
127
|
+
/* xbintsc is not Node; expose the shape the ecosystem probes for
|
|
128
|
+
(`process.versions.node`, `process.versions.bun`) without lying about
|
|
129
|
+
the runtime. A missing key is `undefined`, which is what the `bun`
|
|
130
|
+
checks in portable code expect outside Bun. */
|
|
131
|
+
xt_node_set(versions, "node", xt_string_from_cstr("0.0.0"));
|
|
132
|
+
xt_process_versions_value = versions;
|
|
133
|
+
}
|
|
134
|
+
return xt_process_versions_value;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/* -- method dispatch ------------------------------------------------------ */
|
|
138
|
+
|
|
88
139
|
xt_value xt_process_call(xt_value name, int32_t argc, xt_value *argv) {
|
|
89
140
|
const char *method = xt_string_data(name);
|
|
90
141
|
if (!method) return xt_undefined();
|
|
91
142
|
|
|
143
|
+
if (strcmp(method, "on") == 0 || strcmp(method, "once") == 0 || strcmp(method, "addListener") == 0 ||
|
|
144
|
+
strcmp(method, "prependListener") == 0) {
|
|
145
|
+
return xt_node_on_impl(xt_process_emitter(), XT_UNDEFINED, argc, argv);
|
|
146
|
+
}
|
|
147
|
+
if (strcmp(method, "off") == 0 || strcmp(method, "removeListener") == 0 ||
|
|
148
|
+
strcmp(method, "removeAllListeners") == 0) {
|
|
149
|
+
return xt_node_remove_listener_impl(xt_process_emitter(), XT_UNDEFINED, argc, argv);
|
|
150
|
+
}
|
|
151
|
+
if (strcmp(method, "emit") == 0) {
|
|
152
|
+
return xt_node_emit_impl(xt_process_emitter(), XT_UNDEFINED, argc, argv);
|
|
153
|
+
}
|
|
154
|
+
if (strcmp(method, "nextTick") == 0) {
|
|
155
|
+
/* The microtask queue is the closest thing to Node's next-tick queue: it
|
|
156
|
+
drains before the event loop resumes and after the current stack. */
|
|
157
|
+
if (argc > 0 && XT_IS_FUNCTION(argv[0])) xt_queue_microtask(argc, argv);
|
|
158
|
+
return xt_undefined();
|
|
159
|
+
}
|
|
92
160
|
if (strcmp(method, "cwd") == 0) {
|
|
93
161
|
char buffer[4096];
|
|
94
162
|
if (!xt_process_getcwd(buffer, sizeof(buffer))) return xt_undefined();
|
|
95
163
|
return xt_string_from_cstr(buffer);
|
|
96
164
|
}
|
|
97
165
|
if (strcmp(method, "exit") == 0) {
|
|
98
|
-
|
|
99
|
-
|
|
166
|
+
if (argc > 0 && !xt_truthy(xt_is_nullish(argv[0]))) {
|
|
167
|
+
xt_set_exit_status((int)xt_to_number(argv[0]));
|
|
168
|
+
xt_process_status_set = 1;
|
|
169
|
+
}
|
|
170
|
+
/* Run the "exit" listeners (once) before leaving, then exit with the
|
|
171
|
+
status the program asked for — including one set by a listener. */
|
|
172
|
+
xt_finish();
|
|
173
|
+
exit(xt_get_exit_status());
|
|
100
174
|
return xt_undefined();
|
|
101
175
|
}
|
|
102
176
|
if (strcmp(method, "uptime") == 0) {
|
|
@@ -134,40 +208,51 @@ xt_value xt_process_call(xt_value name, int32_t argc, xt_value *argv) {
|
|
|
134
208
|
return xt_undefined();
|
|
135
209
|
}
|
|
136
210
|
|
|
137
|
-
|
|
138
|
-
(void)thisValue;
|
|
139
|
-
(void)env;
|
|
140
|
-
if (argc > 0) {
|
|
141
|
-
const char *text = xt_string_data(xt_to_string(argv[0]));
|
|
142
|
-
if (text) fputs(text, stdout);
|
|
143
|
-
fflush(stdout);
|
|
144
|
-
}
|
|
145
|
-
return xt_bool(1);
|
|
146
|
-
}
|
|
211
|
+
/* -- property writes ------------------------------------------------------ */
|
|
147
212
|
|
|
148
|
-
|
|
149
|
-
(
|
|
150
|
-
(
|
|
151
|
-
if (
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
213
|
+
xt_value xt_process_set(xt_value name, xt_value value) {
|
|
214
|
+
const char *key = xt_string_data(name);
|
|
215
|
+
if (!key) return value;
|
|
216
|
+
if (strcmp(key, "exitCode") == 0) {
|
|
217
|
+
if (xt_truthy(xt_is_nullish(value))) {
|
|
218
|
+
xt_set_exit_status(0);
|
|
219
|
+
xt_process_status_set = 0;
|
|
220
|
+
return value;
|
|
221
|
+
}
|
|
222
|
+
xt_set_exit_status((int)xt_to_number(value));
|
|
223
|
+
xt_process_status_set = 1;
|
|
224
|
+
return value;
|
|
225
|
+
}
|
|
226
|
+
if (strcmp(key, "title") == 0) {
|
|
227
|
+
const char *title = xt_node_cstr(value);
|
|
228
|
+
xt_process_title_value[0] = '\0';
|
|
229
|
+
if (title) {
|
|
230
|
+
strncpy(xt_process_title_value, title, sizeof(xt_process_title_value) - 1);
|
|
231
|
+
xt_process_title_value[sizeof(xt_process_title_value) - 1] = '\0';
|
|
232
|
+
}
|
|
233
|
+
return value;
|
|
155
234
|
}
|
|
156
|
-
return
|
|
235
|
+
return value;
|
|
157
236
|
}
|
|
158
237
|
|
|
159
|
-
|
|
160
|
-
xt_value stream = xt_object_new();
|
|
161
|
-
xt_set(stream, xt_string_from_cstr("write"), xt_closure_new(write_fn, 0, NULL));
|
|
162
|
-
return stream;
|
|
163
|
-
}
|
|
238
|
+
/* -- property reads ------------------------------------------------------- */
|
|
164
239
|
|
|
165
240
|
xt_value xt_process_get(xt_value name) {
|
|
166
241
|
const char *key = xt_string_data(name);
|
|
167
242
|
if (!key) return xt_undefined();
|
|
168
243
|
|
|
169
|
-
if (strcmp(key, "stdout") == 0) return
|
|
170
|
-
if (strcmp(key, "stderr") == 0) return
|
|
244
|
+
if (strcmp(key, "stdout") == 0) return xt_process_stdout_object();
|
|
245
|
+
if (strcmp(key, "stderr") == 0) return xt_process_stderr_object();
|
|
246
|
+
if (strcmp(key, "stdin") == 0) return xt_process_stdin_object();
|
|
247
|
+
if (strcmp(key, "versions") == 0) return xt_process_versions();
|
|
248
|
+
if (strcmp(key, "exitCode") == 0) {
|
|
249
|
+
if (!xt_process_status_set) return xt_undefined();
|
|
250
|
+
return xt_number((double)xt_get_exit_status());
|
|
251
|
+
}
|
|
252
|
+
if (strcmp(key, "execPath") == 0) {
|
|
253
|
+
if (xt_program_argc <= 0 || !xt_program_argv) return xt_undefined();
|
|
254
|
+
return xt_string_from_cstr(xt_program_argv[0]);
|
|
255
|
+
}
|
|
171
256
|
if (strcmp(key, "platform") == 0) return xt_string_from_cstr(xt_process_platform());
|
|
172
257
|
if (strcmp(key, "arch") == 0) return xt_string_from_cstr(xt_process_arch());
|
|
173
258
|
if (strcmp(key, "pid") == 0) return xt_number((double)xt_getpid());
|
|
@@ -181,6 +266,6 @@ xt_value xt_process_get(xt_value name) {
|
|
|
181
266
|
if (strcmp(key, "version") == 0) return xt_string_from_cstr("v0.0.0");
|
|
182
267
|
if (strcmp(key, "argv") == 0) return xt_process_argv();
|
|
183
268
|
if (strcmp(key, "env") == 0) return xt_process_env();
|
|
184
|
-
if (strcmp(key, "title") == 0) return xt_string_from_cstr(
|
|
269
|
+
if (strcmp(key, "title") == 0) return xt_string_from_cstr(xt_process_title_value);
|
|
185
270
|
return xt_undefined();
|
|
186
271
|
}
|