Add support for unions in arguments and for optional arguments

This commit is contained in:
Anton Danilkin 2018-08-09 19:24:33 +03:00
parent f85fd0e2ad
commit 703b1ab91d
6 changed files with 710 additions and 320 deletions

View File

@ -7,7 +7,7 @@ use Diagnostic;
/// An abstract syntax tree representing a rust program. Contains
/// extra information for joining up this rust code with javascript.
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq))]
#[derive(Default)]
#[derive(Default, Clone)]
pub struct Program {
/// rust -> js interfaces
pub exports: Vec<Export>,
@ -24,6 +24,7 @@ pub struct Program {
/// A rust to js interface. Allows interaction with rust objects/functions
/// from javascript.
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
#[derive(Clone)]
pub struct Export {
/// The javascript class name.
pub class: Option<Ident>,
@ -44,6 +45,7 @@ pub struct Export {
/// The 3 types variations of `self`.
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
#[derive(Clone)]
pub enum MethodSelf {
/// `self`
ByValue,
@ -54,6 +56,7 @@ pub enum MethodSelf {
}
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
#[derive(Clone)]
pub struct Import {
pub module: Option<String>,
pub js_namespace: Option<Ident>,
@ -61,6 +64,7 @@ pub struct Import {
}
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
#[derive(Clone)]
pub enum ImportKind {
Function(ImportFunction),
Static(ImportStatic),
@ -69,6 +73,7 @@ pub enum ImportKind {
}
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
#[derive(Clone)]
pub struct ImportFunction {
pub function: Function,
pub rust_name: Ident,
@ -81,6 +86,7 @@ pub struct ImportFunction {
}
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
#[derive(Clone)]
pub enum ImportFunctionKind {
Method {
class: String,
@ -91,18 +97,21 @@ pub enum ImportFunctionKind {
}
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
#[derive(Clone)]
pub enum MethodKind {
Constructor,
Operation(Operation),
}
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
#[derive(Clone)]
pub struct Operation {
pub is_static: bool,
pub kind: OperationKind,
}
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
#[derive(Clone)]
pub enum OperationKind {
Regular,
Getter(Option<Ident>),
@ -113,6 +122,7 @@ pub enum OperationKind {
}
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
#[derive(Clone)]
pub struct ImportStatic {
pub vis: syn::Visibility,
pub ty: syn::Type,
@ -122,6 +132,7 @@ pub struct ImportStatic {
}
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
#[derive(Clone)]
pub struct ImportType {
pub vis: syn::Visibility,
pub rust_name: Ident,
@ -133,6 +144,7 @@ pub struct ImportType {
}
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
#[derive(Clone)]
pub struct ImportEnum {
/// The Rust enum's visibility
pub vis: syn::Visibility,
@ -147,6 +159,7 @@ pub struct ImportEnum {
}
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
#[derive(Clone)]
pub struct Function {
pub name: String,
pub arguments: Vec<syn::ArgCaptured>,
@ -156,6 +169,7 @@ pub struct Function {
}
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
#[derive(Clone)]
pub struct Struct {
pub name: Ident,
pub fields: Vec<StructField>,
@ -163,6 +177,7 @@ pub struct Struct {
}
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
#[derive(Clone)]
pub struct StructField {
pub name: Ident,
pub struct_name: Ident,
@ -174,6 +189,7 @@ pub struct StructField {
}
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
#[derive(Clone)]
pub struct Enum {
pub name: Ident,
pub variants: Vec<Variant>,
@ -181,6 +197,7 @@ pub struct Enum {
}
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
#[derive(Clone)]
pub struct Variant {
pub name: Ident,
pub value: u32,
@ -202,6 +219,7 @@ pub enum TypeLocation {
}
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq))]
#[derive(Clone)]
pub struct Const {
pub vis: syn::Visibility,
pub name: Ident,
@ -211,6 +229,7 @@ pub struct Const {
}
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq))]
#[derive(Clone)]
/// same as webidl::ast::ConstValue
pub enum ConstValue {
BooleanLiteral(bool),

View File

@ -31,8 +31,7 @@ interface WebGL2RenderingContext
{
};
[NoInterfaceObject]
interface WebGL2RenderingContextBase
interface mixin WebGL2RenderingContextBase
{
const GLenum READ_BUFFER = 0x0C02;
const GLenum UNPACK_ROW_LENGTH = 0x0CF2;
@ -694,8 +693,8 @@ interface WebGL2RenderingContextBase
void bindVertexArray(WebGLVertexArrayObject? array);
};
WebGL2RenderingContextBase implements WebGLRenderingContextBase;
WebGL2RenderingContext implements WebGL2RenderingContextBase;
WebGL2RenderingContextBase includes WebGLRenderingContextBase;
WebGL2RenderingContext includes WebGL2RenderingContextBase;
[NoInterfaceObject]
interface EXT_color_buffer_float {

View File

@ -111,8 +111,7 @@ typedef (Int32Array or sequence<GLint>) Int32List;
// WebGL2RenderingContext have in common. This doesn't have all the things they
// have in common, because we don't support splitting multiple overloads of the
// same method across separate interfaces and pulling them in with "implements".
[Exposed=(Window, Worker), NoInterfaceObject]
interface WebGLRenderingContextBase {
interface mixin WebGLRenderingContextBase {
/* ClearBufferMask */
const GLenum DEPTH_BUFFER_BIT = 0x00000100;
const GLenum STENCIL_BUFFER_BIT = 0x00000400;
@ -802,7 +801,7 @@ interface WebGLRenderingContext {
void uniformMatrix4fv(WebGLUniformLocation? location, GLboolean transpose, Float32List data);
};
WebGLRenderingContext implements WebGLRenderingContextBase;
WebGLRenderingContext includes WebGLRenderingContextBase;
// For OffscreenCanvas
// Reference: https://wiki.whatwg.org/wiki/OffscreenCanvas

View File

@ -26,8 +26,9 @@ pub(crate) struct FirstPassRecord<'src> {
pub(crate) dictionaries: BTreeSet<&'src str>,
pub(crate) enums: BTreeSet<&'src str>,
/// The mixins, mapping their name to the webidl ast node for the mixin.
pub(crate) mixins: BTreeMap<&'src str, Vec<&'src MixinMembers<'src>>>,
pub(crate) mixins: BTreeMap<&'src str, MixinData<'src>>,
pub(crate) typedefs: BTreeMap<&'src str, &'src weedle::types::Type<'src>>,
pub(crate) includes: BTreeMap<&'src str, BTreeSet<&'src str>>,
}
/// We need to collect interface data during the first pass, to be used later.
@ -40,7 +41,16 @@ pub(crate) struct InterfaceData<'src> {
pub(crate) superclass: Option<&'src str>,
}
#[derive(PartialEq, Eq, PartialOrd, Ord)]
/// We need to collect mixin data during the first pass, to be used later.
#[derive(Default)]
pub(crate) struct MixinData<'src> {
/// Whether only partial mixins were encountered
pub(crate) partial: bool,
pub(crate) members: Vec<&'src MixinMembers<'src>>,
pub(crate) operations: BTreeMap<OperationId<'src>, OperationData<'src>>,
}
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug)]
pub(crate) enum OperationId<'src> {
Constructor,
Operation(Option<&'src str>),
@ -79,6 +89,7 @@ impl<'src> FirstPass<'src, ()> for weedle::Definition<'src> {
match self {
Dictionary(dictionary) => dictionary.first_pass(record, ()),
Enum(enum_) => enum_.first_pass(record, ()),
IncludesStatement(includes) => includes.first_pass(record, ()),
Interface(interface) => interface.first_pass(record, ()),
PartialInterface(interface) => interface.first_pass(record, ()),
InterfaceMixin(mixin) => mixin.first_pass(record, ()),
@ -111,8 +122,20 @@ impl<'src> FirstPass<'src, ()> for weedle::EnumDefinition<'src> {
}
}
impl<'src> FirstPass<'src, ()> for weedle::IncludesStatementDefinition<'src> {
fn first_pass(&'src self, record: &mut FirstPassRecord<'src>, (): ()) -> Result<()> {
record
.includes
.entry(self.lhs_identifier.0)
.or_insert_with(Default::default)
.insert(self.rhs_identifier.0);
Ok(())
}
}
fn first_pass_operation<'src>(
record: &mut FirstPassRecord<'src>,
mixin: bool,
self_name: &'src str,
id: OperationId<'src>,
arguments: &[Argument<'src>],
@ -120,15 +143,23 @@ fn first_pass_operation<'src>(
let mut names = Vec::with_capacity(arguments.len());
for argument in arguments {
match argument {
Argument::Single(arg) => names.push(arg.identifier.0),
Argument::Variadic(_) => return Ok(()),
Argument::Single(single) => names.push(single.identifier.0),
Argument::Variadic(variadic) => names.push(variadic.identifier.0),
}
}
record
if mixin {
&mut record
.mixins
.get_mut(self_name)
.unwrap()
.operations
} else {
&mut record
.interfaces
.get_mut(self_name)
.unwrap()
.operations
}
.entry(id)
.and_modify(|operation_data| operation_data.overloaded = true)
.or_insert_with(Default::default)
@ -143,12 +174,12 @@ fn first_pass_operation<'src>(
impl<'src> FirstPass<'src, ()> for weedle::InterfaceDefinition<'src> {
fn first_pass(&'src self, record: &mut FirstPassRecord<'src>, (): ()) -> Result<()> {
{
let interface = record
let interface_data = record
.interfaces
.entry(self.identifier.0)
.or_insert_with(Default::default);
interface.partial = false;
interface.superclass = self.inheritance.map(|s| s.identifier.0);
interface_data.partial = false;
interface_data.superclass = self.inheritance.map(|s| s.identifier.0);
}
if util::is_chrome_only(&self.attributes) {
@ -177,8 +208,8 @@ impl<'src> FirstPass<'src, ()> for weedle::PartialInterfaceDefinition<'src> {
.or_insert_with(||
InterfaceData {
partial: true,
operations: Default::default(),
global: false,
operations: Default::default(),
superclass: None,
},
);
@ -201,6 +232,7 @@ impl<'src> FirstPass<'src, &'src str> for ExtendedAttribute<'src> {
ExtendedAttribute::ArgList(list) if list.identifier.0 == "Constructor" => {
first_pass_operation(
record,
false,
self_name,
OperationId::Constructor,
&list.args.body.list,
@ -209,6 +241,7 @@ impl<'src> FirstPass<'src, &'src str> for ExtendedAttribute<'src> {
ExtendedAttribute::NoArgs(name) if (name.0).0 == "Constructor" => {
first_pass_operation(
record,
false,
self_name,
OperationId::Constructor,
&[],
@ -219,6 +252,7 @@ impl<'src> FirstPass<'src, &'src str> for ExtendedAttribute<'src> {
{
first_pass_operation(
record,
false,
self_name,
OperationId::Constructor,
&list.args.body.list,
@ -260,6 +294,7 @@ impl<'src> FirstPass<'src, &'src str> for weedle::interface::OperationInterfaceM
}
first_pass_operation(
record,
false,
self_name,
match self.identifier.map(|s| s.0) {
None => match self.specials.get(0) {
@ -278,11 +313,23 @@ impl<'src> FirstPass<'src, &'src str> for weedle::interface::OperationInterfaceM
impl<'src> FirstPass<'src, ()> for weedle::InterfaceMixinDefinition<'src>{
fn first_pass(&'src self, record: &mut FirstPassRecord<'src>, (): ()) -> Result<()> {
record
{
let mixin_data = record
.mixins
.entry(self.identifier.0)
.or_insert_with(Default::default)
.push(&self.members.body);
.or_insert_with(Default::default);
mixin_data.partial = false;
mixin_data.members.push(&self.members.body);
}
if util::is_chrome_only(&self.attributes) {
return Ok(())
}
for member in &self.members.body {
member.first_pass(record, self.identifier.0)?;
}
Ok(())
}
}
@ -292,12 +339,55 @@ impl<'src> FirstPass<'src, ()> for weedle::PartialInterfaceMixinDefinition<'src>
record
.mixins
.entry(self.identifier.0)
.or_insert_with(Default::default)
.or_insert_with(||
MixinData {
partial: true,
members: Default::default(),
operations: Default::default(),
},
)
.members
.push(&self.members.body);
if util::is_chrome_only(&self.attributes) {
return Ok(())
}
for member in &self.members.body {
member.first_pass(record, self.identifier.0)?;
}
Ok(())
}
}
impl<'src> FirstPass<'src, &'src str> for weedle::mixin::MixinMember<'src> {
fn first_pass(&'src self, record: &mut FirstPassRecord<'src>, self_name: &'src str) -> Result<()> {
match self {
weedle::mixin::MixinMember::Operation(op) => {
op.first_pass(record, self_name)
}
_ => Ok(()),
}
}
}
impl<'src> FirstPass<'src, &'src str> for weedle::mixin::OperationMixinMember<'src> {
fn first_pass(&'src self, record: &mut FirstPassRecord<'src>, self_name: &'src str) -> Result<()> {
if self.stringifier.is_some() {
warn!("Unsupported webidl operation {:?}", self);
return Ok(())
}
first_pass_operation(
record,
true,
self_name,
OperationId::Operation(self.identifier.map(|s| s.0.clone())),
&self.args.body.list,
)
}
}
impl<'src> FirstPass<'src, ()> for weedle::TypedefDefinition<'src> {
fn first_pass(&'src self, record: &mut FirstPassRecord<'src>, (): ()) -> Result<()> {
if util::is_chrome_only(&self.attributes) {

View File

@ -168,9 +168,6 @@ impl<'src> WebidlParse<'src, ()> for weedle::Definition<'src> {
weedle::Definition::Enum(enumeration) => {
enumeration.webidl_parse(program, first_pass, ())?
}
weedle::Definition::IncludesStatement(includes) => {
includes.webidl_parse(program, first_pass, ())?
}
weedle::Definition::Interface(interface) => {
interface.webidl_parse(program, first_pass, ())?
}
@ -182,6 +179,9 @@ impl<'src> WebidlParse<'src, ()> for weedle::Definition<'src> {
weedle::Definition::PartialInterfaceMixin(_) => {
// handled in the first pass
}
weedle::Definition::IncludesStatement(..) => {
// handled in the first pass
}
weedle::Definition::Implements(..) => {
// nothing to do for this, ignore it
}
@ -199,25 +199,6 @@ impl<'src> WebidlParse<'src, ()> for weedle::Definition<'src> {
}
}
impl<'src> WebidlParse<'src, ()> for weedle::IncludesStatementDefinition<'src> {
fn webidl_parse(
&'src self,
program: &mut backend::ast::Program,
first_pass: &FirstPassRecord<'src>,
(): (),
) -> Result<()> {
match first_pass.mixins.get(self.rhs_identifier.0) {
Some(member_lists) => {
for member in member_lists.iter().flat_map(|list| list.iter()) {
member.webidl_parse(program, first_pass, self.lhs_identifier.0)?;
}
}
None => warn!("Tried to include missing mixin {}", self.rhs_identifier.0),
}
Ok(())
}
}
impl<'src> WebidlParse<'src, ()> for weedle::InterfaceDefinition<'src> {
fn webidl_parse(
&'src self,
@ -261,10 +242,33 @@ impl<'src> WebidlParse<'src, ()> for weedle::InterfaceDefinition<'src> {
}
}
fn parse<'src>(
program: &mut backend::ast::Program,
first_pass: &FirstPassRecord<'src>,
self_name: &str,
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)?;
}
}
}
if let Some(mixin_names) = first_pass.includes.get(mixin_name) {
for mixin_name in mixin_names {
parse(program, first_pass, self_name, mixin_name)?;
}
}
Ok(())
}
for member in &self.members.body {
member.webidl_parse(program, first_pass, self.identifier.0)?;
}
parse(program, first_pass, self.identifier.0, self.identifier.0)?;
Ok(())
}
}
@ -280,7 +284,11 @@ impl<'src> WebidlParse<'src, ()> for weedle::PartialInterfaceDefinition<'src> {
return Ok(());
}
if !first_pass.interfaces.contains_key(self.identifier.0) {
if first_pass
.interfaces
.get(self.identifier.0)
.map(|interface_data| !interface_data.partial)
.unwrap_or(true) {
warn!(
"Partial interface {} missing non-partial interface",
self.identifier.0
@ -345,8 +353,11 @@ impl<'src> WebidlParse<'src, &'src weedle::InterfaceDefinition<'src>> for Extend
throws,
None,
)
.map(wrap_import_function)
.map(|import| program.imports.push(import));
.map(|import_functions|
for import_function in import_functions {
program.imports.push(wrap_import_function(import_function));
}
);
};
match self {
@ -436,8 +447,8 @@ impl<'src> WebidlParse<'src, &'src str> for weedle::interface::InterfaceMember<'
Operation(op) => {
op.webidl_parse(program, first_pass, self_name)
}
Const(cnst) => {
cnst.webidl_parse(program, first_pass, self_name)
Const(const_) => {
const_.webidl_parse(program, first_pass, self_name)
}
Iterable(iterable) => {
iterable.webidl_parse(program, first_pass, self_name)
@ -537,7 +548,7 @@ fn member_attribute<'src>(
return Ok(());
}
let statik = match modifier {
let is_static = match modifier {
Some(Stringifier(_)) => {
warn!("Unsupported stringifier on type {:?}", (self_name, identifier));
return Ok(())
@ -560,12 +571,15 @@ fn member_attribute<'src>(
identifier,
&type_.type_,
self_name,
statik,
is_static,
is_structural,
throws,
)
.map(wrap_import_function)
.map(|import| program.imports.push(import));
.map(|import_functions|
for import_function in import_functions {
program.imports.push(wrap_import_function(import_function));
}
);
if !readonly {
first_pass
@ -573,12 +587,15 @@ fn member_attribute<'src>(
identifier,
type_.type_.clone(),
self_name,
statik,
is_static,
is_structural,
throws,
)
.map(wrap_import_function)
.map(|import| program.imports.push(import));
.map(|import_functions|
for import_function in import_functions {
program.imports.push(wrap_import_function(import_function));
}
);
}
Ok(())
@ -642,7 +659,7 @@ fn member_operation<'src>(
if util::is_chrome_only(attrs) {
return Ok(());
}
let statik = match modifier {
let is_static = match modifier {
Some(Stringifier(_)) => {
warn!("Unsupported stringifier on type {:?}", (self_name, identifier));
return Ok(())
@ -670,7 +687,7 @@ fn member_operation<'src>(
},
return_type,
self_name,
statik,
is_static,
specials.len() == 1 || first_pass
.interfaces
.get(self_name)
@ -678,8 +695,11 @@ fn member_operation<'src>(
.unwrap_or(false),
util::throws(attrs),
)
.map(wrap_import_function)
.map(|import| program.imports.push(import));
.map(|import_functions|
for import_function in import_functions {
program.imports.push(wrap_import_function(import_function));
}
);
Ok(())
}

View File

@ -1,4 +1,5 @@
use std::iter::FromIterator;
use std::iter;
use backend;
use backend::util::{ident_ty, leading_colon_path_ty, raw_ident, rust_ident};
@ -315,64 +316,75 @@ impl<'src> ToSynType<'src> for weedle::term::Object {
}
}
impl<'src> ToSynType<'src> for weedle::types::Type<'src> {
impl<'src> ToSynType<'src> for NonAnyType<'src> {
fn to_syn_type(&self, record: &FirstPassRecord<'src>, pos: TypePosition)
-> Option<syn::Type>
{
use weedle::types::NonAnyType::*;
let single = match self {
Type::Single(s) => s,
Type::Union(_) => return None,
};
match self {
NonAnyType::Boolean(s) => s.to_syn_type(record, pos),
NonAnyType::Octet(s) => s.to_syn_type(record, pos),
NonAnyType::Byte(s) => s.to_syn_type(record, pos),
NonAnyType::Identifier(s) => s.to_syn_type(record, pos),
NonAnyType::Integer(s) => s.to_syn_type(record, pos),
NonAnyType::FloatingPoint(s) => s.to_syn_type(record, pos),
let ty = match single {
// `any` becomes `::wasm_bindgen::JsValue`.
SingleType::Any(_) => {
let path = vec![rust_ident("wasm_bindgen"), rust_ident("JsValue")];
return Some(leading_colon_path_ty(path))
}
SingleType::NonAny(other) => other,
};
NonAnyType::Float32Array(s) => s.to_syn_type(record, pos),
NonAnyType::Float64Array(s) => s.to_syn_type(record, pos),
NonAnyType::Int8Array(s) => s.to_syn_type(record, pos),
NonAnyType::Int16Array(s) => s.to_syn_type(record, pos),
NonAnyType::Int32Array(s) => s.to_syn_type(record, pos),
NonAnyType::Uint8Array(s) => s.to_syn_type(record, pos),
NonAnyType::Uint8ClampedArray(s) => s.to_syn_type(record, pos),
NonAnyType::Uint16Array(s) => s.to_syn_type(record, pos),
NonAnyType::Uint32Array(s) => s.to_syn_type(record, pos),
match ty {
Boolean(s) => s.to_syn_type(record, pos),
Octet(s) => s.to_syn_type(record, pos),
Byte(s) => s.to_syn_type(record, pos),
Identifier(s) => s.to_syn_type(record, pos),
Integer(s) => s.to_syn_type(record, pos),
FloatingPoint(s) => s.to_syn_type(record, pos),
Float32Array(s) => s.to_syn_type(record, pos),
Float64Array(s) => s.to_syn_type(record, pos),
Int8Array(s) => s.to_syn_type(record, pos),
Int16Array(s) => s.to_syn_type(record, pos),
Int32Array(s) => s.to_syn_type(record, pos),
Uint8Array(s) => s.to_syn_type(record, pos),
Uint8ClampedArray(s) => s.to_syn_type(record, pos),
Uint16Array(s) => s.to_syn_type(record, pos),
Uint32Array(s) => s.to_syn_type(record, pos),
DOMString(s) => s.to_syn_type(record, pos),
ByteString(s) => s.to_syn_type(record, pos),
USVString(s) => s.to_syn_type(record, pos),
ArrayBuffer(b) => b.to_syn_type(record, pos),
Object(o) => o.to_syn_type(record, pos),
NonAnyType::DOMString(s) => s.to_syn_type(record, pos),
NonAnyType::ByteString(s) => s.to_syn_type(record, pos),
NonAnyType::USVString(s) => s.to_syn_type(record, pos),
NonAnyType::ArrayBuffer(b) => b.to_syn_type(record, pos),
NonAnyType::Object(o) => o.to_syn_type(record, pos),
// Support for these types is not yet implemented, so skip
// generating any bindings for this function.
| DataView(_)
| Error(_)
| FrozenArrayType(_)
| Promise(_)
| RecordType(..)
| Sequence(_)
| Symbol(_) => {
| NonAnyType::DataView(_)
| NonAnyType::Error(_)
| NonAnyType::FrozenArrayType(_)
| NonAnyType::Promise(_)
| NonAnyType::RecordType(..)
| NonAnyType::Sequence(_)
| NonAnyType::Symbol(_) => {
None
}
}
}
}
impl<'src> ToSynType<'src> for SingleType<'src> {
fn to_syn_type(&self, record: &FirstPassRecord<'src>, pos: TypePosition)
-> Option<syn::Type>
{
match self {
// `any` becomes `::wasm_bindgen::JsValue`.
SingleType::Any(_) => {
let path = vec![rust_ident("wasm_bindgen"), rust_ident("JsValue")];
Some(leading_colon_path_ty(path))
}
SingleType::NonAny(non_any) => non_any.to_syn_type(record, pos),
}
}
}
impl<'src> ToSynType<'src> for Type<'src> {
fn to_syn_type(&self, record: &FirstPassRecord<'src>, pos: TypePosition)
-> Option<syn::Type>
{
match self {
Type::Single(single) => single.to_syn_type(record, pos),
Type::Union(_) => None,
}
}
}
/// 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 {
@ -519,31 +531,153 @@ pub enum TypePosition {
Return,
}
/// Implemented on an AST type node to generate a snake case name.
trait TypeToString {
fn type_to_string(&self, record: &FirstPassRecord, dst: &mut String);
trait GetArgumentPossibilities<'src> {
fn get_argument_possibilities(&self, record: &FirstPassRecord<'src>) -> Option<Vec<(syn::Type, String)>>;
}
impl<T: TypeToString> TypeToString for MayBeNull<T> {
fn type_to_string(&self, record: &FirstPassRecord, dst: &mut String) {
impl<'src, T: GetArgumentPossibilities<'src>> GetArgumentPossibilities<'src> for MayBeNull<T> {
fn get_argument_possibilities(&self, record: &FirstPassRecord<'src>) -> Option<Vec<(syn::Type, String)>> {
Some(
self
.type_
.get_argument_possibilities(record)?
.into_iter()
.map(|(ty, type_name)|
if self.q_mark.is_some() {
(option_ty(ty), "opt_".to_string() + &type_name)
} else {
(ty, type_name)
}
)
.collect()
)
}
}
impl<'src> GetArgumentPossibilities<'src> for weedle::common::Identifier<'src> {
fn get_argument_possibilities(&self, record: &FirstPassRecord<'src>) -> Option<Vec<(syn::Type, String)>> {
if let Some(other) = record.typedefs.get(&self.0) {
other.get_argument_possibilities(record)
} else {
Some(
vec![
(
self.to_syn_type(record, TypePosition::Argument)?,
self.get_type_name(record),
)
]
)
}
}
}
impl<'src> GetArgumentPossibilities<'src> for NonAnyType<'src> {
fn get_argument_possibilities(&self, record: &FirstPassRecord<'src>) -> Option<Vec<(syn::Type, String)>> {
if let NonAnyType::Identifier(identifier) = self {
identifier.get_argument_possibilities(record)
} else {
Some(
vec![
(
self.to_syn_type(record, TypePosition::Argument)?,
self.get_type_name(record),
)
]
)
}
}
}
impl<'src> GetArgumentPossibilities<'src> for SingleType<'src> {
fn get_argument_possibilities(&self, record: &FirstPassRecord<'src>) -> Option<Vec<(syn::Type, String)>> {
if let SingleType::NonAny(non_any) = self {
non_any.get_argument_possibilities(record)
} else {
Some(
vec![
(
self.to_syn_type(record, TypePosition::Argument)?,
self.get_type_name(record),
)
]
)
}
}
}
impl<'src> GetArgumentPossibilities<'src> for UnionMemberType<'src> {
fn get_argument_possibilities(&self, record: &FirstPassRecord<'src>) -> Option<Vec<(syn::Type, String)>> {
match self {
UnionMemberType::Single(single) => single.get_argument_possibilities(record),
UnionMemberType::Union(union) => union.get_argument_possibilities(record),
}
}
}
impl<'src> GetArgumentPossibilities<'src> for UnionType<'src> {
fn get_argument_possibilities(&self, record: &FirstPassRecord<'src>) -> Option<Vec<(syn::Type, String)>> {
let mut result = Vec::new();
for ty in &self.body.list {
result.extend(ty.get_argument_possibilities(record)?.into_iter());
}
Some(result)
}
}
impl<'src> GetArgumentPossibilities<'src> for Type<'src> {
fn get_argument_possibilities(&self, record: &FirstPassRecord<'src>) -> Option<Vec<(syn::Type, String)>> {
match self {
Type::Single(single) => single.get_argument_possibilities(record),
Type::Union(union) => union.get_argument_possibilities(record),
}
}
}
/// Implemented on an AST type node to generate a snake case name.
trait GetTypeName {
fn push_type_name(&self, record: &FirstPassRecord, dst: &mut String);
fn get_type_name(&self, record: &FirstPassRecord) -> String {
let mut string = String::new();
self.push_type_name(record, &mut string);
return string;
}
}
impl<T: GetTypeName> GetTypeName for [T] {
fn push_type_name(&self, record: &FirstPassRecord, dst: &mut String) {
let mut first = true;
for union_member_type in self {
if first {
first = false;
} else {
dst.push_str("_and_");
}
union_member_type.push_type_name(record, dst);
}
}
}
impl<T: GetTypeName> GetTypeName for MayBeNull<T> {
fn push_type_name(&self, record: &FirstPassRecord, dst: &mut String) {
if self.q_mark.is_some() {
dst.push_str("opt_");
}
self.type_.type_to_string(record, dst);
self.type_.push_type_name(record, dst);
}
}
impl<'src> TypeToString for weedle::types::ReturnType<'src> {
fn type_to_string(&self, record: &FirstPassRecord, dst: &mut String) {
impl<'src> GetTypeName for weedle::types::ReturnType<'src> {
fn push_type_name(&self, record: &FirstPassRecord, dst: &mut String) {
match self {
weedle::types::ReturnType::Type(ty) => (*ty).type_to_string(record, dst),
weedle::types::ReturnType::Type(ty) => (*ty).push_type_name(record, dst),
weedle::types::ReturnType::Void(_) => dst.push_str("void"),
}
}
}
impl TypeToString for weedle::types::StringType {
fn type_to_string(&self, _record: &FirstPassRecord, dst: &mut String) {
impl GetTypeName for weedle::types::StringType {
fn push_type_name(&self, _record: &FirstPassRecord, dst: &mut String) {
match self {
weedle::types::StringType::Byte(_) => dst.push_str("byte_str"),
weedle::types::StringType::DOM(_) => dst.push_str("dom_str"),
@ -552,107 +686,107 @@ impl TypeToString for weedle::types::StringType {
}
}
impl TypeToString for weedle::term::Byte {
fn type_to_string(&self, _record: &FirstPassRecord, dst: &mut String) {
impl GetTypeName for weedle::term::Byte {
fn push_type_name(&self, _record: &FirstPassRecord, dst: &mut String) {
dst.push_str("i8");
}
}
impl TypeToString for weedle::term::Octet {
fn type_to_string(&self, _record: &FirstPassRecord, dst: &mut String) {
impl GetTypeName for weedle::term::Octet {
fn push_type_name(&self, _record: &FirstPassRecord, dst: &mut String) {
dst.push_str("u8");
}
}
impl TypeToString for weedle::term::Boolean {
fn type_to_string(&self, _record: &FirstPassRecord, dst: &mut String) {
impl GetTypeName for weedle::term::Boolean {
fn push_type_name(&self, _record: &FirstPassRecord, dst: &mut String) {
dst.push_str("bool");
}
}
impl TypeToString for weedle::term::USVString {
fn type_to_string(&self, _record: &FirstPassRecord, dst: &mut String) {
impl GetTypeName for weedle::term::USVString {
fn push_type_name(&self, _record: &FirstPassRecord, dst: &mut String) {
dst.push_str("usv_str");
}
}
impl TypeToString for weedle::term::ByteString {
fn type_to_string(&self, _record: &FirstPassRecord, dst: &mut String) {
impl GetTypeName for weedle::term::ByteString {
fn push_type_name(&self, _record: &FirstPassRecord, dst: &mut String) {
dst.push_str("byte_str");
}
}
impl TypeToString for weedle::term::DOMString {
fn type_to_string(&self, _record: &FirstPassRecord, dst: &mut String) {
impl GetTypeName for weedle::term::DOMString {
fn push_type_name(&self, _record: &FirstPassRecord, dst: &mut String) {
dst.push_str("dom_str");
}
}
impl TypeToString for weedle::term::Float32Array {
fn type_to_string(&self, _record: &FirstPassRecord, dst: &mut String) {
impl GetTypeName for weedle::term::Float32Array {
fn push_type_name(&self, _record: &FirstPassRecord, dst: &mut String) {
dst.push_str("f32_array");
}
}
impl TypeToString for weedle::term::Float64Array {
fn type_to_string(&self, _record: &FirstPassRecord, dst: &mut String) {
impl GetTypeName for weedle::term::Float64Array {
fn push_type_name(&self, _record: &FirstPassRecord, dst: &mut String) {
dst.push_str("f64_array");
}
}
impl TypeToString for weedle::term::Int8Array {
fn type_to_string(&self, _record: &FirstPassRecord, dst: &mut String) {
impl GetTypeName for weedle::term::Int8Array {
fn push_type_name(&self, _record: &FirstPassRecord, dst: &mut String) {
dst.push_str("i8_array");
}
}
impl TypeToString for weedle::term::Int16Array {
fn type_to_string(&self, _record: &FirstPassRecord, dst: &mut String) {
impl GetTypeName for weedle::term::Int16Array {
fn push_type_name(&self, _record: &FirstPassRecord, dst: &mut String) {
dst.push_str("i16_array");
}
}
impl TypeToString for weedle::term::Int32Array {
fn type_to_string(&self, _record: &FirstPassRecord, dst: &mut String) {
impl GetTypeName for weedle::term::Int32Array {
fn push_type_name(&self, _record: &FirstPassRecord, dst: &mut String) {
dst.push_str("i32_array");
}
}
impl TypeToString for weedle::term::Uint8Array {
fn type_to_string(&self, _record: &FirstPassRecord, dst: &mut String) {
impl GetTypeName for weedle::term::Uint8Array {
fn push_type_name(&self, _record: &FirstPassRecord, dst: &mut String) {
dst.push_str("u8_array");
}
}
impl TypeToString for weedle::term::Uint8ClampedArray {
fn type_to_string(&self, _record: &FirstPassRecord, dst: &mut String) {
impl GetTypeName for weedle::term::Uint8ClampedArray {
fn push_type_name(&self, _record: &FirstPassRecord, dst: &mut String) {
dst.push_str("u8_clamped_array");
}
}
impl TypeToString for weedle::term::Uint16Array {
fn type_to_string(&self, _record: &FirstPassRecord, dst: &mut String) {
impl GetTypeName for weedle::term::Uint16Array {
fn push_type_name(&self, _record: &FirstPassRecord, dst: &mut String) {
dst.push_str("u16_array");
}
}
impl TypeToString for weedle::term::Uint32Array {
fn type_to_string(&self, _record: &FirstPassRecord, dst: &mut String) {
impl GetTypeName for weedle::term::Uint32Array {
fn push_type_name(&self, _record: &FirstPassRecord, dst: &mut String) {
dst.push_str("u32_array");
}
}
impl<'src> TypeToString for weedle::common::Identifier<'src> {
fn type_to_string(&self, record: &FirstPassRecord, dst: &mut String) {
impl<'src> GetTypeName for weedle::common::Identifier<'src> {
fn push_type_name(&self, record: &FirstPassRecord, dst: &mut String) {
match record.typedefs.get(self.0) {
Some(other) => other.type_to_string(record, dst),
Some(other) => other.push_type_name(record, dst),
None => dst.push_str(&self.0.to_snake_case()),
}
}
}
impl TypeToString for IntegerType {
fn type_to_string(&self, _record: &FirstPassRecord, dst: &mut String) {
impl GetTypeName for IntegerType {
fn push_type_name(&self, _record: &FirstPassRecord, dst: &mut String) {
match self {
IntegerType::LongLong(l) if l.unsigned.is_some() => dst.push_str("u64"),
IntegerType::LongLong(_) => dst.push_str("i64"),
@ -664,8 +798,8 @@ impl TypeToString for IntegerType {
}
}
impl TypeToString for FloatingPointType {
fn type_to_string(&self, _record: &FirstPassRecord, dst: &mut String) {
impl GetTypeName for FloatingPointType {
fn push_type_name(&self, _record: &FirstPassRecord, dst: &mut String) {
match self {
FloatingPointType::Float(_) => dst.push_str("f32"),
FloatingPointType::Double(_) => dst.push_str("f64"),
@ -673,111 +807,133 @@ impl TypeToString for FloatingPointType {
}
}
impl TypeToString for weedle::term::ArrayBuffer {
fn type_to_string(&self, _record: &FirstPassRecord, dst: &mut String) {
impl GetTypeName for weedle::term::ArrayBuffer {
fn push_type_name(&self, _record: &FirstPassRecord, dst: &mut String) {
dst.push_str("array_buffer");
}
}
impl TypeToString for weedle::term::Symbol {
fn type_to_string(&self, _record: &FirstPassRecord, dst: &mut String) {
impl GetTypeName for weedle::term::Symbol {
fn push_type_name(&self, _record: &FirstPassRecord, dst: &mut String) {
dst.push_str("symbol");
}
}
impl TypeToString for weedle::term::Object {
fn type_to_string(&self, _record: &FirstPassRecord, dst: &mut String) {
impl GetTypeName for weedle::term::Object {
fn push_type_name(&self, _record: &FirstPassRecord, dst: &mut String) {
dst.push_str("object");
}
}
impl TypeToString for weedle::term::DataView {
fn type_to_string(&self, _record: &FirstPassRecord, dst: &mut String) {
impl GetTypeName for weedle::term::DataView {
fn push_type_name(&self, _record: &FirstPassRecord, dst: &mut String) {
dst.push_str("data_view");
}
}
impl TypeToString for weedle::term::Error {
fn type_to_string(&self, _record: &FirstPassRecord, dst: &mut String) {
impl GetTypeName for weedle::term::Error {
fn push_type_name(&self, _record: &FirstPassRecord, dst: &mut String) {
dst.push_str("error");
}
}
impl<'src> TypeToString for weedle::types::SequenceType<'src> {
fn type_to_string(&self, record: &FirstPassRecord, dst: &mut String) {
dst.push_str("seq_");
self.generics.body.type_to_string(record, dst);
impl<'src> GetTypeName for SequenceType<'src> {
fn push_type_name(&self, record: &FirstPassRecord, dst: &mut String) {
self.generics.body.push_type_name(record, dst);
dst.push_str("_seq");
}
}
impl<'src> TypeToString for weedle::types::PromiseType<'src> {
fn type_to_string(&self, record: &FirstPassRecord, dst: &mut String) {
dst.push_str("promise_");
self.generics.body.type_to_string(record, dst);
impl<'src> GetTypeName for PromiseType<'src> {
fn push_type_name(&self, record: &FirstPassRecord, dst: &mut String) {
self.generics.body.push_type_name(record, dst);
dst.push_str("_promise");
}
}
impl<'src> TypeToString for weedle::types::FrozenArrayType<'src> {
fn type_to_string(&self, record: &FirstPassRecord, dst: &mut String) {
dst.push_str("frozen_array_");
self.generics.body.type_to_string(record, dst);
impl<'src> GetTypeName for FrozenArrayType<'src> {
fn push_type_name(&self, record: &FirstPassRecord, dst: &mut String) {
self.generics.body.push_type_name(record, dst);
dst.push_str("_frozen_array");
}
}
impl<'src> TypeToString for weedle::types::RecordType<'src> {
fn type_to_string(&self, record: &FirstPassRecord, dst: &mut String) {
impl<'src> GetTypeName for RecordType<'src> {
fn push_type_name(&self, record: &FirstPassRecord, dst: &mut String) {
dst.push_str("record_from_");
self.generics.body.0.type_to_string(record, dst);
self.generics.body.0.push_type_name(record, dst);
dst.push_str("_to_");
self.generics.body.2.type_to_string(record, dst);
self.generics.body.2.push_type_name(record, dst);
}
}
impl<'a> TypeToString for weedle::types::Type<'a> {
fn type_to_string(&self, record: &FirstPassRecord, dst: &mut String) {
use weedle::types::NonAnyType::*;
impl<'a> GetTypeName for NonAnyType<'a> {
fn push_type_name(&self, record: &FirstPassRecord, dst: &mut String) {
match self {
NonAnyType::Boolean(s) => s.push_type_name(record, dst),
NonAnyType::Octet(s) => s.push_type_name(record, dst),
NonAnyType::Byte(s) => s.push_type_name(record, dst),
NonAnyType::Identifier(s) => s.push_type_name(record, dst),
NonAnyType::Integer(s) => s.push_type_name(record, dst),
NonAnyType::FloatingPoint(s) => s.push_type_name(record, dst),
let single = match self {
Type::Single(s) => s,
Type::Union(_) => panic!("unions not supported"),
};
NonAnyType::Float32Array(s) => s.push_type_name(record, dst),
NonAnyType::Float64Array(s) => s.push_type_name(record, dst),
NonAnyType::Int8Array(s) => s.push_type_name(record, dst),
NonAnyType::Int16Array(s) => s.push_type_name(record, dst),
NonAnyType::Int32Array(s) => s.push_type_name(record, dst),
NonAnyType::Uint8Array(s) => s.push_type_name(record, dst),
NonAnyType::Uint8ClampedArray(s) => s.push_type_name(record, dst),
NonAnyType::Uint16Array(s) => s.push_type_name(record, dst),
NonAnyType::Uint32Array(s) => s.push_type_name(record, dst),
let ty = match single {
SingleType::Any(_) => return dst.push_str("any"),
SingleType::NonAny(other) => other,
};
NonAnyType::DOMString(s) => s.push_type_name(record, dst),
NonAnyType::ByteString(s) => s.push_type_name(record, dst),
NonAnyType::USVString(s) => s.push_type_name(record, dst),
NonAnyType::ArrayBuffer(s) => s.push_type_name(record, dst),
match ty {
Boolean(s) => s.type_to_string(record, dst),
Octet(s) => s.type_to_string(record, dst),
Byte(s) => s.type_to_string(record, dst),
Identifier(s) => s.type_to_string(record, dst),
Integer(s) => s.type_to_string(record, dst),
FloatingPoint(s) => s.type_to_string(record, dst),
NonAnyType::DataView(s) => s.push_type_name(record, dst),
NonAnyType::Error(s) => s.push_type_name(record, dst),
NonAnyType::FrozenArrayType(s) => s.push_type_name(record, dst),
NonAnyType::Object(s) => s.push_type_name(record, dst),
NonAnyType::Promise(s) => s.push_type_name(record, dst),
NonAnyType::RecordType(s) => s.push_type_name(record, dst),
NonAnyType::Sequence(s) => s.push_type_name(record, dst),
NonAnyType::Symbol(s) => s.push_type_name(record, dst),
}
}
}
Float32Array(s) => s.type_to_string(record, dst),
Float64Array(s) => s.type_to_string(record, dst),
Int8Array(s) => s.type_to_string(record, dst),
Int16Array(s) => s.type_to_string(record, dst),
Int32Array(s) => s.type_to_string(record, dst),
Uint8Array(s) => s.type_to_string(record, dst),
Uint8ClampedArray(s) => s.type_to_string(record, dst),
Uint16Array(s) => s.type_to_string(record, dst),
Uint32Array(s) => s.type_to_string(record, dst),
impl<'a> GetTypeName for SingleType<'a> {
fn push_type_name(&self, record: &FirstPassRecord, dst: &mut String) {
match self {
SingleType::Any(_) => dst.push_str("any"),
SingleType::NonAny(non_any) => non_any.push_type_name(record, dst),
}
}
}
DOMString(s) => s.type_to_string(record, dst),
ByteString(s) => s.type_to_string(record, dst),
USVString(s) => s.type_to_string(record, dst),
ArrayBuffer(s) => s.type_to_string(record, dst),
impl<'a> GetTypeName for UnionMemberType<'a> {
fn push_type_name(&self, record: &FirstPassRecord, dst: &mut String) {
match self {
UnionMemberType::Single(single) => single.push_type_name(record, dst),
UnionMemberType::Union(union) => union.push_type_name(record, dst),
}
}
}
DataView(s) => s.type_to_string(record, dst),
Error(s) => s.type_to_string(record, dst),
FrozenArrayType(s) => s.type_to_string(record, dst),
Object(s) => s.type_to_string(record, dst),
Promise(s) => s.type_to_string(record, dst),
RecordType(s) => s.type_to_string(record, dst),
Sequence(s) => s.type_to_string(record, dst),
Symbol(s) => s.type_to_string(record, dst),
impl<'a> GetTypeName for UnionType<'a> {
fn push_type_name(&self, record: &FirstPassRecord, dst: &mut String) {
dst.push_str("union_of_");
self.body.list.push_type_name(record, dst);
}
}
impl<'a> GetTypeName for Type<'a> {
fn push_type_name(&self, record: &FirstPassRecord, dst: &mut String) {
match self {
Type::Single(single) => single.push_type_name(record, dst),
Type::Union(union) => union.push_type_name(record, dst),
}
}
}
@ -787,46 +943,110 @@ impl<'src> FirstPassRecord<'src> {
///
/// `kind` is whether the function is a method, in which case we would need a `self`
/// parameter.
fn webidl_arguments_to_syn_arg_captured(
///
/// Return option that contains a value if the conversion succeeds.
/// The value is a vector of argument variants.
/// Each variant is a vector of converted argument types and type names.
fn get_variants(
&self,
arguments: &[Argument],
kind: &backend::ast::ImportFunctionKind,
) -> Option<Vec<syn::ArgCaptured>>
) -> Option<Vec<Vec<(syn::ArgCaptured, Option<String>)>>>
{
let arguments_possibilities = {
fn get_argument_possibilities(record: &FirstPassRecord, argument: &Argument) -> Option<Vec<(syn::Type, String)>> {
let single = match argument {
Argument::Single(single) => single,
Argument::Variadic(_) => return None,
};
match single.type_.type_.get_argument_possibilities(record) {
None => {
warn!("Argument's type is not yet supported: {:?}", argument);
None
},
Some(value) => Some(value),
}
}
if !arguments.is_empty() {
let mut optional_arguments_possibilities = Vec::new();
if let Argument::Single(ref single) = arguments[0] {
if single.optional.is_some() {
optional_arguments_possibilities.push(Vec::new());
}
}
let mut arguments_possibilities: Vec<_> = get_argument_possibilities(
self,
&arguments[0]
)?
.into_iter()
.map(|argument_possibility| vec![argument_possibility])
.collect();
for argument in arguments[1..].iter() {
let mut new_arguments_possibilities = Vec::new();
for arguments_possibility in arguments_possibilities {
if let Argument::Single(single) = argument {
if single.optional.is_some() {
optional_arguments_possibilities.push(arguments_possibility.clone());
}
}
let mut element_argument_possibilities = get_argument_possibilities(
self,
&argument
)?;
for element_argument_possibility in element_argument_possibilities {
new_arguments_possibilities.push(
arguments_possibility
.iter()
.cloned()
.chain(iter::once(element_argument_possibility))
.collect()
)
}
}
arguments_possibilities = new_arguments_possibilities
}
optional_arguments_possibilities.extend(arguments_possibilities.into_iter());
optional_arguments_possibilities
} else {
vec![Vec::new()]
}
};
let mut result = Vec::new();
for arguments_possibility in arguments_possibilities {
let mut res = if let backend::ast::ImportFunctionKind::Method {
ty,
kind:
backend::ast::MethodKind::Operation(backend::ast::Operation {
kind: backend::ast::MethodKind::Operation(
backend::ast::Operation {
is_static: false, ..
}),
}
),
..
} = kind
{
} = kind {
let mut res = Vec::with_capacity(arguments.len() + 1);
res.push(simple_fn_arg(raw_ident("self_"), shared_ref(ty.clone())));
res.push((simple_fn_arg(raw_ident("self_"), shared_ref(ty.clone())), None));
res
} else {
Vec::with_capacity(arguments.len())
};
for argument in arguments {
let argument = match argument {
Argument::Single(arg) => arg,
for (argument, argument_possibility) in arguments.iter().zip(arguments_possibility) {
let single = match argument {
Argument::Single(single) => single,
Argument::Variadic(_) => return None,
};
match argument.type_.type_.to_syn_type(self, TypePosition::Argument) {
None => {
warn!("Argument's type is not yet supported: {:?}", argument);
return None;
}
Some(ty) => {
let name = argument.identifier.0.to_snake_case();
res.push(simple_fn_arg(rust_ident(&name), ty))
}
res.push(
(
simple_fn_arg(
rust_ident(&single.identifier.0.to_snake_case()),
argument_possibility.0.clone()
),
Some(argument_possibility.1.clone()),
)
);
}
result.push(res);
}
Some(res)
Some(result)
}
/// Create a wasm-bindgen function, if possible.
@ -841,12 +1061,9 @@ impl<'src> FirstPassRecord<'src> {
structural: bool,
catch: bool,
doc_comment: Option<String>,
) -> Option<backend::ast::ImportFunction>
) -> Option<Vec<backend::ast::ImportFunction>>
{
let ast_arguments = self.webidl_arguments_to_syn_arg_captured(arguments, &kind)?;
let rust_name = rust_ident(
&if overloaded && !arguments.is_empty() {
let rust_name = if overloaded && !arguments.is_empty() {
let mut argument_type_names = String::new();
for arg in arguments {
let arg = match arg {
@ -857,7 +1074,7 @@ impl<'src> FirstPassRecord<'src> {
argument_type_names.push_str("_and_");
}
if same_argument_names {
arg.type_.type_.type_to_string(self, &mut argument_type_names);
arg.type_.type_.push_type_name(self, &mut argument_type_names);
} else {
argument_type_names.push_str(&arg.identifier.0.to_snake_case());
}
@ -869,9 +1086,7 @@ impl<'src> FirstPassRecord<'src> {
}
} else {
name.to_snake_case()
}
);
let name = name.to_string();
};
let js_ret = ret.clone();
@ -879,6 +1094,30 @@ impl<'src> FirstPassRecord<'src> {
ret = Some(ret.map_or_else(|| result_ty(unit_ty()), result_ty))
}
let variants = self.get_variants(arguments, &kind)?;
let multiple_variants = variants.len() > 1;
let mut result = Vec::new();
for variant in variants {
let (variant_types, variant_names): (Vec<_>, Vec<_>) = variant.into_iter().unzip();
let rust_name = if multiple_variants {
let mut rust_name = rust_name.clone();
let mut first = true;
for variant_name in variant_names {
if let Some(type_name) = variant_name {
if first {
rust_name.push_str("_using_");
first = false;
} else {
rust_name.push_str("_and_");
}
rust_name.push_str(&type_name);
}
}
rust_name
} else {
rust_name.clone()
};
let rust_name = rust_ident(&rust_name);
let shim = {
let ns = match kind {
backend::ast::ImportFunctionKind::Normal => "",
@ -888,23 +1127,25 @@ impl<'src> FirstPassRecord<'src> {
raw_ident(&format!("__widl_f_{}_{}", rust_name, ns))
};
Some(backend::ast::ImportFunction {
result.push(backend::ast::ImportFunction {
function: backend::ast::Function {
name,
arguments: ast_arguments,
ret,
name: name.to_string(),
arguments: variant_types,
ret: ret.clone(),
rust_attrs: vec![],
rust_vis: public(),
},
rust_name,
js_ret,
js_ret: js_ret.clone(),
catch,
structural,
kind,
kind: kind.clone(),
shim,
doc_comment,
doc_comment: doc_comment.clone(),
})
}
Some(result)
}
/// Create a wasm-bindgen method, if possible.
pub fn create_basic_method(
@ -916,7 +1157,7 @@ impl<'src> FirstPassRecord<'src> {
is_static: bool,
structural: bool,
catch: bool,
) -> Option<backend::ast::ImportFunction> {
) -> Option<Vec<backend::ast::ImportFunction>> {
let (overloaded, same_argument_names) = self.get_operation_overloading(
arguments,
&operation_id,
@ -999,14 +1240,36 @@ impl<'src> FirstPassRecord<'src> {
id: &::first_pass::OperationId,
self_name: &str,
) -> (bool, bool) {
let data = match self.interfaces.get(self_name) {
Some(data) => data,
None => return (false, false),
};
let data = match data.operations.get(id) {
Some(data) => data,
None => return (false, false),
};
fn get_operation_data<'src>(
record: &'src FirstPassRecord,
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) {
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) {
return Some(operation_data);
}
}
}
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 mut names = Vec::with_capacity(arguments.len());
for arg in arguments {
match arg {
@ -1015,8 +1278,8 @@ impl<'src> FirstPassRecord<'src> {
}
}
(
data.overloaded,
*data
operation_data.overloaded,
*operation_data
.argument_names_same
.get(&names)
.unwrap_or(&false)
@ -1032,7 +1295,7 @@ impl<'src> FirstPassRecord<'src> {
is_static: bool,
is_structural: bool,
catch: bool,
) -> Option<backend::ast::ImportFunction> {
) -> Option<Vec<backend::ast::ImportFunction>> {
let ret = match ty.to_syn_type(self, TypePosition::Return) {
None => {
warn!("Attribute's type does not yet support reading: {:?}", ty);
@ -1063,7 +1326,7 @@ impl<'src> FirstPassRecord<'src> {
is_static: bool,
is_structural: bool,
catch: bool,
) -> Option<backend::ast::ImportFunction> {
) -> Option<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())),