xbintsc 0.6.23 → 0.6.28
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/AGENTS.md +1 -1
- package/dist/src/cli/extensions.js +11 -3
- package/dist/src/cli/extensions.js.map +1 -1
- package/dist/src/cli/help.d.ts +1 -1
- package/dist/src/cli/help.js +1 -1
- package/dist/src/codegen/generator/state.js +7 -0
- package/dist/src/codegen/generator/state.js.map +1 -1
- package/dist/src/codegen/generator/tables.js +19 -1
- package/dist/src/codegen/generator/tables.js.map +1 -1
- package/dist/src/driver/bundler/concatenate.d.ts +23 -0
- package/dist/src/driver/bundler/concatenate.js +210 -0
- package/dist/src/driver/bundler/concatenate.js.map +1 -0
- package/dist/src/driver/bundler/graph.js +9 -5
- package/dist/src/driver/bundler/graph.js.map +1 -1
- package/dist/src/driver/bundler/merge.d.ts +4 -4
- package/dist/src/driver/bundler/merge.js +24 -207
- package/dist/src/driver/bundler/merge.js.map +1 -1
- package/dist/src/driver/bundler/{namespace-import.d.ts → namespace-object.d.ts} +5 -1
- package/dist/src/driver/bundler/{namespace-import.js → namespace-object.js} +6 -2
- package/dist/src/driver/bundler/namespace-object.js.map +1 -0
- package/dist/src/driver/bundler/{reexport-fixup.d.ts → reexport-resolution.d.ts} +9 -3
- package/dist/src/driver/bundler/{reexport-fixup.js → reexport-resolution.js} +16 -6
- package/dist/src/driver/bundler/reexport-resolution.js.map +1 -0
- package/dist/src/driver/fingerprint.d.ts +2 -2
- package/dist/src/driver/fingerprint.js +3 -3
- package/dist/src/driver/fingerprint.js.map +1 -1
- package/dist/src/driver/link-features.js +1 -1
- package/dist/src/driver/link-features.js.map +1 -1
- package/dist/src/driver/modules.d.ts +12 -5
- package/dist/src/driver/modules.js +12 -5
- package/dist/src/driver/modules.js.map +1 -1
- package/dist/src/driver/paths.js +8 -4
- package/dist/src/driver/paths.js.map +1 -1
- package/dist/src/driver/runtime-objects.d.ts +1 -1
- package/dist/src/driver/runtime-objects.js +13 -1
- package/dist/src/driver/runtime-objects.js.map +1 -1
- package/dist/src/driver/version.d.ts +1 -1
- package/dist/src/driver/version.js +1 -1
- package/dist/src/extensions/catalog.js +5 -1
- package/dist/src/extensions/catalog.js.map +1 -1
- package/dist/src/extensions/gui/index.js +0 -1
- package/dist/src/extensions/gui/index.js.map +1 -1
- package/dist/src/extensions/link-feature.d.ts +8 -0
- package/dist/src/extensions/node/stream-web/index.d.ts +6 -1
- package/dist/src/extensions/node/stream-web/index.js +59 -4
- package/dist/src/extensions/node/stream-web/index.js.map +1 -1
- package/dist/src/extensions/node/worker_threads/index.d.ts +14 -4
- package/dist/src/extensions/node/worker_threads/index.js +24 -11
- package/dist/src/extensions/node/worker_threads/index.js.map +1 -1
- package/dist/src/extensions/registry.d.ts +9 -1
- package/dist/src/extensions/registry.js +4 -1
- package/dist/src/extensions/registry.js.map +1 -1
- package/dist/src/extensions/wasm-asset.d.ts +16 -0
- package/dist/src/extensions/wasm-asset.js +28 -0
- package/dist/src/extensions/wasm-asset.js.map +1 -0
- package/dist/src/extensions/wasm.d.ts +17 -0
- package/dist/src/extensions/wasm.js +68 -0
- package/dist/src/extensions/wasm.js.map +1 -0
- package/dist/src/extensions/webworker/index.d.ts +24 -0
- package/dist/src/extensions/webworker/index.js +54 -0
- package/dist/src/extensions/webworker/index.js.map +1 -0
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.js +1 -0
- package/dist/src/index.js.map +1 -1
- package/dist/tests/driver/deferred-reexport.test.js +25 -0
- package/dist/tests/driver/deferred-reexport.test.js.map +1 -1
- package/dist/tests/driver/link-features.test.js +9 -0
- package/dist/tests/driver/link-features.test.js.map +1 -1
- package/dist/tests/e2e/async-fs.test.js +44 -0
- package/dist/tests/e2e/async-fs.test.js.map +1 -1
- package/dist/tests/e2e/harness.d.ts +6 -0
- package/dist/tests/e2e/harness.js +13 -1
- package/dist/tests/e2e/harness.js.map +1 -1
- package/dist/tests/e2e/node-crypto-random.test.d.ts +8 -0
- package/dist/tests/e2e/node-crypto-random.test.js +148 -0
- package/dist/tests/e2e/node-crypto-random.test.js.map +1 -0
- package/dist/tests/e2e/node-modules.test.js +1 -1
- package/dist/tests/e2e/node-modules.test.js.map +1 -1
- package/dist/tests/e2e/web-streams.test.d.ts +8 -0
- package/dist/tests/e2e/web-streams.test.js +274 -0
- package/dist/tests/e2e/web-streams.test.js.map +1 -0
- package/dist/tests/e2e/web-worker.test.d.ts +7 -0
- package/dist/tests/e2e/web-worker.test.js +74 -0
- package/dist/tests/e2e/web-worker.test.js.map +1 -0
- package/dist/tests/e2e/webassembly.test.d.ts +8 -0
- package/dist/tests/e2e/webassembly.test.js +113 -0
- package/dist/tests/e2e/webassembly.test.js.map +1 -0
- package/dist/tests/e2e/worker-api.test.d.ts +14 -0
- package/dist/tests/e2e/worker-api.test.js +460 -0
- package/dist/tests/e2e/worker-api.test.js.map +1 -0
- package/dist/tests/e2e/worker-threads.test.d.ts +8 -0
- package/dist/tests/e2e/worker-threads.test.js +160 -0
- package/dist/tests/e2e/worker-threads.test.js.map +1 -0
- package/dist/tests/extensions/wasm.test.d.ts +1 -0
- package/dist/tests/extensions/wasm.test.js +53 -0
- package/dist/tests/extensions/wasm.test.js.map +1 -0
- package/doc/ai/cli.md +1 -1
- package/doc/ai/contributing.md +11 -4
- package/doc/ai/extensions.md +25 -2
- package/doc/ai/language-support.md +14 -2
- package/doc/ai/zh-CN/cli.md +1 -1
- package/doc/ai/zh-CN/contributing.md +8 -3
- package/doc/ai/zh-CN/extensions.md +23 -2
- package/doc/ai/zh-CN/language-support.md +13 -2
- package/doc/gui/api.md +1 -1
- package/doc/implemented/cli.md +1 -1
- package/doc/implemented/extensions.md +6 -5
- package/doc/implemented/node/stream-web.md +31 -0
- package/doc/implemented/node/worker-threads.md +88 -5
- package/doc/implemented/node.md +2 -1
- package/doc/implemented/streams.md +82 -0
- package/doc/implemented/wasm.md +80 -18
- package/doc/implemented/web-worker.md +84 -0
- package/doc/implemented.md +6 -2
- package/doc/unimplemented/deviations.md +1 -0
- package/doc/unimplemented/node/worker-threads.md +23 -0
- package/doc/unimplemented/node.md +1 -0
- package/doc/unimplemented/stdlib.md +2 -1
- package/doc/unimplemented/wasm.md +21 -9
- package/doc/unimplemented.md +1 -1
- package/doc/zh-CN/gui/api.md +1 -1
- package/doc/zh-CN/implemented/cli.md +1 -1
- package/doc/zh-CN/implemented/extensions.md +7 -6
- package/doc/zh-CN/implemented/node/stream-web.md +24 -0
- package/doc/zh-CN/implemented/node/worker-threads.md +77 -4
- package/doc/zh-CN/implemented/node.md +2 -1
- package/doc/zh-CN/implemented/streams.md +60 -0
- package/doc/zh-CN/implemented/wasm.md +73 -16
- package/doc/zh-CN/implemented/web-worker.md +79 -0
- package/doc/zh-CN/implemented.md +6 -2
- package/doc/zh-CN/unimplemented/deviations.md +1 -0
- package/doc/zh-CN/unimplemented/node/worker-threads.md +20 -0
- package/doc/zh-CN/unimplemented/node.md +1 -0
- package/doc/zh-CN/unimplemented/stdlib.md +2 -1
- package/doc/zh-CN/unimplemented/wasm.md +16 -8
- package/doc/zh-CN/unimplemented.md +1 -1
- package/llms.txt +2 -0
- package/package.json +8 -7
- package/runtime/ext_gui/browser.cpp +1 -1
- package/runtime/ext_node/stream_web/stream_web.c +20 -6
- package/runtime/worker/worker_api.c +422 -0
- package/runtime/worker/worker_codec.c +489 -0
- package/runtime/worker/worker_core.c +445 -0
- package/runtime/worker/worker_internal.h +223 -0
- package/runtime/worker/worker_platform.c +344 -0
- package/runtime/worker/worker_ports.c +203 -0
- package/runtime/worker/worker_transport.c +464 -0
- package/runtime/worker/worker_wire.c +150 -0
- package/runtime/xt_builtins.c +15 -0
- package/runtime/xt_constructors.c +49 -2
- package/runtime/xt_stdlib2/error.inc +4 -0
- package/runtime/xt_streams/common.inc +232 -0
- package/runtime/xt_streams/constructors.inc +232 -0
- package/runtime/xt_streams/encoding.inc +124 -0
- package/runtime/xt_streams/readable-extra.inc +372 -0
- package/runtime/xt_streams/readable.inc +415 -0
- package/runtime/xt_streams/transform.inc +195 -0
- package/runtime/xt_streams/writable.inc +542 -0
- package/runtime/xt_streams.c +30 -0
- package/runtime/xt_wasm/wasm.h +148 -0
- package/runtime/xt_wasm/wasm_api.c +357 -0
- package/runtime/xt_wasm/wasm_api.h +37 -0
- package/runtime/xt_wasm/wasm_bridge.c +30 -0
- package/runtime/xt_wasm/wasm_instance.c +591 -0
- package/runtime/xt_wasm/wasm_module.c +270 -0
- package/runtime/xt_wasm/wasm_values.c +177 -0
- package/runtime/xt_webassembly.c +15 -4
- package/scripts/fetch-vendored.ts +187 -20
- package/scripts/package-release.ts +3 -2
- package/src/cli/extensions.ts +11 -3
- package/src/cli/help.ts +1 -1
- package/src/codegen/generator/state.ts +7 -0
- package/src/codegen/generator/tables.ts +19 -1
- package/src/driver/bundler/README.md +55 -0
- package/src/driver/bundler/concatenate.ts +244 -0
- package/src/driver/bundler/graph.ts +9 -5
- package/src/driver/bundler/merge.ts +24 -215
- package/src/driver/bundler/{namespace-import.ts → namespace-object.ts} +5 -1
- package/src/driver/bundler/{reexport-fixup.ts → reexport-resolution.ts} +15 -5
- package/src/driver/fingerprint.ts +3 -3
- package/src/driver/link-features.ts +1 -1
- package/src/driver/modules.ts +12 -5
- package/src/driver/paths.ts +8 -4
- package/src/driver/runtime-objects.ts +13 -1
- package/src/driver/version.ts +1 -1
- package/src/extensions/catalog.ts +5 -1
- package/src/extensions/gui/index.ts +0 -1
- package/src/extensions/link-feature.ts +8 -0
- package/src/extensions/node/stream-web/index.ts +59 -4
- package/src/extensions/node/worker_threads/index.ts +24 -11
- package/src/extensions/registry.ts +13 -2
- package/src/extensions/wasm-asset.ts +32 -0
- package/src/extensions/wasm.ts +74 -0
- package/src/extensions/webworker/index.ts +58 -0
- package/src/index.ts +1 -0
- package/vendor/wasm3/.pin +1 -0
- package/vendor/wasm3/LICENSE +21 -0
- package/vendor/wasm3/m3_api_libc.c +265 -0
- package/vendor/wasm3/m3_api_libc.h +19 -0
- package/vendor/wasm3/m3_api_meta_wasi.c +1107 -0
- package/vendor/wasm3/m3_api_tracer.c +173 -0
- package/vendor/wasm3/m3_api_tracer.h +19 -0
- package/vendor/wasm3/m3_api_uvwasi.c +1290 -0
- package/vendor/wasm3/m3_api_wasi.c +1207 -0
- package/vendor/wasm3/m3_api_wasi.h +74 -0
- package/vendor/wasm3/m3_api_wasi_host.h +137 -0
- package/vendor/wasm3/m3_api_wasi_posix.h +979 -0
- package/vendor/wasm3/m3_api_wasi_win32.h +1093 -0
- package/vendor/wasm3/m3_atomic.h +114 -0
- package/vendor/wasm3/m3_bind.c +174 -0
- package/vendor/wasm3/m3_bind.h +20 -0
- package/vendor/wasm3/m3_code.c +232 -0
- package/vendor/wasm3/m3_code.h +74 -0
- package/vendor/wasm3/m3_compile.c +6603 -0
- package/vendor/wasm3/m3_compile.h +354 -0
- package/vendor/wasm3/m3_config.h +448 -0
- package/vendor/wasm3/m3_config_platforms.h +345 -0
- package/vendor/wasm3/m3_core.c +1034 -0
- package/vendor/wasm3/m3_core.h +526 -0
- package/vendor/wasm3/m3_deterministic.c +65 -0
- package/vendor/wasm3/m3_deterministic.h +59 -0
- package/vendor/wasm3/m3_env.c +3146 -0
- package/vendor/wasm3/m3_env.h +748 -0
- package/vendor/wasm3/m3_exception.h +33 -0
- package/vendor/wasm3/m3_exec.c +8 -0
- package/vendor/wasm3/m3_exec.h +4110 -0
- package/vendor/wasm3/m3_exec_atomic.h +146 -0
- package/vendor/wasm3/m3_exec_defs.h +76 -0
- package/vendor/wasm3/m3_function.c +278 -0
- package/vendor/wasm3/m3_function.h +244 -0
- package/vendor/wasm3/m3_host.h +226 -0
- package/vendor/wasm3/m3_host_none.h +76 -0
- package/vendor/wasm3/m3_host_posix.h +543 -0
- package/vendor/wasm3/m3_host_win32.h +253 -0
- package/vendor/wasm3/m3_info.c +616 -0
- package/vendor/wasm3/m3_info.h +38 -0
- package/vendor/wasm3/m3_math_utils.h +560 -0
- package/vendor/wasm3/m3_module.c +431 -0
- package/vendor/wasm3/m3_parse.c +1549 -0
- package/vendor/wasm3/m3_snapshot.c +3825 -0
- package/vendor/wasm3/m3_validate.c +1770 -0
- package/vendor/wasm3/m3_validate.h +25 -0
- package/vendor/wasm3/m3_wasi.h +10 -0
- package/vendor/wasm3/m3_xxh64.c +211 -0
- package/vendor/wasm3/wasm3.h +684 -0
- package/vendor/wasm3/wasm3_defs.h +350 -0
- package/dist/src/driver/bundler/namespace-import.js.map +0 -1
- package/dist/src/driver/bundler/reexport-fixup.js.map +0 -1
- package/runtime/ext_node/worker_threads/worker_threads.c +0 -214
|
@@ -14,9 +14,10 @@
|
|
|
14
14
|
* vendor/nanosvg/nanosvg.h, nanosvgrast.h
|
|
15
15
|
* vendor/miniz/… the `compression` link feature
|
|
16
16
|
* vendor/sqlite3/sqlite3.{c,h} the `storage` link feature
|
|
17
|
+
* vendor/wasm3/… the `wasm` link feature
|
|
17
18
|
*
|
|
18
19
|
* `build-gui.ts` fetches just `stb`/`nanosvg`; `npm test` fetches everything.
|
|
19
|
-
* CI caches the
|
|
20
|
+
* CI caches the fetched directories keyed on this script.
|
|
20
21
|
*
|
|
21
22
|
* Note: miniz's upstream tree has no `miniz_export.h` (its CMake build generates
|
|
22
23
|
* one). The static-only shim below is written here rather than tracked.
|
|
@@ -24,22 +25,22 @@
|
|
|
24
25
|
* Run via `npm run vendor` (or `tsx scripts/fetch-vendored.ts`).
|
|
25
26
|
*/
|
|
26
27
|
|
|
27
|
-
import { spawnSync } from "node:child_process";
|
|
28
28
|
import { createHash } from "node:crypto";
|
|
29
|
-
import { copyFileSync, existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
30
|
-
import { basename, dirname, join, resolve } from "node:path";
|
|
29
|
+
import { copyFileSync, existsSync, mkdirSync, readFileSync, readdirSync, rmSync, writeFileSync } from "node:fs";
|
|
30
|
+
import { basename, dirname, join, resolve, sep } from "node:path";
|
|
31
31
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
32
|
+
import { gunzipSync, inflateRawSync } from "node:zlib";
|
|
32
33
|
import { downloadTarball } from "./fetch-tarball.js";
|
|
33
34
|
|
|
34
35
|
const root = resolve(dirname(fileURLToPath(import.meta.url)), "..");
|
|
35
36
|
export const vendorDir = join(root, "vendor");
|
|
36
37
|
|
|
37
38
|
/** The vendored packages, each a directory under `vendor/`. */
|
|
38
|
-
export const VENDOR_PACKAGES = ["stb", "nanosvg", "miniz", "sqlite3"] as const;
|
|
39
|
+
export const VENDOR_PACKAGES = ["stb", "nanosvg", "miniz", "sqlite3", "wasm3"] as const;
|
|
39
40
|
export type VendorPackage = (typeof VENDOR_PACKAGES)[number];
|
|
40
41
|
|
|
41
42
|
interface VendoredFile {
|
|
42
|
-
readonly package: Exclude<VendorPackage, "sqlite3">;
|
|
43
|
+
readonly package: Exclude<VendorPackage, "sqlite3" | "wasm3">;
|
|
43
44
|
readonly name: string;
|
|
44
45
|
readonly url: string;
|
|
45
46
|
readonly sha256: string;
|
|
@@ -122,6 +123,15 @@ const SQLITE_FILES = [
|
|
|
122
123
|
},
|
|
123
124
|
] as const;
|
|
124
125
|
|
|
126
|
+
/* wasm3's interpreter is fetched as one commit archive: its `source/` tree is
|
|
127
|
+
* small but the headers and the `.c` files have to stay in lockstep, so a
|
|
128
|
+
* single pinned tarball is the reviewable unit. Only `source/` and the license
|
|
129
|
+
* are copied out; the archive's tools, tests and examples are not compiled. */
|
|
130
|
+
const WASM3_COMMIT = "28ecb9af6d2040e474a70f7cb7f43666740141fb";
|
|
131
|
+
const WASM3_TARBALL_URL = `https://github.com/wasm3/wasm3/archive/${WASM3_COMMIT}.tar.gz`;
|
|
132
|
+
const WASM3_TARBALL_SHA256 = "5ecbb8851bd9acb54a83be10f0536167cb0da13b007406578761672b0bd94b18";
|
|
133
|
+
const WASM3_PIN = `${WASM3_COMMIT} ${WASM3_TARBALL_SHA256}\n`;
|
|
134
|
+
|
|
125
135
|
function sha256(file: string): string {
|
|
126
136
|
return createHash("sha256").update(readFileSync(file)).digest("hex");
|
|
127
137
|
}
|
|
@@ -154,22 +164,68 @@ function ensureMinizExportHeader(): void {
|
|
|
154
164
|
writeFileSync(target, MINIZ_EXPORT_H);
|
|
155
165
|
}
|
|
156
166
|
|
|
157
|
-
/**
|
|
167
|
+
/** A 32-bit little-endian field of a zip record. */
|
|
168
|
+
function zipU32(bytes: Uint8Array, offset: number): number {
|
|
169
|
+
return new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength).getUint32(offset, true);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** A 16-bit little-endian field of a zip record. */
|
|
173
|
+
function zipU16(bytes: Uint8Array, offset: number): number {
|
|
174
|
+
return new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength).getUint16(offset, true);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Extract a zip archive in Node.
|
|
179
|
+
*
|
|
180
|
+
* An external `unzip`/`tar` is not guaranteed on every runner image, and a CI
|
|
181
|
+
* cache miss (for example after editing this script) must not turn a missing
|
|
182
|
+
* shell tool into a failed job. The central directory is the authoritative
|
|
183
|
+
* index, so it is read instead of walking local headers; stored and deflated
|
|
184
|
+
* entries cover the pinned SQLite amalgamation. Contents are pinned by SHA-256
|
|
185
|
+
* after extraction, which is why the CRC is not rechecked here.
|
|
186
|
+
*/
|
|
158
187
|
function extractZip(zip: string, destination: string): void {
|
|
159
|
-
const
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
188
|
+
const archive = readFileSync(zip);
|
|
189
|
+
const SIGNATURE_EOCD = 0x06054b50;
|
|
190
|
+
const SIGNATURE_CENTRAL = 0x02014b50;
|
|
191
|
+
let eocd = -1;
|
|
192
|
+
for (let offset = archive.length - 22; offset >= 0 && offset >= archive.length - 65557; offset--) {
|
|
193
|
+
if (zipU32(archive, offset) === SIGNATURE_EOCD) {
|
|
194
|
+
eocd = offset;
|
|
195
|
+
break;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
if (eocd < 0) throw new Error(`${basename(zip)}: not a zip archive`);
|
|
199
|
+
const count = zipU16(archive, eocd + 10);
|
|
200
|
+
let offset = zipU32(archive, eocd + 16);
|
|
201
|
+
for (let index = 0; index < count; index++) {
|
|
202
|
+
if (zipU32(archive, offset) !== SIGNATURE_CENTRAL) {
|
|
203
|
+
throw new Error(`${basename(zip)}: corrupt central directory`);
|
|
204
|
+
}
|
|
205
|
+
const method = zipU16(archive, offset + 10);
|
|
206
|
+
const compressedSize = zipU32(archive, offset + 20);
|
|
207
|
+
const nameLength = zipU16(archive, offset + 28);
|
|
208
|
+
const extraLength = zipU16(archive, offset + 30);
|
|
209
|
+
const commentLength = zipU16(archive, offset + 32);
|
|
210
|
+
const localOffset = zipU32(archive, offset + 42);
|
|
211
|
+
const name = Buffer.from(archive.subarray(offset + 46, offset + 46 + nameLength)).toString("utf8");
|
|
212
|
+
offset += 46 + nameLength + extraLength + commentLength;
|
|
213
|
+
if (name.endsWith("/")) continue;
|
|
214
|
+
const localNameLength = zipU16(archive, localOffset + 26);
|
|
215
|
+
const localExtraLength = zipU16(archive, localOffset + 28);
|
|
216
|
+
const dataStart = localOffset + 30 + localNameLength + localExtraLength;
|
|
217
|
+
const payload = archive.subarray(dataStart, dataStart + compressedSize);
|
|
218
|
+
let content: Buffer;
|
|
219
|
+
if (method === 0) content = Buffer.from(payload);
|
|
220
|
+
else if (method === 8) content = inflateRawSync(payload);
|
|
221
|
+
else throw new Error(`${basename(zip)}: unsupported compression method ${method}`);
|
|
222
|
+
const target = resolve(destination, name);
|
|
223
|
+
if (target !== destination && !target.startsWith(`${destination}${sep}`)) {
|
|
224
|
+
throw new Error(`refusing to extract ${name} outside ${destination}`);
|
|
225
|
+
}
|
|
226
|
+
mkdirSync(dirname(target), { recursive: true });
|
|
227
|
+
writeFileSync(target, content);
|
|
171
228
|
}
|
|
172
|
-
throw new Error(`cannot extract ${basename(zip)}; install unzip (or a tar that reads zip archives)`);
|
|
173
229
|
}
|
|
174
230
|
|
|
175
231
|
/** Fetch the pinned SQLite amalgamation and copy out its two sources. */
|
|
@@ -213,6 +269,113 @@ function ensureSqlite(): void {
|
|
|
213
269
|
rmSync(zip, { force: true });
|
|
214
270
|
}
|
|
215
271
|
|
|
272
|
+
/** Read a NUL/space-padded text field out of a tar block. */
|
|
273
|
+
function tarText(bytes: Uint8Array, start: number, length: number): string {
|
|
274
|
+
let end = start;
|
|
275
|
+
while (end < start + length && bytes[end] !== 0) end++;
|
|
276
|
+
return Buffer.from(bytes.subarray(start, end)).toString("utf8").replace(/\s+$/, "");
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/** Read an octal field (tar pads it with spaces and/or NULs). */
|
|
280
|
+
function tarSize(bytes: Uint8Array, start: number, length: number): number {
|
|
281
|
+
const text = tarText(bytes, start, length);
|
|
282
|
+
return text.length === 0 ? 0 : parseInt(text, 8);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Extract selected members of a gzip-compressed tarball.
|
|
287
|
+
*
|
|
288
|
+
* An external `tar` is not reliable here: an older bsdtar on the Windows
|
|
289
|
+
* runners rejects this archive, and a runner image may not ship `tar` at all.
|
|
290
|
+
* Node's gzip decoder plus a minimal ustar reader keeps a cache miss working —
|
|
291
|
+
* the GitHub archive is plain ustar. Only regular files under the named members
|
|
292
|
+
* are written; directories, symlinks and the other repository trees are
|
|
293
|
+
* skipped (which is also why the wasm3 archive's platform symlinks cannot
|
|
294
|
+
* break extraction).
|
|
295
|
+
*/
|
|
296
|
+
function extractTarball(tarball: string, destination: string, members: readonly string[]): void {
|
|
297
|
+
const archive = gunzipSync(readFileSync(tarball));
|
|
298
|
+
const wanted = (path: string): boolean =>
|
|
299
|
+
members.some((member) => path === member || path.startsWith(`${member}/`));
|
|
300
|
+
let offset = 0;
|
|
301
|
+
while (offset + 512 <= archive.length) {
|
|
302
|
+
const header = archive.subarray(offset, offset + 512);
|
|
303
|
+
/* Two zero blocks mark the end of the archive; one is enough to stop. */
|
|
304
|
+
if (header.every((byte) => byte === 0)) return;
|
|
305
|
+
const name = tarText(header, 0, 100);
|
|
306
|
+
const prefix = tarText(header, 345, 155);
|
|
307
|
+
const type = header[156] === 0 ? "0" : String.fromCharCode(header[156]);
|
|
308
|
+
const size = tarSize(header, 124, 12);
|
|
309
|
+
const path = prefix.length > 0 ? `${prefix}/${name}` : name;
|
|
310
|
+
const dataStart = offset + 512;
|
|
311
|
+
const next = dataStart + Math.ceil(size / 512) * 512;
|
|
312
|
+
if ((type === "0" || type === "7") && wanted(path)) {
|
|
313
|
+
const target = resolve(destination, path);
|
|
314
|
+
if (target !== destination && !target.startsWith(`${destination}${sep}`)) {
|
|
315
|
+
throw new Error(`refusing to extract ${path} outside ${destination}`);
|
|
316
|
+
}
|
|
317
|
+
mkdirSync(dirname(target), { recursive: true });
|
|
318
|
+
writeFileSync(target, archive.subarray(dataStart, dataStart + size));
|
|
319
|
+
}
|
|
320
|
+
offset = next;
|
|
321
|
+
}
|
|
322
|
+
throw new Error(`${basename(tarball)}: truncated tar archive`);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Fetch the pinned wasm3 archive and copy its interpreter sources into
|
|
327
|
+
* `vendor/wasm3`. A `.pin` marker records the commit and tarball hash, so an
|
|
328
|
+
* already-fetched tree is reused without touching the network and a changed
|
|
329
|
+
* pin re-extracts instead of silently mixing revisions.
|
|
330
|
+
*/
|
|
331
|
+
function ensureWasm3(): void {
|
|
332
|
+
const directory = join(vendorDir, "wasm3");
|
|
333
|
+
const pin = join(directory, ".pin");
|
|
334
|
+
if (
|
|
335
|
+
existsSync(join(directory, "wasm3.h")) &&
|
|
336
|
+
existsSync(pin) &&
|
|
337
|
+
readFileSync(pin, "utf8").replace(/\r\n/g, "\n") === WASM3_PIN
|
|
338
|
+
) {
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
const cache = join(vendorDir, ".cache");
|
|
343
|
+
mkdirSync(cache, { recursive: true });
|
|
344
|
+
const tarball = join(cache, `wasm3-${WASM3_COMMIT}.tar.gz`);
|
|
345
|
+
if (!isPinned(tarball, WASM3_TARBALL_SHA256)) {
|
|
346
|
+
rmSync(tarball, { force: true });
|
|
347
|
+
downloadTarball(tarball, WASM3_TARBALL_URL, `wasm3-${WASM3_COMMIT}.tar.gz`);
|
|
348
|
+
if (!isPinned(tarball, WASM3_TARBALL_SHA256)) {
|
|
349
|
+
const actual = sha256(tarball);
|
|
350
|
+
rmSync(tarball, { force: true });
|
|
351
|
+
throw new Error(`wasm3-${WASM3_COMMIT}.tar.gz: expected sha256 ${WASM3_TARBALL_SHA256}, got ${actual}`);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
const extracted = join(cache, `wasm3-${WASM3_COMMIT}`);
|
|
356
|
+
rmSync(extracted, { recursive: true, force: true });
|
|
357
|
+
mkdirSync(extracted, { recursive: true });
|
|
358
|
+
const archiveRoot = `wasm3-${WASM3_COMMIT}`;
|
|
359
|
+
extractTarball(tarball, extracted, [`${archiveRoot}/source`, `${archiveRoot}/LICENSE`]);
|
|
360
|
+
const sourceDir = join(extracted, archiveRoot, "source");
|
|
361
|
+
if (!existsSync(join(sourceDir, "wasm3.h"))) {
|
|
362
|
+
throw new Error(`expected ${archiveRoot}/source/wasm3.h after extracting the archive`);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
rmSync(directory, { recursive: true, force: true });
|
|
366
|
+
mkdirSync(directory, { recursive: true });
|
|
367
|
+
for (const entry of readdirSync(sourceDir, { withFileTypes: true })) {
|
|
368
|
+
if (!entry.isFile()) continue;
|
|
369
|
+
if (!entry.name.endsWith(".c") && !entry.name.endsWith(".h")) continue;
|
|
370
|
+
copyFileSync(join(sourceDir, entry.name), join(directory, entry.name));
|
|
371
|
+
}
|
|
372
|
+
const license = join(extracted, `wasm3-${WASM3_COMMIT}`, "LICENSE");
|
|
373
|
+
if (existsSync(license)) copyFileSync(license, join(directory, "LICENSE"));
|
|
374
|
+
writeFileSync(pin, WASM3_PIN);
|
|
375
|
+
rmSync(extracted, { recursive: true, force: true });
|
|
376
|
+
rmSync(tarball, { force: true });
|
|
377
|
+
}
|
|
378
|
+
|
|
216
379
|
/**
|
|
217
380
|
* Ensure the requested vendored packages exist and match their pins.
|
|
218
381
|
* Downloads only what is missing; everything else is a content check.
|
|
@@ -223,6 +386,10 @@ export function ensureVendoredSources(packages: readonly VendorPackage[] = VENDO
|
|
|
223
386
|
ensureSqlite();
|
|
224
387
|
continue;
|
|
225
388
|
}
|
|
389
|
+
if (name === "wasm3") {
|
|
390
|
+
ensureWasm3();
|
|
391
|
+
continue;
|
|
392
|
+
}
|
|
226
393
|
for (const file of FILES) {
|
|
227
394
|
if (file.package === name) ensureFile(file);
|
|
228
395
|
}
|
|
@@ -110,8 +110,9 @@ cpSync(findRuntimeDir(), join(stage, "runtime"), { recursive: true });
|
|
|
110
110
|
console.log("xbintsc: packaged runtime/");
|
|
111
111
|
|
|
112
112
|
// 3. Vendored sources the driver compiles on demand (SQLite for `storage`,
|
|
113
|
-
// miniz for `compression`). They are fetched by `npm run vendor`,
|
|
114
|
-
|
|
113
|
+
// miniz for `compression`, wasm3 for `wasm`). They are fetched by `npm run vendor`,
|
|
114
|
+
// not committed.
|
|
115
|
+
for (const name of ["stb", "nanosvg", "miniz", "sqlite3", "wasm3"]) {
|
|
115
116
|
const source = join(root, "vendor", name);
|
|
116
117
|
if (!existsSync(source)) fail(`missing vendor/${name}; run \`npm run vendor\` before packaging`);
|
|
117
118
|
cpSync(source, join(stage, "vendor", name), { recursive: true });
|
package/src/cli/extensions.ts
CHANGED
|
@@ -18,10 +18,18 @@ export function buildRegistry(
|
|
|
18
18
|
): ExtensionRegistry {
|
|
19
19
|
const registry = createDefaultRegistry();
|
|
20
20
|
const bundled = bundledExtensions();
|
|
21
|
+
const requested = new Set(extNames);
|
|
21
22
|
for (const name of extNames) {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
if (!bundled.some((candidate) => candidate.name === name)) {
|
|
24
|
+
throw new Error(`Unknown extension '${name}'`);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/* Register in catalog order, not flag order: later extensions override
|
|
28
|
+
* earlier global names (`gui` and `webworker` both define `self`), and a
|
|
29
|
+
* deterministic winner is easier to document than "whatever the user typed
|
|
30
|
+
* last". */
|
|
31
|
+
for (const extension of bundled) {
|
|
32
|
+
if (requested.has(extension.name)) registry.register(extension);
|
|
25
33
|
}
|
|
26
34
|
// Each manifest describes a pre-built C++ or Rust library.
|
|
27
35
|
for (const manifest of nativeManifests) {
|
package/src/cli/help.ts
CHANGED
|
@@ -26,7 +26,7 @@ Options:
|
|
|
26
26
|
--emit <kind> exe | obj | ir (default: exe)
|
|
27
27
|
-O0..-O3 Optimization level (default: -O2)
|
|
28
28
|
--ext <names> Comma separated extensions (e.g. node,gui)
|
|
29
|
-
--feature <names> Force optional link features on (media,gpu,images,shaping,webgl,all)
|
|
29
|
+
--feature <names> Force optional link features on (media,gpu,images,shaping,webgl,storage,tls,wasm,all)
|
|
30
30
|
--no-feature <n> Keep optional link features out even when detected
|
|
31
31
|
--ext-native <m> Register a C++/Rust extension from a JSON manifest
|
|
32
32
|
(comma separated for several)
|
|
@@ -207,6 +207,13 @@ export const RUNTIME_DECLARATIONS: readonly string[] = [
|
|
|
207
207
|
"declare i64 @xt_finalization_registry_ctor(i32, i64*)",
|
|
208
208
|
"declare i64 @xt_text_encoder_ctor(i32, i64*)",
|
|
209
209
|
"declare i64 @xt_text_decoder_ctor(i32, i64*)",
|
|
210
|
+
"declare i64 @xt_readable_stream_ctor(i32, i64*)",
|
|
211
|
+
"declare i64 @xt_writable_stream_ctor(i32, i64*)",
|
|
212
|
+
"declare i64 @xt_transform_stream_ctor(i32, i64*)",
|
|
213
|
+
"declare i64 @xt_count_queuing_strategy_ctor(i32, i64*)",
|
|
214
|
+
"declare i64 @xt_byte_length_queuing_strategy_ctor(i32, i64*)",
|
|
215
|
+
"declare i64 @xt_text_encoder_stream_ctor(i32, i64*)",
|
|
216
|
+
"declare i64 @xt_text_decoder_stream_ctor(i32, i64*)",
|
|
210
217
|
"declare i64 @xt_date_ctor(i32, i64*)",
|
|
211
218
|
"declare i64 @xt_regexp_ctor(i32, i64*)",
|
|
212
219
|
"declare i64 @xt_url_ctor(i32, i64*)",
|
|
@@ -49,6 +49,11 @@ export const BUILTIN_INSTANCEOF: Readonly<Record<string, number>> = {
|
|
|
49
49
|
Int32Array: 22,
|
|
50
50
|
Float32Array: 23,
|
|
51
51
|
Float64Array: 24,
|
|
52
|
+
ReadableStream: 11,
|
|
53
|
+
WritableStream: 12,
|
|
54
|
+
TransformStream: 13,
|
|
55
|
+
TextEncoderStream: 14,
|
|
56
|
+
TextDecoderStream: 15,
|
|
52
57
|
};
|
|
53
58
|
|
|
54
59
|
export const BINARY_RUNTIME: Record<string, string | undefined> = {
|
|
@@ -311,7 +316,6 @@ export const WEB_GLOBALS: ReadonlySet<string> = new Set<string>([
|
|
|
311
316
|
"Headers",
|
|
312
317
|
"Request",
|
|
313
318
|
"Response",
|
|
314
|
-
"ReadableStream",
|
|
315
319
|
"EventTarget",
|
|
316
320
|
]);
|
|
317
321
|
|
|
@@ -349,6 +353,13 @@ export const CTOR_FUNCTIONS: Record<string, string> = {
|
|
|
349
353
|
URLSearchParams: "xt_url_search_params_ctor",
|
|
350
354
|
TextEncoder: "xt_text_encoder_ctor",
|
|
351
355
|
TextDecoder: "xt_text_decoder_ctor",
|
|
356
|
+
ReadableStream: "xt_readable_stream_ctor",
|
|
357
|
+
WritableStream: "xt_writable_stream_ctor",
|
|
358
|
+
TransformStream: "xt_transform_stream_ctor",
|
|
359
|
+
CountQueuingStrategy: "xt_count_queuing_strategy_ctor",
|
|
360
|
+
ByteLengthQueuingStrategy: "xt_byte_length_queuing_strategy_ctor",
|
|
361
|
+
TextEncoderStream: "xt_text_encoder_stream_ctor",
|
|
362
|
+
TextDecoderStream: "xt_text_decoder_stream_ctor",
|
|
352
363
|
ArrayBuffer: "xt_array_buffer_ctor",
|
|
353
364
|
DataView: "xt_data_view_ctor",
|
|
354
365
|
...ERROR_CONSTRUCTORS,
|
|
@@ -381,6 +392,13 @@ export const FIRST_CLASS_CONSTRUCTORS = new Set<string>([
|
|
|
381
392
|
"Boolean",
|
|
382
393
|
"BigInt",
|
|
383
394
|
"Symbol",
|
|
395
|
+
"ReadableStream",
|
|
396
|
+
"WritableStream",
|
|
397
|
+
"TransformStream",
|
|
398
|
+
"CountQueuingStrategy",
|
|
399
|
+
"ByteLengthQueuingStrategy",
|
|
400
|
+
"TextEncoderStream",
|
|
401
|
+
"TextDecoderStream",
|
|
384
402
|
]);
|
|
385
403
|
|
|
386
404
|
export const BUILTIN_METHODS = new Set<string>([
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# Source-level module bundler
|
|
2
|
+
|
|
3
|
+
xbintsc has no runtime module system and no module linker. The driver resolves
|
|
4
|
+
every reachable module, renames its top-level bindings to globally unique
|
|
5
|
+
`<prefix><name>` identifiers, rewrites imports to those final names, and
|
|
6
|
+
concatenates the whole graph into one source file for codegen.
|
|
7
|
+
|
|
8
|
+
The public entry point is `bundleModules()` in `../modules.ts`; everything in
|
|
9
|
+
this directory is an implementation detail of it.
|
|
10
|
+
|
|
11
|
+
## Pipeline
|
|
12
|
+
|
|
13
|
+
| Phase | Location | Work |
|
|
14
|
+
| --- | --- | --- |
|
|
15
|
+
| 1 | `merge.ts` | Record top-level bindings and pick a unique final name per symbol. |
|
|
16
|
+
| 1b | `commonjs.ts` | Give CommonJS `node_modules` files synthetic `module` / `exports` state. |
|
|
17
|
+
| 2 | `merge.ts` | Fill every module's `exports` map from declarations and re-exports. |
|
|
18
|
+
| 2b | `commonjs.ts` | Lower `require("…")` to the already-bundled dependency's exports. |
|
|
19
|
+
| 3 | `merge.ts` | Rewrite imported names; lower deferred imports and namespace bindings. |
|
|
20
|
+
| 3b | `reexport-resolution.ts` | Re-resolve re-export chains to a fixpoint; rebuild eager namespace objects. |
|
|
21
|
+
| 4 | `concatenate.ts` | Emit statements in dependency order, dropping local imports and export wrappers. |
|
|
22
|
+
| 5 | `concatenate.ts` | Apply the final rename to each top-level declaration and reference. |
|
|
23
|
+
|
|
24
|
+
Phases 4-5 live in `concatenate.ts` so `merge.ts` stays focused on name and
|
|
25
|
+
export resolution.
|
|
26
|
+
|
|
27
|
+
## Files
|
|
28
|
+
|
|
29
|
+
| File | Responsibility |
|
|
30
|
+
| --- | --- |
|
|
31
|
+
| `types.ts` | `ModuleRecord`, `BundleResult`, resolution types. |
|
|
32
|
+
| `resolve.ts` | Specifier resolution: relative paths, directory indexes, package `exports`, `node_modules`. |
|
|
33
|
+
| `graph.ts` | Load and bind the import graph in dependency order. |
|
|
34
|
+
| `symbols.ts` | Top-level symbol lookup, declaration/reference renaming, export-map remapping. |
|
|
35
|
+
| `merge.ts` | Orchestrates phases 1-3 and produces the resolved module records. |
|
|
36
|
+
| `reexport-resolution.ts` | Fixpoint over `export … from` chains; rebuilds namespace objects from final names. |
|
|
37
|
+
| `namespace-object.ts` | Synthesizes `const ns = { name: finalName, … }` for namespace imports/re-exports. |
|
|
38
|
+
| `deferred.ts` | `<script src>` deferred evaluation: wrapper functions, alias lowering, deferred-set computation. |
|
|
39
|
+
| `dynamic-import.ts` | Lowers string-literal `import()` to a promise over the target's wrapper. |
|
|
40
|
+
| `commonjs.ts` | CommonJS interop for `node_modules` sources. |
|
|
41
|
+
| `concatenate.ts` | Phases 4-5: emit the merged statement list and rename declarations. |
|
|
42
|
+
|
|
43
|
+
## Invariants
|
|
44
|
+
|
|
45
|
+
- **No live bindings.** An imported name is a snapshot of the exported value at
|
|
46
|
+
module-evaluation time.
|
|
47
|
+
- **The entry module is last** in the record order; dependencies are
|
|
48
|
+
post-order.
|
|
49
|
+
- **Deferred modules own their bindings.** A deferred module's top-level names
|
|
50
|
+
are local to its `__xt_deferred_*` wrapper. A namespace binding between two
|
|
51
|
+
deferred modules must stay an alias that reads the dependency's wrapper
|
|
52
|
+
(`deferred.ts`); it must not be rebuilt at top level from wrapper-local names
|
|
53
|
+
(TS3002). Regression coverage: `tests/driver/deferred-reexport.test.ts`.
|
|
54
|
+
- **Re-exports are settled before concatenation.** Phase 3b runs to a fixpoint
|
|
55
|
+
so multi-level `export … from` chains and cycle back-edges see final names.
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Concatenate the merged modules into one source file (bundle phases 4-5).
|
|
3
|
+
*
|
|
4
|
+
* Phase 4 walks every module in dependency order and copies its statements into
|
|
5
|
+
* a single statement list: external imports stay at top level, local imports are
|
|
6
|
+
* dropped (their bindings were rewritten in phase 3), and a deferred module's
|
|
7
|
+
* body is emitted inside its `__xt_deferred_*` wrapper. Phase 5 then renames
|
|
8
|
+
* every top-level declaration and reference to its final unique name.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { dirname } from "node:path";
|
|
12
|
+
import {
|
|
13
|
+
ModifierKind,
|
|
14
|
+
SyntaxKind,
|
|
15
|
+
type ExportAssignment,
|
|
16
|
+
type ExportDeclaration,
|
|
17
|
+
type Identifier,
|
|
18
|
+
type ImportDeclaration,
|
|
19
|
+
type Modifier,
|
|
20
|
+
type Node,
|
|
21
|
+
type SourceFileNode,
|
|
22
|
+
type Statement,
|
|
23
|
+
type VariableDeclaration,
|
|
24
|
+
type VariableDeclarationList,
|
|
25
|
+
} from "../../ast/nodes.js";
|
|
26
|
+
import { SymbolKind } from "../../binder/binder.js";
|
|
27
|
+
import { deferredModuleFunctions, eagerModuleFunctions, type DeferredAlias } from "./deferred.js";
|
|
28
|
+
import { classifyDependency } from "./resolve.js";
|
|
29
|
+
import { renameSymbol, topLevelSymbols } from "./symbols.js";
|
|
30
|
+
import type { ModuleRecord } from "./types.js";
|
|
31
|
+
|
|
32
|
+
export interface ConcatenateOptions {
|
|
33
|
+
readonly records: readonly ModuleRecord[];
|
|
34
|
+
readonly deferredModules: ReadonlySet<string>;
|
|
35
|
+
readonly deferredAliases: ReadonlyMap<string, readonly DeferredAlias[]>;
|
|
36
|
+
readonly markerTargets: ReadonlySet<string>;
|
|
37
|
+
readonly dynamicTargets: ReadonlySet<string>;
|
|
38
|
+
readonly namespaceStatements: ReadonlyMap<Statement, Statement>;
|
|
39
|
+
readonly externalSpecifiers: ReadonlySet<string>;
|
|
40
|
+
readonly moduleOverrides?: Readonly<Record<string, string>>;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function concatenateModules(options: ConcatenateOptions): SourceFileNode {
|
|
44
|
+
const {
|
|
45
|
+
records,
|
|
46
|
+
deferredModules,
|
|
47
|
+
deferredAliases,
|
|
48
|
+
markerTargets,
|
|
49
|
+
dynamicTargets,
|
|
50
|
+
namespaceStatements,
|
|
51
|
+
externalSpecifiers,
|
|
52
|
+
moduleOverrides,
|
|
53
|
+
} = options;
|
|
54
|
+
const merged: Statement[] = [];
|
|
55
|
+
/* Every node keeps the module it was parsed from, so a later codegen
|
|
56
|
+
* diagnostic can name the right file and line instead of pointing into the
|
|
57
|
+
* entry module (whose text happens to start at the same offsets). */
|
|
58
|
+
const origins = new Map<object, string>();
|
|
59
|
+
/* A synthesized module (an extension asset loader such as the `gui` `.html`
|
|
60
|
+
* transform) may contain spans copied from another file — a `<script
|
|
61
|
+
* src="app.js">` body, say. Nodes inside such a span keep that file, so a
|
|
62
|
+
* diagnostic points at real source and a plain-`.js` script stays exempt
|
|
63
|
+
* from the type checker. */
|
|
64
|
+
const originOf = (record: ModuleRecord, node: Node): string => {
|
|
65
|
+
const ranges = record.originRanges;
|
|
66
|
+
if (ranges === undefined) return record.path;
|
|
67
|
+
for (const range of ranges) {
|
|
68
|
+
if (node.start >= range.start && node.end <= range.end) return range.file;
|
|
69
|
+
}
|
|
70
|
+
return record.path;
|
|
71
|
+
};
|
|
72
|
+
const trackOrigin = (record: ModuleRecord, root: Node): void => {
|
|
73
|
+
const visit = (node: Node): void => {
|
|
74
|
+
origins.set(node, originOf(record, node));
|
|
75
|
+
const fields = node as unknown as Record<string, unknown>;
|
|
76
|
+
for (const key of Object.keys(fields)) {
|
|
77
|
+
if (key === "kind" || key === "start" || key === "end" || key === "text" || key === "value" || key === "raw") {
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
const value = fields[key];
|
|
81
|
+
if (Array.isArray(value)) {
|
|
82
|
+
for (const child of value) if (isNode(child)) visit(child);
|
|
83
|
+
} else if (isNode(value)) {
|
|
84
|
+
visit(value);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
visit(root);
|
|
89
|
+
};
|
|
90
|
+
const push = (record: ModuleRecord, statement: Statement): void => {
|
|
91
|
+
trackOrigin(record, statement);
|
|
92
|
+
merged.push(statement);
|
|
93
|
+
};
|
|
94
|
+
const first = records[records.length - 1]!;
|
|
95
|
+
for (const record of records) {
|
|
96
|
+
if (deferredModules.has(record.path)) {
|
|
97
|
+
/* A deferred module is evaluated by the wrapper when a script runs. Its
|
|
98
|
+
* external imports stay top-level (codegen resolves them there); eager
|
|
99
|
+
* namespace objects likewise. */
|
|
100
|
+
const topLevel: Statement[] = [];
|
|
101
|
+
const body: Statement[] = [];
|
|
102
|
+
for (const statement of record.sourceFile.statements) {
|
|
103
|
+
if (statement.kind === SyntaxKind.ImportDeclaration) {
|
|
104
|
+
const declaration = statement as ImportDeclaration;
|
|
105
|
+
const resolution = classifyDependency(
|
|
106
|
+
dirname(record.path), declaration.moduleSpecifier.value, externalSpecifiers, moduleOverrides,
|
|
107
|
+
);
|
|
108
|
+
if (resolution.kind === "external") topLevel.push(statement);
|
|
109
|
+
const synthetic = namespaceStatements.get(statement);
|
|
110
|
+
if (synthetic) topLevel.push(synthetic);
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
if (statement.kind === SyntaxKind.ExportDeclaration) {
|
|
114
|
+
const declaration = statement as ExportDeclaration;
|
|
115
|
+
const inner = (declaration as unknown as { declaration?: Statement }).declaration;
|
|
116
|
+
if (inner) body.push(inner);
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
if (statement.kind === SyntaxKind.ExportAssignment) {
|
|
120
|
+
body.push(exportAssignmentToStatement(record, statement as ExportAssignment));
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
stripExportModifiers(statement);
|
|
124
|
+
body.push(statement);
|
|
125
|
+
}
|
|
126
|
+
for (const statement of topLevel) push(record, statement);
|
|
127
|
+
for (const statement of deferredModuleFunctions(
|
|
128
|
+
record,
|
|
129
|
+
deferredAliases.get(record.path) ?? [],
|
|
130
|
+
body,
|
|
131
|
+
)) {
|
|
132
|
+
push(record, statement);
|
|
133
|
+
}
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
for (const statement of record.sourceFile.statements) {
|
|
137
|
+
if (statement.kind === SyntaxKind.ImportDeclaration) {
|
|
138
|
+
// External (extension / built-in) imports stay in place for codegen.
|
|
139
|
+
const declaration = statement as ImportDeclaration;
|
|
140
|
+
const resolution = classifyDependency(
|
|
141
|
+
dirname(record.path), declaration.moduleSpecifier.value, externalSpecifiers, moduleOverrides,
|
|
142
|
+
);
|
|
143
|
+
if (resolution.kind === "external") {
|
|
144
|
+
push(record, statement);
|
|
145
|
+
} else {
|
|
146
|
+
const synthetic = namespaceStatements.get(statement);
|
|
147
|
+
if (synthetic) push(record, synthetic);
|
|
148
|
+
}
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
if (statement.kind === SyntaxKind.ExportDeclaration) {
|
|
152
|
+
const declaration = statement as ExportDeclaration;
|
|
153
|
+
const synthetic = namespaceStatements.get(statement);
|
|
154
|
+
if (synthetic) {
|
|
155
|
+
push(record, synthetic);
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
const inner = (declaration as unknown as { declaration?: Statement }).declaration;
|
|
159
|
+
if (inner) push(record, inner);
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
if (statement.kind === SyntaxKind.ExportAssignment) {
|
|
163
|
+
const assignment = statement as ExportAssignment;
|
|
164
|
+
push(record, exportAssignmentToStatement(record, assignment));
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
push(record, statement);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/* A marker or dynamic-import target that stayed eager still needs a wrapper:
|
|
172
|
+
* its exports are already initialised. */
|
|
173
|
+
for (const record of records) {
|
|
174
|
+
if (deferredModules.has(record.path) || (!markerTargets.has(record.path) && !dynamicTargets.has(record.path))) continue;
|
|
175
|
+
for (const statement of eagerModuleFunctions(record)) push(record, statement);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
for (const record of records) {
|
|
179
|
+
for (const symbol of topLevelSymbols(record)) {
|
|
180
|
+
if (symbol.kind === SymbolKind.Import) continue;
|
|
181
|
+
const finalName = record.finalNames.get(symbol.id);
|
|
182
|
+
if (finalName) renameSymbol(symbol, finalName);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
return {
|
|
187
|
+
kind: SyntaxKind.SourceFile,
|
|
188
|
+
statements: merged,
|
|
189
|
+
fileName: first.path,
|
|
190
|
+
origins,
|
|
191
|
+
moduleTexts: new Map(records.map((record) => [record.path, record.text])),
|
|
192
|
+
modulePrefixes: records.map((record) => record.prefix),
|
|
193
|
+
text: records.map((record) => record.text).join("\n"),
|
|
194
|
+
start: 0,
|
|
195
|
+
end: 0,
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** Drop `export` / `export default` from a statement moved into a wrapper. */
|
|
200
|
+
function stripExportModifiers(statement: Statement): void {
|
|
201
|
+
const modifiers = (statement as { modifiers?: Modifier[] }).modifiers;
|
|
202
|
+
if (!modifiers) return;
|
|
203
|
+
(statement as { modifiers: Modifier[] }).modifiers = modifiers.filter(
|
|
204
|
+
(modifier) => modifier.modifierKind !== ModifierKind.Export && modifier.modifierKind !== ModifierKind.Default,
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** True for anything shaped like an AST node (constructed nodes carry no trivia). */
|
|
209
|
+
function isNode(value: unknown): value is Node {
|
|
210
|
+
return !!value && typeof value === "object" && "kind" in (value as object);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** Turn `export default expr` into a synthetic `const` declaration. */
|
|
214
|
+
function exportAssignmentToStatement(record: ModuleRecord, assignment: ExportAssignment): Statement {
|
|
215
|
+
const name: Identifier = {
|
|
216
|
+
kind: SyntaxKind.Identifier,
|
|
217
|
+
text: `${record.prefix}default`,
|
|
218
|
+
start: assignment.start,
|
|
219
|
+
end: assignment.start,
|
|
220
|
+
};
|
|
221
|
+
record.exports.set("default", name.text);
|
|
222
|
+
const declaration: VariableDeclaration = {
|
|
223
|
+
kind: SyntaxKind.VariableDeclaration,
|
|
224
|
+
name,
|
|
225
|
+
exclamation: false,
|
|
226
|
+
initializer: assignment.expression,
|
|
227
|
+
start: assignment.start,
|
|
228
|
+
end: assignment.end,
|
|
229
|
+
};
|
|
230
|
+
const list: VariableDeclarationList = {
|
|
231
|
+
kind: SyntaxKind.VariableDeclarationList,
|
|
232
|
+
declarationKind: "const" as const,
|
|
233
|
+
declarations: [declaration],
|
|
234
|
+
start: assignment.start,
|
|
235
|
+
end: assignment.end,
|
|
236
|
+
};
|
|
237
|
+
return {
|
|
238
|
+
kind: SyntaxKind.VariableStatement,
|
|
239
|
+
declarationList: list,
|
|
240
|
+
modifiers: [],
|
|
241
|
+
start: assignment.start,
|
|
242
|
+
end: assignment.end,
|
|
243
|
+
};
|
|
244
|
+
}
|