Skip to content

Commit

Permalink
Use separate directory for unstable WebIDL
Browse files Browse the repository at this point in the history
  • Loading branch information
grovesNL committed Feb 19, 2020
1 parent fc50850 commit 696ef66
Show file tree
Hide file tree
Showing 11 changed files with 332 additions and 329 deletions.
3 changes: 3 additions & 0 deletions crates/backend/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ pub struct Export {
/// Whether or not this function should be flagged as the wasm start
/// function.
pub start: bool,
/// Whether the API is unstable. This is only used internally.
pub unstable_api: bool,
}

/// The 3 types variations of `self`.
Expand All @@ -71,6 +73,7 @@ pub struct Import {
pub module: ImportModule,
pub js_namespace: Option<Ident>,
pub kind: ImportKind,
pub unstable_api: bool,
}

#[cfg_attr(feature = "extra-traits", derive(Debug))]
Expand Down
65 changes: 42 additions & 23 deletions crates/backend/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ impl TryToTokens for ast::Program {
}
}
for i in self.imports.iter() {
DescribeImport(&i.kind).to_tokens(tokens);
DescribeImport {
kind: &i.kind,
unstable_api: i.unstable_api,
}.to_tokens(tokens);

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

Descriptor(
&getter,
quote! {
Descriptor {
ident: &getter,
inner: quote! {
<#ty as WasmDescribe>::describe();
},
)
unstable_api: self.unstable_api,
}
.to_tokens(tokens);

if self.readonly {
Expand Down Expand Up @@ -528,16 +532,17 @@ impl TryToTokens for ast::Export {
// this, but the tl;dr; is that this is stripped from the final wasm
// binary along with anything it references.
let export = Ident::new(&export_name, Span::call_site());
Descriptor(
&export,
quote! {
Descriptor {
ident: &export,
inner: quote! {
inform(FUNCTION);
inform(0);
inform(#nargs);
#(<#argtys as WasmDescribe>::describe();)*
#describe_ret
},
)
unstable_api: self.unstable_api,
}
.to_tokens(into);

Ok(())
Expand Down Expand Up @@ -1073,11 +1078,14 @@ impl TryToTokens for ast::ImportFunction {
}

// See comment above in ast::Export for what's going on here.
struct DescribeImport<'a>(&'a ast::ImportKind);
struct DescribeImport<'a> {
kind: &'a ast::ImportKind,
unstable_api: bool,
}

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

Descriptor(
&f.shim,
quote! {
Descriptor {
ident: &f.shim,
inner: quote! {
inform(FUNCTION);
inform(0);
inform(#nargs);
#(<#argtys as WasmDescribe>::describe();)*
#inform_ret
},
)
unstable_api: self.unstable_api,
}
.to_tokens(tokens);
}
}
Expand Down Expand Up @@ -1203,12 +1212,13 @@ impl ToTokens for ast::ImportStatic {
})
.to_tokens(into);

Descriptor(
&shim_name,
quote! {
Descriptor {
ident: &shim_name,
inner: quote! {
<#ty as WasmDescribe>::describe();
},
)
unstable_api: false,
}
.to_tokens(into);
}
}
Expand Down Expand Up @@ -1456,7 +1466,11 @@ impl ToTokens for ast::DictionaryField {

/// Emits the necessary glue tokens for "descriptor", generating an appropriate
/// symbol name as well as attributes around the descriptor function itself.
struct Descriptor<'a, T>(&'a Ident, T);
struct Descriptor<'a, T> {
ident: &'a Ident,
inner: T,
unstable_api: bool,
}

impl<'a, T: ToTokens> ToTokens for Descriptor<'a, T> {
fn to_tokens(&self, tokens: &mut TokenStream) {
Expand All @@ -1471,22 +1485,27 @@ impl<'a, T: ToTokens> ToTokens for Descriptor<'a, T> {
lazy_static::lazy_static! {
static ref DESCRIPTORS_EMITTED: Mutex<HashSet<String>> = Default::default();
}

let ident = self.ident;

if !DESCRIPTORS_EMITTED
.lock()
.unwrap()
.insert(self.0.to_string())
.insert(ident.to_string())
{
return;
}

let name = Ident::new(&format!("__wbindgen_describe_{}", self.0), self.0.span());
let inner = &self.1;
let name = Ident::new(&format!("__wbindgen_describe_{}", ident), ident.span());
let unstable_api_attr = util::maybe_unstable_api_attr(self.unstable_api);
let inner = &self.inner;
(quote! {
#[no_mangle]
#[allow(non_snake_case)]
#[doc(hidden)]
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
#[allow(clippy::all)]
#unstable_api_attr
pub extern "C" fn #name() {
use wasm_bindgen::describe::*;
// See definition of `link_mem_intrinsics` for what this is doing
Expand Down
1 change: 1 addition & 0 deletions crates/backend/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ fn shared_export<'a>(
function: shared_function(&export.function, intern),
method_kind,
start: export.start,
unstable_api: export.unstable_api,
})
}

Expand Down
2 changes: 2 additions & 0 deletions crates/backend/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,12 @@ pub fn ident_ty(ident: Ident) -> syn::Type {
}

pub fn wrap_import_function(function: ast::ImportFunction) -> ast::Import {
let unstable_api = function.unstable_api;
ast::Import {
module: ast::ImportModule::None,
js_namespace: None,
kind: ast::ImportKind::Function(function),
unstable_api,
}
}

Expand Down
3 changes: 3 additions & 0 deletions crates/macro-support/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,7 @@ impl<'a> MacroParse<(Option<BindgenAttrs>, &'a mut TokenStream)> for syn::Item {
rust_class: None,
rust_name,
start,
unstable_api: false,
});
}
syn::Item::Struct(mut s) => {
Expand Down Expand Up @@ -980,6 +981,7 @@ impl<'a, 'b> MacroParse<(&'a Ident, &'a str)> for &'b mut syn::ImplItemMethod {
rust_class: Some(class.clone()),
rust_name: self.sig.ident.clone(),
start: false,
unstable_api: false,
});
opts.check_used()?;
Ok(())
Expand Down Expand Up @@ -1179,6 +1181,7 @@ impl MacroParse<ast::ImportModule> for syn::ForeignItem {
module,
js_namespace,
kind,
unstable_api: false,
});

