Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRAFT: Adding JSPI and Asyncify #518

Draft
wants to merge 35 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
66f41b5
wip
calvinrp Oct 25, 2024
0152f75
clean up
calvinrp Oct 26, 2024
d9c0796
clippy fixes
calvinrp Oct 26, 2024
4fc199d
wip on asyncify
calvinrp Oct 27, 2024
449ffef
prototyping
calvinrp Oct 28, 2024
94d160e
added asyncify logic
calvinrp Oct 28, 2024
ed0dd1a
clean up
calvinrp Oct 28, 2024
f4b37b0
fix
calvinrp Oct 29, 2024
807c47b
added jco CLI args for async mode
calvinrp Oct 29, 2024
1652b89
fix
calvinrp Oct 29, 2024
cfb8af6
updated docs
calvinrp Oct 29, 2024
11f2b0e
fixes and added default async imports and exports
calvinrp Oct 29, 2024
0c16d11
fix for the optimizeComponent byte range
calvinrp Oct 30, 2024
65c533f
added ts for async
calvinrp Nov 1, 2024
9953b63
fixed cli types subcommand
calvinrp Nov 1, 2024
d783d85
updated asyncify instatiate
calvinrp Nov 1, 2024
c9d9daf
updated docs
calvinrp Nov 1, 2024
60ce5c0
fix
calvinrp Nov 1, 2024
09f7022
fixes
calvinrp Nov 1, 2024
b75cc48
renamed if_async to maybe_async
calvinrp Nov 1, 2024
a55c0d6
added first test
calvinrp Nov 1, 2024
fc026fa
moved asyncify functions to intrinsics; added an option to still use …
calvinrp Nov 2, 2024
88fafd3
calls the async wrapper func at initialization time
calvinrp Nov 2, 2024
cfe3251
removed duplicate type def
calvinrp Nov 3, 2024
62f9896
added more transpile options
calvinrp Nov 3, 2024
3473a00
updated test
calvinrp Nov 3, 2024
ca94f4d
added jspi test
calvinrp Nov 3, 2024
1f18fcf
feature detect jspi support in test suite
calvinrp Nov 3, 2024
e209a8c
fix for test
calvinrp Nov 3, 2024
f218fef
starting to setup wasi shims for async
calvinrp Nov 4, 2024
748077e
added async WASI P2 shims
calvinrp Nov 5, 2024
5314a5e
typo fix in refactor
calvinrp Nov 5, 2024
b2ea7b7
added ability to set stdin
calvinrp Nov 5, 2024
b9a8f25
added strip-debug
calvinrp Nov 8, 2024
93df040
fixes
calvinrp Nov 11, 2024
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
35 changes: 35 additions & 0 deletions crates/js-component-bindgen-component/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ impl From<InstantiationMode> for js_component_bindgen::InstantiationMode {
}
}

impl From<StaticWasmSourceImportsMode> for js_component_bindgen::StaticWasmSourceImportsMode {
fn from(value: StaticWasmSourceImportsMode) -> Self {
match value {
StaticWasmSourceImportsMode::ProposedStandardImportSource => {
js_component_bindgen::StaticWasmSourceImportsMode::ProposedStandardImportSource
}
StaticWasmSourceImportsMode::NonStandardImport => {
js_component_bindgen::StaticWasmSourceImportsMode::NonStandardImport
}
}
}
}

