Skip to content

Commit

Permalink
fixes and added default async imports and exports
Browse files Browse the repository at this point in the history
  • Loading branch information
calvinrp committed Oct 30, 2024
1 parent fbcdb32 commit dcebb1b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
10 changes: 7 additions & 3 deletions crates/js-component-bindgen/src/transpile_bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/opt.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
21 changes: 21 additions & 0 deletions src/cmd/transpile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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');
}
Expand Down
6 changes: 4 additions & 2 deletions src/jco.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <imports...>', 'async component imports (examples: "wasi:io/[email protected]#poll", "wasi:io/[email protected]#[method]pollable.block")')
.option('--async-exports <exports...>', 'async component exports (examples: "wasi:cli/[email protected]#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 <imports...>', 'async component imports (examples: "wasi:io/[email protected]#poll", "wasi:io/poll#[method]pollable.block")')
.option('--async-exports <exports...>', 'async component exports (examples: "wasi:cli/run@#run", "handle")')
.option('--tracing', 'emit `tracing` calls on function entry/exit')
.option('-b, --base64-cutoff <bytes>', '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')
Expand Down

0 comments on commit dcebb1b

Please sign in to comment.