Skip to content

Commit

Permalink
Add support for setlike interfaces (#3268)
Browse files Browse the repository at this point in the history
* Add support for setlike interfaces

* Fixing setlike test suite
  • Loading branch information
gents83 committed Jan 30, 2023
1 parent 5fff0a9 commit 8fc0e2b
Show file tree
Hide file tree
Showing 6 changed files with 441 additions and 21 deletions.
69 changes: 69 additions & 0 deletions crates/web-sys/src/features/gen_GpuSupportedFeatures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,73 @@ extern "C" {
#[doc = "*This API is unstable and requires `--cfg=web_sys_unstable_apis` to be activated, as"]
#[doc = "[described in the `wasm-bindgen` guide](https://rustwasm.github.io/docs/wasm-bindgen/web-sys/unstable-apis.html)*"]
pub type GpuSupportedFeatures;
#[cfg(web_sys_unstable_apis)]
# [wasm_bindgen (structural , method , getter , js_class = "GPUSupportedFeatures" , js_name = size)]
#[doc = "Getter for the `size` field of this object."]
#[doc = ""]
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/GPUSupportedFeatures/size)"]
#[doc = ""]
#[doc = "*This API requires the following crate features to be activated: `GpuSupportedFeatures`*"]
#[doc = ""]
#[doc = "*This API is unstable and requires `--cfg=web_sys_unstable_apis` to be activated, as"]
#[doc = "[described in the `wasm-bindgen` guide](https://rustwasm.github.io/docs/wasm-bindgen/web-sys/unstable-apis.html)*"]
pub fn size(this: &GpuSupportedFeatures) -> u32;
#[cfg(web_sys_unstable_apis)]
# [wasm_bindgen (method , structural , js_class = "GPUSupportedFeatures" , js_name = entries)]
#[doc = "The `entries()` method."]
#[doc = ""]
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/GPUSupportedFeatures/entries)"]
#[doc = ""]
#[doc = "*This API requires the following crate features to be activated: `GpuSupportedFeatures`*"]
#[doc = ""]
#[doc = "*This API is unstable and requires `--cfg=web_sys_unstable_apis` to be activated, as"]
#[doc = "[described in the `wasm-bindgen` guide](https://rustwasm.github.io/docs/wasm-bindgen/web-sys/unstable-apis.html)*"]
pub fn entries(this: &GpuSupportedFeatures) -> ::js_sys::Iterator;
#[cfg(web_sys_unstable_apis)]
# [wasm_bindgen (catch , method , structural , js_class = "GPUSupportedFeatures" , js_name = forEach)]
#[doc = "The `forEach()` method."]
#[doc = ""]
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/GPUSupportedFeatures/forEach)"]
#[doc = ""]
#[doc = "*This API requires the following crate features to be activated: `GpuSupportedFeatures`*"]
#[doc = ""]
#[doc = "*This API is unstable and requires `--cfg=web_sys_unstable_apis` to be activated, as"]
#[doc = "[described in the `wasm-bindgen` guide](https://rustwasm.github.io/docs/wasm-bindgen/web-sys/unstable-apis.html)*"]
pub fn for_each(
this: &GpuSupportedFeatures,
callback: &::js_sys::Function,
) -> Result<(), JsValue>;
#[cfg(web_sys_unstable_apis)]
# [wasm_bindgen (method , structural , js_class = "GPUSupportedFeatures" , js_name = has)]
#[doc = "The `has()` method."]
#[doc = ""]
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/GPUSupportedFeatures/has)"]
#[doc = ""]
#[doc = "*This API requires the following crate features to be activated: `GpuSupportedFeatures`*"]
#[doc = ""]
#[doc = "*This API is unstable and requires `--cfg=web_sys_unstable_apis` to be activated, as"]
#[doc = "[described in the `wasm-bindgen` guide](https://rustwasm.github.io/docs/wasm-bindgen/web-sys/unstable-apis.html)*"]
pub fn has(this: &GpuSupportedFeatures, value: &str) -> bool;
#[cfg(web_sys_unstable_apis)]
# [wasm_bindgen (method , structural , js_class = "GPUSupportedFeatures" , js_name = keys)]
#[doc = "The `keys()` method."]
#[doc = ""]
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/GPUSupportedFeatures/keys)"]
#[doc = ""]
#[doc = "*This API requires the following crate features to be activated: `GpuSupportedFeatures`*"]
#[doc = ""]
#[doc = "*This API is unstable and requires `--cfg=web_sys_unstable_apis` to be activated, as"]
#[doc = "[described in the `wasm-bindgen` guide](https://rustwasm.github.io/docs/wasm-bindgen/web-sys/unstable-apis.html)*"]
pub fn keys(this: &GpuSupportedFeatures) -> ::js_sys::Iterator;
#[cfg(web_sys_unstable_apis)]
# [wasm_bindgen (method , structural , js_class = "GPUSupportedFeatures" , js_name = values)]
#[doc = "The `values()` method."]
#[doc = ""]
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/GPUSupportedFeatures/values)"]
#[doc = ""]
#[doc = "*This API requires the following crate features to be activated: `GpuSupportedFeatures`*"]
#[doc = ""]
#[doc = "*This API is unstable and requires `--cfg=web_sys_unstable_apis` to be activated, as"]
#[doc = "[described in the `wasm-bindgen` guide](https://rustwasm.github.io/docs/wasm-bindgen/web-sys/unstable-apis.html)*"]
pub fn values(this: &GpuSupportedFeatures) -> ::js_sys::Iterator;
}
81 changes: 66 additions & 15 deletions crates/webidl-tests/globals.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const strictEqual = require('assert').strictEqual;