impl From<BindingsMode> for js_component_bindgen::BindingsMode {
fn from(value: BindingsMode) -> Self {
match value {
Expand All @@ -53,6 +66,20 @@ impl From<BindingsMode> for js_component_bindgen::BindingsMode {
}
}

impl From<AsyncMode> for js_component_bindgen::AsyncMode {
fn from(value: AsyncMode) -> Self {
match value {
AsyncMode::Sync => js_component_bindgen::AsyncMode::Sync,
AsyncMode::Jspi(AsyncImportsExports { imports, exports }) => {
js_component_bindgen::AsyncMode::JavaScriptPromiseIntegration { imports, exports }
}
AsyncMode::Asyncify(AsyncImportsExports { imports, exports }) => {
js_component_bindgen::AsyncMode::Asyncify { imports, exports }
}
}
}
}

struct JsComponentBindgenComponent;

export!(JsComponentBindgenComponent);
Expand All @@ -64,6 +91,9 @@ impl Guest for JsComponentBindgenComponent {
name: options.name,
no_typescript: options.no_typescript.unwrap_or(false),
instantiation: options.instantiation.map(Into::into),
cache_wasm_compile: options.cache_wasm_compile.unwrap_or(false),
static_wasm_source_imports: options.static_wasm_source_imports.map(Into::into),
esm_imports: options.esm_imports.unwrap_or(false),
map: options.map.map(|map| map.into_iter().collect()),
no_nodejs_compat: options.no_nodejs_compat.unwrap_or(false),
base64_cutoff: options.base64_cutoff.unwrap_or(5000) as usize,
Expand All @@ -75,6 +105,7 @@ impl Guest for JsComponentBindgenComponent {
no_namespaced_exports: options.no_namespaced_exports.unwrap_or(false),
multi_memory: options.multi_memory.unwrap_or(false),
import_bindings: options.import_bindings.map(Into::into),
async_mode: options.async_mode.map(Into::into),
};

let js_component_bindgen::Transpiled {
Expand Down Expand Up @@ -152,6 +183,9 @@ impl Guest for JsComponentBindgenComponent {
no_typescript: false,
no_nodejs_compat: false,
instantiation: opts.instantiation.map(Into::into),
cache_wasm_compile: false,
static_wasm_source_imports: None,
esm_imports: opts.esm_imports.unwrap_or(false),
map: opts.map.map(|map| map.into_iter().collect()),
tla_compat: opts.tla_compat.unwrap_or(false),
valid_lifting_optimization: false,
Expand All @@ -160,6 +194,7 @@ impl Guest for JsComponentBindgenComponent {
no_namespaced_exports: false,
multi_memory: false,
import_bindings: None,
async_mode: opts.async_mode.map(Into::into),
};

let files = generate_types(name, resolve, world, opts).map_err(|e| e.to_string())?;
Expand Down
41 changes: 41 additions & 0 deletions crates/js-component-bindgen-component/wit/js-component-bindgen.wit
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ world js-component-bindgen {
sync,
}

variant static-wasm-source-imports-mode {
proposed-standard-import-source,
non-standard-import,
}

variant bindings-mode {
js,
hybrid,
Expand All @@ -28,6 +33,18 @@ world js-component-bindgen {
/// of the direct importable native ESM output.
instantiation: option<instantiation-mode>,

/// If `instantiation` is set, on the first `instantiate` call the compiled
/// Wasm modules are cached for subsequent `instantiate` calls.
cache-wasm-compile: option<bool>,

/// Static import of Wasm module with the proposed standard source phase
/// imports or use the non-standard import syntax.
static-wasm-source-imports: option<static-wasm-source-imports-mode>,

/// If `instantiation` is set, instead of providing an import object, use
/// ESM imports.
esm-imports: option<bool>,

/// Import bindings generation mode
import-bindings: option<bindings-mode>,

Expand Down Expand Up @@ -62,6 +79,24 @@ world js-component-bindgen {
/// Whether to output core Wasm utilizing multi-memory or to polyfill
/// this handling.
multi-memory: option<bool>,

/// Configure whether to use `async` imports or exports with
/// JavaScript Promise Integration (JSPI) or Asyncify.
async-mode: option<async-mode>,
}

record async-imports-exports {
imports: list<string>,
exports: list<string>,
}
Comment on lines +88 to +91
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since components can contain multiple sub-components, this is currently joining imports and exports across all sub-components.

If we only consider the outer component import and exports, then we should restrict to that, but we can't do that either since internal components must be async if the outer component is!

Therefore I think we should probably restrict this to outer component imports only

And we should make exports all async as soon as we go into asyncify mode. This also will then help avoid the easy footgun of inadvertantly not making an export async, and then finding it calls an async import down the line which would trap.


variant async-mode {
/// default to sync mode
sync,
/// use JavaScript Promise Integration (JSPI)
jspi(async-imports-exports),
/// use Asyncify
asyncify(async-imports-exports),
}

variant wit {
Expand All @@ -88,9 +123,15 @@ world js-component-bindgen {
%world: option<string>,
tla-compat: option<bool>,
instantiation: option<instantiation-mode>,
/// If `instantiation` is set, instead of providing an import object, use
/// ESM imports.
esm-imports: option<bool>,
map: option<maps>,
/// Features that should be enabled as part of feature gating
features: option<enabled-feature-set>,
/// Configure whether to use `async` imports or exports with
/// JavaScript Promise Integration (JSPI) or Asyncify.
async-mode: option<async-mode>,
}

enum export-type {
Expand Down
18 changes: 15 additions & 3 deletions crates/js-component-bindgen/src/function_bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ pub struct FunctionBindgen<'a> {
pub callee: &'a str,
pub callee_resource_dynamic: bool,
pub resolve: &'a Resolve,
pub is_async: bool,
}

impl FunctionBindgen<'_> {
Expand Down Expand Up @@ -1048,7 +1049,13 @@ impl Bindgen for FunctionBindgen<'_> {
Instruction::CallWasm { sig, .. } => {
let sig_results_length = sig.results.len();
self.bind_results(sig_results_length, results);
uwriteln!(self.src, "{}({});", self.callee, operands.join(", "));
let maybe_async_await = if self.is_async { "await " } else { "" };
uwriteln!(
self.src,
"{maybe_async_await}{}({});",
self.callee,
operands.join(", ")
);

if let Some(prefix) = self.tracing_prefix {
let to_result_string = self.intrinsic(Intrinsic::ToResultString);
Expand All @@ -1066,15 +1073,20 @@ impl Bindgen for FunctionBindgen<'_> {

Instruction::CallInterface { func } => {
let results_length = func.results.len();
let maybe_async_await = if self.is_async { "await " } else { "" };
let call = if self.callee_resource_dynamic {
format!(
"{}.{}({})",
"{maybe_async_await}{}.{}({})",
operands[0],
self.callee,
operands[1..].join(", ")
)
} else {
format!("{}({})", self.callee, operands.join(", "))
format!(
"{maybe_async_await}{}({})",
self.callee,
operands.join(", ")
)
};
if self.err == ErrHandling::ResultCatchHandler {
// result<_, string> allows JS error coercion only, while
Expand Down
Loading
Loading