Skip to content

Commit 696ef66

Browse files
committed
Use separate directory for unstable WebIDL
1 parent fc50850 commit 696ef66

File tree

11 files changed

+332
-329
lines changed

11 files changed

+332
-329
lines changed

crates/backend/src/ast.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ pub struct Export {
5151
/// Whether or not this function should be flagged as the wasm start
5252
/// function.
5353
pub start: bool,
54+
/// Whether the API is unstable. This is only used internally.
55+
pub unstable_api: bool,
5456
}
5557

5658
/// The 3 types variations of `self`.
@@ -71,6 +73,7 @@ pub struct Import {
7173
pub module: ImportModule,
7274
pub js_namespace: Option<Ident>,
7375
pub kind: ImportKind,
76+
pub unstable_api: bool,
7477
}
7578

7679
#[cfg_attr(feature = "extra-traits", derive(Debug))]

crates/backend/src/codegen.rs

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ impl TryToTokens for ast::Program {
3939
}
4040
}
4141
for i in self.imports.iter() {
42-
DescribeImport(&i.kind).to_tokens(tokens);
42+
DescribeImport {
43+
kind: &i.kind,
44+
unstable_api: i.unstable_api,
45+
}.to_tokens(tokens);
4346

4447
// If there is a js namespace, check that name isn't a type. If it is,
4548
// this import might be a method on that type.
@@ -296,12 +299,13 @@ impl ToTokens for ast::StructField {
296299
})
297300
.to_tokens(tokens);
298301

