2018-03-29 09:14:32 -07:00
|
|
|
use std::collections::HashSet;
|
2018-06-14 02:03:52 -07:00
|
|
|
use std::sync::atomic::{AtomicUsize, Ordering, ATOMIC_USIZE_INIT};
|
2018-03-31 09:04:00 -07:00
|
|
|
|
|
|
|
use ast;
|
2018-07-08 22:09:00 -04:00
|
|
|
use proc_macro2::{Ident, Literal, Span, TokenStream};
|
2018-05-21 07:29:34 -07:00
|
|
|
use quote::ToTokens;
|
Overhaul how type information gets to the CLI
This commit is a complete overhaul of how the `#[wasm_bindgen]` macro
communicates type information to the CLI tool, and it's done in a somewhat...
unconventional fashion.
Today we've got a problem where the generated JS needs to understand the types
of each function exported or imported. This understanding is what enables it to
generate the appropriate JS wrappers and such. We want to, however, be quite
flexible and extensible in types that are supported across the boundary, which
means that internally we rely on the trait system to resolve what's what.
Communicating the type information historically was done by creating a four byte
"descriptor" and using associated type projections to communicate that to the
CLI tool. Unfortunately four bytes isn't a lot of space to cram information like
arguments to a generic function, tuple types, etc. In general this just wasn't
flexible enough and the way custom references were treated was also already a
bit of a hack.
This commit takes a radical step of creating a **descriptor function** for each
function imported/exported. The really crazy part is that the `wasm-bindgen` CLI
tool now embeds a wasm interpreter and executes these functions when the CLI
tool is invoked. By allowing arbitrary functions to get executed it's now *much*
easier to inform `wasm-bindgen` about complicated structures of types. Rest
assured though that all these descriptor functions are automatically unexported
and gc'd away, so this should not have any impact on binary sizes
A new internal trait, `WasmDescribe`, is added to represent a description of all
types, sort of like a serialization of the structure of a type that
`wasm-bindgen` can understand. This works by calling a special exported function
with a `u32` value a bunch of times. This means that when we run a descriptor we
effectively get a `Vec<u32>` in the `wasm-bindgen` CLI tool. This list of
integers can then be parsed into a rich `enum` for the JS generation to work
with.
This commit currently only retains feature parity with the previous
implementation. I hope to soon solve issues like #123, #104, and #111 with this
support.
2018-04-13 07:33:46 -07:00
|
|
|
use serde_json;
|
2018-03-31 09:04:00 -07:00
|
|
|
use shared;
|
2018-03-29 09:14:32 -07:00
|
|
|
use syn;
|
2018-07-17 18:24:48 -05:00
|
|
|
use util::ShortHash;
|
2018-03-29 09:14:32 -07:00
|
|
|
|
|
|
|
impl ToTokens for ast::Program {
|
|
|
|
// Generate wrappers for all the items that we've found
|
2018-05-21 07:29:34 -07:00
|
|
|
fn to_tokens(&self, tokens: &mut TokenStream) {
|
2018-03-29 09:14:32 -07:00
|
|
|
for export in self.exports.iter() {
|
|
|
|
export.to_tokens(tokens);
|
|
|
|
}
|
|
|
|
for s in self.structs.iter() {
|
|
|
|
s.to_tokens(tokens);
|
|
|
|
}
|
|
|
|
let mut types = HashSet::new();
|
|
|
|
for i in self.imports.iter() {
|
2018-05-21 07:29:34 -07:00
|
|
|
if let ast::ImportKind::Type(t) = &i.kind {
|
|
|
|
types.insert(t.name.clone());
|
2018-03-29 09:14:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for i in self.imports.iter() {
|
2018-05-11 08:28:09 -07:00
|
|
|
DescribeImport(&i.kind).to_tokens(tokens);
|
|
|
|
|
2018-05-21 07:29:34 -07:00
|
|
|
if let Some(ns) = &i.js_namespace {
|
|
|
|
if types.contains(ns) && i.kind.fits_on_impl() {
|
2018-03-29 09:14:32 -07:00
|
|
|
let kind = &i.kind;
|
|
|
|
(quote! { impl #ns { #kind } }).to_tokens(tokens);
|
2018-06-14 02:03:52 -07:00
|
|
|
continue;
|
2018-03-29 09:14:32 -07:00
|
|
|
}
|
|
|
|
}
|
2018-05-11 08:28:09 -07:00
|
|
|
|
|
|
|
i.kind.to_tokens(tokens);
|
2018-03-29 09:14:32 -07:00
|
|
|
}
|
|
|
|
for e in self.enums.iter() {
|
|
|
|
e.to_tokens(tokens);
|
|
|
|
}
|
2018-06-11 18:35:20 -07:00
|
|
|
for a in self.type_aliases.iter() {
|
|
|
|
a.to_tokens(tokens);
|
|
|
|
}
|
2018-07-13 22:36:51 -07:00
|
|
|
for c in self.consts.iter() {
|
|
|
|
c.to_tokens(tokens);
|
|
|
|
}
|
2018-03-29 09:14:32 -07:00
|
|
|
|
|
|
|
// Generate a static which will eventually be what lives in a custom section
|
|
|
|
// of the wasm executable. For now it's just a plain old static, but we'll
|
|
|
|
// eventually have it actually in its own section.
|
|
|
|
|
|
|
|
static CNT: AtomicUsize = ATOMIC_USIZE_INIT;
|
|
|
|
|
|
|
|
let generated_static_name = format!(
|
2018-07-17 18:24:48 -05:00
|
|
|
"__WASM_BINDGEN_GENERATED_{}",
|
|
|
|
ShortHash(CNT.fetch_add(1, Ordering::SeqCst)),
|
2018-03-29 09:14:32 -07:00
|
|
|
);
|
2018-05-21 07:29:34 -07:00
|
|
|
let generated_static_name = Ident::new(&generated_static_name, Span::call_site());
|
2018-03-29 09:14:32 -07:00
|
|
|
|
Overhaul how type information gets to the CLI
This commit is a complete overhaul of how the `#[wasm_bindgen]` macro
communicates type information to the CLI tool, and it's done in a somewhat...
unconventional fashion.
Today we've got a problem where the generated JS needs to understand the types
of each function exported or imported. This understanding is what enables it to
generate the appropriate JS wrappers and such. We want to, however, be quite
flexible and extensible in types that are supported across the boundary, which
means that internally we rely on the trait system to resolve what's what.
Communicating the type information historically was done by creating a four byte
"descriptor" and using associated type projections to communicate that to the
CLI tool. Unfortunately four bytes isn't a lot of space to cram information like
arguments to a generic function, tuple types, etc. In general this just wasn't
flexible enough and the way custom references were treated was also already a
bit of a hack.
This commit takes a radical step of creating a **descriptor function** for each
function imported/exported. The really crazy part is that the `wasm-bindgen` CLI
tool now embeds a wasm interpreter and executes these functions when the CLI
tool is invoked. By allowing arbitrary functions to get executed it's now *much*
easier to inform `wasm-bindgen` about complicated structures of types. Rest
assured though that all these descriptor functions are automatically unexported
and gc'd away, so this should not have any impact on binary sizes
A new internal trait, `WasmDescribe`, is added to represent a description of all
types, sort of like a serialization of the structure of a type that
`wasm-bindgen` can understand. This works by calling a special exported function
with a `u32` value a bunch of times. This means that when we run a descriptor we
effectively get a `Vec<u32>` in the `wasm-bindgen` CLI tool. This list of
integers can then be parsed into a rich `enum` for the JS generation to work
with.
This commit currently only retains feature parity with the previous
implementation. I hope to soon solve issues like #123, #104, and #111 with this
support.
2018-04-13 07:33:46 -07:00
|
|
|
let description = serde_json::to_string(&self.shared()).unwrap();
|
|
|
|
|
|
|
|
// Each JSON blob is prepended with the length of the JSON blob so when
|
|
|
|
// all these sections are concatenated in the final wasm file we know
|
|
|
|
// how to extract all the JSON pieces, so insert the byte length here.
|
|
|
|
let generated_static_length = description.len() + 4;
|
|
|
|
let mut bytes = vec![
|
|
|
|
(description.len() >> 0) as u8,
|
|
|
|
(description.len() >> 8) as u8,
|
|
|
|
(description.len() >> 16) as u8,
|
|
|
|
(description.len() >> 24) as u8,
|
|
|
|
];
|
|
|
|
bytes.extend_from_slice(description.as_bytes());
|
|
|
|
let generated_static_value = syn::LitByteStr::new(&bytes, Span::call_site());
|
2018-03-29 09:14:32 -07:00
|
|
|
|
2018-04-03 07:07:14 -07:00
|
|
|
(quote! {
|
2018-03-29 09:14:32 -07:00
|
|
|
#[allow(non_upper_case_globals)]
|
2018-07-25 16:56:27 -05:00
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
#[link_section = "__wasm_bindgen_unstable"]
|
2018-07-19 08:57:18 -05:00
|
|
|
#[doc(hidden)]
|
|
|
|
pub static #generated_static_name: [u8; #generated_static_length] =
|
Overhaul how type information gets to the CLI
This commit is a complete overhaul of how the `#[wasm_bindgen]` macro
communicates type information to the CLI tool, and it's done in a somewhat...
unconventional fashion.
Today we've got a problem where the generated JS needs to understand the types
of each function exported or imported. This understanding is what enables it to
generate the appropriate JS wrappers and such. We want to, however, be quite
flexible and extensible in types that are supported across the boundary, which
means that internally we rely on the trait system to resolve what's what.
Communicating the type information historically was done by creating a four byte
"descriptor" and using associated type projections to communicate that to the
CLI tool. Unfortunately four bytes isn't a lot of space to cram information like
arguments to a generic function, tuple types, etc. In general this just wasn't
flexible enough and the way custom references were treated was also already a
bit of a hack.
This commit takes a radical step of creating a **descriptor function** for each
function imported/exported. The really crazy part is that the `wasm-bindgen` CLI
tool now embeds a wasm interpreter and executes these functions when the CLI
tool is invoked. By allowing arbitrary functions to get executed it's now *much*
easier to inform `wasm-bindgen` about complicated structures of types. Rest
assured though that all these descriptor functions are automatically unexported
and gc'd away, so this should not have any impact on binary sizes
A new internal trait, `WasmDescribe`, is added to represent a description of all
types, sort of like a serialization of the structure of a type that
`wasm-bindgen` can understand. This works by calling a special exported function
with a `u32` value a bunch of times. This means that when we run a descriptor we
effectively get a `Vec<u32>` in the `wasm-bindgen` CLI tool. This list of
integers can then be parsed into a rich `enum` for the JS generation to work
with.
This commit currently only retains feature parity with the previous
implementation. I hope to soon solve issues like #123, #104, and #111 with this
support.
2018-04-13 07:33:46 -07:00
|
|
|
*#generated_static_value;
|
2018-03-29 09:14:32 -07:00
|
|
|
}).to_tokens(tokens);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToTokens for ast::Struct {
|
2018-05-21 07:29:34 -07:00
|
|
|
fn to_tokens(&self, tokens: &mut TokenStream) {
|
2018-03-29 09:14:32 -07:00
|
|
|
let name = &self.name;
|
2018-05-21 07:29:34 -07:00
|
|
|
let name_str = name.to_string();
|
|
|
|
let name_len = name_str.len() as u32;
|
|
|
|
let name_chars = name_str.chars().map(|c| c as u32);
|
|
|
|
let new_fn = Ident::new(&shared::new_function(&name_str), Span::call_site());
|
|
|
|
let free_fn = Ident::new(&shared::free_function(&name_str), Span::call_site());
|
2018-04-03 07:07:14 -07:00
|
|
|
(quote! {
|
Overhaul how type information gets to the CLI
This commit is a complete overhaul of how the `#[wasm_bindgen]` macro
communicates type information to the CLI tool, and it's done in a somewhat...
unconventional fashion.
Today we've got a problem where the generated JS needs to understand the types
of each function exported or imported. This understanding is what enables it to
generate the appropriate JS wrappers and such. We want to, however, be quite
flexible and extensible in types that are supported across the boundary, which
means that internally we rely on the trait system to resolve what's what.
Communicating the type information historically was done by creating a four byte
"descriptor" and using associated type projections to communicate that to the
CLI tool. Unfortunately four bytes isn't a lot of space to cram information like
arguments to a generic function, tuple types, etc. In general this just wasn't
flexible enough and the way custom references were treated was also already a
bit of a hack.
This commit takes a radical step of creating a **descriptor function** for each
function imported/exported. The really crazy part is that the `wasm-bindgen` CLI
tool now embeds a wasm interpreter and executes these functions when the CLI
tool is invoked. By allowing arbitrary functions to get executed it's now *much*
easier to inform `wasm-bindgen` about complicated structures of types. Rest
assured though that all these descriptor functions are automatically unexported
and gc'd away, so this should not have any impact on binary sizes
A new internal trait, `WasmDescribe`, is added to represent a description of all
types, sort of like a serialization of the structure of a type that
`wasm-bindgen` can understand. This works by calling a special exported function
with a `u32` value a bunch of times. This means that when we run a descriptor we
effectively get a `Vec<u32>` in the `wasm-bindgen` CLI tool. This list of
integers can then be parsed into a rich `enum` for the JS generation to work
with.
This commit currently only retains feature parity with the previous
implementation. I hope to soon solve issues like #123, #104, and #111 with this
support.
2018-04-13 07:33:46 -07:00
|
|
|
impl ::wasm_bindgen::describe::WasmDescribe for #name {
|
|
|
|
fn describe() {
|
2018-04-26 19:12:56 -07:00
|
|
|
use wasm_bindgen::__wbindgen_if_not_std;
|
|
|
|
__wbindgen_if_not_std! {
|
|
|
|
compile_error! {
|
|
|
|
"exporting a class to JS requires the `std` feature to \
|
|
|
|
be enabled in the `wasm-bindgen` crate"
|
|
|
|
}
|
|
|
|
}
|
Overhaul how type information gets to the CLI
This commit is a complete overhaul of how the `#[wasm_bindgen]` macro
communicates type information to the CLI tool, and it's done in a somewhat...
unconventional fashion.
Today we've got a problem where the generated JS needs to understand the types
of each function exported or imported. This understanding is what enables it to
generate the appropriate JS wrappers and such. We want to, however, be quite
flexible and extensible in types that are supported across the boundary, which
means that internally we rely on the trait system to resolve what's what.
Communicating the type information historically was done by creating a four byte
"descriptor" and using associated type projections to communicate that to the
CLI tool. Unfortunately four bytes isn't a lot of space to cram information like
arguments to a generic function, tuple types, etc. In general this just wasn't
flexible enough and the way custom references were treated was also already a
bit of a hack.
This commit takes a radical step of creating a **descriptor function** for each
function imported/exported. The really crazy part is that the `wasm-bindgen` CLI
tool now embeds a wasm interpreter and executes these functions when the CLI
tool is invoked. By allowing arbitrary functions to get executed it's now *much*
easier to inform `wasm-bindgen` about complicated structures of types. Rest
assured though that all these descriptor functions are automatically unexported
and gc'd away, so this should not have any impact on binary sizes
A new internal trait, `WasmDescribe`, is added to represent a description of all
types, sort of like a serialization of the structure of a type that
`wasm-bindgen` can understand. This works by calling a special exported function
with a `u32` value a bunch of times. This means that when we run a descriptor we
effectively get a `Vec<u32>` in the `wasm-bindgen` CLI tool. This list of
integers can then be parsed into a rich `enum` for the JS generation to work
with.
This commit currently only retains feature parity with the previous
implementation. I hope to soon solve issues like #123, #104, and #111 with this
support.
2018-04-13 07:33:46 -07:00
|
|
|
use wasm_bindgen::describe::*;
|
|
|
|
inform(RUST_STRUCT);
|
|
|
|
inform(#name_len);
|
|
|
|
#(inform(#name_chars);)*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-13 22:58:35 -07:00
|
|
|
impl ::wasm_bindgen::convert::IntoWasmAbi for #name {
|
2018-03-31 07:57:47 -07:00
|
|
|
type Abi = u32;
|
2018-03-29 09:14:32 -07:00
|
|
|
|
2018-03-31 09:04:00 -07:00
|
|
|
fn into_abi(self, _extra: &mut ::wasm_bindgen::convert::Stack)
|
|
|
|
-> u32
|
|
|
|
{
|
2018-04-19 13:08:54 -07:00
|
|
|
use wasm_bindgen::__rt::std::boxed::Box;
|
|
|
|
use wasm_bindgen::__rt::WasmRefCell;
|
|
|
|
Box::into_raw(Box::new(WasmRefCell::new(self))) as u32
|
2018-03-29 09:14:32 -07:00
|
|
|
}
|
2018-04-13 22:58:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ::wasm_bindgen::convert::FromWasmAbi for #name {
|
|
|
|
type Abi = u32;
|
2018-03-29 09:14:32 -07:00
|
|
|
|
2018-04-19 13:16:59 -07:00
|
|
|
unsafe fn from_abi(js: u32, _extra: &mut ::wasm_bindgen::convert::Stack)
|
2018-03-31 09:04:00 -07:00
|
|
|
-> Self
|
|
|
|
{
|
2018-04-19 13:08:54 -07:00
|
|
|
use wasm_bindgen::__rt::std::boxed::Box;
|
|
|
|
use wasm_bindgen::__rt::{assert_not_null, WasmRefCell};
|
|
|
|
|
|
|
|
let ptr = js as *mut WasmRefCell<#name>;
|
|
|
|
assert_not_null(ptr);
|
2018-04-13 22:58:35 -07:00
|
|
|
let js = Box::from_raw(ptr);
|
2018-03-29 09:14:32 -07:00
|
|
|
js.borrow_mut(); // make sure no one's borrowing
|
|
|
|
js.into_inner()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-19 13:08:54 -07:00
|
|
|
impl ::wasm_bindgen::__rt::core::convert::From<#name> for
|
|
|
|
::wasm_bindgen::JsValue
|
|
|
|
{
|
2018-04-27 13:43:47 -07:00
|
|
|
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
|
2018-03-28 01:22:31 +02:00
|
|
|
fn from(value: #name) -> Self {
|
2018-04-13 22:58:35 -07:00
|
|
|
let ptr = ::wasm_bindgen::convert::IntoWasmAbi::into_abi(
|
2018-04-02 09:58:27 -07:00
|
|
|
value,
|
|
|
|
unsafe { &mut ::wasm_bindgen::convert::GlobalStack::new() },
|
|
|
|
);
|
2018-03-28 01:22:31 +02:00
|
|
|
|
2018-07-21 19:00:20 -07:00
|
|
|
#[link(wasm_import_module = "__wbindgen_placeholder__")]
|
2018-03-28 01:22:31 +02:00
|
|
|
extern {
|
|
|
|
fn #new_fn(ptr: u32) -> u32;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe {
|
2018-04-13 22:58:35 -07:00
|
|
|
<::wasm_bindgen::JsValue as ::wasm_bindgen::convert::FromWasmAbi>
|
2018-04-02 09:58:27 -07:00
|
|
|
::from_abi(
|
|
|
|
#new_fn(ptr),
|
2018-04-03 12:53:24 -07:00
|
|
|
&mut ::wasm_bindgen::convert::GlobalStack::new(),
|
2018-04-02 09:58:27 -07:00
|
|
|
)
|
2018-03-28 01:22:31 +02:00
|
|
|
}
|
|
|
|
}
|
2018-04-27 13:43:47 -07:00
|
|
|
|
|
|
|
#[cfg(not(all(target_arch = "wasm32", not(target_os = "emscripten"))))]
|
|
|
|
fn from(_value: #name) -> Self {
|
|
|
|
panic!("cannot convert to JsValue outside of the wasm target")
|
|
|
|
}
|
2018-03-28 01:22:31 +02:00
|
|
|
}
|
|
|
|
|
2018-04-27 13:43:47 -07:00
|
|
|
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
|
2018-03-29 09:14:32 -07:00
|
|
|
#[no_mangle]
|
2018-07-09 08:10:30 -07:00
|
|
|
#[doc(hidden)]
|
2018-03-29 09:14:32 -07:00
|
|
|
pub unsafe extern fn #free_fn(ptr: u32) {
|
2018-04-13 22:58:35 -07:00
|
|
|
<#name as ::wasm_bindgen::convert::FromWasmAbi>::from_abi(
|
2018-03-31 09:04:00 -07:00
|
|
|
ptr,
|
|
|
|
&mut ::wasm_bindgen::convert::GlobalStack::new(),
|
|
|
|
);
|
2018-03-29 09:14:32 -07:00
|
|
|
}
|
2018-04-19 13:08:54 -07:00
|
|
|
|
|
|
|
impl ::wasm_bindgen::convert::RefFromWasmAbi for #name {
|
|
|
|
type Abi = u32;
|
|
|
|
type Anchor = ::wasm_bindgen::__rt::Ref<'static, #name>;
|
|
|
|
|
|
|
|
unsafe fn ref_from_abi(
|
|
|
|
js: Self::Abi,
|
|
|
|
_extra: &mut ::wasm_bindgen::convert::Stack,
|
|
|
|
) -> Self::Anchor {
|
|
|
|
let js = js as *mut ::wasm_bindgen::__rt::WasmRefCell<#name>;
|
|
|
|
::wasm_bindgen::__rt::assert_not_null(js);
|
|
|
|
(*js).borrow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ::wasm_bindgen::convert::RefMutFromWasmAbi for #name {
|
|
|
|
type Abi = u32;
|
|
|
|
type Anchor = ::wasm_bindgen::__rt::RefMut<'static, #name>;
|
|
|
|
|
|
|
|
unsafe fn ref_mut_from_abi(
|
|
|
|
js: Self::Abi,
|
|
|
|
_extra: &mut ::wasm_bindgen::convert::Stack,
|
|
|
|
) -> Self::Anchor {
|
|
|
|
let js = js as *mut ::wasm_bindgen::__rt::WasmRefCell<#name>;
|
|
|
|
::wasm_bindgen::__rt::assert_not_null(js);
|
|
|
|
(*js).borrow_mut()
|
|
|
|
}
|
|
|
|
}
|
2018-03-29 09:14:32 -07:00
|
|
|
}).to_tokens(tokens);
|
2018-04-19 16:49:46 -07:00
|
|
|
|
|
|
|
for field in self.fields.iter() {
|
|
|
|
field.to_tokens(tokens);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToTokens for ast::StructField {
|
2018-05-21 07:29:34 -07:00
|
|
|
fn to_tokens(&self, tokens: &mut TokenStream) {
|
2018-04-19 16:49:46 -07:00
|
|
|
let name = &self.name;
|
|
|
|
let struct_name = &self.struct_name;
|
|
|
|
let ty = &self.ty;
|
|
|
|
let getter = &self.getter;
|
|
|
|
let setter = &self.setter;
|
2018-06-14 02:03:52 -07:00
|
|
|
let desc = Ident::new(
|
|
|
|
&format!("__wbindgen_describe_{}", getter),
|
|
|
|
Span::call_site(),
|
|
|
|
);
|
2018-04-19 16:49:46 -07:00
|
|
|
(quote! {
|
|
|
|
#[no_mangle]
|
2018-07-09 08:10:30 -07:00
|
|
|
#[doc(hidden)]
|
2018-04-27 13:43:47 -07:00
|
|
|
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
|
2018-04-19 16:49:46 -07:00
|
|
|
pub unsafe extern fn #getter(js: u32)
|
|
|
|
-> <#ty as ::wasm_bindgen::convert::IntoWasmAbi>::Abi
|
|
|
|
{
|
|
|
|
use wasm_bindgen::__rt::{WasmRefCell, assert_not_null};
|
|
|
|
use wasm_bindgen::convert::{GlobalStack, IntoWasmAbi};
|
|
|
|
|
|
|
|
fn assert_copy<T: Copy>(){}
|
|
|
|
assert_copy::<#ty>();
|
|
|
|
|
|
|
|
let js = js as *mut WasmRefCell<#struct_name>;
|
|
|
|
assert_not_null(js);
|
|
|
|
let val = (*js).borrow().#name;
|
|
|
|
<#ty as IntoWasmAbi>::into_abi(
|
|
|
|
val,
|
|
|
|
&mut GlobalStack::new(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-04-20 10:56:10 -07:00
|
|
|
#[no_mangle]
|
2018-06-18 15:30:29 -07:00
|
|
|
#[doc(hidden)]
|
2018-04-20 10:56:10 -07:00
|
|
|
pub extern fn #desc() {
|
|
|
|
use wasm_bindgen::describe::*;
|
|
|
|
<#ty as WasmDescribe>::describe();
|
|
|
|
}
|
|
|
|
}).to_tokens(tokens);
|
|
|
|
|
2018-07-07 10:20:31 -07:00
|
|
|
if self.readonly {
|
2018-06-14 02:03:52 -07:00
|
|
|
return;
|
2018-04-20 10:56:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
(quote! {
|
2018-04-19 16:49:46 -07:00
|
|
|
#[no_mangle]
|
2018-07-09 08:10:30 -07:00
|
|
|
#[doc(hidden)]
|
2018-04-27 13:43:47 -07:00
|
|
|
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
|
2018-04-19 16:49:46 -07:00
|
|
|
pub unsafe extern fn #setter(
|
|
|
|
js: u32,
|
|
|
|
val: <#ty as ::wasm_bindgen::convert::FromWasmAbi>::Abi,
|
|
|
|
) {
|
|
|
|
use wasm_bindgen::__rt::{WasmRefCell, assert_not_null};
|
|
|
|
use wasm_bindgen::convert::{GlobalStack, FromWasmAbi};
|
|
|
|
|
|
|
|
let js = js as *mut WasmRefCell<#struct_name>;
|
|
|
|
assert_not_null(js);
|
|
|
|
let val = <#ty as FromWasmAbi>::from_abi(
|
|
|
|
val,
|
|
|
|
&mut GlobalStack::new(),
|
|
|
|
);
|
|
|
|
(*js).borrow_mut().#name = val;
|
|
|
|
}
|
|
|
|
}).to_tokens(tokens);
|
2018-03-29 09:14:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToTokens for ast::Export {
|
2018-05-21 07:29:34 -07:00
|
|
|
fn to_tokens(self: &ast::Export, into: &mut TokenStream) {
|
2018-03-29 09:14:32 -07:00
|
|
|
let generated_name = self.rust_symbol();
|
|
|
|
let export_name = self.export_name();
|
|
|
|
let mut args = vec![];
|
|
|
|
let mut arg_conversions = vec![];
|
|
|
|
let mut converted_arguments = vec![];
|
2018-05-21 07:29:34 -07:00
|
|
|
let ret = Ident::new("_ret", Span::call_site());
|
2018-03-29 09:14:32 -07:00
|
|
|
|
2018-07-15 12:43:55 -04:00
|
|
|
let offset = if self.method_self.is_some() {
|
2018-06-28 20:09:11 -05:00
|
|
|
args.push(quote! { me: u32 });
|
2018-07-15 12:43:55 -04:00
|
|
|
1
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
};
|
2018-03-29 09:14:32 -07:00
|
|
|
|
2018-07-20 12:01:28 -05:00
|
|
|
let name = &self.rust_name;
|
2018-06-28 20:09:11 -05:00
|
|
|
let receiver = match self.method_self {
|
|
|
|
Some(ast::MethodSelf::ByValue) => {
|
|
|
|
let class = self.class.as_ref().unwrap();
|
|
|
|
arg_conversions.push(quote! {
|
|
|
|
let me = unsafe {
|
|
|
|
<#class as ::wasm_bindgen::convert::FromWasmAbi>::from_abi(
|
|
|
|
me,
|
|
|
|
&mut ::wasm_bindgen::convert::GlobalStack::new(),
|
|
|
|
)
|
|
|
|
};
|
|
|
|
});
|
|
|
|
quote! { me.#name }
|
|
|
|
}
|
|
|
|
Some(ast::MethodSelf::RefMutable) => {
|
|
|
|
let class = self.class.as_ref().unwrap();
|
|
|
|
arg_conversions.push(quote! {
|
|
|
|
let mut me = unsafe {
|
|
|
|
<#class as ::wasm_bindgen::convert::RefMutFromWasmAbi>
|
|
|
|
::ref_mut_from_abi(
|
|
|
|
me,
|
|
|
|
&mut ::wasm_bindgen::convert::GlobalStack::new(),
|
|
|
|
)
|
|
|
|
};
|
|
|
|
let me = &mut *me;
|
|
|
|
});
|
|
|
|
quote! { me.#name }
|
|
|
|
}
|
|
|
|
Some(ast::MethodSelf::RefShared) => {
|
|
|
|
let class = self.class.as_ref().unwrap();
|
|
|
|
arg_conversions.push(quote! {
|
|
|
|
let me = unsafe {
|
|
|
|
<#class as ::wasm_bindgen::convert::RefFromWasmAbi>
|
|
|
|
::ref_from_abi(
|
|
|
|
me,
|
|
|
|
&mut ::wasm_bindgen::convert::GlobalStack::new(),
|
|
|
|
)
|
|
|
|
};
|
|
|
|
let me = &*me;
|
|
|
|
});
|
|
|
|
quote! { me.#name }
|
|
|
|
}
|
2018-07-07 10:20:31 -07:00
|
|
|
None => match &self.class {
|
|
|
|
Some(class) => quote! { #class::#name },
|
|
|
|
None => quote! { #name },
|
|
|
|
},
|
2018-06-28 20:09:11 -05:00
|
|
|
};
|
|
|
|
|
2018-06-14 02:03:52 -07:00
|
|
|
for (i, syn::ArgCaptured { ty, .. }) in self.function.arguments.iter().enumerate() {
|
2018-03-29 09:14:32 -07:00
|
|
|
let i = i + offset;
|
2018-05-21 07:29:34 -07:00
|
|
|
let ident = Ident::new(&format!("arg{}", i), Span::call_site());
|
2018-04-13 22:58:35 -07:00
|
|
|
match *ty {
|
|
|
|
syn::Type::Reference(syn::TypeReference {
|
|
|
|
mutability: Some(_),
|
|
|
|
ref elem,
|
|
|
|
..
|
|
|
|
}) => {
|
2018-04-03 07:07:14 -07:00
|
|
|
args.push(quote! {
|
2018-04-13 22:58:35 -07:00
|
|
|
#ident: <#elem as ::wasm_bindgen::convert::RefMutFromWasmAbi>::Abi
|
2018-03-29 09:14:32 -07:00
|
|
|
});
|
2018-04-03 07:07:14 -07:00
|
|
|
arg_conversions.push(quote! {
|
2018-04-13 22:58:35 -07:00
|
|
|
let mut #ident = unsafe {
|
|
|
|
<#elem as ::wasm_bindgen::convert::RefMutFromWasmAbi>
|
|
|
|
::ref_mut_from_abi(#ident, &mut __stack)
|
2018-03-29 09:14:32 -07:00
|
|
|
};
|
2018-04-13 22:58:35 -07:00
|
|
|
let #ident = &mut *#ident;
|
2018-03-29 09:14:32 -07:00
|
|
|
});
|
|
|
|
}
|
2018-04-13 22:58:35 -07:00
|
|
|
syn::Type::Reference(syn::TypeReference { ref elem, .. }) => {
|
2018-04-03 07:07:14 -07:00
|
|
|
args.push(quote! {
|
2018-04-13 22:58:35 -07:00
|
|
|
#ident: <#elem as ::wasm_bindgen::convert::RefFromWasmAbi>::Abi
|
2018-03-29 09:14:32 -07:00
|
|
|
});
|
2018-04-03 07:07:14 -07:00
|
|
|
arg_conversions.push(quote! {
|
2018-03-29 09:14:32 -07:00
|
|
|
let #ident = unsafe {
|
2018-04-13 22:58:35 -07:00
|
|
|
<#elem as ::wasm_bindgen::convert::RefFromWasmAbi>
|
|
|
|
::ref_from_abi(#ident, &mut __stack)
|
2018-03-29 09:14:32 -07:00
|
|
|
};
|
|
|
|
let #ident = &*#ident;
|
|
|
|
});
|
|
|
|
}
|
2018-04-13 22:58:35 -07:00
|
|
|
_ => {
|
2018-04-03 07:07:14 -07:00
|
|
|
args.push(quote! {
|
2018-04-13 22:58:35 -07:00
|
|
|
#ident: <#ty as ::wasm_bindgen::convert::FromWasmAbi>::Abi
|
2018-03-29 09:14:32 -07:00
|
|
|
});
|
2018-04-03 07:07:14 -07:00
|
|
|
arg_conversions.push(quote! {
|
2018-04-13 22:58:35 -07:00
|
|
|
let #ident = unsafe {
|
|
|
|
<#ty as ::wasm_bindgen::convert::FromWasmAbi>
|
|
|
|
::from_abi(#ident, &mut __stack)
|
2018-03-29 09:14:32 -07:00
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2018-04-03 07:07:14 -07:00
|
|
|
converted_arguments.push(quote! { #ident });
|
2018-03-29 09:14:32 -07:00
|
|
|
}
|
|
|
|
let ret_ty;
|
|
|
|
let convert_ret;
|
2018-05-21 07:29:34 -07:00
|
|
|
match &self.function.ret {
|
2018-04-13 22:58:35 -07:00
|
|
|
Some(syn::Type::Reference(_)) => panic!("can't return a borrowed ref"),
|
2018-05-21 07:29:34 -07:00
|
|
|
Some(ty) => {
|
2018-04-03 07:07:14 -07:00
|
|
|
ret_ty = quote! {
|
2018-04-13 22:58:35 -07:00
|
|
|
-> <#ty as ::wasm_bindgen::convert::IntoWasmAbi>::Abi
|
2018-03-29 09:14:32 -07:00
|
|
|
};
|
2018-04-03 07:07:14 -07:00
|
|
|
convert_ret = quote! {
|
2018-04-13 22:58:35 -07:00
|
|
|
<#ty as ::wasm_bindgen::convert::IntoWasmAbi>
|
2018-03-31 07:57:47 -07:00
|
|
|
::into_abi(#ret, &mut unsafe {
|
|
|
|
::wasm_bindgen::convert::GlobalStack::new()
|
|
|
|
})
|
2018-03-29 09:14:32 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
None => {
|
2018-06-14 02:03:52 -07:00
|
|
|
ret_ty = quote!();
|
|
|
|
convert_ret = quote!();
|
2018-03-29 09:14:32 -07:00
|
|
|
}
|
|
|
|
}
|
2018-05-21 07:29:34 -07:00
|
|
|
let describe_ret = match &self.function.ret {
|
|
|
|
Some(ty) => {
|
Overhaul how type information gets to the CLI
This commit is a complete overhaul of how the `#[wasm_bindgen]` macro
communicates type information to the CLI tool, and it's done in a somewhat...
unconventional fashion.
Today we've got a problem where the generated JS needs to understand the types
of each function exported or imported. This understanding is what enables it to
generate the appropriate JS wrappers and such. We want to, however, be quite
flexible and extensible in types that are supported across the boundary, which
means that internally we rely on the trait system to resolve what's what.
Communicating the type information historically was done by creating a four byte
"descriptor" and using associated type projections to communicate that to the
CLI tool. Unfortunately four bytes isn't a lot of space to cram information like
arguments to a generic function, tuple types, etc. In general this just wasn't
flexible enough and the way custom references were treated was also already a
bit of a hack.
This commit takes a radical step of creating a **descriptor function** for each
function imported/exported. The really crazy part is that the `wasm-bindgen` CLI
tool now embeds a wasm interpreter and executes these functions when the CLI
tool is invoked. By allowing arbitrary functions to get executed it's now *much*
easier to inform `wasm-bindgen` about complicated structures of types. Rest
assured though that all these descriptor functions are automatically unexported
and gc'd away, so this should not have any impact on binary sizes
A new internal trait, `WasmDescribe`, is added to represent a description of all
types, sort of like a serialization of the structure of a type that
`wasm-bindgen` can understand. This works by calling a special exported function
with a `u32` value a bunch of times. This means that when we run a descriptor we
effectively get a `Vec<u32>` in the `wasm-bindgen` CLI tool. This list of
integers can then be parsed into a rich `enum` for the JS generation to work
with.
This commit currently only retains feature parity with the previous
implementation. I hope to soon solve issues like #123, #104, and #111 with this
support.
2018-04-13 07:33:46 -07:00
|
|
|
quote! {
|
|
|
|
inform(1);
|
|
|
|
<#ty as WasmDescribe>::describe();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => quote! { inform(0); },
|
|
|
|
};
|
|
|
|
let descriptor_name = format!("__wbindgen_describe_{}", export_name);
|
2018-05-21 07:29:34 -07:00
|
|
|
let descriptor_name = Ident::new(&descriptor_name, Span::call_site());
|
Overhaul how type information gets to the CLI
This commit is a complete overhaul of how the `#[wasm_bindgen]` macro
communicates type information to the CLI tool, and it's done in a somewhat...
unconventional fashion.
Today we've got a problem where the generated JS needs to understand the types
of each function exported or imported. This understanding is what enables it to
generate the appropriate JS wrappers and such. We want to, however, be quite
flexible and extensible in types that are supported across the boundary, which
means that internally we rely on the trait system to resolve what's what.
Communicating the type information historically was done by creating a four byte
"descriptor" and using associated type projections to communicate that to the
CLI tool. Unfortunately four bytes isn't a lot of space to cram information like
arguments to a generic function, tuple types, etc. In general this just wasn't
flexible enough and the way custom references were treated was also already a
bit of a hack.
This commit takes a radical step of creating a **descriptor function** for each
function imported/exported. The really crazy part is that the `wasm-bindgen` CLI
tool now embeds a wasm interpreter and executes these functions when the CLI
tool is invoked. By allowing arbitrary functions to get executed it's now *much*
easier to inform `wasm-bindgen` about complicated structures of types. Rest
assured though that all these descriptor functions are automatically unexported
and gc'd away, so this should not have any impact on binary sizes
A new internal trait, `WasmDescribe`, is added to represent a description of all
types, sort of like a serialization of the structure of a type that
`wasm-bindgen` can understand. This works by calling a special exported function
with a `u32` value a bunch of times. This means that when we run a descriptor we
effectively get a `Vec<u32>` in the `wasm-bindgen` CLI tool. This list of
integers can then be parsed into a rich `enum` for the JS generation to work
with.
This commit currently only retains feature parity with the previous
implementation. I hope to soon solve issues like #123, #104, and #111 with this
support.
2018-04-13 07:33:46 -07:00
|
|
|
let nargs = self.function.arguments.len() as u32;
|
2018-06-14 02:03:52 -07:00
|
|
|
let argtys = self.function.arguments.iter().map(|arg| &arg.ty);
|
2018-07-09 08:10:30 -07:00
|
|
|
let attrs = &self.function.rust_attrs;
|
2018-03-29 09:14:32 -07:00
|
|
|
|
2018-04-03 07:07:14 -07:00
|
|
|
let tokens = quote! {
|
2018-07-09 08:10:30 -07:00
|
|
|
#(#attrs)*
|
2018-03-29 09:14:32 -07:00
|
|
|
#[export_name = #export_name]
|
|
|
|
#[allow(non_snake_case)]
|
2018-04-27 13:43:47 -07:00
|
|
|
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
|
2018-03-29 09:14:32 -07:00
|
|
|
pub extern fn #generated_name(#(#args),*) #ret_ty {
|
2018-07-14 11:04:47 -05:00
|
|
|
// See definition of `link_mem_intrinsics` for what this is doing
|
|
|
|
::wasm_bindgen::__rt::link_mem_intrinsics();
|
2018-03-31 08:26:20 -07:00
|
|
|
let #ret = {
|
|
|
|
let mut __stack = unsafe {
|
|
|
|
::wasm_bindgen::convert::GlobalStack::new()
|
|
|
|
};
|
|
|
|
#(#arg_conversions)*
|
|
|
|
#receiver(#(#converted_arguments),*)
|
2018-03-31 07:57:47 -07:00
|
|
|
};
|
2018-03-29 09:14:32 -07:00
|
|
|
#convert_ret
|
|
|
|
}
|
Overhaul how type information gets to the CLI
This commit is a complete overhaul of how the `#[wasm_bindgen]` macro
communicates type information to the CLI tool, and it's done in a somewhat...
unconventional fashion.
Today we've got a problem where the generated JS needs to understand the types
of each function exported or imported. This understanding is what enables it to
generate the appropriate JS wrappers and such. We want to, however, be quite
flexible and extensible in types that are supported across the boundary, which
means that internally we rely on the trait system to resolve what's what.
Communicating the type information historically was done by creating a four byte
"descriptor" and using associated type projections to communicate that to the
CLI tool. Unfortunately four bytes isn't a lot of space to cram information like
arguments to a generic function, tuple types, etc. In general this just wasn't
flexible enough and the way custom references were treated was also already a
bit of a hack.
This commit takes a radical step of creating a **descriptor function** for each
function imported/exported. The really crazy part is that the `wasm-bindgen` CLI
tool now embeds a wasm interpreter and executes these functions when the CLI
tool is invoked. By allowing arbitrary functions to get executed it's now *much*
easier to inform `wasm-bindgen` about complicated structures of types. Rest
assured though that all these descriptor functions are automatically unexported
and gc'd away, so this should not have any impact on binary sizes
A new internal trait, `WasmDescribe`, is added to represent a description of all
types, sort of like a serialization of the structure of a type that
`wasm-bindgen` can understand. This works by calling a special exported function
with a `u32` value a bunch of times. This means that when we run a descriptor we
effectively get a `Vec<u32>` in the `wasm-bindgen` CLI tool. This list of
integers can then be parsed into a rich `enum` for the JS generation to work
with.
This commit currently only retains feature parity with the previous
implementation. I hope to soon solve issues like #123, #104, and #111 with this
support.
2018-04-13 07:33:46 -07:00
|
|
|
|
|
|
|
// In addition to generating the shim function above which is what
|
|
|
|
// our generated JS will invoke, we *also* generate a "descriptor"
|
|
|
|
// shim. This descriptor shim uses the `WasmDescribe` trait to
|
|
|
|
// programmatically describe the type signature of the generated
|
|
|
|
// shim above. This in turn is then used to inform the
|
|
|
|
// `wasm-bindgen` CLI tool exactly what types and such it should be
|
|
|
|
// using in JS.
|
|
|
|
//
|
|
|
|
// Note that this descriptor function is a purely an internal detail
|
|
|
|
// of `#[wasm_bindgen]` and isn't intended to be exported to anyone
|
|
|
|
// or actually part of the final was binary. Additionally, this is
|
|
|
|
// literally executed when the `wasm-bindgen` tool executes.
|
|
|
|
//
|
|
|
|
// In any case, there's complications in `wasm-bindgen` to handle
|
|
|
|
// this, but the tl;dr; is that this is stripped from the final wasm
|
|
|
|
// binary along with anything it references.
|
|
|
|
#[no_mangle]
|
2018-04-27 13:43:47 -07:00
|
|
|
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
|
2018-06-18 15:30:29 -07:00
|
|
|
#[doc(hidden)]
|
Overhaul how type information gets to the CLI
This commit is a complete overhaul of how the `#[wasm_bindgen]` macro
communicates type information to the CLI tool, and it's done in a somewhat...
unconventional fashion.
Today we've got a problem where the generated JS needs to understand the types
of each function exported or imported. This understanding is what enables it to
generate the appropriate JS wrappers and such. We want to, however, be quite
flexible and extensible in types that are supported across the boundary, which
means that internally we rely on the trait system to resolve what's what.
Communicating the type information historically was done by creating a four byte
"descriptor" and using associated type projections to communicate that to the
CLI tool. Unfortunately four bytes isn't a lot of space to cram information like
arguments to a generic function, tuple types, etc. In general this just wasn't
flexible enough and the way custom references were treated was also already a
bit of a hack.
This commit takes a radical step of creating a **descriptor function** for each
function imported/exported. The really crazy part is that the `wasm-bindgen` CLI
tool now embeds a wasm interpreter and executes these functions when the CLI
tool is invoked. By allowing arbitrary functions to get executed it's now *much*
easier to inform `wasm-bindgen` about complicated structures of types. Rest
assured though that all these descriptor functions are automatically unexported
and gc'd away, so this should not have any impact on binary sizes
A new internal trait, `WasmDescribe`, is added to represent a description of all
types, sort of like a serialization of the structure of a type that
`wasm-bindgen` can understand. This works by calling a special exported function
with a `u32` value a bunch of times. This means that when we run a descriptor we
effectively get a `Vec<u32>` in the `wasm-bindgen` CLI tool. This list of
integers can then be parsed into a rich `enum` for the JS generation to work
with.
This commit currently only retains feature parity with the previous
implementation. I hope to soon solve issues like #123, #104, and #111 with this
support.
2018-04-13 07:33:46 -07:00
|
|
|
pub extern fn #descriptor_name() {
|
|
|
|
use wasm_bindgen::describe::*;
|
|
|
|
inform(FUNCTION);
|
|
|
|
inform(#nargs);
|
|
|
|
#(<#argtys as WasmDescribe>::describe();)*
|
|
|
|
#describe_ret
|
|
|
|
}
|
2018-03-29 09:14:32 -07:00
|
|
|
};
|
|
|
|
tokens.to_tokens(into);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Overhaul how type information gets to the CLI
This commit is a complete overhaul of how the `#[wasm_bindgen]` macro
communicates type information to the CLI tool, and it's done in a somewhat...
unconventional fashion.
Today we've got a problem where the generated JS needs to understand the types
of each function exported or imported. This understanding is what enables it to
generate the appropriate JS wrappers and such. We want to, however, be quite
flexible and extensible in types that are supported across the boundary, which
means that internally we rely on the trait system to resolve what's what.
Communicating the type information historically was done by creating a four byte
"descriptor" and using associated type projections to communicate that to the
CLI tool. Unfortunately four bytes isn't a lot of space to cram information like
arguments to a generic function, tuple types, etc. In general this just wasn't
flexible enough and the way custom references were treated was also already a
bit of a hack.
This commit takes a radical step of creating a **descriptor function** for each
function imported/exported. The really crazy part is that the `wasm-bindgen` CLI
tool now embeds a wasm interpreter and executes these functions when the CLI
tool is invoked. By allowing arbitrary functions to get executed it's now *much*
easier to inform `wasm-bindgen` about complicated structures of types. Rest
assured though that all these descriptor functions are automatically unexported
and gc'd away, so this should not have any impact on binary sizes
A new internal trait, `WasmDescribe`, is added to represent a description of all
types, sort of like a serialization of the structure of a type that
`wasm-bindgen` can understand. This works by calling a special exported function
with a `u32` value a bunch of times. This means that when we run a descriptor we
effectively get a `Vec<u32>` in the `wasm-bindgen` CLI tool. This list of
integers can then be parsed into a rich `enum` for the JS generation to work
with.
This commit currently only retains feature parity with the previous
implementation. I hope to soon solve issues like #123, #104, and #111 with this
support.
2018-04-13 07:33:46 -07:00
|
|
|
impl ToTokens for ast::ImportKind {
|
2018-05-21 07:29:34 -07:00
|
|
|
fn to_tokens(&self, tokens: &mut TokenStream) {
|
Overhaul how type information gets to the CLI
This commit is a complete overhaul of how the `#[wasm_bindgen]` macro
communicates type information to the CLI tool, and it's done in a somewhat...
unconventional fashion.
Today we've got a problem where the generated JS needs to understand the types
of each function exported or imported. This understanding is what enables it to
generate the appropriate JS wrappers and such. We want to, however, be quite
flexible and extensible in types that are supported across the boundary, which
means that internally we rely on the trait system to resolve what's what.
Communicating the type information historically was done by creating a four byte
"descriptor" and using associated type projections to communicate that to the
CLI tool. Unfortunately four bytes isn't a lot of space to cram information like
arguments to a generic function, tuple types, etc. In general this just wasn't
flexible enough and the way custom references were treated was also already a
bit of a hack.
This commit takes a radical step of creating a **descriptor function** for each
function imported/exported. The really crazy part is that the `wasm-bindgen` CLI
tool now embeds a wasm interpreter and executes these functions when the CLI
tool is invoked. By allowing arbitrary functions to get executed it's now *much*
easier to inform `wasm-bindgen` about complicated structures of types. Rest
assured though that all these descriptor functions are automatically unexported
and gc'd away, so this should not have any impact on binary sizes
A new internal trait, `WasmDescribe`, is added to represent a description of all
types, sort of like a serialization of the structure of a type that
`wasm-bindgen` can understand. This works by calling a special exported function
with a `u32` value a bunch of times. This means that when we run a descriptor we
effectively get a `Vec<u32>` in the `wasm-bindgen` CLI tool. This list of
integers can then be parsed into a rich `enum` for the JS generation to work
with.
This commit currently only retains feature parity with the previous
implementation. I hope to soon solve issues like #123, #104, and #111 with this
support.
2018-04-13 07:33:46 -07:00
|
|
|
match *self {
|
|
|
|
ast::ImportKind::Function(ref f) => f.to_tokens(tokens),
|
|
|
|
ast::ImportKind::Static(ref s) => s.to_tokens(tokens),
|
|
|
|
ast::ImportKind::Type(ref t) => t.to_tokens(tokens),
|
2018-07-08 22:09:00 -04:00
|
|
|
ast::ImportKind::Enum(ref e) => e.to_tokens(tokens),
|
Overhaul how type information gets to the CLI
This commit is a complete overhaul of how the `#[wasm_bindgen]` macro
communicates type information to the CLI tool, and it's done in a somewhat...
unconventional fashion.
Today we've got a problem where the generated JS needs to understand the types
of each function exported or imported. This understanding is what enables it to
generate the appropriate JS wrappers and such. We want to, however, be quite
flexible and extensible in types that are supported across the boundary, which
means that internally we rely on the trait system to resolve what's what.
Communicating the type information historically was done by creating a four byte
"descriptor" and using associated type projections to communicate that to the
CLI tool. Unfortunately four bytes isn't a lot of space to cram information like
arguments to a generic function, tuple types, etc. In general this just wasn't
flexible enough and the way custom references were treated was also already a
bit of a hack.
This commit takes a radical step of creating a **descriptor function** for each
function imported/exported. The really crazy part is that the `wasm-bindgen` CLI
tool now embeds a wasm interpreter and executes these functions when the CLI
tool is invoked. By allowing arbitrary functions to get executed it's now *much*
easier to inform `wasm-bindgen` about complicated structures of types. Rest
assured though that all these descriptor functions are automatically unexported
and gc'd away, so this should not have any impact on binary sizes
A new internal trait, `WasmDescribe`, is added to represent a description of all
types, sort of like a serialization of the structure of a type that
`wasm-bindgen` can understand. This works by calling a special exported function
with a `u32` value a bunch of times. This means that when we run a descriptor we
effectively get a `Vec<u32>` in the `wasm-bindgen` CLI tool. This list of
integers can then be parsed into a rich `enum` for the JS generation to work
with.
This commit currently only retains feature parity with the previous
implementation. I hope to soon solve issues like #123, #104, and #111 with this
support.
2018-04-13 07:33:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-29 09:14:32 -07:00
|
|
|
impl ToTokens for ast::ImportType {
|
2018-05-21 07:29:34 -07:00
|
|
|
fn to_tokens(&self, tokens: &mut TokenStream) {
|
2018-03-29 09:14:32 -07:00
|
|
|
let vis = &self.vis;
|
|
|
|
let name = &self.name;
|
2018-07-05 20:28:52 -07:00
|
|
|
let attrs = &self.attrs;
|
2018-07-29 17:12:36 +01:00
|
|
|
let doc_comment = match &self.doc_comment {
|
|
|
|
None => "",
|
|
|
|
Some(comment) => comment,
|
|
|
|
};
|
2018-04-03 07:07:14 -07:00
|
|
|
(quote! {
|
2018-03-29 09:14:32 -07:00
|
|
|
#[allow(bad_style)]
|
2018-07-05 20:28:52 -07:00
|
|
|
#(#attrs)*
|
2018-07-29 17:12:36 +01:00
|
|
|
#[doc = #doc_comment]
|
2018-03-29 09:14:32 -07:00
|
|
|
#vis struct #name {
|
|
|
|
obj: ::wasm_bindgen::JsValue,
|
|
|
|
}
|
|
|
|
|
Overhaul how type information gets to the CLI
This commit is a complete overhaul of how the `#[wasm_bindgen]` macro
communicates type information to the CLI tool, and it's done in a somewhat...
unconventional fashion.
Today we've got a problem where the generated JS needs to understand the types
of each function exported or imported. This understanding is what enables it to
generate the appropriate JS wrappers and such. We want to, however, be quite
flexible and extensible in types that are supported across the boundary, which
means that internally we rely on the trait system to resolve what's what.
Communicating the type information historically was done by creating a four byte
"descriptor" and using associated type projections to communicate that to the
CLI tool. Unfortunately four bytes isn't a lot of space to cram information like
arguments to a generic function, tuple types, etc. In general this just wasn't
flexible enough and the way custom references were treated was also already a
bit of a hack.
This commit takes a radical step of creating a **descriptor function** for each
function imported/exported. The really crazy part is that the `wasm-bindgen` CLI
tool now embeds a wasm interpreter and executes these functions when the CLI
tool is invoked. By allowing arbitrary functions to get executed it's now *much*
easier to inform `wasm-bindgen` about complicated structures of types. Rest
assured though that all these descriptor functions are automatically unexported
and gc'd away, so this should not have any impact on binary sizes
A new internal trait, `WasmDescribe`, is added to represent a description of all
types, sort of like a serialization of the structure of a type that
`wasm-bindgen` can understand. This works by calling a special exported function
with a `u32` value a bunch of times. This means that when we run a descriptor we
effectively get a `Vec<u32>` in the `wasm-bindgen` CLI tool. This list of
integers can then be parsed into a rich `enum` for the JS generation to work
with.
This commit currently only retains feature parity with the previous
implementation. I hope to soon solve issues like #123, #104, and #111 with this
support.
2018-04-13 07:33:46 -07:00
|
|
|
impl ::wasm_bindgen::describe::WasmDescribe for #name {
|
|
|
|
fn describe() {
|
|
|
|
::wasm_bindgen::JsValue::describe();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-13 22:58:35 -07:00
|
|
|
impl ::wasm_bindgen::convert::IntoWasmAbi for #name {
|
2018-03-31 07:57:47 -07:00
|
|
|
type Abi = <::wasm_bindgen::JsValue as
|
2018-04-13 22:58:35 -07:00
|
|
|
::wasm_bindgen::convert::IntoWasmAbi>::Abi;
|
2018-03-29 09:14:32 -07:00
|
|
|
|
2018-03-31 09:04:00 -07:00
|
|
|
fn into_abi(self, extra: &mut ::wasm_bindgen::convert::Stack) -> Self::Abi {
|
|
|
|
self.obj.into_abi(extra)
|
2018-03-29 09:14:32 -07:00
|
|
|
}
|
2018-04-13 22:58:35 -07:00
|
|
|
}
|
|
|
|
|
2018-07-19 14:44:23 -05:00
|
|
|
impl ::wasm_bindgen::convert::OptionIntoWasmAbi for #name {
|
|
|
|
fn none() -> Self::Abi { 0 }
|
|
|
|
}
|
|
|
|
|
2018-07-24 15:00:46 +01:00
|
|
|
impl<'a> ::wasm_bindgen::convert::OptionIntoWasmAbi for &'a #name {
|
|
|
|
fn none() -> Self::Abi { 0 }
|
|
|
|
}
|
|
|
|
|
2018-04-13 22:58:35 -07:00
|
|
|
impl ::wasm_bindgen::convert::FromWasmAbi for #name {
|
|
|
|
type Abi = <::wasm_bindgen::JsValue as
|
|
|
|
::wasm_bindgen::convert::FromWasmAbi>::Abi;
|
2018-03-29 09:14:32 -07:00
|
|
|
|
2018-03-31 09:04:00 -07:00
|
|
|
unsafe fn from_abi(
|
|
|
|
js: Self::Abi,
|
|
|
|
extra: &mut ::wasm_bindgen::convert::Stack,
|
|
|
|
) -> Self {
|
2018-04-13 22:58:35 -07:00
|
|
|
#name {
|
|
|
|
obj: ::wasm_bindgen::JsValue::from_abi(js, extra),
|
|
|
|
}
|
2018-03-29 09:14:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-19 14:44:23 -05:00
|
|
|
impl ::wasm_bindgen::convert::OptionFromWasmAbi for #name {
|
|
|
|
fn is_none(abi: &Self::Abi) -> bool { *abi == 0 }
|
|
|
|
}
|
|
|
|
|
2018-04-13 22:58:35 -07:00
|
|
|
impl<'a> ::wasm_bindgen::convert::IntoWasmAbi for &'a #name {
|
|
|
|
type Abi = <&'a ::wasm_bindgen::JsValue as
|
|
|
|
::wasm_bindgen::convert::IntoWasmAbi>::Abi;
|
2018-03-31 09:04:00 -07:00
|
|
|
|
2018-04-13 22:58:35 -07:00
|
|
|
fn into_abi(self, extra: &mut ::wasm_bindgen::convert::Stack) -> Self::Abi {
|
|
|
|
(&self.obj).into_abi(extra)
|
2018-03-29 09:14:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-13 22:58:35 -07:00
|
|
|
impl ::wasm_bindgen::convert::RefFromWasmAbi for #name {
|
2018-03-31 09:04:00 -07:00
|
|
|
type Abi = <::wasm_bindgen::JsValue as
|
2018-04-13 22:58:35 -07:00
|
|
|
::wasm_bindgen::convert::RefFromWasmAbi>::Abi;
|
2018-04-19 13:08:54 -07:00
|
|
|
type Anchor = ::wasm_bindgen::__rt::core::mem::ManuallyDrop<#name>;
|
2018-03-31 09:04:00 -07:00
|
|
|
|
2018-04-13 22:58:35 -07:00
|
|
|
unsafe fn ref_from_abi(
|
2018-03-31 09:04:00 -07:00
|
|
|
js: Self::Abi,
|
|
|
|
extra: &mut ::wasm_bindgen::convert::Stack,
|
2018-04-13 22:58:35 -07:00
|
|
|
) -> Self::Anchor {
|
|
|
|
let tmp = <::wasm_bindgen::JsValue as ::wasm_bindgen::convert::RefFromWasmAbi>
|
|
|
|
::ref_from_abi(js, extra);
|
2018-04-19 13:08:54 -07:00
|
|
|
::wasm_bindgen::__rt::core::mem::ManuallyDrop::new(#name {
|
|
|
|
obj: ::wasm_bindgen::__rt::core::mem::ManuallyDrop::into_inner(tmp),
|
2018-04-13 22:58:35 -07:00
|
|
|
})
|
2018-03-29 09:14:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<::wasm_bindgen::JsValue> for #name {
|
|
|
|
fn from(obj: ::wasm_bindgen::JsValue) -> #name {
|
|
|
|
#name { obj }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<#name> for ::wasm_bindgen::JsValue {
|
|
|
|
fn from(obj: #name) -> ::wasm_bindgen::JsValue {
|
|
|
|
obj.obj
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).to_tokens(tokens);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-08 22:09:00 -04:00
|
|
|
impl ToTokens for ast::ImportEnum {
|
|
|
|
fn to_tokens(&self, tokens: &mut TokenStream) {
|
|
|
|
let vis = &self.vis;
|
|
|
|
let name = &self.name;
|
2018-07-23 11:04:28 -04:00
|
|
|
let expect_string = format!("attempted to convert invalid {} into JSValue", name);
|
2018-07-08 22:09:00 -04:00
|
|
|
let variants = &self.variants;
|
|
|
|
let variant_strings = &self.variant_values;
|
2018-07-23 11:04:28 -04:00
|
|
|
let attrs = &self.rust_attrs;
|
2018-07-08 22:09:00 -04:00
|
|
|
|
|
|
|
let mut current_idx: usize = 0;
|
|
|
|
let variant_indexes: Vec<Literal> = variants
|
|
|
|
.iter()
|
|
|
|
.map(|_| {
|
|
|
|
let this_index = current_idx;
|
|
|
|
current_idx += 1;
|
|
|
|
Literal::usize_unsuffixed(this_index)
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
// Borrow variant_indexes because we need to use it multiple times inside the quote! macro
|
|
|
|
let variant_indexes_ref = &variant_indexes;
|
|
|
|
|
|
|
|
// A vector of EnumName::VariantName tokens for this enum
|
|
|
|
let variant_paths: Vec<TokenStream> = self
|
|
|
|
.variants
|
|
|
|
.iter()
|
|
|
|
.map(|v| quote!(#name::#v).into_token_stream())
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
// Borrow variant_paths because we need to use it multiple times inside the quote! macro
|
|
|
|
let variant_paths_ref = &variant_paths;
|
|
|
|
|
|
|
|
(quote! {
|
|
|
|
#[allow(bad_style)]
|
2018-07-23 11:04:28 -04:00
|
|
|
#(#attrs)*
|
2018-07-08 22:09:00 -04:00
|
|
|
#vis enum #name {
|
|
|
|
#(#variants = #variant_indexes_ref,)*
|
2018-07-23 11:04:28 -04:00
|
|
|
#[doc(hidden)]
|
|
|
|
__Nonexhaustive,
|
2018-07-08 22:09:00 -04:00
|
|
|
}
|
|
|
|
|
2018-07-10 20:19:06 -04:00
|
|
|
impl #name {
|
2018-07-23 11:04:28 -04:00
|
|
|
#vis fn from_js_value(obj: &::wasm_bindgen::JsValue) -> Option<#name> {
|
2018-07-10 20:19:06 -04:00
|
|
|
obj.as_string().and_then(|obj_str| match obj_str.as_str() {
|
|
|
|
#(#variant_strings => Some(#variant_paths_ref),)*
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-08 22:09:00 -04:00
|
|
|
impl ::wasm_bindgen::describe::WasmDescribe for #name {
|
|
|
|
fn describe() {
|
|
|
|
::wasm_bindgen::JsValue::describe()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ::wasm_bindgen::convert::IntoWasmAbi for #name {
|
|
|
|
type Abi = <::wasm_bindgen::JsValue as
|
|
|
|
::wasm_bindgen::convert::IntoWasmAbi>::Abi;
|
|
|
|
|
|
|
|
fn into_abi(self, extra: &mut ::wasm_bindgen::convert::Stack) -> Self::Abi {
|
2018-07-10 20:23:09 -04:00
|
|
|
::wasm_bindgen::JsValue::from(self).into_abi(extra)
|
2018-07-08 22:09:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ::wasm_bindgen::convert::FromWasmAbi for #name {
|
|
|
|
type Abi = <::wasm_bindgen::JsValue as
|
|
|
|
::wasm_bindgen::convert::FromWasmAbi>::Abi;
|
|
|
|
|
|
|
|
unsafe fn from_abi(
|
|
|
|
js: Self::Abi,
|
|
|
|
extra: &mut ::wasm_bindgen::convert::Stack,
|
|
|
|
) -> Self {
|
2018-07-23 11:04:28 -04:00
|
|
|
#name::from_js_value(&::wasm_bindgen::JsValue::from_abi(js, extra)).unwrap_or(#name::__Nonexhaustive)
|
2018-07-08 22:09:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<#name> for ::wasm_bindgen::JsValue {
|
|
|
|
fn from(obj: #name) -> ::wasm_bindgen::JsValue {
|
|
|
|
match obj {
|
2018-07-23 11:04:28 -04:00
|
|
|
#(#variant_paths_ref => ::wasm_bindgen::JsValue::from_str(#variant_strings),)*
|
|
|
|
#name::__Nonexhaustive => panic!(#expect_string),
|
2018-07-08 22:09:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).to_tokens(tokens);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-29 09:14:32 -07:00
|
|
|
impl ToTokens for ast::ImportFunction {
|
2018-05-21 07:29:34 -07:00
|
|
|
fn to_tokens(&self, tokens: &mut TokenStream) {
|
2018-03-29 09:14:32 -07:00
|
|
|
let mut class_ty = None;
|
|
|
|
let mut is_method = false;
|
|
|
|
match self.kind {
|
2018-06-14 19:21:33 -07:00
|
|
|
ast::ImportFunctionKind::Method {
|
|
|
|
ref ty, ref kind, ..
|
|
|
|
} => {
|
2018-07-07 10:20:31 -07:00
|
|
|
if let ast::MethodKind::Operation(ast::Operation {
|
|
|
|
is_static: false, ..
|
|
|
|
}) = kind
|
|
|
|
{
|
2018-06-14 19:21:33 -07:00
|
|
|
is_method = true;
|
|
|
|
}
|
2018-03-29 09:14:32 -07:00
|
|
|
class_ty = Some(ty);
|
|
|
|
}
|
|
|
|
ast::ImportFunctionKind::Normal => {}
|
|
|
|
}
|
|
|
|
let vis = &self.function.rust_vis;
|
2018-06-14 02:03:52 -07:00
|
|
|
let ret = match &self.function.ret {
|
|
|
|
Some(ty) => quote! { -> #ty },
|
|
|
|
None => quote!(),
|
|
|
|
};
|
2018-03-29 09:14:32 -07:00
|
|
|
|
|
|
|
let mut abi_argument_names = Vec::new();
|
|
|
|
let mut abi_arguments = Vec::new();
|
|
|
|
let mut arg_conversions = Vec::new();
|
2018-06-28 20:06:35 -05:00
|
|
|
let mut arguments = Vec::new();
|
2018-05-21 07:29:34 -07:00
|
|
|
let ret_ident = Ident::new("_ret", Span::call_site());
|
2018-03-29 09:14:32 -07:00
|
|
|
|
2018-06-14 02:03:52 -07:00
|
|
|
for (i, syn::ArgCaptured { pat, ty, .. }) in self.function.arguments.iter().enumerate() {
|
|
|
|
let name = match pat {
|
|
|
|
syn::Pat::Ident(syn::PatIdent {
|
|
|
|
by_ref: None,
|
|
|
|
ident,
|
|
|
|
subpat: None,
|
|
|
|
..
|
|
|
|
}) => ident.clone(),
|
2018-07-07 10:20:31 -07:00
|
|
|
syn::Pat::Wild(_) => syn::Ident::new(&format!("__genarg_{}", i), Span::call_site()),
|
2018-06-26 13:17:40 +05:00
|
|
|
_ => panic!("unsupported pattern in foreign function"),
|
2018-06-14 02:03:52 -07:00
|
|
|
};
|
2018-03-29 09:14:32 -07:00
|
|
|
|
2018-05-21 07:29:34 -07:00
|
|
|
abi_argument_names.push(name.clone());
|
2018-04-13 22:58:35 -07:00
|
|
|
abi_arguments.push(quote! {
|
|
|
|
#name: <#ty as ::wasm_bindgen::convert::IntoWasmAbi>::Abi
|
|
|
|
});
|
|
|
|
let var = if i == 0 && is_method {
|
|
|
|
quote! { self }
|
|
|
|
} else {
|
2018-06-28 20:06:35 -05:00
|
|
|
arguments.push(quote! { #name: #ty });
|
2018-04-13 22:58:35 -07:00
|
|
|
quote! { #name }
|
|
|
|
};
|
|
|
|
arg_conversions.push(quote! {
|
|
|
|
let #name = <#ty as ::wasm_bindgen::convert::IntoWasmAbi>
|
|
|
|
::into_abi(#var, &mut __stack);
|
|
|
|
});
|
2018-03-29 09:14:32 -07:00
|
|
|
}
|
|
|
|
let abi_ret;
|
|
|
|
let mut convert_ret;
|
2018-06-14 02:03:52 -07:00
|
|
|
match &self.js_ret {
|
2018-04-13 22:58:35 -07:00
|
|
|
Some(syn::Type::Reference(_)) => {
|
|
|
|
panic!("cannot return references in imports yet");
|
|
|
|
}
|
|
|
|
Some(ref ty) => {
|
2018-04-03 07:07:14 -07:00
|
|
|
abi_ret = quote! {
|
2018-04-13 22:58:35 -07:00
|
|
|
<#ty as ::wasm_bindgen::convert::FromWasmAbi>::Abi
|
2018-03-29 09:14:32 -07:00
|
|
|
};
|
2018-04-03 07:07:14 -07:00
|
|
|
convert_ret = quote! {
|
2018-04-13 22:58:35 -07:00
|
|
|
<#ty as ::wasm_bindgen::convert::FromWasmAbi>
|
2018-03-31 08:26:20 -07:00
|
|
|
::from_abi(
|
|
|
|
#ret_ident,
|
|
|
|
&mut ::wasm_bindgen::convert::GlobalStack::new(),
|
|
|
|
)
|
2018-03-29 09:14:32 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
None => {
|
2018-04-03 07:07:14 -07:00
|
|
|
abi_ret = quote! { () };
|
|
|
|
convert_ret = quote! { () };
|
2018-03-29 09:14:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-14 02:03:52 -07:00
|
|
|
let mut exceptional_ret = quote!();
|
2018-07-07 10:20:31 -07:00
|
|
|
let exn_data = if self.catch {
|
2018-05-21 07:29:34 -07:00
|
|
|
let exn_data = Ident::new("exn_data", Span::call_site());
|
|
|
|
let exn_data_ptr = Ident::new("exn_data_ptr", Span::call_site());
|
|
|
|
abi_argument_names.push(exn_data_ptr.clone());
|
2018-04-03 07:07:14 -07:00
|
|
|
abi_arguments.push(quote! { #exn_data_ptr: *mut u32 });
|
|
|
|
convert_ret = quote! { Ok(#convert_ret) };
|
|
|
|
exceptional_ret = quote! {
|
2018-03-29 09:14:32 -07:00
|
|
|
if #exn_data[0] == 1 {
|
2018-03-31 08:26:20 -07:00
|
|
|
return Err(
|
|
|
|
<
|
2018-04-13 22:58:35 -07:00
|
|
|
::wasm_bindgen::JsValue as ::wasm_bindgen::convert::FromWasmAbi
|
|
|
|
>::from_abi(#exn_data[1], &mut ::wasm_bindgen::convert::GlobalStack::new())
|
2018-03-31 08:26:20 -07:00
|
|
|
)
|
2018-03-29 09:14:32 -07:00
|
|
|
}
|
|
|
|
};
|
2018-04-03 07:07:14 -07:00
|
|
|
quote! {
|
2018-03-31 08:26:20 -07:00
|
|
|
let mut #exn_data = [0; 2];
|
|
|
|
let #exn_data_ptr = #exn_data.as_mut_ptr();
|
|
|
|
}
|
|
|
|
} else {
|
2018-06-14 02:03:52 -07:00
|
|
|
quote!()
|
2018-03-31 08:26:20 -07:00
|
|
|
};
|
2018-03-29 09:14:32 -07:00
|
|
|
|
2018-05-21 07:29:34 -07:00
|
|
|
let rust_name = &self.rust_name;
|
|
|
|
let import_name = &self.shim;
|
2018-03-29 09:14:32 -07:00
|
|
|
let attrs = &self.function.rust_attrs;
|
2018-06-28 20:06:35 -05:00
|
|
|
let arguments = &arguments;
|
2018-03-29 09:14:32 -07:00
|
|
|
|
2018-07-29 17:12:36 +01:00
|
|
|
let doc_comment = match &self.doc_comment {
|
|
|
|
None => "",
|
|
|
|
Some(doc_string) => doc_string,
|
|
|
|
};
|
2018-03-29 09:14:32 -07:00
|
|
|
let me = if is_method {
|
2018-04-03 07:07:14 -07:00
|
|
|
quote! { &self, }
|
2018-03-29 09:14:32 -07:00
|
|
|
} else {
|
|
|
|
quote!()
|
|
|
|
};
|
|
|
|
|
2018-04-03 07:07:14 -07:00
|
|
|
let invocation = quote! {
|
2018-03-29 09:14:32 -07:00
|
|
|
#(#attrs)*
|
|
|
|
#[allow(bad_style)]
|
2018-04-27 13:43:47 -07:00
|
|
|
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
|
2018-07-29 17:12:36 +01:00
|
|
|
#[doc = #doc_comment]
|
2018-07-07 12:20:42 -05:00
|
|
|
#vis fn #rust_name(#me #(#arguments),*) #ret {
|
2018-07-14 11:04:47 -05:00
|
|
|
// See definition of `link_mem_intrinsics` for what this is doing
|
|
|
|
::wasm_bindgen::__rt::link_mem_intrinsics();
|
2018-07-21 19:00:20 -07:00
|
|
|
#[link(wasm_import_module = "__wbindgen_placeholder__")]
|
2018-03-29 09:14:32 -07:00
|
|
|
extern {
|
|
|
|
fn #import_name(#(#abi_arguments),*) -> #abi_ret;
|
|
|
|
}
|
|
|
|
unsafe {
|
2018-03-31 08:26:20 -07:00
|
|
|
#exn_data
|
|
|
|
let #ret_ident = {
|
|
|
|
let mut __stack = ::wasm_bindgen::convert::GlobalStack::new();
|
|
|
|
#(#arg_conversions)*
|
|
|
|
#import_name(#(#abi_argument_names),*)
|
|
|
|
};
|
2018-03-29 09:14:32 -07:00
|
|
|
#exceptional_ret
|
|
|
|
#convert_ret
|
|
|
|
}
|
|
|
|
}
|
Overhaul how type information gets to the CLI
This commit is a complete overhaul of how the `#[wasm_bindgen]` macro
communicates type information to the CLI tool, and it's done in a somewhat...
unconventional fashion.
Today we've got a problem where the generated JS needs to understand the types
of each function exported or imported. This understanding is what enables it to
generate the appropriate JS wrappers and such. We want to, however, be quite
flexible and extensible in types that are supported across the boundary, which
means that internally we rely on the trait system to resolve what's what.
Communicating the type information historically was done by creating a four byte
"descriptor" and using associated type projections to communicate that to the
CLI tool. Unfortunately four bytes isn't a lot of space to cram information like
arguments to a generic function, tuple types, etc. In general this just wasn't
flexible enough and the way custom references were treated was also already a
bit of a hack.
This commit takes a radical step of creating a **descriptor function** for each
function imported/exported. The really crazy part is that the `wasm-bindgen` CLI
tool now embeds a wasm interpreter and executes these functions when the CLI
tool is invoked. By allowing arbitrary functions to get executed it's now *much*
easier to inform `wasm-bindgen` about complicated structures of types. Rest
assured though that all these descriptor functions are automatically unexported
and gc'd away, so this should not have any impact on binary sizes
A new internal trait, `WasmDescribe`, is added to represent a description of all
types, sort of like a serialization of the structure of a type that
`wasm-bindgen` can understand. This works by calling a special exported function
with a `u32` value a bunch of times. This means that when we run a descriptor we
effectively get a `Vec<u32>` in the `wasm-bindgen` CLI tool. This list of
integers can then be parsed into a rich `enum` for the JS generation to work
with.
This commit currently only retains feature parity with the previous
implementation. I hope to soon solve issues like #123, #104, and #111 with this
support.
2018-04-13 07:33:46 -07:00
|
|
|
|
2018-04-27 13:43:47 -07:00
|
|
|
#(#attrs)*
|
|
|
|
#[allow(bad_style, unused_variables)]
|
|
|
|
#[cfg(not(all(target_arch = "wasm32", not(target_os = "emscripten"))))]
|
2018-07-29 17:12:36 +01:00
|
|
|
#[doc = #doc_comment]
|
2018-07-09 08:13:03 -07:00
|
|
|
#vis fn #rust_name(#me #(#arguments),*) #ret {
|
2018-04-27 13:43:47 -07:00
|
|
|
panic!("cannot call wasm-bindgen imported functions on \
|
|
|
|
non-wasm targets");
|
|
|
|
}
|
|
|
|
|
2018-03-29 09:14:32 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(class) = class_ty {
|
|
|
|
(quote! {
|
|
|
|
impl #class {
|
|
|
|
#invocation
|
|
|
|
}
|
|
|
|
}).to_tokens(tokens);
|
|
|
|
} else {
|
|
|
|
invocation.to_tokens(tokens);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Overhaul how type information gets to the CLI
This commit is a complete overhaul of how the `#[wasm_bindgen]` macro
communicates type information to the CLI tool, and it's done in a somewhat...
unconventional fashion.
Today we've got a problem where the generated JS needs to understand the types
of each function exported or imported. This understanding is what enables it to
generate the appropriate JS wrappers and such. We want to, however, be quite
flexible and extensible in types that are supported across the boundary, which
means that internally we rely on the trait system to resolve what's what.
Communicating the type information historically was done by creating a four byte
"descriptor" and using associated type projections to communicate that to the
CLI tool. Unfortunately four bytes isn't a lot of space to cram information like
arguments to a generic function, tuple types, etc. In general this just wasn't
flexible enough and the way custom references were treated was also already a
bit of a hack.
This commit takes a radical step of creating a **descriptor function** for each
function imported/exported. The really crazy part is that the `wasm-bindgen` CLI
tool now embeds a wasm interpreter and executes these functions when the CLI
tool is invoked. By allowing arbitrary functions to get executed it's now *much*
easier to inform `wasm-bindgen` about complicated structures of types. Rest
assured though that all these descriptor functions are automatically unexported
and gc'd away, so this should not have any impact on binary sizes
A new internal trait, `WasmDescribe`, is added to represent a description of all
types, sort of like a serialization of the structure of a type that
`wasm-bindgen` can understand. This works by calling a special exported function
with a `u32` value a bunch of times. This means that when we run a descriptor we
effectively get a `Vec<u32>` in the `wasm-bindgen` CLI tool. This list of
integers can then be parsed into a rich `enum` for the JS generation to work
with.
This commit currently only retains feature parity with the previous
implementation. I hope to soon solve issues like #123, #104, and #111 with this
support.
2018-04-13 07:33:46 -07:00
|
|
|
// See comment above in ast::Export for what's going on here.
|
|
|
|
struct DescribeImport<'a>(&'a ast::ImportKind);
|
|
|
|
|
|
|
|
impl<'a> ToTokens for DescribeImport<'a> {
|
2018-05-21 07:29:34 -07:00
|
|
|
fn to_tokens(&self, tokens: &mut TokenStream) {
|
Overhaul how type information gets to the CLI
This commit is a complete overhaul of how the `#[wasm_bindgen]` macro
communicates type information to the CLI tool, and it's done in a somewhat...
unconventional fashion.
Today we've got a problem where the generated JS needs to understand the types
of each function exported or imported. This understanding is what enables it to
generate the appropriate JS wrappers and such. We want to, however, be quite
flexible and extensible in types that are supported across the boundary, which
means that internally we rely on the trait system to resolve what's what.
Communicating the type information historically was done by creating a four byte
"descriptor" and using associated type projections to communicate that to the
CLI tool. Unfortunately four bytes isn't a lot of space to cram information like
arguments to a generic function, tuple types, etc. In general this just wasn't
flexible enough and the way custom references were treated was also already a
bit of a hack.
This commit takes a radical step of creating a **descriptor function** for each
function imported/exported. The really crazy part is that the `wasm-bindgen` CLI
tool now embeds a wasm interpreter and executes these functions when the CLI
tool is invoked. By allowing arbitrary functions to get executed it's now *much*
easier to inform `wasm-bindgen` about complicated structures of types. Rest
assured though that all these descriptor functions are automatically unexported
and gc'd away, so this should not have any impact on binary sizes
A new internal trait, `WasmDescribe`, is added to represent a description of all
types, sort of like a serialization of the structure of a type that
`wasm-bindgen` can understand. This works by calling a special exported function
with a `u32` value a bunch of times. This means that when we run a descriptor we
effectively get a `Vec<u32>` in the `wasm-bindgen` CLI tool. This list of
integers can then be parsed into a rich `enum` for the JS generation to work
with.
This commit currently only retains feature parity with the previous
implementation. I hope to soon solve issues like #123, #104, and #111 with this
support.
2018-04-13 07:33:46 -07:00
|
|
|
let f = match *self.0 {
|
|
|
|
ast::ImportKind::Function(ref f) => f,
|
|
|
|
ast::ImportKind::Static(_) => return,
|
|
|
|
ast::ImportKind::Type(_) => return,
|
2018-07-08 22:09:00 -04:00
|
|
|
ast::ImportKind::Enum(_) => return,
|
Overhaul how type information gets to the CLI
This commit is a complete overhaul of how the `#[wasm_bindgen]` macro
communicates type information to the CLI tool, and it's done in a somewhat...
unconventional fashion.
Today we've got a problem where the generated JS needs to understand the types
of each function exported or imported. This understanding is what enables it to
generate the appropriate JS wrappers and such. We want to, however, be quite
flexible and extensible in types that are supported across the boundary, which
means that internally we rely on the trait system to resolve what's what.
Communicating the type information historically was done by creating a four byte
"descriptor" and using associated type projections to communicate that to the
CLI tool. Unfortunately four bytes isn't a lot of space to cram information like
arguments to a generic function, tuple types, etc. In general this just wasn't
flexible enough and the way custom references were treated was also already a
bit of a hack.
This commit takes a radical step of creating a **descriptor function** for each
function imported/exported. The really crazy part is that the `wasm-bindgen` CLI
tool now embeds a wasm interpreter and executes these functions when the CLI
tool is invoked. By allowing arbitrary functions to get executed it's now *much*
easier to inform `wasm-bindgen` about complicated structures of types. Rest
assured though that all these descriptor functions are automatically unexported
and gc'd away, so this should not have any impact on binary sizes
A new internal trait, `WasmDescribe`, is added to represent a description of all
types, sort of like a serialization of the structure of a type that
`wasm-bindgen` can understand. This works by calling a special exported function
with a `u32` value a bunch of times. This means that when we run a descriptor we
effectively get a `Vec<u32>` in the `wasm-bindgen` CLI tool. This list of
integers can then be parsed into a rich `enum` for the JS generation to work
with.
This commit currently only retains feature parity with the previous
implementation. I hope to soon solve issues like #123, #104, and #111 with this
support.
2018-04-13 07:33:46 -07:00
|
|
|
};
|
|
|
|
let describe_name = format!("__wbindgen_describe_{}", f.shim);
|
2018-05-21 07:29:34 -07:00
|
|
|
let describe_name = Ident::new(&describe_name, Span::call_site());
|
2018-06-14 02:03:52 -07:00
|
|
|
let argtys = f.function.arguments.iter().map(|arg| &arg.ty);
|
Overhaul how type information gets to the CLI
This commit is a complete overhaul of how the `#[wasm_bindgen]` macro
communicates type information to the CLI tool, and it's done in a somewhat...
unconventional fashion.
Today we've got a problem where the generated JS needs to understand the types
of each function exported or imported. This understanding is what enables it to
generate the appropriate JS wrappers and such. We want to, however, be quite
flexible and extensible in types that are supported across the boundary, which
means that internally we rely on the trait system to resolve what's what.
Communicating the type information historically was done by creating a four byte
"descriptor" and using associated type projections to communicate that to the
CLI tool. Unfortunately four bytes isn't a lot of space to cram information like
arguments to a generic function, tuple types, etc. In general this just wasn't
flexible enough and the way custom references were treated was also already a
bit of a hack.
This commit takes a radical step of creating a **descriptor function** for each
function imported/exported. The really crazy part is that the `wasm-bindgen` CLI
tool now embeds a wasm interpreter and executes these functions when the CLI
tool is invoked. By allowing arbitrary functions to get executed it's now *much*
easier to inform `wasm-bindgen` about complicated structures of types. Rest
assured though that all these descriptor functions are automatically unexported
and gc'd away, so this should not have any impact on binary sizes
A new internal trait, `WasmDescribe`, is added to represent a description of all
types, sort of like a serialization of the structure of a type that
`wasm-bindgen` can understand. This works by calling a special exported function
with a `u32` value a bunch of times. This means that when we run a descriptor we
effectively get a `Vec<u32>` in the `wasm-bindgen` CLI tool. This list of
integers can then be parsed into a rich `enum` for the JS generation to work
with.
This commit currently only retains feature parity with the previous
implementation. I hope to soon solve issues like #123, #104, and #111 with this
support.
2018-04-13 07:33:46 -07:00
|
|
|
let nargs = f.function.arguments.len() as u32;
|
2018-06-14 02:03:52 -07:00
|
|
|
let inform_ret = match &f.js_ret {
|
Overhaul how type information gets to the CLI
This commit is a complete overhaul of how the `#[wasm_bindgen]` macro
communicates type information to the CLI tool, and it's done in a somewhat...
unconventional fashion.
Today we've got a problem where the generated JS needs to understand the types
of each function exported or imported. This understanding is what enables it to
generate the appropriate JS wrappers and such. We want to, however, be quite
flexible and extensible in types that are supported across the boundary, which
means that internally we rely on the trait system to resolve what's what.
Communicating the type information historically was done by creating a four byte
"descriptor" and using associated type projections to communicate that to the
CLI tool. Unfortunately four bytes isn't a lot of space to cram information like
arguments to a generic function, tuple types, etc. In general this just wasn't
flexible enough and the way custom references were treated was also already a
bit of a hack.
This commit takes a radical step of creating a **descriptor function** for each
function imported/exported. The really crazy part is that the `wasm-bindgen` CLI
tool now embeds a wasm interpreter and executes these functions when the CLI
tool is invoked. By allowing arbitrary functions to get executed it's now *much*
easier to inform `wasm-bindgen` about complicated structures of types. Rest
assured though that all these descriptor functions are automatically unexported
and gc'd away, so this should not have any impact on binary sizes
A new internal trait, `WasmDescribe`, is added to represent a description of all
types, sort of like a serialization of the structure of a type that
`wasm-bindgen` can understand. This works by calling a special exported function
with a `u32` value a bunch of times. This means that when we run a descriptor we
effectively get a `Vec<u32>` in the `wasm-bindgen` CLI tool. This list of
integers can then be parsed into a rich `enum` for the JS generation to work
with.
This commit currently only retains feature parity with the previous
implementation. I hope to soon solve issues like #123, #104, and #111 with this
support.
2018-04-13 07:33:46 -07:00
|
|
|
Some(ref t) => quote! { inform(1); <#t as WasmDescribe>::describe(); },
|
|
|
|
None => quote! { inform(0); },
|
|
|
|
};
|
|
|
|
(quote! {
|
|
|
|
#[no_mangle]
|
|
|
|
#[allow(non_snake_case)]
|
2018-06-18 15:30:29 -07:00
|
|
|
#[doc(hidden)]
|
Overhaul how type information gets to the CLI
This commit is a complete overhaul of how the `#[wasm_bindgen]` macro
communicates type information to the CLI tool, and it's done in a somewhat...
unconventional fashion.
Today we've got a problem where the generated JS needs to understand the types
of each function exported or imported. This understanding is what enables it to
generate the appropriate JS wrappers and such. We want to, however, be quite
flexible and extensible in types that are supported across the boundary, which
means that internally we rely on the trait system to resolve what's what.
Communicating the type information historically was done by creating a four byte
"descriptor" and using associated type projections to communicate that to the
CLI tool. Unfortunately four bytes isn't a lot of space to cram information like
arguments to a generic function, tuple types, etc. In general this just wasn't
flexible enough and the way custom references were treated was also already a
bit of a hack.
This commit takes a radical step of creating a **descriptor function** for each
function imported/exported. The really crazy part is that the `wasm-bindgen` CLI
tool now embeds a wasm interpreter and executes these functions when the CLI
tool is invoked. By allowing arbitrary functions to get executed it's now *much*
easier to inform `wasm-bindgen` about complicated structures of types. Rest
assured though that all these descriptor functions are automatically unexported
and gc'd away, so this should not have any impact on binary sizes
A new internal trait, `WasmDescribe`, is added to represent a description of all
types, sort of like a serialization of the structure of a type that
`wasm-bindgen` can understand. This works by calling a special exported function
with a `u32` value a bunch of times. This means that when we run a descriptor we
effectively get a `Vec<u32>` in the `wasm-bindgen` CLI tool. This list of
integers can then be parsed into a rich `enum` for the JS generation to work
with.
This commit currently only retains feature parity with the previous
implementation. I hope to soon solve issues like #123, #104, and #111 with this
support.
2018-04-13 07:33:46 -07:00
|
|
|
pub extern fn #describe_name() {
|
|
|
|
use wasm_bindgen::describe::*;
|
|
|
|
inform(FUNCTION);
|
|
|
|
inform(#nargs);
|
|
|
|
#(<#argtys as WasmDescribe>::describe();)*
|
|
|
|
#inform_ret
|
|
|
|
}
|
|
|
|
}).to_tokens(tokens);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-29 09:14:32 -07:00
|
|
|
impl ToTokens for ast::Enum {
|
2018-05-21 07:29:34 -07:00
|
|
|
fn to_tokens(&self, into: &mut TokenStream) {
|
2018-03-29 09:14:32 -07:00
|
|
|
let enum_name = &self.name;
|
|
|
|
let cast_clauses = self.variants.iter().map(|variant| {
|
|
|
|
let variant_name = &variant.name;
|
|
|
|
quote! {
|
Overhaul how type information gets to the CLI
This commit is a complete overhaul of how the `#[wasm_bindgen]` macro
communicates type information to the CLI tool, and it's done in a somewhat...
unconventional fashion.
Today we've got a problem where the generated JS needs to understand the types
of each function exported or imported. This understanding is what enables it to
generate the appropriate JS wrappers and such. We want to, however, be quite
flexible and extensible in types that are supported across the boundary, which
means that internally we rely on the trait system to resolve what's what.
Communicating the type information historically was done by creating a four byte
"descriptor" and using associated type projections to communicate that to the
CLI tool. Unfortunately four bytes isn't a lot of space to cram information like
arguments to a generic function, tuple types, etc. In general this just wasn't
flexible enough and the way custom references were treated was also already a
bit of a hack.
This commit takes a radical step of creating a **descriptor function** for each
function imported/exported. The really crazy part is that the `wasm-bindgen` CLI
tool now embeds a wasm interpreter and executes these functions when the CLI
tool is invoked. By allowing arbitrary functions to get executed it's now *much*
easier to inform `wasm-bindgen` about complicated structures of types. Rest
assured though that all these descriptor functions are automatically unexported
and gc'd away, so this should not have any impact on binary sizes
A new internal trait, `WasmDescribe`, is added to represent a description of all
types, sort of like a serialization of the structure of a type that
`wasm-bindgen` can understand. This works by calling a special exported function
with a `u32` value a bunch of times. This means that when we run a descriptor we
effectively get a `Vec<u32>` in the `wasm-bindgen` CLI tool. This list of
integers can then be parsed into a rich `enum` for the JS generation to work
with.
This commit currently only retains feature parity with the previous
implementation. I hope to soon solve issues like #123, #104, and #111 with this
support.
2018-04-13 07:33:46 -07:00
|
|
|
if js == #enum_name::#variant_name as u32 {
|
2018-03-29 09:14:32 -07:00
|
|
|
#enum_name::#variant_name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2018-04-03 07:07:14 -07:00
|
|
|
(quote! {
|
2018-04-13 22:58:35 -07:00
|
|
|
impl ::wasm_bindgen::convert::IntoWasmAbi for #enum_name {
|
2018-03-31 07:57:47 -07:00
|
|
|
type Abi = u32;
|
2018-03-29 09:14:32 -07:00
|
|
|
|
2018-03-31 09:04:00 -07:00
|
|
|
fn into_abi(self, _extra: &mut ::wasm_bindgen::convert::Stack) -> u32 {
|
2018-03-29 09:14:32 -07:00
|
|
|
self as u32
|
|
|
|
}
|
2018-04-13 22:58:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ::wasm_bindgen::convert::FromWasmAbi for #enum_name {
|
|
|
|
type Abi = u32;
|
2018-03-29 09:14:32 -07:00
|
|
|
|
2018-03-31 09:04:00 -07:00
|
|
|
unsafe fn from_abi(
|
|
|
|
js: u32,
|
|
|
|
_extra: &mut ::wasm_bindgen::convert::Stack,
|
|
|
|
) -> Self {
|
Overhaul how type information gets to the CLI
This commit is a complete overhaul of how the `#[wasm_bindgen]` macro
communicates type information to the CLI tool, and it's done in a somewhat...
unconventional fashion.
Today we've got a problem where the generated JS needs to understand the types
of each function exported or imported. This understanding is what enables it to
generate the appropriate JS wrappers and such. We want to, however, be quite
flexible and extensible in types that are supported across the boundary, which
means that internally we rely on the trait system to resolve what's what.
Communicating the type information historically was done by creating a four byte
"descriptor" and using associated type projections to communicate that to the
CLI tool. Unfortunately four bytes isn't a lot of space to cram information like
arguments to a generic function, tuple types, etc. In general this just wasn't
flexible enough and the way custom references were treated was also already a
bit of a hack.
This commit takes a radical step of creating a **descriptor function** for each
function imported/exported. The really crazy part is that the `wasm-bindgen` CLI
tool now embeds a wasm interpreter and executes these functions when the CLI
tool is invoked. By allowing arbitrary functions to get executed it's now *much*
easier to inform `wasm-bindgen` about complicated structures of types. Rest
assured though that all these descriptor functions are automatically unexported
and gc'd away, so this should not have any impact on binary sizes
A new internal trait, `WasmDescribe`, is added to represent a description of all
types, sort of like a serialization of the structure of a type that
`wasm-bindgen` can understand. This works by calling a special exported function
with a `u32` value a bunch of times. This means that when we run a descriptor we
effectively get a `Vec<u32>` in the `wasm-bindgen` CLI tool. This list of
integers can then be parsed into a rich `enum` for the JS generation to work
with.
This commit currently only retains feature parity with the previous
implementation. I hope to soon solve issues like #123, #104, and #111 with this
support.
2018-04-13 07:33:46 -07:00
|
|
|
#(#cast_clauses else)* {
|
2018-05-02 10:16:40 -07:00
|
|
|
::wasm_bindgen::throw("invalid enum value passed")
|
Overhaul how type information gets to the CLI
This commit is a complete overhaul of how the `#[wasm_bindgen]` macro
communicates type information to the CLI tool, and it's done in a somewhat...
unconventional fashion.
Today we've got a problem where the generated JS needs to understand the types
of each function exported or imported. This understanding is what enables it to
generate the appropriate JS wrappers and such. We want to, however, be quite
flexible and extensible in types that are supported across the boundary, which
means that internally we rely on the trait system to resolve what's what.
Communicating the type information historically was done by creating a four byte
"descriptor" and using associated type projections to communicate that to the
CLI tool. Unfortunately four bytes isn't a lot of space to cram information like
arguments to a generic function, tuple types, etc. In general this just wasn't
flexible enough and the way custom references were treated was also already a
bit of a hack.
This commit takes a radical step of creating a **descriptor function** for each
function imported/exported. The really crazy part is that the `wasm-bindgen` CLI
tool now embeds a wasm interpreter and executes these functions when the CLI
tool is invoked. By allowing arbitrary functions to get executed it's now *much*
easier to inform `wasm-bindgen` about complicated structures of types. Rest
assured though that all these descriptor functions are automatically unexported
and gc'd away, so this should not have any impact on binary sizes
A new internal trait, `WasmDescribe`, is added to represent a description of all
types, sort of like a serialization of the structure of a type that
`wasm-bindgen` can understand. This works by calling a special exported function
with a `u32` value a bunch of times. This means that when we run a descriptor we
effectively get a `Vec<u32>` in the `wasm-bindgen` CLI tool. This list of
integers can then be parsed into a rich `enum` for the JS generation to work
with.
This commit currently only retains feature parity with the previous
implementation. I hope to soon solve issues like #123, #104, and #111 with this
support.
2018-04-13 07:33:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ::wasm_bindgen::describe::WasmDescribe for #enum_name {
|
|
|
|
fn describe() {
|
|
|
|
use wasm_bindgen::describe::*;
|
|
|
|
inform(ENUM);
|
2018-03-29 09:14:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}).to_tokens(into);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToTokens for ast::ImportStatic {
|
2018-05-21 07:29:34 -07:00
|
|
|
fn to_tokens(&self, into: &mut TokenStream) {
|
|
|
|
let name = &self.rust_name;
|
2018-03-29 09:14:32 -07:00
|
|
|
let ty = &self.ty;
|
2018-05-21 07:29:34 -07:00
|
|
|
let shim_name = &self.shim;
|
2018-03-29 09:14:32 -07:00
|
|
|
let vis = &self.vis;
|
2018-04-03 07:07:14 -07:00
|
|
|
(quote! {
|
2018-03-29 09:14:32 -07:00
|
|
|
#[allow(bad_style)]
|
|
|
|
#vis static #name: ::wasm_bindgen::JsStatic<#ty> = {
|
2018-04-27 13:43:47 -07:00
|
|
|
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
|
2018-03-29 09:14:32 -07:00
|
|
|
fn init() -> #ty {
|
2018-07-21 19:00:20 -07:00
|
|
|
#[link(wasm_import_module = "__wbindgen_placeholder__")]
|
2018-03-29 09:14:32 -07:00
|
|
|
extern {
|
2018-04-13 22:58:35 -07:00
|
|
|
fn #shim_name() -> <#ty as ::wasm_bindgen::convert::FromWasmAbi>::Abi;
|
2018-03-29 09:14:32 -07:00
|
|
|
}
|
|
|
|
unsafe {
|
2018-04-13 22:58:35 -07:00
|
|
|
<#ty as ::wasm_bindgen::convert::FromWasmAbi>::from_abi(
|
2018-03-31 08:26:20 -07:00
|
|
|
#shim_name(),
|
|
|
|
&mut ::wasm_bindgen::convert::GlobalStack::new(),
|
|
|
|
)
|
2018-04-13 22:58:35 -07:00
|
|
|
|
2018-03-29 09:14:32 -07:00
|
|
|
}
|
|
|
|
}
|
2018-04-27 13:43:47 -07:00
|
|
|
#[cfg(not(all(target_arch = "wasm32", not(target_os = "emscripten"))))]
|
|
|
|
fn init() -> #ty {
|
|
|
|
panic!("cannot access imported statics on non-wasm targets")
|
|
|
|
}
|
2018-04-21 13:14:33 -07:00
|
|
|
static mut _VAL: ::wasm_bindgen::__rt::core::cell::UnsafeCell<Option<#ty>> =
|
|
|
|
::wasm_bindgen::__rt::core::cell::UnsafeCell::new(None);
|
2018-03-29 09:14:32 -07:00
|
|
|
::wasm_bindgen::JsStatic {
|
2018-04-21 13:14:33 -07:00
|
|
|
__inner: unsafe { &_VAL },
|
2018-03-29 09:14:32 -07:00
|
|
|
__init: init,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}).to_tokens(into);
|
|
|
|
}
|
|
|
|
}
|
2018-06-11 18:35:20 -07:00
|
|
|
|
|
|
|
impl ToTokens for ast::TypeAlias {
|
|
|
|
fn to_tokens(&self, into: &mut TokenStream) {
|
|
|
|
let vis = &self.vis;
|
|
|
|
let dest = &self.dest;
|
|
|
|
let src = &self.src;
|
|
|
|
(quote! {
|
|
|
|
#[allow(non_camel_case_types)]
|
|
|
|
#vis type #dest = #src;
|
|
|
|
}).to_tokens(into);
|
|
|
|
}
|
|
|
|
}
|
2018-07-13 04:16:19 +02:00
|
|
|
|
|
|
|
impl ToTokens for ast::Const {
|
|
|
|
fn to_tokens(&self, tokens: &mut TokenStream) {
|
|
|
|
use ast::ConstValue::*;
|
|
|
|
|
|
|
|
let vis = &self.vis;
|
|
|
|
let name = &self.name;
|
|
|
|
let ty = &self.ty;
|
|
|
|
|
|
|
|
let value: TokenStream = match self.value {
|
|
|
|
BooleanLiteral(false) => quote!(false),
|
|
|
|
BooleanLiteral(true) => quote!(true),
|
|
|
|
// the actual type is unknown because of typedefs
|
|
|
|
// so we cannot use std::fxx::INFINITY
|
|
|
|
// but we can use type inference
|
|
|
|
FloatLiteral(f) if f.is_infinite() && f.is_sign_positive() => quote!(1.0 / 0.0),
|
|
|
|
FloatLiteral(f) if f.is_infinite() && f.is_sign_negative() => quote!(-1.0 / 0.0),
|
|
|
|
FloatLiteral(f) if f.is_nan() => quote!(0.0 / 0.0),
|
|
|
|
// again no suffix
|
|
|
|
// panics on +-inf, nan
|
|
|
|
FloatLiteral(f) => {
|
|
|
|
let f = Literal::f64_unsuffixed(f);
|
|
|
|
quote!(#f)
|
2018-07-14 14:48:04 +02:00
|
|
|
},
|
|
|
|
SignedIntegerLiteral(i) => {
|
2018-07-13 04:16:19 +02:00
|
|
|
let i = Literal::i64_unsuffixed(i);
|
|
|
|
quote!(#i)
|
2018-07-14 14:48:04 +02:00
|
|
|
},
|
|
|
|
UnsignedIntegerLiteral(i) => {
|
|
|
|
let i = Literal::u64_unsuffixed(i);
|
|
|
|
quote!(#i)
|
|
|
|
},
|
2018-07-13 04:16:19 +02:00
|
|
|
Null => unimplemented!(),
|
|
|
|
};
|
2018-07-13 22:36:51 -07:00
|
|
|
|
|
|
|
let declaration = quote!(#vis const #name: #ty = #value;);
|
|
|
|
|
|
|
|
if let Some(class) = &self.class {
|
|
|
|
(quote! {
|
|
|
|
impl #class {
|
|
|
|
#declaration
|
|
|
|
}
|
|
|
|
}).to_tokens(tokens);
|
|
|
|
} else {
|
|
|
|
declaration.to_tokens(tokens);
|
|
|
|
}
|
2018-07-13 04:16:19 +02:00
|
|
|
}
|
|
|
|
}
|