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

add emscripten support #4443

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 9 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
[target.'cfg(target_arch = "wasm32")']
runner = 'cargo run -p wasm-bindgen-cli --bin wasm-bindgen-test-runner --'

[build]

[target.'cfg(all(target_arch = "wasm32", target_os = "emscripten"))']
rustflags = [
"-Cllvm-args=-enable-emscripten-cxx-exceptions=0",
"-Clink-arg=-Wno-undefined",
"-Crelocation-model=static",
]
32 changes: 16 additions & 16 deletions crates/backend/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ impl TryToTokens for ast::Program {
let prefix_json_bytes = syn::LitByteStr::new(&prefix_json_bytes, Span::call_site());

(quote! {
#[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))]
#[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none", target_os = "emscripten")))]
#[automatically_derived]
const _: () = {
use #wasm_bindgen::__rt::{flat_len, flat_byte_slices};
Expand Down Expand Up @@ -281,12 +281,12 @@ impl ToTokens for ast::Struct {
let ptr = #wasm_bindgen::convert::IntoWasmAbi::into_abi(value);

#[link(wasm_import_module = "__wbindgen_placeholder__")]
#[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))]
#[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none", target_os = "emscripten")))]
extern "C" {
fn #new_fn(ptr: u32) -> u32;
}

#[cfg(not(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none"))))]
#[cfg(not(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none", target_os = "emscripten"))))]
unsafe fn #new_fn(_: u32) -> u32 {
panic!("cannot convert to JsValue outside of the Wasm target")
}
Expand All @@ -298,7 +298,7 @@ impl ToTokens for ast::Struct {
}
}

#[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))]
#[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none", target_os = "emscripten")))]
#[automatically_derived]
const _: () = {
#wasm_bindgen::__wbindgen_coverage! {
Expand Down Expand Up @@ -388,12 +388,12 @@ impl ToTokens for ast::Struct {
let idx = #wasm_bindgen::convert::IntoWasmAbi::into_abi(&value);

#[link(wasm_import_module = "__wbindgen_placeholder__")]
#[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))]
#[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none", target_os = "emscripten")))]
extern "C" {
fn #unwrap_fn(ptr: u32) -> u32;
}

#[cfg(not(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none"))))]
#[cfg(not(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none", target_os = "emscripten"))))]
unsafe fn #unwrap_fn(_: u32) -> u32 {
panic!("cannot convert from JsValue outside of the Wasm target")
}
Expand Down Expand Up @@ -501,7 +501,7 @@ impl ToTokens for ast::StructField {
#[automatically_derived]
const _: () = {
#wasm_bindgen::__wbindgen_coverage! {
#[cfg_attr(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")), no_mangle)]
#[cfg_attr(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none", target_os = "emscripten")), no_mangle)]
#[doc(hidden)]
pub unsafe extern "C" fn #getter(js: u32)
-> #wasm_bindgen::convert::WasmRet<<#ty as #wasm_bindgen::convert::IntoWasmAbi>::Abi>
Expand Down Expand Up @@ -540,7 +540,7 @@ impl ToTokens for ast::StructField {
let (args, names) = splat(wasm_bindgen, &Ident::new("val", rust_name.span()), &abi);

(quote! {
#[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))]
#[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none", target_os = "emscripten")))]
#[automatically_derived]
const _: () = {
#wasm_bindgen::__wbindgen_coverage! {
Expand Down Expand Up @@ -847,7 +847,7 @@ impl TryToTokens for ast::Export {
#wasm_bindgen::__wbindgen_coverage! {
#(#attrs)*
#[cfg_attr(
all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none", target_os = "emscripten")),
export_name = #export_name,
)]
pub unsafe extern "C" fn #generated_name(#(#args),*) -> #wasm_bindgen::convert::WasmRet<#projection::Abi> {
Expand Down Expand Up @@ -1124,11 +1124,11 @@ impl ToTokens for ast::ImportType {
impl JsCast for #rust_name {
fn instanceof(val: &JsValue) -> bool {
#[link(wasm_import_module = "__wbindgen_placeholder__")]
#[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))]
#[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none", target_os = "emscripten")))]
extern "C" {
fn #instanceof_shim(val: u32) -> u32;
}
#[cfg(not(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none"))))]
#[cfg(not(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none", target_os = "emscripten"))))]
unsafe fn #instanceof_shim(_: u32) -> u32 {
panic!("cannot check instanceof on non-wasm targets");
}
Expand Down Expand Up @@ -1838,12 +1838,12 @@ fn static_init(wasm_bindgen: &syn::Path, ty: &syn::Type, shim_name: &Ident) -> T
};
quote! {
#[link(wasm_import_module = "__wbindgen_placeholder__")]
#[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))]
#[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none", target_os = "emscripten")))]
extern "C" {
fn #shim_name() -> #abi_ret;
}

#[cfg(not(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none"))))]
#[cfg(not(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none", target_os = "emscripten"))))]
unsafe fn #shim_name() -> #abi_ret {
panic!("cannot access imported statics on non-wasm targets")
}
Expand Down Expand Up @@ -1888,7 +1888,7 @@ impl<T: ToTokens> ToTokens for Descriptor<'_, T> {
let attrs = &self.attrs;
let wasm_bindgen = &self.wasm_bindgen;
(quote! {
#[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))]
#[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none", target_os = "emscripten")))]
#[automatically_derived]
const _: () = {
#wasm_bindgen::__wbindgen_coverage! {
Expand Down Expand Up @@ -1916,14 +1916,14 @@ fn extern_fn(
abi_ret: TokenStream,
) -> TokenStream {
quote! {
#[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))]
#[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none", target_os = "emscripten")))]
#(#attrs)*
#[link(wasm_import_module = "__wbindgen_placeholder__")]
extern "C" {
fn #import_name(#(#abi_arguments),*) -> #abi_ret;
}

#[cfg(not(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none"))))]
#[cfg(not(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none", target_os = "emscripten"))))]
unsafe fn #import_name(#(#abi_arguments),*) -> #abi_ret {
#(
drop(#abi_argument_names);
Expand Down
1 change: 1 addition & 0 deletions crates/cli-support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ version = "0.2.100"
anyhow = "1.0"
base64 = "0.22"
log = "0.4"
regex = "1"
rustc-demangle = "0.1.13"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
Expand Down
8 changes: 6 additions & 2 deletions crates/cli-support/src/js/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ impl<'a, 'b> Builder<'a, 'b> {
debug_name: &str,
ret_ty_override: &Option<String>,
ret_desc: &Option<String>,
import_deps: &mut Vec<String>,
) -> Result<JsFunction, Error> {
if self
.cx
Expand Down Expand Up @@ -194,6 +195,7 @@ impl<'a, 'b> Builder<'a, 'b> {
&instr.instr,
&mut self.log_error,
&self.constructor,
import_deps,
)?;
}

Expand Down Expand Up @@ -727,6 +729,7 @@ fn instruction(
instr: &Instruction,
log_error: &mut bool,
constructor: &Option<String>,
import_deps: &mut Vec<String>,
) -> Result<(), Error> {
fn wasm_to_string_enum(name: &str, index: &str) -> String {
// e.g. ["a","b","c"][someIndex]
Expand Down Expand Up @@ -807,7 +810,7 @@ fn instruction(
}

// Call the function through an export of the underlying module.
let call = invoc.invoke(js.cx, &args, &mut js.prelude, log_error)?;
let call = invoc.invoke(js.cx, &args, &mut js.prelude, log_error, import_deps)?;

// And then figure out how to actually handle where the call
// happens. This is pretty conditional depending on the number of
Expand Down Expand Up @@ -1637,6 +1640,7 @@ impl Invocation {
args: &[String],
prelude: &mut String,
log_error: &mut bool,
import_deps: &mut Vec<String>,
) -> Result<String, Error> {
match self {
Invocation::Core { id, .. } => {
Expand All @@ -1656,7 +1660,7 @@ impl Invocation {
if cx.import_never_log_error(import) {
*log_error = false;
}
cx.invoke_import(import, kind, args, variadic, prelude)
cx.invoke_import(import, kind, args, variadic, prelude, import_deps)
}
}
}
Expand Down
Loading
Loading