1 //! When generating our web_sys APIs we default to setting slice references that
2 //! get passed to JS as mutable in case they get mutated in JS.
3 //!
4 //! In certain cases we know for sure that the slice will not get mutated - for
5 //! example when working with the WebGlRenderingContext APIs.
6 //!
7 //! These tests ensure that whitelisted methods do indeed accept immutable slices.
8 //! Especially important since this whitelist is stringly typed and currently
9 //! maintained by hand.
10 //!
11 //! @see https://github.com/rustwasm/wasm-bindgen/issues/1005
12 
13 use wasm_bindgen::{JsCast, JsValue};
14 use web_sys::{WebGl2RenderingContext, WebGlRenderingContext, WebSocket};
15 
16 // Ensure that our whitelisted WebGlRenderingContext methods compile with immutable slices.
test_webgl_rendering_context_immutable_slices()17 fn test_webgl_rendering_context_immutable_slices() {
18     let gl = JsValue::null().unchecked_into::<WebGlRenderingContext>();
19 
20     gl.vertex_attrib1fv_with_f32_array(0, &[1.]);
21     gl.vertex_attrib2fv_with_f32_array(0, &[1.]);
22     gl.vertex_attrib3fv_with_f32_array(0, &[1.]);
23     gl.vertex_attrib4fv_with_f32_array(0, &[1.]);
24 
25     gl.uniform1fv_with_f32_array(None, &[1.]);
26     gl.uniform2fv_with_f32_array(None, &[1.]);
27     gl.uniform3fv_with_f32_array(None, &[1.]);
28     gl.uniform4fv_with_f32_array(None, &[1.]);
29 
30     gl.uniform_matrix2fv_with_f32_array(None, false, &[1.]);
31     gl.uniform_matrix3fv_with_f32_array(None, false, &[1.]);
32     gl.uniform_matrix4fv_with_f32_array(None, false, &[1.]);
33 
34     gl.tex_image_2d_with_i32_and_i32_and_i32_and_format_and_type_and_opt_u8_array(
35         0,
36         0,
37         0,
38         0,
39         0,
40         0,
41         0,
42         0,
43         Some(&[1]),
44     );
45     gl.tex_sub_image_2d_with_i32_and_i32_and_u32_and_type_and_opt_u8_array(
46         0,
47         0,
48         0,
49         0,
50         0,
51         0,
52         0,
53         0,
54         Some(&[1]),
55     );
56     gl.compressed_tex_image_2d_with_u8_array(0, 0, 0, 0, 0, 0, &[1]);
57 }
58 
59 // Ensure that our whitelisted WebGl2RenderingContext methods compile with immutable slices.
test_webgl2_rendering_context_immutable_slices()60 fn test_webgl2_rendering_context_immutable_slices() {
61     let gl = JsValue::null().unchecked_into::<WebGl2RenderingContext>();
62 
63     gl.tex_image_3d_with_opt_u8_array(0, 0, 0, 0, 0, 0, 0, 0, 0, Some(&[1]));
64     gl.tex_sub_image_3d_with_opt_u8_array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Some(&[1]));
65     gl.compressed_tex_image_3d_with_u8_array(0, 0, 0, 0, 0, 0, 0, &[1]);
66 }
67 
68 // Ensure that our whitelisted WebSocket methods compile with immutable slices.
test_websocket_immutable_slices()69 fn test_websocket_immutable_slices() {
70     let ws = JsValue::null().unchecked_into::<WebSocket>();
71     ws.send_with_u8_array(&[0]);
72 }
73 
74 // TODO:
75 //#[wasm_bindgen_test]
76 //fn test_another_types_immutable_slices_here() {
77 //}
78