Skip to content

Commit 6741e53

Browse files
committed
Adding in main bin to wasm-bindgen-webidl
1 parent dd030b3 commit 6741e53

File tree

6 files changed

+111
-117
lines changed

6 files changed

+111
-117
lines changed

Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ wasm-bindgen-test-crate-b = { path = 'tests/crates/b', version = '0.1' }
5252
[workspace]
5353
members = [
5454
"benchmarks",
55-
"bin/build-web-sys",
5655
"crates/cli",
5756
"crates/js-sys",
5857
"crates/test",

bin/build-web-sys/Cargo.toml

-13
This file was deleted.

bin/build-web-sys/src/main.rs

-102
This file was deleted.

crates/web-sys/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ js-sys = { path = '../js-sys', version = '0.3.35' }
2727
wasm-bindgen-test = { path = '../test', version = '0.3.8' }
2828
wasm-bindgen-futures = { path = '../futures', version = '0.4.8' }
2929

30-
# This list is auto-generated by the build-web-sys script
30+
# This list is auto-generated by the wasm-bindgen-webidl program
3131
[features]
3232
AbortController = []
3333
AbortSignal = ["EventTarget"]

crates/webidl/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Support for parsing WebIDL specific to wasm-bindgen
1313
edition = "2018"
1414

1515
[dependencies]
16+
env_logger = { version = "0.7.0", optional = true }
1617
anyhow = "1.0"
1718
heck = "0.3"
1819
log = "0.4.1"
@@ -22,3 +23,4 @@ syn = { version = '1.0', features = ['full'] }
2223
wasm-bindgen-backend = { version = "=0.2.58", path = "../backend" }
2324
weedle = "0.10"
2425
lazy_static = "1.0.0"
26+
sourcefile = "0.1"

crates/webidl/src/main.rs

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
use anyhow::{Context, Result};
2+
use sourcefile::SourceFile;
3+
use std::ffi::OsStr;
4+
use std::fs;
5+
use std::path;
6+
use std::process::Command;
7+
8+
fn unwrap_not_found(err: std::io::Result<()>) -> std::io::Result<()> {
9+
match err {
10+
Ok(()) => Ok(()),
11+
Err(err) => match err.kind() {
12+
std::io::ErrorKind::NotFound => Ok(()),
13+
_ => Err(err),
14+
},
15+
}
16+
}
17+
18+
fn main() -> Result<()> {
19+
#[cfg(feature = "env_logger")]
20+
env_logger::init();
21+
22+
match std::env::args().into_iter().collect::<Vec<_>>().as_slice() {
23+
[ _, from, to ] => {
24+
let from = path::Path::new(from);
25+
let to = path::Path::new(to);
26+
27+
let entries = fs::read_dir(from.join("enabled")).context("reading enabled directory")?;
28+
29+
let mut source = SourceFile::default();
30+
31+
for entry in entries {
32+
let entry = entry.context("getting enabled/*.webidl entry")?;
33+
let path = entry.path();
34+
if path.extension() != Some(OsStr::new("webidl")) {
35+
continue;
36+
}
37+
source = source
38+
.add_file(&path)
39+
.with_context(|| format!("reading contents of file \"{}\"", path.display()))?;
40+
}
41+
42+
let features = match wasm_bindgen_webidl::compile(&source.contents) {
43+
Ok(features) => features,
44+
Err(e) => {
45+
if let Some(err) = e.downcast_ref::<wasm_bindgen_webidl::WebIDLParseError>() {
46+
if let Some(pos) = source.resolve_offset(err.0) {
47+
let ctx = format!(
48+
"compiling WebIDL into wasm-bindgen bindings in file \
49+
\"{}\", line {} column {}",
50+
pos.filename,
51+
pos.line + 1,
52+
pos.col + 1
53+
);
54+
return Err(e.context(ctx));
55+
} else {
56+
return Err(e.context("compiling WebIDL into wasm-bindgen bindings"));
57+
}
58+
}
59+
return Err(e.context("compiling WebIDL into wasm-bindgen bindings"));
60+
}
61+
};
62+
63+
64+
unwrap_not_found(fs::remove_dir_all(&to)).context("Removing features directory")?;
65+
fs::create_dir_all(&to).context("Creating features directory")?;
66+
67+
68+
for (name, feature) in features.iter() {
69+
let out_file_path = to.join(format!("gen_{}.rs", name));
70+
71+
fs::write(&out_file_path, &feature.code)?;
72+
73+
// run rustfmt on the generated file - really handy for debugging
74+
let result = Command::new("rustfmt")
75+
.arg("--edition")
76+
.arg("2018")
77+
.arg(&out_file_path)
78+
.status()
79+
.context(format!("rustfmt on file gen_{}.rs", name))?;
80+
81+
assert!(result.success(), "rustfmt on file gen_{}.rs", name);
82+
}
83+
84+
85+
let binding_file = features.keys().map(|name| {
86+
format!("#[cfg(feature = \"{name}\")] mod gen_{name};\n#[cfg(feature = \"{name}\")] pub use gen_{name}::*;", name = name)
87+
}).collect::<Vec<_>>().join("\n\n");
88+
89+
fs::write(to.join("mod.rs"), format!("#![allow(non_snake_case)]\n\n{}", binding_file))?;
90+
91+
92+
let features = features.iter().map(|(name, feature)| {
93+
let features = feature.required_features.iter()
94+
.map(|x| format!("\"{}\"", x))
95+
.collect::<Vec<_>>()
96+
.join(", ");
97+
format!("{} = [{}]", name, features)
98+
}).collect::<Vec<_>>().join("\n");
99+
100+
fs::write(&"features", features).context("writing features to output file")?;
101+
},
102+
args => {
103+
panic!("Need 2 arguments {:?}", args);
104+
},
105+
}
106+
107+
Ok(())
108+
}

0 commit comments

Comments
 (0)