Skip to content

Commit f2f2d72

Browse files
authored
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
1 parent ade4561 commit f2f2d72

File tree

701 files changed

+31216
-177
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

701 files changed

+31216
-177
lines changed

.eslintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
],
1919
"quotes": [
2020
"error",
21-
"single"
21+
"single",
22+
{ "allowTemplateLiterals": true }
2223
],
2324
"semi": [
2425
"error",

.travis.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ matrix:
6060
./build.sh) || exit 1;
6161
done
6262
63+
# The `web-sys` crate's tests pass on nightly.
64+
- rust: nightly
65+
env: JOB=test-web-sys
66+
before_install: *INSTALL_NODE_VIA_NVM
67+
install:
68+
- npm install
69+
script: cargo test --manifest-path crates/web-sys/Cargo.toml
70+
addons:
71+
firefox: latest
72+
6373
# Tests pass on nightly using yarn
6474
- rust: nightly
6575
env: JOB=test-yarn-smoke

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ members = [
4040
"crates/cli",
4141
"crates/typescript",
4242
"crates/webidl",
43+
"crates/web-sys",
4344
"examples/hello_world",
4445
"examples/smorgasboard",
4546
"examples/console_log",

crates/backend/src/defined.rs

Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
1+
use ast;
2+
use proc_macro2::Ident;
3+
use syn;
4+
5+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6+
pub enum ImportedTypeKind {
7+
/// The definition of an imported type.
8+
Definition,
9+
/// A reference to an imported type.
10+
Reference,
11+
}
12+
13+
/// Iterate over definitions of and references to imported types in the AST.
14+
pub trait ImportedTypes {
15+
fn imported_types<F>(&self, f: &mut F)
16+
where
17+
F: FnMut(&Ident, ImportedTypeKind);
18+
}
19+
20+
/// Iterate over definitions of imported types in the AST.
21+
pub trait ImportedTypeDefinitions {
22+
fn imported_type_definitions<F>(&self, f: &mut F)
23+
where
24+
F: FnMut(&Ident);
25+
}
26+
27+
impl<T> ImportedTypeDefinitions for T
28+
where
29+
T: ImportedTypes,
30+
{
31+
fn imported_type_definitions<F>(&self, f: &mut F)
32+
where
33+
F: FnMut(&Ident),
34+
{
35+
self.imported_types(&mut |id, kind| {
36+
if let ImportedTypeKind::Definition = kind {
37+
f(id);
38+
}
39+
});
40+
}
41+
}
42+
43+
/// Iterate over references to imported types in the AST.
44+
pub trait ImportedTypeReferences {
45+
fn imported_type_references<F>(&self, f: &mut F)
46+
where
47+
F: FnMut(&Ident);
48+
}
49+
50+
impl<T> ImportedTypeReferences for T
51+
where
52+
T: ImportedTypes,
53+
{
54+
fn imported_type_references<F>(&self, f: &mut F)
55+
where
56+
F: FnMut(&Ident),
57+
{
58+
self.imported_types(&mut |id, kind| {
59+
if let ImportedTypeKind::Reference = kind {
60+
f(id);
61+
}
62+
});
63+
}
64+
}
65+
66+
impl ImportedTypes for ast::Program {
67+
fn imported_types<F>(&self, f: &mut F)
68+
where
69+
F: FnMut(&Ident, ImportedTypeKind),
70+
{
71+
self.imports.imported_types(f);
72+
self.type_aliases.imported_types(f);
73+
}
74+
}
75+
76+
impl<T> ImportedTypes for Vec<T>
77+
where
78+
T: ImportedTypes,
79+
{
80+
fn imported_types<F>(&self, f: &mut F)
81+
where
82+
F: FnMut(&Ident, ImportedTypeKind),
83+
{
84+
for x in self {
85+
x.imported_types(f);
86+
}
87+
}
88+
}
89+
90+
impl ImportedTypes for ast::Import {
91+
fn imported_types<F>(&self, f: &mut F)
92+
where
93+
F: FnMut(&Ident, ImportedTypeKind),
94+
{
95+
self.kind.imported_types(f)
96+
}
97+
}
98+
99+
impl ImportedTypes for ast::ImportKind {
100+
fn imported_types<F>(&self, f: &mut F)
101+
where
102+
F: FnMut(&Ident, ImportedTypeKind),
103+
{
104+
match self {
105+
ast::ImportKind::Static(s) => s.imported_types(f),
106+
ast::ImportKind::Function(fun) => fun.imported_types(f),
107+
ast::ImportKind::Type(ty) => ty.imported_types(f),
108+
}
109+
}
110+
}
111+
112+
impl ImportedTypes for ast::ImportStatic {
113+
fn imported_types<F>(&self, f: &mut F)
114+
where
115+
F: FnMut(&Ident, ImportedTypeKind),
116+
{
117+
self.ty.imported_types(f);
118+
}
119+
}
120+
121+
impl ImportedTypes for syn::Type {
122+
fn imported_types<F>(&self, f: &mut F)
123+
where
124+
F: FnMut(&Ident, ImportedTypeKind),
125+
{
126+
match self {
127+
syn::Type::Reference(ref r) => r.imported_types(f),
128+
syn::Type::Path(ref p) => p.imported_types(f),
129+
_ => {}
130+
}
131+
}
132+
}
133+
134+
impl ImportedTypes for syn::TypeReference {
135+
fn imported_types<F>(&self, f: &mut F)
136+
where
137+
F: FnMut(&Ident, ImportedTypeKind),
138+
{
139+
self.elem.imported_types(f);
140+
}
141+
}
142+
143+
impl ImportedTypes for syn::TypePath {
144+
fn imported_types<F>(&self, f: &mut F)
145+
where
146+
F: FnMut(&Ident, ImportedTypeKind),
147+
{
148+
if self.qself.is_some()
149+
|| self.path.leading_colon.is_some()
150+
|| self.path.segments.len() != 1
151+
{
152+
return;
153+
}
154+
f(
155+
&self.path.segments.last().unwrap().value().ident,
156+
ImportedTypeKind::Reference,
157+
);
158+
}
159+
}
160+
161+
impl ImportedTypes for ast::ImportFunction {
162+
fn imported_types<F>(&self, f: &mut F)
163+
where
164+
F: FnMut(&Ident, ImportedTypeKind),
165+
{
166+
self.function.imported_types(f);
167+
self.kind.imported_types(f);
168+
}
169+
}
170+
171+
impl ImportedTypes for ast::ImportFunctionKind {
172+
fn imported_types<F>(&self, f: &mut F)
173+
where
174+
F: FnMut(&Ident, ImportedTypeKind),
175+
{
176+
match self {
177+
ast::ImportFunctionKind::Method { ty, .. } => ty.imported_types(f),
178+
ast::ImportFunctionKind::Normal => {}
179+
}
180+
}
181+
}
182+
183+
impl ImportedTypes for ast::Function {
184+
fn imported_types<F>(&self, f: &mut F)
185+
where
186+
F: FnMut(&Ident, ImportedTypeKind),
187+
{
188+
self.arguments.imported_types(f);
189+
if let Some(ref r) = self.ret {
190+
r.imported_types(f);
191+
}
192+
}
193+
}
194+
195+
impl ImportedTypes for syn::ArgCaptured {
196+
fn imported_types<F>(&self, f: &mut F)
197+
where
198+
F: FnMut(&Ident, ImportedTypeKind),
199+
{
200+
self.ty.imported_types(f);
201+
}
202+
}
203+
204+
impl ImportedTypes for ast::ImportType {
205+
fn imported_types<F>(&self, f: &mut F)
206+
where
207+
F: FnMut(&Ident, ImportedTypeKind),
208+
{
209+
f(&self.name, ImportedTypeKind::Definition);
210+
}
211+
}
212+
213+
impl ImportedTypes for ast::TypeAlias {
214+
fn imported_types<F>(&self, f: &mut F)
215+
where
216+
F: FnMut(&Ident, ImportedTypeKind),
217+
{
218+
f(&self.dest, ImportedTypeKind::Reference);
219+
}
220+
}
221+
222+
/// Remove any methods, statics, &c, that reference types that are *not*
223+
/// defined.
224+
pub trait RemoveUndefinedImports {
225+
fn remove_undefined_imports<F>(&mut self, is_defined: &F)
226+
where
227+
F: Fn(&Ident) -> bool;
228+
}
229+
230+
impl RemoveUndefinedImports for ast::Program {
231+
fn remove_undefined_imports<F>(&mut self, is_defined: &F)
232+
where
233+
F: Fn(&Ident) -> bool,
234+
{
235+
self.imports.remove_undefined_imports(is_defined);
236+
self.type_aliases.remove_undefined_imports(is_defined);
237+
}
238+
}
239+
240+
impl<T> RemoveUndefinedImports for Vec<T>
241+
where
242+
T: ImportedTypeReferences,
243+
{
244+
fn remove_undefined_imports<F>(&mut self, is_defined: &F)
245+
where
246+
F: Fn(&Ident) -> bool,
247+
{
248+
self.retain(|x| {
249+
let mut all_defined = true;
250+
x.imported_type_references(&mut |id| {
251+
all_defined = all_defined && is_defined(id);
252+
});
253+
all_defined
254+
});
255+
}
256+
}

crates/backend/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ extern crate wasm_bindgen_shared as shared;
1111

1212
pub mod ast;
1313
mod codegen;
14+
pub mod defined;
1415
pub mod util;

crates/cli-support/src/js/mod.rs

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,40 @@ impl<'a> Context<'a> {
284284
))
285285
})?;
286286

