mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-04-27 22:52:15 +00:00
Merge pull request #1410 from alexlapa/add-wasm-bindgen-skip-attr
Add wasm_bindgen(skip) attribute
This commit is contained in:
commit
7ee4906661
@ -51,6 +51,7 @@ macro_rules! attrgen {
|
|||||||
(variadic, Variadic(Span)),
|
(variadic, Variadic(Span)),
|
||||||
(typescript_custom_section, TypescriptCustomSection(Span)),
|
(typescript_custom_section, TypescriptCustomSection(Span)),
|
||||||
(start, Start(Span)),
|
(start, Start(Span)),
|
||||||
|
(skip, Skip(Span)),
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -295,7 +296,7 @@ trait ConvertToAst<Ctx> {
|
|||||||
impl<'a> ConvertToAst<BindgenAttrs> for &'a mut syn::ItemStruct {
|
impl<'a> ConvertToAst<BindgenAttrs> for &'a mut syn::ItemStruct {
|
||||||
type Target = ast::Struct;
|
type Target = ast::Struct;
|
||||||
|
|
||||||
fn convert(self, opts: BindgenAttrs) -> Result<Self::Target, Diagnostic> {
|
fn convert(self, attrs: BindgenAttrs) -> Result<Self::Target, Diagnostic> {
|
||||||
if self.generics.params.len() > 0 {
|
if self.generics.params.len() > 0 {
|
||||||
bail_span!(
|
bail_span!(
|
||||||
self.generics,
|
self.generics,
|
||||||
@ -304,7 +305,7 @@ impl<'a> ConvertToAst<BindgenAttrs> for &'a mut syn::ItemStruct {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
let mut fields = Vec::new();
|
let mut fields = Vec::new();
|
||||||
let js_name = opts
|
let js_name = attrs
|
||||||
.js_name()
|
.js_name()
|
||||||
.map(|s| s.0.to_string())
|
.map(|s| s.0.to_string())
|
||||||
.unwrap_or(self.ident.to_string());
|
.unwrap_or(self.ident.to_string());
|
||||||
@ -318,26 +319,33 @@ impl<'a> ConvertToAst<BindgenAttrs> for &'a mut syn::ItemStruct {
|
|||||||
Some(n) => n,
|
Some(n) => n,
|
||||||
None => continue,
|
None => continue,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let attrs = BindgenAttrs::find(&mut field.attrs)?;
|
||||||
|
assert_not_variadic(&attrs)?;
|
||||||
|
if attrs.skip().is_some() {
|
||||||
|
attrs.check_used()?;
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
let comments = extract_doc_comments(&field.attrs);
|
||||||
let name_str = name.to_string();
|
let name_str = name.to_string();
|
||||||
let getter = shared::struct_field_get(&js_name, &name_str);
|
let getter = shared::struct_field_get(&js_name, &name_str);
|
||||||
let setter = shared::struct_field_set(&js_name, &name_str);
|
let setter = shared::struct_field_set(&js_name, &name_str);
|
||||||
let opts = BindgenAttrs::find(&mut field.attrs)?;
|
|
||||||
assert_not_variadic(&opts)?;
|
|
||||||
let comments = extract_doc_comments(&field.attrs);
|
|
||||||
fields.push(ast::StructField {
|
fields.push(ast::StructField {
|
||||||
name: name.clone(),
|
name: name.clone(),
|
||||||
struct_name: self.ident.clone(),
|
struct_name: self.ident.clone(),
|
||||||
readonly: opts.readonly().is_some(),
|
readonly: attrs.readonly().is_some(),
|
||||||
ty: field.ty.clone(),
|
ty: field.ty.clone(),
|
||||||
getter: Ident::new(&getter, Span::call_site()),
|
getter: Ident::new(&getter, Span::call_site()),
|
||||||
setter: Ident::new(&setter, Span::call_site()),
|
setter: Ident::new(&setter, Span::call_site()),
|
||||||
comments,
|
comments,
|
||||||
});
|
});
|
||||||
opts.check_used()?;
|
attrs.check_used()?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let comments: Vec<String> = extract_doc_comments(&self.attrs);
|
let comments: Vec<String> = extract_doc_comments(&self.attrs);
|
||||||
opts.check_used()?;
|
attrs.check_used()?;
|
||||||
Ok(ast::Struct {
|
Ok(ast::Struct {
|
||||||
rust_name: self.ident.clone(),
|
rust_name: self.ident.clone(),
|
||||||
js_name,
|
js_name,
|
||||||
|
@ -75,6 +75,7 @@
|
|||||||
- [`constructor`](./reference/attributes/on-rust-exports/constructor.md)
|
- [`constructor`](./reference/attributes/on-rust-exports/constructor.md)
|
||||||
- [`js_name = Blah`](./reference/attributes/on-rust-exports/js_name.md)
|
- [`js_name = Blah`](./reference/attributes/on-rust-exports/js_name.md)
|
||||||
- [`readonly`](./reference/attributes/on-rust-exports/readonly.md)
|
- [`readonly`](./reference/attributes/on-rust-exports/readonly.md)
|
||||||
|
- [`skip`](./reference/attributes/on-rust-exports/skip.md)
|
||||||
- [`start`](./reference/attributes/on-rust-exports/start.md)
|
- [`start`](./reference/attributes/on-rust-exports/start.md)
|
||||||
- [`typescript_custom_section`](./reference/attributes/on-rust-exports/typescript_custom_section.md)
|
- [`typescript_custom_section`](./reference/attributes/on-rust-exports/typescript_custom_section.md)
|
||||||
|
|
||||||
|
46
guide/src/reference/attributes/on-rust-exports/skip.md
Normal file
46
guide/src/reference/attributes/on-rust-exports/skip.md
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
# `skip`
|
||||||
|
|
||||||
|
When attached to a `pub` struct field this indicates that field will not be exposed to JavaScript,
|
||||||
|
and neither getter nor setter will be generated in ES6 class.
|
||||||
|
|
||||||
|
```rust
|
||||||
|
use wasm_bindgen::prelude::*;
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
pub struct Foo {
|
||||||
|
pub bar: u32,
|
||||||
|
|
||||||
|
#[wasm_bindgen(skip)]
|
||||||
|
pub baz: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
impl Foo {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Foo {
|
||||||
|
bar: 1,
|
||||||
|
baz: 2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Here the `bar` field will be both readable and writable from JS, but the
|
||||||
|
`baz` field will be `undefined` in JS.
|
||||||
|
|
||||||
|
```js
|
||||||
|
import('./pkg/').then(rust => {
|
||||||
|
let foo = rust.Foo.new();
|
||||||
|
|
||||||
|
// bar is accessible by getter
|
||||||
|
console.log(foo.bar);
|
||||||
|
// field marked with `skip` is undefined
|
||||||
|
console.log(foo.baz);
|
||||||
|
|
||||||
|
// you can shadow it
|
||||||
|
foo.baz = 45;
|
||||||
|
// so accessing by getter will return `45`
|
||||||
|
// but it won't affect real value in rust memory
|
||||||
|
console.log(foo.baz);
|
||||||
|
});
|
||||||
|
```
|
@ -108,6 +108,8 @@ exports.js_public_fields = () => {
|
|||||||
assert.strictEqual(a.d, 0);
|
assert.strictEqual(a.d, 0);
|
||||||
a.d = 3.3;
|
a.d = 3.3;
|
||||||
assert.strictEqual(a.d, 3);
|
assert.strictEqual(a.d, 3);
|
||||||
|
|
||||||
|
assert.strictEqual(a.skipped, undefined);
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.js_using_self = () => {
|
exports.js_using_self = () => {
|
||||||
|
@ -274,6 +274,8 @@ pub struct PublicFields {
|
|||||||
pub b: f32,
|
pub b: f32,
|
||||||
pub c: f64,
|
pub c: f64,
|
||||||
pub d: i32,
|
pub d: i32,
|
||||||
|
#[wasm_bindgen(skip)]
|
||||||
|
pub skipped: u32
|
||||||
}
|
}
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user