mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-07-31 03:51:56 +00:00
Migrate from the webidl
crate to weedle
This commit migrates the `wasm-bindgen-webidl` crate from the `webidl` parser to
`weedle`. The main rationale for doing this is that `webidl` depends on
`lalrpop`, which is quite a large dependency and takes a good deal of time to
compile. The `weedle` crate, however, depends on `nom` and is much faster to
compile.
Almost all translations were pretty straightforward. Some abstractions changed
and/or were introduced in this commit when moving to `weedle` like the
`ToSynType` trait, but otherwise the generated bindings should be the same. It's
been verified that the `weedle`-generated bindings are exactly the same as the
`webidl`-generated bindings, with the one exception of `weedle` generates one
more method, `WebGpuCommandEncoder::transition_buffer`. It's not clear currently
why `webidl` didn't generate this method, as its [idl] is pretty straightforward!
This commit is using a [fork] of `weedle` currently which has a number of fixes
for parsing our WebIDL, although all the fixes are quite minor!
Closes #620
[idl]: d66b834afd/crates/web-sys/webidls/enabled/WebGPU.webidl (L499)
[fork]: https://github.com/alexcrichton/weedle/tree/fix-for-web-sys
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
extern crate env_logger;
|
||||
#[macro_use]
|
||||
extern crate failure;
|
||||
extern crate wasm_bindgen_webidl;
|
||||
extern crate sourcefile;
|
||||
@@ -8,9 +9,8 @@ use sourcefile::SourceFile;
|
||||
use std::env;
|
||||
use std::ffi::OsStr;
|
||||
use std::fs;
|
||||
use std::io::Write;
|
||||
use std::path;
|
||||
use std::process;
|
||||
use std::process::{self, Command};
|
||||
|
||||
fn main() {
|
||||
if let Err(e) = try_main() {
|
||||
@@ -32,12 +32,14 @@ fn try_main() -> Result<(), failure::Error> {
|
||||
let mut source = SourceFile::default();
|
||||
for entry in entries {
|
||||
let entry = entry.context("getting webidls/enabled/*.webidl entry")?;
|
||||
if entry.path().extension() == Some(OsStr::new("webidl")) {
|
||||
println!("cargo:rerun-if-changed={}", entry.path().display());
|
||||
source = source.add_file(entry.path())
|
||||
.with_context(|_| format!("reading contents of file \"{}\"",
|
||||
entry.path().display()))?;
|
||||
let path = entry.path();
|
||||
if path.extension() != Some(OsStr::new("webidl")) {
|
||||
continue
|
||||
}
|
||||
println!("cargo:rerun-if-changed={}", path.display());
|
||||
source = source.add_file(&path)
|
||||
.with_context(|_| format!("reading contents of file \"{}\"",
|
||||
path.display()))?;
|
||||
}
|
||||
|
||||
let bindings = match wasm_bindgen_webidl::compile(&source.contents) {
|
||||
@@ -60,17 +62,19 @@ fn try_main() -> Result<(), failure::Error> {
|
||||
|
||||
let out_dir = env::var("OUT_DIR").context("reading OUT_DIR environment variable")?;
|
||||
let out_file_path = path::Path::new(&out_dir).join("bindings.rs");
|
||||
let mut out_file = fs::File::create(&out_file_path)
|
||||
.context("creating output bindings file")?;
|
||||
out_file
|
||||
.write_all(bindings.as_bytes())
|
||||
fs::write(&out_file_path, bindings)
|
||||
.context("writing bindings to output file")?;
|
||||
|
||||
// run rustfmt on the generated file - really handy for debugging
|
||||
//if ! process::Command::new("rustfmt").arg(&out_file_path).status()
|
||||
// .context("running rustfmt")?.success() {
|
||||
// return Err(format_err!("rustfmt failed to format {}", out_file_path.display()));
|
||||
//}
|
||||
if env::var("WEBIDL_RUSTFMT_BINDINGS").is_ok() {
|
||||
let status = Command::new("rustfmt")
|
||||
.arg(&out_file_path)
|
||||
.status()
|
||||
.context("running rustfmt")?;
|
||||
if !status.success() {
|
||||
bail!("rustfmt failed: {}", status)
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
Reference in New Issue
Block a user