287+
self.bind("__wbindgen_is_object", &|me| {
288+
me.expose_get_object();
289+
Ok(String::from(
290+
"
291+
function(i) {
292+
const val = getObject(i);
293+
return typeof(val) === 'object' && val !== null ? 1 : 0;
294+
}
295+
",
296+
))
297+
})?;
298+
299+
self.bind("__wbindgen_is_function", &|me| {
300+
me.expose_get_object();
301+
Ok(String::from(
302+
"
303+
function(i) {
304+
return typeof(getObject(i)) === 'function' ? 1 : 0;
305+
}
306+
",
307+
))
308+
})?;
309+
310+
self.bind("__wbindgen_is_string", &|me| {
311+
me.expose_get_object();
312+
Ok(String::from(
313+
"
314+
function(i) {
315+
return typeof(getObject(i)) === 'string' ? 1 : 0;
316+
}
317+
",
318+
))
319+
})?;
320+
287321
self.bind("__wbindgen_string_get", &|me| {
288322
me.expose_pass_string_to_wasm()?;
289323
me.expose_get_object();
@@ -1501,7 +1535,7 @@ impl<'a> Context<'a> {
15011535
if (desc) return desc;
15021536
obj = Object.getPrototypeOf(obj);
15031537
}
1504-
throw new Error('descriptor not found');
1538+
throw new Error(`descriptor for id='${id}' not found`);
15051539
}
15061540
",
15071541
);
@@ -1866,11 +1900,24 @@ impl<'a, 'b> SubContext<'a, 'b> {
18661900
}
18671901
};
18681902

1903+
let fallback = if import.structural {
1904+
"".to_string()
1905+
} else {
1906+
format!(
1907+
" || function() {{
1908+
throw new Error(`wasm-bindgen: {} does not exist`);
1909+
}}",
1910+
target
1911+
)
1912+
};
1913+
18691914
self.cx.global(&format!(
18701915
"
1871-
const {}_target = {};
1916+
const {}_target = {} {} ;
18721917
",
1873-
import.shim, target
1918+
import.shim,
1919+
target,
1920+
fallback
18741921
));
18751922
format!(
18761923
"{}_target{}",

0 commit comments

Comments
 (0)