mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-18 15:31:25 +00:00
Start handling strings
This commit is contained in:
@ -12,6 +12,8 @@ pub struct Function {
|
||||
|
||||
pub enum Type {
|
||||
Integer(syn::Ident),
|
||||
BorrowedStr,
|
||||
String,
|
||||
}
|
||||
|
||||
impl Function {
|
||||
@ -100,21 +102,41 @@ impl Function {
|
||||
|
||||
impl Type {
|
||||
pub fn from(ty: &syn::Type) -> Type {
|
||||
let extract_path_ident = |path: &syn::Path| {
|
||||
if path.leading_colon.is_some() {
|
||||
panic!("unsupported leading colon in path")
|
||||
}
|
||||
if path.segments.len() != 1 {
|
||||
panic!("unsupported path that needs name resolution")
|
||||
}
|
||||
match path.segments.get(0).item().arguments {
|
||||
syn::PathArguments::None => {}
|
||||
_ => panic!("unsupported path that has path arguments")
|
||||
}
|
||||
path.segments.get(0).item().ident
|
||||
};
|
||||
match *ty {
|
||||
// syn::Type::Reference(ref r) => {
|
||||
// }
|
||||
syn::Type::Reference(ref r) => {
|
||||
if r.lifetime.is_some() {
|
||||
panic!("can't have lifetimes on references yet");
|
||||
}
|
||||
match r.ty.mutability {
|
||||
syn::Mutability::Immutable => {}
|
||||
_ => panic!("can't have mutable references yet"),
|
||||
}
|
||||
match r.ty.ty {
|
||||
syn::Type::Path(syn::TypePath { qself: None, ref path }) => {
|
||||
let ident = extract_path_ident(path);
|
||||
match ident.sym.as_str() {
|
||||
"str" => Type::BorrowedStr,
|
||||
_ => panic!("unsupported reference type"),
|
||||
}
|
||||
}
|
||||
_ => panic!("unsupported reference type"),
|
||||
}
|
||||
}
|
||||
syn::Type::Path(syn::TypePath { qself: None, ref path }) => {
|
||||
if path.leading_colon.is_some() {
|
||||
panic!("unsupported leading colon in path")
|
||||
}
|
||||
if path.segments.len() != 1 {
|
||||
panic!("unsupported path that needs name resolution")
|
||||
}
|
||||
match path.segments.get(0).item().arguments {
|
||||
syn::PathArguments::None => {}
|
||||
_ => panic!("unsupported path that has path arguments")
|
||||
}
|
||||
let ident = path.segments.get(0).item().ident;
|
||||
let ident = extract_path_ident(path);
|
||||
match ident.sym.as_str() {
|
||||
"i8" |
|
||||
"u8" |
|
||||
@ -128,6 +150,7 @@ impl Type {
|
||||
"f64" => {
|
||||
Type::Integer(ident)
|
||||
}
|
||||
"String" => Type::String,
|
||||
s => panic!("unsupported type: {}", s),
|
||||
}
|
||||
}
|
||||
@ -138,6 +161,8 @@ impl Type {
|
||||
fn shared(&self) -> shared::Type {
|
||||
match *self {
|
||||
Type::Integer(_) => shared::Type::Number,
|
||||
Type::BorrowedStr => shared::Type::BorrowedStr,
|
||||
Type::String => shared::Type::String,
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -146,6 +171,13 @@ impl ToTokens for Type {
|
||||
fn to_tokens(&self, tokens: &mut Tokens) {
|
||||
match *self {
|
||||
Type::Integer(i) => i.to_tokens(tokens),
|
||||
Type::String => {
|
||||
syn::Ident::from("String").to_tokens(tokens);
|
||||
}
|
||||
Type::BorrowedStr => {
|
||||
<Token![&]>::default().to_tokens(tokens);
|
||||
syn::Ident::from("str").to_tokens(tokens);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,24 +2,30 @@
|
||||
|
||||
extern crate syn;
|
||||
#[macro_use]
|
||||
extern crate synom;
|
||||
#[macro_use]
|
||||
extern crate quote;
|
||||
extern crate proc_macro;
|
||||
extern crate proc_macro2;
|
||||
extern crate serde_json;
|
||||
extern crate wasm_bindgen_shared;
|
||||
|
||||
use std::sync::atomic::*;
|
||||
|
||||
use proc_macro::TokenStream;
|
||||
use proc_macro2::Literal;
|
||||
use quote::{Tokens, ToTokens};
|
||||
|
||||
mod ast;
|
||||
|
||||
static MALLOC_GENERATED: AtomicBool = ATOMIC_BOOL_INIT;
|
||||
static BOXED_STR_GENERATED: AtomicBool = ATOMIC_BOOL_INIT;
|
||||
|
||||
#[proc_macro]
|
||||
pub fn wasm_bindgen(input: TokenStream) -> TokenStream {
|
||||
let file = syn::parse::<syn::File>(input)
|
||||
.expect("expected a set of valid Rust items");
|
||||
|
||||
|
||||
let mut ret = Tokens::new();
|
||||
|
||||
for item in file.items.iter() {
|
||||
@ -39,19 +45,48 @@ fn bindgen_fn(input: &syn::ItemFn, into: &mut Tokens) {
|
||||
let export_name = function.export_name();
|
||||
let generated_name = function.rust_symbol();
|
||||
let mut args = vec![];
|
||||
let arg_conversions = vec![quote!{}];
|
||||
let mut arg_conversions = vec![];
|
||||
let real_name = &input.ident;
|
||||
let mut converted_arguments = vec![];
|
||||
let ret = syn::Ident::from("_ret");
|
||||
|
||||
let mut malloc = false;
|
||||
let mut boxed_str = false;
|
||||
|
||||
for (i, ty) in function.arguments.iter().enumerate() {
|
||||
let ident = syn::Ident::from(format!("arg{}", i));
|
||||
match *ty {
|
||||
ast::Type::Integer(i) => {
|
||||
args.push(quote! { #ident: #i });
|
||||
converted_arguments.push(quote! { #ident });
|
||||
}
|
||||
ast::Type::BorrowedStr => {
|
||||
malloc = !MALLOC_GENERATED.swap(true, Ordering::SeqCst);
|
||||
let ptr = syn::Ident::from(format!("arg{}_ptr", i));
|
||||
let len = syn::Ident::from(format!("arg{}_len", i));
|
||||
args.push(quote! { #ptr: *const u8 });
|
||||
args.push(quote! { #len: usize });
|
||||
arg_conversions.push(quote! {
|
||||
let #ident = unsafe {
|
||||
let slice = ::std::slice::from_raw_parts(#ptr, #len);
|
||||
::std::str::from_utf8_unchecked(slice)
|
||||
};
|
||||
});
|
||||
}
|
||||
ast::Type::String => {
|
||||
malloc = !MALLOC_GENERATED.swap(true, Ordering::SeqCst);
|
||||
let ptr = syn::Ident::from(format!("arg{}_ptr", i));
|
||||
let len = syn::Ident::from(format!("arg{}_len", i));
|
||||
args.push(quote! { #ptr: *mut u8 });
|
||||
args.push(quote! { #len: usize });
|
||||
arg_conversions.push(quote! {
|
||||
let #ident = unsafe {
|
||||
let vec = ::std::vec::Vec::from_raw_parts(#ptr, #len, #len);
|
||||
::std::string::String::from_utf8_unchecked(vec)
|
||||
};
|
||||
});
|
||||
}
|
||||
}
|
||||
converted_arguments.push(quote! { #ident });
|
||||
}
|
||||
let ret_ty;
|
||||
let convert_ret;
|
||||
@ -60,6 +95,12 @@ fn bindgen_fn(input: &syn::ItemFn, into: &mut Tokens) {
|
||||
ret_ty = quote! { -> #i };
|
||||
convert_ret = quote! { #ret };
|
||||
}
|
||||
Some(ast::Type::BorrowedStr) => panic!("can't return a borrowed string"),
|
||||
Some(ast::Type::String) => {
|
||||
boxed_str = !BOXED_STR_GENERATED.swap(true, Ordering::SeqCst);
|
||||
ret_ty = quote! { -> *mut String };
|
||||
convert_ret = quote! { Box::into_raw(Box::new(#ret)) };
|
||||
}
|
||||
None => {
|
||||
ret_ty = quote! {};
|
||||
convert_ret = quote! {};
|
||||
@ -74,8 +115,52 @@ fn bindgen_fn(input: &syn::ItemFn, into: &mut Tokens) {
|
||||
};
|
||||
let generated_static_length = generated_static.len();
|
||||
|
||||
let malloc = if malloc {
|
||||
quote! {
|
||||
#[no_mangle]
|
||||
pub extern fn __wbindgen_malloc(size: usize) -> *mut u8 {
|
||||
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 {
|
||||
quote! {
|
||||
}
|
||||
};
|
||||
|
||||
let boxed_str = if boxed_str {
|
||||
quote! {
|
||||
#[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 {
|
||||
quote! {
|
||||
}
|
||||
};
|
||||
|
||||
let tokens = quote! {
|
||||
#malloc
|
||||
#boxed_str
|
||||
|
||||
#[no_mangle]
|
||||
#[allow(non_upper_case_globals)]
|
||||
pub static #generated_static_name: [u8; #generated_static_length] =
|
||||
@ -84,10 +169,11 @@ fn bindgen_fn(input: &syn::ItemFn, into: &mut Tokens) {
|
||||
#[no_mangle]
|
||||
#[export_name = #export_name]
|
||||
pub extern fn #generated_name(#(#args),*) #ret_ty {
|
||||
#(#arg_conversions);*
|
||||
#(#arg_conversions)*
|
||||
let #ret = #real_name(#(#converted_arguments),*);
|
||||
#convert_ret
|
||||
}
|
||||
};
|
||||
// println!("{}", tokens);
|
||||
tokens.to_tokens(into);
|
||||
}
|
||||
|
Reference in New Issue
Block a user