Skip to content

Commit bab83a7

Browse files
authored
Whitelist send_with_u8_array slice (#2015)
* Whitelist send_with_u8_array slice Fixes #2014 * Fix send slice * Remove chromedriver comment * Get slice tests running in CI
1 parent 1e75e41 commit bab83a7

File tree

3 files changed

+59
-80
lines changed

3 files changed

+59
-80
lines changed

crates/web-sys/tests/wasm/element.js

-10
Original file line numberDiff line numberDiff line change
@@ -151,16 +151,6 @@ export function new_title() {
151151
return document.createElement("title");
152152
}
153153

154-
export function new_webgl_rendering_context() {
155-
const canvas = document.createElement('canvas');
156-
return canvas.getContext('webgl');
157-
}
158-
159-
export function new_webgl2_rendering_context() {
160-
const canvas = document.createElement('canvas');
161-
return canvas.getContext('webgl2');
162-
}
163-
164154
export function new_xpath_result() {
165155
let xmlDoc = new DOMParser().parseFromString("<root><value>tomato</value></root>", "application/xml");
166156
let xpathResult = xmlDoc.evaluate("/root//value", xmlDoc, null, XPathResult.ANY_TYPE, null);

crates/web-sys/tests/wasm/whitelisted_immutable_slices.rs

+57-70
Original file line numberDiff line numberDiff line change
@@ -4,86 +4,73 @@
44
//! In certain cases we know for sure that the slice will not get mutated - for
55
//! example when working with the WebGlRenderingContext APIs.
66
//!
7-
//! These tests ensure that whitelisted methods do indeed accept mutable slices.
7+
//! These tests ensure that whitelisted methods do indeed accept immutable slices.
88
//! Especially important since this whitelist is stringly typed and currently
99
//! maintained by hand.
1010
//!
1111
//! @see https://github.com/rustwasm/wasm-bindgen/issues/1005
1212
13-
use wasm_bindgen::prelude::*;
14-
use web_sys::{WebGl2RenderingContext, WebGlRenderingContext};
13+
use web_sys::{WebGl2RenderingContext, WebGlRenderingContext, WebSocket};
14+
use wasm_bindgen::{JsValue, JsCast};
1515

16-
#[wasm_bindgen(module = "/tests/wasm/element.js")]
17-
extern "C" {
18-
fn new_webgl_rendering_context() -> WebGlRenderingContext;
19-
fn new_webgl2_rendering_context() -> WebGl2RenderingContext;
20-
// TODO: Add a function to create another type to test here.
21-
// These functions come from element.js
16+
// Ensure that our whitelisted WebGlRenderingContext methods compile with 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]);
2257
}
2358

24-
// TODO: Uncomment WebGlRenderingContext test. Every now and then we can check if this works
25-
// in the latest geckodriver.
26-
//
27-
// Currently commented out because WebGl isn't working in geckodriver.
28-
//
29-
// It currently works in chromedriver so if you need to run this in the meantime you can
30-
// uncomment this block and run it in using chromedriver.
31-
//
32-
// CHROMEDRIVER=chromedriver cargo test --manifest-path crates/web-sys/Cargo.toml --target wasm32-unknown-unknown --all-features test_webgl_rendering_context_immutable_slices
59+
// Ensure that our whitelisted WebGl2RenderingContext methods compile with immutable slices.
60+
fn test_webgl2_rendering_context_immutable_slices() {
61+
let gl = JsValue::null().unchecked_into::<WebGl2RenderingContext>();
3362

34-
// Ensure that our whitelisted WebGlRenderingContext methods work
35-
//#[wasm_bindgen_test]
36-
//fn test_webgl_rendering_context_immutable_slices() {
37-
// let gl = new_webgl_rendering_context();
38-
//
39-
// gl.vertex_attrib1fv_with_f32_array(0, &[1.]);
40-
// gl.vertex_attrib2fv_with_f32_array(0, &[1.]);
41-
// gl.vertex_attrib3fv_with_f32_array(0, &[1.]);
42-
// gl.vertex_attrib4fv_with_f32_array(0, &[1.]);
43-
//
44-
// gl.uniform1fv_with_f32_array(None, &[1.]);
45-
// gl.uniform2fv_with_f32_array(None, &[1.]);
46-
// gl.uniform3fv_with_f32_array(None, &[1.]);
47-
// gl.uniform4fv_with_f32_array(None, &[1.]);
48-
//
49-
// gl.uniform_matrix2fv_with_f32_array(None, false, &[1.]);
50-
// gl.uniform_matrix3fv_with_f32_array(None, false, &[1.]);
51-
// gl.uniform_matrix4fv_with_f32_array(None, false, &[1.]);
52-
//
53-
// gl.tex_image_2d_with_i32_and_i32_and_i32_and_format_and_type_and_opt_u8_array(
54-
// 0,
55-
// 0,
56-
// 0,
57-
// 0,
58-
// 0,
59-
// 0,
60-
// 0,
61-
// 0,
62-
// Some(&[1]),
63-
// );
64-
// gl.tex_sub_image_2d_with_i32_and_i32_and_u32_and_type_and_opt_u8_array(
65-
// 0,
66-
// 0,
67-
// 0,
68-
// 0,
69-
// 0,
70-
// 0,
71-
// 0,
72-
// 0,
73-
// Some(&[1]),
74-
// );
75-
// gl.compressed_tex_image_2d_with_u8_array(0, 0, 0, 0, 0, 0, &[1]);
76-
// }
77-
//
78-
//#[wasm_bindgen_test]
79-
//fn test_webgl2_rendering_context_immutable_slices() {
80-
// let gl = new_webgl2_rendering_context();
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.
69+
fn test_websocket_immutable_slices() {
70+
let ws = JsValue::null().unchecked_into::<WebSocket>();
71+
ws.send_with_u8_array(&[0]);
72+
}
8173

82-
// gl.tex_image_3d_with_opt_u8_array(0, 0, 0, 0, 0, 0, 0, 0, 0, Some(&[1]));
83-
// gl.tex_sub_image_3d_with_opt_u8_array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Some(&[1]));
84-
// gl.compressed_tex_image_3d_with_u8_array(0, 0, 0, 0, 0, 0, 0, &[1]);
85-
//}
86-
//
8774
// TODO:
8875
//#[wasm_bindgen_test]
8976
//fn test_another_types_immutable_slices_here() {

crates/webidl/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,8 @@ fn immutable_slice_whitelist() -> BTreeSet<&'static str> {
244244
"clearBufferfv",
245245
"clearBufferiv",
246246
"clearBufferuiv",
247+
// WebSocket
248+
"send",
247249
// TODO: Add another type's functions here. Leave a comment header with the type name
248250
])
249251
}

0 commit comments

Comments
 (0)