exports.noop = function () {};
exports.noop = function () { };

global.TestArrays = class {
strings(x) {
Expand Down Expand Up @@ -97,11 +97,11 @@ global.ArrayBufferTest = class {
};

global.TakeCallbackInterface = class {
a() {}
b() {}
a() { }
b() { }
};

global.assert_dict_c = function(c) {
global.assert_dict_c = function (c) {
strictEqual(c.a, 1);
strictEqual(c.b, 2);
strictEqual(c.c, 3);
Expand All @@ -112,17 +112,17 @@ global.assert_dict_c = function(c) {
strictEqual(c.h, 8);
};

global.mk_dict_a = function() {
global.mk_dict_a = function () {
return {};
};

global.assert_dict_required = function(c) {
global.assert_dict_required = function (c) {
strictEqual(c.a, 3);
strictEqual(c.b, "a");
strictEqual(c.c, 4);
};

global.assert_camel_case = function(dict) {
global.assert_camel_case = function (dict) {
strictEqual(dict.wierd_fieldName, 1);
}

Expand Down Expand Up @@ -219,6 +219,57 @@ global.TestReadWriteMapLike = class extends global.TestReadOnlyMapLike {
}
};

global.TestReadOnlySetLike = class {
constructor() {
this.set = new Set();
this.set.add('a');
this.set.add('b');
this.set.add('c');
}

entries() {
return this.set.entries();
}

forEach(callback, thisArg) {
return this.set.forEach(callback, thisArg);
}

has(value) {
return this.set.has(value);
}

keys() {
return this.set.keys();
}

values() {
return this.set.values();
}

get size() {
return this.set.size;
}
};

global.TestReadWriteSetLike = class extends global.TestReadOnlySetLike {
constructor() {
super();
}

add(value) {
return this.set.add(value);
}

delete(value) {
return this.set.delete(value);
}

clear() {
return this.set.clear();
}
};

global.math_test = {
pow(base, exp) {
return Math.pow(base, exp);
Expand All @@ -233,7 +284,7 @@ global.GetNoInterfaceObject = class {
static get() {
return {
number: 3,
foo: () => {},
foo: () => { },
}
}
};
Expand Down Expand Up @@ -266,7 +317,7 @@ global.NamedConstructorParent = class NamedConstructor {
this._value = 0;
}

get value(){
get value() {
return this._value;
}
};
Expand Down Expand Up @@ -296,7 +347,7 @@ global.StaticMethod = class StaticMethod {
StaticMethod.value = 0;

global.StaticProperty = class StaticProperty {
static get value(){
static get value() {
return StaticProperty._value;
}

Expand All @@ -308,14 +359,14 @@ global.StaticProperty = class StaticProperty {
StaticProperty._value = 0;

global.UndefinedMethod = class UndefinedMethod {
constructor() {}
constructor() { }
ok_method() {
return true;
}
};

global.NullableMethod = class NullableMethod {
constructor() {}
constructor() { }
opt(a) {
if (a == undefined) {
return undefined;
Expand Down Expand Up @@ -355,14 +406,14 @@ global.Indexing = function () {
};

global.OptionalAndUnionArguments = class OptionalAndUnionArguments {
constructor() {}
constructor() { }
m(a, b = true, c = 123, d = 456) {
return [typeof a, a, typeof b, b, typeof c, c, typeof d, d].join(', ');
}
};

global.Variadic = class Variadic {
constructor() {}
constructor() { }
sum(...values) {
return values.reduce((a, b) => a + b, 0);
}
Expand Down Expand Up @@ -402,7 +453,7 @@ global.MixinFoo = class MixinFoo {
};

global.Overloads = class {
foo() {}
foo() { }
};

global.InvokeCallback = class {
Expand Down
1 change: 1 addition & 0 deletions crates/webidl-tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub mod maplike;
pub mod namespace;
pub mod no_interface;
pub mod promise;
pub mod setlike;
pub mod simple;
pub mod throws;
pub mod unstable;
90 changes: 90 additions & 0 deletions crates/webidl-tests/setlike.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
use crate::generated::*;
use wasm_bindgen::prelude::*;
use wasm_bindgen_test::*;

macro_rules! read_test_suite {
($setlike:ty, $name:ident) => {
#[wasm_bindgen_test]
fn $name() {
// { "a", "b", "c" }
let setlike = <$setlike>::new().unwrap();

// readonly attribute unsigned long size;
assert_eq!(setlike.size(), 3);

// boolean has(V value);
assert!(setlike.has("a"));
assert!(setlike.has("b"));
assert!(setlike.has("c"));
assert!(!setlike.has("d"));

// { "a", "b", "c" }
let cb = Closure::wrap(Box::new(|value: String| match value.as_str() {
"a" => assert_eq!(value, "a"),
"b" => assert_eq!(value, "b"),
"c" => assert_eq!(value, "c"),
_ => panic!("unexpected key"),
}) as Box<dyn Fn(String)>);

setlike.for_each(cb.as_ref().unchecked_ref()).unwrap();

let mut entries_vec = vec![];

for entry in setlike.entries().into_iter() {
let entry = entry.unwrap();
let pair = entry.dyn_into::<js_sys::Array>().unwrap();
let value = pair.get(1).as_string().unwrap();

entries_vec.push(value);
}

assert_eq!(
&entries_vec,
&["a".to_string(), "b".to_string(), "c".to_string()]
);

let mut keys_vec = vec![];

for key in setlike.keys().into_iter() {
let key = key.unwrap();
keys_vec.push(key.as_string().unwrap());
}

assert_eq!(
&keys_vec,
&["a".to_string(), "b".to_string(), "c".to_string()]
);

let mut values_vec = vec![];

for value in setlike.values().into_iter() {
let value = value.unwrap();
values_vec.push(value.as_string().unwrap());
}

assert_eq!(
&values_vec,
&["a".to_string(), "b".to_string(), "c".to_string()]
);
}
};
}

read_test_suite!(TestReadOnlySetLike, read_readonly_setlike);
read_test_suite!(TestReadWriteSetLike, read_setlike);

#[wasm_bindgen_test]
fn write_setlike() {
// { "a", "b", "c" }
let setlike = TestReadWriteSetLike::new().unwrap();

setlike.add("d");
assert_eq!(setlike.size(), 4);

assert!(setlike.delete("d"));
assert_eq!(setlike.size(), 3);

// undefined clear();
setlike.clear();
assert_eq!(setlike.size(), 0);
}
9 changes: 9 additions & 0 deletions crates/webidl-tests/webidls/enabled/setlike.webidl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[Constructor()]
interface TestReadOnlySetLike {
readonly setlike<DOMString>;
};

[Constructor()]
interface TestReadWriteSetLike {
setlike<DOMString>;
};
Loading

0 comments on commit 8fc0e2b

Please sign in to comment.