Add immutable field to IdlType::Float32Array

This commit is contained in:
Chinedu Francis Nwafili 2019-01-21 19:18:35 -05:00
parent 078823aebd
commit 1738f0772c
No known key found for this signature in database
GPG Key ID: 8AD7D5AF294ECA5D
7 changed files with 3579 additions and 48 deletions

3535
crates/web-sys/foo Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,26 +0,0 @@
//! When generating our web_sys APIs we default to setting slice references that
//! get passed to JS as mutable in case they get mutated in JS.
//!
//! In certain cases we know for sure that the slice will not get mutated - for
//! example when working with the WebGlRenderingContext APIs.
//!
//! These tests ensure that whitelisted methods do indeed accept mutable slices.
//!
//! @see https://github.com/rustwasm/wasm-bindgen/issues/1005
use wasm_bindgen::prelude::*;
use wasm_bindgen_test::*;
use web_sys::WebGlRenderingContext;
#[wasm_bindgen(module = "./tests/wasm/element.js")]
extern "C" {
fn new_webgl_rendering_context() -> WebGlRenderingContext;
}
// Ensure that our whitelisted WebGlRenderingContext methods work
#[wasm_bindgen_test]
fn test_webgl_rendering_context_immutable_slices() {
let gl = new_webgl_rendering_context();
gl.vertex_attrib1fv_with_f32_array(0, &[5000.]);
}

View File

@ -56,7 +56,7 @@ pub mod style_element;
pub mod table_element;
pub mod title_element;
pub mod xpath_result;
pub mod immutable_slices;
pub mod whitelisted_immutable_slices;
#[wasm_bindgen_test]
fn deref_works() {

View File

@ -22,5 +22,5 @@ extern "C" {
fn test_webgl_rendering_context_immutable_slices() {
let gl = new_webgl_rendering_context();
gl.vertex_attrib1fv_with_f32_array(0, &[5000.]);
gl.vertex_attrib1fv_with_f32_array(0, &[5000.]);
}

View File

@ -83,6 +83,9 @@ pub(crate) struct CallbackInterfaceData<'src> {
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug, Clone, Copy)]
pub(crate) enum OperationId<'src> {
Constructor(IgnoreTraits<&'src str>),
/// The name of a function in crates/web-sys/webidls/enabled/*.webidl
///
/// ex: Operation(Some("vertexAttrib1fv"))
Operation(Option<&'src str>),
IndexingGetter,
IndexingSetter,

View File

@ -41,7 +41,10 @@ pub(crate) enum IdlType<'a> {
Uint16Array,
Int32Array,
Uint32Array,
Float32Array,
Float32Array {
/// Whether or not the generated web-sys function should use an immutable slice
immutable: bool
},
Float64Array,
ArrayBufferView,
BufferSource,
@ -324,14 +327,26 @@ impl<'a> ToIdlType<'a> for Identifier<'a> {
}
}
// We default to Float32Array's being mutable, but in certain cases where we're certain that
// slices won't get mutated on the JS side (such as the WebGL APIs) we might, later in the flow,
// instead use the immutable version.
impl<'a> ToIdlType<'a> for term::Float32Array {
fn to_idl_type(&self, _record: &FirstPassRecord<'a>) -> IdlType<'a> {
IdlType::Float32Array {immutable: false}
}
}
macro_rules! terms_to_idl_type {
($($t:tt => $r:tt)*) => ($(
impl<'a> ToIdlType<'a> for term::$t {
fn to_idl_type(&self, _record: &FirstPassRecord<'a>) -> IdlType<'a> {
IdlType::$r
match IdlType::$r {
IdlType::Callback => IdlType::Float32Array { immutable: false },
_ => IdlType::$r
}
}
}
)*)
)*);
}
terms_to_idl_type! {
@ -358,7 +373,6 @@ terms_to_idl_type! {
Uint16Array => Uint16Array
Uint32Array => Uint32Array
Uint8ClampedArray => Uint8ClampedArray
Float32Array => Float32Array
Float64Array => Float64Array
ArrayBufferView => ArrayBufferView
BufferSource => BufferSource
@ -396,7 +410,7 @@ impl<'a> IdlType<'a> {
IdlType::Uint16Array => dst.push_str("u16_array"),
IdlType::Int32Array => dst.push_str("i32_array"),
IdlType::Uint32Array => dst.push_str("u32_array"),
IdlType::Float32Array => dst.push_str("f32_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"),
@ -501,16 +515,16 @@ impl<'a> IdlType<'a> {
IdlType::ArrayBuffer => js_sys("ArrayBuffer"),
IdlType::DataView => None,
IdlType::Int8Array => Some(array("i8", pos)),
IdlType::Uint8Array => Some(array("u8", pos)),
IdlType::Uint8ArrayMut => Some(array("u8", pos)),
IdlType::Uint8ClampedArray => Some(clamped(array("u8", pos))),
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::Int8Array => Some(array("i8", pos, false)),
IdlType::Uint8Array => Some(array("u8", pos, false)),
IdlType::Uint8ArrayMut => Some(array("u8", pos, false)),
IdlType::Uint8ClampedArray => Some(clamped(array("u8", pos, false))),
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 {immutable} => Some(array("f32", pos, *immutable)),
IdlType::Float64Array => Some(array("f64", pos, false)),
IdlType::ArrayBufferView | IdlType::BufferSource => js_sys("Object"),
IdlType::Interface(name)

View File

@ -67,13 +67,13 @@ pub fn mdn_doc(class: &str, method: Option<&str>) -> String {
format!("[MDN Documentation]({})", link).into()
}
// 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 {
// Array type is borrowed for arguments (`&mut [T]` or `&[T]`) and owned for return value (`Vec<T>`).
pub(crate) fn array(base_ty: &str, pos: TypePosition, immutable: bool) -> syn::Type {
match pos {
TypePosition::Argument => {
shared_ref(
slice_ty(ident_ty(raw_ident(base_ty))),
/*mutable =*/ true,
/*mutable =*/ !immutable,
)
}
TypePosition::Return => vec_ty(ident_ty(raw_ident(base_ty))),
@ -721,9 +721,14 @@ pub fn public() -> syn::Visibility {
/// maintained by hand.
fn maybe_adjust<'a> (idl_type: IdlType<'a>, id: &'a OperationId) -> IdlType<'a> {
match id {
// TODO: `match op` and return an adjusted idl_type if necessary
OperationId::Operation(Some(op)) => {
// TODO: `match op` and return an adjusted idl_type if necessary
idl_type
match *op {
// "vertexAttrib1fv" => {
// IdlType::Float32Array { immutable: true}
// }
_ => idl_type
}
}
_ => idl_type
}