Skip to content

Commit

Permalink
Add initWithoutStart to the JS shim
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Sep 3, 2022
1 parent f75a3f8 commit 9ff706f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
43 changes: 36 additions & 7 deletions crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ impl<'a> Context<'a> {
OutputMode::Web => {
self.imports_post.push_str("let wasm;\n");
init = self.gen_init(needs_manual_start, Some(&mut imports))?;
footer.push_str("export { initSync }\n");
footer.push_str("export { initSync, initWithoutStart }\n");
footer.push_str("export default init;");
}
}
Expand Down Expand Up @@ -607,6 +607,7 @@ impl<'a> Context<'a> {
// Also in (at least) the NoModules, the `init()` method is renamed to `wasm_bindgen()`.
let setup_function_declaration;
let mut sync_init_function = String::new();
let mut without_start_init_function = String::new();
let declare_or_export;
if self.config.mode.no_modules() {
declare_or_export = "declare";
Expand All @@ -632,6 +633,23 @@ impl<'a> Context<'a> {
memory_param = memory_param
));

without_start_init_function.push_str(&format!(
"\
/**\n\
* If `module_or_path` is {{RequestInfo}} or {{URL}}, makes a request and\n\
* for everything else, calls `WebAssembly.instantiate` directly.\n\
*\n\
* @param {{InitInput | Promise<InitInput>}} module_or_path\n\
{}\
*\n\
* @returns {{Promise<InitOutput>}}\n\
*/\n\
export function initWithoutStart \
(module_or_path{}: InitInput | Promise<InitInput>{}): Promise<InitOutput>;\n\n\
",
memory_doc, arg_optional, memory_param,
));

setup_function_declaration = "export default function init";
}
Ok(format!(
Expand All @@ -644,7 +662,8 @@ impl<'a> Context<'a> {
{sync_init_function}\
/**\n\
* If `module_or_path` is {{RequestInfo}} or {{URL}}, makes a request and\n\
* for everything else, calls `WebAssembly.instantiate` directly.\n\
* for everything else, calls `WebAssembly.instantiate` directly and runs\n\
* the start function.\n\
*\n\
* @param {{InitInput | Promise<InitInput>}} module_or_path\n\
{}\
Expand Down Expand Up @@ -831,11 +850,13 @@ impl<'a> Context<'a> {
{init_memory}
}}
function finalizeInit(instance, module) {{
function finalizeInit(instance, module, start) {{
wasm = instance.exports;
init.__wbindgen_wasm_module = module;
{init_memviews}
{start}
if (start) {{
{start}
}}
return wasm;
}}
Expand All @@ -850,10 +871,10 @@ impl<'a> Context<'a> {
const instance = new WebAssembly.Instance(module, imports);
return finalizeInit(instance, module);
return finalizeInit(instance, module, true);
}}
async function init(input{init_memory_arg}) {{
async function initInternal(input{init_memory_arg}, start) {{
{default_module_path}
const imports = getImports();
Expand All @@ -865,7 +886,15 @@ impl<'a> Context<'a> {
const {{ instance, module }} = await load(await input, imports);
return finalizeInit(instance, module);
return finalizeInit(instance, module, start);
}}
async function initWithoutStart(input{init_memory_arg}) {{
return initInternal(input{init_memory_arg}, false);
}}
async function init(input{init_memory_arg}) {{
return initInternal(input{init_memory_arg}, true);
}}
",
init_memory_arg = init_memory_arg,
Expand Down
8 changes: 4 additions & 4 deletions crates/cli/tests/wasm-bindgen/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ fn default_module_path_target_web() {
let contents = fs::read_to_string(out_dir.join("default_module_path_target_web.js")).unwrap();
assert!(contents.contains(
"\
async function init(input) {
async function initInternal(input, start) {
if (typeof input === 'undefined') {
input = new URL('default_module_path_target_web_bg.wasm', import.meta.url);
}",
Expand All @@ -260,7 +260,7 @@ fn default_module_path_target_no_modules() {
fs::read_to_string(out_dir.join("default_module_path_target_no_modules.js")).unwrap();
assert!(contents.contains(
"\
async function init(input) {
async function initInternal(input, start) {
if (typeof input === 'undefined') {
let src;
if (typeof document === 'undefined') {
Expand All @@ -287,7 +287,7 @@ fn omit_default_module_path_target_web() {
fs::read_to_string(out_dir.join("omit_default_module_path_target_web.js")).unwrap();
assert!(contents.contains(
"\
async function init(input) {
async function initInternal(input, start) {
const imports = getImports();",
));
Expand All @@ -307,7 +307,7 @@ fn omit_default_module_path_target_no_modules() {
fs::read_to_string(out_dir.join("omit_default_module_path_target_no_modules.js")).unwrap();
assert!(contents.contains(
"\
async function init(input) {
async function initInternal(input, start) {
const imports = getImports();",
));
Expand Down

0 comments on commit 9ff706f

Please sign in to comment.