Skip to content

Commit 8f06eeb

Browse files
committed
Add #[wasm_bindgen(assert_no_shim)] on imported functions for testing
This should not be used outside of wasm-bindgen's test suite.
1 parent b8efb55 commit 8f06eeb

File tree

7 files changed

+33
-1
lines changed

7 files changed

+33
-1
lines changed

crates/backend/src/ast.rs

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ pub struct ImportFunction {
122122
pub catch: bool,
123123
pub variadic: bool,
124124
pub structural: bool,
125+
pub assert_no_shim: bool,
125126
pub kind: ImportFunctionKind,
126127
pub shim: Ident,
127128
pub doc_comment: Option<String>,

crates/backend/src/encode.rs

+1
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ fn shared_import_function<'a>(
272272
shim: intern.intern(&i.shim),
273273
catch: i.catch,
274274
method,
275+
assert_no_shim: i.assert_no_shim,
275276
structural: i.structural,
276277
function: shared_function(&i.function, intern),
277278
variadic: i.variadic,

crates/cli-support/src/js/mod.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -1839,7 +1839,8 @@ impl<'a> Context<'a> {
18391839
for (id, import) in sorted_iter(&aux.import_map) {
18401840
let variadic = aux.imports_with_variadic.contains(&id);
18411841
let catch = aux.imports_with_catch.contains(&id);
1842-
self.generate_import(*id, import, bindings, variadic, catch)
1842+
let assert_no_shim = aux.imports_with_assert_no_shim.contains(&id);
1843+
self.generate_import(*id, import, bindings, variadic, catch, assert_no_shim)
18431844
.with_context(|_| {
18441845
format!("failed to generate bindings for import `{:?}`", import,)
18451846
})?;
@@ -1978,6 +1979,7 @@ impl<'a> Context<'a> {
19781979
bindings: &NonstandardWebidlSection,
19791980
variadic: bool,
19801981
catch: bool,
1982+
assert_no_shim: bool,
19811983
) -> Result<(), Error> {
19821984
let binding = &bindings.imports[&id];
19831985
let webidl = bindings
@@ -1996,6 +1998,13 @@ impl<'a> Context<'a> {
19961998
)
19971999
}
19982000
_ => {
2001+
if assert_no_shim {
2002+
panic!(
2003+
"imported function was annotated with `#[wasm_bindgen(assert_no_shim)]` \
2004+
but we need to generate a JS shim for it"
2005+
);
2006+
}
2007+
19992008
let mut builder = binding::Builder::new(self);
20002009
builder.catch(catch)?;
20012010
let js = builder.process(

crates/cli-support/src/webidl/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ pub struct WasmBindgenAux {
163163
/// Small bits of metadata about imports.
164164
pub imports_with_catch: HashSet<ImportId>,
165165
pub imports_with_variadic: HashSet<ImportId>,
166+
pub imports_with_assert_no_shim: HashSet<ImportId>,
166167

167168
/// Auxiliary information to go into JS/TypeScript bindings describing the
168169
/// exported enums from Rust.
@@ -793,6 +794,7 @@ impl<'a> Context<'a> {
793794
method,
794795
structural,
795796
function,
797+
assert_no_shim,
796798
} = function;
797799
let (import_id, _id) = match self.function_imports.get(*shim) {
798800
Some(pair) => *pair,
@@ -811,6 +813,9 @@ impl<'a> Context<'a> {
811813
if *catch {
812814
self.aux.imports_with_catch.insert(import_id);
813815
}
816+
if *assert_no_shim {
817+
self.aux.imports_with_assert_no_shim.insert(import_id);
818+
}
814819

815820
// Perform two functions here. First we're saving off our WebIDL
816821
// bindings signature, indicating what we think our import is going to

crates/macro-support/src/parser.rs

+5
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ macro_rules! attrgen {
5252
(typescript_custom_section, TypescriptCustomSection(Span)),
5353
(start, Start(Span)),
5454
(skip, Skip(Span)),
55+
56+
// For testing purposes only.
57+
(assert_no_shim, AssertNoShim(Span)),
5558
}
5659
};
5760
}
@@ -496,8 +499,10 @@ impl<'a> ConvertToAst<(BindgenAttrs, &'a ast::ImportModule)> for syn::ForeignIte
496499
return Err(Diagnostic::span_error(*span, msg));
497500
}
498501
}
502+
let assert_no_shim = opts.assert_no_shim().is_some();
499503
let ret = ast::ImportKind::Function(ast::ImportFunction {
500504
function: wasm,
505+
assert_no_shim,
501506
kind,
502507
js_ret,
503508
catch,

crates/shared/src/lib.rs

100644100755
+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ macro_rules! shared_api {
4444
shim: &'a str,
4545
catch: bool,
4646
variadic: bool,
47+
assert_no_shim: bool,
4748
method: Option<MethodData<'a>>,
4849
structural: bool,
4950
function: Function<'a>,

tests/wasm/no_shims.rs

+10
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,30 @@ use wasm_bindgen_test::*;
3535
};
3636
")]
3737
extern "C" {
38+
#[wasm_bindgen(assert_no_shim)]
3839
fn trivial();
3940

41+
#[wasm_bindgen(assert_no_shim)]
4042
fn incoming_i32() -> i32;
43+
#[wasm_bindgen(assert_no_shim)]
4144
fn incoming_f32() -> f32;
45+
#[wasm_bindgen(assert_no_shim)]
4246
fn incoming_f64() -> f64;
4347

48+
#[wasm_bindgen(assert_no_shim)]
4449
fn outgoing_i32(x: i32);
50+
#[wasm_bindgen(assert_no_shim)]
4551
fn outgoing_f32(y: f32);
52+
#[wasm_bindgen(assert_no_shim)]
4653
fn outgoing_f64(z: f64);
4754

55+
#[wasm_bindgen(assert_no_shim)]
4856
fn many(x: i32, y: f32, z: f64) -> i32;
4957

5058
// Note that this should only skip the JS shim if we have anyref support
5159
// enabled.
60+
//
61+
// #[wasm_bindgen(assert_no_shim)]
5262
fn works_when_anyref_support_is_enabled(v: JsValue) -> JsValue;
5363
}
5464

0 commit comments

Comments
 (0)