mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-04-25 14:12:13 +00:00
Add #[wasm_bindgen(assert_no_shim)]
on imported functions for testing
This should not be used outside of wasm-bindgen's test suite.
This commit is contained in:
parent
2d0866da9a
commit
bce892b625
@ -122,6 +122,7 @@ pub struct ImportFunction {
|
|||||||
pub catch: bool,
|
pub catch: bool,
|
||||||
pub variadic: bool,
|
pub variadic: bool,
|
||||||
pub structural: bool,
|
pub structural: bool,
|
||||||
|
pub assert_no_shim: bool,
|
||||||
pub kind: ImportFunctionKind,
|
pub kind: ImportFunctionKind,
|
||||||
pub shim: Ident,
|
pub shim: Ident,
|
||||||
pub doc_comment: Option<String>,
|
pub doc_comment: Option<String>,
|
||||||
|
@ -272,6 +272,7 @@ fn shared_import_function<'a>(
|
|||||||
shim: intern.intern(&i.shim),
|
shim: intern.intern(&i.shim),
|
||||||
catch: i.catch,
|
catch: i.catch,
|
||||||
method,
|
method,
|
||||||
|
assert_no_shim: i.assert_no_shim,
|
||||||
structural: i.structural,
|
structural: i.structural,
|
||||||
function: shared_function(&i.function, intern),
|
function: shared_function(&i.function, intern),
|
||||||
variadic: i.variadic,
|
variadic: i.variadic,
|
||||||
|
@ -1839,7 +1839,8 @@ impl<'a> Context<'a> {
|
|||||||
for (id, import) in sorted_iter(&aux.import_map) {
|
for (id, import) in sorted_iter(&aux.import_map) {
|
||||||
let variadic = aux.imports_with_variadic.contains(&id);
|
let variadic = aux.imports_with_variadic.contains(&id);
|
||||||
let catch = aux.imports_with_catch.contains(&id);
|
let catch = aux.imports_with_catch.contains(&id);
|
||||||
self.generate_import(*id, import, bindings, variadic, catch)
|
let assert_no_shim = aux.imports_with_assert_no_shim.contains(&id);
|
||||||
|
self.generate_import(*id, import, bindings, variadic, catch, assert_no_shim)
|
||||||
.with_context(|_| {
|
.with_context(|_| {
|
||||||
format!("failed to generate bindings for import `{:?}`", import,)
|
format!("failed to generate bindings for import `{:?}`", import,)
|
||||||
})?;
|
})?;
|
||||||
@ -1978,6 +1979,7 @@ impl<'a> Context<'a> {
|
|||||||
bindings: &NonstandardWebidlSection,
|
bindings: &NonstandardWebidlSection,
|
||||||
variadic: bool,
|
variadic: bool,
|
||||||
catch: bool,
|
catch: bool,
|
||||||
|
assert_no_shim: bool,
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
let binding = &bindings.imports[&id];
|
let binding = &bindings.imports[&id];
|
||||||
let webidl = bindings
|
let webidl = bindings
|
||||||
@ -1996,6 +1998,13 @@ impl<'a> Context<'a> {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
|
if assert_no_shim {
|
||||||
|
panic!(
|
||||||
|
"imported function was annotated with `#[wasm_bindgen(assert_no_shim)]` \
|
||||||
|
but we need to generate a JS shim for it"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
let mut builder = binding::Builder::new(self);
|
let mut builder = binding::Builder::new(self);
|
||||||
builder.catch(catch)?;
|
builder.catch(catch)?;
|
||||||
let js = builder.process(
|
let js = builder.process(
|
||||||
|
@ -163,6 +163,7 @@ pub struct WasmBindgenAux {
|
|||||||
/// Small bits of metadata about imports.
|
/// Small bits of metadata about imports.
|
||||||
pub imports_with_catch: HashSet<ImportId>,
|
pub imports_with_catch: HashSet<ImportId>,
|
||||||
pub imports_with_variadic: HashSet<ImportId>,
|
pub imports_with_variadic: HashSet<ImportId>,
|
||||||
|
pub imports_with_assert_no_shim: HashSet<ImportId>,
|
||||||
|
|
||||||
/// Auxiliary information to go into JS/TypeScript bindings describing the
|
/// Auxiliary information to go into JS/TypeScript bindings describing the
|
||||||
/// exported enums from Rust.
|
/// exported enums from Rust.
|
||||||
@ -793,6 +794,7 @@ impl<'a> Context<'a> {
|
|||||||
method,
|
method,
|
||||||
structural,
|
structural,
|
||||||
function,
|
function,
|
||||||
|
assert_no_shim,
|
||||||
} = function;
|
} = function;
|
||||||
let (import_id, _id) = match self.function_imports.get(*shim) {
|
let (import_id, _id) = match self.function_imports.get(*shim) {
|
||||||
Some(pair) => *pair,
|
Some(pair) => *pair,
|
||||||
@ -811,6 +813,9 @@ impl<'a> Context<'a> {
|
|||||||
if *catch {
|
if *catch {
|
||||||
self.aux.imports_with_catch.insert(import_id);
|
self.aux.imports_with_catch.insert(import_id);
|
||||||
}
|
}
|
||||||
|
if *assert_no_shim {
|
||||||
|
self.aux.imports_with_assert_no_shim.insert(import_id);
|
||||||
|
}
|
||||||
|
|
||||||
// Perform two functions here. First we're saving off our WebIDL
|
// Perform two functions here. First we're saving off our WebIDL
|
||||||
// bindings signature, indicating what we think our import is going to
|
// bindings signature, indicating what we think our import is going to
|
||||||
|
@ -52,6 +52,9 @@ macro_rules! attrgen {
|
|||||||
(typescript_custom_section, TypescriptCustomSection(Span)),
|
(typescript_custom_section, TypescriptCustomSection(Span)),
|
||||||
(start, Start(Span)),
|
(start, Start(Span)),
|
||||||
(skip, Skip(Span)),
|
(skip, Skip(Span)),
|
||||||
|
|
||||||
|
// For testing purposes only.
|
||||||
|
(assert_no_shim, AssertNoShim(Span)),
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -496,8 +499,10 @@ impl<'a> ConvertToAst<(BindgenAttrs, &'a ast::ImportModule)> for syn::ForeignIte
|
|||||||
return Err(Diagnostic::span_error(*span, msg));
|
return Err(Diagnostic::span_error(*span, msg));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
let assert_no_shim = opts.assert_no_shim().is_some();
|
||||||
let ret = ast::ImportKind::Function(ast::ImportFunction {
|
let ret = ast::ImportKind::Function(ast::ImportFunction {
|
||||||
function: wasm,
|
function: wasm,
|
||||||
|
assert_no_shim,
|
||||||
kind,
|
kind,
|
||||||
js_ret,
|
js_ret,
|
||||||
catch,
|
catch,
|
||||||
|
1
crates/shared/src/lib.rs
Normal file → Executable file
1
crates/shared/src/lib.rs
Normal file → Executable file
@ -44,6 +44,7 @@ macro_rules! shared_api {
|
|||||||
shim: &'a str,
|
shim: &'a str,
|
||||||
catch: bool,
|
catch: bool,
|
||||||
variadic: bool,
|
variadic: bool,
|
||||||
|
assert_no_shim: bool,
|
||||||
method: Option<MethodData<'a>>,
|
method: Option<MethodData<'a>>,
|
||||||
structural: bool,
|
structural: bool,
|
||||||
function: Function<'a>,
|
function: Function<'a>,
|
||||||
|
@ -314,6 +314,7 @@ impl<'src> FirstPassRecord<'src> {
|
|||||||
variadic,
|
variadic,
|
||||||
catch,
|
catch,
|
||||||
structural,
|
structural,
|
||||||
|
assert_no_shim: false,
|
||||||
shim: {
|
shim: {
|
||||||
let ns = match kind {
|
let ns = match kind {
|
||||||
ast::ImportFunctionKind::Normal => "",
|
ast::ImportFunctionKind::Normal => "",
|
||||||
|
@ -35,20 +35,30 @@ use wasm_bindgen_test::*;
|
|||||||
};
|
};
|
||||||
")]
|
")]
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
#[wasm_bindgen(assert_no_shim)]
|
||||||
fn trivial();
|
fn trivial();
|
||||||
|
|
||||||
|
#[wasm_bindgen(assert_no_shim)]
|
||||||
fn incoming_i32() -> i32;
|
fn incoming_i32() -> i32;
|
||||||
|
#[wasm_bindgen(assert_no_shim)]
|
||||||
fn incoming_f32() -> f32;
|
fn incoming_f32() -> f32;
|
||||||
|
#[wasm_bindgen(assert_no_shim)]
|
||||||
fn incoming_f64() -> f64;
|
fn incoming_f64() -> f64;
|
||||||
|
|
||||||
|
#[wasm_bindgen(assert_no_shim)]
|
||||||
fn outgoing_i32(x: i32);
|
fn outgoing_i32(x: i32);
|
||||||
|
#[wasm_bindgen(assert_no_shim)]
|
||||||
fn outgoing_f32(y: f32);
|
fn outgoing_f32(y: f32);
|
||||||
|
#[wasm_bindgen(assert_no_shim)]
|
||||||
fn outgoing_f64(z: f64);
|
fn outgoing_f64(z: f64);
|
||||||
|
|
||||||
|
#[wasm_bindgen(assert_no_shim)]
|
||||||
fn many(x: i32, y: f32, z: f64) -> i32;
|
fn many(x: i32, y: f32, z: f64) -> i32;
|
||||||
|
|
||||||
// Note that this should only skip the JS shim if we have anyref support
|
// Note that this should only skip the JS shim if we have anyref support
|
||||||
// enabled.
|
// enabled.
|
||||||
|
//
|
||||||
|
// #[wasm_bindgen(assert_no_shim)]
|
||||||
fn works_when_anyref_support_is_enabled(v: JsValue) -> JsValue;
|
fn works_when_anyref_support_is_enabled(v: JsValue) -> JsValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user