680 lines
24 KiB
Rust
Raw Normal View History

use std::iter::FromIterator;
use std::collections::BTreeMap;
use backend;
2018-08-04 15:16:02 -07:00
use backend::util::{ident_ty, leading_colon_path_ty, raw_ident, rust_ident};
use heck::{CamelCase, SnakeCase};
use proc_macro2::Ident;
use syn;
use weedle;
use weedle::attribute::{ExtendedAttributeList, ExtendedAttribute};
2018-08-11 23:46:33 +03:00
use weedle::argument::Argument;
use weedle::literal::{ConstValue, FloatLit, IntegerLit};
2018-08-10 17:06:11 +01:00
use first_pass::{self, FirstPassRecord};
2018-08-11 23:46:33 +03:00
use idl_type::{IdlType, ToIdlType, flatten};
/// Take a type and create an immutable shared reference to that type.
2018-08-11 23:46:33 +03:00
pub(crate) fn shared_ref(ty: syn::Type) -> syn::Type {
syn::TypeReference {
and_token: Default::default(),
lifetime: None,
mutability: None,
elem: Box::new(ty),
}.into()
}
/// Fix camelcase of identifiers like HTMLBRElement
pub fn camel_case_ident(identifier: &str) -> String {
identifier.replace("HTML", "HTML_").to_camel_case()
}
// Returns a link to MDN
pub fn mdn_doc(class: &str, method: Option<&str>) -> String {
let mut link = format!("https://developer.mozilla.org/en-US/docs/Web/API/{}", class);
if let Some(method) = method {
link.push_str(&format!("/{}", method));
}
format!("[Documentation]({})", link).into()
}
// Array type is borrowed for arguments (`&[T]`) and owned for return value (`Vec<T>`).
2018-08-11 23:46:33 +03:00
pub(crate) fn array(base_ty: &str, pos: TypePosition) -> syn::Type {
match pos {
TypePosition::Argument => {
shared_ref(slice_ty(ident_ty(raw_ident(base_ty))))
}
TypePosition::Return => {
vec_ty(ident_ty(raw_ident(base_ty)))
}
}
}
/// Map a webidl const value to the correct wasm-bindgen const value
pub fn webidl_const_v_to_backend_const_v(v: &ConstValue) -> backend::ast::ConstValue {
use std::f64::{NEG_INFINITY, INFINITY, NAN};
use backend::ast;
match *v {
ConstValue::Boolean(b) => ast::ConstValue::BooleanLiteral(b.0),
ConstValue::Float(FloatLit::NegInfinity(_)) => {
ast::ConstValue::FloatLiteral(NEG_INFINITY)
}
ConstValue::Float(FloatLit::Infinity(_)) => {
ast::ConstValue::FloatLiteral(INFINITY)
}
ConstValue::Float(FloatLit::NaN(_)) => {
ast::ConstValue::FloatLiteral(NAN)
}
ConstValue::Float(FloatLit::Value(s)) => {
ast::ConstValue::FloatLiteral(s.0.parse().unwrap())
}
ConstValue::Integer(lit) => {
let mklit = |orig_text: &str, base: u32, offset: usize| {
let (negative, text) = if orig_text.starts_with("-") {
(true, &orig_text[1..])
} else {
(false, orig_text)
};
if text == "0" {
return ast::ConstValue::SignedIntegerLiteral(0)
}
let text = &text[offset..];
let n = u64::from_str_radix(text, base)
.unwrap_or_else(|_| panic!("literal too big: {}", orig_text));
if negative {
let n = if n > (i64::min_value() as u64).wrapping_neg() {
panic!("literal too big: {}", orig_text)
} else {
n.wrapping_neg() as i64
};
ast::ConstValue::SignedIntegerLiteral(n)
} else {
ast::ConstValue::UnsignedIntegerLiteral(n)
}
};
match lit {
IntegerLit::Hex(h) => mklit(h.0, 16, 2), // leading 0x
IntegerLit::Oct(h) => mklit(h.0, 8, 1), // leading 0
IntegerLit::Dec(h) => mklit(h.0, 10, 0),
}
}
ConstValue::Null(_) => ast::ConstValue::Null,
}
}
/// From `ident` and `Ty`, create `ident: Ty` for use in e.g. `fn(ident: Ty)`.
fn simple_fn_arg(ident: Ident, ty: syn::Type) -> syn::ArgCaptured {
syn::ArgCaptured {
pat: syn::Pat::Ident(syn::PatIdent {
by_ref: None,
mutability: None,
ident,
subpat: None,
}),
colon_token: Default::default(),
ty,
}
}
/// Create `()`.
fn unit_ty() -> syn::Type {
syn::Type::Tuple(syn::TypeTuple {
paren_token: Default::default(),
elems: syn::punctuated::Punctuated::new(),
})
}
/// From `T` create `Result<T, ::wasm_bindgen::JsValue>`.
fn result_ty(t: syn::Type) -> syn::Type {
let js_value = leading_colon_path_ty(vec![rust_ident("wasm_bindgen"), rust_ident("JsValue")]);
let arguments = syn::PathArguments::AngleBracketed(syn::AngleBracketedGenericArguments {
colon2_token: None,
lt_token: Default::default(),
args: FromIterator::from_iter(vec![
syn::GenericArgument::Type(t),
syn::GenericArgument::Type(js_value),
]),
gt_token: Default::default(),
});
let ident = raw_ident("Result");
let seg = syn::PathSegment { ident, arguments };
let path: syn::Path = seg.into();
let ty = syn::TypePath { qself: None, path };
ty.into()
}
/// From `T` create `[T]`.
2018-08-11 23:46:33 +03:00
pub(crate) fn slice_ty(t: syn::Type) -> syn::Type {
syn::TypeSlice {
bracket_token: Default::default(),
elem: Box::new(t),
}.into()
}
/// From `T` create `Vec<T>`.
2018-08-11 23:46:33 +03:00
pub(crate) fn vec_ty(t: syn::Type) -> syn::Type {
let arguments = syn::PathArguments::AngleBracketed(syn::AngleBracketedGenericArguments {
colon2_token: None,
lt_token: Default::default(),
args: FromIterator::from_iter(vec![
syn::GenericArgument::Type(t),
]),
gt_token: Default::default(),
});
let ident = raw_ident("Vec");
let seg = syn::PathSegment { ident, arguments };
let path: syn::Path = seg.into();
let ty = syn::TypePath { qself: None, path };
ty.into()
}
/// From `T` create `Option<T>`
2018-08-11 23:46:33 +03:00
pub(crate) fn option_ty(t: syn::Type) -> syn::Type {
let arguments = syn::PathArguments::AngleBracketed(syn::AngleBracketedGenericArguments {
colon2_token: None,
lt_token: Default::default(),
args: FromIterator::from_iter(vec![syn::GenericArgument::Type(t)]),
gt_token: Default::default(),
});
let ident = raw_ident("Option");
let seg = syn::PathSegment { ident, arguments };
let path: syn::Path = seg.into();
let ty = syn::TypePath { qself: None, path };
ty.into()
}
/// Possible positions for a type in a function signature.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum TypePosition {
Argument,
Return,
}
impl<'src> FirstPassRecord<'src> {
/// Create a wasm-bindgen function, if possible.
pub fn create_function(
&self,
name: &str,
overloaded: bool,
same_argument_names: bool,
2018-08-11 23:46:33 +03:00
arguments: &[(&str, IdlType<'src>, bool)],
ret: IdlType<'src>,
kind: backend::ast::ImportFunctionKind,
structural: bool,
catch: bool,
doc_comment: Option<String>,
2018-08-11 23:46:33 +03:00
) -> Vec<backend::ast::ImportFunction> {
let rust_name = if overloaded && !arguments.is_empty() {
let mut argument_type_names = String::new();
for argument in arguments {
if argument_type_names.len() > 0 {
argument_type_names.push_str("_and_");
}
if same_argument_names {
2018-08-11 23:46:33 +03:00
argument.1.push_type_name(&mut argument_type_names);
} else {
2018-08-11 23:46:33 +03:00
argument_type_names.push_str(&argument.0.to_snake_case());
}
}
if name == "new" {
"with_".to_owned() + &argument_type_names
} else {
name.to_snake_case() + "_with_" + &argument_type_names
}
} else {
name.to_snake_case()
};
2018-08-11 23:46:33 +03:00
let ret = match ret {
IdlType::Void => None,
ret @ _ => {
match ret.to_syn_type(TypePosition::Return) {
2018-08-13 23:18:16 +03:00
None => {
warn!("Can not convert return type to syn type: {:?}", ret);
return Vec::new();
},
2018-08-11 23:46:33 +03:00
Some(ret) => Some(ret),
}
},
};
let js_ret = ret.clone();
2018-08-11 23:46:33 +03:00
let ret = if catch {
Some(ret.map_or_else(|| result_ty(unit_ty()), result_ty))
} else {
ret
};
2018-08-11 23:46:33 +03:00
let converted_arguments = arguments
.iter()
.cloned()
.map(|(_name, idl_type, optional)| (idl_type, optional))
.collect::<Vec<_>>();
let possibilities = flatten(&converted_arguments);
let mut arguments_count_multiple = BTreeMap::new();
for idl_types in &possibilities {
arguments_count_multiple
.entry(idl_types.len())
.and_modify(|variants_count| { *variants_count = true; })
.or_insert(false);
}
let mut import_functions = Vec::new();
'outer: for idl_types in &possibilities {
let rust_name = if possibilities.len() > 1 {
let mut rust_name = rust_name.clone();
let mut first = true;
2018-08-11 23:46:33 +03:00
for ((argument_name, _, _), idl_type) in arguments.iter().zip(idl_types) {
if first {
rust_name.push_str("_using_");
first = false;
} else {
rust_name.push_str("_and_");
}
if arguments_count_multiple[&idl_types.len()] {
idl_type.push_type_name(&mut rust_name);
} else {
rust_name.push_str(&argument_name.to_snake_case());
}
}
rust_name
} else {
rust_name.clone()
};
let rust_name = rust_ident(&rust_name);
let shim = {
let ns = match kind {
backend::ast::ImportFunctionKind::Normal => "",
backend::ast::ImportFunctionKind::Method { ref class, .. } => class,
};
raw_ident(&format!("__widl_f_{}_{}", rust_name, ns))
};
2018-08-11 23:46:33 +03:00
let mut args_captured = if let &backend::ast::ImportFunctionKind::Method {
ref ty,
kind: backend::ast::MethodKind::Operation(
backend::ast::Operation {
is_static: false, ..
}
),
..
} = &kind {
let mut res = Vec::with_capacity(idl_types.len() + 1);
res.push(simple_fn_arg(raw_ident("self_"), shared_ref(ty.clone())));
res
} else {
Vec::with_capacity(idl_types.len())
};
for ((argument_name, _, _), idl_type) in arguments.iter().zip(idl_types) {
let syn_type = if let Some(syn_type) = idl_type.to_syn_type(TypePosition::Argument) {
syn_type
} else {
2018-08-13 23:18:16 +03:00
warn!("Can not convert argument type to syn type: {:?}", idl_type);
2018-08-11 23:46:33 +03:00
continue 'outer;
};
let argument_name = rust_ident(&argument_name.to_snake_case());
args_captured.push(simple_fn_arg(argument_name, syn_type));
}
import_functions.push(backend::ast::ImportFunction {
function: backend::ast::Function {
name: name.to_string(),
2018-08-11 23:46:33 +03:00
arguments: args_captured,
ret: ret.clone(),
rust_attrs: vec![],
rust_vis: public(),
},
rust_name,
js_ret: js_ret.clone(),
catch,
structural,
kind: kind.clone(),
shim,
doc_comment: doc_comment.clone(),
})
}
2018-08-11 23:46:33 +03:00
import_functions
}
/// Convert arguments to ones suitable crating function
pub(crate) fn convert_arguments(
&self,
arguments: &[weedle::argument::Argument<'src>],
) -> Option<Vec<(&str, IdlType<'src>, bool)>> {
let mut converted_arguments = Vec::with_capacity(arguments.len());
for argument in arguments {
let name = match argument {
Argument::Single(single) => single.identifier.0,
Argument::Variadic(variadic) => variadic.identifier.0,
};
let ty = match argument {
Argument::Single(single) => &single.type_.type_,
Argument::Variadic(variadic) => &variadic.type_,
};
let idl_type = ty.to_idl_type(self)?;
let optional = match argument {
Argument::Single(single) => single.optional.is_some(),
Argument::Variadic(_variadic) => false,
};
converted_arguments.push((name, idl_type, optional));
}
Some(converted_arguments)
}
/// Create a wasm-bindgen method, if possible.
pub fn create_basic_method(
&self,
arguments: &[weedle::argument::Argument],
2018-08-10 17:06:11 +01:00
operation_id: first_pass::OperationId,
return_type: &weedle::types::ReturnType,
self_name: &str,
is_static: bool,
structural: bool,
catch: bool,
2018-08-11 23:46:33 +03:00
) -> Vec<backend::ast::ImportFunction> {
let (overloaded, same_argument_names) = self.get_operation_overloading(
arguments,
&operation_id,
self_name,
false,
);
let name = match &operation_id {
2018-08-10 17:06:11 +01:00
first_pass::OperationId::Constructor => panic!("constructors are unsupported"),
first_pass::OperationId::Operation(name) => match name {
None => {
warn!("Operations without a name are unsupported");
2018-08-11 23:46:33 +03:00
return Vec::new();
}
Some(name) => name.to_string(),
},
2018-08-10 17:06:11 +01:00
first_pass::OperationId::IndexingGetter => "get".to_string(),
first_pass::OperationId::IndexingSetter => "set".to_string(),
first_pass::OperationId::IndexingDeleter => "delete".to_string(),
};
let kind = backend::ast::ImportFunctionKind::Method {
class: self_name.to_string(),
ty: ident_ty(rust_ident(camel_case_ident(&self_name).as_str())),
kind: backend::ast::MethodKind::Operation(backend::ast::Operation {
is_static,
kind: match &operation_id {
2018-08-10 17:06:11 +01:00
first_pass::OperationId::Constructor => panic!("constructors are unsupported"),
first_pass::OperationId::Operation(_) => backend::ast::OperationKind::Regular,
first_pass::OperationId::IndexingGetter => backend::ast::OperationKind::IndexingGetter,
first_pass::OperationId::IndexingSetter => backend::ast::OperationKind::IndexingSetter,
first_pass::OperationId::IndexingDeleter => backend::ast::OperationKind::IndexingDeleter,
},
}),
};
2018-08-11 23:46:33 +03:00
let ret = match return_type.to_idl_type(self) {
None => return Vec::new(),
Some(idl_type) => idl_type,
};
2018-08-11 23:46:33 +03:00
let doc_comment = match &operation_id {
2018-08-10 17:06:11 +01:00
first_pass::OperationId::Constructor => panic!("constructors are unsupported"),
first_pass::OperationId::Operation(_) => Some(
format!(
"The `{}()` method\n\n{}",
name,
mdn_doc(self_name, Some(&name))
)
),
2018-08-10 17:06:11 +01:00
first_pass::OperationId::IndexingGetter => Some("The indexing getter\n\n".to_string()),
first_pass::OperationId::IndexingSetter => Some("The indexing setter\n\n".to_string()),
first_pass::OperationId::IndexingDeleter => Some("The indexing deleter\n\n".to_string()),
};
2018-08-11 23:46:33 +03:00
let arguments = match self.convert_arguments(arguments) {
None => return Vec::new(),
Some(arguments) => arguments
};
self.create_function(
&name,
overloaded,
same_argument_names,
2018-08-11 23:46:33 +03:00
&arguments,
ret,
kind,
structural,
catch,
doc_comment,
)
}
/// Whether operation is overloaded and
/// whether there overloads with same argument names for given argument types
pub fn get_operation_overloading(
&self,
arguments: &[weedle::argument::Argument],
operation_id: &first_pass::OperationId,
self_name: &str,
namespace: bool,
) -> (bool, bool) {
fn get_operation_data<'src>(
record: &'src FirstPassRecord,
operation_id: &'src ::first_pass::OperationId,
self_name: &str,
mixin_name: &str,
) -> Option<&'src ::first_pass::OperationData<'src>> {
if let Some(mixin_data) = record.mixins.get(mixin_name) {
if let Some(operation_data) = mixin_data.operations.get(operation_id) {
return Some(operation_data);
}
}
if let Some(mixin_names) = record.includes.get(mixin_name) {
for mixin_name in mixin_names {
if let Some(operation_data) = get_operation_data(record, operation_id, self_name, mixin_name) {
return Some(operation_data);
}
}
}
None
}
let operation_data = if !namespace {
self
.interfaces
.get(self_name)
.and_then(|interface_data| interface_data.operations.get(operation_id))
.unwrap_or_else(||
get_operation_data(self, operation_id, self_name, self_name)
.expect(&format!("not found operation {:?} in interface {}", operation_id, self_name))
)
} else {
self
.namespaces
.get(self_name)
.and_then(|interface_data| interface_data.operations.get(operation_id))
.expect(&format!("not found operation {:?} in namespace {}", operation_id, self_name))
};
let mut names = Vec::with_capacity(arguments.len());
for argument in arguments {
match argument {
Argument::Single(single) => names.push(single.identifier.0),
Argument::Variadic(variadic) => names.push(variadic.identifier.0),
}
}
(
operation_data.overloaded,
*operation_data
.argument_names_same
.get(&names)
.unwrap_or(&false)
)
}
2018-08-10 17:06:11 +01:00
/// Create a wasm-bindgen operation (free function with no `self` type), if possible.
pub fn create_namespace_operation(
&self,
arguments: &[weedle::argument::Argument],
2018-08-10 19:00:56 +01:00
operation_name: Option<&str>,
2018-08-10 17:06:11 +01:00
return_type: &weedle::types::ReturnType,
self_name: &str,
2018-08-10 17:06:11 +01:00
catch: bool,
) -> Vec<backend::ast::ImportFunction> {
let (overloaded, same_argument_names) = self.get_operation_overloading(
2018-08-10 17:06:11 +01:00
arguments,
&first_pass::OperationId::Operation(operation_name),
self_name,
true,
2018-08-10 17:06:11 +01:00
);
2018-08-10 19:00:56 +01:00
let name = match operation_name {
Some(name) => name.to_string(),
None => {
warn!("Operations without a name are unsupported");
return Vec::new();
2018-08-10 17:06:11 +01:00
}
};
let ret = match return_type.to_idl_type(self) {
None => return Vec::new(),
Some(idl_type) => idl_type,
};
let doc_comment = Some(
format!(
"The `{}.{}()` function\n\n{}",
self_name,
name,
mdn_doc(self_name, Some(&name))
)
);
let arguments = match self.convert_arguments(arguments) {
None => return Vec::new(),
Some(arguments) => arguments
2018-08-10 17:06:11 +01:00
};
self.create_function(
&name,
overloaded,
same_argument_names,
&arguments,
2018-08-10 17:06:11 +01:00
ret,
2018-08-10 19:00:56 +01:00
backend::ast::ImportFunctionKind::Normal,
false,
2018-08-10 17:06:11 +01:00
catch,
doc_comment,
)
}
/// Create a wasm-bindgen getter method, if possible.
pub fn create_getter(
&self,
name: &str,
ty: &weedle::types::Type,
self_name: &str,
is_static: bool,
is_structural: bool,
catch: bool,
2018-08-11 23:46:33 +03:00
) -> Vec<backend::ast::ImportFunction> {
let ret = match ty.to_idl_type(self) {
None => return Vec::new(),
Some(idl_type) => idl_type,
};
2018-06-14 19:21:33 -07:00
let kind = backend::ast::ImportFunctionKind::Method {
class: self_name.to_string(),
ty: ident_ty(rust_ident(camel_case_ident(&self_name).as_str())),
kind: backend::ast::MethodKind::Operation(backend::ast::Operation {
is_static,
kind: backend::ast::OperationKind::Getter(Some(raw_ident(name))),
}),
};
let doc_comment = Some(format!("The `{}` getter\n\n{}", name, mdn_doc(self_name, Some(name))));
self.create_function(name, false, false, &[], ret, kind, is_structural, catch, doc_comment)
}
/// Create a wasm-bindgen setter method, if possible.
pub fn create_setter(
&self,
name: &str,
ty: weedle::types::Type,
self_name: &str,
is_static: bool,
is_structural: bool,
catch: bool,
2018-08-11 23:46:33 +03:00
) -> Vec<backend::ast::ImportFunction> {
let kind = backend::ast::ImportFunctionKind::Method {
class: self_name.to_string(),
ty: ident_ty(rust_ident(camel_case_ident(&self_name).as_str())),
kind: backend::ast::MethodKind::Operation(backend::ast::Operation {
is_static,
kind: backend::ast::OperationKind::Setter(Some(raw_ident(name))),
}),
};
let doc_comment = Some(format!("The `{}` setter\n\n{}", name, mdn_doc(self_name, Some(name))));
self.create_function(
&format!("set_{}", name),
false,
false,
2018-08-11 23:46:33 +03:00
&[(
name,
match ty.to_idl_type(self) {
None => return Vec::new(),
Some(idl_type) => idl_type,
},
2018-08-11 23:46:33 +03:00
false,
)],
IdlType::Void,
kind,
is_structural,
catch,
doc_comment,
)
}
}
2018-07-02 20:35:05 -07:00
/// Search for an attribute by name in some webidl object's attributes.
fn has_named_attribute(list: &Option<ExtendedAttributeList>, attribute: &str) -> bool {
let list = match list {
Some(list) => list,
None => return false,
};
list.body.list.iter().any(|attr| match attr {
ExtendedAttribute::NoArgs(name) => (name.0).0 == attribute,
_ => false,
2018-07-02 20:35:05 -07:00
})
}
Create the `web-sys` crate mechanically from WebIDL (#409) * Create a new `web-sys` crate This will eventually contain all the WebIDL-generated bindings to Web APIs. * ci: Test the new `web-sys` crate in CI * web-sys: Add a small README * web-sys: Vendor all the WebIDL files from mozilla-central * backend: Add a pass to remove AST items that use undefined imports This is necessary for the WebIDL frontend, which can't translate many WebIDL constructs into equivalent wasm-bindgen AST things yet. It lets us make incremental progress: we can generate bindings to methods we can support right now even though there might be methods on the same interface that we can't support yet. * webidl: Add a bunch of missing semicolons * webidl: Make parsing private It was only `pub` so that we could test it, but we ended up moving towards integration tests rather than unit tests that assert particular ASTs are parsed from WebIDL files. * webidl: Remove uses of undefined import types * test-project-builder: Build projects in "very verbose" mode This helps for debugging failing WebIDL-related tests. * test-project-builder: Add more profiling timers * test-project-builder: Detect when webpack-dev-server fails Instead of going into an infinite loop, detect when webpack-dev-server fails to start up and early exit the test. * webidl: Specify version for dev-dependency on wasm-bindgen-backend Instead of only a relative path. * guide: Add section about contributing to `web-sys` * WIP enable Event.webidl Still need to fix and finish the test. * Update expected webidl output * Start out a test's status as incomplete That way if we don't fill it in the error message doesn't look quite so bizarre * Fix onerror function in headless mode Otherwise we don't see any output! * Fix package.json/node_modules handling in project generation Make sure these are looked up in the git project root rather than the crate root * Avoid logging body text This was meant for debugging and is otherwise pretty noisy * Fix a relative path * More expected test fixes * Fix a typo * test-project-builder: Allow asynchronous tests * webidl: Convert [Unforgeable] attributes into `#[wasm_bindgen(structural)]` Fixes #432 * test-project-builder: Print generated WebIDL bindings for debugging purposes Helps debug bad WebIDL bindings generation inside tests. * When we can't find a descriptor, say which one can't be found This helps when debugging things that need to become structural. * web-sys: Test bindings for Event * ci: Use `--manifest-path dir` instead of `cd dir && ...` * web-sys: Just move .webidl files isntead of symlinking to enable them * tests: Polyfill Array.prototype.values for older browsers in CI * test-project-builder: Don't panic on poisoned headless test mutex We only use it to serialize headless tests so that we don't try to bind the port concurrently. Its OK to run another headless test if an earlier one panicked. * JsValue: Add {is,as}_{object,function} methods Allows dynamically casting values to `js::Object` and `js::Function`. * tidy: Fix whitespace and missing semicolons * Allow for dynamic feature detection of methods If we create bindings to a method that doesn't exist in this implementation, then it shouldn't fail until if/when we actually try and invoke that missing method. * tests: Do feature detection in Array.prototype.values test * Add JsValue::{is_string, as_js_string} methods And document all the cast/convert/check methods for js value. * eslint: allow backtick string literals * Only generate a fallback import function for non-structural imports
2018-07-09 16:35:25 -07:00
/// ChromeOnly is for things that are only exposed to privileged code in Firefox.
pub fn is_chrome_only(ext_attrs: &Option<ExtendedAttributeList>) -> bool {
has_named_attribute(ext_attrs, "ChromeOnly")
}
/// Whether a webidl object is marked as a no interface object.
pub fn is_no_interface_object(ext_attrs: &Option<ExtendedAttributeList>) -> bool {
has_named_attribute(ext_attrs, "NoInterfaceObject")
}
/// Whether a webidl object is marked as structural.
pub fn is_structural(attrs: &Option<ExtendedAttributeList>) -> bool {
has_named_attribute(attrs, "Unforgeable")
Create the `web-sys` crate mechanically from WebIDL (#409) * Create a new `web-sys` crate This will eventually contain all the WebIDL-generated bindings to Web APIs. * ci: Test the new `web-sys` crate in CI * web-sys: Add a small README * web-sys: Vendor all the WebIDL files from mozilla-central * backend: Add a pass to remove AST items that use undefined imports This is necessary for the WebIDL frontend, which can't translate many WebIDL constructs into equivalent wasm-bindgen AST things yet. It lets us make incremental progress: we can generate bindings to methods we can support right now even though there might be methods on the same interface that we can't support yet. * webidl: Add a bunch of missing semicolons * webidl: Make parsing private It was only `pub` so that we could test it, but we ended up moving towards integration tests rather than unit tests that assert particular ASTs are parsed from WebIDL files. * webidl: Remove uses of undefined import types * test-project-builder: Build projects in "very verbose" mode This helps for debugging failing WebIDL-related tests. * test-project-builder: Add more profiling timers * test-project-builder: Detect when webpack-dev-server fails Instead of going into an infinite loop, detect when webpack-dev-server fails to start up and early exit the test. * webidl: Specify version for dev-dependency on wasm-bindgen-backend Instead of only a relative path. * guide: Add section about contributing to `web-sys` * WIP enable Event.webidl Still need to fix and finish the test. * Update expected webidl output * Start out a test's status as incomplete That way if we don't fill it in the error message doesn't look quite so bizarre * Fix onerror function in headless mode Otherwise we don't see any output! * Fix package.json/node_modules handling in project generation Make sure these are looked up in the git project root rather than the crate root * Avoid logging body text This was meant for debugging and is otherwise pretty noisy * Fix a relative path * More expected test fixes * Fix a typo * test-project-builder: Allow asynchronous tests * webidl: Convert [Unforgeable] attributes into `#[wasm_bindgen(structural)]` Fixes #432 * test-project-builder: Print generated WebIDL bindings for debugging purposes Helps debug bad WebIDL bindings generation inside tests. * When we can't find a descriptor, say which one can't be found This helps when debugging things that need to become structural. * web-sys: Test bindings for Event * ci: Use `--manifest-path dir` instead of `cd dir && ...` * web-sys: Just move .webidl files isntead of symlinking to enable them * tests: Polyfill Array.prototype.values for older browsers in CI * test-project-builder: Don't panic on poisoned headless test mutex We only use it to serialize headless tests so that we don't try to bind the port concurrently. Its OK to run another headless test if an earlier one panicked. * JsValue: Add {is,as}_{object,function} methods Allows dynamically casting values to `js::Object` and `js::Function`. * tidy: Fix whitespace and missing semicolons * Allow for dynamic feature detection of methods If we create bindings to a method that doesn't exist in this implementation, then it shouldn't fail until if/when we actually try and invoke that missing method. * tests: Do feature detection in Array.prototype.values test * Add JsValue::{is_string, as_js_string} methods And document all the cast/convert/check methods for js value. * eslint: allow backtick string literals * Only generate a fallback import function for non-structural imports
2018-07-09 16:35:25 -07:00
}
/// Whether a webidl object is marked as throwing.
pub fn throws(attrs: &Option<ExtendedAttributeList>) -> bool {
has_named_attribute(attrs, "Throws")
}
/// Create a syn `pub` token
pub fn public() -> syn::Visibility {
syn::Visibility::Public(syn::VisPublic {
pub_token: Default::default(),
})
}