Making WebIDL generation deterministic (#2101)

* Making webidl generation deterministic

* Fixing line endings

* Regenerating WebIDL

* Running rustfmt
This commit is contained in:
Pauan 2020-04-23 03:01:40 +02:00 committed by GitHub
parent 7bc9147258
commit a22bbca92c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 17 deletions

View File

@ -105,18 +105,6 @@ extern "C" {
#[doc = "*This API requires the following crate features to be activated: `CredentialsContainer`, `Navigator`*"] #[doc = "*This API requires the following crate features to be activated: `CredentialsContainer`, `Navigator`*"]
pub fn credentials(this: &Navigator) -> CredentialsContainer; pub fn credentials(this: &Navigator) -> CredentialsContainer;
#[cfg(web_sys_unstable_apis)] #[cfg(web_sys_unstable_apis)]
#[cfg(feature = "Xr")]
# [ wasm_bindgen ( structural , method , getter , js_class = "Navigator" , js_name = xr ) ]
#[doc = "Getter for the `xr` field of this object."]
#[doc = ""]
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/Navigator/xr)"]
#[doc = ""]
#[doc = "*This API requires the following crate features to be activated: `Navigator`, `Xr`*"]
#[doc = ""]
#[doc = "*This API is unstable and requires `--cfg=web_sys_unstable_apis` to be activated, as"]
#[doc = "[described in the `wasm-bindgen` guide](https://rustwasm.github.io/docs/wasm-bindgen/web-sys/unstable-apis.html)*"]
pub fn xr(this: &Navigator) -> Xr;
#[cfg(web_sys_unstable_apis)]
#[cfg(feature = "Gpu")] #[cfg(feature = "Gpu")]
# [ wasm_bindgen ( structural , method , getter , js_class = "Navigator" , js_name = gpu ) ] # [ wasm_bindgen ( structural , method , getter , js_class = "Navigator" , js_name = gpu ) ]
#[doc = "Getter for the `gpu` field of this object."] #[doc = "Getter for the `gpu` field of this object."]
@ -128,6 +116,18 @@ extern "C" {
#[doc = "*This API is unstable and requires `--cfg=web_sys_unstable_apis` to be activated, as"] #[doc = "*This API is unstable and requires `--cfg=web_sys_unstable_apis` to be activated, as"]
#[doc = "[described in the `wasm-bindgen` guide](https://rustwasm.github.io/docs/wasm-bindgen/web-sys/unstable-apis.html)*"] #[doc = "[described in the `wasm-bindgen` guide](https://rustwasm.github.io/docs/wasm-bindgen/web-sys/unstable-apis.html)*"]
pub fn gpu(this: &Navigator) -> Gpu; pub fn gpu(this: &Navigator) -> Gpu;
#[cfg(web_sys_unstable_apis)]
#[cfg(feature = "Xr")]
# [ wasm_bindgen ( structural , method , getter , js_class = "Navigator" , js_name = xr ) ]
#[doc = "Getter for the `xr` field of this object."]
#[doc = ""]
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/Navigator/xr)"]
#[doc = ""]
#[doc = "*This API requires the following crate features to be activated: `Navigator`, `Xr`*"]
#[doc = ""]
#[doc = "*This API is unstable and requires `--cfg=web_sys_unstable_apis` to be activated, as"]
#[doc = "[described in the `wasm-bindgen` guide](https://rustwasm.github.io/docs/wasm-bindgen/web-sys/unstable-apis.html)*"]
pub fn xr(this: &Navigator) -> Xr;
# [ wasm_bindgen ( structural , method , getter , js_class = "Navigator" , js_name = hardwareConcurrency ) ] # [ wasm_bindgen ( structural , method , getter , js_class = "Navigator" , js_name = hardwareConcurrency ) ]
#[doc = "Getter for the `hardwareConcurrency` field of this object."] #[doc = "Getter for the `hardwareConcurrency` field of this object."]
#[doc = ""] #[doc = ""]

View File

@ -25,7 +25,7 @@ use crate::generator::{
use crate::idl_type::ToIdlType; use crate::idl_type::ToIdlType;
use crate::traverse::TraverseType; use crate::traverse::TraverseType;
use crate::util::{ use crate::util::{
camel_case_ident, is_structural, shouty_snake_case_ident, snake_case_ident, throws, camel_case_ident, is_structural, read_dir, shouty_snake_case_ident, snake_case_ident, throws,
webidl_const_v_to_backend_const_v, TypePosition, webidl_const_v_to_backend_const_v, TypePosition,
}; };
use anyhow::Context; use anyhow::Context;
@ -749,11 +749,9 @@ pub fn generate(from: &Path, to: &Path, options: Options) -> Result<String> {
/// Read all WebIDL files in a directory into a single `SourceFile` /// Read all WebIDL files in a directory into a single `SourceFile`
fn read_source_from_path(dir: &Path) -> Result<SourceFile> { fn read_source_from_path(dir: &Path) -> Result<SourceFile> {
let entries = fs::read_dir(dir).context("reading webidls directory")?; let entries = read_dir(dir).context("reading webidls directory")?;
let mut source = SourceFile::default(); let mut source = SourceFile::default();
for entry in entries { for path in entries {
let entry = entry.context(format!("getting {}/*.webidl entry", dir.display()))?;
let path = entry.path();
if path.extension() != Some(OsStr::new("webidl")) { if path.extension() != Some(OsStr::new("webidl")) {
continue; continue;
} }

View File

@ -1,5 +1,7 @@
use std::collections::BTreeSet; use std::collections::BTreeSet;
use std::fs;
use std::iter::FromIterator; use std::iter::FromIterator;
use std::path::{Path, PathBuf};
use std::ptr; use std::ptr;
use heck::{CamelCase, ShoutySnakeCase, SnakeCase}; use heck::{CamelCase, ShoutySnakeCase, SnakeCase};
@ -23,6 +25,21 @@ use crate::Options;
/// in their names, where `n` is this constant. /// in their names, where `n` is this constant.
const MAX_VARIADIC_ARGUMENTS_COUNT: usize = 7; const MAX_VARIADIC_ARGUMENTS_COUNT: usize = 7;
/// Similar to std::fs::read_dir except it returns a sorted Vec,
/// which is important to make the code generation deterministic.
pub(crate) fn read_dir<P>(path: P) -> std::io::Result<Vec<PathBuf>>
where
P: AsRef<Path>,
{
let mut entries = fs::read_dir(path)?
.map(|entry| Ok(entry?.path()))
.collect::<std::io::Result<Vec<_>>>()?;
entries.sort();
Ok(entries)
}
/// Take a type and create an immutable shared reference to that type. /// Take a type and create an immutable shared reference to that type.
pub(crate) fn shared_ref(ty: syn::Type, mutable: bool) -> syn::Type { pub(crate) fn shared_ref(ty: syn::Type, mutable: bool) -> syn::Type {
syn::TypeReference { syn::TypeReference {