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

Remove span in generated field getter #3725

Merged
merged 4 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
9 changes: 8 additions & 1 deletion crates/backend/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,14 @@ impl ToTokens for ast::StructField {
};
let maybe_assert_copy = respan(maybe_assert_copy, ty);

let mut val = quote_spanned!(self.rust_name.span()=> (*js).borrow().#rust_name);
// Split this out so that it isn't affected by `quote_spanned!`.
//
// If we don't do this, it might end up being unable to reference `js`
// properly because it doesn't have the same span.
//
// See https://github.com/rustwasm/wasm-bindgen/pull/3725.
let js_token = quote! { js };
let mut val = quote_spanned!(self.rust_name.span()=> (*#js_token).borrow().#rust_name);
if let Some(span) = self.getter_with_clone {
val = quote_spanned!(span=> <#ty as Clone>::clone(&#val) );
}
Expand Down
7 changes: 7 additions & 0 deletions tests/wasm/classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,10 @@ exports.js_test_inspectable_classes_can_override_generated_methods = () => {
assert.strictEqual(overridden_inspectable.toString(), 'string was overwritten');
overridden_inspectable.free();
};

exports.js_test_class_defined_in_macro = () => {
const macroClass = new wasm.InsideMacro();
assert.strictEqual(macroClass.a, 3);
macroClass.a = 5;
assert.strictEqual(macroClass.a, 5);
};
25 changes: 25 additions & 0 deletions tests/wasm/classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ extern "C" {
fn js_test_option_classes();
fn js_test_inspectable_classes();
fn js_test_inspectable_classes_can_override_generated_methods();
fn js_test_class_defined_in_macro();
}

#[wasm_bindgen_test]
Expand Down Expand Up @@ -608,3 +609,27 @@ impl OverriddenInspectable {
String::from("string was overwritten")
}
}

macro_rules! make_struct {
($field:ident) => {
#[wasm_bindgen]
pub struct InsideMacro {
pub $field: u32,
}
};
}

make_struct!(a);

#[wasm_bindgen]
impl InsideMacro {
#[wasm_bindgen(constructor)]
pub fn new() -> Self {
Self { a: 3 }
}
}

#[wasm_bindgen_test]
fn class_defined_in_macro() {
js_test_class_defined_in_macro();
}