Create the web-sys crate mechanically from WebIDL (#409)

* Create a new `web-sys` crate

This will eventually contain all the WebIDL-generated bindings to Web APIs.

* ci: Test the new `web-sys` crate in CI

* web-sys: Add a small README

* web-sys: Vendor all the WebIDL files from mozilla-central

* backend: Add a pass to remove AST items that use undefined imports

This is necessary for the WebIDL frontend, which can't translate many WebIDL
constructs into equivalent wasm-bindgen AST things yet. It lets us make
incremental progress: we can generate bindings to methods we can support right
now even though there might be methods on the same interface that we can't
support yet.

* webidl: Add a bunch of missing semicolons

* webidl: Make parsing private

It was only `pub` so that we could test it, but we ended up moving towards
integration tests rather than unit tests that assert particular ASTs are parsed
from WebIDL files.

* webidl: Remove uses of undefined import types

* test-project-builder: Build projects in "very verbose" mode

This helps for debugging failing WebIDL-related tests.

* test-project-builder: Add more profiling timers

* test-project-builder: Detect when webpack-dev-server fails

Instead of going into an infinite loop, detect when webpack-dev-server fails to
start up and early exit the test.

* webidl: Specify version for dev-dependency on wasm-bindgen-backend

Instead of only a relative path.

* guide: Add section about contributing to `web-sys`

* WIP enable Event.webidl

Still need to fix and finish the test.

* Update expected webidl output

* Start out a test's status as incomplete

That way if we don't fill it in the error message doesn't look quite so bizarre

* Fix onerror function in headless mode

Otherwise we don't see any output!

* Fix package.json/node_modules handling in project generation

Make sure these are looked up in the git project root rather than the crate root

* Avoid logging body text

This was meant for debugging and is otherwise pretty noisy

* Fix a relative path

* More expected test fixes

* Fix a typo

* test-project-builder: Allow asynchronous tests

* webidl: Convert [Unforgeable] attributes into `#[wasm_bindgen(structural)]`

Fixes #432

* test-project-builder: Print generated WebIDL bindings for debugging purposes

Helps debug bad WebIDL bindings generation inside tests.

* When we can't find a descriptor, say which one can't be found

This helps when debugging things that need to become structural.

* web-sys: Test bindings for Event

* ci: Use `--manifest-path dir` instead of `cd dir && ...`

* web-sys: Just move .webidl files isntead of symlinking to enable them

* tests: Polyfill Array.prototype.values for older browsers in CI

* test-project-builder: Don't panic on poisoned headless test mutex

We only use it to serialize headless tests so that we don't try to bind the port
concurrently. Its OK to run another headless test if an earlier one panicked.

* JsValue: Add {is,as}_{object,function} methods

Allows dynamically casting values to `js::Object` and `js::Function`.

* tidy: Fix whitespace and missing semicolons

* Allow for dynamic feature detection of methods

If we create bindings to a method that doesn't exist in this implementation,
then it shouldn't fail until if/when we actually try and invoke that missing
method.

* tests: Do feature detection in Array.prototype.values test

* Add JsValue::{is_string, as_js_string} methods

And document all the cast/convert/check methods for js value.

* eslint: allow backtick string literals

* Only generate a fallback import function for non-structural imports
This commit is contained in:
Nick Fitzgerald
2018-07-09 16:35:25 -07:00
committed by GitHub
parent ade4561eba
commit f2f2d7231a
701 changed files with 31216 additions and 177 deletions

View File

@ -16,6 +16,7 @@ use std::time::{Duration, Instant};
static CNT: AtomicUsize = ATOMIC_USIZE_INIT;
thread_local!(static IDX: usize = CNT.fetch_add(1, Ordering::SeqCst));
#[derive(Clone)]
pub struct Project {
files: Vec<(String, String)>,
debug: bool,
@ -423,18 +424,17 @@ impl Project {
buildrs.push_str(&format!(
r#"
fs::create_dir_all("{}").unwrap();
let bindings = compile_file(Path::new("{}")).expect("should compile OK");
println!("generated WebIDL bindings = '''\n{{}}\n'''", bindings);
File::create(&Path::new(&dest).join("{}"))
.unwrap()
.write_all(
compile_file(Path::new("{}"))
.unwrap()
.as_bytes()
)
.write_all(bindings.as_bytes())
.unwrap();
"#,
path.parent().unwrap().to_str().unwrap(),
path.to_str().unwrap(),
origpath.to_str().unwrap(),
path.to_str().unwrap(),
));
self.files.push((
@ -477,8 +477,8 @@ impl Project {
}
runjs.push_str("
function run(test, wasm) {
test.test();
async function run(test, wasm) {
await test.test();
if (wasm.assertStackEmpty)
wasm.assertStackEmpty();
@ -544,11 +544,13 @@ impl Project {
assert!(modules.is_empty());
runjs.push_str("
const test = require('./test');
try {
run(test, {});
} catch (e) {
onerror(e);
}
(async function () {
try {
await run(test, {});
} catch (e) {
onerror(e);
}
}());
");
}
self.files.push(("run.js".to_string(), runjs));
@ -560,6 +562,7 @@ impl Project {
let (root, target_dir) = self.build();
let mut cmd = Command::new("cargo");
cmd.arg("build")
.arg("-vv")
.arg("--target")
.arg("wasm32-unknown-unknown")
.current_dir(&root)
@ -638,12 +641,15 @@ impl Project {
// move files from the root into each test, it looks like this may be
// needed for webpack to work well when invoked concurrently.
fs::hard_link("package.json", root.join("package.json")).unwrap();
if !Path::new("node_modules").exists() {
let mut cwd = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
cwd.pop(); // chop off test-project-builder
cwd.pop(); // chop off crates
fs::copy(cwd.join("package.json"), root.join("package.json")).unwrap();
let modules = cwd.join("node_modules");
if !modules.exists() {
panic!("\n\nfailed to find `node_modules`, have you run `npm install` yet?\n\n");
}
let cwd = env::current_dir().unwrap();
symlink_dir(&cwd.join("node_modules"), &root.join("node_modules")).unwrap();
symlink_dir(&modules, &root.join("node_modules")).unwrap();
if self.headless {
return self.test_headless(&root)
@ -677,7 +683,12 @@ impl Project {
lazy_static! {
static ref MUTEX: Mutex<()> = Mutex::new(());
}
let _lock = MUTEX.lock();
let _lock = {
let _x = wrap_step("waiting on headless test lock");
// Don't panic on a poisoned mutex, since we only use it to
// serialize servers.
MUTEX.lock().unwrap_or_else(|poisoned| poisoned.into_inner())
};
let mut cmd = self.npm();
cmd.arg("run")
@ -686,20 +697,29 @@ impl Project {
.arg("--quiet")
.arg("--watch-stdin")
.current_dir(&root);
let _server = run_in_background(&mut cmd, "webpack-dev-server".into());
let mut server = run_in_background(&mut cmd, "webpack-dev-server".into());
// wait for webpack-dev-server to come online and bind its port
loop {
if TcpStream::connect("127.0.0.1:8080").is_ok() {
break;
{
let _x = wrap_step("waiting for webpack-dev-server");
loop {
if TcpStream::connect("127.0.0.1:8080").is_ok() {
break;
}
if server.exited() {
panic!("webpack-dev-server exited during headless test initialization")
}
thread::sleep(Duration::from_millis(100));
}
thread::sleep(Duration::from_millis(100));
}
let path = env::var_os("PATH").unwrap_or_default();
let mut path = env::split_paths(&path).collect::<Vec<_>>();
path.push(root.join("node_modules/geckodriver"));
let _x = wrap_step("running headless test");
let mut cmd = Command::new("node");
cmd.args(&self.node_args)
.arg(root.join("run-headless.js"))
@ -775,6 +795,12 @@ struct BackgroundChild {
stderr: Option<thread::JoinHandle<io::Result<String>>>,
}
impl BackgroundChild {
pub fn exited(&mut self) -> bool {
self.child.try_wait().expect("should try_wait OK").is_some()
}
}
impl Drop for BackgroundChild {
fn drop(&mut self) {
drop(self.stdin.take());