Skip to content

Commit 2a1c7ac

Browse files
Test different CLI flags in reference tests (#4264)
1 parent 8771907 commit 2a1c7ac

17 files changed

+610
-17
lines changed

crates/cli/tests/reference.rs

+78-17
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,24 @@
3434
//!
3535
//! Multiple dependencies can be declared in a single test file using multiple
3636
//! `DEPENDENCY:` comments.
37+
//!
38+
//! ## Custom CLI flags
39+
//!
40+
//! By default, tests will use the `bundler` target. Custom CLI flags can be
41+
//! passed to the `wasm-bindgen` CLI by declaring them in a comment at the top
42+
//! of the test file. For example:
43+
//!
44+
//! ```rust
45+
//! // FLAGS: --target=web --reference-types
46+
//! ```
47+
//!
48+
//! Multiple comments can be used to run the test multiple times with different
49+
//! flags.
50+
//!
51+
//! ```rust
52+
//! // FLAGS: --target=web
53+
//! // FLAGS: --target=nodejs
54+
//! ```
3755
3856
use anyhow::{bail, Result};
3957
use assert_cmd::prelude::*;
@@ -101,6 +119,16 @@ fn runtest(test: &Path) -> Result<()> {
101119
let root = repo_root();
102120
let root = root.display();
103121

122+
// parse target declarations
123+
let mut all_flags: Vec<_> = contents
124+
.lines()
125+
.filter_map(|l| l.strip_prefix("// FLAGS: "))
126+
.map(|l| l.trim())
127+
.collect();
128+
if all_flags.is_empty() {
129+
all_flags.push("");
130+
}
131+
104132
// parse additional dependency declarations
105133
let dependencies = contents
106134
.lines()
@@ -144,25 +172,58 @@ fn runtest(test: &Path) -> Result<()> {
144172
.join("debug")
145173
.join("reference_test.wasm");
146174

147-
let mut bindgen = Command::cargo_bin("wasm-bindgen")?;
148-
bindgen
149-
.arg("--out-dir")
150-
.arg(td.path())
151-
.arg(&wasm)
152-
.arg("--remove-producers-section");
153-
if contents.contains("// enable-externref") {
154-
bindgen.env("WASM_BINDGEN_EXTERNREF", "1");
155-
}
156-
exec(&mut bindgen)?;
175+
for (flags_index, &flags) in all_flags.iter().enumerate() {
176+
// extract the target from the flags
177+
let target = flags
178+
.split_whitespace()
179+
.find_map(|f| f.strip_prefix("--target="))
180+
.unwrap_or("bundler");
181+
182+
let out_dir = &td.path().join(target);
183+
fs::create_dir(out_dir)?;
184+
185+
let mut bindgen = Command::cargo_bin("wasm-bindgen")?;
186+
bindgen
187+
.arg("--out-dir")
188+
.arg(out_dir)
189+
.arg(&wasm)
190+
.arg("--remove-producers-section");
191+
for flag in flags.split_whitespace() {
192+
bindgen.arg(flag);
193+
}
194+
if contents.contains("// enable-externref") {
195+
bindgen.env("WASM_BINDGEN_EXTERNREF", "1");
196+
}
197+
exec(&mut bindgen)?;
157198

158-
if !contents.contains("async") {
159-
let js = fs::read_to_string(td.path().join("reference_test_bg.js"))?;
160-
assert_same(&js, &test.with_extension("js"))?;
161-
let wat = sanitize_wasm(&td.path().join("reference_test_bg.wasm"))?;
162-
assert_same(&wat, &test.with_extension("wat"))?;
199+
// suffix the file name with the target
200+
let test = if all_flags.len() > 1 {
201+
let base_file_name = format!(
202+
"{}-{}.rs",
203+
test.file_stem().unwrap().to_string_lossy(),
204+
flags_index
205+
);
206+
test.with_file_name(base_file_name)
207+
} else {
208+
test.to_owned()
209+
};
210+
211+
// bundler uses a different main JS file, because its
212+
// reference_test.js just imports the reference_test_bg.js
213+
let main_js_file = match target {
214+
"bundler" => "reference_test_bg.js",
215+
_ => "reference_test.js",
216+
};
217+
218+
if !contents.contains("async") {
219+
let js = fs::read_to_string(out_dir.join(main_js_file))?;
220+
assert_same(&js, &test.with_extension("js"))?;
221+
let wat = sanitize_wasm(&out_dir.join("reference_test_bg.wasm"))?;
222+
assert_same(&wat, &test.with_extension("wat"))?;
223+
}
224+
let d_ts = fs::read_to_string(out_dir.join("reference_test.d.ts"))?;
225+
assert_same(&d_ts, &test.with_extension("d.ts"))?;
163226
}
164-
let d_ts = fs::read_to_string(td.path().join("reference_test.d.ts"))?;
165-
assert_same(&d_ts, &test.with_extension("d.ts"))?;
166227

167228
Ok(())
168229
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/* tslint:disable */
2+
/* eslint-disable */
3+
export function add_that_might_fail(a: number, b: number): number;
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
let wasm;
2+
export function __wbg_set_wasm(val) {
3+
wasm = val;
4+
}
5+
6+
/**
7+
* @param {number} a
8+
* @param {number} b
9+
* @returns {number}
10+
*/
11+
export function add_that_might_fail(a, b) {
12+
const ret = wasm.add_that_might_fail(a, b);
13+
return ret >>> 0;
14+
}
15+
16+
export function __wbg_random_5d40be260a2cfbac() {
17+
const ret = Math.random();
18+
return ret;
19+
};
20+
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
(module $reference_test.wasm
2+
(type (;0;) (func (param i32 i32) (result i32)))
3+
(func $add_that_might_fail (;0;) (type 0) (param i32 i32) (result i32))
4+
(memory (;0;) 17)
5+
(export "memory" (memory 0))
6+
(export "add_that_might_fail" (func $add_that_might_fail))
7+
(@custom "target_features" (after code) "\04+\0amultivalue+\0fmutable-globals+\0freference-types+\08sign-ext")
8+
)
9+
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* tslint:disable */
2+
/* eslint-disable */
3+
export function add_that_might_fail(a: number, b: number): number;
4+
5+
export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
6+
7+
export interface InitOutput {
8+
readonly memory: WebAssembly.Memory;
9+
readonly add_that_might_fail: (a: number, b: number) => number;
10+
readonly __wbindgen_export_0: WebAssembly.Table;
11+
readonly __wbindgen_start: () => void;
12+
}
13+
14+
export type SyncInitInput = BufferSource | WebAssembly.Module;
15+
/**
16+
* Instantiates the given `module`, which can either be bytes or
17+
* a precompiled `WebAssembly.Module`.
18+
*
19+
* @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
20+
*
21+
* @returns {InitOutput}
22+
*/
23+
export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput;
24+
25+
/**
26+
* If `module_or_path` is {RequestInfo} or {URL}, makes a request and
27+
* for everything else, calls `WebAssembly.instantiate` directly.
28+
*
29+
* @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
30+
*
31+
* @returns {Promise<InitOutput>}
32+
*/
33+
export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;
+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
let wasm;
2+
3+
/**
4+
* @param {number} a
5+
* @param {number} b
6+
* @returns {number}
7+
*/
8+
export function add_that_might_fail(a, b) {
9+
const ret = wasm.add_that_might_fail(a, b);
10+
return ret >>> 0;
11+
}
12+
13+
async function __wbg_load(module, imports) {
14+
if (typeof Response === 'function' && module instanceof Response) {
15+
if (typeof WebAssembly.instantiateStreaming === 'function') {
16+
try {
17+
return await WebAssembly.instantiateStreaming(module, imports);
18+
19+
} catch (e) {
20+
if (module.headers.get('Content-Type') != 'application/wasm') {
21+
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);
22+
23+
} else {
24+
throw e;
25+
}
26+
}
27+
}
28+
29+
const bytes = await module.arrayBuffer();
30+
return await WebAssembly.instantiate(bytes, imports);
31+
32+
} else {
33+
const instance = await WebAssembly.instantiate(module, imports);
34+
35+
if (instance instanceof WebAssembly.Instance) {
36+
return { instance, module };
37+
38+
} else {
39+
return instance;
40+
}
41+
}
42+
}
43+
44+
function __wbg_get_imports() {
45+
const imports = {};
46+
imports.wbg = {};
47+
imports.wbg.__wbg_random_5d40be260a2cfbac = function() {
48+
const ret = Math.random();
49+
return ret;
50+
};
51+
imports.wbg.__wbindgen_init_externref_table = function() {
52+
const table = wasm.__wbindgen_export_0;
53+
const offset = table.grow(4);
54+
table.set(0, undefined);
55+
table.set(offset + 0, undefined);
56+
table.set(offset + 1, null);
57+
table.set(offset + 2, true);
58+
table.set(offset + 3, false);
59+
;
60+
};
61+
62+
return imports;
63+
}
64+
65+
function __wbg_init_memory(imports, memory) {
66+
67+
}
68+
69+
function __wbg_finalize_init(instance, module) {
70+
wasm = instance.exports;
71+
__wbg_init.__wbindgen_wasm_module = module;
72+
73+
74+
wasm.__wbindgen_start();
75+
return wasm;
76+
}
77+
78+
function initSync(module) {
79+
if (wasm !== undefined) return wasm;
80+
81+
82+
if (typeof module !== 'undefined') {
83+
if (Object.getPrototypeOf(module) === Object.prototype) {
84+
({module} = module)
85+
} else {
86+
console.warn('using deprecated parameters for `initSync()`; pass a single object instead')
87+
}
88+
}
89+
90+
const imports = __wbg_get_imports();
91+
92+
__wbg_init_memory(imports);
93+
94+
if (!(module instanceof WebAssembly.Module)) {
95+
module = new WebAssembly.Module(module);
96+
}
97+
98+
const instance = new WebAssembly.Instance(module, imports);
99+
100+
return __wbg_finalize_init(instance, module);
101+
}
102+
103+
async function __wbg_init(module_or_path) {
104+
if (wasm !== undefined) return wasm;
105+
106+
107+
if (typeof module_or_path !== 'undefined') {
108+
if (Object.getPrototypeOf(module_or_path) === Object.prototype) {
109+
({module_or_path} = module_or_path)
110+
} else {
111+
console.warn('using deprecated parameters for the initialization function; pass a single object instead')
112+
}
113+
}
114+
115+
if (typeof module_or_path === 'undefined') {
116+
module_or_path = new URL('reference_test_bg.wasm', import.meta.url);
117+
}
118+
const imports = __wbg_get_imports();
119+
120+
if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) {
121+
module_or_path = fetch(module_or_path);
122+
}
123+
124+
__wbg_init_memory(imports);
125+
126+
const { instance, module } = await __wbg_load(await module_or_path, imports);
127+
128+
return __wbg_finalize_init(instance, module);
129+
}
130+
131+
export { initSync };
132+
export default __wbg_init;
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
(module $reference_test.wasm
2+
(type (;0;) (func))
3+
(type (;1;) (func (param i32 i32) (result i32)))
4+
(import "wbg" "__wbindgen_init_externref_table" (func (;0;) (type 0)))
5+
(func $add_that_might_fail (;1;) (type 1) (param i32 i32) (result i32))
6+
(table (;0;) 128 externref)
7+
(memory (;0;) 17)
8+
(export "memory" (memory 0))
9+
(export "add_that_might_fail" (func $add_that_might_fail))
10+
(export "__wbindgen_export_0" (table 0))
11+
(export "__wbindgen_start" (func 0))
12+
(@custom "target_features" (after code) "\04+\0amultivalue+\0fmutable-globals+\0freference-types+\08sign-ext")
13+
)
14+
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
declare namespace wasm_bindgen {
2+
/* tslint:disable */
3+
/* eslint-disable */
4+
export function add_that_might_fail(a: number, b: number): number;
5+
6+
}
7+
8+
declare type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
9+
10+
declare interface InitOutput {
11+
readonly memory: WebAssembly.Memory;
12+
readonly add_that_might_fail: (a: number, b: number) => number;
13+
readonly __wbindgen_export_0: WebAssembly.Table;
14+
readonly __wbindgen_start: () => void;
15+
}
16+
17+
/**
18+
* If `module_or_path` is {RequestInfo} or {URL}, makes a request and
19+
* for everything else, calls `WebAssembly.instantiate` directly.
20+
*
21+
* @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
22+
*
23+
* @returns {Promise<InitOutput>}
24+
*/
25+
declare function wasm_bindgen (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;

0 commit comments

Comments
 (0)