Skip to content

Implement shim_format #3168

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions crates/cli-support/src/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ fn opt_i64() -> Descriptor {
Descriptor::Option(Box::new(Descriptor::I64))
}

fn opt_u32() -> Descriptor {
Descriptor::Option(Box::new(Descriptor::U32))
}

fn slice(contents: Descriptor) -> Descriptor {
Descriptor::Ref(Box::new(Descriptor::Slice(Box::new(contents))))
}
Expand Down Expand Up @@ -276,5 +280,11 @@ intrinsics! {
#[symbol = "__wbindgen_init_externref_table"]
#[signature = fn() -> Unit]
InitExternrefTable,
#[symbol = "__wbindgen_shim_format_variant"]
#[signature = fn() -> opt_u32()]
ShimFormatVariant,
#[symbol = "__wbindgen_shim_format_string"]
#[signature = fn() -> String]
ShimFormatString,
}
}
11 changes: 11 additions & 0 deletions crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3551,6 +3551,17 @@ impl<'a> Context<'a> {
}
base
}

Intrinsic::ShimFormatVariant => match &self.config.mode {
OutputMode::Web => 0.to_string(),
OutputMode::NoModules { .. } => 1.to_string(),
_ => String::from("null"),
},

Intrinsic::ShimFormatString => match &self.config.mode {
OutputMode::NoModules { global } => format!("'{global}'"),
_ => String::from("undefined"),
},
};
Ok(expr)
}
Expand Down
37 changes: 37 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,9 @@ externs! {
fn __wbindgen_memory() -> u32;
fn __wbindgen_module() -> u32;
fn __wbindgen_function_table() -> u32;

fn __wbindgen_shim_format_variant() -> WasmOption<u32>;
fn __wbindgen_shim_format_string() -> WasmSlice;
}
}

Expand Down Expand Up @@ -1367,6 +1370,40 @@ pub fn function_table() -> JsValue {
unsafe { JsValue::_new(__wbindgen_function_table()) }
}

/// Represents the format of the shim.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum ShimFormat {
/// The shim is an ES module.
EsModule,
/// The shim doesn't use any module system, and instead exposes a global variable
/// with its API.
///
/// It has to be imported as a script to work properly, as the global variable is
/// created by simply declaring a top-level variable, which only works in scripts.
NoModules {
/// The name of the global variable the shim exposes.
///
/// By default, this is `"wasm_bindgen"`.
global_name: String,
},
}

/// Returns the format of the generated JS shim.
///
/// This will currently return `None` on every target except `web` and `no-modules`.
pub fn shim_format() -> Option<ShimFormat> {
match unsafe { __wbindgen_shim_format_variant() } {
WasmOption::Some(0) => Some(ShimFormat::EsModule),
WasmOption::Some(1) => {
let global_name = unsafe { String::from_abi(__wbindgen_shim_format_string()) };
Some(ShimFormat::NoModules { global_name })
}
WasmOption::Some(_) => unreachable!("unexpected variant identifier"),
WasmOption::None => None,
}
}

#[doc(hidden)]
pub mod __rt {
use crate::JsValue;
Expand Down