2017-12-14 19:31:01 -08:00
|
|
|
#![feature(proc_macro)]
|
|
|
|
|
|
|
|
extern crate syn;
|
2017-12-14 21:55:21 -08:00
|
|
|
#[macro_use]
|
2017-12-14 19:31:01 -08:00
|
|
|
extern crate quote;
|
|
|
|
extern crate proc_macro;
|
|
|
|
extern crate proc_macro2;
|
|
|
|
extern crate serde_json;
|
|
|
|
extern crate wasm_bindgen_shared;
|
|
|
|
|
2017-12-14 21:55:21 -08:00
|
|
|
use std::sync::atomic::*;
|
|
|
|
|
2017-12-14 19:31:01 -08:00
|
|
|
use proc_macro::TokenStream;
|
2017-12-18 14:44:09 -08:00
|
|
|
use proc_macro2::{Literal, Span};
|
2017-12-14 19:31:01 -08:00
|
|
|
use quote::{Tokens, ToTokens};
|
|
|
|
|
|
|
|
mod ast;
|
|
|
|
|
2017-12-14 21:55:21 -08:00
|
|
|
static MALLOC_GENERATED: AtomicBool = ATOMIC_BOOL_INIT;
|
|
|
|
static BOXED_STR_GENERATED: AtomicBool = ATOMIC_BOOL_INIT;
|
|
|
|
|
2017-12-18 14:44:09 -08:00
|
|
|
macro_rules! my_quote {
|
|
|
|
($($t:tt)*) => (quote_spanned!(Span::call_site(), $($t)*))
|
|
|
|
}
|
|
|
|
|
2017-12-14 19:31:01 -08:00
|
|
|
#[proc_macro]
|
|
|
|
pub fn wasm_bindgen(input: TokenStream) -> TokenStream {
|
2017-12-18 19:01:37 -08:00
|
|
|
// Parse the input as a list of Rust items, reusing the `syn::File` parser.
|
2017-12-14 19:31:01 -08:00
|
|
|
let file = syn::parse::<syn::File>(input)
|
|
|
|
.expect("expected a set of valid Rust items");
|
|
|
|
|
|
|
|
let mut ret = Tokens::new();
|
|
|
|
|
2017-12-18 12:39:14 -08:00
|
|
|
let mut program = ast::Program {
|
|
|
|
structs: Vec::new(),
|
|
|
|
free_functions: Vec::new(),
|
2017-12-18 21:43:16 -08:00
|
|
|
imports: Vec::new(),
|
2017-12-18 12:39:14 -08:00
|
|
|
};
|
|
|
|
|
2017-12-18 19:01:37 -08:00
|
|
|
// Translate all input items into our own internal representation (the `ast`
|
|
|
|
// module). We'll be panicking here on anything that we can't process
|
|
|
|
|
2017-12-14 19:31:01 -08:00
|
|
|
for item in file.items.iter() {
|
|
|
|
match *item {
|
2017-12-18 12:39:14 -08:00
|
|
|
syn::Item::Fn(ref f) => {
|
2017-12-18 19:01:37 -08:00
|
|
|
item.to_tokens(&mut ret);
|
2017-12-18 12:39:14 -08:00
|
|
|
program.free_functions.push(ast::Function::from(f));
|
|
|
|
}
|
|
|
|
syn::Item::Struct(ref s) => {
|
2017-12-18 19:01:37 -08:00
|
|
|
item.to_tokens(&mut ret);
|
2017-12-18 12:39:14 -08:00
|
|
|
let s = ast::Struct::from(s);
|
|
|
|
if program.structs.iter().any(|a| a.name == s.name) {
|
|
|
|
panic!("redefinition of struct: {}", s.name);
|
|
|
|
}
|
|
|
|
program.structs.push(s);
|
|
|
|
}
|
2017-12-18 21:43:16 -08:00
|
|
|
syn::Item::Impl(ref i) => {
|
2017-12-18 19:01:37 -08:00
|
|
|
item.to_tokens(&mut ret);
|
2017-12-18 21:43:16 -08:00
|
|
|
program.push_impl(i);
|
|
|
|
}
|
|
|
|
syn::Item::ForeignMod(ref f) => {
|
|
|
|
program.push_foreign_mod(f);
|
2017-12-18 19:01:37 -08:00
|
|
|
}
|
2017-12-14 19:31:01 -08:00
|
|
|
_ => panic!("unexpected item in bindgen macro"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-18 19:01:37 -08:00
|
|
|
// Generate wrappers for all the items that we've found
|
|
|
|
|
2017-12-18 12:39:14 -08:00
|
|
|
for function in program.free_functions.iter() {
|
|
|
|
bindgen_fn(function, &mut ret);
|
|
|
|
}
|
|
|
|
for s in program.structs.iter() {
|
|
|
|
bindgen_struct(s, &mut ret);
|
|
|
|
}
|
2017-12-18 21:43:16 -08:00
|
|
|
for i in program.imports.iter() {
|
|
|
|
bindgen_import(i, &mut ret);
|
|
|
|
}
|
2017-12-18 12:39:14 -08:00
|
|
|
|
2017-12-18 19:01:37 -08:00
|
|
|
// Finally 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.
|
|
|
|
|
2017-12-18 12:39:14 -08:00
|
|
|
static CNT: AtomicUsize = ATOMIC_USIZE_INIT;
|
|
|
|
let generated_static_name = format!("__WASM_BINDGEN_GENERATED{}",
|
|
|
|
CNT.fetch_add(1, Ordering::SeqCst));
|
2017-12-18 14:31:01 -08:00
|
|
|
let generated_static_name = syn::Ident::from(generated_static_name);
|
2017-12-18 12:39:14 -08:00
|
|
|
let mut generated_static = String::from("wbg:");
|
|
|
|
generated_static.push_str(&serde_json::to_string(&program.shared()).unwrap());
|
|
|
|
let generated_static_value = syn::Lit {
|
|
|
|
value: syn::LitKind::Other(Literal::byte_string(generated_static.as_bytes())),
|
|
|
|
span: Default::default(),
|
|
|
|
};
|
|
|
|
let generated_static_length = generated_static.len();
|
|
|
|
|
2017-12-18 14:44:09 -08:00
|
|
|
(my_quote! {
|
2017-12-18 12:39:14 -08:00
|
|
|
#[no_mangle]
|
|
|
|
#[allow(non_upper_case_globals)]
|
|
|
|
pub static #generated_static_name: [u8; #generated_static_length] =
|
|
|
|
*#generated_static_value;
|
|
|
|
}).to_tokens(&mut ret);
|
|
|
|
|
2017-12-19 09:25:41 -08:00
|
|
|
// println!("{}", ret);
|
2017-12-18 14:31:01 -08:00
|
|
|
|
2017-12-14 19:31:01 -08:00
|
|
|
ret.into()
|
|
|
|
}
|
|
|
|
|
2017-12-18 12:39:14 -08:00
|
|
|
fn bindgen_fn(function: &ast::Function, into: &mut Tokens) {
|
2017-12-18 14:31:01 -08:00
|
|
|
bindgen(&function.free_function_export_name(),
|
|
|
|
function.rust_symbol(None),
|
|
|
|
Receiver::FreeFunction(function.name),
|
|
|
|
&function.arguments,
|
|
|
|
function.ret.as_ref(),
|
|
|
|
into)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bindgen_struct(s: &ast::Struct, into: &mut Tokens) {
|
|
|
|
for f in s.functions.iter() {
|
|
|
|
bindgen_struct_fn(s, f, into);
|
|
|
|
}
|
|
|
|
for f in s.methods.iter() {
|
|
|
|
bindgen_struct_method(s, f, into);
|
|
|
|
}
|
|
|
|
|
|
|
|
let name = &s.name;
|
|
|
|
let free_fn = s.free_function();
|
2017-12-18 14:44:09 -08:00
|
|
|
(my_quote! {
|
2017-12-18 14:31:01 -08:00
|
|
|
#[no_mangle]
|
2017-12-19 19:50:06 -08:00
|
|
|
pub unsafe extern fn #free_fn(ptr: *mut ::wasm_bindgen::__rt::WasmRefCell<#name>) {
|
|
|
|
::wasm_bindgen::__rt::assert_not_null(ptr);
|
2017-12-18 14:31:01 -08:00
|
|
|
drop(Box::from_raw(ptr));
|
|
|
|
}
|
|
|
|
}).to_tokens(into);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bindgen_struct_fn(s: &ast::Struct, f: &ast::Function, into: &mut Tokens) {
|
|
|
|
bindgen(&f.struct_function_export_name(s.name),
|
|
|
|
f.rust_symbol(Some(s.name)),
|
|
|
|
Receiver::StructFunction(s.name, f.name),
|
|
|
|
&f.arguments,
|
|
|
|
f.ret.as_ref(),
|
|
|
|
into)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bindgen_struct_method(s: &ast::Struct, m: &ast::Method, into: &mut Tokens) {
|
|
|
|
bindgen(&m.function.struct_function_export_name(s.name),
|
|
|
|
m.function.rust_symbol(Some(s.name)),
|
|
|
|
Receiver::StructMethod(s.name, m.mutable, m.function.name),
|
|
|
|
&m.function.arguments,
|
|
|
|
m.function.ret.as_ref(),
|
|
|
|
into)
|
|
|
|
}
|
|
|
|
|
|
|
|
enum Receiver {
|
|
|
|
FreeFunction(syn::Ident),
|
|
|
|
StructFunction(syn::Ident, syn::Ident),
|
|
|
|
StructMethod(syn::Ident, bool, syn::Ident),
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bindgen(export_name: &syn::Lit,
|
|
|
|
generated_name: syn::Ident,
|
|
|
|
receiver: Receiver,
|
|
|
|
arguments: &[ast::Type],
|
|
|
|
ret_type: Option<&ast::Type>,
|
|
|
|
into: &mut Tokens) {
|
2017-12-14 19:31:01 -08:00
|
|
|
let mut args = vec![];
|
2017-12-14 21:55:21 -08:00
|
|
|
let mut arg_conversions = vec![];
|
2017-12-14 19:31:01 -08:00
|
|
|
let mut converted_arguments = vec![];
|
|
|
|
let ret = syn::Ident::from("_ret");
|
|
|
|
|
2017-12-14 21:55:21 -08:00
|
|
|
let mut malloc = false;
|
|
|
|
let mut boxed_str = false;
|
|
|
|
|
2017-12-18 14:31:01 -08:00
|
|
|
let mut offset = 0;
|
|
|
|
if let Receiver::StructMethod(class, _, _) = receiver {
|
2017-12-19 19:50:06 -08:00
|
|
|
args.push(my_quote! { me: *mut ::wasm_bindgen::__rt::WasmRefCell<#class> });
|
2017-12-18 14:44:09 -08:00
|
|
|
arg_conversions.push(my_quote! {
|
2017-12-19 19:50:06 -08:00
|
|
|
::wasm_bindgen::__rt::assert_not_null(me);
|
2017-12-18 14:31:01 -08:00
|
|
|
let me = unsafe { &*me };
|
|
|
|
});
|
|
|
|
offset = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i, ty) in arguments.iter().enumerate() {
|
|
|
|
let i = i + offset;
|
2017-12-14 19:31:01 -08:00
|
|
|
let ident = syn::Ident::from(format!("arg{}", i));
|
|
|
|
match *ty {
|
|
|
|
ast::Type::Integer(i) => {
|
2017-12-18 14:44:09 -08:00
|
|
|
args.push(my_quote! { #ident: #i });
|
2017-12-14 21:55:21 -08:00
|
|
|
}
|
2017-12-20 10:22:18 -08:00
|
|
|
ast::Type::Boolean => {
|
|
|
|
args.push(my_quote! { #ident: u32 });
|
|
|
|
arg_conversions.push(my_quote! {
|
|
|
|
let #ident = #ident != 0;
|
|
|
|
});
|
|
|
|
}
|
2017-12-20 07:35:14 -08:00
|
|
|
ast::Type::RawMutPtr(i) => {
|
|
|
|
args.push(my_quote! { #ident: *mut #i });
|
|
|
|
}
|
|
|
|
ast::Type::RawConstPtr(i) => {
|
|
|
|
args.push(my_quote! { #ident: *const #i });
|
|
|
|
}
|
2017-12-14 21:55:21 -08:00
|
|
|
ast::Type::BorrowedStr => {
|
2017-12-18 16:13:37 -08:00
|
|
|
malloc = malloc || !MALLOC_GENERATED.swap(true, Ordering::SeqCst);
|
2017-12-14 21:55:21 -08:00
|
|
|
let ptr = syn::Ident::from(format!("arg{}_ptr", i));
|
|
|
|
let len = syn::Ident::from(format!("arg{}_len", i));
|
2017-12-18 14:44:09 -08:00
|
|
|
args.push(my_quote! { #ptr: *const u8 });
|
|
|
|
args.push(my_quote! { #len: usize });
|
|
|
|
arg_conversions.push(my_quote! {
|
2017-12-14 21:55:21 -08:00
|
|
|
let #ident = unsafe {
|
|
|
|
let slice = ::std::slice::from_raw_parts(#ptr, #len);
|
|
|
|
::std::str::from_utf8_unchecked(slice)
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
ast::Type::String => {
|
2017-12-18 16:13:37 -08:00
|
|
|
malloc = malloc || !MALLOC_GENERATED.swap(true, Ordering::SeqCst);
|
2017-12-14 21:55:21 -08:00
|
|
|
let ptr = syn::Ident::from(format!("arg{}_ptr", i));
|
|
|
|
let len = syn::Ident::from(format!("arg{}_len", i));
|
2017-12-18 14:44:09 -08:00
|
|
|
args.push(my_quote! { #ptr: *mut u8 });
|
|
|
|
args.push(my_quote! { #len: usize });
|
|
|
|
arg_conversions.push(my_quote! {
|
2017-12-14 21:55:21 -08:00
|
|
|
let #ident = unsafe {
|
|
|
|
let vec = ::std::vec::Vec::from_raw_parts(#ptr, #len, #len);
|
|
|
|
::std::string::String::from_utf8_unchecked(vec)
|
|
|
|
};
|
|
|
|
});
|
2017-12-14 19:31:01 -08:00
|
|
|
}
|
2017-12-18 12:39:14 -08:00
|
|
|
ast::Type::ByValue(name) => {
|
2017-12-19 19:50:06 -08:00
|
|
|
args.push(my_quote! { #ident: *mut ::wasm_bindgen::__rt::WasmRefCell<#name> });
|
2017-12-18 14:44:09 -08:00
|
|
|
arg_conversions.push(my_quote! {
|
2017-12-19 19:50:06 -08:00
|
|
|
::wasm_bindgen::__rt::assert_not_null(#ident);
|
2017-12-18 12:39:14 -08:00
|
|
|
let #ident = unsafe {
|
|
|
|
(*#ident).borrow_mut();
|
|
|
|
Box::from_raw(#ident).into_inner()
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
ast::Type::ByRef(name) => {
|
2017-12-19 19:50:06 -08:00
|
|
|
args.push(my_quote! { #ident: *mut ::wasm_bindgen::__rt::WasmRefCell<#name> });
|
2017-12-18 14:44:09 -08:00
|
|
|
arg_conversions.push(my_quote! {
|
2017-12-19 19:50:06 -08:00
|
|
|
::wasm_bindgen::__rt::assert_not_null(#ident);
|
2017-12-18 12:39:14 -08:00
|
|
|
let #ident = unsafe { (*#ident).borrow() };
|
|
|
|
let #ident = &*#ident;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
ast::Type::ByMutRef(name) => {
|
2017-12-19 19:50:06 -08:00
|
|
|
args.push(my_quote! { #ident: *mut ::wasm_bindgen::__rt::WasmRefCell<#name> });
|
2017-12-18 14:44:09 -08:00
|
|
|
arg_conversions.push(my_quote! {
|
2017-12-19 19:50:06 -08:00
|
|
|
::wasm_bindgen::__rt::assert_not_null(#ident);
|
2017-12-18 12:39:14 -08:00
|
|
|
let mut #ident = unsafe { (*#ident).borrow_mut() };
|
|
|
|
let #ident = &mut *#ident;
|
|
|
|
});
|
|
|
|
}
|
2017-12-19 09:25:41 -08:00
|
|
|
ast::Type::JsObject => {
|
|
|
|
args.push(my_quote! { #ident: u32 });
|
|
|
|
arg_conversions.push(my_quote! {
|
|
|
|
let #ident = ::wasm_bindgen::JsObject::__from_idx(#ident);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
ast::Type::JsObjectRef => {
|
|
|
|
args.push(my_quote! { #ident: u32 });
|
|
|
|
arg_conversions.push(my_quote! {
|
|
|
|
let #ident = ::std::mem::ManuallyDrop::new(
|
|
|
|
::wasm_bindgen::JsObject::__from_idx(#ident)
|
|
|
|
);
|
|
|
|
let #ident = &*#ident;
|
|
|
|
});
|
|
|
|
}
|
2017-12-14 19:31:01 -08:00
|
|
|
}
|
2017-12-18 14:44:09 -08:00
|
|
|
converted_arguments.push(my_quote! { #ident });
|
2017-12-14 19:31:01 -08:00
|
|
|
}
|
|
|
|
let ret_ty;
|
|
|
|
let convert_ret;
|
2017-12-18 14:31:01 -08:00
|
|
|
match ret_type {
|
|
|
|
Some(&ast::Type::Integer(i)) => {
|
2017-12-18 14:44:09 -08:00
|
|
|
ret_ty = my_quote! { -> #i };
|
|
|
|
convert_ret = my_quote! { #ret };
|
2017-12-14 19:31:01 -08:00
|
|
|
}
|
2017-12-20 10:22:18 -08:00
|
|
|
Some(&ast::Type::Boolean) => {
|
|
|
|
ret_ty = my_quote! { -> u32 };
|
|
|
|
convert_ret = my_quote! { #ret as u32 };
|
|
|
|
}
|
2017-12-20 07:35:14 -08:00
|
|
|
Some(&ast::Type::RawMutPtr(i)) => {
|
|
|
|
ret_ty = my_quote! { -> *mut #i };
|
|
|
|
convert_ret = my_quote! { #ret };
|
|
|
|
}
|
|
|
|
Some(&ast::Type::RawConstPtr(i)) => {
|
|
|
|
ret_ty = my_quote! { -> *const #i };
|
|
|
|
convert_ret = my_quote! { #ret };
|
|
|
|
}
|
2017-12-18 14:31:01 -08:00
|
|
|
Some(&ast::Type::BorrowedStr) => panic!("can't return a borrowed string"),
|
|
|
|
Some(&ast::Type::ByRef(_)) => panic!("can't return a borrowed ref"),
|
|
|
|
Some(&ast::Type::ByMutRef(_)) => panic!("can't return a borrowed ref"),
|
|
|
|
Some(&ast::Type::String) => {
|
2017-12-14 21:55:21 -08:00
|
|
|
boxed_str = !BOXED_STR_GENERATED.swap(true, Ordering::SeqCst);
|
2017-12-18 14:44:09 -08:00
|
|
|
ret_ty = my_quote! { -> *mut String };
|
|
|
|
convert_ret = my_quote! { Box::into_raw(Box::new(#ret)) };
|
2017-12-14 21:55:21 -08:00
|
|
|
}
|
2017-12-18 14:31:01 -08:00
|
|
|
Some(&ast::Type::ByValue(name)) => {
|
2017-12-19 19:50:06 -08:00
|
|
|
ret_ty = my_quote! { -> *mut ::wasm_bindgen::__rt::WasmRefCell<#name> };
|
2017-12-18 14:44:09 -08:00
|
|
|
convert_ret = my_quote! {
|
2017-12-19 19:50:06 -08:00
|
|
|
Box::into_raw(Box::new(::wasm_bindgen::__rt::WasmRefCell::new(#ret)))
|
2017-12-18 12:39:14 -08:00
|
|
|
};
|
|
|
|
}
|
2017-12-19 09:25:41 -08:00
|
|
|
Some(&ast::Type::JsObject) => {
|
|
|
|
ret_ty = my_quote! { -> u32 };
|
|
|
|
convert_ret = my_quote! {
|
|
|
|
::wasm_bindgen::JsObject::__into_idx(#ret)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
Some(&ast::Type::JsObjectRef) => {
|
|
|
|
panic!("can't return a borrowed ref");
|
|
|
|
}
|
2017-12-14 19:31:01 -08:00
|
|
|
None => {
|
2017-12-18 14:44:09 -08:00
|
|
|
ret_ty = my_quote! {};
|
|
|
|
convert_ret = my_quote! {};
|
2017-12-14 19:31:01 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 21:55:21 -08:00
|
|
|
let malloc = if malloc {
|
2017-12-18 14:44:09 -08:00
|
|
|
my_quote! {
|
2017-12-14 21:55:21 -08:00
|
|
|
#[no_mangle]
|
|
|
|
pub extern fn __wbindgen_malloc(size: usize) -> *mut u8 {
|
2017-12-19 19:50:06 -08:00
|
|
|
// Any malloc request this big is bogus anyway. If this actually
|
|
|
|
// goes down to `Vec` we trigger a whole bunch of panicking
|
|
|
|
// machinery to get pulled in from libstd anyway as it'll verify
|
|
|
|
// the size passed in below.
|
|
|
|
//
|
|
|
|
// Head this all off by just aborting on too-big sizes. This
|
|
|
|
// avoids panicking (code bloat) and gives a better error
|
|
|
|
// message too hopefully.
|
|
|
|
if size >= usize::max_value() / 2 {
|
|
|
|
::wasm_bindgen::throw("invalid malloc request");
|
|
|
|
}
|
2017-12-14 21:55:21 -08:00
|
|
|
let mut ret = Vec::with_capacity(size);
|
|
|
|
let ptr = ret.as_mut_ptr();
|
|
|
|
::std::mem::forget(ret);
|
|
|
|
return ptr
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern fn __wbindgen_free(ptr: *mut u8, size: usize) {
|
|
|
|
drop(Vec::<u8>::from_raw_parts(ptr, 0, size));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2017-12-18 14:44:09 -08:00
|
|
|
my_quote! {
|
2017-12-14 21:55:21 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let boxed_str = if boxed_str {
|
2017-12-18 14:44:09 -08:00
|
|
|
my_quote! {
|
2017-12-14 21:55:21 -08:00
|
|
|
#[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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2017-12-18 14:44:09 -08:00
|
|
|
my_quote! {
|
2017-12-14 21:55:21 -08:00
|
|
|
}
|
|
|
|
};
|
2017-12-14 19:31:01 -08:00
|
|
|
|
2017-12-18 14:44:09 -08:00
|
|
|
let tokens = my_quote! {
|
2017-12-14 21:55:21 -08:00
|
|
|
#malloc
|
|
|
|
#boxed_str
|
|
|
|
|
2017-12-14 19:31:01 -08:00
|
|
|
#[export_name = #export_name]
|
2017-12-18 14:31:01 -08:00
|
|
|
#[allow(non_snake_case)]
|
2017-12-14 19:31:01 -08:00
|
|
|
pub extern fn #generated_name(#(#args),*) #ret_ty {
|
2017-12-14 21:55:21 -08:00
|
|
|
#(#arg_conversions)*
|
2017-12-18 14:31:01 -08:00
|
|
|
let #ret = #receiver(#(#converted_arguments),*);
|
2017-12-14 19:31:01 -08:00
|
|
|
#convert_ret
|
|
|
|
}
|
|
|
|
};
|
|
|
|
tokens.to_tokens(into);
|
|
|
|
}
|
2017-12-18 12:39:14 -08:00
|
|
|
|
2017-12-18 14:31:01 -08:00
|
|
|
impl ToTokens for Receiver {
|
|
|
|
fn to_tokens(&self, tokens: &mut Tokens) {
|
|
|
|
match *self {
|
|
|
|
Receiver::FreeFunction(name) => name.to_tokens(tokens),
|
|
|
|
Receiver::StructFunction(s, name) => {
|
|
|
|
s.to_tokens(tokens);
|
2017-12-27 10:27:58 +01:00
|
|
|
syn::token::Colon2::default().to_tokens(tokens);
|
2017-12-18 14:31:01 -08:00
|
|
|
name.to_tokens(tokens);
|
|
|
|
}
|
|
|
|
Receiver::StructMethod(_, mutable, name) => {
|
2017-12-18 15:54:43 -08:00
|
|
|
(my_quote! { me }).to_tokens(tokens);
|
2017-12-27 10:27:58 +01:00
|
|
|
syn::token::Dot::default().to_tokens(tokens);
|
2017-12-18 14:31:01 -08:00
|
|
|
if mutable {
|
|
|
|
syn::Ident::from("borrow_mut").to_tokens(tokens);
|
|
|
|
} else {
|
|
|
|
syn::Ident::from("borrow").to_tokens(tokens);
|
|
|
|
}
|
|
|
|
tokens.append_delimited("(", Default::default(), |_| ());
|
2017-12-27 10:27:58 +01:00
|
|
|
syn::token::Dot::default().to_tokens(tokens);
|
2017-12-18 14:31:01 -08:00
|
|
|
name.to_tokens(tokens);
|
|
|
|
}
|
|
|
|
}
|
2017-12-18 12:39:14 -08:00
|
|
|
}
|
|
|
|
}
|
2017-12-18 21:43:16 -08:00
|
|
|
|
|
|
|
fn bindgen_import(import: &ast::Import, tokens: &mut Tokens) {
|
|
|
|
let vis = &import.vis;
|
|
|
|
let ret = &import.decl.output;
|
|
|
|
let name = &import.ident;
|
|
|
|
let fn_token = &import.decl.fn_token;
|
|
|
|
let arguments = &import.decl.inputs;
|
|
|
|
|
|
|
|
let mut abi_argument_names = Vec::new();
|
|
|
|
let mut abi_arguments = Vec::new();
|
|
|
|
let mut arg_conversions = Vec::new();
|
|
|
|
let ret_ident = syn::Ident::from("_ret");
|
|
|
|
|
|
|
|
let names = import.decl.inputs
|
|
|
|
.iter()
|
|
|
|
.map(|i| i.into_item())
|
|
|
|
.map(|arg| {
|
|
|
|
match *arg {
|
|
|
|
syn::FnArg::Captured(ref c) => c,
|
|
|
|
_ => panic!("arguments cannot be `self` or ignored"),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.map(|arg| {
|
|
|
|
match arg.pat {
|
|
|
|
syn::Pat::Ident(syn::PatIdent {
|
|
|
|
mode: syn::BindingMode::ByValue(_),
|
|
|
|
ident,
|
|
|
|
subpat: None,
|
|
|
|
..
|
|
|
|
}) => {
|
|
|
|
ident
|
|
|
|
}
|
|
|
|
_ => panic!("unsupported pattern in foreign function"),
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
for (ty, name) in import.function.arguments.iter().zip(names) {
|
|
|
|
match *ty {
|
|
|
|
ast::Type::Integer(i) => {
|
|
|
|
abi_argument_names.push(name);
|
|
|
|
abi_arguments.push(my_quote! { #name: #i });
|
|
|
|
arg_conversions.push(my_quote! {});
|
|
|
|
}
|
2017-12-20 10:22:18 -08:00
|
|
|
ast::Type::Boolean => {
|
|
|
|
abi_argument_names.push(name);
|
|
|
|
abi_arguments.push(my_quote! { #name: u32 });
|
|
|
|
arg_conversions.push(my_quote! { let #name = #name as u32; });
|
|
|
|
}
|
2017-12-20 07:35:14 -08:00
|
|
|
ast::Type::RawMutPtr(i) => {
|
|
|
|
abi_argument_names.push(name);
|
|
|
|
abi_arguments.push(my_quote! { #name: *mut #i });
|
|
|
|
arg_conversions.push(my_quote! {});
|
|
|
|
}
|
|
|
|
ast::Type::RawConstPtr(i) => {
|
|
|
|
abi_argument_names.push(name);
|
|
|
|
abi_arguments.push(my_quote! { #name: *const #i });
|
|
|
|
arg_conversions.push(my_quote! {});
|
|
|
|
}
|
2017-12-18 21:43:16 -08:00
|
|
|
ast::Type::BorrowedStr => {
|
|
|
|
let ptr = syn::Ident::from(format!("{}_ptr", name));
|
|
|
|
let len = syn::Ident::from(format!("{}_len", name));
|
|
|
|
abi_argument_names.push(ptr);
|
|
|
|
abi_argument_names.push(len);
|
|
|
|
abi_arguments.push(my_quote! { #ptr: *const u8 });
|
|
|
|
abi_arguments.push(my_quote! { #len: usize });
|
|
|
|
arg_conversions.push(my_quote! {
|
|
|
|
let #ptr = #name.as_ptr();
|
|
|
|
let #len = #name.len();
|
|
|
|
});
|
|
|
|
}
|
2017-12-19 09:25:41 -08:00
|
|
|
ast::Type::JsObject => {
|
|
|
|
abi_argument_names.push(name);
|
|
|
|
abi_arguments.push(my_quote! { #name: u32 });
|
|
|
|
arg_conversions.push(my_quote! {
|
|
|
|
let #name = ::wasm_bindgen::JsObject::__into_idx(#name);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
ast::Type::JsObjectRef => {
|
|
|
|
abi_argument_names.push(name);
|
|
|
|
abi_arguments.push(my_quote! { #name: u32 });
|
|
|
|
arg_conversions.push(my_quote! {
|
|
|
|
let #name = ::wasm_bindgen::JsObject::__get_idx(#name);
|
|
|
|
});
|
|
|
|
}
|
2017-12-18 21:43:16 -08:00
|
|
|
ast::Type::String => panic!("can't use `String` in foreign functions"),
|
|
|
|
ast::Type::ByValue(_name) |
|
|
|
|
ast::Type::ByRef(_name) |
|
|
|
|
ast::Type::ByMutRef(_name) => {
|
2017-12-19 09:25:41 -08:00
|
|
|
panic!("can't use struct types in foreign functions yet");
|
2017-12-18 21:43:16 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let abi_ret;
|
|
|
|
let convert_ret;
|
|
|
|
match import.function.ret {
|
|
|
|
Some(ast::Type::Integer(i)) => {
|
|
|
|
abi_ret = my_quote! { #i };
|
|
|
|
convert_ret = my_quote! { #ret_ident };
|
|
|
|
}
|
2017-12-20 10:22:18 -08:00
|
|
|
Some(ast::Type::Boolean) => {
|
|
|
|
abi_ret = my_quote! { u32 };
|
|
|
|
convert_ret = my_quote! { #ret_ident != 0 };
|
|
|
|
}
|
2017-12-20 07:35:14 -08:00
|
|
|
Some(ast::Type::RawConstPtr(i)) => {
|
|
|
|
abi_ret = my_quote! { *const #i };
|
|
|
|
convert_ret = my_quote! { #ret_ident };
|
|
|
|
}
|
|
|
|
Some(ast::Type::RawMutPtr(i)) => {
|
|
|
|
abi_ret = my_quote! { *mut #i };
|
|
|
|
convert_ret = my_quote! { #ret_ident };
|
|
|
|
}
|
2017-12-19 09:25:41 -08:00
|
|
|
Some(ast::Type::JsObject) => {
|
|
|
|
abi_ret = my_quote! { u32 };
|
|
|
|
convert_ret = my_quote! {
|
|
|
|
::wasm_bindgen::JsObject::__from_idx(#ret_ident)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
Some(ast::Type::JsObjectRef) => panic!("can't return a borrowed ref"),
|
2017-12-18 21:43:16 -08:00
|
|
|
Some(ast::Type::BorrowedStr) => panic!("can't return a borrowed string"),
|
|
|
|
Some(ast::Type::ByRef(_)) => panic!("can't return a borrowed ref"),
|
|
|
|
Some(ast::Type::ByMutRef(_)) => panic!("can't return a borrowed ref"),
|
|
|
|
Some(ast::Type::String) => panic!("can't return a string in foreign functions"),
|
|
|
|
Some(ast::Type::ByValue(_)) => panic!("can't return a struct in a foreign function"),
|
|
|
|
None => {
|
|
|
|
abi_ret = my_quote! { () };
|
|
|
|
convert_ret = my_quote! {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(quote! {
|
|
|
|
#vis #fn_token #name(#arguments) #ret {
|
|
|
|
extern {
|
|
|
|
fn #name(#(#abi_arguments),*) -> #abi_ret;
|
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
#(#arg_conversions)*
|
|
|
|
let #ret_ident = #name(#(#abi_argument_names),*);
|
|
|
|
#convert_ret
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).to_tokens(tokens);
|
|
|
|
}
|