diff --git a/.github/workflows/cmake-test-release-linux.yml b/.github/workflows/cmake-test-release-linux.yml index 7e8bb5c3b..6c8d40dab 100644 --- a/.github/workflows/cmake-test-release-linux.yml +++ b/.github/workflows/cmake-test-release-linux.yml @@ -159,4 +159,41 @@ jobs: LLVM_SYMBOLIZER_PATH: ${{github.workspace}}/3rdParty/llvm/release/bin TSLANG_TEST_PROGRAMS: ${{ matrix.programs }} KIND: ${{ matrix.kind }} + + # wasm32 and wasm64, as in the Windows workflow: the exe legs only, each with its kind of program, + # against the wasm default library built here by this compiler. + - name: Set up Node (wasm tests) + if: matrix.kind == 'exe' + uses: actions/setup-node@v4 + with: + node-version: 24 + + - name: Build wasm Default Library (${{ matrix.programs }}) + if: matrix.kind == 'exe' + working-directory: ${{github.workspace}} + shell: bash + env: + TOOL_PATH: ${{github.workspace}}/__build/tslang/ninja/release/bin + LLVM_AR: ${{github.workspace}}/3rdParty/llvm/release/bin/llvm-ar + MODE: ${{ matrix.programs }} + run: | + set -e + git clone --depth 1 https://github.com/ASDAlexander77/TypeScriptCompilerDefaultLib.git + bash ./TypeScriptCompilerDefaultLib/scripts/build_wasm.sh "wasm32 wasm64" "$MODE" + + - name: Test wasm32 and wasm64 (${{ matrix.programs }} programs) + if: matrix.kind == 'exe' + working-directory: ${{github.workspace}}/tslang/test/wasm + shell: bash + env: + MODE: ${{ matrix.programs }} + run: | + # wasm-ld, which tslang links a wasm program with + export PATH="${{github.workspace}}/3rdParty/llvm/release/bin:$PATH" + debug=""; if [ "$MODE" = debug ]; then debug=--debug; fi + code=0 + node run_tests.mjs --tslang=${{github.workspace}}/__build/tslang/ninja/release/bin/tslang \ + --default-lib-repo=${{github.workspace}}/TypeScriptCompilerDefaultLib --require-run -j4 $debug || code=$? + if [ $code -ne 0 ]; then cat out/results.txt; fi + exit $code diff --git a/.github/workflows/cmake-test-release-win.yml b/.github/workflows/cmake-test-release-win.yml index c8c7331df..0ada8251c 100644 --- a/.github/workflows/cmake-test-release-win.yml +++ b/.github/workflows/cmake-test-release-win.yml @@ -167,3 +167,47 @@ jobs: $filter = if ($env:KIND -eq 'jit') { '-R' } else { '-E' } ctest -j8 -C ${{ env.BUILD_TYPE }} $filter '^test-jit' -T test --output-on-failure shell: pwsh + + # wasm32 and wasm64 (tslang/test/wasm): the tester's tests and the default library's, under + # -mm=none, rc and own, each program run under Node with the JavaScript host. The exe legs only, + # each with its kind of program (release: --opt; debug: --di --opt_level=0), against the wasm + # default library built here by this compiler from TypeScriptCompilerDefaultLib's main. + # wasm64 needs Node 24 (64-bit tables); --require-run fails a test that is only compiled. + - name: Set up Node (wasm tests) + if: matrix.kind == 'exe' + uses: actions/setup-node@v4 + with: + node-version: 24 + + - name: Build wasm Default Library (${{ matrix.programs }}) + if: matrix.kind == 'exe' + working-directory: ${{github.workspace}} + env: + TOOL_PATH: ${{github.workspace}}/__build/tslang/msbuild/x64/release/bin + LLVM_AR: ${{github.workspace}}/3rdParty/llvm/x64/release/bin/llvm-ar.exe + MODE: ${{ matrix.programs }} + run: | + git clone --depth 1 https://github.com/ASDAlexander77/TypeScriptCompilerDefaultLib.git + if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } + foreach ($arch in 'wasm32', 'wasm64') { + & .\TypeScriptCompilerDefaultLib\scripts\build_wasm.bat $arch $env:MODE + if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } + } + shell: pwsh + + - name: Test wasm32 and wasm64 (${{ matrix.programs }} programs) + if: matrix.kind == 'exe' + working-directory: ${{github.workspace}}/tslang/test/wasm + env: + MODE: ${{ matrix.programs }} + run: | + # wasm-ld, which tslang links a wasm program with + $env:PATH = "${{github.workspace}}\3rdParty\llvm\x64\release\bin;$env:PATH" + $runArgs = @("--tslang=${{github.workspace}}\__build\tslang\msbuild\x64\release\bin\tslang.exe", + "--default-lib-repo=${{github.workspace}}\TypeScriptCompilerDefaultLib", "--require-run", "-j4") + if ($env:MODE -eq 'debug') { $runArgs += '--debug' } + node run_tests.mjs @runArgs + $code = $LASTEXITCODE + if ($code -ne 0) { Get-Content out\results.txt } + exit $code + shell: pwsh diff --git a/.github/workflows/create-release.yml b/.github/workflows/create-release.yml index 584743abb..68ce5fda7 100644 --- a/.github/workflows/create-release.yml +++ b/.github/workflows/create-release.yml @@ -215,6 +215,20 @@ jobs: # The Default Library's own tests run in test_defaultlib_windows, one job per memory model, # against the package this job uploads. + # wasm32 and wasm64 (release and debug, -mm=rc, none and own) into the same tree, so the + # package carries them: defaultlib/lib//unknown/unknown/unknown//. + # They are tested in test_wasm_windows, against the package. + - name: Build wasm Default Library + continue-on-error: false + working-directory: ${{github.workspace}}/TypeScriptCompilerDefaultLib + env: + TOOL_PATH: ${{github.workspace}}/__build/tslang/msbuild/x64/release/bin + LLVM_AR: ${{github.workspace}}/3rdParty/llvm/x64/release/bin/llvm-ar.exe + run: | + .\scripts\build_wasm.bat + if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } + shell: pwsh + # Android (API 29, arm64-v8a, armeabi-v7a and x86_64): the collector, the async runtime and the default # library, cross-built with the runner's Android NDK by the same scripts a developer runs. # tslang links for Android through the NDK too (--android-ndk-path / ANDROID_NDK_HOME). @@ -455,6 +469,17 @@ jobs: # As on Windows: the Default Library's own tests run in test_defaultlib_linux. + # As on Windows: wasm32 and wasm64 into the package's default-library tree, tested in + # test_wasm_linux. + - name: Build wasm Default Library + continue-on-error: false + working-directory: ${{github.workspace}}/TypeScriptCompilerDefaultLib + shell: bash + env: + TOOL_PATH: ${{github.workspace}}/__build/tslang/ninja/release/bin + LLVM_AR: ${{github.workspace}}/3rdParty/llvm/release/bin/llvm-ar + run: bash ./scripts/build_wasm.sh + - name: Create Tar.GZ of Linux Asset working-directory: ${{github.workspace}}/__build shell: sh @@ -607,6 +632,127 @@ jobs: chmod +x ./tests.sh ./tests.sh "$MODEL" + # wasm32 and wasm64 (tslang/test/wasm), against each package: its tslang, the wasm-ld beside it + # and its wasm default library. The tester's tests and the default library's, under -mm=none, rc + # and own, each program run under Node with the JavaScript host; one job per kind of program. + # wasm64 needs Node 24; --require-run fails a test that is only compiled. + test_wasm_windows: + name: Test wasm (Windows, ${{ matrix.programs }} programs) + needs: [defaultlib_commit, build_on_windows] + strategy: + fail-fast: false + matrix: + programs: [release, debug] + runs-on: windows-2025-vs2026 + timeout-minutes: 90 + env: + TOOL_PATH: ${{ github.workspace }}\__tslang + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Node + uses: actions/setup-node@v4 + with: + node-version: 24 + + - name: Clone Default Library + shell: pwsh + env: + DEFAULTLIB_SHA: ${{ needs.defaultlib_commit.outputs.sha }} + run: | + git clone https://github.com/ASDAlexander77/TypeScriptCompilerDefaultLib.git + if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } + git -C TypeScriptCompilerDefaultLib checkout --detach $env:DEFAULTLIB_SHA + if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } + + - name: Download Release Asset from Store + uses: actions/download-artifact@v4 + with: + name: tslang-windows-asset + + - name: Unpack the package + shell: pwsh + run: | + Expand-Archive -Path "${{ env.ASSET_NAME }}-windows-x64.zip" -DestinationPath $env:TOOL_PATH + Remove-Item "${{ env.ASSET_NAME }}-windows-x64.zip" + New-Item -ItemType Directory -Force -Path TypeScriptCompilerDefaultLib\__build | Out-Null + Move-Item -Path (Join-Path $env:TOOL_PATH 'defaultlib') -Destination TypeScriptCompilerDefaultLib\__build\defaultlib -ErrorAction Stop + & (Join-Path $env:TOOL_PATH 'tslang.exe') --version + + - name: Test wasm32 and wasm64 (${{ matrix.programs }} programs) + shell: pwsh + working-directory: ${{ github.workspace }}/tslang/test/wasm + env: + MODE: ${{ matrix.programs }} + run: | + $runArgs = @("--tslang=$(Join-Path $env:TOOL_PATH 'tslang.exe')", + "--default-lib-repo=${{ github.workspace }}\TypeScriptCompilerDefaultLib", "--require-run", "-j4") + if ($env:MODE -eq 'debug') { $runArgs += '--debug' } + node run_tests.mjs @runArgs + $code = $LASTEXITCODE + if ($code -ne 0) { Get-Content out\results.txt } + exit $code + + test_wasm_linux: + name: Test wasm (${{ matrix.os }}, ${{ matrix.programs }} programs) + needs: [defaultlib_commit, build_on_linux] + strategy: + fail-fast: false + matrix: + os: [ubuntu-22.04, ubuntu-24.04, ubuntu-26.04] + programs: [release, debug] + runs-on: ${{ matrix.os }} + timeout-minutes: 90 + env: + TOOL_PATH: ${{ github.workspace }}/__tslang + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Node + uses: actions/setup-node@v4 + with: + node-version: 24 + + - name: Clone Default Library + shell: bash + env: + DEFAULTLIB_SHA: ${{ needs.defaultlib_commit.outputs.sha }} + run: | + set -e + git clone https://github.com/ASDAlexander77/TypeScriptCompilerDefaultLib.git + git -C TypeScriptCompilerDefaultLib checkout --detach "$DEFAULTLIB_SHA" + + - name: Download Release Asset from Store + uses: actions/download-artifact@v4 + with: + name: tslang-${{ matrix.os }}-asset + + - name: Unpack the package + shell: bash + run: | + set -e + mkdir -p "$TOOL_PATH" + tar -xzf "${{ env.ASSET_NAME }}-${{ matrix.os }}-x64.tar.gz" -C "$TOOL_PATH" + rm "${{ env.ASSET_NAME }}-${{ matrix.os }}-x64.tar.gz" + mkdir -p TypeScriptCompilerDefaultLib/__build + mv "$TOOL_PATH/defaultlib" TypeScriptCompilerDefaultLib/__build/defaultlib + "$TOOL_PATH/tslang" --version + + - name: Test wasm32 and wasm64 (${{ matrix.programs }} programs) + shell: bash + working-directory: ${{ github.workspace }}/tslang/test/wasm + env: + MODE: ${{ matrix.programs }} + run: | + debug=""; if [ "$MODE" = debug ]; then debug=--debug; fi + code=0 + node run_tests.mjs --tslang="$TOOL_PATH/tslang" \ + --default-lib-repo=${{ github.workspace }}/TypeScriptCompilerDefaultLib --require-run -j4 $debug || code=$? + if [ $code -ne 0 ]; then cat out/results.txt; fi + exit $code + # The build jobs above test release programs. The debug ones (TSLANG_TEST_PROGRAMS=debug: debug # info, no optimization), as a pull request's CI runs them: its own workflows, those legs only. test_debug_programs_windows: @@ -623,7 +769,7 @@ jobs: create_release: needs: [build_on_windows, build_on_linux, test_defaultlib_windows, test_defaultlib_linux, - test_debug_programs_windows, test_debug_programs_linux] + test_wasm_windows, test_wasm_linux, test_debug_programs_windows, test_debug_programs_linux] name: Create Release runs-on: ubuntu-latest steps: diff --git a/tslang/test/wasm/.gitignore b/tslang/test/wasm/.gitignore new file mode 100644 index 000000000..39ce2cd40 --- /dev/null +++ b/tslang/test/wasm/.gitignore @@ -0,0 +1,2 @@ +out*/ +wasm/ diff --git a/docs/how/wasm/test/README.md b/tslang/test/wasm/README.md similarity index 89% rename from docs/how/wasm/test/README.md rename to tslang/test/wasm/README.md index 55658b0e3..793f59d62 100644 --- a/docs/how/wasm/test/README.md +++ b/tslang/test/wasm/README.md @@ -91,6 +91,16 @@ the test is listed as NATIVE and not counted as a failure: it is something `-mm= yet. Native ctest runs only a few tests under own, but the whole corpus under none and rc, so under those two models every wasm compile error counts as a failure. +## In CI + +Every pull request runs both suites on wasm32 and wasm64 under none, rc and own, on Windows and +Linux: the exe leg of each build job, with its kind of program (release, or `--debug`), against a +wasm default library that job builds from TypeScriptCompilerDefaultLib's main with the compiler it +just built (`.github/workflows/cmake-test-release-*.yml`). A release builds the wasm default library +into each package and tests the package's compiler, `wasm-ld` and library the same way, release and +debug (`test_wasm_*` in `create-release.yml`). CI passes `--require-run`: on Node 24, which wasm64 +needs, a test that is only compiled fails. + ## In a browser Put the `.wasm` files in `wasm\`, serve this folder over HTTP (e.g. `npx http-server .`) and open diff --git a/docs/how/wasm/test/host.mjs b/tslang/test/wasm/host.mjs similarity index 95% rename from docs/how/wasm/test/host.mjs rename to tslang/test/wasm/host.mjs index 981cc8956..b9e01f122 100644 --- a/docs/how/wasm/test/host.mjs +++ b/tslang/test/wasm/host.mjs @@ -37,6 +37,58 @@ export class UncaughtThrow extends Error // memory section or, for a program that starts threads, of its `env.memory` import (a shared memory // the host makes, and passes to each thread's worker). The C++ exception tag the module imports // carries a pointer, an i64 in a memory64 (wasm64) module. +// Writes all of text to fd with Node's fs. A pipe Node made non-blocking (stdout under a parent +// that reads it, as run_tests.mjs and CI do) takes part of a write, or none (EAGAIN) while it is +// full: write the rest once it drains, as a C library's blocking write would. +export function writeAllSync(fs, fd, text) +{ + const bytes = typeof text === "string" ? new TextEncoder().encode(text) : text; + let wait; + for (let off = 0; off < bytes.length;) + { + try + { + off += fs.writeSync(fd, bytes, off, bytes.length - off); + } + catch (e) + { + if (e?.code !== "EAGAIN") throw e; + Atomics.wait(wait ??= new Int32Array(new SharedArrayBuffer(4)), 0, 0, 1); + } + } +} + +// A program's output under Node, buffered as a C library buffers stdout into a pipe: a program +// that prints a line at a time (00stack_test prints two million) would otherwise make a write +// call per line, too slow on a busy CI runner. stdout is written out at 64 KB, before anything +// goes to stderr, by flush() (the host calls it before it blocks or reads stdin) and at the end. +export function bufferedOutput(fs) +{ + let pending = [], size = 0; + const flush = () => + { + if (size === 0) return; + const text = pending.join(""); + pending = []; + size = 0; + writeAllSync(fs, 1, text); + }; + const write = (fd, text) => + { + if (fd === 2) + { + flush(); + writeAllSync(fs, 2, text); + return; + } + + pending.push(text); + size += text.length; + if (size >= 65536) flush(); + }; + return { write, flush }; +} + export function memoryOf(bytes) { const b = bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes.buffer ?? bytes, bytes.byteOffset ?? 0, bytes.byteLength); @@ -88,6 +140,8 @@ export function createHost(options = {}) { const args = options.args ?? ["program.wasm"]; const write = options.write ?? ((fd, text) => (fd === 2 ? console.error : console.log)(text)); + // writes out what write() holds back: before the thread blocks or reads stdin + const flush = options.flush ?? (() => { }); const node = options.node; // { fs, process } under Node let instance; @@ -107,8 +161,23 @@ export function createHost(options = {}) const N = (v) => Number(v); const P = (v) => (is64 ? BigInt(v) : v); - const u8 = () => new Uint8Array(memory.buffer); - const dv = () => new DataView(memory.buffer); + // The memory's bytes, up to date. When another thread has grown a shared memory, this thread's + // memory.buffer lags until its isolate handles the grow (the wasm code sees the new size at + // once, JavaScript does not), and a block another thread just allocated can lie past its end: + // memory.grow(0) brings it up to date. Every address a program holds is below the heap's top + // (its stacks and thread blocks are heap blocks too), so a view that ends before the top is stale. + const buffer = () => + { + let b = memory.buffer; + if (sharedMemory && heap && new DataView(b).getFloat64(heap + 8, true) > b.byteLength) + { + memory.grow(is64 ? 0n : 0); + b = memory.buffer; + } + return b; + }; + const u8 = () => new Uint8Array(buffer()); + const dv = () => new DataView(buffer()); const utf8Decoder = new TextDecoder("utf-8"); const utf8Encoder = new TextEncoder(); @@ -165,7 +234,7 @@ export function createHost(options = {}) const ensure = (end) => { - const have = memory.buffer.byteLength; + const have = buffer().byteLength; if (end <= have) return; const pages = Math.ceil((end - have) / 65536); memory.grow(is64 ? BigInt(pages) : pages); @@ -281,6 +350,7 @@ export function createHost(options = {}) // thread) a busy wait const sleepFor = (millis) => { + flush(); try { Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, millis); return; } catch { } const end = Date.now() + millis; while (Date.now() < end) { } @@ -292,11 +362,12 @@ export function createHost(options = {}) const index = address / ArrayType.BYTES_PER_ELEMENT; if (sharedMemory) { - const result = Atomics.wait(new ArrayType(memory.buffer), index, expected, timeout); + flush(); + const result = Atomics.wait(new ArrayType(buffer()), index, expected, timeout); return result === "ok" ? 0 : result === "not-equal" ? 1 : 2; } - if (new ArrayType(memory.buffer)[index] !== expected) return 1; + if (new ArrayType(buffer())[index] !== expected) return 1; if (timeout === Infinity) throw new Error("Atomics.wait would wait forever: this program starts no thread to wake it"); sleepFor(timeout); return 2; @@ -796,6 +867,7 @@ export function createHost(options = {}) if (stdinBuffer.pos >= stdinBuffer.bytes.length) { if (stdinBuffer.eof || !node) return -1; + flush(); const chunk = new Uint8Array(65536); let n = 0; try { n = node.fs.readSync(0, chunk, 0, chunk.length, null); } catch (e) { if (e.code === "EAGAIN") return stdinByte(); if (e.code !== "EOF") n = 0; } @@ -1083,7 +1155,8 @@ export function createHost(options = {}) tslang_thread_join: (handle) => { const control = N(handle); - const i32 = new Int32Array(memory.buffer, control, 3); + const i32 = new Int32Array(buffer(), control, 3); + if (Atomics.load(i32, 0) === 0) flush(); while (Atomics.load(i32, 0) === 0) Atomics.wait(i32, 0, 0); const ended = i32[1], code = i32[2]; const thread = threads.get(control); @@ -1101,7 +1174,7 @@ export function createHost(options = {}) tslang_atomics_wait32: (address, expected, timeoutMs) => atomicsWait(Int32Array, N(address), expected, timeoutMs), tslang_atomics_wait64: (address, expected, timeoutMs) => atomicsWait(BigInt64Array, N(address), expected, timeoutMs), tslang_atomics_notify: (address, count) => - sharedMemory ? Atomics.notify(new Int32Array(memory.buffer), N(address) / 4, count < 0 ? undefined : count) : 0, + sharedMemory ? Atomics.notify(new Int32Array(buffer()), N(address) / 4, count < 0 ? undefined : count) : 0, time: (p) => { const t = BigInt(Math.floor(Date.now() / 1000)); if (N(p) !== 0) dv().setBigInt64(N(p), t, true); return t; }, tslang_wasm_now: () => Date.now(), tslang_wasm_timezone: () => standardOffset(new Date().getFullYear()) * 60, @@ -1315,7 +1388,7 @@ export function createHost(options = {}) { attach(inst); findHeap(); - const i32 = new Int32Array(memory.buffer, control, 3); + const i32 = new Int32Array(buffer(), control, 3); let ended = THREAD_RETURNED, code = 0; try { @@ -1340,6 +1413,8 @@ export function createHost(options = {}) } finally { + // what the thread printed is out before join returns + flush(); i32[1] = ended; i32[2] = code; Atomics.store(i32, 0, 1); diff --git a/docs/how/wasm/test/index.html b/tslang/test/wasm/index.html similarity index 100% rename from docs/how/wasm/test/index.html rename to tslang/test/wasm/index.html diff --git a/docs/how/wasm/test/run.mjs b/tslang/test/wasm/run.mjs similarity index 78% rename from docs/how/wasm/test/run.mjs rename to tslang/test/wasm/run.mjs index ad72e5441..6041dbf81 100644 --- a/docs/how/wasm/test/run.mjs +++ b/tslang/test/wasm/run.mjs @@ -7,7 +7,7 @@ import fs from "node:fs"; import os from "node:os"; import process from "node:process"; import worker_threads from "node:worker_threads"; -import { createHost, UncaughtThrow } from "./host.mjs"; +import { createHost, UncaughtThrow, bufferedOutput } from "./host.mjs"; const file = process.argv[2]; if (!file) @@ -16,10 +16,12 @@ if (!file) process.exit(2); } +const output = bufferedOutput(fs); const host = createHost({ args: process.argv.slice(2), node: { fs, process, worker_threads, os }, - write: (fd, text) => fs.writeSync(fd, text), + write: output.write, + flush: output.flush, }); let code; @@ -32,14 +34,15 @@ catch (e) { if (e instanceof UncaughtThrow) { - fs.writeSync(2, "Uncaught exception\n"); + output.write(2, "Uncaught exception\n"); } else { - fs.writeSync(2, `${e?.stack ?? e}\n`); + output.write(2, `${e?.stack ?? e}\n`); } code = 1; } +output.flush(); process.exitCode = code & 0xff; diff --git a/docs/how/wasm/test/run_tests.mjs b/tslang/test/wasm/run_tests.mjs similarity index 97% rename from docs/how/wasm/test/run_tests.mjs rename to tslang/test/wasm/run_tests.mjs index 61525cad0..4fb08b42e 100644 --- a/docs/how/wasm/test/run_tests.mjs +++ b/tslang/test/wasm/run_tests.mjs @@ -19,6 +19,7 @@ // -j tests at a time (default: the number of CPUs) // --out= where the .wasm files and results.txt go (default: ./out) // --timeout= per program run (default: 60) +// --require-run a test compiled but not run (wasm64 before Node 24) fails (CI) // filter a regular expression the test's file name has to match // // The tester's tests are the single-file ones its CMakeLists.txt runs (`test-compile-*`): the others @@ -39,7 +40,7 @@ import process from "node:process"; import { fileURLToPath } from "node:url"; const here = path.dirname(fileURLToPath(import.meta.url)); -const repo = path.resolve(here, "../../../.."); +const repo = path.resolve(here, "../../.."); const options = { suite: ["tester", "defaultlib"], triple: ["wasm32", "wasm64"], mm: ["none", "rc", "own"], debug: false, @@ -57,6 +58,7 @@ for (const arg of process.argv.slice(2)) else if ((m = /^-j(\d+)$/.exec(arg))) options.jobs = Number(m[1]); else if ((m = /^--out=(.+)$/.exec(arg))) options.out = path.resolve(m[1]); else if ((m = /^--timeout=(\d+)$/.exec(arg))) options.timeout = Number(m[1]); + else if (arg === "--require-run") options.requireRun = true; else if (!arg.startsWith("-")) options.filter = new RegExp(arg); else { console.error(`unknown option ${arg}`); process.exit(2); } } @@ -223,7 +225,8 @@ const runOne = async ({ leg, file, outDir }) => if (!canRun(leg)) { - return { leg: leg.name, file, status: "BUILT", detail: `compiled; not run: ${leg.triple} needs Node 24 or later (this is ${process.version})` }; + return { leg: leg.name, file, status: options.requireRun ? "FAIL" : "BUILT", + detail: `compiled; not run: ${leg.triple} needs Node 24 or later (this is ${process.version})` }; } const stdinFile = source.replace(/\.ts$/, ".stdin"); diff --git a/docs/how/wasm/test/thread.mjs b/tslang/test/wasm/thread.mjs similarity index 70% rename from docs/how/wasm/test/thread.mjs rename to tslang/test/wasm/thread.mjs index 5fccf0d0a..cfbb7cdf7 100644 --- a/docs/how/wasm/test/thread.mjs +++ b/tslang/test/wasm/thread.mjs @@ -1,19 +1,21 @@ // One thread of a tslang wasm program, in a Node worker: host.mjs's tslang_thread_spawn starts it // with the program's compiled module, its shared memory and what the thread runs (see runThread). -// The thread writes as the program does, straight to the file descriptors, so its output is not -// held back while the main thread waits in join. +// The thread writes to the file descriptors itself (bufferedOutput, flushed before it blocks and +// before join returns), so its output is not held back by the main thread. import fs from "node:fs"; import os from "node:os"; import process from "node:process"; import worker_threads from "node:worker_threads"; -import { createHost } from "./host.mjs"; +import { createHost, bufferedOutput } from "./host.mjs"; const { module, memory, memoryType, args, ...thread } = worker_threads.workerData; +const output = bufferedOutput(fs); const host = createHost({ args, node: { fs, process, worker_threads, os }, - write: (fd, text) => fs.writeSync(fd, text), + write: output.write, + flush: output.flush, }); try @@ -23,7 +25,7 @@ try catch (e) { // the thread did not start (runThread reports what ends a started one): join must still wake - fs.writeSync(2, `${e?.stack ?? e}\n`); + output.write(2, `${e?.stack ?? e}\n`); const control = new Int32Array(memory.buffer, thread.control, 3); control[1] = 2; Atomics.store(control, 0, 1); diff --git a/docs/how/wasm/test/wasm_skips.txt b/tslang/test/wasm/wasm_skips.txt similarity index 100% rename from docs/how/wasm/test/wasm_skips.txt rename to tslang/test/wasm/wasm_skips.txt