Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ jobs:
if: ${{ !cancelled() }}
run: cat ffi/target/tmp/corpus-${{ matrix.shard }}-of-4/report.md >> "$GITHUB_STEP_SUMMARY"

# The api fixture's web test in headless Chromium and Firefox, which only the bench shell
# The api fixture's web test in headless Chromium, Firefox, and WebKit, which only the bench shell
# carries: a job of its own, so a browser's failure hides no other result.
ffi-web:
name: Web binding in browsers
Expand Down
4 changes: 2 additions & 2 deletions ffi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1443,7 +1443,7 @@ packs the api fixture's with npm, installs it into a project of its own, type-ch
payload costs 0.2 µs a call at any length, against a copy's 0.5 µs at 100 B, 1.1 µs at
1 KiB, and 97 µs at 1 MiB; [the benchmark](../bench/ffi/README.md#lending-returned-bytes)
has each engine's.
- **Objects** are an `rt.Handle`, disposed by `[Symbol.dispose]`, as `using` does. Of a
- **Objects** are an `rt.Handle`, disposed by `[Symbol.dispose]`, as `using` does; where the engine lacks that symbol, as stable Safari does, `rt` defines it as `Symbol.for("Symbol.dispose")`, which esbuild's `using` falls back to too. Of a
checked type, disposing it drops the value through Rust's own `_drop`, which fails while
a call borrows it; of any other, it throws while a call holds it. The handle is freed once
disposed and no call holds it, so one disposed while a later argument lowers fails the
Expand Down Expand Up @@ -1485,7 +1485,7 @@ just ffi shapes # round-trip every sampled type shape in every language but KN
just ffi corpus # census, export, and bind each crate of corpus/crates.txt through a facade, as the nightly does; it runs their build scripts
just ffi corpus-update # take corpus/crates.txt afresh from crates.io's top crates, and resolve corpus/Cargo.lock
just ffi protocol # run longer protocol scripts from more seeds through the C ABI and every binding, as the nightly does
nix develop .#bench --command just ffi browsers # api's TypeScript in headless Chromium and Firefox, as the nightly runs it
nix develop .#bench --command just ffi browsers # api's TypeScript in headless Chromium, Firefox, and on Linux WebKit, lowering `using` three ways for WebKit, as the nightly runs it
nix develop .#android --command just ffi android # the Kotlin test, shrunk by R8, on the Android device adb reaches
nix develop .#bench --command just ffi bench # the binding benchmark: bench/ffi/README.md's cases from Rust, and through the export from C, C++, Kotlin, Python, Swift, Go, and Dart
nix develop .#bench --command just ffi codspeed # after bench-build, each single-threaded case once under CodSpeed's CPU simulation, as the CodSpeed workflow runs it
Expand Down
2 changes: 1 addition & 1 deletion ffi/justfile
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ corpus-update:
protocol:
cargo test --locked --test fixtures protocol_nightly -- --ignored

# Run the api fixture's web test in headless Chromium and Firefox, in the bench shell, which carries them: nix develop .#bench.
# Run the api fixture's web test in headless Chromium, Firefox, and on Linux WebKit, in the bench shell, which carries them: nix develop .#bench.
browsers:
cargo test --locked --test fixtures -- --ignored --exact api_browsers

Expand Down
5 changes: 5 additions & 0 deletions ffi/runtime/rt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
// past 32 bits, futures as promises, what JavaScript implements for Rust, and the web APIs
// that stand in for what the module's std imports.

// `Symbol.dispose` where the engine lacks it, as stable Safari does: the registry's symbol,
// which esbuild's `using` helper falls back to too, though TypeScript's throws without one.
// It runs before any class below keys a method on it.
if (!Symbol.dispose) Object.defineProperty(Symbol, "dispose", { value: Symbol.for("Symbol.dispose") });

declare const brand: unique symbol;

/** A `u64` past 2^53: its high and low 32 bits, each a uint32. */
Expand Down
10 changes: 10 additions & 0 deletions ffi/tests/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1425,6 +1425,16 @@ async function closedObjects(): Promise<void> {
const c = Counter.new("c", 1);
close(c);
closed(() => Counter.get(c), "Counter", "a method of a disposed object");
// `using` disposes at the end of its scope, as stable Safari, which lacks `Symbol.dispose`,
// needs the runtime's own symbol for, however the bundler lowers it. No garbage collector
// is involved, so every browser runs it.
let scoped: ReturnType<typeof Counter.new> | undefined;
{
using inner = Counter.new("u", 3);
scoped = inner;
check(Counter.get(inner) === 3, "a `using` object is open in its scope");
}
closed(() => Counter.get(scoped!), "Counter", "a `using` object after its scope");
closed(() => Counter.clone(c), "Counter", "clone of a disposed object");
closed(() => Counter.debug(c), "Counter", "debug of a disposed object");
const url = unwrap(Url.parse("https://example.com"));
Expand Down
69 changes: 60 additions & 9 deletions ffi/tests/browser.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,73 @@
// Runs tests/api.ts's cases in one headless browser, Chromium or Firefox, as Node runs
// them: Bun bundles the script and its binding for the page, and each case runs in a
// Runs tests/api.ts's cases in one headless browser, Chromium, Firefox, or WebKit, as Node
// runs them: the script and its binding are bundled for the page, and each case runs in a
// frame of its own, a fresh realm, against a `process` that serves the module's bytes, the
// case, and the garbage collector where the browser exposes it. Each case posts whether it
// finished and what it printed, which this prints as a line of JSON.
//
// usage: bun browser.ts <chromium|firefox> <script-dir> <module.wasm> <out-dir> <case>...
// How `using` in the script reaches the page is the bundle's: `bun` is Bun's bundler, which
// lowers it; `native` leaves it to the engine; `esbuild` and `tsc` lower it as a consumer's
// build would, to the helper that falls back to `Symbol.for("Symbol.dispose")` and to the one
// that throws without `Symbol.dispose`, which stable Safari lacks.
//
// usage: bun browser.ts <chromium|firefox|webkit> <bun|native|esbuild|tsc> <script-dir> <module.wasm> <out-dir> <case>...
// where a case is `-` for the transcript.

import { mkdir, rm } from "node:fs/promises";
import { join, resolve } from "node:path";

const [browser, dirArg, wasmPath, outArg, ...cases] = process.argv.slice(2);
if (!browser || !dirArg || !wasmPath || !outArg || cases.length === 0) {
throw new Error("usage: browser.ts <chromium|firefox> <script-dir> <module.wasm> <out-dir> <case>...");
const [browser, lowering, dirArg, wasmPath, outArg, ...cases] = process.argv.slice(2);
if (!browser || !lowering || !dirArg || !wasmPath || !outArg || cases.length === 0) {
throw new Error("usage: browser.ts <chromium|firefox|webkit> <bun|native|esbuild|tsc> <script-dir> <module.wasm> <out-dir> <case>...");
}
const out = resolve(outArg);
await rm(out, { recursive: true, force: true });
await mkdir(out, { recursive: true });

const bundled = await Bun.build({ entrypoints: [join(dirArg, "api.ts")], target: "browser", format: "esm" });
if (!bundled.success) throw new AggregateError(bundled.logs, "bundling api.ts failed");
await Bun.write(join(out, "api.js"), bundled.outputs[0]!);
/** Runs a build tool, failing with its output. */
async function build(...command: string[]): Promise<void> {
const tool = Bun.spawn(command, { stdout: "pipe", stderr: "pipe" });
const [stdout, stderr, code] = await Promise.all([tool.stdout.text(), tool.stderr.text(), tool.exited]);
if (code !== 0) throw new Error(`${command.join(" ")} exited with ${code}:\n${stdout}${stderr}`);
}

const entry = join(dirArg, "api.ts");
const bundle = join(out, "api.js");
switch (lowering) {
case "bun": {
const bundled = await Bun.build({ entrypoints: [entry], target: "browser", format: "esm" });
if (!bundled.success) throw new AggregateError(bundled.logs, "bundling api.ts failed");
await Bun.write(bundle, bundled.outputs[0]!);
break;
}
case "native":
await build("esbuild", entry, "--bundle", "--format=esm", "--target=esnext", `--outfile=${bundle}`);
break;
case "esbuild":
await build("esbuild", entry, "--bundle", "--format=esm", "--target=es2022", `--outfile=${bundle}`);
break;
case "tsc": {
// The script's own `tsc` emit, lowered to ES2022 as a project's build would, then bundled.
const emitted = join(out, "tsc");
await build(
"tsc",
"-p",
dirArg,
"--noEmit",
"false",
"--outDir",
emitted,
"--target",
"es2022",
"--rewriteRelativeImportExtensions",
"--allowImportingTsExtensions",
"false",
);
await build("esbuild", join(emitted, "api.js"), "--bundle", "--format=esm", "--target=esnext", `--outfile=${bundle}`);
break;
}
default:
throw new Error(`no lowering ${lowering}`);
}

// The frame of one case: a `process` as api.ts reads Node's, then the script. What it
// printed, and whether it finished, go back once it settles; an exit or a throw ends it.
Expand Down Expand Up @@ -133,6 +180,10 @@ const command = (() => {
];
case "firefox":
return ["firefox", "--headless", "--no-remote", "--profile", profile, server.url.href];
// The `webkit-headless` the bench shell carries: Playwright's WPE build, which exposes
// no garbage collector, as Firefox does not.
case "webkit":
return ["webkit-headless", server.url.href];
default:
throw new Error(`no browser ${browser}`);
}
Expand Down
39 changes: 39 additions & 0 deletions ffi/tests/dispose.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// An engine without `Symbol.dispose`, as stable Safari is: the runtime, bundled and run in a
// context whose `Symbol` lacks it, defines it as the registry's `Symbol.for("Symbol.dispose")`,
// which esbuild's `using` helper falls back to, before any class keys a method on it.
//
// usage: node dispose.ts <runtime-entry>

import { execFileSync } from "node:child_process";
import vm from "node:vm";

const entry = process.argv[2];
if (!entry) throw new Error("usage: dispose.ts <runtime-entry>");
const code = execFileSync("esbuild", [entry, "--bundle", "--format=iife", "--global-name=runtime", "--target=es2022"], {
encoding: "utf8",
});

// The built-in `Symbol`, but for `dispose` and `asyncDispose`, which cannot be deleted from it.
const FakeSymbol = Object.defineProperties(
(description?: string) => Symbol(description),
Object.fromEntries(Reflect.ownKeys(Symbol).filter((key) => key !== "dispose" && key !== "asyncDispose").map((key) => [key, Object.getOwnPropertyDescriptor(Symbol, key)!])),
);

// What a browser gives the runtime that a bare context lacks.
const { TextEncoder, TextDecoder, URL, console, queueMicrotask, setTimeout, clearTimeout, performance } = globalThis;
const context = vm.createContext({ Symbol: FakeSymbol, TextEncoder, TextDecoder, URL, console, queueMicrotask, setTimeout, clearTimeout, performance });
if (vm.runInContext("typeof Symbol.dispose", context) !== "undefined") throw new Error("the context's Symbol still has dispose");
vm.runInContext(`${code}\nglobalThis.runtime = runtime;`, context);

const registry = Symbol.for("Symbol.dispose");
const defined = vm.runInContext("Symbol.dispose", context);
if (defined !== registry) throw new Error(`Symbol.dispose is ${String(defined)}, not the registry's`);

// Each class that disposes does so under that symbol, and none under the string "undefined".
let disposing = 0;
for (const [name, value] of Object.entries(vm.runInContext("runtime", context) as Record<string, unknown>)) {
if (typeof value !== "function" || !value.prototype) continue;
if (Object.hasOwn(value.prototype, "undefined")) throw new Error(`${name} keys a method on "undefined"`);
if (Object.getOwnPropertySymbols(value.prototype).includes(registry)) disposing++;
}
if (disposing === 0) throw new Error("no exported class disposes under the registry's symbol");
41 changes: 32 additions & 9 deletions ffi/tests/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1867,6 +1867,15 @@ fn api_web() {
);
}

/// Where the engine lacks `Symbol.dispose`, as stable Safari does, the web runtime defines it
/// as the registry's symbol before its classes key a method on it, which `dispose.ts` checks.
#[test]
fn web_without_dispose() {
run(Command::new("node")
.arg(manifest_dir().join("tests").join("dispose.ts"))
.arg(manifest_dir().join("runtime").join("rt.ts")));
}

/// 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`.
Expand Down Expand Up @@ -1947,30 +1956,43 @@ fn api_web_package() {
}
}

/// `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`.
/// `api_web`'s checks in headless Chromium, Firefox, and WebKit, which only the bench shell
/// carries, WebKit on Linux alone, where nixpkgs builds it: `tests/browser.ts` bundles the
/// script and runs each case in a frame of its own.
/// Firefox and WebKit expose no garbage collector to a page, so they run neither `owned` nor
/// `bytes`. The script's `using` runs in WebKit as the engine has it, and lowered as `tsc` and
/// esbuild lower it, each of which keys on `Symbol.dispose`. Playwright's WebKit has the symbol,
/// which stable Safari lacks: `web_without_dispose` covers its absence.
#[test]
#[ignore = "the nightly's, in the bench shell: just ffi browsers"]
fn api_browsers() {
let web = web();
for browser in ["chromium", "firefox"] {
for (browser, lowering) in [
("chromium", "bun"),
("firefox", "bun"),
("webkit", "native"),
("webkit", "esbuild"),
("webkit", "tsc"),
]
.into_iter()
.filter(|(browser, _)| *browser != "webkit" || cfg!(target_os = "linux"))
{
let cases: Vec<&str> = match browser {
"firefox" => WEB_CASES
"chromium" => WEB_CASES.to_vec(),
_ => WEB_CASES
.into_iter()
.filter(|c| !matches!(*c, "owned" | "bytes"))
.collect(),
_ => WEB_CASES.to_vec(),
};
let all = std::iter::once("-")
.chain(cases.iter().copied())
.chain(WEB_BROKEN.iter().map(|(case, _)| *case));
let output = run(Command::new("bun")
.arg(manifest_dir().join("tests").join("browser.ts"))
.arg(browser)
.args([browser, lowering])
.arg(&web.dir)
.arg(&web.wasm)
.arg(scratch().join("api").join(browser))
.arg(scratch().join("api").join(format!("{browser}-{lowering}")))
.args(all));
let mut runs: std::collections::HashMap<String, Ran> = String::from_utf8_lossy(&output.stdout)
.lines()
Expand All @@ -1985,7 +2007,7 @@ fn api_browsers() {
(text("case"), ran)
})
.collect();
web_checks(browser, &cases, &mut |case| {
web_checks(&format!("{browser} ({lowering})"), &cases, &mut |case| {
let case = case.unwrap_or("-");
runs.remove(case).unwrap_or_else(|| panic!("{browser} ran no {case}"))
});
Expand All @@ -1999,6 +2021,7 @@ const TSCONFIG: &str = r#"{
"strict": true,
"noEmit": true,
"pretty": false,
"noImplicitOverride": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"target": "es2024",
Expand Down
33 changes: 26 additions & 7 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,21 @@
overlays = [ (import rust-overlay) ];
};
toolchain = pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml;
# Playwright's WPE build of WebKit, headless over Mesa's software EGL, so a host
# with no GPU or display runs it: `webkit-headless <url>`. Its tip-of-tree engine
# stands in for Safari, which has no Linux build.
webkit-headless =
let
browsers = pkgs.playwright-driver.browsers;
webkit = pkgs.lib.findFirst (
name: pkgs.lib.hasPrefix "webkit-" name
) (throw "playwright-driver ships no WebKit") (builtins.attrNames browsers.passthru.entries);
in
pkgs.writeShellScriptBin "webkit-headless" ''
export LIBGL_ALWAYS_SOFTWARE=1
export __EGL_VENDOR_LIBRARY_DIRS=${pkgs.mesa}/share/glvnd/egl_vendor.d
exec ${browsers.passthru.entries.${webkit}}/minibrowser-wpe/MiniBrowser --headless "$@"
'';
# The benchmark's nightly levers (--jobs-frontend, cranelift, self-profile).
# Bumping the date is a benchmark corpus change: numbers before and
# after it do not compare.
Expand Down Expand Up @@ -299,14 +314,18 @@
'';
};
# `just ts bench` adds Chrome and Firefox as engines, and `just ffi browsers` runs the
# web binding's test in them. Apart for their size. Binaryen's wasm2js lowers the
# bench's wasm32 build to JavaScript for its wasm2js route.
# web binding's test in them, and in WebKit on Linux, which stands in for Safari. Apart for
# their size. Binaryen's wasm2js lowers the bench's wasm32 build to JavaScript for its
# wasm2js route.
devShells.bench = self.devShells.${system}.default.overrideAttrs (old: {
nativeBuildInputs = old.nativeBuildInputs ++ [
pkgs.chromium
pkgs.firefox
pkgs.binaryen
];
nativeBuildInputs =
old.nativeBuildInputs
++ [
pkgs.chromium
pkgs.firefox
pkgs.binaryen
]
++ pkgs.lib.optional pkgs.stdenv.hostPlatform.isLinux webkit-headless;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
# `just ffi bench` builds moq's rs/libmoq, whose VAAPI bindings run bindgen.
LIBCLANG_PATH = "${pkgs.llvmPackages.libclang.lib}/lib";
# ... and its programs over instrument-hooks, for CodSpeed.
Expand Down
29 changes: 0 additions & 29 deletions quest/m2/wasm/ts-consumers.md

This file was deleted.

Loading