Skip to content

Commit a22bbca

Browse files
authored
Making WebIDL generation deterministic (#2101)
* Making webidl generation deterministic * Fixing line endings * Regenerating WebIDL * Running rustfmt
1 parent 7bc9147 commit a22bbca

File tree

3 files changed

+32
-17
lines changed

3 files changed

+32
-17
lines changed

crates/web-sys/src/features/gen_Navigator.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,18 +105,6 @@ extern "C" {
105105
#[doc = "*This API requires the following crate features to be activated: `CredentialsContainer`, `Navigator`*"]
106106
pub fn credentials(this: &Navigator) -> CredentialsContainer;
107107
#[cfg(web_sys_unstable_apis)]
108-
#[cfg(feature = "Xr")]
109-
# [ wasm_bindgen ( structural , method , getter , js_class = "Navigator" , js_name = xr ) ]
110-
#[doc = "Getter for the `xr` field of this object."]
111-
#[doc = ""]
112-
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/Navigator/xr)"]
113-
#[doc = ""]
114-
#[doc = "*This API requires the following crate features to be activated: `Navigator`, `Xr`*"]
115-
#[doc = ""]
116-
#[doc = "*This API is unstable and requires `--cfg=web_sys_unstable_apis` to be activated, as"]
117-
#[doc = "[described in the `wasm-bindgen` guide](https://rustwasm.github.io/docs/wasm-bindgen/web-sys/unstable-apis.html)*"]
118-
pub fn xr(this: &Navigator) -> Xr;
119-
#[cfg(web_sys_unstable_apis)]
120108
#[cfg(feature = "Gpu")]
121109
# [ wasm_bindgen ( structural , method , getter , js_class = "Navigator" , js_name = gpu ) ]
122110
#[doc = "Getter for the `gpu` field of this object."]
@@ -128,6 +116,18 @@ extern "C" {
128116
#[doc = "*This API is unstable and requires `--cfg=web_sys_unstable_apis` to be activated, as"]
129117
#[doc = "[described in the `wasm-bindgen` guide](https://rustwasm.github.io/docs/wasm-bindgen/web-sys/unstable-apis.html)*"]
130118
pub fn gpu(this: &Navigator) -> Gpu;
119+
#[cfg(web_sys_unstable_apis)]
120+
#[cfg(feature = "Xr")]
121+
# [ wasm_bindgen ( structural , method , getter , js_class = "Navigator" , js_name = xr ) ]
122+
#[doc = "Getter for the `xr` field of this object."]
123+
#[doc = ""]
124+
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/Navigator/xr)"]
125+
#[doc = ""]
126+
#[doc = "*This API requires the following crate features to be activated: `Navigator`, `Xr`*"]
127+
#[doc = ""]
128+
#[doc = "*This API is unstable and requires `--cfg=web_sys_unstable_apis` to be activated, as"]
129+
#[doc = "[described in the `wasm-bindgen` guide](https://rustwasm.github.io/docs/wasm-bindgen/web-sys/unstable-apis.html)*"]
130+
pub fn xr(this: &Navigator) -> Xr;
131131
# [ wasm_bindgen ( structural , method , getter , js_class = "Navigator" , js_name = hardwareConcurrency ) ]
132132
#[doc = "Getter for the `hardwareConcurrency` field of this object."]
133133
#[doc = ""]

crates/webidl/src/lib.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::generator::{
2525
use crate::idl_type::ToIdlType;
2626
use crate::traverse::TraverseType;
2727
use crate::util::{
28-
camel_case_ident, is_structural, shouty_snake_case_ident, snake_case_ident, throws,
28+
camel_case_ident, is_structural, read_dir, shouty_snake_case_ident, snake_case_ident, throws,
2929
webidl_const_v_to_backend_const_v, TypePosition,
3030
};
3131
use anyhow::Context;
@@ -749,11 +749,9 @@ pub fn generate(from: &Path, to: &Path, options: Options) -> Result<String> {
749749

750750
/// Read all WebIDL files in a directory into a single `SourceFile`
751751
fn read_source_from_path(dir: &Path) -> Result<SourceFile> {
752-
let entries = fs::read_dir(dir).context("reading webidls directory")?;
752+
let entries = read_dir(dir).context("reading webidls directory")?;
753753
let mut source = SourceFile::default();
754-
for entry in entries {
755-
let entry = entry.context(format!("getting {}/*.webidl entry", dir.display()))?;
756-
let path = entry.path();
754+
for path in entries {
757755
if path.extension() != Some(OsStr::new("webidl")) {
758756
continue;
759757
}

crates/webidl/src/util.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use std::collections::BTreeSet;
2+
use std::fs;
23
use std::iter::FromIterator;
4+
use std::path::{Path, PathBuf};
35
use std::ptr;
46

57
use heck::{CamelCase, ShoutySnakeCase, SnakeCase};
@@ -23,6 +25,21 @@ use crate::Options;
2325
/// in their names, where `n` is this constant.
2426
const MAX_VARIADIC_ARGUMENTS_COUNT: usize = 7;
2527

28+
/// Similar to std::fs::read_dir except it returns a sorted Vec,
29+
/// which is important to make the code generation deterministic.
30+
pub(crate) fn read_dir<P>(path: P) -> std::io::Result<Vec<PathBuf>>
31+
where
32+
P: AsRef<Path>,
33+
{
34+
let mut entries = fs::read_dir(path)?
35+
.map(|entry| Ok(entry?.path()))
36+
.collect::<std::io::Result<Vec<_>>>()?;
37+
38+
entries.sort();
39+
40+
Ok(entries)
41+
}
42+
2643
/// Take a type and create an immutable shared reference to that type.
2744
pub(crate) fn shared_ref(ty: syn::Type, mutable: bool) -> syn::Type {
2845
syn::TypeReference {

0 commit comments

Comments
 (0)