Implement readonly struct fields

Add support for `#[wasm_bindgen(readonly)]` which indicates that an exported
struct field is readonly and attempting to set it in JS will throw an exception.

Closes #151
This commit is contained in:
Alex Crichton
2018-04-20 10:56:10 -07:00
parent 3b4bf475be
commit 7108206835
6 changed files with 103 additions and 22 deletions

View File

@ -247,6 +247,18 @@ impl ToTokens for ast::StructField {
)
}
#[no_mangle]
pub extern fn #desc() {
use wasm_bindgen::describe::*;
<#ty as WasmDescribe>::describe();
}
}).to_tokens(tokens);
if self.opts.readonly() {
return
}
(quote! {
#[no_mangle]
pub unsafe extern fn #setter(
js: u32,
@ -263,13 +275,6 @@ impl ToTokens for ast::StructField {
);
(*js).borrow_mut().#name = val;
}
#[no_mangle]
pub extern fn #desc() {
use wasm_bindgen::describe::*;
<#ty as WasmDescribe>::describe();
}
}).to_tokens(tokens);
}
}