diff --git a/.agents/skills/golem-scala-base-image/SKILL.md b/.agents/skills/golem-scala-base-image/SKILL.md index 5bf40e0e46..1c25bca199 100644 --- a/.agents/skills/golem-scala-base-image/SKILL.md +++ b/.agents/skills/golem-scala-base-image/SKILL.md @@ -43,12 +43,12 @@ The current pipeline uses: - Rust stable with `wasm32-wasip2` - Preview 3 (`wasm-rquickjs --target wasi-p3`) -- `wasm-rquickjs-cli` **0.5.5**, matching `wasm-rquickjs-version` in `.github/actions/setup-main-validation/action.yml` +- `wasm-rquickjs-cli` **0.5.6**, matching `wasm-rquickjs-version` in `.github/actions/setup-main-validation/action.yml` - ordinary `cargo build --target wasm32-wasip2 --release` on generated wrapper crates; it does not use `cargo-component` ```bash rustup target add wasm32-wasip2 -cargo install --locked wasm-rquickjs-cli@0.5.5 +cargo install --locked wasm-rquickjs-cli@0.5.6 # Repository root cargo make wit diff --git a/.github/actions/setup-main-validation/action.yml b/.github/actions/setup-main-validation/action.yml index 4c7efd2068..bb805b0f15 100644 --- a/.github/actions/setup-main-validation/action.yml +++ b/.github/actions/setup-main-validation/action.yml @@ -14,7 +14,7 @@ runs: - uses: ./.validation-control/.github/actions/setup-js-wasm-tools with: node-version: '24' - wasm-rquickjs-version: '0.5.5' + wasm-rquickjs-version: '0.5.6' install-cargo-component: 'true' - uses: oven-sh/setup-bun@v2 - name: Install validation tooling for selected and control sources diff --git a/.github/workflows/benchmark.yaml b/.github/workflows/benchmark.yaml index 788f2fab4c..dbccb13501 100644 --- a/.github/workflows/benchmark.yaml +++ b/.github/workflows/benchmark.yaml @@ -14,7 +14,7 @@ permissions: contents: read env: - WASM_RQUICKJS_VERSION: "0.5.5" + WASM_RQUICKJS_VERSION: "0.5.6" WASI_SDK_VERSION: "25" WASI_SDK_PATH: /opt/wasi-sdk diff --git a/.github/workflows/prepare-golem-release.yaml b/.github/workflows/prepare-golem-release.yaml index 5b55d38342..f3bcf2c22b 100644 --- a/.github/workflows/prepare-golem-release.yaml +++ b/.github/workflows/prepare-golem-release.yaml @@ -72,7 +72,7 @@ jobs: - uses: ./.github/actions/check-published-sdks with: prepare-binaries: 'true' - wasm-rquickjs-version: '0.5.5' + wasm-rquickjs-version: '0.5.6' moonbit-version: '0.10.18+6c83a7f62' - uses: actions/upload-artifact@v4 if: always() @@ -96,7 +96,7 @@ jobs: env: CARGO_BUILD_JOBS: 20 WASI_SDK_PATH: /opt/wasi-sdk - WASM_RQUICKJS_VERSION: '0.5.5' + WASM_RQUICKJS_VERSION: '0.5.6' steps: - uses: actions/checkout@v5 with: diff --git a/.github/workflows/publish-golem-effect.yaml b/.github/workflows/publish-golem-effect.yaml index 4fd5be6e2f..3d5b137543 100644 --- a/.github/workflows/publish-golem-effect.yaml +++ b/.github/workflows/publish-golem-effect.yaml @@ -19,7 +19,7 @@ permissions: contents: read env: - WASM_RQUICKJS_VERSION: "0.5.5" + WASM_RQUICKJS_VERSION: "0.5.6" WASI_SDK_VERSION: "25" WASI_SDK_PATH: /opt/wasi-sdk NPM_CONFIG_AUDIT: "false" diff --git a/.github/workflows/publish-golem-scala.yaml b/.github/workflows/publish-golem-scala.yaml index 907c206f6c..8c753c0c44 100644 --- a/.github/workflows/publish-golem-scala.yaml +++ b/.github/workflows/publish-golem-scala.yaml @@ -7,7 +7,7 @@ on: env: JDK_JAVA_OPTIONS: -XX:+PrintCommandLineFlags - WASM_RQUICKJS_VERSION: "0.5.5" + WASM_RQUICKJS_VERSION: "0.5.6" WASI_SDK_VERSION: 25 WASI_SDK_PATH: /opt/wasi-sdk diff --git a/.github/workflows/publish-golem-ts.yaml b/.github/workflows/publish-golem-ts.yaml index 2b4817e972..817c12b369 100644 --- a/.github/workflows/publish-golem-ts.yaml +++ b/.github/workflows/publish-golem-ts.yaml @@ -16,7 +16,7 @@ concurrency: env: CARGO_TERM_COLOR: always - WASM_RQUICKJS_VERSION: "0.5.5" + WASM_RQUICKJS_VERSION: "0.5.6" WASI_SDK_VERSION: "25" WASI_SDK_PATH: /opt/wasi-sdk diff --git a/.github/workflows/skill-harness.yaml b/.github/workflows/skill-harness.yaml index cfe59d629d..ec842325d1 100644 --- a/.github/workflows/skill-harness.yaml +++ b/.github/workflows/skill-harness.yaml @@ -17,7 +17,7 @@ permissions: env: BUILD_TARGET: "x86_64-unknown-linux-gnu" - WASM_RQUICKJS_VERSION: "0.5.5" + WASM_RQUICKJS_VERSION: "0.5.6" WASI_SDK_VERSION: "25" WASI_SDK_PATH: /opt/wasi-sdk diff --git a/Cargo.lock b/Cargo.lock index 6984441223..f5dcd34ebc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -12628,9 +12628,9 @@ dependencies = [ [[package]] name = "wasm-rquickjs" -version = "0.5.5" +version = "0.5.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f9c7b004fe9dff02a9cf8d7f7893c9a778228e2e3a9951c980cc0c803487a9a8" +checksum = "12182612a8e529893ef170c9ea7281a046006a1be9e6c922b9bed9d2c735ce80" dependencies = [ "anyhow", "bytes", diff --git a/Cargo.toml b/Cargo.toml index dc1ded16aa..bfb4f6cfc9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -356,7 +356,7 @@ warp = "0.3.7" wasip2 = "1.0.2" wasm-encoder = "0.251.0" wasm-metadata = { version = "0.251.0", features = ["oci"] } -wasm-rquickjs = { version = "0.5.5", features = ["optimize", "use-golem-wasmtime"] } +wasm-rquickjs = { version = "0.5.6", features = ["optimize", "use-golem-wasmtime"] } wasm-wave = "0.251" wasmparser = "0.251.0" wasmprinter = "0.251.0" diff --git a/cli/golem-cli/src/app/build/command.rs b/cli/golem-cli/src/app/build/command.rs index 50ab0c4bc6..4b42fb4286 100644 --- a/cli/golem-cli/src/app/build/command.rs +++ b/cli/golem-cli/src/app/build/command.rs @@ -60,7 +60,7 @@ pub async fn execute_build_command( execute_inject_to_prebuilt_quick_js(ctx, &base_build_dir, command) } app_raw::BuildCommand::PreinitializeJs(command) => { - execute_preinitialize_js(ctx, &base_build_dir, command).await + execute_preinitialize_js(ctx, component_name, &base_build_dir, command).await } } } @@ -114,6 +114,7 @@ fn execute_inject_to_prebuilt_quick_js( async fn execute_preinitialize_js( ctx: &BuildContext<'_>, + component_name: &ComponentName, base_build_dir: &Path, command: &PreinitializeJs, ) -> anyhow::Result<()> { @@ -145,6 +146,7 @@ async fn execute_preinitialize_js( .with_context(|| format!("Failed to read JS module from {js_module}"))?; fs::create_dir_all(fs::parent_or_err(output.as_std_path())?)?; let result = golem_common::javascript::preinitialize_js( + component_name, &input, &output, source, diff --git a/golem-common/src/javascript.rs b/golem-common/src/javascript.rs index 75aa6509fb..8ed8c04995 100644 --- a/golem-common/src/javascript.rs +++ b/golem-common/src/javascript.rs @@ -1,8 +1,9 @@ // Copyright 2024-2026 Golem Cloud // Licensed under the Golem Source License v1.1 (http://license.golem.cloud/LICENSE). +use crate::model::component::ComponentName; use crate::wasmtime_config::create_wasmtime_config_without_fs_cache; -use anyhow::Context; +use anyhow::{Context, anyhow}; use camino::Utf8Path; use std::collections::BTreeMap; use std::io::ErrorKind; @@ -14,7 +15,14 @@ pub const JS_COMPILATION_CACHE_MAX_BYTES: u64 = 512 * 1024 * 1024; /// Preinitializes the current JavaScript source in an unchanged guest template. /// Only compiled native code is cached; each call initializes a fresh instance. +/// +/// A failure names the component. When the initializer traps, the error keeps the guest's +/// diagnostics, such as a JavaScript exception and its stack, and the trap cause, such as +/// the qualified import that module-level code called. It drops the Wasm backtrace of the +/// guest runtime and the `unreachable` trap that follows a reported guest failure. Other +/// optimizer failures keep their original error. pub async fn preinitialize_js( + component_name: &ComponentName, input: &Utf8Path, output: &Utf8Path, source: String, @@ -72,7 +80,42 @@ pub async fn preinitialize_js( tracing::warn!(error = %error, "Failed to trim JavaScript compilation cache"); } } - result + result.map_err(|error| preinitialization_error(component_name, error)) +} + +fn preinitialization_error(component_name: &ComponentName, error: anyhow::Error) -> anyhow::Error { + let failed = format!("Failed to pre-initialize component {component_name}"); + if error.downcast_ref::().is_none() { + return error.context(failed); + } + + // The optimizer adds the guest's stderr tail as the outermost context. + let guest_stderr = error + .chain() + .next() + .map(ToString::to_string) + .filter(|diagnostic| diagnostic.starts_with("Guest stderr")) + .map(|diagnostic| { + diagnostic + .split_once('\n') + .map_or("", |(_, stderr)| stderr) + .trim() + .to_string() + }) + .filter(|diagnostic| !diagnostic.is_empty()); + + let root_cause = error.root_cause(); + let reported_abort = guest_stderr.is_some() + && matches!( + root_cause.downcast_ref::(), + Some(wasmtime::Trap::UnreachableCodeReached) + ); + + match (guest_stderr, reported_abort) { + (Some(guest_stderr), true) => anyhow!("{failed}:\n{guest_stderr}"), + (Some(guest_stderr), false) => anyhow!("{failed}: {root_cause}\n{guest_stderr}"), + (None, _) => anyhow!("{failed}: {root_cause}"), + } } fn trim_compilation_cache(directory: &Path) -> anyhow::Result<()> { diff --git a/golem-js-guest-tests/README.md b/golem-js-guest-tests/README.md index 72d6bd7f7b..cf9c676364 100644 --- a/golem-js-guest-tests/README.md +++ b/golem-js-guest-tests/README.md @@ -90,8 +90,17 @@ eviction. Parallel builds can temporarily exceed the limit. The cache lives at `/js-compilation-cache`; deleting it only makes the next build cold. The output Wasm is standalone and does not need this cache or external JS. +The failure tests preinitialize small JavaScript sources, written inline in the Rust +tests, in the TypeScript guest. They check the error that `golem build` prints when +preinitialization fails: a module-level host call names the component and the +qualified import, also after the guest wrote to stderr; a module-level throw keeps its +JavaScript message and stack; and a corrupt input keeps the optimizer's original +error. A top-level `await` of a host call fails with `Host error: WouldBlock` and does +not name the import yet; its test records that behavior until module evaluation runs +on the async executor. + The tests do not call the agent's `get` method or cover worker execution, awaited -I/O or source-map diagnostic accuracy. Source maps accompany the build outputs for +I/O that completes or source-map diagnostic accuracy. Source maps accompany the build outputs for inspection; dependency paths omit pnpm's virtual-store prefix and use forward slashes. The separate CLI and worker-executor suites retain their existing coverage. diff --git a/golem-js-guest-tests/tests/js_guest.rs b/golem-js-guest-tests/tests/js_guest.rs index 5c2ea72be6..f0f074cb86 100644 --- a/golem-js-guest-tests/tests/js_guest.rs +++ b/golem-js-guest-tests/tests/js_guest.rs @@ -5,6 +5,7 @@ use anyhow::{Context, ensure}; use camino::{Utf8Path, Utf8PathBuf}; use golem_common::javascript::{JS_COMPILATION_CACHE_MAX_BYTES, preinitialize_js}; use golem_common::model::agent::extraction::extract_agent_type_schemas; +use golem_common::model::component::ComponentName; use std::path::{Path, PathBuf}; use test_r::{test, timeout}; @@ -46,6 +47,7 @@ async fn smoke_test(guest: &Path, bundle: &Path, expected: &str) -> anyhow::Resu ); assert_ne!(quick_agents[0].description, expected); golem_common::javascript::preinitialize_js( + &ComponentName("js-guest:smoke".to_string()), Utf8Path::from_path(guest).context("Non-UTF-8 guest path")?, Utf8Path::from_path(&initialized).context("Non-UTF-8 temporary path")?, source, @@ -61,6 +63,164 @@ async fn smoke_test(guest: &Path, bundle: &Path, expected: &str) -> anyhow::Resu Ok(()) } +/// Preinitializes `source` in the TypeScript guest and returns the reported failure. +async fn preinitialization_failure(component: &str, source: &str) -> anyhow::Result { + let root = Path::new(env!("CARGO_MANIFEST_DIR")).join(".."); + let guest = root.join("sdks/ts/packages/golem-ts-sdk/wasm/agent_guest.wasm"); + ensure!( + guest.is_file(), + "Missing JS guest {}; build its SDK agent template first", + guest.display() + ); + let directory = tempfile::tempdir()?; + let initialized = directory.path().join("initialized.wasm"); + let error = preinitialize_js( + &ComponentName(component.to_string()), + Utf8Path::from_path(&guest).context("Non-UTF-8 guest path")?, + Utf8Path::from_path(&initialized).context("Non-UTF-8 temporary path")?, + source.to_string(), + None, + ) + .await + .expect_err("preinitialization must fail"); + ensure!( + !initialized.exists(), + "a failed preinitialization wrote output" + ); + Ok(format!("{error:#}")) +} + +#[test] +#[timeout("120s")] +async fn module_level_host_call_names_import_and_component() -> anyhow::Result<()> { + let text = preinitialization_failure( + "js-guest:config-read", + r#"import { getConfigValue } from 'golem:agent/host@2.0.0'; +const metadata = { doc: undefined, aliases: [], examples: [], deprecated: undefined, role: undefined }; +const graph = { typeNodes: [{ body: { tag: 'bool-type' }, metadata }], defs: [], root: 0 }; +export default getConfigValue(['greeting'], graph);"#, + ) + .await?; + assert_eq!( + text, + "Failed to pre-initialize component js-guest:config-read: wizer pre-initialization \ + called unknown import `golem:agent/host@2.0.0#get-config-value` — this import is not \ + available during pre-initialization" + ); + Ok(()) +} + +#[test] +#[timeout("120s")] +async fn module_level_throw_keeps_javascript_message_and_stack() -> anyhow::Result<()> { + let text = preinitialization_failure( + "js-guest:throw", + "function loadSettings() { throw new TypeError('settings are missing'); }\n\ + export default loadSettings();", + ) + .await?; + assert!( + text.starts_with("Failed to pre-initialize component js-guest:throw:\n"), + "{text}" + ); + assert!( + text.contains("JavaScript error: TypeError: settings are missing\n"), + "{text}" + ); + assert!(text.contains("at loadSettings (user:"), "{text}"); + for noise in [ + "wasm backtrace", + "agent_guest.wasm!", + "unreachable", + "panicked at", + "RUST_BACKTRACE", + ] { + assert!(!text.contains(noise), "{noise:?} in:\n{text}"); + } + Ok(()) +} + +#[test] +#[timeout("120s")] +async fn guest_stderr_before_host_call_keeps_cause_and_stderr() -> anyhow::Result<()> { + let text = preinitialization_failure( + "js-guest:stderr-host-call", + r#"import { getConfigValue } from 'golem:agent/host@2.0.0'; +process.stderr.write('raw stderr line\n'); +const metadata = { doc: undefined, aliases: [], examples: [], deprecated: undefined, role: undefined }; +const graph = { typeNodes: [{ body: { tag: 'bool-type' }, metadata }], defs: [], root: 0 }; +export default getConfigValue(['greeting'], graph);"#, + ) + .await?; + assert_eq!( + text, + "Failed to pre-initialize component js-guest:stderr-host-call: wizer pre-initialization \ + called unknown import `golem:agent/host@2.0.0#get-config-value` — this import is not \ + available during pre-initialization\nraw stderr line" + ); + Ok(()) +} + +/// A top-level `await` of host work cannot settle during preinitialization. The guest +/// reports `WouldBlock` without the import, as for a promise that never settles. +#[test] +#[timeout("120s")] +async fn module_level_await_fails_without_import_name() -> anyhow::Result<()> { + let text = preinitialization_failure( + "js-guest:await-async-import", + r#"import { beginCustomDurableInvocationAsync } from 'golem:durability/durability@1.6.0'; +const metadata = { doc: undefined, aliases: [], examples: [], deprecated: undefined, role: undefined }; +const graph = { typeNodes: [{ body: { tag: 'bool-type' }, metadata }], defs: [], root: 0 }; +const value = { valueNodes: [{ tag: 'bool-value', val: true }], root: 0 }; +export default await beginCustomDurableInvocationAsync('f', { graph, value }, { tag: 'read-local' });"#, + ) + .await?; + assert_eq!( + text, + "Failed to pre-initialize component js-guest:await-async-import:\n\ + Failed to finish module initialization:\n\ + Host error: WouldBlock" + ); + Ok(()) +} + +#[test] +#[timeout("120s")] +async fn non_trap_failure_keeps_original_error() -> anyhow::Result<()> { + let directory = tempfile::tempdir()?; + let input = Utf8Path::from_path(directory.path()) + .context("Non-UTF-8 temporary path")? + .join("corrupt.wasm"); + let output = input.with_file_name("initialized.wasm"); + std::fs::write(&input, b"\0asm corrupt component")?; + let sources = [String::new()]; + let original = wasm_rquickjs::optimize_component_with_options( + &input, + &output, + "wizer-initialize", + &wasm_rquickjs::OptimizeOptions { + sources: Some(&sources), + ..Default::default() + }, + ) + .await + .expect_err("a corrupt component must fail"); + let error = preinitialize_js( + &ComponentName("js-guest:corrupt".to_string()), + &input, + &output, + String::new(), + None, + ) + .await + .expect_err("a corrupt component must fail"); + assert_eq!( + format!("{error:#}"), + format!("Failed to pre-initialize component js-guest:corrupt: {original:#}") + ); + Ok(()) +} + fn bundle(sdk: &str) -> anyhow::Result { let directory = std::env::var_os("GOLEM_JS_GUEST_BUNDLE_DIR").context( "GOLEM_JS_GUEST_BUNDLE_DIR is unset; run cargo make js-guest-tests to prepare and test fresh bundles", @@ -100,6 +260,7 @@ async fn typescript_compilation_cache() -> anyhow::Result<()> { root.join("sdks/ts/packages/golem-ts-sdk/wasm/agent_guest.wasm"), ) .unwrap(); + let cache_test = ComponentName("js-guest:cache".to_string()); let source = std::fs::read_to_string(bundle("ts")?)?; let edited = source.replace("TS initialized value:", "Edited TS initialized value:"); assert_ne!( @@ -115,19 +276,31 @@ async fn typescript_compilation_cache() -> anyhow::Result<()> { let edited_cached = dir.join("edited-cached.wasm"); let edited_uncached = dir.join("edited-uncached.wasm"); - let stats = preinitialize_js(&guest, &cold, source.clone(), Some(cache.as_std_path())) - .await? - .compilation_cache - .unwrap(); + let stats = preinitialize_js( + &cache_test, + &guest, + &cold, + source.clone(), + Some(cache.as_std_path()), + ) + .await? + .compilation_cache + .unwrap(); assert!(stats.misses > 0); - let stats = preinitialize_js(&guest, &warm, source.clone(), Some(cache.as_std_path())) - .await? - .compilation_cache - .unwrap(); + let stats = preinitialize_js( + &cache_test, + &guest, + &warm, + source.clone(), + Some(cache.as_std_path()), + ) + .await? + .compilation_cache + .unwrap(); assert!(stats.hits > 0); assert_eq!(stats.misses, 0); assert!( - preinitialize_js(&guest, &uncached, source.clone(), None) + preinitialize_js(&cache_test, &guest, &uncached, source.clone(), None) .await? .compilation_cache .is_none() @@ -140,6 +313,7 @@ async fn typescript_compilation_cache() -> anyhow::Result<()> { let fallback = dir.join("fallback.wasm"); assert!( preinitialize_js( + &cache_test, &guest, &fallback, source.clone(), @@ -152,6 +326,7 @@ async fn typescript_compilation_cache() -> anyhow::Result<()> { assert_eq!(std::fs::read(&fallback)?, std::fs::read(&uncached)?); let stats = preinitialize_js( + &cache_test, &guest, &edited_cached, edited.clone(), @@ -165,7 +340,7 @@ async fn typescript_compilation_cache() -> anyhow::Result<()> { "a JS edit must reuse compiled template code" ); assert_eq!(stats.misses, 0); - preinitialize_js(&guest, &edited_uncached, edited.clone(), None).await?; + preinitialize_js(&cache_test, &guest, &edited_uncached, edited.clone(), None).await?; assert_eq!( std::fs::read(&edited_cached)?, std::fs::read(&edited_uncached)? @@ -180,12 +355,19 @@ async fn typescript_compilation_cache() -> anyhow::Result<()> { let parallel_b = dir.join("parallel-b.wasm"); let (a, b) = tokio::join!( preinitialize_js( + &cache_test, &guest, ¶llel_a, source.clone(), Some(cache.as_std_path()) ), - preinitialize_js(&guest, ¶llel_b, edited, Some(cache.as_std_path())), + preinitialize_js( + &cache_test, + &guest, + ¶llel_b, + edited, + Some(cache.as_std_path()) + ), ); a?; b?; @@ -226,10 +408,16 @@ async fn typescript_compilation_cache() -> anyhow::Result<()> { } } let recovered = dir.join("recovered.wasm"); - let stats = preinitialize_js(&guest, &recovered, source, Some(cache.as_std_path())) - .await? - .compilation_cache - .unwrap(); + let stats = preinitialize_js( + &cache_test, + &guest, + &recovered, + source, + Some(cache.as_std_path()), + ) + .await? + .compilation_cache + .unwrap(); assert!(stats.misses > 0); assert_eq!(std::fs::read(&recovered)?, std::fs::read(&uncached)?); Ok(()) @@ -246,6 +434,7 @@ async fn compilation_cache_evicts_cold_entries_together() -> anyhow::Result<()> root.join("sdks/ts/packages/golem-ts-sdk/wasm/agent_guest.wasm"), ) .unwrap(); + let cache_test = ComponentName("js-guest:cache".to_string()); let source = std::fs::read_to_string(bundle("ts")?)?; let directory = tempfile::tempdir()?; let directory = Utf8Path::from_path(directory.path()).unwrap(); @@ -266,7 +455,14 @@ async fn compilation_cache_evicts_cold_entries_together() -> anyhow::Result<()> for name in ["entry.wip-123", ".cleanup.wip-123", "maintenance.lock"] { std::fs::write(cache.join(name), "in progress")?; } - preinitialize_js(&guest, &output, source, Some(cache.as_std_path())).await?; + preinitialize_js( + &cache_test, + &guest, + &output, + source, + Some(cache.as_std_path()), + ) + .await?; for name in [ "hot", "hot.stats", diff --git a/sdks/effect/golem-types/wasm-rquickjs-import-abort.d.ts b/sdks/effect/golem-types/wasm-rquickjs-import-abort.d.ts new file mode 100644 index 0000000000..b2d011c2c5 --- /dev/null +++ b/sdks/effect/golem-types/wasm-rquickjs-import-abort.d.ts @@ -0,0 +1,25 @@ +/** + * The abort signal of generated async imports. + * + * `withSignal(signal, fn)` makes `signal` current while `fn` runs synchronously. Every async + * import that is called meanwhile takes the signal with it. When the signal aborts before the + * import finishes, the import's promise rejects with `signal.reason` and the runtime drops the + * import, which cancels its component-model subtask: the host stops the call or learns that its + * caller stopped waiting. A signal that is already aborted rejects without starting the import. + * Imports that `fn` starts after an `await` do not take the signal. + */ +declare module 'wasm-rquickjs:import-abort' { + /** The part of `AbortSignal` the runtime uses. */ + export interface ImportSignal { + readonly aborted: boolean; + readonly reason: unknown; + addEventListener(type: 'abort', listener: () => void, options?: { once?: boolean }): void; + removeEventListener(type: 'abort', listener: () => void): void; + } + + /** + * Calls `fn` with `signal` current, then restores the previous signal. `undefined` and `null` + * clear the current signal. + */ + export function withSignal(signal: ImportSignal | undefined | null, fn: () => T): T; +} diff --git a/sdks/scala/wit/dts/wasm-rquickjs-import-abort.d.ts b/sdks/scala/wit/dts/wasm-rquickjs-import-abort.d.ts new file mode 100644 index 0000000000..b2d011c2c5 --- /dev/null +++ b/sdks/scala/wit/dts/wasm-rquickjs-import-abort.d.ts @@ -0,0 +1,25 @@ +/** + * The abort signal of generated async imports. + * + * `withSignal(signal, fn)` makes `signal` current while `fn` runs synchronously. Every async + * import that is called meanwhile takes the signal with it. When the signal aborts before the + * import finishes, the import's promise rejects with `signal.reason` and the runtime drops the + * import, which cancels its component-model subtask: the host stops the call or learns that its + * caller stopped waiting. A signal that is already aborted rejects without starting the import. + * Imports that `fn` starts after an `await` do not take the signal. + */ +declare module 'wasm-rquickjs:import-abort' { + /** The part of `AbortSignal` the runtime uses. */ + export interface ImportSignal { + readonly aborted: boolean; + readonly reason: unknown; + addEventListener(type: 'abort', listener: () => void, options?: { once?: boolean }): void; + removeEventListener(type: 'abort', listener: () => void): void; + } + + /** + * Calls `fn` with `signal` current, then restores the previous signal. `undefined` and `null` + * clear the current signal. + */ + export function withSignal(signal: ImportSignal | undefined | null, fn: () => T): T; +} diff --git a/sdks/ts/packages/golem-ts-sdk/types/wasm-rquickjs-import-abort.d.ts b/sdks/ts/packages/golem-ts-sdk/types/wasm-rquickjs-import-abort.d.ts new file mode 100644 index 0000000000..b2d011c2c5 --- /dev/null +++ b/sdks/ts/packages/golem-ts-sdk/types/wasm-rquickjs-import-abort.d.ts @@ -0,0 +1,25 @@ +/** + * The abort signal of generated async imports. + * + * `withSignal(signal, fn)` makes `signal` current while `fn` runs synchronously. Every async + * import that is called meanwhile takes the signal with it. When the signal aborts before the + * import finishes, the import's promise rejects with `signal.reason` and the runtime drops the + * import, which cancels its component-model subtask: the host stops the call or learns that its + * caller stopped waiting. A signal that is already aborted rejects without starting the import. + * Imports that `fn` starts after an `await` do not take the signal. + */ +declare module 'wasm-rquickjs:import-abort' { + /** The part of `AbortSignal` the runtime uses. */ + export interface ImportSignal { + readonly aborted: boolean; + readonly reason: unknown; + addEventListener(type: 'abort', listener: () => void, options?: { once?: boolean }): void; + removeEventListener(type: 'abort', listener: () => void): void; + } + + /** + * Calls `fn` with `signal` current, then restores the previous signal. `undefined` and `null` + * clear the current signal. + */ + export function withSignal(signal: ImportSignal | undefined | null, fn: () => T): T; +}