Skip to content

Allow constructors to be immutable and add it for ImageData #2400

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/web-sys/src/features/gen_ImageData.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ extern "C" {
#[doc = ""]
#[doc = "*This API requires the following crate features to be activated: `ImageData`*"]
pub fn new_with_u8_clamped_array(
data: ::wasm_bindgen::Clamped<&mut [u8]>,
data: ::wasm_bindgen::Clamped<&[u8]>,
sw: u32,
) -> Result<ImageData, JsValue>;
#[wasm_bindgen(catch, constructor, js_class = "ImageData")]
Expand All @@ -56,7 +56,7 @@ extern "C" {
#[doc = ""]
#[doc = "*This API requires the following crate features to be activated: `ImageData`*"]
pub fn new_with_u8_clamped_array_and_sh(
data: ::wasm_bindgen::Clamped<&mut [u8]>,
data: ::wasm_bindgen::Clamped<&[u8]>,
sw: u32,
sh: u32,
) -> Result<ImageData, JsValue>;
Expand Down
18 changes: 18 additions & 0 deletions crates/web-sys/tests/wasm/image_data.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use wasm_bindgen::prelude::*;
use wasm_bindgen::Clamped;
use wasm_bindgen_test::*;
use web_sys::HtmlAnchorElement;
#[wasm_bindgen(module = "/tests/wasm/element.js")]
extern "C" {
fn new_a() -> HtmlAnchorElement;
}

#[wasm_bindgen_test]
fn test_anchor_element() {
// This test is to make sure there is no weird mutability going on.
let buf = vec![1, 2, 3, 255];
let image_data = web_sys::ImageData::new_with_u8_clamped_array(Clamped(&buf), 1).unwrap();
let mut data = image_data.data();
data[1] = 4;
assert_eq!(buf[1], 2);
}
1 change: 1 addition & 0 deletions crates/web-sys/tests/wasm/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub mod history;
pub mod hr_element;
pub mod html_element;
pub mod html_html_element;
pub mod image_data;
pub mod input_element;
//TODO: Both menu-related tests completely break in Chrome, but run fine in Firefox.
//pub mod menu_element;
Expand Down
2 changes: 2 additions & 0 deletions crates/webidl/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ lazy_static! {


pub(crate) static ref IMMUTABLE_SLICE_WHITELIST: BTreeSet<&'static str> = BTreeSet::from_iter(vec![
// ImageData
"ImageData",
// WebGlRenderingContext, WebGl2RenderingContext
"uniform1fv",
"uniform2fv",
Expand Down
9 changes: 6 additions & 3 deletions crates/webidl/src/first_pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ pub(crate) struct CallbackInterfaceData<'src> {

#[derive(PartialEq, Eq, PartialOrd, Ord, Debug, Clone, Copy)]
pub(crate) enum OperationId<'src> {
Constructor,
/// The name of a constructor in crates/web-sys/webidls/enabled/*.webidl
///
/// ex: Constructor(Some("ImageData"))
Constructor(Option<&'src str>),
NamedConstructor(IgnoreTraits<&'src str>),
/// The name of a function in crates/web-sys/webidls/enabled/*.webidl
///
Expand Down Expand Up @@ -390,7 +393,7 @@ fn process_interface_attribute<'src>(
record,
FirstPassOperationType::Interface,
self_name,
&[OperationId::Constructor],
&[OperationId::Constructor(Some(self_name))],
&list.args.body.list,
&return_ty,
&None,
Expand All @@ -402,7 +405,7 @@ fn process_interface_attribute<'src>(
record,
FirstPassOperationType::Interface,
self_name,
&[OperationId::Constructor],
&[OperationId::Constructor(Some(self_name))],
&[],
&return_ty,
&None,
Expand Down
2 changes: 1 addition & 1 deletion crates/webidl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ impl<'src> FirstPassRecord<'src> {
) {
match id {
OperationId::Operation(Some(_)) => {}
OperationId::Constructor
OperationId::Constructor(_)
| OperationId::NamedConstructor(_)
| OperationId::Operation(None)
| OperationId::IndexingGetter
Expand Down
3 changes: 2 additions & 1 deletion crates/webidl/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ impl<'src> FirstPassRecord<'src> {
// > value of a type corresponding to the interface the
// > `[Constructor]` extended attribute appears on, **or throw an
// > exception**.
OperationId::Constructor => {
OperationId::Constructor(_) => {
("new", InterfaceMethodKind::Constructor(None), false, true)
}
OperationId::NamedConstructor(n) => (
Expand Down Expand Up @@ -502,6 +502,7 @@ impl<'src> FirstPassRecord<'src> {
fn maybe_adjust<'a>(&self, mut idl_type: IdlType<'a>, id: &'a OperationId) -> IdlType<'a> {
let op = match id {
OperationId::Operation(Some(op)) => op,
OperationId::Constructor(Some(op)) => op,
_ => return idl_type,
};

Expand Down