Ok(())
Expand Down
1 change: 1 addition & 0 deletions crates/shared/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ macro_rules! shared_api {
function: Function<'a>,
method_kind: MethodKind<'a>,
start: bool,
unstable_api: bool,
}

struct Enum<'a> {
Expand Down
27 changes: 18 additions & 9 deletions crates/web-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,12 @@ use std::fs;
use std::path::{self, PathBuf};
use std::process::Command;

fn main() -> Result<()> {
#[cfg(feature = "env_logger")]
env_logger::init();
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")?;
/// Read all WebIDL files in a directory into a single `SourceFile`
fn read_source_from_path(dir: &str) -> Result<SourceFile> {
let entries = fs::read_dir(dir).context("reading webidls/enabled directory")?;
let mut source = SourceFile::default();
for entry in entries {
let entry = entry.context("getting webidls/enabled/*.webidl entry")?;
let entry = entry.context(format!("getting {}/*.webidl entry", dir))?;
let path = entry.path();
if path.extension() != Some(OsStr::new("webidl")) {
continue;
Expand All @@ -27,6 +23,16 @@ fn main() -> Result<()> {
.with_context(|| format!("reading contents of file \"{}\"", path.display()))?;
}

Ok(source)
}

fn main() -> Result<()> {
#[cfg(feature = "env_logger")]
env_logger::init();
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=webidls/enabled");
println!("cargo:rerun-if-changed=webidls/unstable");

// Read our manifest, learn all `[feature]` directives with "toml parsing".
// Use all these names to match against environment variables set by Cargo
// to figure out which features are activated to we can pass that down to
Expand Down Expand Up @@ -63,7 +69,10 @@ fn main() -> Result<()> {
Some(&allowed[..])
};

let bindings = match wasm_bindgen_webidl::compile(&source.contents, allowed) {
let source = read_source_from_path("webidls/enabled")?;
let unstable_source = read_source_from_path("webidls/unstable")?;

let bindings = match wasm_bindgen_webidl::compile(&source.contents, &unstable_source.contents, allowed) {
Ok(bindings) => bindings,
Err(e) => {
if let Some(err) = e.downcast_ref::<wasm_bindgen_webidl::WebIDLParseError>() {
Expand Down
Loading

0 comments on commit 696ef66

Please sign in to comment.