mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-05-07 19:42:16 +00:00
Merge pull request #745 from gnunicorn/webcrypto
Add ArrayBufferView & BufferSource; Enables Webcrypto API
This commit is contained in:
commit
3c41d39b16
@ -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"
|
||||
|
@ -33,6 +33,7 @@ pub(crate) enum IdlType<'a> {
|
||||
DataView,
|
||||
Int8Array,
|
||||
Uint8Array,
|
||||
Uint8ArrayMut,
|
||||
Uint8ClampedArray,
|
||||
Int16Array,
|
||||
Uint16Array,
|
||||
@ -40,6 +41,8 @@ pub(crate) enum IdlType<'a> {
|
||||
Uint32Array,
|
||||
Float32Array,
|
||||
Float64Array,
|
||||
ArrayBufferView,
|
||||
BufferSource,
|
||||
|
||||
Interface(&'a str),
|
||||
Dictionary(&'a str),
|
||||
@ -116,6 +119,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 +336,8 @@ terms_to_idl_type! {
|
||||
Uint8ClampedArray => Uint8ClampedArray
|
||||
Float32Array => Float32Array
|
||||
Float64Array => Float64Array
|
||||
ArrayBufferView => ArrayBufferView
|
||||
BufferSource => BufferSource
|
||||
Error => Error
|
||||
}
|
||||
|
||||
@ -362,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"),
|
||||
@ -369,6 +377,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)),
|
||||
@ -433,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")))),
|
||||
TypePosition::Argument => Some(shared_ref(ident_ty(raw_ident("str")), false)),
|
||||
TypePosition::Return => Some(ident_ty(raw_ident("String"))),
|
||||
},
|
||||
IdlType::Object => {
|
||||
@ -448,21 +458,26 @@ 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, 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)),
|
||||
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, 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::Interface(name)
|
||||
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()));
|
||||
if pos == TypePosition::Argument {
|
||||
Some(shared_ref(ty))
|
||||
Some(shared_ref(ty, false))
|
||||
} else {
|
||||
Some(ty)
|
||||
}
|
||||
@ -476,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))
|
||||
Some(shared_ref(ty, false))
|
||||
} else {
|
||||
Some(ty)
|
||||
}
|
||||
@ -562,6 +577,8 @@ impl<'a> IdlType<'a> {
|
||||
.iter()
|
||||
.flat_map(|idl_type| idl_type.flatten())
|
||||
.collect(),
|
||||
IdlType::ArrayBufferView | IdlType::BufferSource =>
|
||||
vec![IdlType::Object, IdlType::Uint8ArrayMut],
|
||||
|
||||
idl_type @ _ => vec![idl_type.clone()],
|
||||
}
|
||||
|
@ -291,12 +291,28 @@ impl<'src> FirstPassRecord<'src> {
|
||||
|
||||
// Slice types aren't supported because they don't implement
|
||||
// `Into<JsValue>`
|
||||
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<JsValue>`
|
||||
|
@ -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<T>`).
|
||||
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(), false)));
|
||||
res
|
||||
} else {
|
||||
Vec::with_capacity(idl_arguments.size_hint().0)
|
||||
|
Loading…
x
Reference in New Issue
Block a user