From 2a1c7acc295ad77d3fd36179a34b9573f724c7a1 Mon Sep 17 00:00:00 2001 From: Michael Schmidt Date: Tue, 12 Nov 2024 21:57:51 +0100 Subject: [PATCH] Test different CLI flags in reference tests (#4264) --- crates/cli/tests/reference.rs | 95 ++++++++++++--- crates/cli/tests/reference/targets-0.d.ts | 3 + crates/cli/tests/reference/targets-0.js | 20 ++++ crates/cli/tests/reference/targets-0.wat | 9 ++ crates/cli/tests/reference/targets-1.d.ts | 33 +++++ crates/cli/tests/reference/targets-1.js | 132 ++++++++++++++++++++ crates/cli/tests/reference/targets-1.wat | 14 +++ crates/cli/tests/reference/targets-2.d.ts | 25 ++++ crates/cli/tests/reference/targets-2.js | 139 ++++++++++++++++++++++ crates/cli/tests/reference/targets-2.wat | 14 +++ crates/cli/tests/reference/targets-3.d.ts | 3 + crates/cli/tests/reference/targets-3.js | 40 +++++++ crates/cli/tests/reference/targets-3.wat | 14 +++ crates/cli/tests/reference/targets-4.d.ts | 3 + crates/cli/tests/reference/targets-4.js | 50 ++++++++ crates/cli/tests/reference/targets-4.wat | 14 +++ crates/cli/tests/reference/targets.rs | 19 +++ 17 files changed, 610 insertions(+), 17 deletions(-) create mode 100644 crates/cli/tests/reference/targets-0.d.ts create mode 100644 crates/cli/tests/reference/targets-0.js create mode 100644 crates/cli/tests/reference/targets-0.wat create mode 100644 crates/cli/tests/reference/targets-1.d.ts create mode 100644 crates/cli/tests/reference/targets-1.js create mode 100644 crates/cli/tests/reference/targets-1.wat create mode 100644 crates/cli/tests/reference/targets-2.d.ts create mode 100644 crates/cli/tests/reference/targets-2.js create mode 100644 crates/cli/tests/reference/targets-2.wat create mode 100644 crates/cli/tests/reference/targets-3.d.ts create mode 100644 crates/cli/tests/reference/targets-3.js create mode 100644 crates/cli/tests/reference/targets-3.wat create mode 100644 crates/cli/tests/reference/targets-4.d.ts create mode 100644 crates/cli/tests/reference/targets-4.js create mode 100644 crates/cli/tests/reference/targets-4.wat create mode 100644 crates/cli/tests/reference/targets.rs diff --git a/crates/cli/tests/reference.rs b/crates/cli/tests/reference.rs index cd7604062d9..190df67b541 100644 --- a/crates/cli/tests/reference.rs +++ b/crates/cli/tests/reference.rs @@ -34,6 +34,24 @@ //! //! Multiple dependencies can be declared in a single test file using multiple //! `DEPENDENCY:` comments. +//! +//! ## Custom CLI flags +//! +//! By default, tests will use the `bundler` target. Custom CLI flags can be +//! passed to the `wasm-bindgen` CLI by declaring them in a comment at the top +//! of the test file. For example: +//! +//! ```rust +//! // FLAGS: --target=web --reference-types +//! ``` +//! +//! Multiple comments can be used to run the test multiple times with different +//! flags. +//! +//! ```rust +//! // FLAGS: --target=web +//! // FLAGS: --target=nodejs +//! ``` use anyhow::{bail, Result}; use assert_cmd::prelude::*; @@ -101,6 +119,16 @@ fn runtest(test: &Path) -> Result<()> { let root = repo_root(); let root = root.display(); + // parse target declarations + let mut all_flags: Vec<_> = contents + .lines() + .filter_map(|l| l.strip_prefix("// FLAGS: ")) + .map(|l| l.trim()) + .collect(); + if all_flags.is_empty() { + all_flags.push(""); + } + // parse additional dependency declarations let dependencies = contents .lines() @@ -144,25 +172,58 @@ fn runtest(test: &Path) -> Result<()> { .join("debug") .join("reference_test.wasm"); - let mut bindgen = Command::cargo_bin("wasm-bindgen")?; - bindgen - .arg("--out-dir") - .arg(td.path()) - .arg(&wasm) - .arg("--remove-producers-section"); - if contents.contains("// enable-externref") { - bindgen.env("WASM_BINDGEN_EXTERNREF", "1"); - } - exec(&mut bindgen)?; + for (flags_index, &flags) in all_flags.iter().enumerate() { + // extract the target from the flags + let target = flags + .split_whitespace() + .find_map(|f| f.strip_prefix("--target=")) + .unwrap_or("bundler"); + + let out_dir = &td.path().join(target); + fs::create_dir(out_dir)?; + + let mut bindgen = Command::cargo_bin("wasm-bindgen")?; + bindgen + .arg("--out-dir") + .arg(out_dir) + .arg(&wasm) + .arg("--remove-producers-section"); + for flag in flags.split_whitespace() { + bindgen.arg(flag); + } + if contents.contains("// enable-externref") { + bindgen.env("WASM_BINDGEN_EXTERNREF", "1"); + } + exec(&mut bindgen)?; - if !contents.contains("async") { - let js = fs::read_to_string(td.path().join("reference_test_bg.js"))?; - assert_same(&js, &test.with_extension("js"))?; - let wat = sanitize_wasm(&td.path().join("reference_test_bg.wasm"))?; - assert_same(&wat, &test.with_extension("wat"))?; + // suffix the file name with the target + let test = if all_flags.len() > 1 { + let base_file_name = format!( + "{}-{}.rs", + test.file_stem().unwrap().to_string_lossy(), + flags_index + ); + test.with_file_name(base_file_name) + } else { + test.to_owned() + }; + + // bundler uses a different main JS file, because its + // reference_test.js just imports the reference_test_bg.js + let main_js_file = match target { + "bundler" => "reference_test_bg.js", + _ => "reference_test.js", + }; + + if !contents.contains("async") { + let js = fs::read_to_string(out_dir.join(main_js_file))?; + assert_same(&js, &test.with_extension("js"))?; + let wat = sanitize_wasm(&out_dir.join("reference_test_bg.wasm"))?; + assert_same(&wat, &test.with_extension("wat"))?; + } + let d_ts = fs::read_to_string(out_dir.join("reference_test.d.ts"))?; + assert_same(&d_ts, &test.with_extension("d.ts"))?; } - let d_ts = fs::read_to_string(td.path().join("reference_test.d.ts"))?; - assert_same(&d_ts, &test.with_extension("d.ts"))?; Ok(()) } diff --git a/crates/cli/tests/reference/targets-0.d.ts b/crates/cli/tests/reference/targets-0.d.ts new file mode 100644 index 00000000000..a01199843fc --- /dev/null +++ b/crates/cli/tests/reference/targets-0.d.ts @@ -0,0 +1,3 @@ +/* tslint:disable */ +/* eslint-disable */ +export function add_that_might_fail(a: number, b: number): number; diff --git a/crates/cli/tests/reference/targets-0.js b/crates/cli/tests/reference/targets-0.js new file mode 100644 index 00000000000..5e2b7e8959b --- /dev/null +++ b/crates/cli/tests/reference/targets-0.js @@ -0,0 +1,20 @@ +let wasm; +export function __wbg_set_wasm(val) { + wasm = val; +} + +/** + * @param {number} a + * @param {number} b + * @returns {number} + */ +export function add_that_might_fail(a, b) { + const ret = wasm.add_that_might_fail(a, b); + return ret >>> 0; +} + +export function __wbg_random_5d40be260a2cfbac() { + const ret = Math.random(); + return ret; +}; + diff --git a/crates/cli/tests/reference/targets-0.wat b/crates/cli/tests/reference/targets-0.wat new file mode 100644 index 00000000000..b8d3e4b8c61 --- /dev/null +++ b/crates/cli/tests/reference/targets-0.wat @@ -0,0 +1,9 @@ +(module $reference_test.wasm + (type (;0;) (func (param i32 i32) (result i32))) + (func $add_that_might_fail (;0;) (type 0) (param i32 i32) (result i32)) + (memory (;0;) 17) + (export "memory" (memory 0)) + (export "add_that_might_fail" (func $add_that_might_fail)) + (@custom "target_features" (after code) "\04+\0amultivalue+\0fmutable-globals+\0freference-types+\08sign-ext") +) + diff --git a/crates/cli/tests/reference/targets-1.d.ts b/crates/cli/tests/reference/targets-1.d.ts new file mode 100644 index 00000000000..67e807819b3 --- /dev/null +++ b/crates/cli/tests/reference/targets-1.d.ts @@ -0,0 +1,33 @@ +/* tslint:disable */ +/* eslint-disable */ +export function add_that_might_fail(a: number, b: number): number; + +export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module; + +export interface InitOutput { + readonly memory: WebAssembly.Memory; + readonly add_that_might_fail: (a: number, b: number) => number; + readonly __wbindgen_export_0: WebAssembly.Table; + readonly __wbindgen_start: () => void; +} + +export type SyncInitInput = BufferSource | WebAssembly.Module; +/** +* Instantiates the given `module`, which can either be bytes or +* a precompiled `WebAssembly.Module`. +* +* @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated. +* +* @returns {InitOutput} +*/ +export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput; + +/** +* If `module_or_path` is {RequestInfo} or {URL}, makes a request and +* for everything else, calls `WebAssembly.instantiate` directly. +* +* @param {{ module_or_path: InitInput | Promise }} module_or_path - Passing `InitInput` directly is deprecated. +* +* @returns {Promise} +*/ +export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise } | InitInput | Promise): Promise; diff --git a/crates/cli/tests/reference/targets-1.js b/crates/cli/tests/reference/targets-1.js new file mode 100644 index 00000000000..69dc3f36f14 --- /dev/null +++ b/crates/cli/tests/reference/targets-1.js @@ -0,0 +1,132 @@ +let wasm; + +/** + * @param {number} a + * @param {number} b + * @returns {number} + */ +export function add_that_might_fail(a, b) { + const ret = wasm.add_that_might_fail(a, b); + return ret >>> 0; +} + +async function __wbg_load(module, imports) { + if (typeof Response === 'function' && module instanceof Response) { + if (typeof WebAssembly.instantiateStreaming === 'function') { + try { + return await WebAssembly.instantiateStreaming(module, imports); + + } catch (e) { + if (module.headers.get('Content-Type') != 'application/wasm') { + console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e); + + } else { + throw e; + } + } + } + + const bytes = await module.arrayBuffer(); + return await WebAssembly.instantiate(bytes, imports); + + } else { + const instance = await WebAssembly.instantiate(module, imports); + + if (instance instanceof WebAssembly.Instance) { + return { instance, module }; + + } else { + return instance; + } + } +} + +function __wbg_get_imports() { + const imports = {}; + imports.wbg = {}; + imports.wbg.__wbg_random_5d40be260a2cfbac = function() { + const ret = Math.random(); + return ret; + }; + imports.wbg.__wbindgen_init_externref_table = function() { + const table = wasm.__wbindgen_export_0; + const offset = table.grow(4); + table.set(0, undefined); + table.set(offset + 0, undefined); + table.set(offset + 1, null); + table.set(offset + 2, true); + table.set(offset + 3, false); + ; + }; + + return imports; +} + +function __wbg_init_memory(imports, memory) { + +} + +function __wbg_finalize_init(instance, module) { + wasm = instance.exports; + __wbg_init.__wbindgen_wasm_module = module; + + + wasm.__wbindgen_start(); + return wasm; +} + +function initSync(module) { + if (wasm !== undefined) return wasm; + + + if (typeof module !== 'undefined') { + if (Object.getPrototypeOf(module) === Object.prototype) { + ({module} = module) + } else { + console.warn('using deprecated parameters for `initSync()`; pass a single object instead') + } + } + + const imports = __wbg_get_imports(); + + __wbg_init_memory(imports); + + if (!(module instanceof WebAssembly.Module)) { + module = new WebAssembly.Module(module); + } + + const instance = new WebAssembly.Instance(module, imports); + + return __wbg_finalize_init(instance, module); +} + +async function __wbg_init(module_or_path) { + if (wasm !== undefined) return wasm; + + + if (typeof module_or_path !== 'undefined') { + if (Object.getPrototypeOf(module_or_path) === Object.prototype) { + ({module_or_path} = module_or_path) + } else { + console.warn('using deprecated parameters for the initialization function; pass a single object instead') + } + } + + if (typeof module_or_path === 'undefined') { + module_or_path = new URL('reference_test_bg.wasm', import.meta.url); + } + const imports = __wbg_get_imports(); + + if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) { + module_or_path = fetch(module_or_path); + } + + __wbg_init_memory(imports); + + const { instance, module } = await __wbg_load(await module_or_path, imports); + + return __wbg_finalize_init(instance, module); +} + +export { initSync }; +export default __wbg_init; diff --git a/crates/cli/tests/reference/targets-1.wat b/crates/cli/tests/reference/targets-1.wat new file mode 100644 index 00000000000..db2cb5a6b2e --- /dev/null +++ b/crates/cli/tests/reference/targets-1.wat @@ -0,0 +1,14 @@ +(module $reference_test.wasm + (type (;0;) (func)) + (type (;1;) (func (param i32 i32) (result i32))) + (import "wbg" "__wbindgen_init_externref_table" (func (;0;) (type 0))) + (func $add_that_might_fail (;1;) (type 1) (param i32 i32) (result i32)) + (table (;0;) 128 externref) + (memory (;0;) 17) + (export "memory" (memory 0)) + (export "add_that_might_fail" (func $add_that_might_fail)) + (export "__wbindgen_export_0" (table 0)) + (export "__wbindgen_start" (func 0)) + (@custom "target_features" (after code) "\04+\0amultivalue+\0fmutable-globals+\0freference-types+\08sign-ext") +) + diff --git a/crates/cli/tests/reference/targets-2.d.ts b/crates/cli/tests/reference/targets-2.d.ts new file mode 100644 index 00000000000..9e1c41bd645 --- /dev/null +++ b/crates/cli/tests/reference/targets-2.d.ts @@ -0,0 +1,25 @@ +declare namespace wasm_bindgen { + /* tslint:disable */ + /* eslint-disable */ + export function add_that_might_fail(a: number, b: number): number; + +} + +declare type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module; + +declare interface InitOutput { + readonly memory: WebAssembly.Memory; + readonly add_that_might_fail: (a: number, b: number) => number; + readonly __wbindgen_export_0: WebAssembly.Table; + readonly __wbindgen_start: () => void; +} + +/** +* If `module_or_path` is {RequestInfo} or {URL}, makes a request and +* for everything else, calls `WebAssembly.instantiate` directly. +* +* @param {{ module_or_path: InitInput | Promise }} module_or_path - Passing `InitInput` directly is deprecated. +* +* @returns {Promise} +*/ +declare function wasm_bindgen (module_or_path?: { module_or_path: InitInput | Promise } | InitInput | Promise): Promise; diff --git a/crates/cli/tests/reference/targets-2.js b/crates/cli/tests/reference/targets-2.js new file mode 100644 index 00000000000..f1d867286d5 --- /dev/null +++ b/crates/cli/tests/reference/targets-2.js @@ -0,0 +1,139 @@ +let wasm_bindgen; +(function() { + const __exports = {}; + let script_src; + if (typeof document !== 'undefined' && document.currentScript !== null) { + script_src = new URL(document.currentScript.src, location.href).toString(); + } + let wasm = undefined; + /** + * @param {number} a + * @param {number} b + * @returns {number} + */ + __exports.add_that_might_fail = function(a, b) { + const ret = wasm.add_that_might_fail(a, b); + return ret >>> 0; + }; + + async function __wbg_load(module, imports) { + if (typeof Response === 'function' && module instanceof Response) { + if (typeof WebAssembly.instantiateStreaming === 'function') { + try { + return await WebAssembly.instantiateStreaming(module, imports); + + } catch (e) { + if (module.headers.get('Content-Type') != 'application/wasm') { + console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e); + + } else { + throw e; + } + } + } + + const bytes = await module.arrayBuffer(); + return await WebAssembly.instantiate(bytes, imports); + + } else { + const instance = await WebAssembly.instantiate(module, imports); + + if (instance instanceof WebAssembly.Instance) { + return { instance, module }; + + } else { + return instance; + } + } + } + + function __wbg_get_imports() { + const imports = {}; + imports.wbg = {}; + imports.wbg.__wbg_random_5d40be260a2cfbac = function() { + const ret = Math.random(); + return ret; + }; + imports.wbg.__wbindgen_init_externref_table = function() { + const table = wasm.__wbindgen_export_0; + const offset = table.grow(4); + table.set(0, undefined); + table.set(offset + 0, undefined); + table.set(offset + 1, null); + table.set(offset + 2, true); + table.set(offset + 3, false); + ; + }; + + return imports; + } + + function __wbg_init_memory(imports, memory) { + + } + + function __wbg_finalize_init(instance, module) { + wasm = instance.exports; + __wbg_init.__wbindgen_wasm_module = module; + + + wasm.__wbindgen_start(); + return wasm; + } + + function initSync(module) { + if (wasm !== undefined) return wasm; + + + if (typeof module !== 'undefined') { + if (Object.getPrototypeOf(module) === Object.prototype) { + ({module} = module) + } else { + console.warn('using deprecated parameters for `initSync()`; pass a single object instead') + } + } + + const imports = __wbg_get_imports(); + + __wbg_init_memory(imports); + + if (!(module instanceof WebAssembly.Module)) { + module = new WebAssembly.Module(module); + } + + const instance = new WebAssembly.Instance(module, imports); + + return __wbg_finalize_init(instance, module); + } + + async function __wbg_init(module_or_path) { + if (wasm !== undefined) return wasm; + + + if (typeof module_or_path !== 'undefined') { + if (Object.getPrototypeOf(module_or_path) === Object.prototype) { + ({module_or_path} = module_or_path) + } else { + console.warn('using deprecated parameters for the initialization function; pass a single object instead') + } + } + + if (typeof module_or_path === 'undefined' && typeof script_src !== 'undefined') { + module_or_path = script_src.replace(/\.js$/, '_bg.wasm'); + } + const imports = __wbg_get_imports(); + + if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) { + module_or_path = fetch(module_or_path); + } + + __wbg_init_memory(imports); + + const { instance, module } = await __wbg_load(await module_or_path, imports); + + return __wbg_finalize_init(instance, module); + } + + wasm_bindgen = Object.assign(__wbg_init, { initSync }, __exports); + +})(); diff --git a/crates/cli/tests/reference/targets-2.wat b/crates/cli/tests/reference/targets-2.wat new file mode 100644 index 00000000000..db2cb5a6b2e --- /dev/null +++ b/crates/cli/tests/reference/targets-2.wat @@ -0,0 +1,14 @@ +(module $reference_test.wasm + (type (;0;) (func)) + (type (;1;) (func (param i32 i32) (result i32))) + (import "wbg" "__wbindgen_init_externref_table" (func (;0;) (type 0))) + (func $add_that_might_fail (;1;) (type 1) (param i32 i32) (result i32)) + (table (;0;) 128 externref) + (memory (;0;) 17) + (export "memory" (memory 0)) + (export "add_that_might_fail" (func $add_that_might_fail)) + (export "__wbindgen_export_0" (table 0)) + (export "__wbindgen_start" (func 0)) + (@custom "target_features" (after code) "\04+\0amultivalue+\0fmutable-globals+\0freference-types+\08sign-ext") +) + diff --git a/crates/cli/tests/reference/targets-3.d.ts b/crates/cli/tests/reference/targets-3.d.ts new file mode 100644 index 00000000000..a01199843fc --- /dev/null +++ b/crates/cli/tests/reference/targets-3.d.ts @@ -0,0 +1,3 @@ +/* tslint:disable */ +/* eslint-disable */ +export function add_that_might_fail(a: number, b: number): number; diff --git a/crates/cli/tests/reference/targets-3.js b/crates/cli/tests/reference/targets-3.js new file mode 100644 index 00000000000..92291a18bb0 --- /dev/null +++ b/crates/cli/tests/reference/targets-3.js @@ -0,0 +1,40 @@ + +let imports = {}; +imports['__wbindgen_placeholder__'] = module.exports; +let wasm; +/** + * @param {number} a + * @param {number} b + * @returns {number} + */ +module.exports.add_that_might_fail = function(a, b) { + const ret = wasm.add_that_might_fail(a, b); + return ret >>> 0; +}; + +module.exports.__wbg_random_5d40be260a2cfbac = function() { + const ret = Math.random(); + return ret; +}; + +module.exports.__wbindgen_init_externref_table = function() { + const table = wasm.__wbindgen_export_0; + const offset = table.grow(4); + table.set(0, undefined); + table.set(offset + 0, undefined); + table.set(offset + 1, null); + table.set(offset + 2, true); + table.set(offset + 3, false); + ; +}; + +const path = require('path').join(__dirname, 'reference_test_bg.wasm'); +const bytes = require('fs').readFileSync(path); + +const wasmModule = new WebAssembly.Module(bytes); +const wasmInstance = new WebAssembly.Instance(wasmModule, imports); +wasm = wasmInstance.exports; +module.exports.__wasm = wasm; + +wasm.__wbindgen_start(); + diff --git a/crates/cli/tests/reference/targets-3.wat b/crates/cli/tests/reference/targets-3.wat new file mode 100644 index 00000000000..ec8996a0fd3 --- /dev/null +++ b/crates/cli/tests/reference/targets-3.wat @@ -0,0 +1,14 @@ +(module $reference_test.wasm + (type (;0;) (func)) + (type (;1;) (func (param i32 i32) (result i32))) + (import "__wbindgen_placeholder__" "__wbindgen_init_externref_table" (func (;0;) (type 0))) + (func $add_that_might_fail (;1;) (type 1) (param i32 i32) (result i32)) + (table (;0;) 128 externref) + (memory (;0;) 17) + (export "memory" (memory 0)) + (export "add_that_might_fail" (func $add_that_might_fail)) + (export "__wbindgen_export_0" (table 0)) + (export "__wbindgen_start" (func 0)) + (@custom "target_features" (after code) "\04+\0amultivalue+\0fmutable-globals+\0freference-types+\08sign-ext") +) + diff --git a/crates/cli/tests/reference/targets-4.d.ts b/crates/cli/tests/reference/targets-4.d.ts new file mode 100644 index 00000000000..a01199843fc --- /dev/null +++ b/crates/cli/tests/reference/targets-4.d.ts @@ -0,0 +1,3 @@ +/* tslint:disable */ +/* eslint-disable */ +export function add_that_might_fail(a: number, b: number): number; diff --git a/crates/cli/tests/reference/targets-4.js b/crates/cli/tests/reference/targets-4.js new file mode 100644 index 00000000000..9dd2431bb72 --- /dev/null +++ b/crates/cli/tests/reference/targets-4.js @@ -0,0 +1,50 @@ +/** + * @param {number} a + * @param {number} b + * @returns {number} + */ +export function add_that_might_fail(a, b) { + const ret = wasm.add_that_might_fail(a, b); + return ret >>> 0; +} + +const imports = { + __wbindgen_placeholder__: { + __wbg_random_5d40be260a2cfbac: function() { + const ret = Math.random(); + return ret; + }, + __wbindgen_init_externref_table: function() { + const table = wasm.__wbindgen_export_0; + const offset = table.grow(4); + table.set(0, undefined); + table.set(offset + 0, undefined); + table.set(offset + 1, null); + table.set(offset + 2, true); + table.set(offset + 3, false); + ; + }, + }, + +}; + +const wasm_url = new URL('reference_test_bg.wasm', import.meta.url); +let wasmCode = ''; +switch (wasm_url.protocol) { + case 'file:': + wasmCode = await Deno.readFile(wasm_url); + break + case 'https:': + case 'http:': + wasmCode = await (await fetch(wasm_url)).arrayBuffer(); + break + default: + throw new Error(`Unsupported protocol: ${wasm_url.protocol}`); +} + +const wasmInstance = (await WebAssembly.instantiate(wasmCode, imports)).instance; +const wasm = wasmInstance.exports; +export const __wasm = wasm; + +wasm.__wbindgen_start(); + diff --git a/crates/cli/tests/reference/targets-4.wat b/crates/cli/tests/reference/targets-4.wat new file mode 100644 index 00000000000..ec8996a0fd3 --- /dev/null +++ b/crates/cli/tests/reference/targets-4.wat @@ -0,0 +1,14 @@ +(module $reference_test.wasm + (type (;0;) (func)) + (type (;1;) (func (param i32 i32) (result i32))) + (import "__wbindgen_placeholder__" "__wbindgen_init_externref_table" (func (;0;) (type 0))) + (func $add_that_might_fail (;1;) (type 1) (param i32 i32) (result i32)) + (table (;0;) 128 externref) + (memory (;0;) 17) + (export "memory" (memory 0)) + (export "add_that_might_fail" (func $add_that_might_fail)) + (export "__wbindgen_export_0" (table 0)) + (export "__wbindgen_start" (func 0)) + (@custom "target_features" (after code) "\04+\0amultivalue+\0fmutable-globals+\0freference-types+\08sign-ext") +) + diff --git a/crates/cli/tests/reference/targets.rs b/crates/cli/tests/reference/targets.rs new file mode 100644 index 00000000000..86e22ada3b7 --- /dev/null +++ b/crates/cli/tests/reference/targets.rs @@ -0,0 +1,19 @@ +// FLAGS: --target=bundler +// FLAGS: --target=web +// FLAGS: --target=no-modules +// FLAGS: --target=nodejs +// FLAGS: --target=deno + +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +extern "C" { + #[wasm_bindgen(js_namespace = Math)] + fn random() -> f64; +} + +#[wasm_bindgen] +pub fn add_that_might_fail(a: u32, b: u32) -> u32 { + assert!(random() > 0.5); + a + b +}