Skip to content

Commit c195746

Browse files
authored
Allow constructors to be immutable and add it for ImageData (#2400)
* this patch allws constructors to take immutable arrays and implements it for ImageData * fmt * fmt?
1 parent 9d80e7d commit c195746

File tree

7 files changed

+32
-7
lines changed

7 files changed

+32
-7
lines changed

crates/web-sys/src/features/gen_ImageData.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ extern "C" {
4646
#[doc = ""]
4747
#[doc = "*This API requires the following crate features to be activated: `ImageData`*"]
4848
pub fn new_with_u8_clamped_array(
49-
data: ::wasm_bindgen::Clamped<&mut [u8]>,
49+
data: ::wasm_bindgen::Clamped<&[u8]>,
5050
sw: u32,
5151
) -> Result<ImageData, JsValue>;
5252
#[wasm_bindgen(catch, constructor, js_class = "ImageData")]
@@ -56,7 +56,7 @@ extern "C" {
5656
#[doc = ""]
5757
#[doc = "*This API requires the following crate features to be activated: `ImageData`*"]
5858
pub fn new_with_u8_clamped_array_and_sh(
59-
data: ::wasm_bindgen::Clamped<&mut [u8]>,
59+
data: ::wasm_bindgen::Clamped<&[u8]>,
6060
sw: u32,
6161
sh: u32,
6262
) -> Result<ImageData, JsValue>;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use wasm_bindgen::prelude::*;
2+
use wasm_bindgen::Clamped;
3+
use wasm_bindgen_test::*;
4+
use web_sys::HtmlAnchorElement;
5+
#[wasm_bindgen(module = "/tests/wasm/element.js")]
6+
extern "C" {
7+
fn new_a() -> HtmlAnchorElement;
8+
}
9+
10+
#[wasm_bindgen_test]
11+
fn test_anchor_element() {
12+
// This test is to make sure there is no weird mutability going on.
13+
let buf = vec![1, 2, 3, 255];
14+
let image_data = web_sys::ImageData::new_with_u8_clamped_array(Clamped(&buf), 1).unwrap();
15+
let mut data = image_data.data();
16+
data[1] = 4;
17+
assert_eq!(buf[1], 2);
18+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub mod history;
2020
pub mod hr_element;
2121
pub mod html_element;
2222
pub mod html_html_element;
23+
pub mod image_data;
2324
pub mod input_element;
2425
//TODO: Both menu-related tests completely break in Chrome, but run fine in Firefox.
2526
//pub mod menu_element;

crates/webidl/src/constants.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ lazy_static! {
4242

4343

4444
pub(crate) static ref IMMUTABLE_SLICE_WHITELIST: BTreeSet<&'static str> = BTreeSet::from_iter(vec![
45+
// ImageData
46+
"ImageData",
4547
// WebGlRenderingContext, WebGl2RenderingContext
4648
"uniform1fv",
4749
"uniform2fv",

crates/webidl/src/first_pass.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,10 @@ pub(crate) struct CallbackInterfaceData<'src> {
101101

102102
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug, Clone, Copy)]
103103
pub(crate) enum OperationId<'src> {
104-
Constructor,
104+
/// The name of a constructor in crates/web-sys/webidls/enabled/*.webidl
105+
///
106+
/// ex: Constructor(Some("ImageData"))
107+
Constructor(Option<&'src str>),
105108
NamedConstructor(IgnoreTraits<&'src str>),
106109
/// The name of a function in crates/web-sys/webidls/enabled/*.webidl
107110
///
@@ -390,7 +393,7 @@ fn process_interface_attribute<'src>(
390393
record,
391394
FirstPassOperationType::Interface,
392395
self_name,
393-
&[OperationId::Constructor],
396+
&[OperationId::Constructor(Some(self_name))],
394397
&list.args.body.list,
395398
&return_ty,
396399
&None,
@@ -402,7 +405,7 @@ fn process_interface_attribute<'src>(
402405
record,
403406
FirstPassOperationType::Interface,
404407
self_name,
405-
&[OperationId::Constructor],
408+
&[OperationId::Constructor(Some(self_name))],
406409
&[],
407410
&return_ty,
408411
&None,

crates/webidl/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ impl<'src> FirstPassRecord<'src> {
413413
) {
414414
match id {
415415
OperationId::Operation(Some(_)) => {}
416-
OperationId::Constructor
416+
OperationId::Constructor(_)
417417
| OperationId::NamedConstructor(_)
418418
| OperationId::Operation(None)
419419
| OperationId::IndexingGetter

crates/webidl/src/util.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ impl<'src> FirstPassRecord<'src> {
297297
// > value of a type corresponding to the interface the
298298
// > `[Constructor]` extended attribute appears on, **or throw an
299299
// > exception**.
300-
OperationId::Constructor => {
300+
OperationId::Constructor(_) => {
301301
("new", InterfaceMethodKind::Constructor(None), false, true)
302302
}
303303
OperationId::NamedConstructor(n) => (
@@ -502,6 +502,7 @@ impl<'src> FirstPassRecord<'src> {
502502
fn maybe_adjust<'a>(&self, mut idl_type: IdlType<'a>, id: &'a OperationId) -> IdlType<'a> {
503503
let op = match id {
504504
OperationId::Operation(Some(op)) => op,
505+
OperationId::Constructor(Some(op)) => op,
505506
_ => return idl_type,
506507
};
507508

0 commit comments

Comments
 (0)