Skip to content

Commit

Permalink
Adding in support for unstable APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
Pauan committed Feb 28, 2020
1 parent ac9724a commit eb301b8
Show file tree
Hide file tree
Showing 112 changed files with 5,755 additions and 101 deletions.
9 changes: 0 additions & 9 deletions crates/backend/src/ast.rs
Expand Up @@ -57,8 +57,6 @@ 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 @@ -79,7 +77,6 @@ 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 Expand Up @@ -135,7 +132,6 @@ pub struct ImportFunction {
pub kind: ImportFunctionKind,
pub shim: Ident,
pub doc_comment: Option<String>,
pub unstable_api: bool,
}

#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
Expand Down Expand Up @@ -192,7 +188,6 @@ pub struct ImportType {
pub js_name: String,
pub attrs: Vec<syn::Attribute>,
pub typescript_name: Option<String>,
pub unstable_api: bool,
pub doc_comment: Option<String>,
pub instanceof_shim: String,
pub is_type_of: Option<syn::Expr>,
Expand All @@ -213,8 +208,6 @@ pub struct ImportEnum {
pub variant_values: Vec<String>,
/// Attributes to apply to the Rust enum
pub rust_attrs: Vec<syn::Attribute>,
/// Whether the enum is part of an unstable WebIDL
pub unstable_api: bool,
}

#[cfg_attr(feature = "extra-traits", derive(Debug))]
Expand Down Expand Up @@ -250,7 +243,6 @@ pub struct StructField {
pub getter: Ident,
pub setter: Ident,
pub comments: Vec<String>,
pub unstable_api: bool,
}

#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
Expand All @@ -260,7 +252,6 @@ pub struct Enum {
pub variants: Vec<Variant>,
pub comments: Vec<String>,
pub hole: u32,
pub unstable_api: bool,
}

#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
Expand Down
33 changes: 1 addition & 32 deletions crates/backend/src/codegen.rs
@@ -1,6 +1,6 @@
use crate::ast;
use crate::encode;
use crate::util::{self, ShortHash};
use crate::util::ShortHash;
use crate::Diagnostic;
use proc_macro2::{Ident, Literal, Span, TokenStream};
use quote::{quote, ToTokens};
Expand Down Expand Up @@ -41,7 +41,6 @@ impl TryToTokens for ast::Program {
for i in self.imports.iter() {
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,
Expand Down Expand Up @@ -298,7 +297,6 @@ impl ToTokens for ast::StructField {
inner: quote! {
<#ty as WasmDescribe>::describe();
},
unstable_api: self.unstable_api,
attrs: vec![],
}
.to_tokens(tokens);
Expand Down Expand Up @@ -536,7 +534,6 @@ impl TryToTokens for ast::Export {
#(<#argtys as WasmDescribe>::describe();)*
#describe_ret
},
unstable_api: self.unstable_api,
attrs: attrs.clone(),
}
.to_tokens(into);
Expand All @@ -563,7 +560,6 @@ impl ToTokens for ast::ImportType {
let vis = &self.vis;
let rust_name = &self.rust_name;
let attrs = &self.attrs;
let unstable_api_attr = util::maybe_unstable_api_attr(self.unstable_api);
let doc_comment = match &self.doc_comment {
None => "",
Some(comment) => comment,
Expand Down Expand Up @@ -612,14 +608,12 @@ impl ToTokens for ast::ImportType {
#[doc = #doc_comment]
#[repr(transparent)]
#[allow(clippy::all)]
#unstable_api_attr
#vis struct #rust_name {
obj: #internal_obj
}

#[allow(bad_style)]
#[allow(clippy::all)]
#unstable_api_attr
const #const_name: () = {
use wasm_bindgen::convert::{IntoWasmAbi, FromWasmAbi};
use wasm_bindgen::convert::{OptionIntoWasmAbi, OptionFromWasmAbi};
Expand Down Expand Up @@ -770,7 +764,6 @@ impl ToTokens for ast::ImportType {
for superclass in self.extends.iter() {
(quote! {
#[allow(clippy::all)]
#unstable_api_attr
impl From<#rust_name> for #superclass {
#[inline]
fn from(obj: #rust_name) -> #superclass {
Expand All @@ -780,7 +773,6 @@ impl ToTokens for ast::ImportType {
}

#[allow(clippy::all)]
#unstable_api_attr
impl AsRef<#superclass> for #rust_name {
#[inline]
fn as_ref(&self) -> &#superclass {
Expand All @@ -802,7 +794,6 @@ impl ToTokens for ast::ImportEnum {
let variants = &self.variants;
let variant_strings = &self.variant_values;
let attrs = &self.rust_attrs;
let unstable_api_attr = util::maybe_unstable_api_attr(self.unstable_api);

let mut current_idx: usize = 0;
let variant_indexes: Vec<Literal> = variants
Expand Down Expand Up @@ -831,15 +822,13 @@ impl ToTokens for ast::ImportEnum {
#[allow(bad_style)]
#(#attrs)*
#[allow(clippy::all)]
#unstable_api_attr
#vis enum #name {
#(#variants = #variant_indexes_ref,)*
#[doc(hidden)]
__Nonexhaustive,
}

#[allow(clippy::all)]
#unstable_api_attr
impl #name {
#vis fn from_js_value(obj: &wasm_bindgen::JsValue) -> Option<#name> {
obj.as_string().and_then(|obj_str| match obj_str.as_str() {
Expand All @@ -850,15 +839,13 @@ impl ToTokens for ast::ImportEnum {
}

#[allow(clippy::all)]
#unstable_api_attr
impl wasm_bindgen::describe::WasmDescribe for #name {
fn describe() {
wasm_bindgen::JsValue::describe()
}
}

#[allow(clippy::all)]
#unstable_api_attr
impl wasm_bindgen::convert::IntoWasmAbi for #name {
type Abi = <wasm_bindgen::JsValue as
wasm_bindgen::convert::IntoWasmAbi>::Abi;
Expand All @@ -870,7 +857,6 @@ impl ToTokens for ast::ImportEnum {
}

#[allow(clippy::all)]
#unstable_api_attr
impl wasm_bindgen::convert::FromWasmAbi for #name {
type Abi = <wasm_bindgen::JsValue as
wasm_bindgen::convert::FromWasmAbi>::Abi;
Expand All @@ -881,21 +867,18 @@ impl ToTokens for ast::ImportEnum {
}

#[allow(clippy::all)]
#unstable_api_attr
impl wasm_bindgen::convert::OptionIntoWasmAbi for #name {
#[inline]
fn none() -> Self::Abi { Object::none() }
}

#[allow(clippy::all)]
#unstable_api_attr
impl wasm_bindgen::convert::OptionFromWasmAbi for #name {
#[inline]
fn is_none(abi: &Self::Abi) -> bool { Object::is_none(abi) }
}

#[allow(clippy::all)]
#unstable_api_attr
impl From<#name> for wasm_bindgen::JsValue {
fn from(obj: #name) -> wasm_bindgen::JsValue {
match obj {
Expand Down Expand Up @@ -1007,7 +990,6 @@ impl TryToTokens for ast::ImportFunction {
let arguments = &arguments;
let abi_arguments = &abi_arguments;
let abi_argument_names = &abi_argument_names;
let unstable_api_attr = util::maybe_unstable_api_attr(self.unstable_api);

let doc_comment = match &self.doc_comment {
None => "",
Expand Down Expand Up @@ -1075,7 +1057,6 @@ impl TryToTokens for ast::ImportFunction {

if let Some(class) = class_ty {
(quote! {
#unstable_api_attr
impl #class {
#invocation
}
Expand All @@ -1092,7 +1073,6 @@ impl TryToTokens for ast::ImportFunction {
// See comment above in ast::Export for what's going on here.
struct DescribeImport<'a> {
kind: &'a ast::ImportKind,
unstable_api: bool,
}

impl<'a> ToTokens for DescribeImport<'a> {
Expand All @@ -1119,7 +1099,6 @@ impl<'a> ToTokens for DescribeImport<'a> {
#(<#argtys as WasmDescribe>::describe();)*
#inform_ret
},
unstable_api: self.unstable_api,
attrs: f.function.rust_attrs.clone(),
}
.to_tokens(tokens);
Expand All @@ -1130,7 +1109,6 @@ impl ToTokens for ast::Enum {
fn to_tokens(&self, into: &mut TokenStream) {
let enum_name = &self.name;
let hole = &self.hole;
let unstable_api_attr = util::maybe_unstable_api_attr(self.unstable_api);
let cast_clauses = self.variants.iter().map(|variant| {
let variant_name = &variant.name;
quote! {
Expand All @@ -1141,7 +1119,6 @@ impl ToTokens for ast::Enum {
});
(quote! {
#[allow(clippy::all)]
#unstable_api_attr
impl wasm_bindgen::convert::IntoWasmAbi for #enum_name {
type Abi = u32;

Expand All @@ -1152,7 +1129,6 @@ impl ToTokens for ast::Enum {
}

#[allow(clippy::all)]
#unstable_api_attr
impl wasm_bindgen::convert::FromWasmAbi for #enum_name {
type Abi = u32;

Expand All @@ -1165,21 +1141,18 @@ impl ToTokens for ast::Enum {
}

#[allow(clippy::all)]
#unstable_api_attr
impl wasm_bindgen::convert::OptionFromWasmAbi for #enum_name {
#[inline]
fn is_none(val: &u32) -> bool { *val == #hole }
}

#[allow(clippy::all)]
#unstable_api_attr
impl wasm_bindgen::convert::OptionIntoWasmAbi for #enum_name {
#[inline]
fn none() -> Self::Abi { #hole }
}

#[allow(clippy::all)]
#unstable_api_attr
impl wasm_bindgen::describe::WasmDescribe for #enum_name {
fn describe() {
use wasm_bindgen::describe::*;
Expand Down Expand Up @@ -1230,7 +1203,6 @@ impl ToTokens for ast::ImportStatic {
inner: quote! {
<#ty as WasmDescribe>::describe();
},
unstable_api: false,
attrs: vec![],
}
.to_tokens(into);
Expand All @@ -1242,7 +1214,6 @@ impl ToTokens for ast::ImportStatic {
struct Descriptor<'a, T> {
ident: &'a Ident,
inner: T,
unstable_api: bool,
attrs: Vec<syn::Attribute>,
}

Expand Down Expand Up @@ -1271,7 +1242,6 @@ impl<'a, T: ToTokens> ToTokens for Descriptor<'a, T> {
}

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;
let attrs = &self.attrs;
(quote! {
Expand All @@ -1281,7 +1251,6 @@ impl<'a, T: ToTokens> ToTokens for Descriptor<'a, T> {
#[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: 0 additions & 1 deletion crates/backend/src/encode.rs
Expand Up @@ -188,7 +188,6 @@ fn shared_export<'a>(
function: shared_function(&export.function, intern),
method_kind,
start: export.start,
unstable_api: export.unstable_api,
})
}

Expand Down
19 changes: 0 additions & 19 deletions crates/backend/src/util.rs
Expand Up @@ -113,12 +113,10 @@ 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 Expand Up @@ -156,20 +154,3 @@ impl<T: Hash> fmt::Display for ShortHash<T> {
write!(f, "{:016x}", h.finish())
}
}


/// Create syn attribute for `#[cfg(web_sys_unstable_apis)]` and a doc comment.
pub fn unstable_api_attrs() -> proc_macro2::TokenStream {
quote::quote!(
#[cfg(web_sys_unstable_apis)]
#[doc = "\n\n*This API is unstable and requires `--cfg=web_sys_unstable_apis` to be activated, as [described in the `wasm-bindgen` guide](https://rustwasm.github.io/docs/wasm-bindgen/web-sys/unstable-apis.html)*"]
)
}

pub fn maybe_unstable_api_attr(unstable_api: bool) -> Option<proc_macro2::TokenStream> {
if unstable_api {
Some(unstable_api_attrs())
} else {
None
}
}

0 comments on commit eb301b8

Please sign in to comment.