Skip to content

Commit

Permalink
fix: handle raw identifier in field (#3621)
Browse files Browse the repository at this point in the history
  • Loading branch information
Solo-steven committed Sep 20, 2023
1 parent 2b7ab44 commit 9ae2a60
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
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

0 comments on commit 9ae2a60

Please sign in to comment.