299-
Descriptor(
300-
&getter,
301-
quote! {
302+
Descriptor {
303+
ident: &getter,
304+
inner: quote! {
302305
<#ty as WasmDescribe>::describe();
303306
},
304-
)
307+
unstable_api: self.unstable_api,
308+
}
305309
.to_tokens(tokens);
306310

307311
if self.readonly {
@@ -528,16 +532,17 @@ impl TryToTokens for ast::Export {
528532
// this, but the tl;dr; is that this is stripped from the final wasm
529533
// binary along with anything it references.
530534
let export = Ident::new(&export_name, Span::call_site());
531-
Descriptor(
532-
&export,
533-
quote! {
535+
Descriptor {
536+
ident: &export,
537+
inner: quote! {
534538
inform(FUNCTION);
535539
inform(0);
536540
inform(#nargs);
537541
#(<#argtys as WasmDescribe>::describe();)*
538542
#describe_ret
539543
},
540-
)
544+
unstable_api: self.unstable_api,
545+
}
541546
.to_tokens(into);
542547

543548
Ok(())
@@ -1073,11 +1078,14 @@ impl TryToTokens for ast::ImportFunction {
10731078
}
10741079

10751080
// See comment above in ast::Export for what's going on here.
1076-
struct DescribeImport<'a>(&'a ast::ImportKind);
1081+
struct DescribeImport<'a> {
1082+
kind: &'a ast::ImportKind,
1083+
unstable_api: bool,
1084+
}
10771085

10781086
impl<'a> ToTokens for DescribeImport<'a> {
10791087
fn to_tokens(&self, tokens: &mut TokenStream) {
1080-
let f = match *self.0 {
1088+
let f = match *self.kind {
10811089
ast::ImportKind::Function(ref f) => f,
10821090
ast::ImportKind::Static(_) => return,
10831091
ast::ImportKind::Type(_) => return,
@@ -1090,16 +1098,17 @@ impl<'a> ToTokens for DescribeImport<'a> {
10901098
None => quote! { <() as WasmDescribe>::describe(); },
10911099
};
10921100

1093-
Descriptor(
1094-
&f.shim,
1095-
quote! {
1101+
Descriptor {
1102+
ident: &f.shim,
1103+
inner: quote! {
10961104
inform(FUNCTION);
10971105
inform(0);
10981106
inform(#nargs);
10991107
#(<#argtys as WasmDescribe>::describe();)*
11001108
#inform_ret
11011109
},
1102-
)
1110+
unstable_api: self.unstable_api,
1111+
}
11031112
.to_tokens(tokens);
11041113
}
11051114
}
@@ -1203,12 +1212,13 @@ impl ToTokens for ast::ImportStatic {
12031212
})
12041213
.to_tokens(into);
12051214

1206-
Descriptor(
1207-
&shim_name,
1208-
quote! {
1215+
Descriptor {
1216+
ident: &shim_name,
1217+
inner: quote! {
12091218
<#ty as WasmDescribe>::describe();
12101219
},
1211-
)
1220+
unstable_api: false,
1221+
}
12121222
.to_tokens(into);
12131223
}
12141224
}
@@ -1456,7 +1466,11 @@ impl ToTokens for ast::DictionaryField {
14561466

14571467
/// Emits the necessary glue tokens for "descriptor", generating an appropriate
14581468
/// symbol name as well as attributes around the descriptor function itself.
1459-
struct Descriptor<'a, T>(&'a Ident, T);
1469+
struct Descriptor<'a, T> {
1470+
ident: &'a Ident,
1471+
inner: T,
1472+
unstable_api: bool,
1473+
}
14601474

14611475
impl<'a, T: ToTokens> ToTokens for Descriptor<'a, T> {
14621476
fn to_tokens(&self, tokens: &mut TokenStream) {
@@ -1471,22 +1485,27 @@ impl<'a, T: ToTokens> ToTokens for Descriptor<'a, T> {
14711485
lazy_static::lazy_static! {
14721486
static ref DESCRIPTORS_EMITTED: Mutex<HashSet<String>> = Default::default();
14731487
}
1488+
1489+
let ident = self.ident;
1490+
14741491
if !DESCRIPTORS_EMITTED
14751492
.lock()
14761493
.unwrap()
1477-
.insert(self.0.to_string())
1494+
.insert(ident.to_string())
14781495
{
14791496
return;
14801497
}
14811498

1482-
let name = Ident::new(&format!("__wbindgen_describe_{}", self.0), self.0.span());
1483-
let inner = &self.1;
1499+
let name = Ident::new(&format!("__wbindgen_describe_{}", ident), ident.span());
1500+
let unstable_api_attr = util::maybe_unstable_api_attr(self.unstable_api);
1501+
let inner = &self.inner;
14841502
(quote! {
14851503
#[no_mangle]
14861504
#[allow(non_snake_case)]
14871505
#[doc(hidden)]
14881506
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
14891507
#[allow(clippy::all)]
1508+
#unstable_api_attr
14901509
pub extern "C" fn #name() {
14911510
use wasm_bindgen::describe::*;
14921511
// See definition of `link_mem_intrinsics` for what this is doing

crates/backend/src/encode.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ fn shared_export<'a>(
188188
function: shared_function(&export.function, intern),
189189
method_kind,
190190
start: export.start,
191+
unstable_api: export.unstable_api,
191192
})
192193
}
193194

crates/backend/src/util.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,12 @@ pub fn ident_ty(ident: Ident) -> syn::Type {
113113
}
114114

115115
pub fn wrap_import_function(function: ast::ImportFunction) -> ast::Import {
116+
let unstable_api = function.unstable_api;
116117
ast::Import {
117118
module: ast::ImportModule::None,
118119
js_namespace: None,
119120
kind: ast::ImportKind::Function(function),
121+
unstable_api,
120122
}
121123
}
122124

crates/macro-support/src/parser.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,7 @@ impl<'a> MacroParse<(Option<BindgenAttrs>, &'a mut TokenStream)> for syn::Item {
780780
rust_class: None,
781781
rust_name,
782782
start,
783+
unstable_api: false,
783784
});
784785
}
785786
syn::Item::Struct(mut s) => {
@@ -980,6 +981,7 @@ impl<'a, 'b> MacroParse<(&'a Ident, &'a str)> for &'b mut syn::ImplItemMethod {
980981
rust_class: Some(class.clone()),
981982
rust_name: self.sig.ident.clone(),
982983
start: false,
984+
unstable_api: false,
983985
});
984986
opts.check_used()?;
985987
Ok(())
@@ -1179,6 +1181,7 @@ impl MacroParse<ast::ImportModule> for syn::ForeignItem {
11791181
module,
11801182
js_namespace,
11811183
kind,
1184+
unstable_api: false,
11821185
});
11831186

11841187
Ok(())

crates/shared/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ macro_rules! shared_api {
9494
function: Function<'a>,
9595
method_kind: MethodKind<'a>,
9696
start: bool,
97+
unstable_api: bool,
9798
}
9899

99100
struct Enum<'a> {

crates/web-sys/build.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,12 @@ use std::fs;
77
use std::path::{self, PathBuf};
88
use std::process::Command;
99

10-
fn main() -> Result<()> {
11-
#[cfg(feature = "env_logger")]
12-
env_logger::init();
13-
println!("cargo:rerun-if-changed=build.rs");
14-
println!("cargo:rerun-if-changed=webidls/enabled");
15-
16-
let entries = fs::read_dir("webidls/enabled").context("reading webidls/enabled directory")?;
10+
/// Read all WebIDL files in a directory into a single `SourceFile`
11+
fn read_source_from_path(dir: &str) -> Result<SourceFile> {
12+
let entries = fs::read_dir(dir).context("reading webidls/enabled directory")?;
1713
let mut source = SourceFile::default();
1814
for entry in entries {
19-
let entry = entry.context("getting webidls/enabled/*.webidl entry")?;
15+
let entry = entry.context(format!("getting {}/*.webidl entry", dir))?;
2016
let path = entry.path();
2117
if path.extension() != Some(OsStr::new("webidl")) {
2218
continue;
@@ -27,6 +23,16 @@ fn main() -> Result<()> {
2723
.with_context(|| format!("reading contents of file \"{}\"", path.display()))?;
2824
}
2925

26+
Ok(source)
27+
}
28+
29+
fn main() -> Result<()> {
30+
#[cfg(feature = "env_logger")]
31+
env_logger::init();
32+
println!("cargo:rerun-if-changed=build.rs");
33+
println!("cargo:rerun-if-changed=webidls/enabled");
34+
println!("cargo:rerun-if-changed=webidls/unstable");
35+
3036
// Read our manifest, learn all `[feature]` directives with "toml parsing".
3137
// Use all these names to match against environment variables set by Cargo
3238
// to figure out which features are activated to we can pass that down to
@@ -63,7 +69,10 @@ fn main() -> Result<()> {
6369
Some(&allowed[..])
6470
};
6571

66-
let bindings = match wasm_bindgen_webidl::compile(&source.contents, allowed) {
72+
let source = read_source_from_path("webidls/enabled")?;
73+
let unstable_source = read_source_from_path("webidls/unstable")?;
74+
75+
let bindings = match wasm_bindgen_webidl::compile(&source.contents, &unstable_source.contents, allowed) {
6776
Ok(bindings) => bindings,
6877
Err(e) => {
6978
if let Some(err) = e.downcast_ref::<wasm_bindgen_webidl::WebIDLParseError>() {

0 commit comments

Comments
 (0)