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

fix: handle raw identifier in field #3621

Merged
merged 12 commits into from
Sep 20, 2023
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@

### Fixed

* Fixed `wasm_bindgen` macro to handle raw identifiers in field names.
[#3621](https://github.com/rustwasm/wasm-bindgen/pull/3621)

* Fixed bindings and comments for `Atomics.wait`.
[#3509](https://github.com/rustwasm/wasm-bindgen/pull/3509)

Expand Down
3 changes: 2 additions & 1 deletion crates/macro-support/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use backend::util::{ident_ty, ShortHash};
use backend::Diagnostic;
use proc_macro2::{Ident, Span, TokenStream, TokenTree};
use quote::ToTokens;
use syn::ext::IdentExt;
use syn::parse::{Parse, ParseStream, Result as SynResult};
use syn::spanned::Spanned;
use syn::{ItemFn, Lit, MacroDelimiter, ReturnType};
Expand Down Expand Up @@ -420,7 +421,7 @@ impl<'a> ConvertToAst<(&ast::Program, BindgenAttrs)> for &'a mut syn::ItemStruct
_ => continue,
}
let (js_field_name, member) = match &field.ident {
Some(ident) => (ident.to_string(), syn::Member::Named(ident.clone())),
Some(ident) => (ident.unraw().to_string(), syn::Member::Named(ident.clone())),
None => (i.to_string(), syn::Member::Unnamed(i.into())),
};

Expand Down
19 changes: 19 additions & 0 deletions tests/wasm/getters_and_setters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ extern "C" {
fn _12_js(rules: Rules) -> Rules;
fn _13_js(rules: Rules) -> Rules;

fn raw_identifer(rules: RulesWithRawField) -> RulesWithRawField;

fn test_getter_compute(x: GetterCompute);
fn test_setter_compute(x: SetterCompute);
fn test_statics(x: Statics);
Expand All @@ -32,6 +34,23 @@ pub struct Rules {
pub field: i32,
}

#[wasm_bindgen]
pub struct RulesWithRawField {
pub r#mod: i32,
}

#[wasm_bindgen]
impl RulesWithRawField {
#[wasm_bindgen]
pub fn get_field_value(&self) -> i32 {
self.r#mod
}
#[wasm_bindgen]
pub fn set_field_value(&mut self, value: i32) {
self.r#mod = value;
}
}

#[wasm_bindgen]
#[allow(non_snake_case)]
impl Rules {
Expand Down