mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-04-25 22:22:12 +00:00
Support #[wasm_bindgen(setter, js_name = ...)]
Previously we'd require the explicit `js_name` to *also* start with `set_`, but when explicitly specified it shouldn't be mangled at all! Closes #584
This commit is contained in:
parent
534cceafc8
commit
75f005be23
@ -168,6 +168,7 @@ pub struct ImportEnum {
|
|||||||
pub struct Function {
|
pub struct Function {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub name_span: Span,
|
pub name_span: Span,
|
||||||
|
pub renamed_via_js_name: bool,
|
||||||
pub arguments: Vec<syn::ArgCaptured>,
|
pub arguments: Vec<syn::ArgCaptured>,
|
||||||
pub ret: Option<syn::Type>,
|
pub ret: Option<syn::Type>,
|
||||||
pub rust_attrs: Vec<syn::Attribute>,
|
pub rust_attrs: Vec<syn::Attribute>,
|
||||||
@ -393,6 +394,14 @@ impl ImportFunction {
|
|||||||
/// for a setter in javascript (in this case `xxx`, so you can write `obj.xxx = val`)
|
/// for a setter in javascript (in this case `xxx`, so you can write `obj.xxx = val`)
|
||||||
fn infer_setter_property(&self) -> Result<String, Diagnostic> {
|
fn infer_setter_property(&self) -> Result<String, Diagnostic> {
|
||||||
let name = self.function.name.to_string();
|
let name = self.function.name.to_string();
|
||||||
|
|
||||||
|
// if `#[wasm_bindgen(js_name = "...")]` is used then that explicitly
|
||||||
|
// because it was hand-written anyway.
|
||||||
|
if self.function.renamed_via_js_name {
|
||||||
|
return Ok(name)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherwise we infer names based on the Rust function name.
|
||||||
if !name.starts_with("set_") {
|
if !name.starts_with("set_") {
|
||||||
bail_span!(
|
bail_span!(
|
||||||
syn::token::Pub(self.function.name_span),
|
syn::token::Pub(self.function.name_span),
|
||||||
|
@ -689,6 +689,7 @@ fn function_from_decl(
|
|||||||
ast::Function {
|
ast::Function {
|
||||||
name: js_name.map(|s| s.0.to_string()).unwrap_or(decl_name.to_string()),
|
name: js_name.map(|s| s.0.to_string()).unwrap_or(decl_name.to_string()),
|
||||||
name_span: js_name.map(|s| s.1).unwrap_or(decl_name.span()),
|
name_span: js_name.map(|s| s.1).unwrap_or(decl_name.span()),
|
||||||
|
renamed_via_js_name: js_name.is_some(),
|
||||||
arguments,
|
arguments,
|
||||||
ret,
|
ret,
|
||||||
rust_vis: vis,
|
rust_vis: vis,
|
||||||
|
@ -4,11 +4,5 @@ error: setters must start with `set_`, found: a
|
|||||||
10 | fn a(this: &A, b: i32);
|
10 | fn a(this: &A, b: i32);
|
||||||
| ^
|
| ^
|
||||||
|
|
||||||
error: setters must start with `set_`, found: x
|
error: aborting due to previous error
|
||||||
--> $DIR/invalid-setter.rs:15:46
|
|
||||||
|
|
|
||||||
15 | #[wasm_bindgen(setter, method, js_name = x)]
|
|
||||||
| ^
|
|
||||||
|
|
||||||
error: aborting due to 2 previous errors
|
|
||||||
|
|
||||||
|
@ -4,7 +4,7 @@ use std::ptr;
|
|||||||
use backend;
|
use backend;
|
||||||
use backend::util::{ident_ty, leading_colon_path_ty, raw_ident, rust_ident};
|
use backend::util::{ident_ty, leading_colon_path_ty, raw_ident, rust_ident};
|
||||||
use heck::{CamelCase, ShoutySnakeCase, SnakeCase};
|
use heck::{CamelCase, ShoutySnakeCase, SnakeCase};
|
||||||
use proc_macro2::Ident;
|
use proc_macro2::{Ident, Span};
|
||||||
use syn;
|
use syn;
|
||||||
use weedle;
|
use weedle;
|
||||||
use weedle::attribute::{ExtendedAttributeList, ExtendedAttribute};
|
use weedle::attribute::{ExtendedAttributeList, ExtendedAttribute};
|
||||||
@ -304,6 +304,8 @@ impl<'src> FirstPassRecord<'src> {
|
|||||||
Some(backend::ast::ImportFunction {
|
Some(backend::ast::ImportFunction {
|
||||||
function: backend::ast::Function {
|
function: backend::ast::Function {
|
||||||
name: js_name.to_string(),
|
name: js_name.to_string(),
|
||||||
|
name_span: Span::call_site(),
|
||||||
|
renamed_via_js_name: false,
|
||||||
arguments,
|
arguments,
|
||||||
ret: ret.clone(),
|
ret: ret.clone(),
|
||||||
rust_attrs: vec![],
|
rust_attrs: vec![],
|
||||||
|
@ -53,8 +53,12 @@ extern {
|
|||||||
fn new() -> RenameProperties;
|
fn new() -> RenameProperties;
|
||||||
#[wasm_bindgen(getter = a, method)]
|
#[wasm_bindgen(getter = a, method)]
|
||||||
fn test(this: &RenameProperties) -> i32;
|
fn test(this: &RenameProperties) -> i32;
|
||||||
|
#[wasm_bindgen(getter, method, js_name = a)]
|
||||||
|
fn test2(this: &RenameProperties) -> i32;
|
||||||
#[wasm_bindgen(setter = a, method)]
|
#[wasm_bindgen(setter = a, method)]
|
||||||
fn another(this: &RenameProperties, a: i32);
|
fn another(this: &RenameProperties, a: i32);
|
||||||
|
#[wasm_bindgen(setter, method, js_name = a)]
|
||||||
|
fn another2(this: &RenameProperties, a: i32);
|
||||||
|
|
||||||
/// dox
|
/// dox
|
||||||
pub type AssertImportDenyDocsWorks;
|
pub type AssertImportDenyDocsWorks;
|
||||||
@ -154,6 +158,8 @@ fn rename_setter_getter() {
|
|||||||
assert_eq!(a.test(), 1);
|
assert_eq!(a.test(), 1);
|
||||||
a.another(2);
|
a.another(2);
|
||||||
assert_eq!(a.test(), 2);
|
assert_eq!(a.test(), 2);
|
||||||
|
a.another2(3);
|
||||||
|
assert_eq!(a.test2(), 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// dox
|
/// dox
|
||||||
|
Loading…
x
Reference in New Issue
Block a user