Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply only allowed attributes to generated functions #3784

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions crates/backend/src/codegen.rs
Expand Up @@ -678,6 +678,14 @@ impl TryToTokens for ast::Export {
};
let nargs = self.function.arguments.len() as u32;
let attrs = &self.function.rust_attrs;
let safe_attrs = attrs
.iter()
.filter(|a| {
let path = a.path();
path.is_ident("cfg") || path.is_ident("cfg_attr")
})
.cloned()
.collect::<Vec<_>>();

let start_check = if self.start {
quote! { const _ASSERT: fn() = || -> #projection::Abi { loop {} }; }
Expand All @@ -688,7 +696,7 @@ impl TryToTokens for ast::Export {
(quote! {
#[automatically_derived]
const _: () = {
#(#attrs)*
#(#safe_attrs)*
#[cfg_attr(
all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))),
export_name = #export_name,
Expand Down Expand Up @@ -745,7 +753,7 @@ impl TryToTokens for ast::Export {
#describe_args
#describe_ret
},
attrs: attrs.clone(),
attrs: safe_attrs,
daxpedda marked this conversation as resolved.
Show resolved Hide resolved
wasm_bindgen: &self.wasm_bindgen,
}
.to_tokens(into);
Expand Down
9 changes: 9 additions & 0 deletions tests/wasm/attributes.js
@@ -0,0 +1,9 @@
const wasm = require("wasm-bindgen-test.js");
const assert = require("assert");

exports.js_works = () => {
assert.strictEqual(wasm.valid_export(), true);
assert.strictEqual(wasm.invalid_export, undefined);
assert.strictEqual(wasm.valid_attr_export(), true);
assert.strictEqual(wasm.invalid_attr_export, undefined);
};
36 changes: 36 additions & 0 deletions tests/wasm/attributes.rs
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add another test with custom proc-macros, to make sure they are skipped for the generated functions?
Feel free to add any reasonable dev-dependencies to make it happen, e.g. tracing.

@@ -0,0 +1,36 @@
use wasm_bindgen::prelude::*;
use wasm_bindgen_test::*;

#[wasm_bindgen(module = "tests/wasm/attributes.js")]
extern "C" {
fn js_works();
}

#[wasm_bindgen_test]
fn works() {
js_works();
}

#[wasm_bindgen]
#[cfg(target_arch = "wasm32")]
pub fn valid_export() -> bool {
true
}

#[wasm_bindgen]
#[cfg(not(target_arch = "wasm32"))]
pub fn invalid_export() -> bool {
false
}

#[wasm_bindgen]
#[cfg_attr(not(target_arch = "wasm32"), cfg(feature = "missing-feature"))]
pub fn valid_attr_export() -> bool {
true
}

#[wasm_bindgen]
#[cfg_attr(target_arch = "wasm32", cfg(feature = "missing-feature"))]
pub fn invalid_attr_export() -> bool {
false
}
1 change: 1 addition & 0 deletions tests/wasm/main.rs
Expand Up @@ -16,6 +16,7 @@ use wasm_bindgen::prelude::*;

pub mod api;
pub mod arg_names;
pub mod attributes;
pub mod bigint;
pub mod char;
pub mod classes;
Expand Down