diff --git a/crates/web-sys/tests/wasm/element.js b/crates/web-sys/tests/wasm/element.js index 38d1f517..3f34bc5f 100644 --- a/crates/web-sys/tests/wasm/element.js +++ b/crates/web-sys/tests/wasm/element.js @@ -152,9 +152,6 @@ export function new_title() { } export function new_webgl_rendering_context() { - const foo = document.createElement('canvas'); - console.log('Does get context work? ' + foo.getContext('webgl')); - const canvas = document.createElement('canvas'); return canvas.getContext('webgl'); } diff --git a/crates/web-sys/tests/wasm/whitelisted_immutable_slices.rs b/crates/web-sys/tests/wasm/whitelisted_immutable_slices.rs index 59c60eb7..3b0cac98 100644 --- a/crates/web-sys/tests/wasm/whitelisted_immutable_slices.rs +++ b/crates/web-sys/tests/wasm/whitelisted_immutable_slices.rs @@ -5,6 +5,8 @@ //! example when working with the WebGlRenderingContext APIs. //! //! These tests ensure that whitelisted methods do indeed accept mutable slices. +//! Especially important since this whitelist is stringly typed and currently +//! maintained by hand. //! //! @see https://github.com/rustwasm/wasm-bindgen/issues/1005 @@ -20,7 +22,25 @@ extern "C" { // Ensure that our whitelisted WebGlRenderingContext methods work #[wasm_bindgen_test] fn test_webgl_rendering_context_immutable_slices() { + // WebGl wasn't working in headless firefox at the time of writing.. let gl = new_webgl_rendering_context(); - gl.vertex_attrib1fv_with_f32_array(0, &[5000.]); + gl.vertex_attrib1fv_with_f32_array(0, &[1.]); + gl.vertex_attrib2fv_with_f32_array(0, &[1.]); + gl.vertex_attrib3fv_with_f32_array(0, &[1.]); + gl.vertex_attrib4fv_with_f32_array(0, &[1.]); + + gl.uniform1fv_with_f32_array(None, &[1.]); + gl.uniform2fv_with_f32_array(None, &[1.]); + gl.uniform3fv_with_f32_array(None, &[1.]); + gl.uniform4fv_with_f32_array(None, &[1.]); + + gl.uniform_matrix2fv_with_f32_array(None, false, &[1.]); + gl.uniform_matrix3fv_with_f32_array(None, false, &[1.]); + gl.uniform_matrix4fv_with_f32_array(None, false, &[1.]); } + +// TODO: +//#[wasm_bindgen_test] +//fn test_another_types_immutable_slices_here() { +//} diff --git a/crates/webidl/src/idl_type.rs b/crates/webidl/src/idl_type.rs index 3d24a18d..ca9fcb58 100644 --- a/crates/webidl/src/idl_type.rs +++ b/crates/webidl/src/idl_type.rs @@ -340,10 +340,7 @@ 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> { - match IdlType::$r { - IdlType::Callback => IdlType::Float32Array { immutable: false }, - _ => IdlType::$r - } + IdlType::$r } } )*); diff --git a/crates/webidl/src/util.rs b/crates/webidl/src/util.rs index 2f82288e..e12146b8 100644 --- a/crates/webidl/src/util.rs +++ b/crates/webidl/src/util.rs @@ -12,8 +12,8 @@ use weedle::literal::{ConstValue, FloatLit, IntegerLit}; use first_pass::{FirstPassRecord, OperationData, OperationId, Signature}; use idl_type::{IdlType, ToIdlType}; -// TODO: Remove.. just using this to see what idl types I need to change.. -use std::io::Write; + +use std::collections::HashSet; /// For variadic operations an overload with a `js_sys::Array` argument is generated alongside with /// `operation_name_0`, `operation_name_1`, `operation_name_2`, ..., `operation_name_n` overloads @@ -721,31 +721,40 @@ pub fn public() -> syn::Visibility { /// /// Here we implement a whitelist for those cases. This whitelist is currently /// maintained by hand. -fn maybe_adjust<'a> (mut idl_type: IdlType<'a>, id: &'a OperationId) -> IdlType<'a> { -// let mut file = ::std::fs::OpenOptions::new().append(true).create(true).open("foo").unwrap(); +fn maybe_adjust<'a>(mut idl_type: IdlType<'a>, id: &'a OperationId) -> IdlType<'a> { + let immutable_f32_fns: Vec<&'static str> = vec![ + // WebGlRenderingContext + "vertexAttrib1fv", + "vertexAttrib2fv", + "vertexAttrib3fv", + "vertexAttrib4fv", + "uniform1fv", + "uniform2fv", + "uniform3fv", + "uniform4fv", + "uniformMatrix2fv", + "uniformMatrix3fv", + "uniformMatrix4fv", + // TODO: Add another type's functions here + ]; - match id { - OperationId::Operation(Some(op)) => { - match *op { - "vertexAttrib1fv" => { -// TODO: Remove.. just using this to see what idl types I need to change.. -// file.write( -// format!("{:#?}", idl_type).as_bytes() -// ); -// file.write(r#" -// "#.as_bytes()); - - if let IdlType::Union(ref mut union) = idl_type { - if let IdlType::Float32Array { ref mut immutable } = union[0] { - *immutable = true; - } - } - } - _ => {} - }; - - return idl_type - } - _ => idl_type + let mut immutable_f32_slice_whitelist: HashSet<&'static str> = HashSet::new(); + for function_name in immutable_f32_fns.into_iter() { + immutable_f32_slice_whitelist.insert(function_name); } + + // example `op`... -> "vertexAttrib1fv" + if let OperationId::Operation(Some(op)) = id { + // Look up this funtion in our whitelist and see if we should make its + // slice argument immutable. + if immutable_f32_slice_whitelist.get(op).is_some() { + if let IdlType::Union(ref mut union) = idl_type { + if let IdlType::Float32Array { ref mut immutable } = union[0] { + *immutable = true; + } + } + } + } + + idl_type }