wasm-bindgen/crates/web-sys/build.rs
Alex Crichton 03eb1b1d01 Support named "special" operations in WebIDL
This commit adds support for two different features of the "special" operations
in WebIDL. First, it implements the desugaring [described by WebIDL][1] where
this:

    interface Dictionary {
      getter double getProperty(DOMString propertyName);
      setter void setProperty(DOMString propertyName, double propertyValue);
    };

becomes ...

    interface Dictionary {
      double getProperty(DOMString propertyName);
      void setProperty(DOMString propertyName, double propertyValue);

      getter double (DOMString propertyName);
      setter void (DOMString propertyName, double propertyValue);
    };

where specifically a named `getter` generates both a getter and a named
function.

Second it implements the distinction between two different types of getters in
WebIDL, described as:

> Getters and setters come in two varieties: ones that take a DOMString as a
> property name, known as named property getters and named property setters, and
> ones that take an unsigned long as a property index, known as indexed property
> getters and indexed property setters.

The name `get` is given to DOMString arguments, and the name `get_idx` is given
to index property getters.

[1]: https://heycam.github.io/webidl/#idl-special-operations
2018-08-13 17:52:34 -07:00

82 lines
2.7 KiB
Rust

extern crate env_logger;
#[macro_use]
extern crate failure;
extern crate wasm_bindgen_webidl;
extern crate sourcefile;
use failure::{Fail, ResultExt};
use sourcefile::SourceFile;
use std::env;
use std::ffi::OsStr;
use std::fs;
use std::path;
use std::process::{self, Command};
fn main() {
env_logger::init();
if let Err(e) = try_main() {
eprintln!("Error: {}", e);
for c in e.iter_causes() {
eprintln!(" caused by {}", c);
}
process::exit(1);
}
}
fn try_main() -> Result<(), failure::Error> {
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=webidls/enabled");
let entries = fs::read_dir("webidls/enabled").context("reading webidls/enabled directory")?;
let mut source = SourceFile::default();
for entry in entries {
let entry = entry.context("getting webidls/enabled/*.webidl entry")?;
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) {
Ok(bindings) => bindings,
Err(e) => match e.kind() {
wasm_bindgen_webidl::ErrorKind::ParsingWebIDLSourcePos(pos) => {
if let Some(pos) = source.resolve_offset(pos) {
let ctx = format!("compiling WebIDL into wasm-bindgen bindings in file \
\"{}\", line {} column {}", pos.filename, pos.line + 1, pos.col + 1);
return Err(e.context(ctx).into());
} else {
return Err(e.context("compiling WebIDL into wasm-bindgen bindings").into());
}
}
_ => {
return Err(e.context("compiling WebIDL into wasm-bindgen bindings").into());
}
}
};
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");
fs::write(&out_file_path, bindings)
.context("writing bindings to output file")?;
// run rustfmt on the generated file - really handy for debugging
println!("cargo:rerun-if-env-changed=WEBIDL_RUSTFMT_BINDINGS");
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(())
}