From dcebb1bf25d7767fbb03b3507e85c5b43155108f Mon Sep 17 00:00:00 2001 From: Calvin Prewitt Date: Tue, 29 Oct 2024 15:09:58 -0500 Subject: [PATCH] fixes and added default async imports and exports --- .../src/transpile_bindgen.rs | 10 ++++++--- src/cmd/opt.js | 2 +- src/cmd/transpile.js | 21 +++++++++++++++++++ src/jco.js | 6 ++++-- 4 files changed, 33 insertions(+), 6 deletions(-) diff --git a/crates/js-component-bindgen/src/transpile_bindgen.rs b/crates/js-component-bindgen/src/transpile_bindgen.rs index e5c58784f..97292f742 100644 --- a/crates/js-component-bindgen/src/transpile_bindgen.rs +++ b/crates/js-component-bindgen/src/transpile_bindgen.rs @@ -1232,9 +1232,13 @@ impl<'a> Instantiator<'a, '_> { let is_async = self .async_imports .contains(&format!("{import_name}#{func_name}")) - || self - .async_imports - .contains(&format!("{import_specifier}#{func_name}")); + || import_name + .find('@') + .map(|i| { + self.async_imports + .contains(&format!("{}#{func_name}", import_name.get(0..i).unwrap())) + }) + .unwrap_or(false); let mut resource_map = ResourceMap::new(); self.create_resource_fn_map(func, func_ty, &mut resource_map); diff --git a/src/cmd/opt.js b/src/cmd/opt.js index 5b0e954ca..8aef4e157 100644 --- a/src/cmd/opt.js +++ b/src/cmd/opt.js @@ -68,7 +68,7 @@ export async function optimizeComponent (componentBytes, opts) { } const args = opts?.optArgs ? [...opts.optArgs] : ['-Os', '--low-memory-unused', '--enable-bulk-memory']; - if (opts.asyncMode === 'asyncify') args.push('--asyncify'); + if (opts?.asyncMode === 'asyncify') args.push('--asyncify'); const optimizedCoreModules = await Promise.all(coreModules.map(async ([coreModuleStart, coreModuleEnd]) => { const optimized = wasmOpt(componentBytes.subarray(coreModuleStart, coreModuleEnd), args); diff --git a/src/cmd/transpile.js b/src/cmd/transpile.js index 3d4e1e9e6..31785da34 100644 --- a/src/cmd/transpile.js +++ b/src/cmd/transpile.js @@ -14,6 +14,22 @@ import { platform } from 'node:process'; const isWindows = platform === 'win32'; +const DEFAULT_ASYNC_IMPORTS = [ + "wasi:io/poll#poll", + "wasi:io/poll#[method]pollable.block", + "wasi:io/streams#[method]input-stream.blocking-read", + "wasi:io/streams#[method]input-stream.blocking-skip", + "wasi:io/streams#[method]output-stream.blocking-flush", + "wasi:io/streams#[method]output-stream.blocking-write-and-flush", + "wasi:io/streams#[method]output-stream.blocking-write-zeroes-and-flush", + "wasi:io/streams#[method]output-stream.blocking-splice", +]; + +const DEFAULT_ASYNC_EXPORTS = [ + "wasi:cli/run#run", + "wasi:http/incoming-handler#handle", +]; + export async function types (witPath, opts) { const files = await typesComponent(witPath, opts); await writeFiles(files, opts.quiet ? false : 'Generated Type Files'); @@ -102,6 +118,11 @@ export async function transpile (componentPath, opts, program) { if (opts.map) opts.map = Object.fromEntries(opts.map.map(mapping => mapping.split('='))); + if (opts.defaultAsyncImports) + opts.asyncImports = DEFAULT_ASYNC_IMPORTS.concat(opts.asyncImports || []); + if (opts.defaultAsyncExports) + opts.asyncExports = DEFAULT_ASYNC_EXPORTS.concat(opts.asyncExports || []); + const { files } = await transpileComponent(component, opts); await writeFiles(files, opts.quiet ? false : 'Transpiled JS Component Files'); } diff --git a/src/jco.js b/src/jco.js index d0585cfe0..da42167d2 100755 --- a/src/jco.js +++ b/src/jco.js @@ -52,8 +52,10 @@ program.command('transpile') .option('--valid-lifting-optimization', 'optimize component binary validations assuming all lifted values are valid') .addOption(new Option('--import-bindings [mode]', 'bindings mode for imports').choices(['js', 'optimized', 'hybrid', 'direct-optimized']).preset('js')) .addOption(new Option('--async-mode [mode]', 'use async imports and exports').choices(['sync', 'jspi', 'asyncify']).preset('sync')) - .option('--async-imports ', 'async component imports (examples: "wasi:io/poll@0.2.0#poll", "wasi:io/poll@0.2.0#[method]pollable.block")') - .option('--async-exports ', 'async component exports (examples: "wasi:cli/run@0.2.0#run", "handle")') + .option('--default-async-imports', 'default async component imports from WASI interfaces') + .option('--default-async-exports', 'default async component exports from WASI interfaces') + .option('--async-imports ', 'async component imports (examples: "wasi:io/poll@0.2.0#poll", "wasi:io/poll#[method]pollable.block")') + .option('--async-exports ', 'async component exports (examples: "wasi:cli/run@#run", "handle")') .option('--tracing', 'emit `tracing` calls on function entry/exit') .option('-b, --base64-cutoff ', 'set the byte size under which core Wasm binaries will be inlined as base64', myParseInt) .option('--tla-compat', 'enables compatibility for JS environments without top-level await support via an async $init promise export')