Skip to content

Commit 9057941

Browse files
committed
Add WebIDL support for the object type
This maps to the `Object` type in the `js_sys` crate.
1 parent 654bb9b commit 9057941

File tree

6 files changed

+59
-4
lines changed

6 files changed

+59
-4
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
use wasm_bindgen::JsValue;
2+
use wasm_bindgen_test::*;
3+
use web_sys::{DomPoint, DomPointReadOnly};
4+
5+
#[wasm_bindgen_test]
6+
fn dom_point() {
7+
let x = DomPoint::new(1.0, 2.0, 3.0, 4.0).unwrap();
8+
assert_eq!(x.x(), 1.0);
9+
x.set_x(1.5);
10+
assert_eq!(x.x(), 1.5);
11+
12+
assert_eq!(x.y(), 2.0);
13+
x.set_y(2.5);
14+
assert_eq!(x.y(), 2.5);
15+
16+
assert_eq!(x.z(), 3.0);
17+
x.set_z(3.5);
18+
assert_eq!(x.z(), 3.5);
19+
20+
assert_eq!(x.w(), 4.0);
21+
x.set_w(4.5);
22+
assert_eq!(x.w(), 4.5);
23+
}
24+
25+
#[wasm_bindgen_test]
26+
fn dom_point_readonly() {
27+
let x = DomPoint::new(1.0, 2.0, 3.0, 4.0).unwrap();
28+
let x = DomPointReadOnly::from(JsValue::from(x));
29+
assert_eq!(x.x(), 1.0);
30+
assert_eq!(x.y(), 2.0);
31+
assert_eq!(x.z(), 3.0);
32+
assert_eq!(x.w(), 4.0);
33+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,5 @@ pub mod style_element;
5050
pub mod table_element;
5151
pub mod title_element;
5252
pub mod xpath_result;
53+
pub mod dom_point;
54+
pub mod performance;

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use wasm_bindgen_test::*;
2-
use wasm_bindgen::prelude::*;
32
use web_sys::HtmlOptionElement;
43

54
#[wasm_bindgen_test]
@@ -41,4 +40,4 @@ fn test_option_element() {
4140
assert_eq!(option.text(), "potato", "Option should have the text we gave it.");
4241

4342
assert_eq!(option.index(), 0, "This should be the first option, since there are no other known options.");
44-
}
43+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use wasm_bindgen::prelude::*;
2+
use wasm_bindgen_test::*;
3+
use web_sys::Performance;
4+
5+
#[wasm_bindgen]
6+
extern {
7+
#[wasm_bindgen(js_name = performance)]
8+
static PERFORMANCE: Performance;
9+
}
10+
11+
#[wasm_bindgen_test]
12+
fn to_json() {
13+
let perf = JsValue::from(PERFORMANCE.to_json());
14+
assert!(perf.is_object());
15+
}

crates/webidl/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ fn compile_ast(mut ast: backend::ast::Program) -> String {
102102
vec![
103103
"str", "char", "bool", "JsValue", "u8", "i8", "u16", "i16", "u32", "i32", "u64", "i64",
104104
"usize", "isize", "f32", "f64", "Result", "String", "Vec", "Option",
105-
"ArrayBuffer",
105+
"ArrayBuffer", "Object",
106106
].into_iter()
107107
.map(|id| proc_macro2::Ident::new(id, proc_macro2::Span::call_site())),
108108
);

crates/webidl/src/util.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,12 +430,18 @@ impl<'a> FirstPassRecord<'a> {
430430
leading_colon_path_ty(vec![rust_ident("js_sys"), rust_ident("ArrayBuffer")])
431431
}
432432

433+
// The WebIDL `object` maps to the ECMAScript `Object`
434+
//
435+
// https://heycam.github.io/webidl/#es-object
436+
webidl::ast::TypeKind::Object => {
437+
leading_colon_path_ty(vec![rust_ident("js_sys"), rust_ident("Object")])
438+
}
439+
433440
// Support for these types is not yet implemented, so skip
434441
// generating any bindings for this function.
435442
| webidl::ast::TypeKind::DataView
436443
| webidl::ast::TypeKind::Error
437444
| webidl::ast::TypeKind::FrozenArray(_)
438-
| webidl::ast::TypeKind::Object
439445
| webidl::ast::TypeKind::Promise(_)
440446
| webidl::ast::TypeKind::Record(..)
441447
| webidl::ast::TypeKind::Sequence(_)

0 commit comments

Comments
 (0)