From 9b6804a01becbd3a481b78190e1e52ecacd4ab32 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Wed, 18 Jul 2018 17:59:24 -0500 Subject: [PATCH] Translate `ByteString` in WebIDL to `[u8]` (#505) In arguments take `&[u8]` and in return value return `Vec`. Should help fill out a few more APIs on `Header` and `Response`! --- crates/backend/Cargo.toml | 7 ++++--- crates/backend/src/defined.rs | 7 ++++++- crates/backend/src/lib.rs | 2 ++ crates/webidl/src/lib.rs | 2 +- crates/webidl/src/util.rs | 34 +++++++++++++++++++++++++++++++++- 5 files changed, 46 insertions(+), 6 deletions(-) diff --git a/crates/backend/Cargo.toml b/crates/backend/Cargo.toml index 068eb045..332edbf5 100644 --- a/crates/backend/Cargo.toml +++ b/crates/backend/Cargo.toml @@ -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" } diff --git a/crates/backend/src/defined.rs b/crates/backend/src/defined.rs index 9b503555..1fcbe740 100644 --- a/crates/backend/src/defined.rs +++ b/crates/backend/src/defined.rs @@ -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 }); diff --git a/crates/backend/src/lib.rs b/crates/backend/src/lib.rs index 5eca3c23..7b2ae253 100755 --- a/crates/backend/src/lib.rs +++ b/crates/backend/src/lib.rs @@ -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; diff --git a/crates/webidl/src/lib.rs b/crates/webidl/src/lib.rs index 17612b45..fe29c732 100644 --- a/crates/webidl/src/lib.rs +++ b/crates/webidl/src/lib.rs @@ -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())), ); diff --git a/crates/webidl/src/util.rs b/crates/webidl/src/util.rs index e8de5e7c..65660290 100644 --- a/crates/webidl/src/util.rs +++ b/crates/webidl/src/util.rs @@ -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` 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