Translate ByteString in WebIDL to [u8] (#505)

In arguments take `&[u8]` and in return value return `Vec<u8>`. Should help fill
out a few more APIs on `Header` and `Response`!
This commit is contained in:
Alex Crichton 2018-07-18 17:59:24 -05:00 committed by GitHub
parent 32fa5724dd
commit 9b6804a01b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 46 additions and 6 deletions

View File

@ -15,8 +15,9 @@ spans = ["proc-macro2/nightly"]
extra-traits = ["syn/extra-traits"]
[dependencies]
quote = '0.6'
log = "0.4"
proc-macro2 = "0.4.8"
wasm-bindgen-shared = { path = "../shared", version = "=0.2.11" }
syn = { version = '0.14', features = ['full', 'visit-mut'] }
quote = '0.6'
serde_json = "1.0"
syn = { version = '0.14', features = ['full', 'visit-mut'] }
wasm-bindgen-shared = { path = "../shared", version = "=0.2.11" }

View File

@ -269,7 +269,12 @@ where
self.retain(|x| {
let mut all_defined = true;
x.imported_type_references(&mut |id| {
all_defined = all_defined && is_defined(id);
if all_defined {
if !is_defined(id) {
info!("removing due to {} not being defined", id);
all_defined = false;
}
}
});
all_defined
});

View File

@ -1,6 +1,8 @@
#![recursion_limit = "256"]
#![cfg_attr(feature = "extra-traits", deny(missing_debug_implementations))]
#[macro_use]
extern crate log;
extern crate proc_macro2;
#[macro_use]
extern crate quote;

View File

@ -79,7 +79,7 @@ fn compile_ast(mut ast: backend::ast::Program) -> String {
let mut defined = BTreeSet::from_iter(
vec![
"str", "char", "bool", "JsValue", "u8", "i8", "u16", "i16", "u32", "i32", "u64", "i64",
"usize", "isize", "f32", "f64", "Result",
"usize", "isize", "f32", "f64", "Result", "String", "Vec",
].into_iter()
.map(|id| proc_macro2::Ident::new(id, proc_macro2::Span::call_site())),
);

View File

@ -89,6 +89,30 @@ fn result_ty(t: syn::Type) -> syn::Type {
ty.into()
}
fn slice_ty(t: syn::Type) -> syn::Type {
syn::TypeSlice {
bracket_token: Default::default(),
elem: Box::new(t),
}.into()
}
fn vec_ty(t: syn::Type) -> syn::Type {
let arguments = syn::PathArguments::AngleBracketed(syn::AngleBracketedGenericArguments {
colon2_token: None,
lt_token: Default::default(),
args: FromIterator::from_iter(vec![
syn::GenericArgument::Type(t),
]),
gt_token: Default::default(),
});
let ident = raw_ident("Vec");
let seg = syn::PathSegment { ident, arguments };
let path: syn::Path = seg.into();
let ty = syn::TypePath { qself: None, path };
ty.into()
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum TypePosition {
Argument,
@ -155,10 +179,18 @@ impl<'a> FirstPassRecord<'a> {
// `DOMString` is not supported yet in other positions.
webidl::ast::TypeKind::DOMString => return None,
// `ByteString -> `&[u8]` for arguments
webidl::ast::TypeKind::ByteString if pos == TypePosition::Argument => {
shared_ref(slice_ty(ident_ty(raw_ident("u8"))))
}
// ... and `Vec<u8>` for arguments
webidl::ast::TypeKind::ByteString => {
vec_ty(ident_ty(raw_ident("u8")))
}
// Support for these types is not yet implemented, so skip
// generating any bindings for this function.
webidl::ast::TypeKind::ArrayBuffer
| webidl::ast::TypeKind::ByteString
| webidl::ast::TypeKind::DataView
| webidl::ast::TypeKind::Error
| webidl::ast::TypeKind::Float32Array