From a98b5ea2a0af7d7223f839155dcd66d42d2cc805 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sat, 4 Aug 2018 13:51:22 -0700 Subject: [PATCH] Add WebIDL support for the `ArrayBuffer` type Should help enable a slew of new bindings as well. --- crates/web-sys/Cargo.toml | 1 + crates/web-sys/src/lib.rs | 1 + crates/webidl-tests/Cargo.toml | 3 ++- crates/webidl-tests/array_buffer.js | 8 ++++++++ crates/webidl-tests/array_buffer.rs | 11 +++++++++++ crates/webidl-tests/array_buffer.webidl | 5 +++++ crates/webidl-tests/build.rs | 9 +++++++-- crates/webidl-tests/main.rs | 6 ++++-- crates/webidl/src/lib.rs | 1 + crates/webidl/src/util.rs | 9 ++++++++- 10 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 crates/webidl-tests/array_buffer.js create mode 100644 crates/webidl-tests/array_buffer.rs create mode 100644 crates/webidl-tests/array_buffer.webidl diff --git a/crates/web-sys/Cargo.toml b/crates/web-sys/Cargo.toml index 13ca6478..d07f5830 100644 --- a/crates/web-sys/Cargo.toml +++ b/crates/web-sys/Cargo.toml @@ -16,6 +16,7 @@ sourcefile = "0.1" [dependencies] wasm-bindgen = { path = "../..", version = "0.2.15" } +js-sys = { path = '../js-sys', version = '0.2.0' } [target.'cfg(target_arch = "wasm32")'.dev-dependencies] futures = "0.1" diff --git a/crates/web-sys/src/lib.rs b/crates/web-sys/src/lib.rs index 696d1fd2..51ab3425 100755 --- a/crates/web-sys/src/lib.rs +++ b/crates/web-sys/src/lib.rs @@ -1,5 +1,6 @@ #![doc(html_root_url = "https://docs.rs/web-sys/0.2")] extern crate wasm_bindgen; +extern crate js_sys; include!(concat!(env!("OUT_DIR"), "/bindings.rs")); diff --git a/crates/webidl-tests/Cargo.toml b/crates/webidl-tests/Cargo.toml index 3c3efb0b..99dd82ed 100644 --- a/crates/webidl-tests/Cargo.toml +++ b/crates/webidl-tests/Cargo.toml @@ -12,8 +12,9 @@ path = 'lib.rs' wasm-bindgen-webidl = { path = '../webidl' } [dev-dependencies] -wasm-bindgen-test = { path = '../test' } +js-sys = { path = '../js-sys' } wasm-bindgen = { path = '../..' } +wasm-bindgen-test = { path = '../test' } [[test]] name = 'wasm' diff --git a/crates/webidl-tests/array_buffer.js b/crates/webidl-tests/array_buffer.js new file mode 100644 index 00000000..f4c1b871 --- /dev/null +++ b/crates/webidl-tests/array_buffer.js @@ -0,0 +1,8 @@ +global.ArrayBufferTest = class { + getBuffer() { + return new ArrayBuffer(3); + } + setBuffer(x) { + // ... + } +}; diff --git a/crates/webidl-tests/array_buffer.rs b/crates/webidl-tests/array_buffer.rs new file mode 100644 index 00000000..5aed55f9 --- /dev/null +++ b/crates/webidl-tests/array_buffer.rs @@ -0,0 +1,11 @@ +use wasm_bindgen_test::*; + +include!(concat!(env!("OUT_DIR"), "/array_buffer.rs")); + +#[wasm_bindgen_test] +fn take_and_return_a_bunch_of_slices() { + let f = ArrayBufferTest::new().unwrap(); + let x = f.get_buffer(); + f.set_buffer(None); + f.set_buffer(Some(x)); +} diff --git a/crates/webidl-tests/array_buffer.webidl b/crates/webidl-tests/array_buffer.webidl new file mode 100644 index 00000000..d3f7b24b --- /dev/null +++ b/crates/webidl-tests/array_buffer.webidl @@ -0,0 +1,5 @@ +[Constructor] +interface ArrayBufferTest { + ArrayBuffer getBuffer(); + void setBuffer(ArrayBuffer? b); +}; diff --git a/crates/webidl-tests/build.rs b/crates/webidl-tests/build.rs index 2c468802..2f8294b3 100644 --- a/crates/webidl-tests/build.rs +++ b/crates/webidl-tests/build.rs @@ -1,8 +1,9 @@ extern crate wasm_bindgen_webidl; -use std::fs; use std::env; +use std::fs; use std::path::PathBuf; +use std::process::Command; fn main() { let idls = fs::read_dir(".") @@ -41,6 +42,10 @@ fn main() { }} "#, js_file.display(), i)); - fs::write(out_file, generated_rust).unwrap(); + fs::write(&out_file, generated_rust).unwrap(); + + // Attempt to run rustfmt, but don't worry if it fails or if it isn't + // installed, this is just to help with debugging + drop(Command::new("rustfmt").arg(&out_file).status()); } } diff --git a/crates/webidl-tests/main.rs b/crates/webidl-tests/main.rs index 7e0e0008..2a31140b 100644 --- a/crates/webidl-tests/main.rs +++ b/crates/webidl-tests/main.rs @@ -1,10 +1,12 @@ #![feature(use_extern_macros)] -extern crate wasm_bindgen_test; +extern crate js_sys; extern crate wasm_bindgen; +extern crate wasm_bindgen_test; +pub mod array; +pub mod array_buffer; pub mod consts; pub mod enums; pub mod simple; pub mod throws; -pub mod array; diff --git a/crates/webidl/src/lib.rs b/crates/webidl/src/lib.rs index 6462ea02..5f5cf8f9 100644 --- a/crates/webidl/src/lib.rs +++ b/crates/webidl/src/lib.rs @@ -102,6 +102,7 @@ fn compile_ast(mut ast: backend::ast::Program) -> String { vec![ "str", "char", "bool", "JsValue", "u8", "i8", "u16", "i16", "u32", "i32", "u64", "i64", "usize", "isize", "f32", "f64", "Result", "String", "Vec", "Option", + "ArrayBuffer", ].into_iter() .map(|id| proc_macro2::Ident::new(id, proc_macro2::Span::call_site())), ); diff --git a/crates/webidl/src/util.rs b/crates/webidl/src/util.rs index 21910184..8581eaee 100644 --- a/crates/webidl/src/util.rs +++ b/crates/webidl/src/util.rs @@ -422,9 +422,16 @@ impl<'a> FirstPassRecord<'a> { } } + // This seems like a "naively correct" mapping, but the online docs + // are a bit scary in this regard... + // + // https://heycam.github.io/webidl/#es-buffer-source-types + webidl::ast::TypeKind::ArrayBuffer => { + simple_path_ty(vec![rust_ident("js_sys"), rust_ident("ArrayBuffer")]) + } + // Support for these types is not yet implemented, so skip // generating any bindings for this function. - webidl::ast::TypeKind::ArrayBuffer | webidl::ast::TypeKind::DataView | webidl::ast::TypeKind::Error | webidl::ast::TypeKind::FrozenArray(_)