Merge pull request #1410 from alexlapa/add-wasm-bindgen-skip-attr

Add wasm_bindgen(skip) attribute
This commit is contained in:
Alex Crichton 2019-04-15 09:31:01 -05:00 committed by GitHub
commit 7ee4906661
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 67 additions and 8 deletions

View File

@ -51,6 +51,7 @@ macro_rules! attrgen {
(variadic, Variadic(Span)),
(typescript_custom_section, TypescriptCustomSection(Span)),
(start, Start(Span)),
(skip, Skip(Span)),
}
};
}
@ -295,7 +296,7 @@ trait ConvertToAst<Ctx> {
impl<'a> ConvertToAst<BindgenAttrs> for &'a mut syn::ItemStruct {
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 {
bail_span!(
self.generics,
@ -304,7 +305,7 @@ impl<'a> ConvertToAst<BindgenAttrs> for &'a mut syn::ItemStruct {
);
}
let mut fields = Vec::new();
let js_name = opts
let js_name = attrs
.js_name()
.map(|s| s.0.to_string())
.unwrap_or(self.ident.to_string());
@ -318,26 +319,33 @@ impl<'a> ConvertToAst<BindgenAttrs> for &'a mut syn::ItemStruct {
Some(n) => n,
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 getter = shared::struct_field_get(&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 {
name: name.clone(),
struct_name: self.ident.clone(),
readonly: opts.readonly().is_some(),
readonly: attrs.readonly().is_some(),
ty: field.ty.clone(),
getter: Ident::new(&getter, Span::call_site()),
setter: Ident::new(&setter, Span::call_site()),
comments,
});
opts.check_used()?;
attrs.check_used()?;
}
}
let comments: Vec<String> = extract_doc_comments(&self.attrs);
opts.check_used()?;
attrs.check_used()?;
Ok(ast::Struct {
rust_name: self.ident.clone(),
js_name,

View File

@ -75,6 +75,7 @@
- [`constructor`](./reference/attributes/on-rust-exports/constructor.md)
- [`js_name = Blah`](./reference/attributes/on-rust-exports/js_name.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)
- [`typescript_custom_section`](./reference/attributes/on-rust-exports/typescript_custom_section.md)

View 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);
});
```

View File

@ -108,6 +108,8 @@ exports.js_public_fields = () => {
assert.strictEqual(a.d, 0);
a.d = 3.3;
assert.strictEqual(a.d, 3);
assert.strictEqual(a.skipped, undefined);
};
exports.js_using_self = () => {

View File

@ -274,6 +274,8 @@ pub struct PublicFields {
pub b: f32,
pub c: f64,
pub d: i32,
#[wasm_bindgen(skip)]
pub skipped: u32
}
#[wasm_bindgen]