mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-04-21 04:02:14 +00:00
Start removal of vector special-casing
This commit starts wasm-bindgen down a path of removing the special casing it currently has around vectors, slices, and strings. This has long been a thorn in wasm-bindgen's side as it doesn't handle other kinds of vectors and otherwise is very inflexible with future additions. Additionally it leads to a lot of duplicated-ish code throughout various portions of codegen. The fundamental reason for this was that two arguments were required to be passed back to wasm, and I couldn't figure out a way to shove both those arguments into a function argument. The new strategy here is that there is one global stack well known to both JS and Rust which arguments *may* also be transferred between. By default all ABI arguments pass as literal function arguments, but if two or more arguments need to be passed then the extra ones are all passed through this global stack. The stack is effectively temporary scratch space when crossing the JS/Rust boundary (both ways). No long term storage is intended here. The `simple` test is passing as a result of this commit, using strings internally. The `Vector` type in the AST has been removed (yay!) and the bulk of the implementation of slices and vectors now resides in the `wasm-bindgen` crate itself, defining how to pass all these arguments around. The JS generator, however, still needs to know about all the sorts of vectors so it can generate appropriate code for JS. Future commits will continue cleanup and get the rest of the tests working.
This commit is contained in:
parent
25af16c7d9
commit
cdbb31f3a9
@ -82,9 +82,6 @@ pub struct Variant {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub enum Type {
|
pub enum Type {
|
||||||
// special
|
|
||||||
Vector(VectorType, bool),
|
|
||||||
|
|
||||||
ByRef(syn::Type),
|
ByRef(syn::Type),
|
||||||
ByMutRef(syn::Type),
|
ByMutRef(syn::Type),
|
||||||
ByValue(syn::Type),
|
ByValue(syn::Type),
|
||||||
@ -320,7 +317,6 @@ impl Program {
|
|||||||
let class = match *class {
|
let class = match *class {
|
||||||
Type::ByRef(ref t) | Type::ByValue(ref t) => t,
|
Type::ByRef(ref t) | Type::ByValue(ref t) => t,
|
||||||
Type::ByMutRef(_) => panic!("first method argument cannot be mutable ref"),
|
Type::ByMutRef(_) => panic!("first method argument cannot be mutable ref"),
|
||||||
Type::Vector(..) => panic!("method receivers cannot be vectors"),
|
|
||||||
};
|
};
|
||||||
let class_name = match *class {
|
let class_name = match *class {
|
||||||
syn::Type::Path(syn::TypePath {
|
syn::Type::Path(syn::TypePath {
|
||||||
@ -505,61 +501,13 @@ pub fn extract_path_ident(path: &syn::Path) -> Option<syn::Ident> {
|
|||||||
|
|
||||||
impl Type {
|
impl Type {
|
||||||
pub fn from(ty: &syn::Type) -> Type {
|
pub fn from(ty: &syn::Type) -> Type {
|
||||||
match *ty {
|
if let syn::Type::Reference(ref r) = *ty {
|
||||||
syn::Type::Reference(ref r) => {
|
|
||||||
match *r.elem {
|
|
||||||
syn::Type::Path(syn::TypePath {
|
|
||||||
qself: None,
|
|
||||||
ref path,
|
|
||||||
}) => {
|
|
||||||
let ident = extract_path_ident(path);
|
|
||||||
match ident.as_ref().map(|s| s.as_ref()) {
|
|
||||||
Some("str") => return Type::Vector(VectorType::String, false),
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
syn::Type::Slice(ref slice) => {
|
|
||||||
if let Some(ty) = VectorType::from(&slice.elem) {
|
|
||||||
return Type::Vector(ty, false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
return if r.mutability.is_some() {
|
return if r.mutability.is_some() {
|
||||||
Type::ByMutRef((*r.elem).clone())
|
Type::ByMutRef((*r.elem).clone())
|
||||||
} else {
|
} else {
|
||||||
Type::ByRef((*r.elem).clone())
|
Type::ByRef((*r.elem).clone())
|
||||||
};
|
|
||||||
}
|
|
||||||
syn::Type::Path(syn::TypePath {
|
|
||||||
qself: None,
|
|
||||||
ref path,
|
|
||||||
}) if path.leading_colon.is_none() && path.segments.len() == 1 =>
|
|
||||||
{
|
|
||||||
let seg = path.segments.first().unwrap().into_value();
|
|
||||||
match seg.arguments {
|
|
||||||
syn::PathArguments::None => match seg.ident.as_ref() {
|
|
||||||
"String" => return Type::Vector(VectorType::String, true),
|
|
||||||
_ => {}
|
|
||||||
},
|
|
||||||
syn::PathArguments::AngleBracketed(ref t)
|
|
||||||
if seg.ident == "Vec" && t.args.len() == 1 =>
|
|
||||||
{
|
|
||||||
match **t.args.first().unwrap().value() {
|
|
||||||
syn::GenericArgument::Type(ref t) => {
|
|
||||||
if let Some(ty) = VectorType::from(t) {
|
|
||||||
return Type::Vector(ty, true);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
|
|
||||||
Type::ByValue(ty.clone())
|
Type::ByValue(ty.clone())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -91,14 +91,14 @@ impl ToTokens for ast::Struct {
|
|||||||
let c = shared::name_to_descriptor(name.as_ref()) as u32;
|
let c = shared::name_to_descriptor(name.as_ref()) as u32;
|
||||||
(my_quote! {
|
(my_quote! {
|
||||||
impl ::wasm_bindgen::convert::WasmBoundary for #name {
|
impl ::wasm_bindgen::convert::WasmBoundary for #name {
|
||||||
type Js = u32;
|
type Abi = u32;
|
||||||
const DESCRIPTOR: u32 = #c;
|
const DESCRIPTOR: u32 = #c;
|
||||||
|
|
||||||
fn into_js(self) -> u32 {
|
fn into_abi(self) -> u32 {
|
||||||
Box::into_raw(Box::new(::wasm_bindgen::__rt::WasmRefCell::new(self))) as u32
|
Box::into_raw(Box::new(::wasm_bindgen::__rt::WasmRefCell::new(self))) as u32
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe fn from_js(js: u32) -> Self {
|
unsafe fn from_abi(js: u32) -> Self {
|
||||||
let js = js as *mut ::wasm_bindgen::__rt::WasmRefCell<#name>;
|
let js = js as *mut ::wasm_bindgen::__rt::WasmRefCell<#name>;
|
||||||
::wasm_bindgen::__rt::assert_not_null(js);
|
::wasm_bindgen::__rt::assert_not_null(js);
|
||||||
let js = Box::from_raw(js);
|
let js = Box::from_raw(js);
|
||||||
@ -109,7 +109,7 @@ impl ToTokens for ast::Struct {
|
|||||||
|
|
||||||
impl ::wasm_bindgen::convert::FromRefWasmBoundary for #name {
|
impl ::wasm_bindgen::convert::FromRefWasmBoundary for #name {
|
||||||
type RefAnchor = ::wasm_bindgen::__rt::Ref<'static, #name>;
|
type RefAnchor = ::wasm_bindgen::__rt::Ref<'static, #name>;
|
||||||
unsafe fn from_js_ref(js: Self::Js) -> Self::RefAnchor {
|
unsafe fn from_abi_ref(js: Self::Abi) -> Self::RefAnchor {
|
||||||
let js = js as *mut ::wasm_bindgen::__rt::WasmRefCell<#name>;
|
let js = js as *mut ::wasm_bindgen::__rt::WasmRefCell<#name>;
|
||||||
::wasm_bindgen::__rt::assert_not_null(js);
|
::wasm_bindgen::__rt::assert_not_null(js);
|
||||||
(*js).borrow()
|
(*js).borrow()
|
||||||
@ -119,7 +119,7 @@ impl ToTokens for ast::Struct {
|
|||||||
impl ::wasm_bindgen::convert::FromRefMutWasmBoundary for #name {
|
impl ::wasm_bindgen::convert::FromRefMutWasmBoundary for #name {
|
||||||
type RefAnchor = ::wasm_bindgen::__rt::RefMut<'static, #name>;
|
type RefAnchor = ::wasm_bindgen::__rt::RefMut<'static, #name>;
|
||||||
|
|
||||||
unsafe fn from_js_ref_mut(js: Self::Js) -> Self::RefAnchor {
|
unsafe fn from_abi_ref_mut(js: Self::Abi) -> Self::RefAnchor {
|
||||||
let js = js as *mut ::wasm_bindgen::__rt::WasmRefCell<#name>;
|
let js = js as *mut ::wasm_bindgen::__rt::WasmRefCell<#name>;
|
||||||
::wasm_bindgen::__rt::assert_not_null(js);
|
::wasm_bindgen::__rt::assert_not_null(js);
|
||||||
(*js).borrow_mut()
|
(*js).borrow_mut()
|
||||||
@ -128,7 +128,7 @@ impl ToTokens for ast::Struct {
|
|||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub unsafe extern fn #free_fn(ptr: u32) {
|
pub unsafe extern fn #free_fn(ptr: u32) {
|
||||||
<#name as ::wasm_bindgen::convert::WasmBoundary>::from_js(ptr);
|
<#name as ::wasm_bindgen::convert::WasmBoundary>::from_abi(ptr);
|
||||||
}
|
}
|
||||||
}).to_tokens(tokens);
|
}).to_tokens(tokens);
|
||||||
}
|
}
|
||||||
@ -158,72 +158,72 @@ impl ToTokens for ast::Export {
|
|||||||
let i = i + offset;
|
let i = i + offset;
|
||||||
let ident = syn::Ident::from(format!("arg{}", i));
|
let ident = syn::Ident::from(format!("arg{}", i));
|
||||||
match *ty {
|
match *ty {
|
||||||
ast::Type::Vector(ref ty, owned) => {
|
// ast::Type::Vector(ref ty, owned) => {
|
||||||
let ptr = syn::Ident::from(format!("arg{}_ptr", i));
|
// let ptr = syn::Ident::from(format!("arg{}_ptr", i));
|
||||||
let len = syn::Ident::from(format!("arg{}_len", i));
|
// let len = syn::Ident::from(format!("arg{}_len", i));
|
||||||
let abi_ty = ty.abi_element();
|
// let abi_ty = ty.abi_element();
|
||||||
args.push(my_quote! { #ptr: *mut #abi_ty });
|
// args.push(my_quote! { #ptr: *mut #abi_ty });
|
||||||
args.push(my_quote! { #len: usize });
|
// args.push(my_quote! { #len: usize });
|
||||||
if owned {
|
// if owned {
|
||||||
arg_conversions.push(my_quote! {
|
// arg_conversions.push(my_quote! {
|
||||||
let #ident = unsafe {
|
// let #ident = unsafe {
|
||||||
::std::vec::Vec::from_raw_parts(#ptr, #len, #len)
|
// ::std::vec::Vec::from_raw_parts(#ptr, #len, #len)
|
||||||
};
|
// };
|
||||||
});
|
// });
|
||||||
} else {
|
// } else {
|
||||||
arg_conversions.push(my_quote! {
|
// arg_conversions.push(my_quote! {
|
||||||
let #ident = unsafe {
|
// let #ident = unsafe {
|
||||||
::std::slice::from_raw_parts(#ptr as *const #abi_ty, #len)
|
// ::std::slice::from_raw_parts(#ptr as *const #abi_ty, #len)
|
||||||
};
|
// };
|
||||||
});
|
// });
|
||||||
}
|
// }
|
||||||
if let ast::VectorType::String = *ty {
|
// if let ast::VectorType::String = *ty {
|
||||||
if owned {
|
// if owned {
|
||||||
arg_conversions.push(my_quote! {
|
// arg_conversions.push(my_quote! {
|
||||||
let #ident = unsafe {
|
// let #ident = unsafe {
|
||||||
::std::string::String::from_utf8_unchecked(#ident)
|
// ::std::string::String::from_utf8_unchecked(#ident)
|
||||||
};
|
// };
|
||||||
});
|
// });
|
||||||
} else {
|
// } else {
|
||||||
arg_conversions.push(my_quote! {
|
// arg_conversions.push(my_quote! {
|
||||||
let #ident = unsafe {
|
// let #ident = unsafe {
|
||||||
::std::str::from_utf8_unchecked(#ident)
|
// ::std::str::from_utf8_unchecked(#ident)
|
||||||
};
|
// };
|
||||||
});
|
// });
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
ast::Type::ByValue(ref t) => {
|
ast::Type::ByValue(ref t) => {
|
||||||
args.push(my_quote! {
|
args.push(my_quote! {
|
||||||
#ident: <#t as ::wasm_bindgen::convert::WasmBoundary >::Js
|
#ident: <#t as ::wasm_bindgen::convert::WasmBoundary>::Abi
|
||||||
});
|
});
|
||||||
arg_conversions.push(my_quote! {
|
arg_conversions.push(my_quote! {
|
||||||
let #ident = unsafe {
|
let #ident = unsafe {
|
||||||
<#t as ::wasm_bindgen::convert::WasmBoundary>
|
<#t as ::wasm_bindgen::convert::WasmBoundary>
|
||||||
::from_js(#ident)
|
::from_abi(#ident, &mut __stack)
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
ast::Type::ByRef(ref ty) => {
|
ast::Type::ByRef(ref ty) => {
|
||||||
args.push(my_quote! {
|
args.push(my_quote! {
|
||||||
#ident: <#ty as ::wasm_bindgen::convert::WasmBoundary>::Js
|
#ident: <#ty as ::wasm_bindgen::convert::FromRefWasmBoundary>::Abi
|
||||||
});
|
});
|
||||||
arg_conversions.push(my_quote! {
|
arg_conversions.push(my_quote! {
|
||||||
let #ident = unsafe {
|
let #ident = unsafe {
|
||||||
<#ty as ::wasm_bindgen::convert::FromRefWasmBoundary>
|
<#ty as ::wasm_bindgen::convert::FromRefWasmBoundary>
|
||||||
::from_js_ref(#ident)
|
::from_abi_ref(#ident, &mut __stack)
|
||||||
};
|
};
|
||||||
let #ident = &*#ident;
|
let #ident = &*#ident;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
ast::Type::ByMutRef(ref ty) => {
|
ast::Type::ByMutRef(ref ty) => {
|
||||||
args.push(my_quote! {
|
args.push(my_quote! {
|
||||||
#ident: <#ty as ::wasm_bindgen::convert::WasmBoundary>::Js
|
#ident: <#ty as ::wasm_bindgen::convert::FromRefutWasmBoundary>::Abi
|
||||||
});
|
});
|
||||||
arg_conversions.push(my_quote! {
|
arg_conversions.push(my_quote! {
|
||||||
let mut #ident = unsafe {
|
let mut #ident = unsafe {
|
||||||
<#ty as ::wasm_bindgen::convert::FromRefMutWasmBoundary>
|
<#ty as ::wasm_bindgen::convert::FromRefMutWasmBoundary>
|
||||||
::from_js_ref_mut(#ident)
|
::from_abi_ref_mut(#ident, &mut __stack)
|
||||||
};
|
};
|
||||||
let #ident = &mut *#ident;
|
let #ident = &mut *#ident;
|
||||||
});
|
});
|
||||||
@ -234,20 +234,22 @@ impl ToTokens for ast::Export {
|
|||||||
let ret_ty;
|
let ret_ty;
|
||||||
let convert_ret;
|
let convert_ret;
|
||||||
match self.function.ret {
|
match self.function.ret {
|
||||||
Some(ast::Type::Vector(ref ty, true)) => {
|
// Some(ast::Type::Vector(ref ty, true)) => {
|
||||||
ret_ty = my_quote! { -> *mut #ty };
|
// ret_ty = my_quote! { -> *mut #ty };
|
||||||
convert_ret = my_quote! { Box::into_raw(Box::new(#ret)) };
|
// convert_ret = my_quote! { Box::into_raw(Box::new(#ret)) };
|
||||||
}
|
// }
|
||||||
Some(ast::Type::ByValue(ref t)) => {
|
Some(ast::Type::ByValue(ref t)) => {
|
||||||
ret_ty = my_quote! {
|
ret_ty = my_quote! {
|
||||||
-> <#t as ::wasm_bindgen::convert::WasmBoundary>::Js
|
-> <#t as ::wasm_bindgen::convert::WasmBoundary>::Abi
|
||||||
};
|
};
|
||||||
convert_ret = my_quote! {
|
convert_ret = my_quote! {
|
||||||
<#t as ::wasm_bindgen::convert::WasmBoundary>::into_js(#ret)
|
<#t as ::wasm_bindgen::convert::WasmBoundary>
|
||||||
|
::into_abi(#ret, &mut unsafe {
|
||||||
|
::wasm_bindgen::convert::GlobalStack::new()
|
||||||
|
})
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
Some(ast::Type::Vector(_, false))
|
Some(ast::Type::ByMutRef(_))
|
||||||
| Some(ast::Type::ByMutRef(_))
|
|
||||||
| Some(ast::Type::ByRef(_)) => {
|
| Some(ast::Type::ByRef(_)) => {
|
||||||
panic!("can't return a borrowed ref");
|
panic!("can't return a borrowed ref");
|
||||||
}
|
}
|
||||||
@ -275,6 +277,9 @@ impl ToTokens for ast::Export {
|
|||||||
#[allow(non_snake_case)]
|
#[allow(non_snake_case)]
|
||||||
pub extern fn #generated_name(#(#args),*) #ret_ty {
|
pub extern fn #generated_name(#(#args),*) #ret_ty {
|
||||||
::wasm_bindgen::__rt::link_this_library();
|
::wasm_bindgen::__rt::link_this_library();
|
||||||
|
let mut __stack = unsafe {
|
||||||
|
::wasm_bindgen::convert::GlobalStack::new()
|
||||||
|
};
|
||||||
#(#arg_conversions)*
|
#(#arg_conversions)*
|
||||||
let #ret = #receiver(#(#converted_arguments),*);
|
let #ret = #receiver(#(#converted_arguments),*);
|
||||||
#convert_ret
|
#convert_ret
|
||||||
@ -295,31 +300,31 @@ impl ToTokens for ast::ImportType {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ::wasm_bindgen::convert::WasmBoundary for #name {
|
impl ::wasm_bindgen::convert::WasmBoundary for #name {
|
||||||
type Js = <::wasm_bindgen::JsValue as
|
type Abi = <::wasm_bindgen::JsValue as
|
||||||
::wasm_bindgen::convert::WasmBoundary>::Js;
|
::wasm_bindgen::convert::WasmBoundary>::Abi;
|
||||||
const DESCRIPTOR: u32 = <::wasm_bindgen::JsValue as
|
const DESCRIPTOR: u32 = <::wasm_bindgen::JsValue as
|
||||||
::wasm_bindgen::convert::WasmBoundary>::DESCRIPTOR;
|
::wasm_bindgen::convert::WasmBoundary>::DESCRIPTOR;
|
||||||
|
|
||||||
fn into_js(self) -> Self::Js {
|
fn into_abi(self) -> Self::Abi {
|
||||||
self.obj.into_js()
|
self.obj.into_abi()
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe fn from_js(js: Self::Js) -> Self {
|
unsafe fn from_abi(js: Self::Abi) -> Self {
|
||||||
#name { obj: ::wasm_bindgen::JsValue::from_js(js) }
|
#name { obj: ::wasm_bindgen::JsValue::from_abi(js) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ::wasm_bindgen::convert::ToRefWasmBoundary for #name {
|
impl ::wasm_bindgen::convert::ToRefWasmBoundary for #name {
|
||||||
fn to_js_ref(&self) -> u32 {
|
fn to_abi_ref(&self) -> u32 {
|
||||||
self.obj.to_js_ref()
|
self.obj.to_abi_ref()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ::wasm_bindgen::convert::FromRefWasmBoundary for #name {
|
impl ::wasm_bindgen::convert::FromRefWasmBoundary for #name {
|
||||||
type RefAnchor = ::std::mem::ManuallyDrop<#name>;
|
type RefAnchor = ::std::mem::ManuallyDrop<#name>;
|
||||||
unsafe fn from_js_ref(js: Self::Js) -> Self::RefAnchor {
|
unsafe fn from_abi_ref(js: Self::Abi) -> Self::RefAnchor {
|
||||||
let obj = <::wasm_bindgen::JsValue as ::wasm_bindgen::convert::WasmBoundary>
|
let obj = <::wasm_bindgen::JsValue as ::wasm_bindgen::convert::WasmBoundary>
|
||||||
::from_js(js);
|
::from_abi(js);
|
||||||
::std::mem::ManuallyDrop::new(#name { obj })
|
::std::mem::ManuallyDrop::new(#name { obj })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -393,36 +398,36 @@ impl ToTokens for ast::ImportFunction {
|
|||||||
|
|
||||||
for (i, (ty, name)) in self.function.arguments.iter().zip(names).enumerate() {
|
for (i, (ty, name)) in self.function.arguments.iter().zip(names).enumerate() {
|
||||||
match *ty {
|
match *ty {
|
||||||
ast::Type::Vector(ref ty, owned) => {
|
// ast::Type::Vector(ref ty, owned) => {
|
||||||
let ptr = syn::Ident::from(format!("{}_ptr", name));
|
// let ptr = syn::Ident::from(format!("{}_ptr", name));
|
||||||
let len = syn::Ident::from(format!("{}_len", name));
|
// let len = syn::Ident::from(format!("{}_len", name));
|
||||||
abi_argument_names.push(ptr);
|
// abi_argument_names.push(ptr);
|
||||||
abi_argument_names.push(len);
|
// abi_argument_names.push(len);
|
||||||
let abi_ty = ty.abi_element();
|
// let abi_ty = ty.abi_element();
|
||||||
abi_arguments.push(my_quote! { #ptr: *const #abi_ty });
|
// abi_arguments.push(my_quote! { #ptr: *const #abi_ty });
|
||||||
abi_arguments.push(my_quote! { #len: usize });
|
// abi_arguments.push(my_quote! { #len: usize });
|
||||||
arg_conversions.push(my_quote! {
|
// arg_conversions.push(my_quote! {
|
||||||
let #ptr = #name.as_ptr();
|
// let #ptr = #name.as_ptr();
|
||||||
let #len = #name.len();
|
// let #len = #name.len();
|
||||||
});
|
// });
|
||||||
if owned {
|
// if owned {
|
||||||
arg_conversions.push(my_quote! { ::std::mem::forget(#name); });
|
// arg_conversions.push(my_quote! { ::std::mem::forget(#name); });
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
ast::Type::ByValue(ref t) => {
|
ast::Type::ByValue(ref t) => {
|
||||||
abi_argument_names.push(name);
|
abi_argument_names.push(name);
|
||||||
abi_arguments.push(my_quote! {
|
abi_arguments.push(my_quote! {
|
||||||
#name: <#t as ::wasm_bindgen::convert::WasmBoundary>::Js
|
#name: <#t as ::wasm_bindgen::convert::WasmBoundary>::Abi
|
||||||
});
|
});
|
||||||
if i == 0 && is_method {
|
if i == 0 && is_method {
|
||||||
arg_conversions.push(my_quote! {
|
arg_conversions.push(my_quote! {
|
||||||
let #name = <#t as ::wasm_bindgen::convert::WasmBoundary>
|
let #name = <#t as ::wasm_bindgen::convert::WasmBoundary>
|
||||||
::into_js(self);
|
::into_abi(self);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
arg_conversions.push(my_quote! {
|
arg_conversions.push(my_quote! {
|
||||||
let #name = <#t as ::wasm_bindgen::convert::WasmBoundary>
|
let #name = <#t as ::wasm_bindgen::convert::WasmBoundary>
|
||||||
::into_js(#name);
|
::into_abi(#name);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -433,12 +438,12 @@ impl ToTokens for ast::ImportFunction {
|
|||||||
if i == 0 && is_method {
|
if i == 0 && is_method {
|
||||||
arg_conversions.push(my_quote! {
|
arg_conversions.push(my_quote! {
|
||||||
let #name = <#t as ::wasm_bindgen::convert::ToRefWasmBoundary>
|
let #name = <#t as ::wasm_bindgen::convert::ToRefWasmBoundary>
|
||||||
::to_js_ref(self);
|
::to_abi_ref(self);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
arg_conversions.push(my_quote! {
|
arg_conversions.push(my_quote! {
|
||||||
let #name = <#t as ::wasm_bindgen::convert::ToRefWasmBoundary>
|
let #name = <#t as ::wasm_bindgen::convert::ToRefWasmBoundary>
|
||||||
::to_js_ref(#name);
|
::to_abi_ref(#name);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -449,38 +454,37 @@ impl ToTokens for ast::ImportFunction {
|
|||||||
match self.function.ret {
|
match self.function.ret {
|
||||||
Some(ast::Type::ByValue(ref t)) => {
|
Some(ast::Type::ByValue(ref t)) => {
|
||||||
abi_ret = my_quote! {
|
abi_ret = my_quote! {
|
||||||
<#t as ::wasm_bindgen::convert::WasmBoundary>::Js
|
<#t as ::wasm_bindgen::convert::WasmBoundary>::Abi
|
||||||
};
|
};
|
||||||
convert_ret = my_quote! {
|
convert_ret = my_quote! {
|
||||||
<#t as ::wasm_bindgen::convert::WasmBoundary>::from_js(#ret_ident)
|
<#t as ::wasm_bindgen::convert::WasmBoundary>::from_abi(#ret_ident)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
Some(ast::Type::Vector(ref ty, true)) => {
|
// Some(ast::Type::Vector(ref ty, true)) => {
|
||||||
let name = syn::Ident::from("__ret_len");
|
// let name = syn::Ident::from("__ret_len");
|
||||||
let name_ptr = syn::Ident::from("__ret_len_ptr");
|
// let name_ptr = syn::Ident::from("__ret_len_ptr");
|
||||||
abi_argument_names.push(name_ptr);
|
// abi_argument_names.push(name_ptr);
|
||||||
abi_arguments.push(my_quote! { #name_ptr: *mut usize });
|
// abi_arguments.push(my_quote! { #name_ptr: *mut usize });
|
||||||
arg_conversions.push(my_quote! {
|
// arg_conversions.push(my_quote! {
|
||||||
let mut #name = 0;
|
// let mut #name = 0;
|
||||||
let mut #name_ptr = &mut #name as *mut usize;
|
// let mut #name_ptr = &mut #name as *mut usize;
|
||||||
});
|
// });
|
||||||
let abi_ty = ty.abi_element();
|
// let abi_ty = ty.abi_element();
|
||||||
abi_ret = my_quote! { *mut #abi_ty };
|
// abi_ret = my_quote! { *mut #abi_ty };
|
||||||
if let ast::VectorType::String = *ty {
|
// if let ast::VectorType::String = *ty {
|
||||||
convert_ret = my_quote! {
|
// convert_ret = my_quote! {
|
||||||
String::from_utf8_unchecked(
|
// String::from_utf8_unchecked(
|
||||||
Vec::from_raw_parts(#ret_ident, #name, #name)
|
// Vec::from_raw_parts(#ret_ident, #name, #name)
|
||||||
)
|
// )
|
||||||
};
|
// };
|
||||||
} else {
|
// } else {
|
||||||
convert_ret = my_quote! {
|
// convert_ret = my_quote! {
|
||||||
Vec::from_raw_parts(#ret_ident, #name, #name)
|
// Vec::from_raw_parts(#ret_ident, #name, #name)
|
||||||
};
|
// };
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
Some(ast::Type::ByRef(_))
|
Some(ast::Type::ByRef(_))
|
||||||
| Some(ast::Type::Vector(_, false))
|
|
||||||
| Some(ast::Type::ByMutRef(_)) => panic!("can't return a borrowed ref"),
|
| Some(ast::Type::ByMutRef(_)) => panic!("can't return a borrowed ref"),
|
||||||
None => {
|
None => {
|
||||||
abi_ret = my_quote! { () };
|
abi_ret = my_quote! { () };
|
||||||
@ -502,7 +506,7 @@ impl ToTokens for ast::ImportFunction {
|
|||||||
exceptional_ret = my_quote! {
|
exceptional_ret = my_quote! {
|
||||||
if #exn_data[0] == 1 {
|
if #exn_data[0] == 1 {
|
||||||
return Err(<::wasm_bindgen::JsValue as
|
return Err(<::wasm_bindgen::JsValue as
|
||||||
::wasm_bindgen::convert::WasmBoundary>::from_js(#exn_data[1]))
|
::wasm_bindgen::convert::WasmBoundary>::from_abi(#exn_data[1]))
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -578,14 +582,14 @@ impl ToTokens for ast::Enum {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ::wasm_bindgen::convert::WasmBoundary for #enum_name {
|
impl ::wasm_bindgen::convert::WasmBoundary for #enum_name {
|
||||||
type Js = u32;
|
type Abi = u32;
|
||||||
const DESCRIPTOR: u32 = #c;
|
const DESCRIPTOR: u32 = #c;
|
||||||
|
|
||||||
fn into_js(self) -> u32 {
|
fn into_abi(self) -> u32 {
|
||||||
self as u32
|
self as u32
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe fn from_js(js: u32) -> Self {
|
unsafe fn from_abi(js: u32) -> Self {
|
||||||
#enum_name::from_u32(js)
|
#enum_name::from_u32(js)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -608,7 +612,7 @@ impl ToTokens for ast::ImportStatic {
|
|||||||
fn #shim_name() -> u32;
|
fn #shim_name() -> u32;
|
||||||
}
|
}
|
||||||
unsafe {
|
unsafe {
|
||||||
::wasm_bindgen::convert::WasmBoundary::from_js(#shim_name())
|
::wasm_bindgen::convert::WasmBoundary::from_abi(#shim_name())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
::wasm_bindgen::JsStatic {
|
::wasm_bindgen::JsStatic {
|
||||||
|
@ -24,17 +24,9 @@ impl<'a> LiteralBuilder<'a> {
|
|||||||
b.to_tokens(self.dst);
|
b.to_tokens(self.dst);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn char_lit(&mut self, c: char) {
|
|
||||||
let c = c as u32;
|
|
||||||
self.byte(c as u8);
|
|
||||||
self.byte((c >> 8) as u8);
|
|
||||||
self.byte((c >> 16) as u8);
|
|
||||||
self.byte((c >> 24) as u8);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn append(&mut self, s: &str) {
|
fn append(&mut self, s: &str) {
|
||||||
for c in s.chars() {
|
for &b in s.as_bytes() {
|
||||||
self.char_lit(c);
|
self.byte(b);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -52,22 +44,18 @@ impl<'a> LiteralBuilder<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn char(&mut self, s: char) {
|
fn u32(&mut self, s: u32) {
|
||||||
self.append("\"");
|
self.append(&s.to_string())
|
||||||
self.char_lit(s);
|
|
||||||
self.append("\"");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn as_char(&mut self, tokens: Tokens) {
|
fn as_char(&mut self, tokens: Tokens) {
|
||||||
self.append("\"");
|
|
||||||
(quote! {
|
(quote! {
|
||||||
, (((#tokens) as u32) >> 0) as u8
|
,(#tokens).__x[0]
|
||||||
, (((#tokens) as u32) >> 8) as u8
|
,(#tokens).__x[1]
|
||||||
, (((#tokens) as u32) >> 16) as u8
|
,(#tokens).__x[2]
|
||||||
, (((#tokens) as u32) >> 24) as u8
|
,(#tokens).__x[3]
|
||||||
}).to_tokens(self.dst);
|
}).to_tokens(self.dst);
|
||||||
self.cnt += 4;
|
self.cnt += 4;
|
||||||
self.append("\"");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn fields(&mut self, fields: &[(&str, &Fn(&mut Self))]) {
|
pub fn fields(&mut self, fields: &[(&str, &Fn(&mut Self))]) {
|
||||||
@ -126,7 +114,7 @@ impl Literal for ast::Program {
|
|||||||
a.list(&names, |s, a| {
|
a.list(&names, |s, a| {
|
||||||
let val = shared::name_to_descriptor(s.as_ref());
|
let val = shared::name_to_descriptor(s.as_ref());
|
||||||
a.fields(&[
|
a.fields(&[
|
||||||
("descriptor", &|a| a.char(val)),
|
("descriptor", &|a| a.u32(val)),
|
||||||
("name", &|a| a.str(s.as_ref())),
|
("name", &|a| a.str(s.as_ref())),
|
||||||
]);
|
]);
|
||||||
})
|
})
|
||||||
@ -153,39 +141,16 @@ impl Literal for ast::Function {
|
|||||||
impl Literal for ast::Type {
|
impl Literal for ast::Type {
|
||||||
fn literal(&self, a: &mut LiteralBuilder) {
|
fn literal(&self, a: &mut LiteralBuilder) {
|
||||||
match *self {
|
match *self {
|
||||||
ast::Type::Vector(ast::VectorType::String, true) => a.char(shared::TYPE_STRING),
|
|
||||||
ast::Type::Vector(ast::VectorType::String, false) => a.char(shared::TYPE_BORROWED_STR),
|
|
||||||
ast::Type::Vector(ast::VectorType::U8, true) => a.char(shared::TYPE_VECTOR_U8),
|
|
||||||
ast::Type::Vector(ast::VectorType::U8, false) => a.char(shared::TYPE_SLICE_U8),
|
|
||||||
ast::Type::Vector(ast::VectorType::I8, true) => a.char(shared::TYPE_VECTOR_I8),
|
|
||||||
ast::Type::Vector(ast::VectorType::I8, false) => a.char(shared::TYPE_SLICE_I8),
|
|
||||||
ast::Type::Vector(ast::VectorType::U16, true) => a.char(shared::TYPE_VECTOR_U16),
|
|
||||||
ast::Type::Vector(ast::VectorType::U16, false) => a.char(shared::TYPE_SLICE_U16),
|
|
||||||
ast::Type::Vector(ast::VectorType::I16, true) => a.char(shared::TYPE_VECTOR_I16),
|
|
||||||
ast::Type::Vector(ast::VectorType::I16, false) => a.char(shared::TYPE_SLICE_I16),
|
|
||||||
ast::Type::Vector(ast::VectorType::U32, true) => a.char(shared::TYPE_VECTOR_U32),
|
|
||||||
ast::Type::Vector(ast::VectorType::U32, false) => a.char(shared::TYPE_SLICE_U32),
|
|
||||||
ast::Type::Vector(ast::VectorType::I32, true) => a.char(shared::TYPE_VECTOR_I32),
|
|
||||||
ast::Type::Vector(ast::VectorType::I32, false) => a.char(shared::TYPE_SLICE_I32),
|
|
||||||
ast::Type::Vector(ast::VectorType::F32, true) => a.char(shared::TYPE_VECTOR_F32),
|
|
||||||
ast::Type::Vector(ast::VectorType::F32, false) => a.char(shared::TYPE_SLICE_F32),
|
|
||||||
ast::Type::Vector(ast::VectorType::F64, true) => a.char(shared::TYPE_VECTOR_F64),
|
|
||||||
ast::Type::Vector(ast::VectorType::F64, false) => a.char(shared::TYPE_SLICE_F64),
|
|
||||||
ast::Type::Vector(ast::VectorType::JsValue, true) => {
|
|
||||||
a.char(shared::TYPE_VECTOR_JSVALUE)
|
|
||||||
}
|
|
||||||
ast::Type::Vector(ast::VectorType::JsValue, false) => {
|
|
||||||
panic!("Slices of JsValues not supported")
|
|
||||||
}
|
|
||||||
ast::Type::ByValue(ref t) => {
|
ast::Type::ByValue(ref t) => {
|
||||||
a.as_char(my_quote! {
|
a.as_char(my_quote! {
|
||||||
<#t as ::wasm_bindgen::convert::WasmBoundary>::DESCRIPTOR
|
<#t as ::wasm_bindgen::convert::WasmBoundary>::DESCRIPTOR
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
ast::Type::ByRef(ref ty) | ast::Type::ByMutRef(ref ty) => {
|
ast::Type::ByRef(ref ty) | ast::Type::ByMutRef(ref ty) => {
|
||||||
|
// TODO: this assumes `ToRef*` and `FromRef*` use the same
|
||||||
|
// descriptor.
|
||||||
a.as_char(my_quote! {
|
a.as_char(my_quote! {
|
||||||
(<#ty as ::wasm_bindgen::convert::WasmBoundary>::DESCRIPTOR |
|
<#ty as ::wasm_bindgen::convert::ToRefWasmBoundary>::DESCRIPTOR
|
||||||
::wasm_bindgen::convert::DESCRIPTOR_CUSTOM_REF_FLAG)
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,7 @@ pub struct Context<'a> {
|
|||||||
pub required_internal_exports: HashSet<&'static str>,
|
pub required_internal_exports: HashSet<&'static str>,
|
||||||
pub config: &'a Bindgen,
|
pub config: &'a Bindgen,
|
||||||
pub module: &'a mut Module,
|
pub module: &'a mut Module,
|
||||||
pub custom_type_names: HashMap<char, String>,
|
pub custom_type_names: HashMap<u32, String>,
|
||||||
pub imported_names: HashSet<String>,
|
pub imported_names: HashSet<String>,
|
||||||
pub exported_classes: HashMap<String, ExportedClass>,
|
pub exported_classes: HashMap<String, ExportedClass>,
|
||||||
}
|
}
|
||||||
@ -528,6 +528,7 @@ impl<'a> Context<'a> {
|
|||||||
self.required_internal_exports.insert("__wbindgen_malloc");
|
self.required_internal_exports.insert("__wbindgen_malloc");
|
||||||
self.expose_text_encoder();
|
self.expose_text_encoder();
|
||||||
self.expose_uint8_memory();
|
self.expose_uint8_memory();
|
||||||
|
self.expose_push_global_argument();
|
||||||
self.globals.push_str(&format!("
|
self.globals.push_str(&format!("
|
||||||
function passStringToWasm(arg) {{
|
function passStringToWasm(arg) {{
|
||||||
if (typeof(arg) !== 'string')
|
if (typeof(arg) !== 'string')
|
||||||
@ -936,9 +937,8 @@ impl<'a> Context<'a> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn custom_type_name(&self, c: char) -> &str {
|
fn custom_type_name(&self, c: u32) -> &str {
|
||||||
let c = (c as u32) & !shared::TYPE_CUSTOM_REF_FLAG;
|
let c = c & !shared::TYPE_CUSTOM_REF_FLAG;
|
||||||
let c = char::from_u32(c).unwrap();
|
|
||||||
&self.custom_type_names[&c]
|
&self.custom_type_names[&c]
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1016,6 +1016,51 @@ impl<'a> Context<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn expose_push_global_argument(&mut self) {
|
||||||
|
if !self.exposed_globals.insert("push_global_argument") {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
self.expose_uint32_memory();
|
||||||
|
self.expose_global_argument_ptr();
|
||||||
|
self.globals.push_str("
|
||||||
|
function pushGlobalArgument(arg) {{
|
||||||
|
const idx = globalArgumentPtr() / 4 + GLOBAL_ARGUMENT_CNT;
|
||||||
|
getUint32Memory()[idx] = arg;
|
||||||
|
GLOBAL_ARGUMENT_CNT += 1;
|
||||||
|
}}
|
||||||
|
");
|
||||||
|
}
|
||||||
|
|
||||||
|
fn expose_get_global_argument(&mut self) {
|
||||||
|
if !self.exposed_globals.insert("get_global_argument") {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
self.expose_uint32_memory();
|
||||||
|
self.expose_global_argument_ptr();
|
||||||
|
self.globals.push_str("
|
||||||
|
function getGlobalArgument(arg) {{
|
||||||
|
const idx = globalArgumentPtr() / 4 + arg;
|
||||||
|
return getUint32Memory()[idx];
|
||||||
|
}}
|
||||||
|
");
|
||||||
|
}
|
||||||
|
|
||||||
|
fn expose_global_argument_ptr(&mut self) {
|
||||||
|
if !self.exposed_globals.insert("global_argument_ptr") {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
self.required_internal_exports.insert("__wbindgen_global_argument_ptr");
|
||||||
|
self.globals.push_str("
|
||||||
|
let cachedGlobalArgumentPtr = null;
|
||||||
|
let GLOBAL_ARGUMENT_CNT = 0;
|
||||||
|
function globalArgumentPtr() {
|
||||||
|
if (cachedGlobalArgumentPtr === null)
|
||||||
|
cachedGlobalArgumentPtr = wasm.__wbindgen_global_argument_ptr();
|
||||||
|
return cachedGlobalArgumentPtr;
|
||||||
|
}
|
||||||
|
");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'b> SubContext<'a, 'b> {
|
impl<'a, 'b> SubContext<'a, 'b> {
|
||||||
@ -1095,6 +1140,7 @@ impl<'a, 'b> SubContext<'a, 'b> {
|
|||||||
passed_args.push_str("this.ptr");
|
passed_args.push_str("this.ptr");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut argument_pushed = false;
|
||||||
for (i, arg) in function.arguments.iter().enumerate() {
|
for (i, arg) in function.arguments.iter().enumerate() {
|
||||||
let name = format!("arg{}", i);
|
let name = format!("arg{}", i);
|
||||||
if i > 0 {
|
if i > 0 {
|
||||||
@ -1130,40 +1176,6 @@ impl<'a, 'b> SubContext<'a, 'b> {
|
|||||||
}
|
}
|
||||||
pass(&format!("arg{i} ? 1 : 0", i = i))
|
pass(&format!("arg{i} ? 1 : 0", i = i))
|
||||||
}
|
}
|
||||||
shared::TYPE_BORROWED_STR |
|
|
||||||
shared::TYPE_STRING |
|
|
||||||
shared::TYPE_VECTOR_U8 |
|
|
||||||
shared::TYPE_VECTOR_I8 |
|
|
||||||
shared::TYPE_SLICE_U8 |
|
|
||||||
shared::TYPE_SLICE_I8 |
|
|
||||||
shared::TYPE_VECTOR_U16 |
|
|
||||||
shared::TYPE_VECTOR_I16 |
|
|
||||||
shared::TYPE_SLICE_U16 |
|
|
||||||
shared::TYPE_SLICE_I16 |
|
|
||||||
shared::TYPE_VECTOR_U32 |
|
|
||||||
shared::TYPE_VECTOR_I32 |
|
|
||||||
shared::TYPE_SLICE_U32 |
|
|
||||||
shared::TYPE_SLICE_I32 |
|
|
||||||
shared::TYPE_VECTOR_F32 |
|
|
||||||
shared::TYPE_VECTOR_F64 |
|
|
||||||
shared::TYPE_SLICE_F32 |
|
|
||||||
shared::TYPE_SLICE_F64 => {
|
|
||||||
let ty = VectorType::from(*arg);
|
|
||||||
dst_ts.push_str(": ");
|
|
||||||
dst_ts.push_str(ty.js_ty());
|
|
||||||
let func = self.cx.pass_to_wasm_function(&ty);
|
|
||||||
arg_conversions.push_str(&format!("\
|
|
||||||
const [ptr{i}, len{i}] = {func}({arg});
|
|
||||||
", i = i, func = func, arg = name));
|
|
||||||
pass(&format!("ptr{}", i));
|
|
||||||
pass(&format!("len{}", i));
|
|
||||||
if !ty.owned {
|
|
||||||
destructors.push_str(&format!("\n\
|
|
||||||
wasm.__wbindgen_free(ptr{i}, len{i});\n\
|
|
||||||
", i = i));
|
|
||||||
self.cx.required_internal_exports.insert("__wbindgen_free");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
shared::TYPE_JS_OWNED => {
|
shared::TYPE_JS_OWNED => {
|
||||||
dst_ts.push_str(": any");
|
dst_ts.push_str(": any");
|
||||||
self.cx.expose_add_heap_object();
|
self.cx.expose_add_heap_object();
|
||||||
@ -1181,8 +1193,30 @@ impl<'a, 'b> SubContext<'a, 'b> {
|
|||||||
destructors.push_str("stack.pop();\n");
|
destructors.push_str("stack.pop();\n");
|
||||||
pass(&format!("idx{}", i));
|
pass(&format!("idx{}", i));
|
||||||
}
|
}
|
||||||
custom if (custom as u32) & shared::TYPE_CUSTOM_REF_FLAG != 0 => {
|
other => {
|
||||||
let s = self.cx.custom_type_name(custom).to_string();
|
match VectorType::from(other) {
|
||||||
|
Some(ty) => {
|
||||||
|
dst_ts.push_str(": ");
|
||||||
|
dst_ts.push_str(ty.js_ty());
|
||||||
|
let func = self.cx.pass_to_wasm_function(&ty);
|
||||||
|
self.cx.expose_push_global_argument();
|
||||||
|
argument_pushed = true;
|
||||||
|
arg_conversions.push_str(&format!("\
|
||||||
|
const [ptr{i}, len{i}] = {func}({arg});
|
||||||
|
pushGlobalArgument(len{i});
|
||||||
|
", i = i, func = func, arg = name));
|
||||||
|
pass(&format!("ptr{}", i));
|
||||||
|
if !ty.owned {
|
||||||
|
destructors.push_str(&format!("\n\
|
||||||
|
wasm.__wbindgen_free(ptr{i}, len{i} * {size});\n\
|
||||||
|
", i = i, size = ty.size()));
|
||||||
|
self.cx.required_internal_exports.insert(
|
||||||
|
"__wbindgen_free",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
let s = self.cx.custom_type_name(other).to_string();
|
||||||
dst_ts.push_str(&format!(": {}", s));
|
dst_ts.push_str(&format!(": {}", s));
|
||||||
if self.cx.config.debug {
|
if self.cx.config.debug {
|
||||||
self.cx.expose_assert_class();
|
self.cx.expose_assert_class();
|
||||||
@ -1190,17 +1224,10 @@ impl<'a, 'b> SubContext<'a, 'b> {
|
|||||||
_assertClass({arg}, {struct_});
|
_assertClass({arg}, {struct_});
|
||||||
", arg = name, struct_ = s));
|
", arg = name, struct_ = s));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if other & shared::TYPE_CUSTOM_REF_FLAG != 0 {
|
||||||
pass(&format!("{}.ptr", name));
|
pass(&format!("{}.ptr", name));
|
||||||
}
|
} else {
|
||||||
custom => {
|
|
||||||
let s = self.cx.custom_type_name(custom).to_string();
|
|
||||||
dst_ts.push_str(&format!(": {}", s));
|
|
||||||
if self.cx.config.debug {
|
|
||||||
self.cx.expose_assert_class();
|
|
||||||
arg_conversions.push_str(&format!("\
|
|
||||||
_assertClass({arg}, {struct_});
|
|
||||||
", arg = name, struct_ = s));
|
|
||||||
}
|
|
||||||
arg_conversions.push_str(&format!("\
|
arg_conversions.push_str(&format!("\
|
||||||
const ptr{i} = {arg}.ptr;
|
const ptr{i} = {arg}.ptr;
|
||||||
{arg}.ptr = 0;
|
{arg}.ptr = 0;
|
||||||
@ -1209,6 +1236,9 @@ impl<'a, 'b> SubContext<'a, 'b> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
dst.push_str(")");
|
dst.push_str(")");
|
||||||
dst_ts.push_str(")");
|
dst_ts.push_str(")");
|
||||||
let convert_ret = match function.ret {
|
let convert_ret = match function.ret {
|
||||||
@ -1233,36 +1263,33 @@ impl<'a, 'b> SubContext<'a, 'b> {
|
|||||||
self.cx.expose_take_object();
|
self.cx.expose_take_object();
|
||||||
format!("return takeObject(ret);")
|
format!("return takeObject(ret);")
|
||||||
}
|
}
|
||||||
Some(shared::TYPE_STRING) |
|
Some(shared::TYPE_JS_REF) => {
|
||||||
Some(shared::TYPE_VECTOR_U8) |
|
dst_ts.push_str(": any");
|
||||||
Some(shared::TYPE_VECTOR_I8) |
|
self.cx.expose_get_object();
|
||||||
Some(shared::TYPE_VECTOR_U16) |
|
format!("return getObject(ret);")
|
||||||
Some(shared::TYPE_VECTOR_I16) |
|
}
|
||||||
Some(shared::TYPE_VECTOR_U32) |
|
Some(other) => {
|
||||||
Some(shared::TYPE_VECTOR_I32) |
|
if other & shared::TYPE_CUSTOM_REF_FLAG != 0 {
|
||||||
Some(shared::TYPE_VECTOR_F32) |
|
panic!("cannot return references yet");
|
||||||
Some(shared::TYPE_VECTOR_F64) |
|
}
|
||||||
Some(shared::TYPE_VECTOR_JSVALUE) => {
|
match VectorType::from(other) {
|
||||||
let ty = VectorType::from(function.ret.unwrap());
|
Some(ty) => {
|
||||||
dst_ts.push_str(": ");
|
dst_ts.push_str(": ");
|
||||||
dst_ts.push_str(ty.js_ty());
|
dst_ts.push_str(ty.js_ty());
|
||||||
let f = self.cx.expose_get_vector_from_wasm(&ty);
|
let f = self.cx.expose_get_vector_from_wasm(&ty);
|
||||||
self.cx.required_internal_exports.insert("__wbindgen_boxed_str_ptr");
|
self.cx.expose_get_global_argument();
|
||||||
self.cx.required_internal_exports.insert("__wbindgen_boxed_str_len");
|
self.cx.required_internal_exports.insert(
|
||||||
self.cx.required_internal_exports.insert("__wbindgen_boxed_str_free");
|
"__wbindgen_free",
|
||||||
|
);
|
||||||
format!("
|
format!("
|
||||||
const ptr = wasm.__wbindgen_boxed_str_ptr(ret);
|
const len = getGlobalArgument(0);
|
||||||
const len = wasm.__wbindgen_boxed_str_len(ret);
|
const realRet = {}(ret, len);
|
||||||
const realRet = {}(ptr, len);
|
wasm.__wbindgen_free(ret, len * {});
|
||||||
wasm.__wbindgen_boxed_str_free(ret);
|
|
||||||
return realRet;
|
return realRet;
|
||||||
", f)
|
", f, ty.size())
|
||||||
}
|
}
|
||||||
Some(shared::TYPE_JS_REF) |
|
None => {
|
||||||
Some(shared::TYPE_BORROWED_STR) => panic!(),
|
let name = self.cx.custom_type_name(other);
|
||||||
Some(t) if (t as u32) & shared::TYPE_CUSTOM_REF_FLAG != 0 => panic!(),
|
|
||||||
Some(custom) => {
|
|
||||||
let name = self.cx.custom_type_name(custom);
|
|
||||||
dst_ts.push_str(": ");
|
dst_ts.push_str(": ");
|
||||||
dst_ts.push_str(name);
|
dst_ts.push_str(name);
|
||||||
if self.cx.config.debug {
|
if self.cx.config.debug {
|
||||||
@ -1275,9 +1302,14 @@ impl<'a, 'b> SubContext<'a, 'b> {
|
|||||||
", name = name)
|
", name = name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
dst_ts.push_str(";");
|
dst_ts.push_str(";");
|
||||||
dst.push_str(" {\n ");
|
dst.push_str(" {\n ");
|
||||||
|
if argument_pushed {
|
||||||
|
dst.push_str("GLOBAL_ARGUMENT_CNT = 0;\n");
|
||||||
|
}
|
||||||
dst.push_str(&arg_conversions);
|
dst.push_str(&arg_conversions);
|
||||||
if destructors.len() == 0 {
|
if destructors.len() == 0 {
|
||||||
dst.push_str(&format!("\
|
dst.push_str(&format!("\
|
||||||
@ -1353,40 +1385,40 @@ impl<'a, 'b> SubContext<'a, 'b> {
|
|||||||
invoc_args.push(format!("arg{} !== 0", i));
|
invoc_args.push(format!("arg{} !== 0", i));
|
||||||
abi_args.push(format!("arg{}", i));
|
abi_args.push(format!("arg{}", i));
|
||||||
}
|
}
|
||||||
shared::TYPE_BORROWED_STR |
|
// shared::TYPE_BORROWED_STR |
|
||||||
shared::TYPE_STRING |
|
// shared::TYPE_STRING |
|
||||||
shared::TYPE_VECTOR_U8 |
|
// shared::TYPE_VECTOR_U8 |
|
||||||
shared::TYPE_VECTOR_I8 |
|
// shared::TYPE_VECTOR_I8 |
|
||||||
shared::TYPE_SLICE_U8 |
|
// shared::TYPE_SLICE_U8 |
|
||||||
shared::TYPE_SLICE_I8 |
|
// shared::TYPE_SLICE_I8 |
|
||||||
shared::TYPE_VECTOR_U16 |
|
// shared::TYPE_VECTOR_U16 |
|
||||||
shared::TYPE_VECTOR_I16 |
|
// shared::TYPE_VECTOR_I16 |
|
||||||
shared::TYPE_SLICE_U16 |
|
// shared::TYPE_SLICE_U16 |
|
||||||
shared::TYPE_SLICE_I16 |
|
// shared::TYPE_SLICE_I16 |
|
||||||
shared::TYPE_VECTOR_U32 |
|
// shared::TYPE_VECTOR_U32 |
|
||||||
shared::TYPE_VECTOR_I32 |
|
// shared::TYPE_VECTOR_I32 |
|
||||||
shared::TYPE_SLICE_U32 |
|
// shared::TYPE_SLICE_U32 |
|
||||||
shared::TYPE_SLICE_I32 |
|
// shared::TYPE_SLICE_I32 |
|
||||||
shared::TYPE_VECTOR_F32 |
|
// shared::TYPE_VECTOR_F32 |
|
||||||
shared::TYPE_VECTOR_F64 |
|
// shared::TYPE_VECTOR_F64 |
|
||||||
shared::TYPE_SLICE_F32 |
|
// shared::TYPE_SLICE_F32 |
|
||||||
shared::TYPE_SLICE_F64 => {
|
// shared::TYPE_SLICE_F64 => {
|
||||||
let ty = VectorType::from(*arg);
|
// let ty = VectorType::from(*arg);
|
||||||
let f = self.cx.expose_get_vector_from_wasm(&ty);
|
// let f = self.cx.expose_get_vector_from_wasm(&ty);
|
||||||
abi_args.push(format!("ptr{}", i));
|
// abi_args.push(format!("ptr{}", i));
|
||||||
abi_args.push(format!("len{}", i));
|
// abi_args.push(format!("len{}", i));
|
||||||
extra.push_str(&format!("
|
// extra.push_str(&format!("
|
||||||
let arg{0} = {func}(ptr{0}, len{0});
|
// let arg{0} = {func}(ptr{0}, len{0});
|
||||||
", i, func = f));
|
// ", i, func = f));
|
||||||
invoc_args.push(format!("arg{}", i));
|
// invoc_args.push(format!("arg{}", i));
|
||||||
|
//
|
||||||
if ty.owned {
|
// if ty.owned {
|
||||||
extra.push_str(&format!("
|
// extra.push_str(&format!("
|
||||||
wasm.__wbindgen_free(ptr{0}, len{0});
|
// wasm.__wbindgen_free(ptr{0}, len{0});
|
||||||
", i));
|
// ", i));
|
||||||
self.cx.required_internal_exports.insert("__wbindgen_free");
|
// self.cx.required_internal_exports.insert("__wbindgen_free");
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
shared::TYPE_JS_OWNED => {
|
shared::TYPE_JS_OWNED => {
|
||||||
self.cx.expose_take_object();
|
self.cx.expose_take_object();
|
||||||
invoc_args.push(format!("takeObject(arg{})", i));
|
invoc_args.push(format!("takeObject(arg{})", i));
|
||||||
@ -1397,7 +1429,7 @@ impl<'a, 'b> SubContext<'a, 'b> {
|
|||||||
invoc_args.push(format!("getObject(arg{})", i));
|
invoc_args.push(format!("getObject(arg{})", i));
|
||||||
abi_args.push(format!("arg{}", i));
|
abi_args.push(format!("arg{}", i));
|
||||||
}
|
}
|
||||||
custom if (custom as u32) & shared::TYPE_CUSTOM_REF_FLAG == 0 => {
|
custom if custom & shared::TYPE_CUSTOM_REF_FLAG == 0 => {
|
||||||
let s = self.cx.custom_type_name(custom).to_string();
|
let s = self.cx.custom_type_name(custom).to_string();
|
||||||
abi_args.push(format!("ptr{}", i));
|
abi_args.push(format!("ptr{}", i));
|
||||||
let assign = if self.cx.config.debug {
|
let assign = if self.cx.config.debug {
|
||||||
@ -1501,25 +1533,25 @@ impl<'a, 'b> SubContext<'a, 'b> {
|
|||||||
self.cx.expose_add_heap_object();
|
self.cx.expose_add_heap_object();
|
||||||
format!("return addHeapObject({});", invoc)
|
format!("return addHeapObject({});", invoc)
|
||||||
}
|
}
|
||||||
Some(shared::TYPE_STRING) |
|
// Some(shared::TYPE_STRING) |
|
||||||
Some(shared::TYPE_VECTOR_U8) |
|
// Some(shared::TYPE_VECTOR_U8) |
|
||||||
Some(shared::TYPE_VECTOR_I8) |
|
// Some(shared::TYPE_VECTOR_I8) |
|
||||||
Some(shared::TYPE_VECTOR_U16) |
|
// Some(shared::TYPE_VECTOR_U16) |
|
||||||
Some(shared::TYPE_VECTOR_I16) |
|
// Some(shared::TYPE_VECTOR_I16) |
|
||||||
Some(shared::TYPE_VECTOR_U32) |
|
// Some(shared::TYPE_VECTOR_U32) |
|
||||||
Some(shared::TYPE_VECTOR_I32) |
|
// Some(shared::TYPE_VECTOR_I32) |
|
||||||
Some(shared::TYPE_VECTOR_F32) |
|
// Some(shared::TYPE_VECTOR_F32) |
|
||||||
Some(shared::TYPE_VECTOR_F64) => {
|
// Some(shared::TYPE_VECTOR_F64) => {
|
||||||
let ty = VectorType::from(import.function.ret.unwrap());
|
// let ty = VectorType::from(import.function.ret.unwrap());
|
||||||
let f = self.cx.pass_to_wasm_function(&ty);
|
// let f = self.cx.pass_to_wasm_function(&ty);
|
||||||
self.cx.expose_uint32_memory();
|
// self.cx.expose_uint32_memory();
|
||||||
abi_args.push("wasmretptr".to_string());
|
// abi_args.push("wasmretptr".to_string());
|
||||||
format!("
|
// format!("
|
||||||
const [retptr, retlen] = {}({});
|
// const [retptr, retlen] = {}({});
|
||||||
getUint32Memory()[wasmretptr / 4] = retlen;
|
// getUint32Memory()[wasmretptr / 4] = retlen;
|
||||||
return retptr;
|
// return retptr;
|
||||||
", f, invoc)
|
// ", f, invoc)
|
||||||
}
|
// }
|
||||||
None => invoc,
|
None => invoc,
|
||||||
_ => unimplemented!(),
|
_ => unimplemented!(),
|
||||||
};
|
};
|
||||||
@ -1632,8 +1664,8 @@ enum VectorKind {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl VectorType {
|
impl VectorType {
|
||||||
fn from(desc: char) -> VectorType {
|
fn from(desc: u32) -> Option<VectorType> {
|
||||||
match desc {
|
let ty = match desc {
|
||||||
shared::TYPE_BORROWED_STR => {
|
shared::TYPE_BORROWED_STR => {
|
||||||
VectorType { owned: false, kind: VectorKind::String }
|
VectorType { owned: false, kind: VectorKind::String }
|
||||||
}
|
}
|
||||||
@ -1691,8 +1723,9 @@ impl VectorType {
|
|||||||
shared::TYPE_VECTOR_JSVALUE => {
|
shared::TYPE_VECTOR_JSVALUE => {
|
||||||
VectorType { owned: true, kind: VectorKind::JsValue }
|
VectorType { owned: true, kind: VectorKind::JsValue }
|
||||||
}
|
}
|
||||||
_ => panic!()
|
_ => return None
|
||||||
}
|
};
|
||||||
|
Some(ty)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn js_ty(&self) -> &str {
|
fn js_ty(&self) -> &str {
|
||||||
@ -1709,4 +1742,19 @@ impl VectorType {
|
|||||||
VectorKind::JsValue => "any[]",
|
VectorKind::JsValue => "any[]",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn size(&self) -> usize {
|
||||||
|
match self.kind {
|
||||||
|
VectorKind::String => 1,
|
||||||
|
VectorKind::I8 => 1,
|
||||||
|
VectorKind::U8 => 1,
|
||||||
|
VectorKind::I16 => 2,
|
||||||
|
VectorKind::U16 => 2,
|
||||||
|
VectorKind::I32 => 4,
|
||||||
|
VectorKind::U32 => 4,
|
||||||
|
VectorKind::F32 => 4,
|
||||||
|
VectorKind::F64 => 8,
|
||||||
|
VectorKind::JsValue => 4,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -166,9 +166,6 @@ fn extract_programs(module: &mut Module) -> Vec<shared::Program> {
|
|||||||
let version = shared::version();
|
let version = shared::version();
|
||||||
let mut ret = Vec::new();
|
let mut ret = Vec::new();
|
||||||
|
|
||||||
#[repr(packed)]
|
|
||||||
struct Unaligned(u32);
|
|
||||||
|
|
||||||
module.sections_mut().retain(|s| {
|
module.sections_mut().retain(|s| {
|
||||||
let custom = match *s {
|
let custom = match *s {
|
||||||
Section::Custom(ref s) => s,
|
Section::Custom(ref s) => s,
|
||||||
@ -178,21 +175,16 @@ fn extract_programs(module: &mut Module) -> Vec<shared::Program> {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
assert!(custom.payload().len() % 4 == 0);
|
let mut payload = custom.payload();
|
||||||
let mut payload = unsafe {
|
|
||||||
slice::from_raw_parts(custom.payload().as_ptr() as *const Unaligned,
|
|
||||||
custom.payload().len() / 4)
|
|
||||||
};
|
|
||||||
|
|
||||||
while payload.len() > 0 {
|
while payload.len() > 0 {
|
||||||
let len = payload[0].0.to_le();
|
let len =
|
||||||
assert!(len % 4 == 0);
|
((payload[0] as usize) << 0) |
|
||||||
let (a, b) = payload[1..].split_at((len / 4) as usize);
|
((payload[1] as usize) << 8) |
|
||||||
|
((payload[2] as usize) << 16) |
|
||||||
|
((payload[3] as usize) << 24);
|
||||||
|
let (a, b) = payload[4..].split_at(len as usize);
|
||||||
payload = b;
|
payload = b;
|
||||||
let json = a.iter()
|
let p: shared::Program = match serde_json::from_slice(&a) {
|
||||||
.map(|i| char::from_u32(i.0.to_le()).unwrap())
|
|
||||||
.collect::<String>();
|
|
||||||
let p: shared::Program = match serde_json::from_str(&json) {
|
|
||||||
Ok(f) => f,
|
Ok(f) => f,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
panic!("failed to decode what looked like wasm-bindgen data: {}", e)
|
panic!("failed to decode what looked like wasm-bindgen data: {}", e)
|
||||||
|
@ -85,7 +85,7 @@ pub struct Function {
|
|||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
pub struct CustomTypeName {
|
pub struct CustomTypeName {
|
||||||
pub descriptor: char,
|
pub descriptor: u32,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -112,51 +112,42 @@ pub fn struct_function_export_name(struct_: &str, f: &str) -> String {
|
|||||||
return name
|
return name
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type Type = char;
|
pub type Type = u32;
|
||||||
|
|
||||||
pub const TYPE_VECTOR_JSVALUE: char = '\u{5b}';
|
pub const TYPE_VECTOR_JSVALUE: u32 = 0;
|
||||||
// Note: '\u{5c}' is '\' which breaks json encoding/decoding
|
pub const TYPE_ENUM: u32 = 1;
|
||||||
pub const TYPE_ENUM: char = '\u{5d}';
|
pub const TYPE_NUMBER: u32 = 2;
|
||||||
pub const TYPE_NUMBER: char = '\u{5e}';
|
pub const TYPE_BORROWED_STR: u32 = 3;
|
||||||
pub const TYPE_BORROWED_STR: char = '\u{5f}';
|
pub const TYPE_STRING: u32 = 4;
|
||||||
pub const TYPE_STRING: char = '\u{60}';
|
pub const TYPE_BOOLEAN: u32 = 5;
|
||||||
pub const TYPE_BOOLEAN: char = '\u{61}';
|
pub const TYPE_SLICE_U8: u32 = 6;
|
||||||
pub const TYPE_SLICE_U8: char = '\u{62}';
|
pub const TYPE_VECTOR_U8: u32 = 7;
|
||||||
pub const TYPE_VECTOR_U8: char = '\u{63}';
|
pub const TYPE_SLICE_I8: u32 = 8;
|
||||||
pub const TYPE_SLICE_I8: char = '\u{64}';
|
pub const TYPE_VECTOR_I8: u32 = 9;
|
||||||
pub const TYPE_VECTOR_I8: char = '\u{65}';
|
pub const TYPE_SLICE_U16: u32 = 10;
|
||||||
pub const TYPE_SLICE_U16: char = '\u{66}';
|
pub const TYPE_VECTOR_U16: u32 = 11;
|
||||||
pub const TYPE_VECTOR_U16: char = '\u{67}';
|
pub const TYPE_SLICE_I16: u32 = 12;
|
||||||
pub const TYPE_SLICE_I16: char = '\u{68}';
|
pub const TYPE_VECTOR_I16: u32 = 13;
|
||||||
pub const TYPE_VECTOR_I16: char = '\u{69}';
|
pub const TYPE_SLICE_U32: u32 = 14;
|
||||||
pub const TYPE_SLICE_U32: char = '\u{6a}';
|
pub const TYPE_VECTOR_U32: u32 = 15;
|
||||||
pub const TYPE_VECTOR_U32: char = '\u{6b}';
|
pub const TYPE_SLICE_I32: u32 = 16;
|
||||||
pub const TYPE_SLICE_I32: char = '\u{6c}';
|
pub const TYPE_VECTOR_I32: u32 = 17;
|
||||||
pub const TYPE_VECTOR_I32: char = '\u{6d}';
|
pub const TYPE_VECTOR_F32: u32 = 18;
|
||||||
pub const TYPE_VECTOR_F32: char = '\u{6e}';
|
pub const TYPE_SLICE_F32: u32 = 19;
|
||||||
pub const TYPE_SLICE_F32: char = '\u{6f}';
|
pub const TYPE_VECTOR_F64: u32 = 20;
|
||||||
pub const TYPE_VECTOR_F64: char = '\u{70}';
|
pub const TYPE_SLICE_F64: u32 = 21;
|
||||||
pub const TYPE_SLICE_F64: char = '\u{71}';
|
pub const TYPE_JS_OWNED: u32 = 22;
|
||||||
pub const TYPE_JS_OWNED: char = '\u{72}';
|
pub const TYPE_JS_REF: u32 = 23;
|
||||||
pub const TYPE_JS_REF: char = '\u{73}';
|
|
||||||
|
|
||||||
pub const TYPE_CUSTOM_START: u32 = 0x74;
|
pub const TYPE_CUSTOM_START: u32 = 24;
|
||||||
pub const TYPE_CUSTOM_REF_FLAG: u32 = 1;
|
pub const TYPE_CUSTOM_REF_FLAG: u32 = 1;
|
||||||
|
|
||||||
pub fn name_to_descriptor(name: &str) -> char {
|
pub fn name_to_descriptor(name: &str) -> u32 {
|
||||||
const CHAR_MAX: u32 = 0x10ffff;
|
const MAX: u32 = 10_000;
|
||||||
const CHAR_HOLE_START: u32 = 0xd800;
|
|
||||||
const CHAR_HOLE_END: u32 = 0xe000;
|
|
||||||
let mut h = fnv::FnvHasher::default();
|
let mut h = fnv::FnvHasher::default();
|
||||||
name.hash(&mut h);
|
name.hash(&mut h);
|
||||||
let val = h.finish();
|
((h.finish() as u32) % (MAX - TYPE_CUSTOM_START)) + TYPE_CUSTOM_START
|
||||||
let range = (CHAR_MAX - (CHAR_HOLE_END - CHAR_HOLE_START) - TYPE_CUSTOM_START) / 2;
|
|
||||||
let idx = (val % (range as u64)) as u32;
|
|
||||||
let mut ret = TYPE_CUSTOM_START + idx * 2;
|
|
||||||
if CHAR_HOLE_START <= ret && ret < CHAR_HOLE_END {
|
|
||||||
ret += CHAR_HOLE_END - CHAR_HOLE_START;
|
|
||||||
}
|
|
||||||
char::from_u32(ret).unwrap()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn version() -> String {
|
pub fn version() -> String {
|
||||||
|
278
src/convert.rs
278
src/convert.rs
@ -1,38 +1,68 @@
|
|||||||
use std::mem::{self, ManuallyDrop};
|
use std::mem::{self, ManuallyDrop};
|
||||||
use std::ops::{Deref, DerefMut};
|
use std::ops::{Deref, DerefMut};
|
||||||
|
use std::slice;
|
||||||
|
use std::str;
|
||||||
|
|
||||||
use super::JsValue;
|
use super::JsValue;
|
||||||
|
|
||||||
|
#[derive(PartialEq, Eq, Copy, Clone)]
|
||||||
|
pub struct Descriptor {
|
||||||
|
#[doc(hidden)]
|
||||||
|
pub __x: [u8; 4],
|
||||||
|
}
|
||||||
|
|
||||||
// keep in sync with shared/src/lib.rs TYPE constants
|
// keep in sync with shared/src/lib.rs TYPE constants
|
||||||
pub const DESCRIPTOR_CUSTOM_REF_FLAG: u32 = 0x1;
|
// pub const DESCRIPTOR_CUSTOM_REF_FLAG: Descriptor = Descriptor { __x: *b" 1", };
|
||||||
pub const DESCRIPTOR_NUMBER: u32 = 0x5e;
|
pub const DESCRIPTOR_NUMBER: Descriptor = Descriptor { __x: *b" 2", };
|
||||||
pub const DESCRIPTOR_BOOLEAN: u32 = 0x61;
|
pub const DESCRIPTOR_BORROWED_STR: Descriptor = Descriptor { __x: *b" 3", };
|
||||||
pub const DESCRIPTOR_JS_OWNED: u32 = 0x72;
|
pub const DESCRIPTOR_STRING: Descriptor = Descriptor { __x: *b" 4", };
|
||||||
|
pub const DESCRIPTOR_BOOLEAN: Descriptor = Descriptor { __x: *b" 5", };
|
||||||
|
|
||||||
|
pub const DESCRIPTOR_JS_OWNED: Descriptor = Descriptor { __x: *b" 22", };
|
||||||
|
pub const DESCRIPTOR_JS_REF: Descriptor = Descriptor { __x: *b" 23", };
|
||||||
|
|
||||||
pub trait WasmBoundary {
|
pub trait WasmBoundary {
|
||||||
type Js: WasmAbi;
|
type Abi: WasmAbi;
|
||||||
const DESCRIPTOR: u32;
|
const DESCRIPTOR: Descriptor;
|
||||||
|
|
||||||
fn into_js(self) -> Self::Js;
|
fn into_abi(self, extra: &mut Stack) -> Self::Abi;
|
||||||
unsafe fn from_js(js: Self::Js) -> Self;
|
unsafe fn from_abi(js: Self::Abi, extra: &mut Stack) -> Self;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait FromRefWasmBoundary: WasmBoundary {
|
pub trait FromRefWasmBoundary {
|
||||||
|
type Abi: WasmAbi;
|
||||||
|
const DESCRIPTOR: Descriptor;
|
||||||
type RefAnchor: Deref<Target = Self>;
|
type RefAnchor: Deref<Target = Self>;
|
||||||
|
|
||||||
unsafe fn from_js_ref(js: Self::Js) -> Self::RefAnchor;
|
unsafe fn from_abi_ref(js: Self::Abi, extra: &mut Stack) -> Self::RefAnchor;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait FromRefMutWasmBoundary: WasmBoundary {
|
pub trait FromRefMutWasmBoundary {
|
||||||
|
type Abi: WasmAbi;
|
||||||
|
const DESCRIPTOR: Descriptor;
|
||||||
type RefAnchor: DerefMut<Target = Self>;
|
type RefAnchor: DerefMut<Target = Self>;
|
||||||
|
|
||||||
unsafe fn from_js_ref_mut(js: Self::Js) -> Self::RefAnchor;
|
unsafe fn from_abi_ref_mut(js: Self::Abi, extra: &mut Stack) -> Self::RefAnchor;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait ToRefWasmBoundary: WasmBoundary {
|
pub trait ToRefWasmBoundary {
|
||||||
fn to_js_ref(&self) -> u32;
|
type Abi: WasmAbi;
|
||||||
|
const DESCRIPTOR: Descriptor;
|
||||||
|
|
||||||
|
fn to_abi_ref(&self, extra: &mut Stack) -> u32;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub trait Stack {
|
||||||
|
fn push(&mut self, bits: u32);
|
||||||
|
fn pop(&mut self) -> u32;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// An unsafe trait which represents types that are ABI-safe to pass via wasm
|
||||||
|
/// arguments.
|
||||||
|
///
|
||||||
|
/// This is an unsafe trait to implement as there's no guarantee the type is
|
||||||
|
/// actually safe to transfer across the was boundary, it's up to you to
|
||||||
|
/// guarantee this so codegen works correctly.
|
||||||
pub unsafe trait WasmAbi {}
|
pub unsafe trait WasmAbi {}
|
||||||
|
|
||||||
unsafe impl WasmAbi for u32 {}
|
unsafe impl WasmAbi for u32 {}
|
||||||
@ -43,11 +73,11 @@ unsafe impl WasmAbi for f64 {}
|
|||||||
macro_rules! simple {
|
macro_rules! simple {
|
||||||
($($t:tt)*) => ($(
|
($($t:tt)*) => ($(
|
||||||
impl WasmBoundary for $t {
|
impl WasmBoundary for $t {
|
||||||
type Js = $t;
|
type Abi = $t;
|
||||||
const DESCRIPTOR: u32 = DESCRIPTOR_NUMBER;
|
const DESCRIPTOR: Descriptor = DESCRIPTOR_NUMBER;
|
||||||
|
|
||||||
fn into_js(self) -> $t { self }
|
fn into_abi(self, _extra: &mut Stack) -> $t { self }
|
||||||
unsafe fn from_js(js: $t) -> $t { js }
|
unsafe fn from_abi(js: $t, _extra: &mut Stack) -> $t { js }
|
||||||
}
|
}
|
||||||
)*)
|
)*)
|
||||||
}
|
}
|
||||||
@ -57,11 +87,11 @@ simple!(u32 u64 f32 f64);
|
|||||||
macro_rules! as_u32 {
|
macro_rules! as_u32 {
|
||||||
($($t:tt)*) => ($(
|
($($t:tt)*) => ($(
|
||||||
impl WasmBoundary for $t {
|
impl WasmBoundary for $t {
|
||||||
type Js = u32;
|
type Abi = u32;
|
||||||
const DESCRIPTOR: u32 = DESCRIPTOR_NUMBER;
|
const DESCRIPTOR: Descriptor = DESCRIPTOR_NUMBER;
|
||||||
|
|
||||||
fn into_js(self) -> u32 { self as u32 }
|
fn into_abi(self, _extra: &mut Stack) -> u32 { self as u32 }
|
||||||
unsafe fn from_js(js: u32) -> $t { js as $t }
|
unsafe fn from_abi(js: u32, _extra: &mut Stack) -> $t { js as $t }
|
||||||
}
|
}
|
||||||
)*)
|
)*)
|
||||||
}
|
}
|
||||||
@ -69,54 +99,222 @@ macro_rules! as_u32 {
|
|||||||
as_u32!(i8 u8 i16 u16 i32 isize usize);
|
as_u32!(i8 u8 i16 u16 i32 isize usize);
|
||||||
|
|
||||||
impl WasmBoundary for bool {
|
impl WasmBoundary for bool {
|
||||||
type Js = u32;
|
type Abi = u32;
|
||||||
const DESCRIPTOR: u32 = DESCRIPTOR_BOOLEAN;
|
const DESCRIPTOR: Descriptor = DESCRIPTOR_BOOLEAN;
|
||||||
|
|
||||||
fn into_js(self) -> u32 { self as u32 }
|
fn into_abi(self, _extra: &mut Stack) -> u32 { self as u32 }
|
||||||
unsafe fn from_js(js: u32) -> bool { js != 0 }
|
unsafe fn from_abi(js: u32, _extra: &mut Stack) -> bool { js != 0 }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> WasmBoundary for *const T {
|
impl<T> WasmBoundary for *const T {
|
||||||
type Js = u32;
|
type Abi = u32;
|
||||||
const DESCRIPTOR: u32 = DESCRIPTOR_NUMBER;
|
const DESCRIPTOR: Descriptor = DESCRIPTOR_NUMBER;
|
||||||
|
|
||||||
fn into_js(self) -> u32 { self as u32 }
|
fn into_abi(self, _extra: &mut Stack) -> u32 { self as u32 }
|
||||||
unsafe fn from_js(js: u32) -> *const T { js as *const T }
|
unsafe fn from_abi(js: u32, _extra: &mut Stack) -> *const T { js as *const T }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> WasmBoundary for *mut T {
|
impl<T> WasmBoundary for *mut T {
|
||||||
type Js = u32;
|
type Abi = u32;
|
||||||
const DESCRIPTOR: u32 = DESCRIPTOR_NUMBER;
|
const DESCRIPTOR: Descriptor = DESCRIPTOR_NUMBER;
|
||||||
|
|
||||||
fn into_js(self) -> u32 { self as u32 }
|
fn into_abi(self, _extra: &mut Stack) -> u32 { self as u32 }
|
||||||
unsafe fn from_js(js: u32) -> *mut T { js as *mut T }
|
unsafe fn from_abi(js: u32, _extra: &mut Stack) -> *mut T { js as *mut T }
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! vectors {
|
||||||
|
($($t:ident => ($owned:expr, $slice:expr))*) => ($(
|
||||||
|
impl WasmBoundary for Box<[$t]> {
|
||||||
|
type Abi = u32;
|
||||||
|
const DESCRIPTOR: Descriptor = Descriptor { __x: *$owned };
|
||||||
|
|
||||||
|
fn into_abi(self, extra: &mut Stack) -> u32 {
|
||||||
|
let ptr = self.as_ptr();
|
||||||
|
let len = self.len();
|
||||||
|
mem::forget(self);
|
||||||
|
extra.push(len as u32);
|
||||||
|
ptr.into_abi(extra)
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe fn from_abi(js: u32, extra: &mut Stack) -> Box<[$t]> {
|
||||||
|
let ptr = <*mut $t>::from_abi(js, extra);
|
||||||
|
let len = extra.pop() as usize;
|
||||||
|
Vec::from_raw_parts(ptr, len, len).into_boxed_slice()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ToRefWasmBoundary for [$t] {
|
||||||
|
type Abi = u32;
|
||||||
|
const DESCRIPTOR: Descriptor = Descriptor { __x: *$slice };
|
||||||
|
|
||||||
|
fn to_abi_ref(&self, extra: &mut Stack) -> u32 {
|
||||||
|
let ptr = self.as_ptr();
|
||||||
|
let len = self.len();
|
||||||
|
extra.push(len as u32);
|
||||||
|
ptr.into_abi(extra)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FromRefWasmBoundary for [$t] {
|
||||||
|
type Abi = u32;
|
||||||
|
const DESCRIPTOR: Descriptor = Descriptor { __x: *$slice };
|
||||||
|
type RefAnchor = SliceAnchor<$t>;
|
||||||
|
|
||||||
|
unsafe fn from_abi_ref(js: u32, extra: &mut Stack) -> SliceAnchor<$t> {
|
||||||
|
SliceAnchor {
|
||||||
|
ptr: <*mut $t>::from_abi(js, extra),
|
||||||
|
len: extra.pop() as usize,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)*)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct SliceAnchor<T> {
|
||||||
|
ptr: *mut T,
|
||||||
|
len: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Deref for SliceAnchor<T> {
|
||||||
|
type Target = [T];
|
||||||
|
fn deref(&self) -> &[T] {
|
||||||
|
unsafe { slice::from_raw_parts(self.ptr, self.len) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
vectors! {
|
||||||
|
u8 => (b" 6", b" 7")
|
||||||
|
i8 => (b" 8", b" 9")
|
||||||
|
u16 => (b" 10", b" 11")
|
||||||
|
i16 => (b" 12", b" 13")
|
||||||
|
u32 => (b" 14", b" 15")
|
||||||
|
i32 => (b" 16", b" 17")
|
||||||
|
f32 => (b" 18", b" 19")
|
||||||
|
f64 => (b" 20", b" 21")
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> WasmBoundary for Vec<T> where Box<[T]>: WasmBoundary {
|
||||||
|
type Abi = <Box<[T]> as WasmBoundary>::Abi;
|
||||||
|
const DESCRIPTOR: Descriptor = <Box<[T]> as WasmBoundary>::DESCRIPTOR;
|
||||||
|
|
||||||
|
fn into_abi(self, extra: &mut Stack) -> Self::Abi {
|
||||||
|
self.into_boxed_slice().into_abi(extra)
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe fn from_abi(js: Self::Abi, extra: &mut Stack) -> Self {
|
||||||
|
<Box<[T]>>::from_abi(js, extra).into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl WasmBoundary for String {
|
||||||
|
type Abi = u32;
|
||||||
|
const DESCRIPTOR: Descriptor = DESCRIPTOR_STRING;
|
||||||
|
|
||||||
|
fn into_abi(self, extra: &mut Stack) -> u32 {
|
||||||
|
self.into_bytes().into_abi(extra)
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe fn from_abi(js: u32, extra: &mut Stack) -> String {
|
||||||
|
String::from_utf8_unchecked(Vec::from_abi(js, extra))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ToRefWasmBoundary for str {
|
||||||
|
type Abi = <[u8] as ToRefWasmBoundary>::Abi;
|
||||||
|
const DESCRIPTOR: Descriptor = DESCRIPTOR_BORROWED_STR;
|
||||||
|
|
||||||
|
fn to_abi_ref(&self, extra: &mut Stack) -> Self::Abi {
|
||||||
|
self.as_bytes().to_abi_ref(extra)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FromRefWasmBoundary for str {
|
||||||
|
type Abi = <[u8] as ToRefWasmBoundary>::Abi;
|
||||||
|
const DESCRIPTOR: Descriptor = DESCRIPTOR_BORROWED_STR;
|
||||||
|
type RefAnchor = StrAnchor;
|
||||||
|
|
||||||
|
unsafe fn from_abi_ref(js: Self::Abi, extra: &mut Stack) -> Self::RefAnchor {
|
||||||
|
StrAnchor { inner: <[u8]>::from_abi_ref(js, extra) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct StrAnchor {
|
||||||
|
inner: SliceAnchor<u8>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Deref for StrAnchor {
|
||||||
|
type Target = str;
|
||||||
|
|
||||||
|
fn deref(&self) -> &str {
|
||||||
|
unsafe { str::from_utf8_unchecked(&self.inner) }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WasmBoundary for JsValue {
|
impl WasmBoundary for JsValue {
|
||||||
type Js = u32;
|
type Abi = u32;
|
||||||
const DESCRIPTOR: u32 = DESCRIPTOR_JS_OWNED;
|
const DESCRIPTOR: Descriptor = DESCRIPTOR_JS_OWNED;
|
||||||
|
|
||||||
fn into_js(self) -> u32 {
|
fn into_abi(self, _extra: &mut Stack) -> u32 {
|
||||||
let ret = self.idx;
|
let ret = self.idx;
|
||||||
mem::forget(self);
|
mem::forget(self);
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe fn from_js(js: u32) -> JsValue {
|
unsafe fn from_abi(js: u32, _extra: &mut Stack) -> JsValue {
|
||||||
JsValue { idx: js }
|
JsValue { idx: js }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ToRefWasmBoundary for JsValue {
|
impl ToRefWasmBoundary for JsValue {
|
||||||
fn to_js_ref(&self) -> u32 {
|
type Abi = u32;
|
||||||
|
const DESCRIPTOR: Descriptor = DESCRIPTOR_JS_REF;
|
||||||
|
fn to_abi_ref(&self, _extra: &mut Stack) -> u32 {
|
||||||
self.idx
|
self.idx
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromRefWasmBoundary for JsValue {
|
impl FromRefWasmBoundary for JsValue {
|
||||||
|
type Abi = u32;
|
||||||
|
const DESCRIPTOR: Descriptor = DESCRIPTOR_JS_REF;
|
||||||
type RefAnchor = ManuallyDrop<JsValue>;
|
type RefAnchor = ManuallyDrop<JsValue>;
|
||||||
|
|
||||||
unsafe fn from_js_ref(js: u32) -> ManuallyDrop<JsValue> {
|
unsafe fn from_abi_ref(js: u32, _extra: &mut Stack) -> ManuallyDrop<JsValue> {
|
||||||
ManuallyDrop::new(JsValue { idx: js })
|
ManuallyDrop::new(JsValue { idx: js })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct GlobalStack { next: usize }
|
||||||
|
|
||||||
|
const GLOBAL_STACK_CAP: usize = 16;
|
||||||
|
static mut GLOBAL_STACK: [u32; GLOBAL_STACK_CAP] = [0; GLOBAL_STACK_CAP];
|
||||||
|
|
||||||
|
impl GlobalStack {
|
||||||
|
pub unsafe fn new() -> GlobalStack {
|
||||||
|
GlobalStack { next: 0 }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Stack for GlobalStack {
|
||||||
|
fn push(&mut self, val: u32) {
|
||||||
|
unsafe {
|
||||||
|
assert!(self.next < GLOBAL_STACK_CAP);
|
||||||
|
GLOBAL_STACK[self.next] = val;
|
||||||
|
self.next += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn pop(&mut self) -> u32 {
|
||||||
|
unsafe {
|
||||||
|
assert!(self.next < GLOBAL_STACK_CAP);
|
||||||
|
let ret = GLOBAL_STACK[self.next];
|
||||||
|
self.next += 1;
|
||||||
|
ret
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
|
#[no_mangle]
|
||||||
|
pub unsafe extern fn __wbindgen_global_argument_ptr() -> *mut u32 {
|
||||||
|
GLOBAL_STACK.as_mut_ptr()
|
||||||
|
}
|
||||||
|
15
src/lib.rs
15
src/lib.rs
@ -459,20 +459,5 @@ pub mod __rt {
|
|||||||
drop(Vec::<u8>::from_raw_parts(ptr, 0, size));
|
drop(Vec::<u8>::from_raw_parts(ptr, 0, size));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
|
||||||
pub unsafe extern fn __wbindgen_boxed_str_len(ptr: *mut String) -> usize {
|
|
||||||
(*ptr).len()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[no_mangle]
|
|
||||||
pub unsafe extern fn __wbindgen_boxed_str_ptr(ptr: *mut String) -> *const u8 {
|
|
||||||
(*ptr).as_ptr()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[no_mangle]
|
|
||||||
pub unsafe extern fn __wbindgen_boxed_str_free(ptr: *mut String) {
|
|
||||||
drop(Box::from_raw(ptr));
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn link_this_library() {}
|
pub fn link_this_library() {}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user