Skip to content
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

Create the web-sys crate mechanically from WebIDL #409

Merged
merged 42 commits into from
Jul 9, 2018
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
0b3c2c3
Create a new `web-sys` crate
fitzgen Jul 5, 2018
248d233
ci: Test the new `web-sys` crate in CI
fitzgen Jul 5, 2018
f591478
web-sys: Add a small README
fitzgen Jul 5, 2018
40239d3
web-sys: Vendor all the WebIDL files from mozilla-central
fitzgen Jul 5, 2018
6d26828
backend: Add a pass to remove AST items that use undefined imports
fitzgen Jul 6, 2018
8005a89
webidl: Add a bunch of missing semicolons
fitzgen Jul 6, 2018
c55e1c5
webidl: Make parsing private
fitzgen Jul 6, 2018
2d552a1
webidl: Remove uses of undefined import types
fitzgen Jul 6, 2018
d432b5b
test-project-builder: Add more profiling timers
fitzgen Jul 6, 2018
43a740a
test-project-builder: Detect when webpack-dev-server fails
fitzgen Jul 6, 2018
af2aa4f
webidl: Specify version for dev-dependency on wasm-bindgen-backend
fitzgen Jul 6, 2018
71ec407
test-project-builder: Build projects in "very verbose" mode
fitzgen Jul 6, 2018
efee676
guide: Add section about contributing to `web-sys`
fitzgen Jul 5, 2018
13c93c4
WIP enable Event.webidl
fitzgen Jul 6, 2018
6c3ed64
Update expected webidl output
alexcrichton Jul 7, 2018
5f671f8
Start out a test's status as incomplete
alexcrichton Jul 7, 2018
455c102
Fix onerror function in headless mode
alexcrichton Jul 7, 2018
a79dd3c
Fix package.json/node_modules handling in project generation
alexcrichton Jul 7, 2018
94529ae
Avoid logging body text
alexcrichton Jul 7, 2018
b4f0fb5
Fix a relative path
alexcrichton Jul 7, 2018
cff9559
More expected test fixes
alexcrichton Jul 7, 2018
ecf6bdd
Fix a typo
alexcrichton Jul 7, 2018
384a983
Merge remote-tracking branch 'origin/master' into web-sys
alexcrichton Jul 7, 2018
c43d740
Merge remote-tracking branch 'rustwasm/master' into web-sys
fitzgen Jul 9, 2018
3266ced
test-project-builder: Allow asynchronous tests
fitzgen Jul 9, 2018
16ea94c
webidl: Convert [Unforgeable] attributes into `#[wasm_bindgen(structu…
fitzgen Jul 9, 2018
4040020
test-project-builder: Print generated WebIDL bindings for debugging p…
fitzgen Jul 9, 2018
882183b
When we can't find a descriptor, say which one can't be found
fitzgen Jul 9, 2018
126207d
web-sys: Test bindings for Event
fitzgen Jul 9, 2018
c992343
ci: Use `--manifest-path dir` instead of `cd dir && ...`
fitzgen Jul 9, 2018
68478c6
web-sys: Just move .webidl files isntead of symlinking to enable them
fitzgen Jul 9, 2018
5d7fdea
Merge remote-tracking branch 'rustwasm/master' into web-sys
fitzgen Jul 9, 2018
6bfeab5
tests: Polyfill Array.prototype.values for older browsers in CI
fitzgen Jul 9, 2018
bc06137
test-project-builder: Don't panic on poisoned headless test mutex
fitzgen Jul 9, 2018
f7bb74d
JsValue: Add {is,as}_{object,function} methods
fitzgen Jul 9, 2018
ae3b610
tidy: Fix whitespace and missing semicolons
fitzgen Jul 9, 2018
05d9eb8
Allow for dynamic feature detection of methods
fitzgen Jul 9, 2018
834bfad
tests: Do feature detection in Array.prototype.values test
fitzgen Jul 9, 2018
8bc01f4
Merge remote-tracking branch 'rustwasm/master' into web-sys
fitzgen Jul 9, 2018
4076f4f
Add JsValue::{is_string, as_js_string} methods
fitzgen Jul 9, 2018
eeebca9
eslint: allow backtick string literals
fitzgen Jul 9, 2018
0a5a50b
Only generate a fallback import function for non-structural imports
fitzgen Jul 9, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ matrix:
./build.sh) || exit 1;
done

# The `web-sys` crate's tests pass on nightly.
- rust: nightly
env: JOB=test-web-sys
before_install: *INSTALL_NODE_VIA_NVM
install:
- npm install
script:
- (cd crates/web-sys && cargo test)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm personally also a fan of cargo test --manifest-path crates/web-sys/Cargo.toml

addons:
firefox: latest

# Tests pass on nightly using yarn
- rust: nightly
env: JOB=test-yarn-smoke
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ members = [
"crates/cli",
"crates/typescript",
"crates/webidl",
"crates/web-sys",
"examples/hello_world",
"examples/smorgasboard",
"examples/console_log",
Expand Down
256 changes: 256 additions & 0 deletions crates/backend/src/defined.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@
use ast;
use proc_macro2::Ident;
use syn;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ImportedTypeKind {
/// The definition of an imported type.
Definition,
/// A reference to an imported type.
Reference,
}

/// Iterate over definitions of and references to imported types in the AST.
pub trait ImportedTypes {
fn imported_types<F>(&self, f: &mut F)
where
F: FnMut(&Ident, ImportedTypeKind);
}

/// Iterate over definitions of imported types in the AST.
pub trait ImportedTypeDefinitions {
fn imported_type_definitions<F>(&self, f: &mut F)
where
F: FnMut(&Ident);
}

impl<T> ImportedTypeDefinitions for T
where
T: ImportedTypes,
{
fn imported_type_definitions<F>(&self, f: &mut F)
where
F: FnMut(&Ident),
{
self.imported_types(&mut |id, kind| {
if let ImportedTypeKind::Definition = kind {
f(id);
}
});
}
}

/// Iterate over references to imported types in the AST.
pub trait ImportedTypeReferences {
fn imported_type_references<F>(&self, f: &mut F)
where
F: FnMut(&Ident);
}

impl<T> ImportedTypeReferences for T
where
T: ImportedTypes,
{
fn imported_type_references<F>(&self, f: &mut F)
where
F: FnMut(&Ident),
{
self.imported_types(&mut |id, kind| {
if let ImportedTypeKind::Reference = kind {
f(id);
}
});
}
}

impl ImportedTypes for ast::Program {
fn imported_types<F>(&self, f: &mut F)
where
F: FnMut(&Ident, ImportedTypeKind),
{
self.imports.imported_types(f);
self.type_aliases.imported_types(f);
}
}

impl<T> ImportedTypes for Vec<T>
where
T: ImportedTypes,
{
fn imported_types<F>(&self, f: &mut F)
where
F: FnMut(&Ident, ImportedTypeKind),
{
for x in self {
x.imported_types(f);
}
}
}

impl ImportedTypes for ast::Import {
fn imported_types<F>(&self, f: &mut F)
where
F: FnMut(&Ident, ImportedTypeKind),
{
self.kind.imported_types(f)
}
}

impl ImportedTypes for ast::ImportKind {
fn imported_types<F>(&self, f: &mut F)
where
F: FnMut(&Ident, ImportedTypeKind),
{
match self {
ast::ImportKind::Static(s) => s.imported_types(f),
ast::ImportKind::Function(fun) => fun.imported_types(f),
ast::ImportKind::Type(ty) => ty.imported_types(f),
}
}
}

impl ImportedTypes for ast::ImportStatic {
fn imported_types<F>(&self, f: &mut F)
where
F: FnMut(&Ident, ImportedTypeKind),
{
self.ty.imported_types(f);
}
}

impl ImportedTypes for syn::Type {
fn imported_types<F>(&self, f: &mut F)
where
F: FnMut(&Ident, ImportedTypeKind),
{
match self {
syn::Type::Reference(ref r) => r.imported_types(f),
syn::Type::Path(ref p) => p.imported_types(f),
_ => {}
}
}
}

impl ImportedTypes for syn::TypeReference {
fn imported_types<F>(&self, f: &mut F)
where
F: FnMut(&Ident, ImportedTypeKind),
{
self.elem.imported_types(f);
}
}

impl ImportedTypes for syn::TypePath {
fn imported_types<F>(&self, f: &mut F)
where
F: FnMut(&Ident, ImportedTypeKind),
{
if self.qself.is_some()
|| self.path.leading_colon.is_some()
|| self.path.segments.len() != 1
{
return;
}
f(
&self.path.segments.last().unwrap().value().ident,
ImportedTypeKind::Reference,
);
}
}

impl ImportedTypes for ast::ImportFunction {
fn imported_types<F>(&self, f: &mut F)
where
F: FnMut(&Ident, ImportedTypeKind),
{
self.function.imported_types(f);
self.kind.imported_types(f);
}
}

impl ImportedTypes for ast::ImportFunctionKind {
fn imported_types<F>(&self, f: &mut F)
where
F: FnMut(&Ident, ImportedTypeKind),
{
match self {
ast::ImportFunctionKind::Method { ty, .. } => ty.imported_types(f),
ast::ImportFunctionKind::Normal => {}
}
}
}

impl ImportedTypes for ast::Function {
fn imported_types<F>(&self, f: &mut F)
where
F: FnMut(&Ident, ImportedTypeKind),
{
self.arguments.imported_types(f);
if let Some(ref r) = self.ret {
r.imported_types(f);
}
}
}

impl ImportedTypes for syn::ArgCaptured {
fn imported_types<F>(&self, f: &mut F)
where
F: FnMut(&Ident, ImportedTypeKind),
{
self.ty.imported_types(f);
}
}

impl ImportedTypes for ast::ImportType {
fn imported_types<F>(&self, f: &mut F)
where
F: FnMut(&Ident, ImportedTypeKind),
{
f(&self.name, ImportedTypeKind::Definition);
}
}

impl ImportedTypes for ast::TypeAlias {
fn imported_types<F>(&self, f: &mut F)
where
F: FnMut(&Ident, ImportedTypeKind),
{
f(&self.dest, ImportedTypeKind::Reference);
}
}

/// Remove any methods, statics, &c, that reference types that are *not*
/// defined.
pub trait RemoveUndefinedImports {
fn remove_undefined_imports<F>(&mut self, is_defined: &F)
where
F: Fn(&Ident) -> bool;
}

impl RemoveUndefinedImports for ast::Program {
fn remove_undefined_imports<F>(&mut self, is_defined: &F)
where
F: Fn(&Ident) -> bool,
{
self.imports.remove_undefined_imports(is_defined);
self.type_aliases.remove_undefined_imports(is_defined);
}
}

impl<T> RemoveUndefinedImports for Vec<T>
where
T: ImportedTypeReferences,
{
fn remove_undefined_imports<F>(&mut self, is_defined: &F)
where
F: Fn(&Ident) -> bool,
{
self.retain(|x| {
let mut all_defined = true;
x.imported_type_references(&mut |id| {
all_defined = all_defined && is_defined(id);
});
all_defined
});
}
}
1 change: 1 addition & 0 deletions crates/backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ extern crate wasm_bindgen_shared as shared;

pub mod ast;
mod codegen;
pub mod defined;
pub mod util;
31 changes: 25 additions & 6 deletions crates/test-project-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,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)
Expand Down Expand Up @@ -677,7 +678,10 @@ impl Project {
lazy_static! {
static ref MUTEX: Mutex<()> = Mutex::new(());
}
let _lock = MUTEX.lock().unwrap();
let _lock = {
let _x = wrap_step("waiting on headless test lock");
MUTEX.lock().unwrap()
};

let mut cmd = self.npm();
cmd.arg("run")
Expand All @@ -686,20 +690,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"))
Expand Down Expand Up @@ -775,6 +788,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());
Expand Down
16 changes: 16 additions & 0 deletions crates/web-sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "web-sys"
version = "0.1.0"
authors = ["Nick Fitzgerald <fitzgen@gmail.com>"]
readme = "./README.md"

[build-dependencies]
env_logger = "0.5.10"
failure = "0.1"
wasm-bindgen-webidl = { path = "../webidl", version = "=0.2.11" }

[dependencies]
wasm-bindgen = { path = "../..", version = "=0.2.11" }

[dev-dependencies]
wasm-bindgen-test-project-builder = { path = "../test-project-builder", version = '=0.2.11' }
3 changes: 3 additions & 0 deletions crates/web-sys/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# `web-sys`

Raw bindings to Web APIs for projects using `wasm-bindgen`.
Loading