From cb8411b346fcff490361396f08e1bc7d46992c14 Mon Sep 17 00:00:00 2001 From: Benjamin Kampmann Date: Mon, 27 Aug 2018 14:13:13 +0200 Subject: [PATCH 1/5] Add BufferSource && ArrayBufferView to &[u8] --- crates/webidl/src/idl_type.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/crates/webidl/src/idl_type.rs b/crates/webidl/src/idl_type.rs index 8a681900..44c1f8e6 100644 --- a/crates/webidl/src/idl_type.rs +++ b/crates/webidl/src/idl_type.rs @@ -40,6 +40,8 @@ pub(crate) enum IdlType<'a> { Uint32Array, Float32Array, Float64Array, + ArrayBufferView, + BufferSource, Interface(&'a str), Dictionary(&'a str), @@ -116,6 +118,8 @@ impl<'a> ToIdlType<'a> for NonAnyType<'a> { NonAnyType::Float32Array(t) => t.to_idl_type(record), NonAnyType::Float64Array(t) => t.to_idl_type(record), NonAnyType::FrozenArrayType(t) => t.to_idl_type(record), + NonAnyType::ArrayBufferView(t) => t.to_idl_type(record), + NonAnyType::BufferSource(t) => t.to_idl_type(record), NonAnyType::RecordType(t) => t.to_idl_type(record), NonAnyType::Identifier(t) => t.to_idl_type(record), } @@ -331,6 +335,8 @@ terms_to_idl_type! { Uint8ClampedArray => Uint8ClampedArray Float32Array => Float32Array Float64Array => Float64Array + ArrayBufferView => ArrayBufferView + BufferSource => BufferSource Error => Error } @@ -369,6 +375,8 @@ impl<'a> IdlType<'a> { IdlType::Uint32Array => dst.push_str("u32_array"), IdlType::Float32Array => dst.push_str("f32_array"), IdlType::Float64Array => dst.push_str("f64_array"), + IdlType::ArrayBufferView => dst.push_str("array_buffer_view"), + IdlType::BufferSource => dst.push_str("buffer_source"), IdlType::Interface(name) => dst.push_str(&snake_case_ident(name)), IdlType::Dictionary(name) => dst.push_str(&snake_case_ident(name)), @@ -458,7 +466,10 @@ impl<'a> IdlType<'a> { IdlType::Float32Array => Some(array("f32", pos)), IdlType::Float64Array => Some(array("f64", pos)), - | IdlType::Interface(name) + // we alias ArrayBufferView & BufferSource to &u8 in Rust + IdlType::ArrayBufferView + | IdlType::BufferSource => Some(array("u8", pos)), + IdlType::Interface(name) | IdlType::Dictionary(name) => { let ty = ident_ty(rust_ident(camel_case_ident(name).as_str())); if pos == TypePosition::Argument { From 031ba39036de3b4fb9586924b9f2f1088c730fe2 Mon Sep 17 00:00:00 2001 From: Benjamin Kampmann Date: Mon, 27 Aug 2018 20:53:57 +0200 Subject: [PATCH 2/5] Bump weedle version --- crates/webidl/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/webidl/Cargo.toml b/crates/webidl/Cargo.toml index bcf03b11..e530e818 100644 --- a/crates/webidl/Cargo.toml +++ b/crates/webidl/Cargo.toml @@ -20,4 +20,4 @@ proc-macro2 = "0.4.8" quote = '0.6' syn = { version = '0.14', features = ['full'] } wasm-bindgen-backend = { version = "=0.2.19", path = "../backend" } -weedle = "0.6" +weedle = "0.7" From 8b08fc16c5081f2cce3c9bdf275ae44c3450b4de Mon Sep 17 00:00:00 2001 From: Benjamin Kampmann Date: Thu, 30 Aug 2018 12:15:37 +0200 Subject: [PATCH 3/5] Making ArrayBufferView & BufferSource a union --- crates/webidl/src/idl_type.rs | 9 ++++++--- crates/webidl/src/lib.rs | 28 ++++++++++++++++++++++------ 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/crates/webidl/src/idl_type.rs b/crates/webidl/src/idl_type.rs index 44c1f8e6..2fa33b05 100644 --- a/crates/webidl/src/idl_type.rs +++ b/crates/webidl/src/idl_type.rs @@ -466,9 +466,10 @@ impl<'a> IdlType<'a> { IdlType::Float32Array => Some(array("f32", pos)), IdlType::Float64Array => Some(array("f64", pos)), - // we alias ArrayBufferView & BufferSource to &u8 in Rust - IdlType::ArrayBufferView - | IdlType::BufferSource => Some(array("u8", pos)), + IdlType::ArrayBufferView | IdlType::BufferSource => { + let path = vec![rust_ident("js_sys"), rust_ident("Object")]; + Some(leading_colon_path_ty(path)) + }, IdlType::Interface(name) | IdlType::Dictionary(name) => { let ty = ident_ty(rust_ident(camel_case_ident(name).as_str())); @@ -573,6 +574,8 @@ impl<'a> IdlType<'a> { .iter() .flat_map(|idl_type| idl_type.flatten()) .collect(), + IdlType::ArrayBufferView | IdlType::BufferSource => + vec![IdlType::Object, IdlType::Uint8Array], idl_type @ _ => vec![idl_type.clone()], } diff --git a/crates/webidl/src/lib.rs b/crates/webidl/src/lib.rs index b450acfb..c8253644 100644 --- a/crates/webidl/src/lib.rs +++ b/crates/webidl/src/lib.rs @@ -291,12 +291,28 @@ impl<'src> FirstPassRecord<'src> { // Slice types aren't supported because they don't implement // `Into` - if let syn::Type::Reference(ty) = &ty { - match &*ty.elem { - syn::Type::Slice(_) => return None, - _ => {} - } - } + match ty { + syn::Type::Reference(ref i) => + match &*i.elem { + syn::Type::Slice(_) => return None, + _ => () + } + syn::Type::Path(ref path, ..) => + // check that our inner don't contains slices either + for seg in path.path.segments.iter() { + if let syn::PathArguments::AngleBracketed(ref arg) = seg.arguments { + for elem in &arg.args { + if let syn::GenericArgument::Type(syn::Type::Reference(ref i)) = elem { + match &*i.elem { + syn::Type::Slice(_) => return None, + _ => () + } + } + } + } + } + _ => () + }; // Similarly i64/u64 aren't supported because they don't // implement `Into` From 3076e40b217da58666b4e2ce4e48c4c933a9d231 Mon Sep 17 00:00:00 2001 From: Benjamin Kampmann Date: Mon, 3 Sep 2018 13:27:24 +0200 Subject: [PATCH 4/5] Require shared refs to be mutable --- crates/webidl/src/idl_type.rs | 22 +++++++++++----------- crates/webidl/src/util.rs | 10 +++++----- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/crates/webidl/src/idl_type.rs b/crates/webidl/src/idl_type.rs index 2fa33b05..92a392e0 100644 --- a/crates/webidl/src/idl_type.rs +++ b/crates/webidl/src/idl_type.rs @@ -441,7 +441,7 @@ impl<'a> IdlType<'a> { | IdlType::DomString | IdlType::ByteString | IdlType::UsvString => match pos { - TypePosition::Argument => Some(shared_ref(ident_ty(raw_ident("str")))), + TypePosition::Argument => Some(shared_ref(ident_ty(raw_ident("str")), true)), TypePosition::Return => Some(ident_ty(raw_ident("String"))), }, IdlType::Object => { @@ -456,15 +456,15 @@ impl<'a> IdlType<'a> { Some(leading_colon_path_ty(path)) }, IdlType::DataView => None, - IdlType::Int8Array => Some(array("i8", pos)), - IdlType::Uint8Array => Some(array("u8", pos)), + IdlType::Int8Array => Some(array("i8", pos, true)), + IdlType::Uint8Array => Some(array("u8", pos, true)), IdlType::Uint8ClampedArray => None, // FIXME(#421) - IdlType::Int16Array => Some(array("i16", pos)), - IdlType::Uint16Array => Some(array("u16", pos)), - IdlType::Int32Array => Some(array("i32", pos)), - IdlType::Uint32Array => Some(array("u32", pos)), - IdlType::Float32Array => Some(array("f32", pos)), - IdlType::Float64Array => Some(array("f64", pos)), + IdlType::Int16Array => Some(array("i16", pos, true)), + IdlType::Uint16Array => Some(array("u16", pos, true)), + IdlType::Int32Array => Some(array("i32", pos, true)), + IdlType::Uint32Array => Some(array("u32", pos, true)), + IdlType::Float32Array => Some(array("f32", pos, true)), + IdlType::Float64Array => Some(array("f64", pos, true)), IdlType::ArrayBufferView | IdlType::BufferSource => { let path = vec![rust_ident("js_sys"), rust_ident("Object")]; @@ -474,7 +474,7 @@ impl<'a> IdlType<'a> { | IdlType::Dictionary(name) => { let ty = ident_ty(rust_ident(camel_case_ident(name).as_str())); if pos == TypePosition::Argument { - Some(shared_ref(ty)) + Some(shared_ref(ty, true)) } else { Some(ty) } @@ -488,7 +488,7 @@ impl<'a> IdlType<'a> { let path = vec![rust_ident("js_sys"), rust_ident("Promise")]; let ty = leading_colon_path_ty(path); if pos == TypePosition::Argument { - Some(shared_ref(ty)) + Some(shared_ref(ty, true)) } else { Some(ty) } diff --git a/crates/webidl/src/util.rs b/crates/webidl/src/util.rs index 8c330b55..a2b66679 100644 --- a/crates/webidl/src/util.rs +++ b/crates/webidl/src/util.rs @@ -14,11 +14,11 @@ use first_pass::{FirstPassRecord, OperationId, OperationData, Signature}; use idl_type::{IdlType, ToIdlType}; /// Take a type and create an immutable shared reference to that type. -pub(crate) fn shared_ref(ty: syn::Type) -> syn::Type { +pub(crate) fn shared_ref(ty: syn::Type, mutable: bool) -> syn::Type { syn::TypeReference { and_token: Default::default(), lifetime: None, - mutability: None, + mutability: if mutable { Some(syn::token::Mut::default()) } else { None }, elem: Box::new(ty), }.into() } @@ -57,10 +57,10 @@ pub fn mdn_doc(class: &str, method: Option<&str>) -> String { } // Array type is borrowed for arguments (`&[T]`) and owned for return value (`Vec`). -pub(crate) fn array(base_ty: &str, pos: TypePosition) -> syn::Type { +pub(crate) fn array(base_ty: &str, pos: TypePosition, mutable: bool) -> syn::Type { match pos { TypePosition::Argument => { - shared_ref(slice_ty(ident_ty(raw_ident(base_ty)))) + shared_ref(slice_ty(ident_ty(raw_ident(base_ty))), mutable) } TypePosition::Return => { vec_ty(ident_ty(raw_ident(base_ty))) @@ -240,7 +240,7 @@ impl<'src> FirstPassRecord<'src> { .. } = &kind { let mut res = Vec::with_capacity(idl_arguments.size_hint().0 + 1); - res.push(simple_fn_arg(raw_ident("self_"), shared_ref(ty.clone()))); + res.push(simple_fn_arg(raw_ident("self_"), shared_ref(ty.clone(), true))); res } else { Vec::with_capacity(idl_arguments.size_hint().0) From 6efbf5076ceafaac0c7e6eab4a85bc5f7e7490a9 Mon Sep 17 00:00:00 2001 From: Benjamin Kampmann Date: Thu, 6 Sep 2018 16:16:24 +0200 Subject: [PATCH 5/5] Adding Uint8ArrayMut, disable mutability for all other than this one --- crates/webidl/src/idl_type.rs | 27 +++++++++++++++------------ crates/webidl/src/util.rs | 2 +- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/crates/webidl/src/idl_type.rs b/crates/webidl/src/idl_type.rs index 92a392e0..83d303d4 100644 --- a/crates/webidl/src/idl_type.rs +++ b/crates/webidl/src/idl_type.rs @@ -33,6 +33,7 @@ pub(crate) enum IdlType<'a> { DataView, Int8Array, Uint8Array, + Uint8ArrayMut, Uint8ClampedArray, Int16Array, Uint16Array, @@ -368,6 +369,7 @@ impl<'a> IdlType<'a> { IdlType::DataView => dst.push_str("data_view"), IdlType::Int8Array => dst.push_str("i8_array"), IdlType::Uint8Array => dst.push_str("u8_array"), + IdlType::Uint8ArrayMut => dst.push_str("u8_array"), IdlType::Uint8ClampedArray => dst.push_str("u8_clamped_array"), IdlType::Int16Array => dst.push_str("i16_array"), IdlType::Uint16Array => dst.push_str("u16_array"), @@ -441,7 +443,7 @@ impl<'a> IdlType<'a> { | IdlType::DomString | IdlType::ByteString | IdlType::UsvString => match pos { - TypePosition::Argument => Some(shared_ref(ident_ty(raw_ident("str")), true)), + TypePosition::Argument => Some(shared_ref(ident_ty(raw_ident("str")), false)), TypePosition::Return => Some(ident_ty(raw_ident("String"))), }, IdlType::Object => { @@ -456,15 +458,16 @@ impl<'a> IdlType<'a> { Some(leading_colon_path_ty(path)) }, IdlType::DataView => None, - IdlType::Int8Array => Some(array("i8", pos, true)), - IdlType::Uint8Array => Some(array("u8", pos, true)), + IdlType::Int8Array => Some(array("i8", pos, false)), + IdlType::Uint8Array => Some(array("u8", pos, false)), + IdlType::Uint8ArrayMut => Some(array("u8", pos, true)), IdlType::Uint8ClampedArray => None, // FIXME(#421) - IdlType::Int16Array => Some(array("i16", pos, true)), - IdlType::Uint16Array => Some(array("u16", pos, true)), - IdlType::Int32Array => Some(array("i32", pos, true)), - IdlType::Uint32Array => Some(array("u32", pos, true)), - IdlType::Float32Array => Some(array("f32", pos, true)), - IdlType::Float64Array => Some(array("f64", pos, true)), + IdlType::Int16Array => Some(array("i16", pos, false)), + IdlType::Uint16Array => Some(array("u16", pos, false)), + IdlType::Int32Array => Some(array("i32", pos, false)), + IdlType::Uint32Array => Some(array("u32", pos, false)), + IdlType::Float32Array => Some(array("f32", pos, false)), + IdlType::Float64Array => Some(array("f64", pos, false)), IdlType::ArrayBufferView | IdlType::BufferSource => { let path = vec![rust_ident("js_sys"), rust_ident("Object")]; @@ -474,7 +477,7 @@ impl<'a> IdlType<'a> { | IdlType::Dictionary(name) => { let ty = ident_ty(rust_ident(camel_case_ident(name).as_str())); if pos == TypePosition::Argument { - Some(shared_ref(ty, true)) + Some(shared_ref(ty, false)) } else { Some(ty) } @@ -488,7 +491,7 @@ impl<'a> IdlType<'a> { let path = vec![rust_ident("js_sys"), rust_ident("Promise")]; let ty = leading_colon_path_ty(path); if pos == TypePosition::Argument { - Some(shared_ref(ty, true)) + Some(shared_ref(ty, false)) } else { Some(ty) } @@ -575,7 +578,7 @@ impl<'a> IdlType<'a> { .flat_map(|idl_type| idl_type.flatten()) .collect(), IdlType::ArrayBufferView | IdlType::BufferSource => - vec![IdlType::Object, IdlType::Uint8Array], + vec![IdlType::Object, IdlType::Uint8ArrayMut], idl_type @ _ => vec![idl_type.clone()], } diff --git a/crates/webidl/src/util.rs b/crates/webidl/src/util.rs index a2b66679..513989ea 100644 --- a/crates/webidl/src/util.rs +++ b/crates/webidl/src/util.rs @@ -240,7 +240,7 @@ impl<'src> FirstPassRecord<'src> { .. } = &kind { let mut res = Vec::with_capacity(idl_arguments.size_hint().0 + 1); - res.push(simple_fn_arg(raw_ident("self_"), shared_ref(ty.clone(), true))); + res.push(simple_fn_arg(raw_ident("self_"), shared_ref(ty.clone(), false))); res } else { Vec::with_capacity(idl_arguments.size_hint().0)