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 all 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
  •  
  •  
  •  
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
],
"quotes": [
"error",
"single"
"single",
{ "allowTemplateLiterals": true }
],
"semi": [
"error",
Expand Down
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ 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: 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 @@ -11,4 +11,5 @@ extern crate wasm_bindgen_shared as shared;

pub mod ast;
mod codegen;
pub mod defined;
pub mod util;
53 changes: 50 additions & 3 deletions crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,40 @@ impl<'a> Context<'a> {
))
})?;

self.bind("__wbindgen_is_object", &|me| {
me.expose_get_object();
Ok(String::from(
"
function(i) {
const val = getObject(i);
return typeof(val) === 'object' && val !== null ? 1 : 0;
}
",
))
})?;

self.bind("__wbindgen_is_function", &|me| {
me.expose_get_object();
Ok(String::from(
"
function(i) {
return typeof(getObject(i)) === 'function' ? 1 : 0;
}
",
))
})?;

self.bind("__wbindgen_is_string", &|me| {
me.expose_get_object();
Ok(String::from(
"
function(i) {
return typeof(getObject(i)) === 'string' ? 1 : 0;
}
",
))
})?;

self.bind("__wbindgen_string_get", &|me| {
me.expose_pass_string_to_wasm()?;
me.expose_get_object();
Expand Down Expand Up @@ -1501,7 +1535,7 @@ impl<'a> Context<'a> {
if (desc) return desc;
obj = Object.getPrototypeOf(obj);
}
throw new Error('descriptor not found');
throw new Error(`descriptor for id='${id}' not found`);
}
",
);
Expand Down Expand Up @@ -1866,11 +1900,24 @@ impl<'a, 'b> SubContext<'a, 'b> {
}
};

let fallback = if import.structural {
"".to_string()
} else {
format!(
" || function() {{
throw new Error(`wasm-bindgen: {} does not exist`);
}}",
target
)
};

self.cx.global(&format!(
"
const {}_target = {};
const {}_target = {} {} ;
",
import.shim, target
import.shim,
target,
fallback
));
format!(
"{}_target{}",
Expand Down
Loading