diff --git a/.github/workflows/bindings-test.yml b/.github/workflows/bindings-test.yml index 291ad38e8..419c5209f 100644 --- a/.github/workflows/bindings-test.yml +++ b/.github/workflows/bindings-test.yml @@ -1,7 +1,7 @@ name: Binding packages test -# bindings.yml on the api fixture, whose Python consumers must print native Rust's transcript -# byte for byte, and on moq's stand-in facade at the exporter's moq pin, its first consumer. +# bindings.yml on the api fixture, whose Python and web consumers must print native Rust's +# transcript byte for byte, and on moq's stand-in facade at the exporter's moq pin, its first consumer. # Read-only: nothing here publishes. on: workflow_call: @@ -18,6 +18,9 @@ jobs: features: doubled example: transcript python-check: ffi/tests/api.py + web-manifest-path: ffi/fixtures/api/Cargo.toml + scope: kixel-ai + web-check: ffi/tests/api.ts # A module path a proxy could serve. No go-check, as no swift-check: api.go needs the # wasm32 items this export lacks, so the consumer only loads the runtime. go-module: example.com/api @@ -31,5 +34,8 @@ jobs: manifest-path: ffi/target/moq-ffi/Cargo.toml python-check: ffi/moq/moq.py swift-check: ffi/moq/moq.swift + # moq's wasm32 stand-in, which leaves moq-tokio out, as no wasm32 target builds it. + web-manifest-path: ffi/target/moq-ffi-web/Cargo.toml + scope: kixel-ai # Under the export's own module, `moq`, which moq.go imports. go-check: ffi/moq/moq.go diff --git a/.github/workflows/bindings.yml b/.github/workflows/bindings.yml index 67ee13861..55d987368 100644 --- a/.github/workflows/bindings.yml +++ b/.github/workflows/bindings.yml @@ -1,8 +1,8 @@ name: Binding packages -# The shared, read-only builder of a crate's rust2-ffi Python wheels, Swift package, and Go -# module, which -# a release workflow ffi/release/package.py's `init` writes calls at a pinned rust2 commit. +# The shared, read-only builder of a crate's rust2-ffi Python wheels, Swift package, Go +# module, and web package, which a release workflow ffi/release/package.py's `init` writes +# calls at a pinned rust2 commit. # It publishes nothing and holds no credentials: the caller's own jobs publish what it # uploads, once its consumers pass. See ffi/release/README.md. on: @@ -28,6 +28,26 @@ on: description: The main.swift of an executable run on the installed Swift package default: '' type: string + web-manifest-path: + description: The Cargo.toml of the package whose wasm32-wasip1 export the npm and JSR package holds, often manifest-path; without it, no web package + default: '' + type: string + scope: + description: The npm and JSR scope the web package is named under, @/, without its @ + default: '' + type: string + web-check: + description: A TypeScript module run on the installed npm package, importing the binding as ./web/.ts does beside the export + default: '' + type: string + web-features: + description: Cargo features of a web crate apart from manifest-path's, which takes features + default: '' + type: string + web-example: + description: The example of a web crate apart from manifest-path's whose stdout web-check must print, which takes example + default: '' + type: string go-module: description: The Go module's path; without it, the export's own, which no proxy serves default: '' @@ -46,6 +66,9 @@ on: module: description: The Swift module, whose XCFramework is FFI.xcframework.zip value: ${{ jobs.facts.outputs.module }} + web: + description: Whether the build made the web package, -web + value: ${{ inputs.web-manifest-path != '' }} permissions: contents: read @@ -65,6 +88,7 @@ jobs: tag: ${{ steps.facts.outputs.tag }} module: ${{ steps.facts.outputs.module }} fixture: ${{ steps.mode.outputs.fixture }} + web-fixture: ${{ steps.mode.outputs.web-fixture }} rust2: ${{ steps.identity.outputs.workflow_repository }} rust2-sha: ${{ steps.identity.outputs.workflow_sha }} steps: @@ -82,12 +106,18 @@ jobs: - id: mode env: RUST2: ${{ steps.identity.outputs.workflow_repository }} + WEB_MANIFEST: ${{ inputs.web-manifest-path }} run: | if [[ "$RUST2" == "$GITHUB_REPOSITORY" && "$MANIFEST" == ffi/target/moq-ffi/Cargo.toml ]]; then echo fixture=true >> "$GITHUB_OUTPUT" else echo fixture=false >> "$GITHUB_OUTPUT" fi + if [[ "$RUST2" == "$GITHUB_REPOSITORY" && "$WEB_MANIFEST" == ffi/target/moq-ffi-web/Cargo.toml ]]; then + echo web-fixture=true >> "$GITHUB_OUTPUT" + else + echo web-fixture=false >> "$GITHUB_OUTPUT" + fi - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: path: source @@ -433,6 +463,100 @@ jobs: MODULE: ${{ needs.facts.outputs.module }} run: bash rust2/ffi/release/consume-swift.sh swift apple + # The web binding's npm and JSR packages: web-manifest-path exported for wasm32-wasip1 + # alone, the binding's only target, and its module built beside it; with native Rust's + # output on that target, which the consumers compare with. + web: + needs: facts + if: inputs.web-manifest-path != '' + runs-on: ubuntu-24.04 + timeout-minutes: 60 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + path: source + persist-credentials: false + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + repository: ${{ needs.facts.outputs.rust2 }} + ref: ${{ needs.facts.outputs.rust2-sha }} + path: rust2 + persist-credentials: false + - uses: taiki-e/install-action@3a3daafd3b7f23b8f65d9f5ba2db203fb59b79e2 # v2.90.2 + if: needs.facts.outputs.web-fixture == 'true' + with: + tool: just + - name: Stand-in + if: needs.facts.outputs.web-fixture == 'true' + working-directory: source + run: just ffi standin "" web && cp ffi/moq/web.toml ffi/target/moq-ffi-web/ffi.toml + # npm packs, and Node's WASI runs the native example. + - uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 + with: + node-version: '24' + - name: Exporter + working-directory: rust2/ffi + run: cargo build --locked --release + # The crate's own features and example, or a web crate's apart from it. + - name: Package + env: + WEB_MANIFEST: ${{ inputs.web-manifest-path }} + SCOPE: ${{ inputs.scope }} + VERSION: ${{ needs.facts.outputs.version }} + EXAMPLE: ${{ inputs.example }} + WEB_FEATURES: ${{ inputs.web-features }} + WEB_EXAMPLE: ${{ inputs.web-example }} + WEB_CHECK: ${{ inputs.web-check }} + run: | + if [[ "$(realpath -m "source/$WEB_MANIFEST")" == "$(realpath -m "source/$MANIFEST")" ]]; then + if [[ -n "$WEB_FEATURES$WEB_EXAMPLE" ]]; then + echo "::error::web-features and web-example are for a web crate apart from manifest-path's: set features and example" + exit 1 + fi + features=$FEATURES example=$EXAMPLE + else + features=$WEB_FEATURES example=$WEB_EXAMPLE + fi + python3 -I rust2/ffi/release/package.py web --manifest-path "source/$WEB_MANIFEST" --features "$features" --scope "$SCOPE" --version "$VERSION" --example "$example" --out web + if [[ -n "$WEB_CHECK" ]]; then cp "source/$WEB_CHECK" web/check.ts; fi + - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: ${{ needs.facts.outputs.name }}-web + path: web + if-no-files-found: error + + # A new npm project depending on only the packed tarball, as an app's would, run on + # Node, Deno, and Bun and bundled by Vite and webpack; and JSR's checks of its package. + web-consumer: + needs: [facts, web] + runs-on: ubuntu-24.04 + timeout-minutes: 20 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + repository: ${{ needs.facts.outputs.rust2 }} + ref: ${{ needs.facts.outputs.rust2-sha }} + path: rust2 + sparse-checkout: ffi/release + persist-credentials: false + - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + name: ${{ needs.facts.outputs.name }}-web + path: web + - uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 + with: + node-version: '24' + - uses: denoland/setup-deno@22d081ff2d3a40755e97629de92e3bcbfa7cf2ed # v2.0.5 + with: + deno-version: v2.x + - uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2.2.0 + - name: Install and run + run: | + args=() + if [[ -f web/check.ts ]]; then args+=(--check web/check.ts); fi + if [[ -f web/native.txt ]]; then args+=(--native web/native.txt); fi + python3 -I rust2/ffi/release/package.py consume-web --dist web "${args[@]}" + # The module as a GOPROXY serves it: the export's packages, and each Go target's library # beside the runtime that links it. go: diff --git a/bench/ffi/README.md b/bench/ffi/README.md index 121f5c0d3..55e09955f 100644 --- a/bench/ffi/README.md +++ b/bench/ffi/README.md @@ -430,12 +430,21 @@ revision later, on the same host and load (1 and 3 rounds). | Swift | 3 | 2,147 KiB | 302 KiB | none | | Go | 23 | 3,554 KiB | 435 KiB | none | | Dart | 1 | 4,034 KiB | 376 KiB | 1,643 KiB / 62 KiB | +| TypeScript | 684 | 4,498 KiB | 1,000 KiB | 4,535 KiB / 259 KiB | | native library | as built | stripped | |---|---|---| | `libmoq.a`, the C ABI that C, C++, Swift, and Go link | 193,455 KiB | 104,224 KiB | | `libmoq.so`, with the Kotlin and Dart glue | 61,361 KiB | 46,029 KiB | | `python/libmoq.so`, the CPython extension | 56,145 KiB | 42,166 KiB | +| `moq.wasm`, the wasm32 stand-in's module, with the web glue | 19,596 KiB | 17,632 KiB | + +| web binding | raw | gzip | brotli | +|---|---|---|---| +| `moq.wasm`, stripped | 17,632 KiB | 4,657 KiB | 2,893 KiB | +| the TypeScript, 684 modules | 4,498 KiB | 1,000 KiB | 855 KiB | +| the npm package's JavaScript and declarations | 5,670 KiB | 1,454 KiB | 1,226 KiB | +| `export.ts` bundled by esbuild, minified | 23 KiB | 7 KiB | 7 KiB | The stand-in exports all of moq-net, kio, and moq-pattern, and hang, moq-mux, and moq-tokio's session side, far more than moq-ffi's UniFFI interface, so its sizes do not @@ -445,6 +454,17 @@ but Python's links, and `src/jni`, `src/python`, and `src/dart`. The native libr the export's default `release` build, which links all of moq's crates; "stripped" is `strip --strip-all` on a copy. +The web rows (measured 2026-11-19 on fedora at the same moq pin, with this table's other +rows from 2026-11-13) are the wasm32 stand-in's export, which leaves out moq-tokio, built +with moq's release profile: fat LTO at `opt-level = 3`. A module "stripped" has no custom +section, its names among them; each file is compressed alone, gzip at level 6 and brotli at +11, as a host serves it. The release builder makes the packaged module at `opt-level = +"s"` when the crate sets no level: 13,359 KiB as built, 10,981 KiB stripped, 3,250 KiB +gzipped, and 2,135 KiB brotli'd. A caller pays for what it imports: `export.ts`, the +TypeScript benchmark, names 13 of the 684 modules, and esbuild's bundle of it and the +binding is 23 KiB, against 4.5 MiB of TypeScript. JSR caps a version at 20 MiB, which the +TypeScript and the `opt-level = 3` module pass, and the `"s"` module fits. + ### What a call spends From the CPU tables of 2026-11-10, measured at rust2 `7383790d` at a load average of 22 to diff --git a/bench/ffi/report.py b/bench/ffi/report.py index d4bca35ce..60c2c8ad0 100644 --- a/bench/ffi/report.py +++ b/bench/ffi/report.py @@ -2,9 +2,10 @@ in CPU and wall time, each beside the median 1-minute load average over its rounds, read from `just ffi bench-run`'s raw.tsv; the median time and peak RSS of the export and its crate's build, beside moq's own, from its build.tsv; and the sizes of the export's generated bindings and -native libraries, and of the wasm32 stand-in's TypeScript and module. +native libraries, and of the wasm32 stand-in's TypeScript, module, and npm package, and of +one caller's bundle. -Usage: report.py +Usage: report.py """ import collections @@ -85,6 +86,14 @@ # The TypeScript, under the wasm32 stand-in's export, and the glue its module builds with. WEB_BINDING = ("TypeScript", ["web"], ["src/lib.rs", "src/rt.rs", "src/web"]) + +# Brotli at its densest, as a host precompresses what it serves; Python's standard library +# lacks it, so Node's. +BROTLI = ( + "const zlib = require('node:zlib');" + "const data = require('node:fs').readFileSync(0);" + "process.stdout.write(String(zlib.brotliCompressSync(data, {params: {[zlib.constants.BROTLI_PARAM_QUALITY]: 11}}).length));" +) WASM = ("`moq.wasm`, the wasm32 stand-in's module, with the web glue", "target/wasm32-wasip1/release/moq.wasm") # The shared library's suffix, as `just ffi moq-export` names it on each platform it builds on. @@ -124,6 +133,38 @@ def size(paths): return raw, zipped +def typescript(path): + """A TypeScript module the web binding writes, not its JavaScript or declarations.""" + return path.endswith(".ts") and not path.endswith(".d.ts") + + +def compressed(data): + """The raw, gzipped, and brotli'd bytes of `data`.""" + brotli = subprocess.run(["node", "-e", BROTLI], input=data, capture_output=True, check=True).stdout + return len(data), len(gzip.compress(data, compresslevel=6, mtime=0)), int(brotli) + + +def sizes(paths): + """The raw, gzipped, and brotli'd bytes of `paths`, each compressed alone as a host serves it.""" + total = [0, 0, 0] + for path in paths: + with open(path, "rb") as f: + for i, n in enumerate(compressed(f.read())): + total[i] += n + return total + + +def bundle(caller): + """`caller` and what it imports of the binding, bundled for the browser, minified and + tree-shaken, as an app's build ships it.""" + with tempfile.TemporaryDirectory() as tmp: + out = os.path.join(tmp, "bundle.js") + subprocess.run(["esbuild", caller, "--bundle", "--minify", "--format=esm", "--platform=browser", + "--target=es2024", "--log-level=warning", f"--outfile={out}"], check=True) + with open(out, "rb") as f: + return f.read() + + def wasm_stripped(data): """A wasm module without its custom sections, its names and debug info among them.""" out = bytearray(data[:8]) @@ -175,7 +216,7 @@ def builds(path): return table(["step", "wall s", "CPU s", "peak RSS", "load average"], rows) -def main(raw_path, build_path, export, web): +def main(raw_path, build_path, export, web, caller): rounds = collections.defaultdict(list) with open(raw_path) as f: for line in f: @@ -224,9 +265,10 @@ def cell(lang, case, i): print("### Size\n") rows = [] for root, (name, generated, glue) in [(export, b) for b in BINDINGS] + [(web, WEB_BINDING)]: - raw, zipped = size(files(root, generated)) + ours = [p for p in files(root, generated) if root != web or typescript(p)] + raw, zipped = size(ours) glue_raw, glue_zipped = size(files(root, glue)) - count = len(files(root, generated)) + count = len(ours) glue_cell = f"{kib(glue_raw)} / {kib(glue_zipped)}" if glue else "none" rows.append([name, str(count), kib(raw), kib(zipped), glue_cell]) print(table(["binding", "files", "raw", "gzip", "Rust glue, raw / gzip"], rows)) @@ -246,8 +288,20 @@ def cell(lang, case, i): rows.append([label, kib(len(data)), kib(len(wasm_stripped(data)))]) print(table(["native library", "as built", "stripped"], rows)) + print() + everything = files(web, ["web"]) + modules = [p for p in everything if typescript(p)] + package = [p for p in everything if p.endswith(".js") or p.endswith(".d.ts")] + rows = [ + ["`moq.wasm`, stripped", *map(kib, compressed(wasm_stripped(data)))], + [f"the TypeScript, {len(modules)} modules", *map(kib, sizes(modules))], + ["the npm package's JavaScript and declarations", *map(kib, sizes(package))], + [f"`{os.path.basename(caller)}` bundled by esbuild, minified", *map(kib, compressed(bundle(caller)))], + ] + print(table(["web binding", "raw", "gzip", "brotli"], rows)) + if __name__ == "__main__": - if len(sys.argv) != 5: + if len(sys.argv) != 6: sys.exit(__doc__) - main(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4]) + main(*sys.argv[1:]) diff --git a/ffi/Cargo.lock b/ffi/Cargo.lock index 7af119daf..e74618aaf 100644 --- a/ffi/Cargo.lock +++ b/ffi/Cargo.lock @@ -2,6 +2,12 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "adler2" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" + [[package]] name = "aho-corasick" version = "1.1.5" @@ -11,6 +17,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "allocator-api2" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923" + [[package]] name = "api-fixture" version = "0.0.0" @@ -25,6 +37,28 @@ dependencies = [ "url", ] +[[package]] +name = "autocfg" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53" + +[[package]] +name = "base64" +version = "0.23.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac07cdecf99051d9a5238b80f35af32cdeba5b336e55d957b318b50137e18da5" + +[[package]] +name = "base64-simd" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "339abbe78e73178762e23bea9dfd08e697eb3f3301cd4be981c0f78ba5859195" +dependencies = [ + "outref", + "vsimd", +] + [[package]] name = "bitflags" version = "2.13.2" @@ -37,12 +71,27 @@ version = "3.20.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "87a0cf66fd1eb2fb7b822cb78370d82303750809bc6ba855f3f58e517d3238db" +[[package]] +name = "bytecount" +version = "0.6.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "175812e0be2bccb6abe50bb8d566126198344f707e304f45c648fd8f2cc0365e" + [[package]] name = "bytes" version = "1.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04" +[[package]] +name = "castaway" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dec551ab6e7578819132c713a93c022a05d60159dc86e7a7050223577484c55a" +dependencies = [ + "rustversion", +] + [[package]] name = "cc" version = "1.11.2" @@ -59,6 +108,15 @@ version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600" +[[package]] +name = "cobs" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fa961b519f0b462e3a3b4a34b64d119eeaca1d59af726fe450bbba07a9fc0a1" +dependencies = [ + "thiserror", +] + [[package]] name = "combine" version = "4.6.9" @@ -69,6 +127,25 @@ dependencies = [ "memchr", ] +[[package]] +name = "compact_str" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79fcda08c33bb58b97008b2cdada6622500e949e060f5913361763121abd2416" +dependencies = [ + "castaway", + "cfg-if", + "itoa", + "static_assertions", + "zmij", +] + +[[package]] +name = "cow-utils" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "417bef24afe1460300965a25ff4a24b8b45ad011948302ec221e8a0a81eb2c79" + [[package]] name = "dep-fixture" version = "0.0.0" @@ -84,12 +161,42 @@ dependencies = [ "syn 3.0.9", ] +[[package]] +name = "either" +version = "1.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e9c71c2167ca323c882b99918929403426e2373ea17242ff5653e0d5e1058be" + +[[package]] +name = "embedded-io" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef1a6892d9eef45c8fa6b9e0086428a2cca8491aca8f787c534a3d6d0bcb3ced" + +[[package]] +name = "embedded-io" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edd0f118536f44f5ccd48bcb8b111bdc3de888b58c74639dfb034a357d0f206d" + [[package]] name = "equivalent" version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "00d174d5400e5e8fd687ad1049e2f578285fa914201b1af7e8b112a4546bd826" +[[package]] +name = "escape-simd" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0440157235aa4badd45ea073af8de2ba44ffead74bf8a7833b215533c36f08e" + +[[package]] +name = "fastrand" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da7c62ceae207dd37ea5b845da6a0696c799f85e97da1ab5b7910be3c1c80223" + [[package]] name = "find-msvc-tools" version = "0.1.15" @@ -148,6 +255,15 @@ name = "hashbrown" version = "0.17.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" +dependencies = [ + "allocator-api2", +] + +[[package]] +name = "hmac-sha1-compact" +version = "1.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5286b48a73829e3541cd9f533359c01150f7879e2716796eda0631ad5fe7e668" [[package]] name = "icu_collections" @@ -171,11 +287,32 @@ checksum = "25d29b57c17c6528d0dc4c2141cfa2d08200465acb537a3909c8b9b8a4a984c0" dependencies = [ "displaydoc", "litemap", + "serde", "tinystr", "writeable", "zerovec", ] +[[package]] +name = "icu_locale_fallback" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "406e9ae5b95d8aa37db6293405931b766a6172d1848daee22d70eb44aa78a43c" +dependencies = [ + "icu_locale_core", + "icu_locale_fallback_data", + "icu_provider", + "potential_utf", + "tinystr", + "zerovec", +] + +[[package]] +name = "icu_locale_fallback_data" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3632ebbd0c5c3637ba1a40bdf8288f2cb9d2268e73441a69d5fc73d7cfbaa87c" + [[package]] name = "icu_normalizer" version = "2.4.0" @@ -225,6 +362,8 @@ checksum = "413099315636c588ee8f011190b329b971a09a8fae9d157e01dcae8379468514" dependencies = [ "displaydoc", "icu_locale_core", + "serde", + "stable_deref_trait", "writeable", "yoke", "zerofrom", @@ -232,6 +371,29 @@ dependencies = [ "zerovec", ] +[[package]] +name = "icu_segmenter" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61b195dc605f81aa181bec9b3bd14e26cc71b903e4644f6af578ca9772c7a710" +dependencies = [ + "icu_collections", + "icu_locale_fallback", + "icu_provider", + "icu_segmenter_data", + "potential_utf", + "smallvec", + "utf8_iter", + "zerotrie", + "zerovec", +] + +[[package]] +name = "icu_segmenter_data" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "913a7d294ad056474749152107309fdee3ec17d2482d1ab70fdc61a81c64b9a3" + [[package]] name = "idna" version = "1.1.0" @@ -263,6 +425,15 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "itertools" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b4baf93f58d4425749ca49a51c50ebab072c5df6994d08fed93541c331481dc" +dependencies = [ + "either", +] + [[package]] name = "itoa" version = "1.0.18" @@ -329,6 +500,15 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "json-escape-simd" +version = "3.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d302fe9761f19c4123edfe82b66be55a08e54012403cd7e27f8a176538737954" +dependencies = [ + "escape-simd", +] + [[package]] name = "lacks-fixture" version = "0.0.0" @@ -392,6 +572,21 @@ version = "2.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98" +[[package]] +name = "miniz_oxide" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b63fbc4a50860e98e7b2aa7804ded1db5cbc3aff9193adaff57a6931bf7c4b4c" +dependencies = [ + "adler2", +] + +[[package]] +name = "nonmax" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "610a5acd306ec67f907abe5567859a3c693fb9886eb1f012ab8f2a47bef3db51" + [[package]] name = "nu-ansi-term" version = "0.50.3" @@ -401,30 +596,519 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "num-bigint" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93e7820bc0a80a0238e650327316f929ba18d5be054b647490a3a6a339f3e7c0" +dependencies = [ + "num-integer", + "num-traits", +] + +[[package]] +name = "num-integer" +version = "0.1.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ce2d95d4b3734dc35aa2f45e1aa22cd416814592a4f9d9205e11affd5b8e10b" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + [[package]] name = "once_cell" version = "1.21.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" +[[package]] +name = "outref" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a80800c0488c3a21695ea981a54918fbb37abf04f4d0720c453632255e2ff0e" + +[[package]] +name = "oxc" +version = "0.159.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "467b554224f10c87093d827f7d46912e848cbe725ed53aa2e1fa7bba8d64463c" +dependencies = [ + "oxc_allocator", + "oxc_ast", + "oxc_codegen", + "oxc_diagnostics", + "oxc_isolated_declarations", + "oxc_parser", + "oxc_semantic", + "oxc_span", + "oxc_str", + "oxc_syntax", + "oxc_transformer", + "oxc_transformer_plugins", +] + +[[package]] +name = "oxc-browserslist" +version = "5.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "456720daa870e4f69f8d29fded1c8deea82da4780a6b8553baa160eb0cd8d864" +dependencies = [ + "js-sys", + "libc", + "miniz_oxide", + "postcard", + "rustc-hash", + "serde", + "thiserror", +] + +[[package]] +name = "oxc_allocator" +version = "0.159.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aaf8c5fa53291fec5f52e179974cc58cd002e45eab0defdbdfb6b36a1809c9ef" +dependencies = [ + "allocator-api2", + "hashbrown", + "oxc_data_structures", + "rustc-hash", +] + +[[package]] +name = "oxc_ast" +version = "0.159.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc5ac049e885e553878688ca583184cc2b8de02177914328f59616e59ffc224f" +dependencies = [ + "bitflags", + "oxc_allocator", + "oxc_ast_macros", + "oxc_data_structures", + "oxc_diagnostics", + "oxc_estree", + "oxc_regular_expression", + "oxc_span", + "oxc_str", + "oxc_syntax", +] + +[[package]] +name = "oxc_ast_macros" +version = "0.159.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb36e1df5e540ebd898865ed79ffd8d8f9e06310521dc0d8e5b9cc4567fc6739" +dependencies = [ + "phf", + "proc-macro2", + "quote", + "syn 3.0.9", +] + +[[package]] +name = "oxc_ast_visit" +version = "0.159.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a019293646229f79954cf7c5d022a13324c84e1b874e8ce030c8e7c0a56e5b0f" +dependencies = [ + "oxc_allocator", + "oxc_ast", + "oxc_span", + "oxc_syntax", +] + +[[package]] +name = "oxc_codegen" +version = "0.159.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a56802bea7d890e3c36215a28efc7623390c67abb4011d0d7c0be4e63c4e7411" +dependencies = [ + "bitflags", + "cow-utils", + "oxc_allocator", + "oxc_ast", + "oxc_data_structures", + "oxc_ecmascript", + "oxc_index", + "oxc_semantic", + "oxc_sourcemap", + "oxc_span", + "oxc_str", + "oxc_syntax", + "rustc-hash", + "smallvec", +] + +[[package]] +name = "oxc_compat" +version = "0.159.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1c101b66996fc42e0eb1185936398b06bc83761926ea745e736cbf58103bd4e" +dependencies = [ + "cow-utils", + "oxc-browserslist", + "oxc_syntax", + "rustc-hash", + "serde", +] + +[[package]] +name = "oxc_data_structures" +version = "0.159.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8932e4c5ad3c6973548bf2f7750b778315012ecd7086617721d23f17b43e5a55" +dependencies = [ + "ropey", +] + +[[package]] +name = "oxc_diagnostics" +version = "0.159.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "901588f26a8f1e7a0a8f7ebab60bf311bc9a1d22935e740a1b545c33b020f29f" +dependencies = [ + "bytecount", + "cow-utils", + "itoa", + "memchr", + "oxc_span", + "percent-encoding", + "smallvec", + "textwrap", + "unicode-segmentation", + "unicode-width", +] + +[[package]] +name = "oxc_ecmascript" +version = "0.159.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4127c1685ec3e85c34bcbe9b4055b1ce34bc0e2614c4bf5cf56c52a5424c7fc5" +dependencies = [ + "itoa", + "memchr", + "num-bigint", + "num-traits", + "oxc_ast", + "oxc_data_structures", + "oxc_span", + "oxc_str", + "oxc_syntax", + "zmij_ecma", +] + +[[package]] +name = "oxc_estree" +version = "0.159.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00c806bed591dc97e36678eeaaccb6e85ecc21766eaf6f8a77e7a261a4457a2c" + +[[package]] +name = "oxc_index" +version = "5.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c448ca664753487e107221c172cb54ac55b500d6a8d7d126507a837f626c1f26" +dependencies = [ + "nonmax", + "serde", +] + +[[package]] +name = "oxc_isolated_declarations" +version = "0.159.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0d18a51a51d2fc43bf8510de83e118cda410bb3109d9a4ca9b512c86569c462" +dependencies = [ + "bitflags", + "oxc_allocator", + "oxc_ast", + "oxc_ast_visit", + "oxc_diagnostics", + "oxc_ecmascript", + "oxc_span", + "oxc_str", + "oxc_syntax", + "rustc-hash", +] + +[[package]] +name = "oxc_parser" +version = "0.159.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fd93cb9811da8634acfd28a864889d1bf68bce2c546a52372205afabd4a63e7" +dependencies = [ + "bitflags", + "cow-utils", + "memchr", + "num-bigint", + "num-traits", + "oxc_allocator", + "oxc_ast", + "oxc_data_structures", + "oxc_diagnostics", + "oxc_ecmascript", + "oxc_regular_expression", + "oxc_span", + "oxc_str", + "oxc_syntax", + "rustc-hash", + "seq-macro", +] + +[[package]] +name = "oxc_regular_expression" +version = "0.159.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c85fe16e0f852ba1b1085f08efec7b9d611ecaba1851ca6fecb7f91b09631714" +dependencies = [ + "bitflags", + "oxc_allocator", + "oxc_ast_macros", + "oxc_data_structures", + "oxc_diagnostics", + "oxc_span", + "oxc_str", + "phf", + "rustc-hash", + "unicode-id-start", +] + +[[package]] +name = "oxc_semantic" +version = "0.159.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c61b34d4fb57e1f91ed099708e09b7450960d34d3a706ee84be55f821da27021" +dependencies = [ + "itertools", + "memchr", + "oxc_allocator", + "oxc_ast", + "oxc_ast_visit", + "oxc_data_structures", + "oxc_diagnostics", + "oxc_ecmascript", + "oxc_index", + "oxc_span", + "oxc_str", + "oxc_syntax", + "rustc-hash", + "self_cell", + "smallvec", +] + +[[package]] +name = "oxc_sourcemap" +version = "10.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ca9998ccb8eabb05099c5827ce983cca9f5dfc008c6c47d7dcc034cee301e91" +dependencies = [ + "base64-simd", + "json-escape-simd", + "rustc-hash", + "serde", + "serde_json", +] + +[[package]] +name = "oxc_span" +version = "0.159.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0bb9aed024358d262b9daab20b7aa7ae89b42946e549b52ff92ddfef033d0e1" +dependencies = [ + "oxc_allocator", + "oxc_ast_macros", + "oxc_estree", +] + +[[package]] +name = "oxc_str" +version = "0.159.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b51303dd938569998ad07110fff48d76da59cfe47748a39833c4652424babb4" +dependencies = [ + "compact_str", + "hashbrown", + "memchr", + "oxc_allocator", + "oxc_estree", + "oxc_span", +] + +[[package]] +name = "oxc_syntax" +version = "0.159.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f832591474dac1e451b6619d9e9d6187f08445d9d73c47b331dedc34439eb81" +dependencies = [ + "bitflags", + "cow-utils", + "nonmax", + "oxc_allocator", + "oxc_ast_macros", + "oxc_estree", + "oxc_index", + "oxc_span", + "oxc_str", + "phf", + "unicode-id-start", + "zmij_ecma", +] + +[[package]] +name = "oxc_transformer" +version = "0.159.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85e22f92c0a7005978913a68a8dd6af050bc5f1323724211452e46c6ac1e10e1" +dependencies = [ + "base64", + "compact_str", + "hmac-sha1-compact", + "indexmap", + "itoa", + "memchr", + "oxc_allocator", + "oxc_ast", + "oxc_ast_visit", + "oxc_compat", + "oxc_data_structures", + "oxc_diagnostics", + "oxc_ecmascript", + "oxc_regular_expression", + "oxc_semantic", + "oxc_span", + "oxc_str", + "oxc_syntax", + "oxc_traverse", + "rustc-hash", + "serde", + "serde_json", +] + +[[package]] +name = "oxc_transformer_plugins" +version = "0.159.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e65677ac2ce0519dfbf72738ed25a1cc5a31ede29b0847c72e914163c2fc1aef" +dependencies = [ + "cow-utils", + "itoa", + "oxc_allocator", + "oxc_ast", + "oxc_ast_visit", + "oxc_diagnostics", + "oxc_ecmascript", + "oxc_parser", + "oxc_semantic", + "oxc_span", + "oxc_str", + "oxc_syntax", + "oxc_transformer", + "oxc_traverse", + "rustc-hash", +] + +[[package]] +name = "oxc_traverse" +version = "0.159.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87d49160912ade6eb58b7889a4be7b52df22397aef044de51a2dad7c66801d42" +dependencies = [ + "itoa", + "oxc_allocator", + "oxc_ast", + "oxc_ast_visit", + "oxc_data_structures", + "oxc_ecmascript", + "oxc_semantic", + "oxc_span", + "oxc_str", + "oxc_syntax", + "rustc-hash", +] + [[package]] name = "percent-encoding" version = "2.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" +[[package]] +name = "phf" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "010378780309880b08997fae13be7834dba947d36393bd372f2b1556deb2a2f6" +dependencies = [ + "phf_macros", + "phf_shared", + "serde", +] + +[[package]] +name = "phf_generator" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aeb62e0959d5a1bebc965f4d15d9e2b7cea002b6b0f5ba8cde6cc26738467100" +dependencies = [ + "fastrand", + "phf_shared", +] + +[[package]] +name = "phf_macros" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5fa8d0ca26d424d27630da600c6624696e7dec8bf7b3b492b383c5dc49e5e085" +dependencies = [ + "phf_generator", + "phf_shared", + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "phf_shared" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6fd9027e2d9319be6349febd1db4e8d02aa544921200c9b777720ac34a3aa89" +dependencies = [ + "siphasher", +] + [[package]] name = "pin-project-lite" version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" +[[package]] +name = "postcard" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6764c3b5dd454e283a30e6dfe78e9b31096d9e32036b5d1eaac7a6119ccb9a24" +dependencies = [ + "cobs", + "embedded-io 0.4.0", + "embedded-io 0.6.1", + "serde", +] + [[package]] name = "potential_utf" version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d83eb9bc6d8e5cf568e7a1101d60ee05e81ed50ea106026f3d18deeb046d7661" dependencies = [ + "serde_core", + "writeable", "zerovec", ] @@ -472,6 +1156,16 @@ dependencies = [ "tls-fixture", ] +[[package]] +name = "ropey" +version = "1.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93411e420bcd1a75ddd1dc3caf18c23155eda2c090631a85af21ba19e97093b5" +dependencies = [ + "smallvec", + "str_indices", +] + [[package]] name = "rust2-driver" version = "0.0.0" @@ -481,6 +1175,7 @@ name = "rust2-ffi" version = "0.0.0" dependencies = [ "loom", + "oxc", "rust2-driver", "serde", "serde_json", @@ -488,6 +1183,12 @@ dependencies = [ "wasmparser", ] +[[package]] +name = "rustc-hash" +version = "2.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b1e7f9a428571be2dc5bc0505c13fb6bf936822b894ec87abf8a08a4e51742d" + [[package]] name = "rustc_version" version = "0.4.1" @@ -518,12 +1219,24 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294" +[[package]] +name = "self_cell" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ab42ca02749e120097e328d91d415325bdf43b1c72c4c8badf37375fe40a813" + [[package]] name = "semver" version = "1.0.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a7852d02fc848982e0c167ef163aaff9cd91dc640ba85e263cb1ce46fae51cd" +[[package]] +name = "seq-macro" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bc711410fbe7399f390ca1c3b60ad0f53f80e95c5eb935e52268a0e2cd49acc" + [[package]] name = "serde" version = "1.0.229" @@ -607,6 +1320,12 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e3a9fe34e3e7a50316060351f37187a3f546bce95496156754b601a5fa71b76e" +[[package]] +name = "siphasher" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33f4fe9184a62d842c9ef383018f3306d8ba224fd9d836f56d7288308847c256" + [[package]] name = "slab" version = "0.4.12" @@ -618,6 +1337,15 @@ name = "smallvec" version = "1.16.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d0d5d4b1981b8bbbb7aed204cae5726d683d5d6ac7e74ec54122ea28f315e7c8" +dependencies = [ + "serde", +] + +[[package]] +name = "smawk" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8e2fb0f499abb4d162f2bedad68f5ef91a1682b5a03596ddb67efd37768d100" [[package]] name = "stable_deref_trait" @@ -625,6 +1353,18 @@ version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596" +[[package]] +name = "static_assertions" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" + +[[package]] +name = "str_indices" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d08889ec5408683408db66ad89e0e1f93dff55c73a4ccc71c427d5b277ee47e6" + [[package]] name = "syn" version = "2.0.119" @@ -658,6 +1398,17 @@ dependencies = [ "syn 3.0.9", ] +[[package]] +name = "textwrap" +version = "0.16.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ecfad6c3abc80a577f2b91c1e412ee57e7a060d430b553c1b0c940974ebcd49" +dependencies = [ + "icu_segmenter", + "smawk", + "unicode-width", +] + [[package]] name = "thiserror" version = "2.0.21" @@ -694,6 +1445,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "abc840c238154d58f9e86b2792261edfde565a17696e1cda1a449de27979801a" dependencies = [ "displaydoc", + "serde_core", "zerovec", ] @@ -789,12 +1541,30 @@ dependencies = [ "tracing-log", ] +[[package]] +name = "unicode-id-start" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0825d2e09dcae814a4273c1e1435979d414ede25b2ee235b1545da6682483765" + [[package]] name = "unicode-ident" version = "1.0.27" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563" +[[package]] +name = "unicode-segmentation" +version = "1.13.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6f5d3c3b1bf09027a88a6bc961fc00497d651009560b5463668dc81b0fa87a8" + +[[package]] +name = "unicode-width" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca718471876992059f251e33aafc334346ada08042b7661dd1fe313a9bced557" + [[package]] name = "url" version = "2.5.8" @@ -819,6 +1589,12 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65" +[[package]] +name = "vsimd" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c3082ca00d5a5ef149bb8b555a72ae84c9c59f7250f013ac822ac2e49b19c64" + [[package]] name = "walkdir" version = "2.5.0" @@ -1041,6 +1817,7 @@ dependencies = [ "displaydoc", "yoke", "zerofrom", + "zerovec", ] [[package]] @@ -1049,6 +1826,7 @@ version = "0.11.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f58a2799cfe9a1670c7ea8edbd52ef8edb93cb37a7f83bf31ba9e35d9dae6a40" dependencies = [ + "serde", "yoke", "zerofrom", "zerovec-derive", @@ -1070,3 +1848,9 @@ name = "zmij" version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b" + +[[package]] +name = "zmij_ecma" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "458b3e7a977f14499066b6bdb63d69da30ce337d6c160f077e6c575c82a44db9" diff --git a/ffi/Cargo.toml b/ffi/Cargo.toml index 064c0d35e..0c1810fb7 100644 --- a/ffi/Cargo.toml +++ b/ffi/Cargo.toml @@ -27,6 +27,7 @@ resolver = "3" allow_attributes_without_reason = "warn" [dependencies] +oxc = { version = "0.159.0", default-features = false, features = ["codegen", "isolated_declarations", "semantic", "transformer"] } rust2-driver = { path = "../driver" } serde = { version = "1.0.228", features = ["derive"] } serde_json = "1.0.154" diff --git a/ffi/README.md b/ffi/README.md index e10722bb7..28dbaad2a 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -1329,8 +1329,24 @@ its neighbors by their `.ts` names; the `.wasm` needs no tool run on it after `c and no wasm-bindgen. The binding serves `wasm32-wasip1` alone: an item another target has and it lacks is left out, not declared to fail. `load(source)` of `index.ts` compiles and instantiates the module, from its bytes, a -`Response`, or a `WebAssembly.Module`, before any other call, and checks its contract and -target there. +`Response`, a `WebAssembly.Module`, or its URL, before any other call, and checks its +contract and target there. Without a source it loads `.wasm` beside `index.ts`, +through `new URL("./.wasm", import.meta.url)`, which Vite and webpack follow into +the bundle; a `file:` URL is read through Node's file system, where `process.getBuiltinModule` +finds one, since Node's `fetch` refuses the scheme. A runtime with source phase imports, +Node 24 and Deno, may pass `import source module from "/.wasm"` instead. + +`web/` is an npm package too, since Node strips no type under `node_modules` +([nodejs#57215](https://github.com/nodejs/node/issues/57215)): beside each `.ts` the +export writes its `.js`, the types stripped by [oxc](https://oxc.rs) and each import named +by its `.js` file, and its `.d.ts`, declared from that file alone, as JSR requires too, so +every export states its type. Its `package.json` names it as the facade's package and +version, exports `index` as the package and each other module by its name, such as +`/Counter` and `/rt`, and the module as `/.wasm`, and +ships the JavaScript and declarations with the module `cargo build` puts beside them. +[The release builder](release/README.md) packs it for npm and JSR. `tests/fixtures.rs` +packs the api fixture's with npm, installs it into a project of its own, type-checks +`tests/api.ts` against the declarations alone, and runs it on Node, Deno, and Bun. - **The platform** is the web's: the module's std is `wasm32-wasip1`'s, whose imports are a detail between the generated module and its binding, which implements each with the web diff --git a/ffi/justfile b/ffi/justfile index 59680671d..71d1ce72e 100644 --- a/ffi/justfile +++ b/ffi/justfile @@ -198,7 +198,7 @@ bench-run rounds="3": run "$lang" done done - python3 -I ../bench/ffi/report.py "$raw" "$builds" "$out" target/export/moq-web > target/bench/report.md + python3 -I ../bench/ffi/report.py "$raw" "$builds" "$out" target/export/moq-web target/bench-ts/export.ts > target/bench/report.md cat target/bench/report.md # Run what bench-build built once each, under `codspeed run` in the bench shell's environment, so CodSpeed's CPU simulation counts each single-threaded case: the CodSpeed workflow's run. @@ -376,7 +376,7 @@ ts-program: printf '%s\n' '{"compilerOptions": {"strict": true, "noEmit": true, "noUnusedLocals": true, "noUnusedParameters": true,' \ '"target": "es2024", "lib": ["es2024", "esnext.disposable", "dom"], "module": "esnext", "moduleResolution": "bundler",' \ '"allowImportingTsExtensions": true, "verbatimModuleSyntax": true, "erasableSyntaxOnly": true, "skipLibCheck": true, "types": []},' \ - '"include": ["export.ts", "web/*.ts"]}' > "$dir/tsconfig.json" + '"include": ["export.ts", "web/*.ts"], "exclude": ["web/*.d.ts"]}' > "$dir/tsconfig.json" tsc -p "$dir" # Lay out target/ as a Dart program whose main is
, importing the export's Dart as package:moq/moq.dart, whose native asset is the shared library, as a package's build hook would bundle it, which `dart compile` has no flag for; analyze it, and compile it into target//main.dill. @@ -548,8 +548,10 @@ standin checkout="" web="": dir=target/moq-ffi-web deps=(hang kio moq-mux moq-net moq-pattern) fi - features=$(cd "$src" && cargo metadata --format-version 1 --no-deps \ - | jq -c '[.packages[] | select(.name == "moq-ffi") | .dependencies[] | select(.kind == null) | {key: .name, value: .features}] | from_entries') + ffi=$(cd "$src" && cargo metadata --format-version 1 --no-deps | jq -c '.packages[] | select(.name == "moq-ffi")') + features=$(jq -c '[.dependencies[] | select(.kind == null) | {key: .name, value: .features}] | from_entries' <<< "$ffi") + # moq-ffi's license, which a package of the stand-in states as moq-ffi's would. + license=$(jq -r '.license // empty' <<< "$ffi") mkdir -p "$dir/src" # Replace a file only when its content changes, so an unchanged stand-in, and the # export over it, stay fresh. @@ -562,7 +564,9 @@ standin checkout="" web="": } { printf '# Generated by just ffi standin, from %s.\n' "$src" - printf '[package]\nname = "moq-ffi"\nversion = "0.0.0"\nedition = "2024"\npublish = false\n\n' + printf '[package]\nname = "moq-ffi"\nversion = "0.0.0"\nedition = "2024"\npublish = false\n' + if [ -n "$license" ]; then printf 'license = "%s"\n' "$license"; fi + printf '\n' # The prefix of every C name. printf '[lib]\nname = "moq"\n\n[dependencies]\n' for name in "${deps[@]}"; do diff --git a/ffi/release/README.md b/ffi/release/README.md index e02dca74c..69f0f3b9a 100644 --- a/ffi/release/README.md +++ b/ffi/release/README.md @@ -1,7 +1,8 @@ # Binding releases A crate's [rust2-ffi](/ffi/README.md) export, released as Python wheels, a Swift package, -and a Go module. Two halves, split where Cargo's knowledge ends: +a Go module, and a web package on npm and JSR. Two halves, split where Cargo's knowledge +ends: - **The builder**, [`bindings.yml`](/.github/workflows/bindings.yml) over `package.py`, shared and read-only. It derives everything from Cargo metadata and the export: package @@ -9,20 +10,22 @@ and a Go module. Two halves, split where Cargo's knowledge ends: exporter gives the crate's library. It builds, repairs, and checks the packages in clean consumers, and publishes nothing. - **The release workflow**, the caller's own file, which `package.py init` writes once. - It holds what Cargo cannot establish: who may publish to PyPI, where the XCFramework is - hosted, which repository SwiftPM resolves the package from, the Go module's path, and - which tag releases. + It holds what Cargo cannot establish: who may publish to PyPI, npm, and JSR, the scope + the web package is named under, where the XCFramework is hosted, which repository + SwiftPM resolves the package from, the Go module's path, and which tag releases. ## Adopting it From a rust2 checkout at the commit to pin, in the repository holding the crate: ```sh -python3 /ffi/release/package.py init --manifest-path rs/moq-ffi/Cargo.toml --swift-mirror moq-dev/moq-swift-ffi --go-module moq.dev/moq-ffi +python3 /ffi/release/package.py init --manifest-path rs/moq-ffi/Cargo.toml --swift-mirror moq-dev/moq-swift-ffi --go-module moq.dev/moq-ffi --scope moq ``` It writes `.github/workflows/release-.yml`, calling the builder at that commit, and -refuses to overwrite a different file. Then, outside the repository: +refuses to overwrite a different file. `--scope` ships the crate's web package as +`@/`; without it, the lines that would are written commented out. Then, +outside the repository: - register the workflow and the `pypi` environment as the PyPI project's trusted publisher; - create the mirror repository, install a GitHub App on it with contents write, and put its @@ -33,6 +36,13 @@ refuses to overwrite a different file. Then, outside the repository: resolves a `github.com` path by git alone, so its consumers set `GOPROXY` to the proxy. No job publishes it: a git mirror holds no library over GitHub's 100 MiB, and a proxy is the caller's to run. +- for the web package, publish its first version by hand, as npm configures a trusted + publisher only for a package it holds, then register the workflow and the `npm` + environment as its trusted publisher; and create the package in the JSR scope and link + it to the repository, which JSR's publishing from GitHub Actions trusts. Make `npm` and + `jsr` environments that only release tags may deploy to. npm's provenance needs Cargo's + `repository` to be the publishing repository, which the release checks before any + registry takes it. Releasing is pushing `-v`, the crate's version, which the release checks. Called without a tag, such as from a nightly, it runs everything up to publication. @@ -47,7 +57,13 @@ The builder takes the crate's `Cargo.toml`, the `go-module` path, which without export's own `` that no proxy serves, and optional inputs the caller's tests own: `features` to ship, an `example` whose stdout is native Rust's transcript, and the `python-check`, `swift-check`, and `go-check` programs that must print it through the -installed packages. Without a check, a consumer only imports the module. +installed packages. Without a check, a consumer only imports the module. The web package +takes `web-manifest-path`, the package exported for `wasm32-wasip1`, often the crate's own, +which no Cargo fact says builds for the web, and `scope`; and an optional `web-check`, a +TypeScript module importing the binding as `./web/.ts` does beside an export, +whose imports the consumer rewrites to the package's name. Its crate's version must be +the release's. `features` and `example` apply to it when it is the crate's own; a web +crate apart from it takes `web-features` and `web-example` instead. ## What the builder does @@ -96,24 +112,37 @@ installed packages. Without a check, a consumer only imports the module. and the Go module, unpacked from its zip into a new module that requires it, vetted, built with the newest Go and no `CGO_LDFLAGS`, and run on Linux x64 and arm64, macOS, and Windows. With an `example`, each must print native Rust's stdout byte for byte. +8. Web: `web-manifest-path` exported for `wasm32-wasip1` alone, whose `web/` holds the + TypeScript, its JavaScript and declarations, and a `package.json` + ([TypeScript (web)](/ffi/README.md#typescript-web)), and its module built at + `opt-level = "s"` unless the crate's release profile sets a level, as a page downloads + it. The npm package is the JavaScript, declarations, and module, named + `@/` with Cargo's description, license, and URLs, packed by `npm pack`; + the JSR package is the TypeScript and module with a `jsr.json` exporting each module by + name, refused past JSR's 20 MiB. The example runs for `wasm32-wasip1` under Node's WASI, + native Rust on the binding's target. A new npm project installs the tarball, as an + app would, and the `web-check`, or else a `load()`, runs on Node, Deno, and Bun, and + bundled by Vite and by webpack into a browser's script that Node runs, each finding the + module where `load` looks by default; and `deno publish --dry-run` checks the JSR + package, its types against Deno's included. rust2 runs it on every manual Check and nightly through [`bindings-test.yml`](/.github/workflows/bindings-test.yml): on the api fixture, whose -wheels must print its native transcript, and on moq's stand-in facade at the exporter's moq -pin, through `moq/moq.py`, `moq/moq.swift`, and `moq/moq.go`. +wheels and web package must print its native transcript, and on moq's stand-in facade at +the exporter's moq pin, through `moq/moq.py`, `moq/moq.swift`, and `moq/moq.go`, with its +wasm32 stand-in as the web package. ## Other registries The same boundary holds for each destination: the builder makes and checks the artifact from Cargo facts, the caller's workflow names the identity and credential. A language whose -generator rust2-ffi lacks, such as Node or C#, is unsupported, not a package without its +generator rust2-ffi lacks, such as C#, is unsupported, not a package without its binding. | Destination | Builder work | What only the caller can say | | --- | --- | --- | | Maven Central | An AAR with classes.jar, JNI libraries per Android ABI and the generated R8 rules; POM, sources and javadoc jars | The verified groupId namespace, publisher authorization, and signing key | | pub.dev | The generated Dart library with a native-assets build hook and per-platform archives, pubspec, and license | Publisher authorization and the archives' download host | -| npm | Needs a Node generator first: platform packages and a JS loader | Scope ownership and trusted-publisher registration | | NuGet | Needs a C# generator first: the managed assembly and RID-specific native assets | Package ID ownership, credentials, and target frameworks | Cargo's `repository` can suggest a Go module path or a Maven namespace but cannot prove diff --git a/ffi/release/package.py b/ffi/release/package.py index e6dcea60e..2d4cab781 100644 --- a/ffi/release/package.py +++ b/ffi/release/package.py @@ -1,4 +1,4 @@ -"""Build and check the Python wheels, Swift package, and Go module of a crate's rust2-ffi export. +"""Build and check the Python wheels, Swift package, Go module, and web package of a crate's rust2-ffi export. bindings.yml runs each step on the host it needs; release.yml is the caller `init` writes. Every fact about the package comes from Cargo metadata or the export, so nothing here @@ -20,6 +20,7 @@ import subprocess import sys import tempfile +import tomllib import zipfile HERE = Path(__file__).resolve().parent @@ -62,6 +63,21 @@ # Cargo metadata names a library target by its crate types; a proc-macro is no library to export. LIBRARY_KINDS = {"lib", "rlib", "dylib", "cdylib", "staticlib"} COLLECTIONS = "https://github.com/apple/swift-collections" +# The target the web binding serves, whose module the npm and JSR packages hold. +WEB_TARGET = "wasm32-wasip1" +# JSR's cap on a version's files, unpacked. +JSR_LIMIT = 20 * 1024 * 1024 +# The license file names JSR reads, in any case. +JSR_LICENSES = {"license", "license.md", "license.txt", "licence", "licence.md", "licence.txt"} +# The bundlers the web package's consumers build with, as an app would take them from npm. +BUNDLERS = {"vite": "8.3.8", "webpack": "5.112.2", "webpack-cli": "7.3.1"} +# Runs a WASI command module on Node: native Rust's output on the web binding's target. +WASI_RUN = """import { WASI } from "node:wasi"; +import { readFileSync } from "node:fs"; +const wasi = new WASI({ version: "preview1", args: [], env: {}, returnOnExit: false }); +const module = await WebAssembly.compile(readFileSync(process.argv[1])); +wasi.start(await WebAssembly.instantiate(module, wasi.getImportObject())); +""" SEMVER = re.compile(r"(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(?:-(alpha|beta|rc)\.(0|[1-9][0-9]*))?") @@ -737,8 +753,195 @@ def consume_go(proxy, check, native): compare(Path(native).read_bytes(), result.stdout, "Go module") -def template(info, manifest, rust2, mirror, go_module): - """The caller workflow, with every identity Cargo cannot establish spelled out in it.""" +def package_name(info, scope): + """The web package's name on npm and JSR, `@/`, refusing what JSR cannot spell.""" + name = f"@{scope}/{info['name']}" + if not re.fullmatch(r"@[a-z0-9][a-z0-9-]{1,19}/[a-z0-9][a-z0-9-]{1,57}", name): + raise SystemExit(f"{name} is no JSR package name: a scope and crate name of lowercase letters, digits, and hyphens") + return name + + +def web_package(info, web_dir, wasm, scope, out): + """The npm package, the JavaScript and declarations, and the JSR package, the TypeScript, + each with the module beside them where `load` finds it and the license files.""" + web_dir, out = Path(web_dir), Path(out) + lib, name = info["lib"], package_name(info, scope) + manifest = json.loads((web_dir / "package.json").read_text(encoding="utf-8")) + if manifest["version"] != info["version"]: + raise SystemExit(f"the export's package.json is {manifest['version']}, not the crate's {info['version']}") + npm, jsr = out / "npm", out / "jsr" + # A tarball of an earlier build, which `consume-web` would find beside this one. + for tarball in out.glob("*.tgz") if out.exists() else []: + tarball.unlink() + files = licenses(info) + # JSR takes a license from jsr.json's field, which Cargo's `license` fills, or else from + # a file by one of its own names. + jsr_names = {path: path.name for path in files} + if not info["license"] and not any(path.name.lower() in JSR_LICENSES for path in files): + if len(files) != 1: + raise SystemExit(f"JSR needs Cargo's `license`, or one license file, not {[p.name for p in files]}") + jsr_names = {files[0]: "LICENSE"} + for part in (npm, jsr): + if part.exists(): + shutil.rmtree(part) + part.mkdir(parents=True) + shutil.copy(wasm, part / f"{lib}.wasm") + for path in files: + shutil.copy(path, part / (jsr_names[path] if part == jsr else path.name)) + modules = [] + for path in sorted(web_dir.iterdir()): + if path.name.endswith(".d.ts") or path.suffix == ".js": + shutil.copy(path, npm) + elif path.suffix == ".ts": + shutil.copy(path, jsr) + modules.append(path.stem) + if "index" not in modules: + raise SystemExit(f"{web_dir} holds no web binding") + # What Cargo says of the package, beside what the export wrote. + said = {"name": name, "version": info["version"]} + if info["description"]: + said["description"] = " ".join(info["description"].split()) + if info["license"]: + said["license"] = info["license"] + if info["repository"]: + said["repository"] = {"type": "git", "url": f"git+{info['repository']}"} + if info["homepage"]: + said["homepage"] = info["homepage"] + manifest = {**said, **{k: v for k, v in manifest.items() if k not in said}} + # npm packs a license file by a name of its own only if `files` names it. + manifest["files"] = manifest["files"] + [p.name for p in licenses(info) if p.name not in manifest["files"]] + (npm / "package.json").write_text(json.dumps(manifest, indent="\t") + "\n", newline="\n", encoding="utf-8") + # JSR takes no export pattern: each module by its name. The module ships unexported, + # as Deno would link an exported one as a module of its own, whose imports none serves. + exports = {".": "./index.ts", **{f"./{m}": f"./{m}.ts" for m in modules if m != "index"}} + config = {"name": name, "version": info["version"]} + if info["license"]: + config["license"] = info["license"] + config["exports"] = exports + (jsr / "jsr.json").write_text(json.dumps(config, indent="\t") + "\n", newline="\n", encoding="utf-8") + size = sum(p.stat().st_size for p in jsr.iterdir()) + if size > JSR_LIMIT: + raise SystemExit(f"the JSR package is {size} bytes, past JSR's {JSR_LIMIT}") + return npm, jsr + + +def web_profile(manifest): + """The web module's build at `opt-level = "s"`, which a page downloads, unless the crate's + release profile, which the export carries, chooses its own level.""" + with open(manifest, "rb") as f: + release = tomllib.load(f).get("profile", {}).get("release", {}) + return {} if "opt-level" in release else {"CARGO_PROFILE_RELEASE_OPT_LEVEL": "s"} + + +def web(args): + """The web binding's npm tarball and JSR package, and native Rust's output on its target.""" + info = facts(args.manifest_path) + if args.version and info["version"] != args.version: + raise SystemExit(f"{info['name']} is {info['version']}, not the release's {args.version}") + out = Path(args.out).resolve() + export_dir = out / "export" + exporter(args.manifest_path, args.features, "--target", WEB_TARGET, "--out", export_dir) + manifest = Path(args.manifest_path).resolve() + run("rustup", "target", "add", WEB_TARGET, cwd=manifest.parent) + built = cargo(args.manifest_path, "build", "--locked", "--release", "--manifest-path", export_dir / "Cargo.toml", + "--target", WEB_TARGET, "--target-dir", export_dir / "target", "--features", "web", + env=web_profile(export_dir / "Cargo.toml")) + sys.stderr.write(built.stderr) + wasm = export_dir / "target" / WEB_TARGET / "release" / f"{info['lib']}.wasm" + npm, _ = web_package(info, export_dir / "web", wasm, args.scope, out) + packed = output("npm", "pack", "--silent", "--pack-destination", out, cwd=npm).strip() + if not (out / packed).is_file(): + raise SystemExit(f"npm pack wrote no {packed}") + shutil.rmtree(export_dir) + if args.example: + if args.example not in info["examples"]: + raise SystemExit(f"{info['name']} has no example {args.example}: it has {info['examples']}") + features = ["--features", args.features] if args.features.strip() else [] + target_dir = out / "native-target" + cargo(args.manifest_path, "build", "--locked", "--release", "--manifest-path", manifest, "--example", + args.example, "--target", WEB_TARGET, "--target-dir", target_dir, *features) + program = target_dir / WEB_TARGET / "release" / "examples" / f"{args.example}.wasm" + native = run("node", "--no-warnings", "--input-type=module", "-e", WASI_RUN, program, capture_output=True) + (out / "native.txt").write_bytes(native.stdout) + shutil.rmtree(target_dir) + + +def check_program(check, name): + """A check written beside an export, importing `./web/.ts`, as it imports the package.""" + text = Path(check).read_text(encoding="utf-8") + # Whitespace and comments, which may separate the keyword from its specifier. + gap = r"(?:\s|/\*[\s\S]*?\*/|//[^\n]*\n)*" + # Only a specifier, after `from` or `import`, in either quote, kept as written: a string + # the check prints stays as the transcript has it. + text = re.sub(rf"""(\b(?:from|import){gap}(?:\({gap})?)(["'])\./web/([A-Za-z0-9_$]+)\.ts\2""", + lambda m: m.group(1) + m.group(2) + (name if m.group(3) == "index" else f"{name}/{m.group(3)}") + m.group(2), + text) + if re.search(rf"""\b(?:from|import){gap}(?:\({gap})?["']\./web/""", text): + raise SystemExit(f"{check} imports the binding by a path other than ./web/.ts") + return text + + +def consume_web(dist, check, native): + """Install the npm tarball into a new project, as an app would, and run the check, or else + a load, on Node, Deno, and Bun, and bundled by Vite and by webpack; and check the JSR + package as publishing it would.""" + dist = Path(dist).resolve() + tarballs = list(dist.glob("*.tgz")) + if len(tarballs) != 1: + raise SystemExit(f"{dist} holds {len(tarballs)} npm tarballs") + name = json.loads((dist / "npm" / "package.json").read_text(encoding="utf-8"))["name"] + program = check_program(check, name) if check else ( + f"import * as binding from {json.dumps(name)};\n\nawait binding.load();\n") + expected = Path(native).read_bytes() if native and check else None + with tempfile.TemporaryDirectory() as clean: + clean = Path(clean) + manifest = {"type": "module", "private": True, "dependencies": {name: f"file:{tarballs[0]}"}, + "devDependencies": BUNDLERS} + (clean / "package.json").write_text(json.dumps(manifest, indent=2) + "\n", encoding="utf-8") + run("npm", "install", "--no-audit", "--no-fund", "--ignore-scripts", cwd=clean, capture_output=True) + (clean / "check.ts").write_text(program, encoding="utf-8") + # The bundles target the browser, whose `document` Vite's preload polyfill reads; + # each runs on Node from where the bundler wrote it, a relative base reaching its module. + (clean / "index.html").write_text('\n', encoding="utf-8") + (clean / "vite.config.js").write_text( + 'export default { base: "./", build: { target: "es2024", modulePreload: { polyfill: false } } };\n', encoding="utf-8") + run("node", "--no-warnings", "--input-type=module", "-e", + 'import { stripTypeScriptTypes } from "node:module"; import { readFileSync, writeFileSync } from "node:fs";' + ' writeFileSync("check.js", stripTypeScriptTypes(readFileSync("check.ts", "utf8")));', cwd=clean) + (clean / "webpack.config.js").write_text( + 'export default { mode: "production", entry: "./check.js", target: ["web", "es2024"],' + ' experiments: { outputModule: true },' + ' output: { module: true, path: new URL("./webpack", import.meta.url).pathname, filename: "check.js" } };\n', + encoding="utf-8") + run("npx", "--no-install", "vite", "build", "--logLevel", "warn", cwd=clean, capture_output=True) + run("npx", "--no-install", "webpack", "--no-stats", cwd=clean, capture_output=True) + bundled = list((clean / "dist" / "assets").glob("*.js")) + if len(bundled) != 1: + raise SystemExit(f"Vite wrote {len(bundled)} scripts") + consumers = { + "Node": ["node", "check.ts"], + "Deno": ["deno", "run", "--allow-read", "--allow-env", "check.ts"], + "Bun": ["bun", "check.ts"], + "Vite": ["node", bundled[0]], + "webpack": ["node", "webpack/check.js"], + } + # JSR's own checks of the TypeScript package, its types against Deno's included. + run("deno", "publish", "--dry-run", "--allow-dirty", cwd=dist / "jsr", capture_output=True, + env={**os.environ, "NO_COLOR": "1", "DENO_NO_UPDATE_CHECK": "1"}) + for consumer, command in consumers.items(): + result = subprocess.run([str(a) for a in command], capture_output=True, cwd=clean, + env={**os.environ, "NO_COLOR": "1", "DENO_NO_UPDATE_CHECK": "1"}) + sys.stderr.buffer.write(result.stderr) + if result.returncode: + raise SystemExit(f"{consumer} failed on the installed package with status {result.returncode}") + if expected is not None: + compare(expected, result.stdout, f"{consumer}'s installed package") + + +def template(info, manifest, rust2, mirror, go_module, scope=None): + """The caller workflow, with every identity Cargo cannot establish spelled out in it. + With a scope, it ships the crate's own web package; without, the lines that would are + left commented out.""" if not re.fullmatch(r"[0-9a-f]{40}", rust2): raise SystemExit("pin rust2 by a full commit SHA") if not re.fullmatch(r"[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+", mirror): @@ -747,6 +950,14 @@ def template(info, manifest, rust2, mirror, go_module): if not re.fullmatch(r"[a-z0-9-]+(\.[a-z0-9-]+)+(/[A-Za-z0-9._~-]+)*", go_module): raise SystemExit(f"the Go module must be a path a proxy serves, such as example.com/name, not {go_module!r}") check_major(go_module, go_version(info["version"])) + indent = " " + if scope: + name = package_name(info, scope) + web = (f"{indent}# The web package, {name} on npm and JSR: delete these two lines to ship none.\n" + f"{indent}web-manifest-path: {manifest}\n{indent}scope: {scope}") + else: + web = (f"{indent}# The web package on npm and JSR, @/{info['name']}, for a crate that builds for wasm32-wasip1:\n" + f"{indent}# web-manifest-path: {manifest}\n{indent}# scope: ") values = { "GO_MODULE": go_module, "NAME": info["name"], @@ -756,6 +967,7 @@ def template(info, manifest, rust2, mirror, go_module): "MIRROR": mirror, "MIRROR_OWNER": mirror.split("/")[0], "MIRROR_REPO": mirror.split("/")[1], + "WEB": web, } text = (HERE / "release.yml").read_text(encoding="utf-8") for key, value in values.items(): @@ -774,7 +986,7 @@ def init(args): rust2 = args.rust2 or output("git", "-C", HERE, "rev-parse", "HEAD").strip() relative = manifest.resolve().relative_to(root).as_posix() path = root / ".github" / "workflows" / f"release-{info['name']}.yml" - text = template(info, relative, rust2, args.swift_mirror, args.go_module) + text = template(info, relative, rust2, args.swift_mirror, args.go_module, args.scope) if path.exists() and path.read_text(encoding="utf-8") != text: raise SystemExit(f"{path} exists; delete it to write the release workflow") path.parent.mkdir(parents=True, exist_ok=True) @@ -801,6 +1013,17 @@ def main(): if name == "build": p.add_argument("--export", required=True) p.add_argument("--example", default="") + p = sub.add_parser("web", help="export, build, and pack the web binding for npm and JSR") + p.add_argument("--manifest-path", required=True) + p.add_argument("--features", default="") + p.add_argument("--scope", required=True, help="the npm and JSR scope, without its @") + p.add_argument("--version", default="", help="the release's version, which the crate's must equal") + p.add_argument("--example", default="") + p.add_argument("--out", required=True) + p = sub.add_parser("consume-web", help="run a check program, or a load, on the installed npm package") + p.add_argument("--dist", required=True, help="what `web` wrote") + p.add_argument("--check") + p.add_argument("--native") p = sub.add_parser("digest", help="print the hash of the generated bindings") p.add_argument("--manifest-path", required=True) p.add_argument("--export", required=True) @@ -839,6 +1062,7 @@ def main(): p.add_argument("--go-module", required=True, metavar="PATH", help="the Go module's path, which a proxy you run serves, /vN suffix included") p.add_argument("--rust2", help="the rust2 commit to pin; this checkout's by default") + p.add_argument("--scope", help="the npm and JSR scope of the crate's web package, without its @; without it, none ships") args = parser.parse_args() # winapi links MinGW import libraries of its own, from cargo's registry, where nothing # linking the Go module's static library finds them: the system's serve every check and @@ -857,6 +1081,10 @@ def main(): export(args) elif args.command == "build": build(args) + elif args.command == "web": + web(args) + elif args.command == "consume-web": + consume_web(args.dist, args.check, args.native) elif args.command == "digest": print(digest(args.export, facts(args.manifest_path)["lib"])) elif args.command == "swift": diff --git a/ffi/release/release.yml b/ffi/release/release.yml index a3f341bf3..0194c5fad 100644 --- a/ffi/release/release.yml +++ b/ffi/release/release.yml @@ -1,8 +1,9 @@ name: Release @NAME@ bindings # Written by rust2's `ffi/release/package.py init`; yours to edit. rust2's builder, pinned -# below, exports @NAME@'s pub API, builds its Python wheels and Swift package, and checks -# them in clean consumers, all read-only. The jobs after it publish only a tag's build: +# below, exports @NAME@'s pub API, builds its Python wheels, Swift package, and web package, +# and checks them in clean consumers, all read-only. The jobs after it publish only a tag's +# build: # # - PyPI: the project @NAME@, through the trusted publisher this repository registers there # for this workflow and the `pypi` environment. @@ -10,6 +11,9 @@ name: Release @NAME@ bindings # the Package.swift pushed to @MIRROR@ names by URL and checksum, tagged . # The mirror's token comes from a GitHub App installed on it, whose ID and key live in # the `swift-mirror` environment. +# - npm and JSR: the web package, when the build makes one, through the trusted publisher +# npm registers for this workflow and the `npm` environment, and JSR's link to this +# repository, each with provenance. # # The Go module @GO_MODULE@ is the build's @NAME@-go artifact, its .info, .mod, .zip, and list laid # out as a GOPROXY serves them; no job here publishes it. Serve it from a module proxy: on a @@ -41,8 +45,10 @@ jobs: with: manifest-path: @MANIFEST@ go-module: @GO_MODULE@ +@WEB@ # Optional: cargo features to ship, an example of the crate whose output every check - # program must print, and those programs: `python-check`, `swift-check`, and `go-check`. + # program must print, and those programs: `python-check`, `swift-check`, `go-check`, and + # `web-check`. tag: needs: build @@ -58,6 +64,23 @@ jobs: echo "::error::$TAG is not the crate's version, $WANT: bump Cargo.toml before tagging." exit 1 fi + - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + if: needs.build.outputs.web == 'true' + with: + name: @NAME@-web + path: web + # npm's provenance refuses a package whose repository is not the one publishing it, + # which is checked here, before any registry takes this release. + - name: Web package names this repository + if: needs.build.outputs.web == 'true' + run: | + url=$(jq -r '.repository.url // ""' web/npm/package.json) + url=${url#git+} + url=${url%.git} + if [[ "${url%/}" != "https://github.com/$GITHUB_REPOSITORY" ]]; then + echo "::error::the web package's repository is '$url', not https://github.com/$GITHUB_REPOSITORY: set Cargo's repository to it." + exit 1 + fi pypi: needs: [build, tag] @@ -207,3 +230,56 @@ jobs: git diff --cached --quiet || git commit --quiet -m "$VERSION" git tag "$VERSION" git push --quiet --atomic origin HEAD "refs/tags/$VERSION" + + # A rerun publishes only a version npm lacks, and fails on one it hosts with other bytes. + npm: + needs: [build, tag] + if: needs.build.outputs.web == 'true' + runs-on: ubuntu-24.04 + environment: npm + permissions: + id-token: write + steps: + - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + name: @NAME@-web + path: web + # Trusted publishing needs npm 11.5.1 or later. + - uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 + with: + node-version: '24' + - name: Publish + run: | + tarball=$(echo web/*.tgz) + name=$(jq -r .name web/npm/package.json) + version=$(jq -r .version web/npm/package.json) + ours="sha512-$(openssl dgst -sha512 -binary "$tarball" | base64 -w0)" + if hosted=$(npm view "$name@$version" dist.integrity 2> /dev/null) && [[ -n "$hosted" ]]; then + [[ "$hosted" == "$ours" ]] || { echo "::error::npm hosts another $name@$version"; exit 1; } + echo "npm already hosts $name@$version" + exit 0 + fi + # npm takes a prerelease only under a tag of its own, so it is never `latest`. + tag=latest + if [[ "$version" == *-* ]]; then tag=next; fi + npm publish --provenance --access public --tag "$tag" "$tarball" + + # JSR skips a version it already hosts. + jsr: + needs: [build, tag] + if: needs.build.outputs.web == 'true' + runs-on: ubuntu-24.04 + environment: jsr + permissions: + id-token: write + steps: + - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + name: @NAME@-web + path: web + - uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 + with: + node-version: '24' + - name: Publish + working-directory: web/jsr + run: npx --yes jsr@0.14.3 publish diff --git a/ffi/release/test_package.py b/ffi/release/test_package.py index 76df68b64..eb0d9e5fb 100644 --- a/ffi/release/test_package.py +++ b/ffi/release/test_package.py @@ -278,6 +278,119 @@ def test_refuses_a_movable_pin_or_bad_mirror(self): with self.assertRaises(SystemExit): package.template(info("/src"), "Cargo.toml", "a" * 40, "moq-dev/moq-swift-ffi", module) + def test_web_lines_follow_the_scope(self): + shipped = package.template(info("/src"), "rs/moq-ffi/Cargo.toml", "a" * 40, "moq-dev/moq-swift-ffi", "moq.dev/moq-ffi", "moq") + self.assertIn("\n web-manifest-path: rs/moq-ffi/Cargo.toml\n scope: moq\n", shipped) + self.assertIn("@moq/moq-ffi on npm and JSR", shipped) + none = package.template(info("/src"), "rs/moq-ffi/Cargo.toml", "a" * 40, "moq-dev/moq-swift-ffi", "moq.dev/moq-ffi") + self.assertIn(" # web-manifest-path: rs/moq-ffi/Cargo.toml\n", none) + self.assertNotIn("\n web-manifest-path:", none) + self.assertNotRegex(none, r"@[A-Z_]+@") + + +def web_export(root, version="0.4.12"): + """The parts of an export's web/ directory a web package reads.""" + web = root / "web" + web.mkdir(parents=True) + for stem in ("index", "rt", "glue", "Session"): + (web / f"{stem}.ts").write_text(f"// {stem}\n") + (web / f"{stem}.js").write_text(f"// {stem}\n") + (web / f"{stem}.d.ts").write_text(f"// {stem}\n") + manifest = {"name": "moq-ffi", "version": version, "description": "generated", "type": "module", + "sideEffects": False, "exports": {".": {"types": "./index.d.ts", "default": "./index.js"}}, + "files": ["*.js", "*.d.ts", "moq.wasm"]} + (web / "package.json").write_text(package.json.dumps(manifest)) + (root / "moq.wasm").write_bytes(b"\0asm\1\0\0\0") + return web + + +class Web(unittest.TestCase): + def test_npm_and_jsr_layouts(self): + with tempfile.TemporaryDirectory() as tmp: + root = Path(tmp) + (root / "LICENSE-MIT").write_text("MIT\n") + web = web_export(root / "export") + npm, jsr = package.web_package(info(root), web, root / "export/moq.wasm", "moq", root / "out") + self.assertEqual(sorted(p.name for p in npm.iterdir()), [ + "LICENSE-MIT", "Session.d.ts", "Session.js", "glue.d.ts", "glue.js", "index.d.ts", "index.js", + "moq.wasm", "package.json", "rt.d.ts", "rt.js"]) + self.assertEqual(sorted(p.name for p in jsr.iterdir()), [ + "LICENSE-MIT", "Session.ts", "glue.ts", "index.ts", "jsr.json", "moq.wasm", "rt.ts"]) + manifest = package.json.loads((npm / "package.json").read_text()) + self.assertEqual(list(manifest)[:6], ["name", "version", "description", "license", "repository", "type"]) + self.assertEqual(manifest["name"], "@moq/moq-ffi") + self.assertEqual(manifest["description"], "Media over QUIC, bindings") + self.assertEqual(manifest["repository"], {"type": "git", "url": "git+https://github.com/moq-dev/moq"}) + self.assertEqual(manifest["exports"]["."]["types"], "./index.d.ts") + self.assertIn("LICENSE-MIT", manifest["files"]) + config = package.json.loads((jsr / "jsr.json").read_text()) + self.assertEqual(config["name"], "@moq/moq-ffi") + self.assertEqual(config["exports"], {".": "./index.ts", "./Session": "./Session.ts", "./glue": "./glue.ts", "./rt": "./rt.ts"}) + + def test_names_a_lone_license_file_for_jsr(self): + with tempfile.TemporaryDirectory() as tmp: + root = Path(tmp) + (root / "terms.txt").write_text("Terms\n") + web = web_export(root / "export") + unlicensed = info(root, license=None, license_file="terms.txt") + npm, jsr = package.web_package(unlicensed, web, root / "export/moq.wasm", "moq", root / "out") + self.assertTrue((npm / "terms.txt").is_file()) + self.assertEqual((jsr / "LICENSE").read_text(), "Terms\n") + self.assertFalse((jsr / "terms.txt").exists()) + (root / "terms.txt").unlink() + with self.assertRaises(SystemExit): + package.web_package(info(root, license=None), web, root / "export/moq.wasm", "moq", root / "out") + + def test_size_unless_the_crate_chooses(self): + with tempfile.TemporaryDirectory() as tmp: + manifest = Path(tmp) / "Cargo.toml" + manifest.write_text('[profile.release]\nlto = "fat"\n') + self.assertEqual(package.web_profile(manifest), {"CARGO_PROFILE_RELEASE_OPT_LEVEL": "s"}) + manifest.write_text('[profile.release]\nopt-level = 3\n') + self.assertEqual(package.web_profile(manifest), {}) + + def test_drops_an_earlier_tarball(self): + with tempfile.TemporaryDirectory() as tmp: + root = Path(tmp) + web = web_export(root / "export") + (root / "out").mkdir() + (root / "out/moq-moq-ffi-0.4.11.tgz").write_bytes(b"old") + package.web_package(info(root), web, root / "export/moq.wasm", "moq", root / "out") + self.assertEqual(list((root / "out").glob("*.tgz")), []) + + def test_refuses_another_version(self): + with tempfile.TemporaryDirectory() as tmp: + root = Path(tmp) + web = web_export(root / "export", version="0.0.1") + with self.assertRaises(SystemExit): + package.web_package(info(root), web, root / "export/moq.wasm", "moq", root / "out") + + def test_names_jsr_can_spell(self): + self.assertEqual(package.package_name(info("/src"), "moq"), "@moq/moq-ffi") + for scope in ("Moq", "-moq", "moq_dev", "m"): + with self.assertRaises(SystemExit): + package.package_name(info("/src"), scope) + with self.assertRaises(SystemExit): + package.package_name(info("/src", name="moq_ffi"), "moq") + + def test_check_imports_the_package(self): + with tempfile.TemporaryDirectory() as tmp: + check = Path(tmp) / "check.ts" + check.write_text('import * as api from "./web/index.ts";\nimport type * as rt from "./web/rt.ts";\n' + "import * as Error_ from './web/Error_.ts';\n") + self.assertEqual(package.check_program(check, "@moq/moq-ffi"), + 'import * as api from "@moq/moq-ffi";\nimport type * as rt from "@moq/moq-ffi/rt";\n' + "import * as Error_ from '@moq/moq-ffi/Error_';\n") + check.write_text('import * as api from "./web/index.ts";\nconsole.log("./web/index.ts", await import("./web/rt.ts"));\n') + self.assertEqual(package.check_program(check, "@moq/moq-ffi"), + 'import * as api from "@moq/moq-ffi";\nconsole.log("./web/index.ts", await import("@moq/moq-ffi/rt"));\n') + check.write_text("import * as api from /* binding */ './web/index.ts';\nimport(\n // rt\n './web/rt.ts');\n") + self.assertEqual(package.check_program(check, "@moq/moq-ffi"), + "import * as api from /* binding */ '@moq/moq-ffi';\nimport(\n // rt\n '@moq/moq-ffi/rt');\n") + check.write_text('import * as api from "./web/nested/index.ts";\n') + with self.assertRaises(SystemExit): + package.check_program(check, "@moq/moq-ffi") + if __name__ == "__main__": unittest.main() diff --git a/ffi/runtime/rt.ts b/ffi/runtime/rt.ts index 46b88da98..94e488175 100644 --- a/ffi/runtime/rt.ts +++ b/ffi/runtime/rt.ts @@ -59,12 +59,16 @@ const minting: unique symbol = Symbol("minting"); let made: (bytes: Uint8Array) => Opaque; let bytesOf: (v: Opaque) => Uint8Array | undefined; +// Its brand, which a class field keyed by a symbol would not declare without a type checker. +export interface Opaque { + readonly [brand]: T; +} + /** * A `Copy` type whose fields are private: its bytes, which only Rust makes or reads, so * JavaScript copies one but never makes one. */ export class Opaque { - declare readonly [brand]: T; // Private, so no caller makes or changes one: the bytes may hold what Rust forbids, such // as a zero `NonZero`. readonly #bytes: Uint8Array; @@ -183,8 +187,10 @@ let bytes: Uint8Array = new Uint8Array(0); /** A view of the module's memory, made again once memory grows, which detaches the last. */ export function dv(): DataView { if (view.buffer !== memory.buffer) { - view = new DataView(memory.buffer); - bytes = new Uint8Array(memory.buffer); + // Never shared: the module is built without threads. Deno's types say it may be. + const buffer = memory.buffer as ArrayBuffer; + view = new DataView(buffer); + bytes = new Uint8Array(buffer); } return view; } @@ -332,6 +338,11 @@ const collected = new FinalizationRegistry<[number, string]>(([p, free]) => { s.d--; }); +// Its brand, which a class field keyed by a symbol would not declare without a type checker. +export interface Handle { + readonly [brand]: T; +} + /** * A handle to a Rust object, which counts the calls holding it. An owned one of a value * Rust checks is Rust's to account: disposing it drops the value as Rust's own drop does, @@ -341,7 +352,6 @@ const collected = new FinalizationRegistry<[number, string]>(([p, free]) => { * it unreachable, if it was never disposed. */ export class Handle { - declare readonly [brand]: T; /** The handle's pointer. */ readonly p: number; readonly k: Kind; @@ -1436,8 +1446,25 @@ const wasi = { }, }; -/** What `load` takes the module from. */ -export type Source = WebAssembly.Module | BufferSource | Response | PromiseLike; +/** + * What `load` takes the module from: a compiled module, its bytes, a `Response` such as + * `fetch` gives, or its URL, which a `file:` URL reads through Node's file system where the + * runtime has one, as Node's `fetch` refuses that scheme, and any other fetches. + */ +export type Source = WebAssembly.Module | BufferSource | Response | PromiseLike | URL; + +/** Node's file system, where the runtime has one: Node, Bun, and Deno, but no browser. */ +type Files = { readFile(path: URL): Promise> }; + +/** + * The bytes or the response at `url`. Node's file system is found by + * `process.getBuiltinModule`, a call no bundler follows, so a browser's bundle imports none. + */ +function fetchUrl(url: URL): Promise { + const node = (globalThis as { process?: { getBuiltinModule?(id: string): unknown } }).process; + const files = url.protocol === "file:" ? (node?.getBuiltinModule?.("node:fs/promises") as Files | undefined) : undefined; + return files ? files.readFile(url) : fetch(url); +} /** What a binding's `load` passes: its C prefix and glue's namespace, its contract and target, and its imports. */ export type Glue = { @@ -1477,9 +1504,11 @@ let loaded = false; async function instantiate(source: Source, glue: Glue): Promise { let module: WebAssembly.Module; + if (source instanceof URL) source = await fetchUrl(source); if (source instanceof WebAssembly.Module) module = source; else if (source instanceof ArrayBuffer || ArrayBuffer.isView(source)) module = await WebAssembly.compile(source); - else module = await WebAssembly.compileStreaming(source as Response | PromiseLike); + // A promise of its own, as Deno's types take no other thenable. + else module = await WebAssembly.compileStreaming(Promise.resolve(source as Response | PromiseLike)); const rust = rust2(glue.imports as Record unknown>); for (const i of WebAssembly.Module.imports(module)) { // Its own, not what every object inherits, such as `toString`. diff --git a/ffi/src/cli.rs b/ffi/src/cli.rs index 388bb468c..816b721f9 100644 --- a/ffi/src/cli.rs +++ b/ffi/src/cli.rs @@ -365,6 +365,7 @@ fn facade(manifest: &Path, target: &str, picks: &Picks<'_>) -> Result<(config::P let facade = config::Package { name: top.name.clone(), lib: lib.name.replace('-', "_"), + version: top.version.clone(), dir: manifest.parent().expect("a manifest path has a parent").into(), features: Vec::new(), }; diff --git a/ffi/src/config.rs b/ffi/src/config.rs index b6d5991bb..1b9961a90 100644 --- a/ffi/src/config.rs +++ b/ffi/src/config.rs @@ -41,6 +41,8 @@ pub(crate) struct Package { pub(crate) name: String, /// Its library's crate name, Cargo's `[lib] name`: the prefix of every C name. pub(crate) lib: String, + /// Its Cargo version, which the web binding's `package.json` takes. + pub(crate) version: String, pub(crate) dir: PathBuf, /// The features rustc compiles it with, which the generated crate enables in it. pub(crate) features: Vec, @@ -55,15 +57,16 @@ impl Package { /// [`env::PACKAGE`]'s value. pub(crate) fn line(&self) -> String { - format!("{}\t{}\t{}", self.name, self.lib, self.dir.display()) + format!("{}\t{}\t{}\t{}", self.name, self.lib, self.version, self.dir.display()) } /// The package [`env::PACKAGE`] holds, without its features. pub(crate) fn parse(line: &str) -> Result { match line.split('\t').collect::>()[..] { - [name, lib, dir] => Ok(Self { + [name, lib, version, dir] => Ok(Self { name: name.to_string(), lib: lib.to_string(), + version: version.to_string(), dir: PathBuf::from(dir), features: Vec::new(), }), diff --git a/ffi/src/glue.rs b/ffi/src/glue.rs index 154de21c0..965545579 100644 --- a/ffi/src/glue.rs +++ b/ffi/src/glue.rs @@ -371,7 +371,7 @@ impl Crate<'_> { let (module, go_mod) = go_module(out, &self.abi.prefix)?; let go = crate::go::generate(&rest, &self.packages(), contract, &module)?; let dart = crate::dart::generate(&rest, &self.packages(), contract)?; - let web = crate::web::generate(&rest, &self.packages(), contract)?; + let web = crate::web::generate(&rest, self.facade, contract)?; let mut names: std::collections::BTreeMap> = [ ("C++", cpp.names), ("Kotlin", kotlin.names), diff --git a/ffi/src/js.rs b/ffi/src/js.rs new file mode 100644 index 000000000..c09d3acd4 --- /dev/null +++ b/ffi/src/js.rs @@ -0,0 +1,112 @@ +//! The JavaScript and declarations of each TypeScript module the web binding writes, so a +//! registry's consumer runs it with no TypeScript loader: Node refuses to strip types under +//! `node_modules`. The modules hold only syntax type stripping erases, and every export +//! states its type, so each file's declarations come from it alone, as JSR requires too. + +use std::path::Path; + +use oxc::allocator::Allocator; +use oxc::codegen::Codegen; +use oxc::diagnostics::OxcDiagnostic; +use oxc::isolated_declarations::{IsolatedDeclarations, IsolatedDeclarationsOptions}; +use oxc::parser::Parser; +use oxc::semantic::SemanticBuilder; +use oxc::span::SourceType; +use oxc::transformer::{TransformOptions, Transformer, TypeScriptOptions}; + +/// The JavaScript and the declarations of the module `name`, a `.ts` file, whose imports +/// of its neighbors name their `.js` files instead. +pub(crate) fn emit(name: &str, ts: &str) -> Result<(String, String), String> { + let fail = |what: &str, errors: &[OxcDiagnostic]| { + let errors: Vec = errors.iter().map(|e| format!("{name}: {e}")).collect(); + format!( + "the web binding's {what} failed, a bug in rust2-ffi:\n{}", + errors.join("\n") + ) + }; + let allocator = Allocator::default(); + let path = Path::new(name); + let parsed = Parser::new(&allocator, ts, SourceType::ts()).parse(); + if !parsed.diagnostics.is_empty() { + return Err(fail("parse", &parsed.diagnostics)); + } + // Each import of a neighbor, `"./.ts"`, names its `.js` file instead: the same + // length, so every span the parse found still holds. + let mut source = ts.to_string(); + for (specifier, requests) in &parsed.module_record.requested_modules { + if !(specifier.starts_with("./") && specifier.ends_with(".ts")) { + continue; + } + for request in requests { + // The span holds the specifier's quotes. + let end = request.span.end as usize - 1; + if source.get(end - 3..end) != Some(".ts") { + return Err(format!("{name}: the import of {specifier} is not spelled as written")); + } + source.replace_range(end - 3..end, ".js"); + } + } + let parsed = Parser::new(&allocator, allocator.alloc_str(&source), SourceType::ts()).parse(); + let mut program = parsed.program; + // The header comment, which neither printing keeps whole. + let header: String = (ts.lines()) + .take_while(|line| line.starts_with("//")) + .map(|line| format!("{line}\n")) + .collect(); + + let declared = IsolatedDeclarations::new(&allocator, IsolatedDeclarationsOptions::default()).build(&program); + if !declared.diagnostics.is_empty() { + return Err(fail("declarations", &declared.diagnostics)); + } + let dts = format!("{header}{}", Codegen::new().build(&declared.program).code); + + let scoping = SemanticBuilder::new().build(&program).semantic.into_scoping(); + let options = TransformOptions { + typescript: TypeScriptOptions { + // `verbatimModuleSyntax`'s rule: an import is erased only if it says it is a type's. + only_remove_type_imports: true, + ..TypeScriptOptions::default() + }, + // No lowering: the modules are ES2024 as they are written. + ..TransformOptions::default() + }; + let stripped = Transformer::new(&allocator, path, &options).build_with_scoping(scoping, &mut program); + if !stripped.diagnostics.is_empty() { + return Err(fail("type stripping", &stripped.diagnostics)); + } + let js = Codegen::new().build(&program).code; + // Printing keeps the header but its blank line. + let js = match js.strip_prefix(&header) { + Some(rest) if !header.is_empty() => format!("{header}\n{rest}"), + _ => js, + }; + Ok((js, dts)) +} + +#[cfg(test)] +mod tests { + use super::emit; + + #[test] + fn strips_and_rewrites() { + let ts = "// Generated.\n\nimport * as rt from \"./rt.ts\";\nimport type { A } from \"./A.ts\";\n\n/** Doc. */\nexport function f(a: A, n: number): rt.u64 {\n\treturn rt.u64(n as number);\n}\n"; + let (js, dts) = emit("f.ts", ts).unwrap(); + assert_eq!( + js, + "// Generated.\n\nimport * as rt from \"./rt.js\";\n/** Doc. */\nexport function f(a, n) {\n\treturn rt.u64(n);\n}\n" + ); + assert!(dts.starts_with("// Generated.\n"), "{dts}"); + assert!(dts.contains("import * as rt from \"./rt.js\";"), "{dts}"); + assert!(dts.contains("import type { A } from \"./A.js\";"), "{dts}"); + assert!( + dts.contains("export declare function f(a: A, n: number): rt.u64;"), + "{dts}" + ); + } + + #[test] + fn refuses_an_inferred_export() { + let err = emit("g.ts", "declare function f(): number;\nexport const g = f();\n").unwrap_err(); + assert!(err.contains("g.ts"), "{err}"); + } +} diff --git a/ffi/src/main.rs b/ffi/src/main.rs index 59d72949a..eb4b1c054 100644 --- a/ffi/src/main.rs +++ b/ffi/src/main.rs @@ -40,6 +40,7 @@ mod driver; mod glue; mod go; mod ir; +mod js; mod kotlin; mod lock; mod merge; diff --git a/ffi/src/web.rs b/ffi/src/web.rs index e3233fc72..c71d222a9 100644 --- a/ffi/src/web.rs +++ b/ffi/src/web.rs @@ -22,11 +22,16 @@ //! [`runtime/rt.ts`](../runtime/rt.ts), and the module's std imports only web APIs: its //! clock is `performance.now`, its randomness `crypto.getRandomValues`, and its output the //! console. +//! +//! Beside each module it writes the module's JavaScript and declarations, which +//! [`crate::js`] makes, and a `package.json`, so `web/` is an npm package whose `load` +//! defaults to the `.wasm` beside it. use std::collections::{BTreeMap, BTreeSet, HashMap}; use std::fmt::Write; use crate::abi::{self, Abi, Ty, pascal}; +use crate::config::Package; use crate::glue::rs; use crate::ir::{Access, Closure, Family, Function, Int, Literal, Type}; use crate::kotlin::strip_refs; @@ -181,7 +186,8 @@ fn round(n: u32, align: u32) -> u32 { /// The TypeScript for `abi`'s items on [`TARGET`], whose header's contract is `contract`, /// or none if the API does not serve it. -pub(crate) fn generate(abi: &Abi<'_>, packages: &str, contract: u64) -> Result, String> { +pub(crate) fn generate(abi: &Abi<'_>, facade: &Package, contract: u64) -> Result, String> { + let packages = &facade.name; let api = abi.api; let Some(web) = api.targets.iter().position(|t| t == TARGET) else { return Ok(None); @@ -255,7 +261,7 @@ pub(crate) fn generate(abi: &Abi<'_>, packages: &str, contract: u64) -> Result {{\n\treturn rt.load(source, {{ prefix: \"{p}\", space: \"{space}\", file: \"index.ts\", contract: 0x{contract:016x}n, target: {}, imports: g.imports }});\n}}\n\n{}", + "\n/**\n * Compile and instantiate the module `cargo build --target {TARGET} --features web` made of\n * the crate this binding was generated with, from `source`, before any other call: its\n * bytes, a `Response` such as `fetch` gives, a compiled `WebAssembly.Module`, or its URL.\n * By default it is `{p}.wasm` beside this module, as the package holds it, whose URL Vite\n * and webpack follow into the bundle. A runtime with source phase imports passes\n * `import source module from \"/{p}.wasm\"`.\n */\nexport function load(source: rt.Source = new URL(\"./{p}.wasm\", import.meta.url)): Promise {{\n\treturn rt.load(source, {{ prefix: \"{p}\", space: \"{space}\", file: \"index.ts\", contract: 0x{contract:016x}n, target: {}, imports: g.imports }});\n}}\n\n{}", g.web, index.body ); files.push(("index.ts".to_string(), trim(out))); @@ -275,6 +281,15 @@ pub(crate) fn generate(abi: &Abi<'_>, packages: &str, contract: u64) -> Result, packages: &str, contract: u64) -> Result/rt`, and the module `cargo build` makes, which +/// the package holds beside them. +fn package_json(facade: &Package, lib: &str) -> String { + // By hand, since a condition's order is its precedence: `types` before `default`. + let json = |s: &str| serde_json::Value::from(s).to_string(); + let module = |path: &str| format!("{{ \"types\": \"./{path}.d.ts\", \"default\": \"./{path}.js\" }}"); + format!( + "{{\n\t\"name\": {},\n\t\"version\": {},\n\t\"description\": {},\n\t\"type\": \"module\",\n\t\"sideEffects\": false,\n\t\"exports\": {{\n\t\t\".\": {},\n\t\t\"./{lib}.wasm\": \"./{lib}.wasm\",\n\t\t\"./*\": {}\n\t}},\n\t\"files\": [\"*.js\", \"*.d.ts\", \"{lib}.wasm\"]\n}}\n", + json(&facade.name), + json(&facade.version), + json(&format!( + "The pub API of {} for the web, generated by rust2-ffi", + facade.name + )), + module("index"), + module("*"), + ) +} + /// The imports of the runtime and the glue that `code` names. fn module_imports(code: &str) -> String { let mut out = String::new(); @@ -1876,6 +1911,16 @@ fn closed(lines: &[String], body: &[String], lent: &[String]) -> String { code.join("\n") } +/// A constant's local name in `index.ts`: its own, but `URL`'s, which would shadow the global +/// `load` takes the module's URL by, so it is exported from `URL$`. Bundlers find the +/// module by `new URL(...)` written so, which `globalThis.URL` would hide from them. +fn constant_local(name: &str) -> String { + match name { + "URL" => format!("{name}$"), + _ => name.to_string(), + } +} + /// A trait method's name in camel case, with `$` appended if a reserved word or `close`. fn method_name(name: &str) -> String { let name = camel(name); @@ -1924,7 +1969,11 @@ impl Gen<'_> { Literal::Int(Int::I64, _) => "rt.i64", Literal::Int(..) => "number", }; - let code = format!("{}export const {name}: {ty} = {value};\n\n", docs(&constant.docs, "")); + let local = constant_local(&name); + if local != name { + self.out().renames.insert(local.clone(), name.clone()); + } + let code = format!("{}export const {local}: {ty} = {value};\n\n", docs(&constant.docs, "")); self.out().body.push_str(&code); } for i in 0..api.records.len() { @@ -2699,4 +2748,11 @@ mod tests { assert_eq!(variant_fields(&fields(&["tag", "tag_name"])), ["tag$", "tagName"]); assert_eq!(field_names(&fields(&["tag", "foo", "foo_"])), ["tag", "foo", "foo2"]); } + + /// A constant named `URL` keeps clear of the global `load` constructs. + #[test] + fn constant_url() { + assert_eq!(constant_local("URL"), "URL$"); + assert_eq!(constant_local("LIMIT"), "LIMIT"); + } } diff --git a/ffi/tests/api.ts b/ffi/tests/api.ts index 9d0b4d8ec..83fb54728 100644 --- a/ffi/tests/api.ts +++ b/ffi/tests/api.ts @@ -1,7 +1,8 @@ // The api fixture's web test: the script fixtures/api/examples/transcript.rs runs as // native Rust does on wasm32-wasip1, through the generated TypeScript on Node or Deno, from // beside the binding's `web` directory, or in a browser, through tests/browser.ts's -// `process`. RUST2_WASM is the path of the module, built with the crate's `web` feature. +// `process`. RUST2_WASM is the path of the module, built with the crate's `web` feature; +// without it, the script loads the module its package holds, as `load` does by default. // tests/fixtures.rs compares the transcripts. // // With an argument, it instead checks the futures' semantics, what disposing and the @@ -1917,13 +1918,16 @@ async function broken(which: string): Promise { } async function main(): Promise { + // Without RUST2_WASM, the module beside the binding, as its package holds it, which + // `load` takes by default. const path = process.env["RUST2_WASM"]; - check(path !== undefined, "RUST2_WASM names the module"); - const module = await WebAssembly.compile(process.getBuiltinModule("node:fs").readFileSync(path!)); + const module = path === undefined ? undefined : await WebAssembly.compile(process.getBuiltinModule("node:fs").readFileSync(path)); // A load that fails leaves the next free to succeed, and a load while one runs is refused. const glue = { prefix: "api", space: "api_web", file: "stale.ts", contract: 1n, target: 0, imports: g.imports }; const message = (p: Promise) => p.then(() => "", (e: unknown) => (e instanceof Error ? e.message : String(e))); - check((await message(rt.load(module, glue))) === "stale.ts and the module are from different builds", "a stale load"); + if (module !== undefined) { + check((await message(rt.load(module, glue))) === "stale.ts and the module are from different builds", "a stale load"); + } const first = api.load(module); check((await message(api.load(module))) === "index.ts: the module is loaded already", "a load meanwhile"); await first; diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index 1efc53339..624025c4c 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -1621,7 +1621,7 @@ fn wasm_transcript() -> String { } /// The api fixture's web binding, beside copies of `tests/api.ts` and `tests/protocol.ts` -/// as a caller's project holds them, type-checked with no issue, and the module the crate's `web` feature builds +/// as a caller's project holds them, type-checked with no issue by `tsc` and by Deno, and the module the crate's `web` feature builds /// for wasm32-wasip1, built once for each runtime's test. struct Web { dir: PathBuf, @@ -1651,6 +1651,13 @@ fn web() -> &'static Web { ); step::write(&dir.join("tsconfig.json"), TSCONFIG); run(Command::new("tsc").arg("-p").arg(&dir)); + // Deno's own types too, which JSR checks a package with as it publishes. + run(Command::new("deno") + .args(["check", "api.ts", "protocol.ts"]) + .current_dir(&dir) + .env("DENO_DIR", scratch().join("deno")) + .env("DENO_NO_UPDATE_CHECK", "1") + .env("NO_COLOR", "1")); Web { dir, wasm: target.join(WEB).join("debug").join("api.wasm"), @@ -1840,6 +1847,86 @@ fn api_web() { ); } +/// The web binding as a registry serves it: packed by npm with the module beside it, +/// installed from that tarball into a project of its own, and `tests/api.ts` importing it by +/// the package's name type-checks against its declarations alone, with no `skipLibCheck`. +/// The script runs the package's JavaScript, which `load` finds the module beside by +/// default: every check on Node, which strips no type under `node_modules`, and the +/// transcript on Deno and Bun. +#[test] +fn api_web_package() { + let api = api(); + let web = web(); + let dir = scratch().join("api").join("web-package"); + let package = dir.join("package"); + step::mirror(&api.out.join("web"), &package); + step::copy(&web.wasm, &package.join("api.wasm")); + let manifest: serde_json::Value = + serde_json::from_str(&std::fs::read_to_string(package.join("package.json")).unwrap()).unwrap(); + let name = manifest["name"].as_str().expect("package.json names the package"); + let npm = |dir: &Path| { + let mut cmd = Command::new("npm"); + cmd.current_dir(dir) + .env("npm_config_cache", scratch().join("npm")) + .env("npm_config_update_notifier", "false") + .env("npm_config_fund", "false") + .env("npm_config_audit", "false"); + cmd + }; + let packed = run(npm(&package).args(["pack", "--silent", "--pack-destination"]).arg(&dir)); + let tarball = dir.join(String::from_utf8_lossy(&packed.stdout).trim()); + let consumer = dir.join("consumer"); + if consumer.exists() { + std::fs::remove_dir_all(&consumer).unwrap(); + } + let dependency = serde_json::json!({ name: format!("file:{}", tarball.display()) }); + step::write( + &consumer.join("package.json"), + serde_json::json!({ "type": "module", "private": true, "dependencies": dependency }).to_string(), + ); + run(npm(&consumer).args(["install", "--offline", "--ignore-scripts"])); + // What npm installed is the package's JavaScript and declarations, not its TypeScript. + let installed = consumer.join("node_modules").join(name); + assert!(installed.join("index.js").exists() && installed.join("index.d.ts").exists()); + assert!(installed.join("api.wasm").exists()); + assert!( + !installed.join("index.ts").exists(), + "the npm package ships its TypeScript" + ); + let script = std::fs::read_to_string(manifest_dir().join("tests").join("api.ts")).unwrap(); + let script = script + .replace("\"./web/index.ts\"", &format!("\"{name}\"")) + .replace("\"./web/", &format!("\"{name}/")) + .replace(".ts\";\n", "\";\n"); + assert!( + !script.contains("./web/"), + "api.ts imports the binding by a path the rewrite missed" + ); + step::write(&consumer.join("api.ts"), script); + step::write(&consumer.join("tsconfig.json"), TSCONFIG_PACKAGE); + run(Command::new("tsc").arg("-p").arg(&consumer)); + let runtime = |runtime: &str, case: Option<&str>| { + let mut cmd = Command::new(runtime); + if runtime == "deno" { + cmd.args(["run", "--allow-read", "--allow-env"]) + .env("DENO_DIR", scratch().join("deno")) + .env("DENO_NO_UPDATE_CHECK", "1") + .env("NO_COLOR", "1"); + } + cmd.arg("api.ts").args(case).current_dir(&consumer); + Ran::from(output(&mut cmd)) + }; + web_checks("node's package", &WEB_CASES, &mut |case| runtime("node", case)); + for other in ["deno", "bun"] { + let ran = runtime(other, None); + assert!(ran.ok, "{other}'s transcript of the package failed:\n{}", ran.stderr); + assert_eq!( + ran.stdout, web.transcript, + "{other}'s transcript of the package differs from native Rust's" + ); + } +} + /// `api_web`'s checks in headless Chromium and Firefox, which only the bench shell carries: /// `tests/browser.ts` bundles the script with Bun and runs each case in a frame of its own. /// Firefox exposes no garbage collector to a page, so it runs neither `owned` nor `bytes`. @@ -1904,7 +1991,29 @@ const TSCONFIG: &str = r#"{ "skipLibCheck": true, "types": [] }, - "include": ["api.ts", "protocol.ts", "web/*.ts"] + "include": ["api.ts", "protocol.ts", "web/*.ts"], + "exclude": ["web/*.d.ts"] +} +"#; + +/// What `tsc` checks a caller of the web binding's npm package with: as `TSCONFIG`, but +/// resolving the package as Node does, through its `exports` and declarations alone. +const TSCONFIG_PACKAGE: &str = r#"{ + "compilerOptions": { + "strict": true, + "noEmit": true, + "pretty": false, + "noUnusedLocals": true, + "noUnusedParameters": true, + "target": "es2024", + "lib": ["es2024", "esnext.disposable", "dom"], + "module": "nodenext", + "moduleResolution": "nodenext", + "verbatimModuleSyntax": true, + "erasableSyntaxOnly": true, + "types": [] + }, + "include": ["api.ts"] } "#; diff --git a/quest/m1/ffi-packaging/README.md b/quest/m1/ffi-packaging/README.md index a8dc4df94..d7d14b6d5 100644 --- a/quest/m1/ffi-packaging/README.md +++ b/quest/m1/ffi-packaging/README.md @@ -22,5 +22,4 @@ Each child follows the builder's split: the shared, read-only builder derives ev from Cargo metadata and the export, and the caller's workflow holds only what Cargo cannot know, such as who may publish. Each builds, checks in a clean consumer against native Rust's transcript, and publishes through trusted publishing where the registry has it. -`ffi/release/README.md`'s "Other registries" table loses each row as its child lands; its -npm row ("needs a Node generator first") is stale since the web target landed. +`ffi/release/README.md`'s "Other registries" table loses each row as its child lands. diff --git a/quest/m1/ffi-packaging/npm.md b/quest/m1/ffi-packaging/npm.md deleted file mode 100644 index 3e5dc26c0..000000000 --- a/quest/m1/ffi-packaging/npm.md +++ /dev/null @@ -1,26 +0,0 @@ -# [M] The web binding on npm and JSR - -## Goal - -The web export installs and imports from a registry: it writes `.js` and `.d.ts` beside -its `.ts`, a `package.json` with an `exports` map, and a `load()` that defaults to the -`.wasm` beside it through `new URL(..., import.meta.url)`, which Vite and webpack follow. -The builder publishes it to npm with trusted publishing and to JSR. Clean consumers under -Node, Deno, Bun, Vite, and webpack import it and run the transcript. -[The binding benchmark](/bench/ffi/README.md)'s size table gains the web binding's rows: -the `.wasm` stripped, gzipped, and brotli'd, the `.ts`, and a minified, tree-shaken -bundle of one caller. - -## Plan - -Found by the 2026-11-18 language audit: `web/` ships only `.ts`, which Node refuses to -strip under `node_modules` ([nodejs#57215](https://github.com/nodejs/node/issues/57215), -closed as not planned), with no `package.json`; and the epic's requirement that every -generator report its size raw and gzipped has no web row. The generator already writes -only syntax type stripping erases, so emitting `.js` is mechanical, except the import -specifiers: today's modules import `./rt.ts` and `./glue.ts`, and each emitted `.js` must -import the `.js` counterpart of every module instead, so no consumer needs a TypeScript -loader. Document `import -source`, which `load` already accepts as a `WebAssembly.Module`. - -Public API: the package layout and `load()`'s default source.