Merge pull request #77 from dflemstr/wasm-construct-js-class

Add support for constructing JsValue instances generically
This commit is contained in:
Alex Crichton
2018-04-03 16:08:30 +02:00
committed by GitHub
7 changed files with 106 additions and 9 deletions

View File

@ -88,6 +88,7 @@ impl ToTokens for ast::Program {
impl ToTokens for ast::Struct {
fn to_tokens(&self, tokens: &mut Tokens) {
let name = &self.name;
let new_fn = syn::Ident::from(shared::new_function(self.name.as_ref()));
let free_fn = syn::Ident::from(shared::free_function(self.name.as_ref()));
let c = shared::name_to_descriptor(name.as_ref());
let descriptor = Literal::byte_string(format!("{:4}", c).as_bytes());
@ -153,6 +154,28 @@ impl ToTokens for ast::Struct {
}
}
impl ::std::convert::From<#name> for ::wasm_bindgen::JsValue {
fn from(value: #name) -> Self {
let ptr = ::wasm_bindgen::convert::WasmBoundary::into_abi(
value,
unsafe { &mut ::wasm_bindgen::convert::GlobalStack::new() },
);
#[wasm_import_module = "__wbindgen_placeholder__"]
extern {
fn #new_fn(ptr: u32) -> u32;
}
unsafe {
<::wasm_bindgen::JsValue as ::wasm_bindgen::convert::WasmBoundary>
::from_abi(
#new_fn(ptr),
unsafe { &mut ::wasm_bindgen::convert::GlobalStack::new() },
)
}
}
}
#[no_mangle]
pub unsafe extern fn #free_fn(ptr: u32) {
<#name as ::wasm_bindgen::convert::WasmBoundary>::from_abi(