mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-20 08:16:31 +00:00
Merge branch 'master' of https://github.com/rustwasm/wasm-bindgen
This commit is contained in:
@ -12,7 +12,8 @@ use std::collections::{BTreeMap, BTreeSet};
|
||||
use weedle::argument::Argument;
|
||||
use weedle::attribute::ExtendedAttribute;
|
||||
use weedle::interface::StringifierOrStatic;
|
||||
use weedle::mixin::MixinMembers;
|
||||
use weedle::mixin::MixinMember;
|
||||
use weedle::namespace::NamespaceMember;
|
||||
use weedle;
|
||||
|
||||
use super::Result;
|
||||
@ -28,6 +29,7 @@ pub(crate) struct FirstPassRecord<'src> {
|
||||
/// The mixins, mapping their name to the webidl ast node for the mixin.
|
||||
pub(crate) mixins: BTreeMap<&'src str, MixinData<'src>>,
|
||||
pub(crate) typedefs: BTreeMap<&'src str, &'src weedle::types::Type<'src>>,
|
||||
pub(crate) namespaces: BTreeMap<&'src str, NamespaceData<'src>>,
|
||||
pub(crate) includes: BTreeMap<&'src str, BTreeSet<&'src str>>,
|
||||
}
|
||||
|
||||
@ -46,7 +48,16 @@ pub(crate) struct InterfaceData<'src> {
|
||||
pub(crate) struct MixinData<'src> {
|
||||
/// Whether only partial mixins were encountered
|
||||
pub(crate) partial: bool,
|
||||
pub(crate) members: Vec<&'src MixinMembers<'src>>,
|
||||
pub(crate) members: Vec<&'src MixinMember<'src>>,
|
||||
pub(crate) operations: BTreeMap<OperationId<'src>, OperationData<'src>>,
|
||||
}
|
||||
|
||||
/// We need to collect namespace data during the first pass, to be used later.
|
||||
#[derive(Default)]
|
||||
pub(crate) struct NamespaceData<'src> {
|
||||
/// Whether only partial namespaces were encountered
|
||||
pub(crate) partial: bool,
|
||||
pub(crate) members: Vec<&'src NamespaceMember<'src>>,
|
||||
pub(crate) operations: BTreeMap<OperationId<'src>, OperationData<'src>>,
|
||||
}
|
||||
|
||||
@ -94,6 +105,8 @@ impl<'src> FirstPass<'src, ()> for weedle::Definition<'src> {
|
||||
PartialInterface(interface) => interface.first_pass(record, ()),
|
||||
InterfaceMixin(mixin) => mixin.first_pass(record, ()),
|
||||
PartialInterfaceMixin(mixin) => mixin.first_pass(record, ()),
|
||||
Namespace(namespace) => namespace.first_pass(record, ()),
|
||||
PartialNamespace(namespace) => namespace.first_pass(record, ()),
|
||||
Typedef(typedef) => typedef.first_pass(record, ()),
|
||||
_ => {
|
||||
// Other definitions aren't currently used in the first pass
|
||||
@ -147,9 +160,16 @@ impl<'src> FirstPass<'src, ()> for weedle::IncludesStatementDefinition<'src> {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
enum FirstPassOperationType {
|
||||
Interface,
|
||||
Mixin,
|
||||
Namespace,
|
||||
}
|
||||
|
||||
fn first_pass_operation<'src>(
|
||||
record: &mut FirstPassRecord<'src>,
|
||||
mixin: bool,
|
||||
first_pass_operation_type: FirstPassOperationType,
|
||||
self_name: &'src str,
|
||||
id: OperationId<'src>,
|
||||
arguments: &[Argument<'src>],
|
||||
@ -161,22 +181,32 @@ fn first_pass_operation<'src>(
|
||||
Argument::Variadic(variadic) => names.push(variadic.identifier.0),
|
||||
}
|
||||
}
|
||||
if mixin {
|
||||
&mut record
|
||||
.mixins
|
||||
.get_mut(self_name)
|
||||
.expect(&format!("not found {} mixin", self_name))
|
||||
.operations
|
||||
} else {
|
||||
&mut record
|
||||
.interfaces
|
||||
.get_mut(self_name)
|
||||
.expect(&format!("not found {} interface", self_name))
|
||||
.operations
|
||||
match first_pass_operation_type{
|
||||
FirstPassOperationType::Interface => {
|
||||
&mut record
|
||||
.interfaces
|
||||
.get_mut(self_name)
|
||||
.expect(&format!("not found {} interface", self_name))
|
||||
.operations
|
||||
},
|
||||
FirstPassOperationType::Mixin => {
|
||||
&mut record
|
||||
.mixins
|
||||
.get_mut(self_name)
|
||||
.expect(&format!("not found {} mixin", self_name))
|
||||
.operations
|
||||
},
|
||||
FirstPassOperationType::Namespace => {
|
||||
&mut record
|
||||
.namespaces
|
||||
.get_mut(self_name)
|
||||
.expect(&format!("not found {} namesace", self_name))
|
||||
.operations
|
||||
},
|
||||
}
|
||||
.entry(id)
|
||||
.and_modify(|operation_data| operation_data.overloaded = true)
|
||||
.or_insert_with(Default::default)
|
||||
.or_default()
|
||||
.argument_names_same
|
||||
.entry(names)
|
||||
.and_modify(|same_argument_names| *same_argument_names = true)
|
||||
@ -195,7 +225,7 @@ impl<'src> FirstPass<'src, ()> for weedle::InterfaceDefinition<'src> {
|
||||
let interface_data = record
|
||||
.interfaces
|
||||
.entry(self.identifier.0)
|
||||
.or_insert_with(Default::default);
|
||||
.or_default();
|
||||
interface_data.partial = false;
|
||||
interface_data.superclass = self.inheritance.map(|s| s.identifier.0);
|
||||
}
|
||||
@ -246,7 +276,7 @@ impl<'src> FirstPass<'src, &'src str> for ExtendedAttribute<'src> {
|
||||
ExtendedAttribute::ArgList(list) if list.identifier.0 == "Constructor" => {
|
||||
first_pass_operation(
|
||||
record,
|
||||
false,
|
||||
FirstPassOperationType::Interface,
|
||||
self_name,
|
||||
OperationId::Constructor,
|
||||
&list.args.body.list,
|
||||
@ -255,7 +285,7 @@ impl<'src> FirstPass<'src, &'src str> for ExtendedAttribute<'src> {
|
||||
ExtendedAttribute::NoArgs(name) if (name.0).0 == "Constructor" => {
|
||||
first_pass_operation(
|
||||
record,
|
||||
false,
|
||||
FirstPassOperationType::Interface,
|
||||
self_name,
|
||||
OperationId::Constructor,
|
||||
&[],
|
||||
@ -266,7 +296,7 @@ impl<'src> FirstPass<'src, &'src str> for ExtendedAttribute<'src> {
|
||||
{
|
||||
first_pass_operation(
|
||||
record,
|
||||
false,
|
||||
FirstPassOperationType::Interface,
|
||||
self_name,
|
||||
OperationId::Constructor,
|
||||
&list.args.body.list,
|
||||
@ -312,15 +342,15 @@ impl<'src> FirstPass<'src, &'src str> for weedle::interface::OperationInterfaceM
|
||||
}
|
||||
first_pass_operation(
|
||||
record,
|
||||
false,
|
||||
FirstPassOperationType::Interface,
|
||||
self_name,
|
||||
match self.identifier.map(|s| s.0) {
|
||||
None => match self.specials.get(0) {
|
||||
None => OperationId::Operation(None),
|
||||
Some(weedle::interface::Special::Getter(weedle::term::Getter)) => OperationId::IndexingGetter,
|
||||
Some(weedle::interface::Special::Setter(weedle::term::Setter)) => OperationId::IndexingSetter,
|
||||
Some(weedle::interface::Special::Deleter(weedle::term::Deleter)) => OperationId::IndexingDeleter,
|
||||
Some(weedle::interface::Special::LegacyCaller(weedle::term::LegacyCaller)) => return Ok(()),
|
||||
Some(weedle::interface::Special::Getter(_)) => OperationId::IndexingGetter,
|
||||
Some(weedle::interface::Special::Setter(_)) => OperationId::IndexingSetter,
|
||||
Some(weedle::interface::Special::Deleter(_)) => OperationId::IndexingDeleter,
|
||||
Some(weedle::interface::Special::LegacyCaller(_)) => return Ok(()),
|
||||
},
|
||||
Some(ref name) => OperationId::Operation(Some(name.clone())),
|
||||
},
|
||||
@ -341,7 +371,7 @@ impl<'src> FirstPass<'src, ()> for weedle::InterfaceMixinDefinition<'src>{
|
||||
.entry(self.identifier.0)
|
||||
.or_insert_with(Default::default);
|
||||
mixin_data.partial = false;
|
||||
mixin_data.members.push(&self.members.body);
|
||||
mixin_data.members.extend(&self.members.body);
|
||||
}
|
||||
|
||||
for member in &self.members.body {
|
||||
@ -369,7 +399,7 @@ impl<'src> FirstPass<'src, ()> for weedle::PartialInterfaceMixinDefinition<'src>
|
||||
},
|
||||
)
|
||||
.members
|
||||
.push(&self.members.body);
|
||||
.extend(&self.members.body);
|
||||
|
||||
for member in &self.members.body {
|
||||
member.first_pass(record, self.identifier.0)?;
|
||||
@ -402,7 +432,7 @@ impl<'src> FirstPass<'src, &'src str> for weedle::mixin::OperationMixinMember<'s
|
||||
}
|
||||
first_pass_operation(
|
||||
record,
|
||||
true,
|
||||
FirstPassOperationType::Mixin,
|
||||
self_name,
|
||||
OperationId::Operation(self.identifier.map(|s| s.0.clone())),
|
||||
&self.args.body.list,
|
||||
@ -424,6 +454,82 @@ impl<'src> FirstPass<'src, ()> for weedle::TypedefDefinition<'src> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'src> FirstPass<'src, ()> for weedle::NamespaceDefinition<'src> {
|
||||
fn first_pass(&'src self, record: &mut FirstPassRecord<'src>, (): ()) -> Result<()> {
|
||||
if util::is_chrome_only(&self.attributes) {
|
||||
return Ok(())
|
||||
}
|
||||
|
||||
record
|
||||
.namespaces
|
||||
.entry(self.identifier.0)
|
||||
.and_modify(|namespace_data| namespace_data.partial = false)
|
||||
.or_insert_with(||
|
||||
NamespaceData {
|
||||
partial: true,
|
||||
members: Default::default(),
|
||||
operations: Default::default(),
|
||||
},
|
||||
)
|
||||
.members
|
||||
.extend(&self.members.body);
|
||||
|
||||
for member in &self.members.body {
|
||||
member.first_pass(record, self.identifier.0)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'src> FirstPass<'src, ()> for weedle::PartialNamespaceDefinition<'src> {
|
||||
fn first_pass(&'src self, record: &mut FirstPassRecord<'src>, (): ()) -> Result<()> {
|
||||
if util::is_chrome_only(&self.attributes) {
|
||||
return Ok(())
|
||||
}
|
||||
|
||||
record
|
||||
.namespaces
|
||||
.entry(self.identifier.0)
|
||||
.or_default()
|
||||
.members
|
||||
.extend(&self.members.body);
|
||||
|
||||
for member in &self.members.body {
|
||||
member.first_pass(record, self.identifier.0)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'src> FirstPass<'src, &'src str> for weedle::namespace::NamespaceMember<'src> {
|
||||
fn first_pass(&'src self, record: &mut FirstPassRecord<'src>, self_name: &'src str) -> Result<()> {
|
||||
match self {
|
||||
weedle::namespace::NamespaceMember::Operation(op) => {
|
||||
op.first_pass(record, self_name)
|
||||
}
|
||||
_ => Ok(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'src> FirstPass<'src, &'src str> for weedle::namespace::OperationNamespaceMember<'src> {
|
||||
fn first_pass(&'src self, record: &mut FirstPassRecord<'src>, self_name: &'src str) -> Result<()> {
|
||||
if util::is_chrome_only(&self.attributes) {
|
||||
return Ok(())
|
||||
}
|
||||
|
||||
first_pass_operation(
|
||||
record,
|
||||
FirstPassOperationType::Namespace,
|
||||
self_name,
|
||||
OperationId::Operation(self.identifier.map(|s| s.0.clone())),
|
||||
&self.args.body.list,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> FirstPassRecord<'a> {
|
||||
pub fn all_superclasses<'me>(&'me self, interface: &str)
|
||||
-> impl Iterator<Item = String> + 'me
|
||||
|
@ -37,9 +37,9 @@ use std::path::Path;
|
||||
|
||||
use backend::TryToTokens;
|
||||
use backend::defined::{ImportedTypeDefinitions, RemoveUndefinedImports};
|
||||
use backend::util::{ident_ty, rust_ident, wrap_import_function};
|
||||
use backend::util::{ident_ty, rust_ident, raw_ident, wrap_import_function};
|
||||
use failure::ResultExt;
|
||||
use heck::{ShoutySnakeCase};
|
||||
use heck::{ShoutySnakeCase, SnakeCase};
|
||||
use proc_macro2::{Ident, Span};
|
||||
use weedle::argument::Argument;
|
||||
use weedle::attribute::{ExtendedAttribute, ExtendedAttributeList};
|
||||
@ -175,24 +175,24 @@ impl<'src> WebidlParse<'src, ()> for weedle::Definition<'src> {
|
||||
weedle::Definition::PartialInterface(interface) => {
|
||||
interface.webidl_parse(program, first_pass, ())?
|
||||
}
|
||||
weedle::Definition::Typedef(_) |
|
||||
weedle::Definition::InterfaceMixin(_) |
|
||||
weedle::Definition::PartialInterfaceMixin(_) => {
|
||||
// handled in the first pass
|
||||
}
|
||||
weedle::Definition::IncludesStatement(..) => {
|
||||
| weedle::Definition::Typedef(_)
|
||||
| weedle::Definition::InterfaceMixin(_)
|
||||
| weedle::Definition::PartialInterfaceMixin(_)
|
||||
| weedle::Definition::IncludesStatement(..)
|
||||
| weedle::Definition::PartialNamespace(..)=> {
|
||||
// handled in the first pass
|
||||
}
|
||||
weedle::Definition::Implements(..) => {
|
||||
// nothing to do for this, ignore it
|
||||
}
|
||||
weedle::Definition::Namespace(namespace) => {
|
||||
namespace.webidl_parse(program, first_pass, ())?
|
||||
}
|
||||
// TODO
|
||||
weedle::Definition::Callback(..)
|
||||
| weedle::Definition::Callback(..)
|
||||
| weedle::Definition::CallbackInterface(..)
|
||||
| weedle::Definition::Dictionary(..)
|
||||
| weedle::Definition::PartialDictionary(..)
|
||||
| weedle::Definition::Namespace(..)
|
||||
| weedle::Definition::PartialNamespace(..) => {
|
||||
| weedle::Definition::PartialDictionary(..) => {
|
||||
warn!("Unsupported WebIDL definition: {:?}", self)
|
||||
}
|
||||
}
|
||||
@ -250,10 +250,8 @@ impl<'src> WebidlParse<'src, ()> for weedle::InterfaceDefinition<'src> {
|
||||
mixin_name: &str,
|
||||
) -> Result<()> {
|
||||
if let Some(mixin_data) = first_pass.mixins.get(mixin_name) {
|
||||
for members in &mixin_data.members {
|
||||
for member in *members {
|
||||
member.webidl_parse(program, first_pass, self_name)?;
|
||||
}
|
||||
for member in &mixin_data.members {
|
||||
member.webidl_parse(program, first_pass, self_name)?;
|
||||
}
|
||||
}
|
||||
if let Some(mixin_names) = first_pass.includes.get(mixin_name) {
|
||||
@ -316,6 +314,7 @@ impl<'src> WebidlParse<'src, &'src weedle::InterfaceDefinition<'src>> for Extend
|
||||
arguments,
|
||||
&::first_pass::OperationId::Constructor,
|
||||
interface.identifier.0,
|
||||
false,
|
||||
);
|
||||
|
||||
let self_ty = ident_ty(rust_ident(camel_case_ident(interface.identifier.0).as_str()));
|
||||
@ -566,16 +565,16 @@ fn member_attribute<'src>(
|
||||
let is_structural = util::is_structural(attrs);
|
||||
let throws = util::throws(attrs);
|
||||
|
||||
for import_function in first_pass.create_getter(
|
||||
identifier,
|
||||
&type_.type_,
|
||||
self_name,
|
||||
is_static,
|
||||
is_structural,
|
||||
throws,
|
||||
) {
|
||||
program.imports.push(wrap_import_function(import_function));
|
||||
}
|
||||
for import_function in first_pass.create_getter(
|
||||
identifier,
|
||||
&type_.type_,
|
||||
self_name,
|
||||
is_static,
|
||||
is_structural,
|
||||
throws,
|
||||
) {
|
||||
program.imports.push(wrap_import_function(import_function));
|
||||
}
|
||||
|
||||
if !readonly {
|
||||
for import_function in first_pass.create_setter(
|
||||
@ -666,10 +665,10 @@ fn member_operation<'src>(
|
||||
match identifier.map(|s| s.0) {
|
||||
None if specials.is_empty() => ::first_pass::OperationId::Operation(None),
|
||||
None if specials.len() == 1 => match specials[0] {
|
||||
weedle::interface::Special::Getter(weedle::term::Getter) => ::first_pass::OperationId::IndexingGetter,
|
||||
weedle::interface::Special::Setter(weedle::term::Setter) => ::first_pass::OperationId::IndexingSetter,
|
||||
weedle::interface::Special::Deleter(weedle::term::Deleter) => ::first_pass::OperationId::IndexingDeleter,
|
||||
weedle::interface::Special::LegacyCaller(weedle::term::LegacyCaller) => return Ok(()),
|
||||
weedle::interface::Special::Getter(_) => ::first_pass::OperationId::IndexingGetter,
|
||||
weedle::interface::Special::Setter(_) => ::first_pass::OperationId::IndexingSetter,
|
||||
weedle::interface::Special::Deleter(_) => ::first_pass::OperationId::IndexingDeleter,
|
||||
weedle::interface::Special::LegacyCaller(_) => return Ok(()),
|
||||
},
|
||||
Some(ref name) if specials.is_empty() => ::first_pass::OperationId::Operation(Some(name.clone())),
|
||||
_ => {
|
||||
@ -757,8 +756,8 @@ impl<'src> WebidlParse<'src, ()> for weedle::EnumDefinition<'src> {
|
||||
variants: variants
|
||||
.iter()
|
||||
.map(|v| {
|
||||
if !v.0.is_empty() {
|
||||
rust_ident(camel_case_ident(&v.0).as_str())
|
||||
if !v.0.is_empty() {
|
||||
rust_ident(camel_case_ident(&v.0).as_str())
|
||||
} else {
|
||||
rust_ident("None")
|
||||
}
|
||||
@ -777,14 +776,14 @@ impl<'src> WebidlParse<'src, &'src str> for weedle::interface::ConstMember<'src>
|
||||
fn webidl_parse(
|
||||
&'src self,
|
||||
program: &mut backend::ast::Program,
|
||||
record: &FirstPassRecord<'src>,
|
||||
first_pass: &FirstPassRecord<'src>,
|
||||
self_name: &'src str,
|
||||
) -> Result<()> {
|
||||
if util::is_chrome_only(&self.attributes) {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let idl_type = match self.const_type.to_idl_type(record) {
|
||||
let idl_type = match self.const_type.to_idl_type(first_pass) {
|
||||
None => return Ok(()),
|
||||
Some(idl_type) => idl_type,
|
||||
};
|
||||
@ -805,3 +804,100 @@ impl<'src> WebidlParse<'src, &'src str> for weedle::interface::ConstMember<'src>
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'src> WebidlParse<'src, ()> for weedle::NamespaceDefinition<'src> {
|
||||
fn webidl_parse(
|
||||
&'src self,
|
||||
program: &mut backend::ast::Program,
|
||||
first_pass: &FirstPassRecord<'src>,
|
||||
(): (),
|
||||
) -> Result<()> {
|
||||
if util::is_chrome_only(&self.attributes) {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if let Some(attrs) = &self.attributes {
|
||||
for attr in &attrs.body.list {
|
||||
attr.webidl_parse(program, first_pass, self)?;
|
||||
}
|
||||
}
|
||||
|
||||
let mut module = backend::ast::Module {
|
||||
vis: public(),
|
||||
name: rust_ident(self.identifier.0.to_snake_case().as_str()),
|
||||
imports: Default::default(),
|
||||
};
|
||||
|
||||
if let Some(namespace_data) = first_pass.namespaces.get(&self.identifier.0) {
|
||||
for member in &namespace_data.members {
|
||||
member.webidl_parse(program, first_pass, (&self.identifier.0, &mut module))?;
|
||||
}
|
||||
}
|
||||
|
||||
program.modules.push(module);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'src> WebidlParse<'src, &'src weedle::NamespaceDefinition<'src>> for ExtendedAttribute<'src> {
|
||||
fn webidl_parse(
|
||||
&'src self,
|
||||
_program: &mut backend::ast::Program,
|
||||
_first_pass: &FirstPassRecord<'src>,
|
||||
_namespace: &'src weedle::NamespaceDefinition<'src>,
|
||||
) -> Result<()> {
|
||||
warn!("Unsupported WebIDL extended attribute: {:?}", self);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'src> WebidlParse<'src, (&'src str, &'src mut backend::ast::Module)> for weedle::namespace::NamespaceMember<'src> {
|
||||
fn webidl_parse(
|
||||
&'src self,
|
||||
program: &mut backend::ast::Program,
|
||||
first_pass: &FirstPassRecord<'src>,
|
||||
(self_name, module): (&'src str, &mut backend::ast::Module),
|
||||
) -> Result<()> {
|
||||
match self {
|
||||
weedle::namespace::NamespaceMember::Operation(op) => {
|
||||
op.webidl_parse(program, first_pass, (self_name, module))?;
|
||||
}
|
||||
weedle::namespace::NamespaceMember::Attribute(_) => {
|
||||
warn!("Attribute namespace members are not supported")
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'src> WebidlParse<'src, (&'src str, &'src mut backend::ast::Module)> for weedle::namespace::OperationNamespaceMember<'src> {
|
||||
fn webidl_parse(
|
||||
&'src self,
|
||||
_program: &mut backend::ast::Program,
|
||||
first_pass: &FirstPassRecord<'src>,
|
||||
(self_name, module): (&'src str, &mut backend::ast::Module),
|
||||
) -> Result<()> {
|
||||
if util::is_chrome_only(&self.attributes) {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
for import_function in first_pass.create_namespace_operation(
|
||||
&self.args.body.list,
|
||||
self.identifier.as_ref().map(|id| id.0),
|
||||
&self.return_type,
|
||||
self_name,
|
||||
util::throws(&self.attributes)
|
||||
) {
|
||||
module.imports.push(
|
||||
backend::ast::Import {
|
||||
module: None,
|
||||
js_namespace: Some(raw_ident(self_name)),
|
||||
kind: backend::ast::ImportKind::Function(import_function),
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ use weedle::attribute::{ExtendedAttributeList, ExtendedAttribute};
|
||||
use weedle::argument::Argument;
|
||||
use weedle::literal::{ConstValue, FloatLit, IntegerLit};
|
||||
|
||||
use first_pass::FirstPassRecord;
|
||||
use first_pass::{self, FirstPassRecord};
|
||||
use idl_type::{IdlType, ToIdlType, flatten};
|
||||
|
||||
/// Take a type and create an immutable shared reference to that type.
|
||||
@ -367,7 +367,7 @@ impl<'src> FirstPassRecord<'src> {
|
||||
pub fn create_basic_method(
|
||||
&self,
|
||||
arguments: &[weedle::argument::Argument],
|
||||
operation_id: ::first_pass::OperationId,
|
||||
operation_id: first_pass::OperationId,
|
||||
return_type: &weedle::types::ReturnType,
|
||||
self_name: &str,
|
||||
is_static: bool,
|
||||
@ -378,20 +378,21 @@ impl<'src> FirstPassRecord<'src> {
|
||||
arguments,
|
||||
&operation_id,
|
||||
self_name,
|
||||
false,
|
||||
);
|
||||
|
||||
let name = match &operation_id {
|
||||
::first_pass::OperationId::Constructor => panic!("constructors are unsupported"),
|
||||
::first_pass::OperationId::Operation(name) => match name {
|
||||
first_pass::OperationId::Constructor => panic!("constructors are unsupported"),
|
||||
first_pass::OperationId::Operation(name) => match name {
|
||||
None => {
|
||||
warn!("Operations without a name are unsupported");
|
||||
return Vec::new();
|
||||
}
|
||||
Some(name) => name.to_string(),
|
||||
},
|
||||
::first_pass::OperationId::IndexingGetter => "get".to_string(),
|
||||
::first_pass::OperationId::IndexingSetter => "set".to_string(),
|
||||
::first_pass::OperationId::IndexingDeleter => "delete".to_string(),
|
||||
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 {
|
||||
@ -400,11 +401,11 @@ impl<'src> FirstPassRecord<'src> {
|
||||
kind: backend::ast::MethodKind::Operation(backend::ast::Operation {
|
||||
is_static,
|
||||
kind: match &operation_id {
|
||||
::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,
|
||||
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,
|
||||
},
|
||||
}),
|
||||
};
|
||||
@ -415,17 +416,17 @@ impl<'src> FirstPassRecord<'src> {
|
||||
};
|
||||
|
||||
let doc_comment = match &operation_id {
|
||||
::first_pass::OperationId::Constructor => panic!("constructors are unsupported"),
|
||||
::first_pass::OperationId::Operation(_) => Some(
|
||||
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))
|
||||
)
|
||||
),
|
||||
::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()),
|
||||
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()),
|
||||
};
|
||||
|
||||
let arguments = match self.convert_arguments(arguments) {
|
||||
@ -451,23 +452,24 @@ impl<'src> FirstPassRecord<'src> {
|
||||
pub fn get_operation_overloading(
|
||||
&self,
|
||||
arguments: &[weedle::argument::Argument],
|
||||
id: &::first_pass::OperationId,
|
||||
operation_id: &first_pass::OperationId,
|
||||
self_name: &str,
|
||||
namespace: bool,
|
||||
) -> (bool, bool) {
|
||||
fn get_operation_data<'src>(
|
||||
record: &'src FirstPassRecord,
|
||||
id: &'src ::first_pass::OperationId,
|
||||
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(id) {
|
||||
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, id, self_name, mixin_name) {
|
||||
if let Some(operation_data) = get_operation_data(record, operation_id, self_name, mixin_name) {
|
||||
return Some(operation_data);
|
||||
}
|
||||
}
|
||||
@ -475,20 +477,28 @@ impl<'src> FirstPassRecord<'src> {
|
||||
None
|
||||
}
|
||||
|
||||
let operation_data = self
|
||||
.interfaces
|
||||
.get(self_name)
|
||||
.and_then(|interface_data| interface_data.operations.get(id))
|
||||
.unwrap_or_else(||
|
||||
get_operation_data(self, id, self_name, self_name)
|
||||
.expect(&format!("not found operation {:?} in interface {}", id, self_name))
|
||||
);
|
||||
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 arg in arguments {
|
||||
match arg {
|
||||
Argument::Single(arg) => names.push(arg.identifier.0),
|
||||
Argument::Variadic(_) => return (false, false),
|
||||
for argument in arguments {
|
||||
match argument {
|
||||
Argument::Single(single) => names.push(single.identifier.0),
|
||||
Argument::Variadic(variadic) => names.push(variadic.identifier.0),
|
||||
}
|
||||
}
|
||||
(
|
||||
@ -500,6 +510,62 @@ impl<'src> FirstPassRecord<'src> {
|
||||
)
|
||||
}
|
||||
|
||||
/// Create a wasm-bindgen operation (free function with no `self` type), if possible.
|
||||
pub fn create_namespace_operation(
|
||||
&self,
|
||||
arguments: &[weedle::argument::Argument],
|
||||
operation_name: Option<&str>,
|
||||
return_type: &weedle::types::ReturnType,
|
||||
self_name: &str,
|
||||
catch: bool,
|
||||
) -> Vec<backend::ast::ImportFunction> {
|
||||
let (overloaded, same_argument_names) = self.get_operation_overloading(
|
||||
arguments,
|
||||
&first_pass::OperationId::Operation(operation_name),
|
||||
self_name,
|
||||
true,
|
||||
);
|
||||
|
||||
let name = match operation_name {
|
||||
Some(name) => name.to_string(),
|
||||
None => {
|
||||
warn!("Operations without a name are unsupported");
|
||||
return Vec::new();
|
||||
}
|
||||
};
|
||||
|
||||
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
|
||||
};
|
||||
|
||||
self.create_function(
|
||||
&name,
|
||||
overloaded,
|
||||
same_argument_names,
|
||||
&arguments,
|
||||
ret,
|
||||
backend::ast::ImportFunctionKind::Normal,
|
||||
false,
|
||||
catch,
|
||||
doc_comment,
|
||||
)
|
||||
}
|
||||
|
||||
/// Create a wasm-bindgen getter method, if possible.
|
||||
pub fn create_getter(
|
||||
&self,
|
||||
|
Reference in New Issue
Block a user