2018-07-07 10:20:31 -07:00
|
|
|
use proc_macro2::{Ident, Span};
|
2018-02-06 07:56:14 -08:00
|
|
|
use shared;
|
2017-12-14 19:31:01 -08:00
|
|
|
use syn;
|
2018-11-27 12:07:59 -08:00
|
|
|
use Diagnostic;
|
2019-02-25 11:11:30 -08:00
|
|
|
use std::hash::{Hash, Hasher};
|
2017-12-14 19:31:01 -08:00
|
|
|
|
2018-07-24 17:37:49 +01:00
|
|
|
/// An abstract syntax tree representing a rust program. Contains
|
|
|
|
/// extra information for joining up this rust code with javascript.
|
2019-03-07 15:00:55 -03:00
|
|
|
#[cfg_attr(feature = "extra-traits", derive(Debug))]
|
2018-08-09 19:24:33 +03:00
|
|
|
#[derive(Default, Clone)]
|
2017-12-18 12:39:14 -08:00
|
|
|
pub struct Program {
|
2018-07-24 17:37:49 +01:00
|
|
|
/// rust -> js interfaces
|
2018-02-07 16:41:33 -08:00
|
|
|
pub exports: Vec<Export>,
|
2018-07-24 17:37:49 +01:00
|
|
|
/// js -> rust interfaces
|
2017-12-18 21:43:16 -08:00
|
|
|
pub imports: Vec<Import>,
|
2018-07-24 17:37:49 +01:00
|
|
|
/// rust enums
|
2018-02-22 00:55:11 +01:00
|
|
|
pub enums: Vec<Enum>,
|
2018-07-24 17:37:49 +01:00
|
|
|
/// rust structs
|
2018-02-07 16:41:33 -08:00
|
|
|
pub structs: Vec<Struct>,
|
2018-07-24 17:37:49 +01:00
|
|
|
/// rust consts
|
2018-07-13 22:36:51 -07:00
|
|
|
pub consts: Vec<Const>,
|
2018-08-14 10:16:18 -07:00
|
|
|
/// "dictionaries", generated for WebIDL, which are basically just "typed
|
|
|
|
/// objects" in the sense that they represent a JS object with a particular
|
|
|
|
/// shape in JIT parlance.
|
|
|
|
pub dictionaries: Vec<Dictionary>,
|
2018-11-17 23:04:19 -05:00
|
|
|
/// custom typescript sections to be included in the definition file
|
|
|
|
pub typescript_custom_sections: Vec<String>,
|
2019-02-25 11:11:30 -08:00
|
|
|
/// Inline JS snippets
|
|
|
|
pub inline_js: Vec<String>,
|
2017-12-18 12:39:14 -08:00
|
|
|
}
|
|
|
|
|
2018-07-24 17:37:49 +01:00
|
|
|
/// A rust to js interface. Allows interaction with rust objects/functions
|
|
|
|
/// from javascript.
|
2019-03-07 15:00:55 -03:00
|
|
|
#[cfg_attr(feature = "extra-traits", derive(Debug))]
|
2018-08-09 19:24:33 +03:00
|
|
|
#[derive(Clone)]
|
2018-02-07 16:41:33 -08:00
|
|
|
pub struct Export {
|
2018-11-05 12:29:14 -08:00
|
|
|
/// The struct name, in Rust, this is attached to
|
|
|
|
pub rust_class: Option<Ident>,
|
|
|
|
/// The class name in JS this is attached to
|
|
|
|
pub js_class: Option<String>,
|
2018-07-24 17:37:49 +01:00
|
|
|
/// The type of `self` (either `self`, `&self`, or `&mut self`)
|
2018-06-28 20:09:11 -05:00
|
|
|
pub method_self: Option<MethodSelf>,
|
2018-09-21 15:45:31 -07:00
|
|
|
/// Whether or not this export is flagged as a constructor, returning an
|
|
|
|
/// instance of the `impl` type
|
|
|
|
pub is_constructor: bool,
|
2018-07-24 17:37:49 +01:00
|
|
|
/// The rust function
|
2018-02-07 16:41:33 -08:00
|
|
|
pub function: Function,
|
2018-07-25 18:08:57 +01:00
|
|
|
/// Comments extracted from the rust source.
|
2018-06-15 11:20:56 -05:00
|
|
|
pub comments: Vec<String>,
|
2018-07-25 18:08:57 +01:00
|
|
|
/// The name of the rust function/method on the rust side.
|
2018-07-20 12:01:28 -05:00
|
|
|
pub rust_name: Ident,
|
2018-11-28 09:25:51 -08:00
|
|
|
/// Whether or not this function should be flagged as the wasm start
|
|
|
|
/// function.
|
|
|
|
pub start: bool,
|
2017-12-14 19:31:01 -08:00
|
|
|
}
|
|
|
|
|
2018-07-25 18:08:57 +01:00
|
|
|
/// The 3 types variations of `self`.
|
2018-06-28 20:09:11 -05:00
|
|
|
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
|
2018-08-09 19:24:33 +03:00
|
|
|
#[derive(Clone)]
|
2018-06-28 20:09:11 -05:00
|
|
|
pub enum MethodSelf {
|
2018-07-25 18:08:57 +01:00
|
|
|
/// `self`
|
2018-06-28 20:09:11 -05:00
|
|
|
ByValue,
|
2018-07-25 18:08:57 +01:00
|
|
|
/// `&mut self`
|
2018-06-28 20:09:11 -05:00
|
|
|
RefMutable,
|
2018-07-25 18:08:57 +01:00
|
|
|
/// `&self`
|
2018-06-28 20:09:11 -05:00
|
|
|
RefShared,
|
|
|
|
}
|
|
|
|
|
2019-03-07 15:00:55 -03:00
|
|
|
#[cfg_attr(feature = "extra-traits", derive(Debug))]
|
2018-08-09 19:24:33 +03:00
|
|
|
#[derive(Clone)]
|
2017-12-18 21:43:16 -08:00
|
|
|
pub struct Import {
|
2019-02-25 11:11:30 -08:00
|
|
|
pub module: ImportModule,
|
2018-05-21 07:29:34 -07:00
|
|
|
pub js_namespace: Option<Ident>,
|
2018-02-07 16:41:33 -08:00
|
|
|
pub kind: ImportKind,
|
2018-02-05 14:24:25 -08:00
|
|
|
}
|
|
|
|
|
2019-03-07 15:00:55 -03:00
|
|
|
#[cfg_attr(feature = "extra-traits", derive(Debug))]
|
2019-02-25 11:11:30 -08:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub enum ImportModule {
|
|
|
|
None,
|
|
|
|
Named(String, Span),
|
|
|
|
Inline(usize, Span),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Hash for ImportModule {
|
|
|
|
fn hash<H: Hasher>(&self, h: &mut H) {
|
|
|
|
match self {
|
|
|
|
ImportModule::None => {
|
|
|
|
0u8.hash(h);
|
|
|
|
}
|
|
|
|
ImportModule::Named(name, _) => {
|
|
|
|
1u8.hash(h);
|
|
|
|
name.hash(h);
|
|
|
|
}
|
|
|
|
ImportModule::Inline(idx, _) => {
|
|
|
|
2u8.hash(h);
|
|
|
|
idx.hash(h);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-07 15:00:55 -03:00
|
|
|
#[cfg_attr(feature = "extra-traits", derive(Debug))]
|
2018-08-09 19:24:33 +03:00
|
|
|
#[derive(Clone)]
|
2018-02-07 16:41:33 -08:00
|
|
|
pub enum ImportKind {
|
2018-03-21 09:55:16 -07:00
|
|
|
Function(ImportFunction),
|
|
|
|
Static(ImportStatic),
|
|
|
|
Type(ImportType),
|
2018-07-08 22:09:00 -04:00
|
|
|
Enum(ImportEnum),
|
2018-03-21 09:55:16 -07:00
|
|
|
}
|
|
|
|
|
2019-03-07 15:00:55 -03:00
|
|
|
#[cfg_attr(feature = "extra-traits", derive(Debug))]
|
2018-08-09 19:24:33 +03:00
|
|
|
#[derive(Clone)]
|
2018-03-21 09:55:16 -07:00
|
|
|
pub struct ImportFunction {
|
|
|
|
pub function: Function,
|
2018-05-21 07:29:34 -07:00
|
|
|
pub rust_name: Ident,
|
2018-06-14 02:03:52 -07:00
|
|
|
pub js_ret: Option<syn::Type>,
|
2018-07-07 10:20:31 -07:00
|
|
|
pub catch: bool,
|
2018-08-18 22:15:29 +01:00
|
|
|
pub variadic: bool,
|
2018-07-07 10:20:31 -07:00
|
|
|
pub structural: bool,
|
2018-03-21 09:55:16 -07:00
|
|
|
pub kind: ImportFunctionKind,
|
2018-05-21 07:29:34 -07:00
|
|
|
pub shim: Ident,
|
2018-07-29 17:12:36 +01:00
|
|
|
pub doc_comment: Option<String>,
|
2018-03-21 09:55:16 -07:00
|
|
|
}
|
|
|
|
|
2018-05-29 11:24:40 -07:00
|
|
|
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
|
2018-08-09 19:24:33 +03:00
|
|
|
#[derive(Clone)]
|
2018-03-21 09:55:16 -07:00
|
|
|
pub enum ImportFunctionKind {
|
2018-06-14 19:21:33 -07:00
|
|
|
Method {
|
|
|
|
class: String,
|
|
|
|
ty: syn::Type,
|
|
|
|
kind: MethodKind,
|
|
|
|
},
|
2018-02-07 16:41:33 -08:00
|
|
|
Normal,
|
2018-02-05 14:24:25 -08:00
|
|
|
}
|
|
|
|
|
2018-06-14 19:21:33 -07:00
|
|
|
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
|
2018-08-09 19:24:33 +03:00
|
|
|
#[derive(Clone)]
|
2018-06-14 19:21:33 -07:00
|
|
|
pub enum MethodKind {
|
|
|
|
Constructor,
|
2018-07-07 10:20:31 -07:00
|
|
|
Operation(Operation),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
|
2018-08-09 19:24:33 +03:00
|
|
|
#[derive(Clone)]
|
2018-07-07 10:20:31 -07:00
|
|
|
pub struct Operation {
|
|
|
|
pub is_static: bool,
|
|
|
|
pub kind: OperationKind,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
|
2018-08-09 19:24:33 +03:00
|
|
|
#[derive(Clone)]
|
2018-07-07 10:20:31 -07:00
|
|
|
pub enum OperationKind {
|
|
|
|
Regular,
|
|
|
|
Getter(Option<Ident>),
|
2018-08-05 23:32:31 +03:00
|
|
|
Setter(Option<Ident>),
|
2018-08-07 00:06:04 +03:00
|
|
|
IndexingGetter,
|
|
|
|
IndexingSetter,
|
|
|
|
IndexingDeleter,
|
2018-06-14 19:21:33 -07:00
|
|
|
}
|
|
|
|
|
2018-05-29 11:24:40 -07:00
|
|
|
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
|
2018-08-09 19:24:33 +03:00
|
|
|
#[derive(Clone)]
|
2018-03-21 09:55:16 -07:00
|
|
|
pub struct ImportStatic {
|
|
|
|
pub vis: syn::Visibility,
|
|
|
|
pub ty: syn::Type,
|
2018-05-21 07:29:34 -07:00
|
|
|
pub shim: Ident,
|
|
|
|
pub rust_name: Ident,
|
2018-08-05 00:17:30 +02:00
|
|
|
pub js_name: String,
|
2018-03-21 09:55:16 -07:00
|
|
|
}
|
|
|
|
|
2018-05-29 11:24:40 -07:00
|
|
|
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
|
2018-08-09 19:24:33 +03:00
|
|
|
#[derive(Clone)]
|
2018-03-21 09:55:16 -07:00
|
|
|
pub struct ImportType {
|
|
|
|
pub vis: syn::Visibility,
|
2018-08-07 16:09:38 -07:00
|
|
|
pub rust_name: Ident,
|
2018-08-07 16:18:13 -07:00
|
|
|
pub js_name: String,
|
2018-07-05 20:28:52 -07:00
|
|
|
pub attrs: Vec<syn::Attribute>,
|
2018-07-29 17:12:36 +01:00
|
|
|
pub doc_comment: Option<String>,
|
2018-08-04 09:41:59 -07:00
|
|
|
pub instanceof_shim: String,
|
2018-10-03 00:04:43 -07:00
|
|
|
pub extends: Vec<syn::Path>,
|
2018-10-01 12:33:33 -07:00
|
|
|
pub vendor_prefixes: Vec<Ident>,
|
2018-03-21 09:55:16 -07:00
|
|
|
}
|
|
|
|
|
2018-07-08 22:09:00 -04:00
|
|
|
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
|
2018-08-09 19:24:33 +03:00
|
|
|
#[derive(Clone)]
|
2018-07-08 22:09:00 -04:00
|
|
|
pub struct ImportEnum {
|
|
|
|
/// The Rust enum's visibility
|
|
|
|
pub vis: syn::Visibility,
|
|
|
|
/// The Rust enum's identifiers
|
|
|
|
pub name: Ident,
|
|
|
|
/// The Rust identifiers for the variants
|
|
|
|
pub variants: Vec<Ident>,
|
|
|
|
/// The JS string values of the variants
|
|
|
|
pub variant_values: Vec<String>,
|
2018-07-23 11:04:28 -04:00
|
|
|
/// Attributes to apply to the Rust enum
|
|
|
|
pub rust_attrs: Vec<syn::Attribute>,
|
2018-07-08 22:09:00 -04:00
|
|
|
}
|
|
|
|
|
2019-03-07 15:00:55 -03:00
|
|
|
#[cfg_attr(feature = "extra-traits", derive(Debug))]
|
2018-08-09 19:24:33 +03:00
|
|
|
#[derive(Clone)]
|
2018-02-07 16:41:33 -08:00
|
|
|
pub struct Function {
|
2018-08-05 00:17:30 +02:00
|
|
|
pub name: String,
|
2018-09-21 17:29:50 -07:00
|
|
|
pub name_span: Span,
|
2018-09-21 17:34:41 -07:00
|
|
|
pub renamed_via_js_name: bool,
|
2018-06-14 02:03:52 -07:00
|
|
|
pub arguments: Vec<syn::ArgCaptured>,
|
2018-04-13 22:58:35 -07:00
|
|
|
pub ret: Option<syn::Type>,
|
2018-02-07 16:41:33 -08:00
|
|
|
pub rust_attrs: Vec<syn::Attribute>,
|
|
|
|
pub rust_vis: syn::Visibility,
|
2018-02-06 15:19:47 -08:00
|
|
|
}
|
|
|
|
|
2018-05-29 11:24:40 -07:00
|
|
|
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
|
2018-08-09 19:24:33 +03:00
|
|
|
#[derive(Clone)]
|
2018-02-07 16:41:33 -08:00
|
|
|
pub struct Struct {
|
2018-11-05 12:29:14 -08:00
|
|
|
pub rust_name: Ident,
|
|
|
|
pub js_name: String,
|
2018-04-19 16:49:46 -07:00
|
|
|
pub fields: Vec<StructField>,
|
2018-06-15 11:20:56 -05:00
|
|
|
pub comments: Vec<String>,
|
2018-04-19 16:49:46 -07:00
|
|
|
}
|
|
|
|
|
2018-05-29 11:24:40 -07:00
|
|
|
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
|
2018-08-09 19:24:33 +03:00
|
|
|
#[derive(Clone)]
|
2018-04-19 16:49:46 -07:00
|
|
|
pub struct StructField {
|
2018-05-21 07:29:34 -07:00
|
|
|
pub name: Ident,
|
|
|
|
pub struct_name: Ident,
|
2018-07-07 10:20:31 -07:00
|
|
|
pub readonly: bool,
|
2018-04-19 16:49:46 -07:00
|
|
|
pub ty: syn::Type,
|
2018-05-21 07:29:34 -07:00
|
|
|
pub getter: Ident,
|
|
|
|
pub setter: Ident,
|
2018-06-15 11:20:56 -05:00
|
|
|
pub comments: Vec<String>,
|
2017-12-18 21:43:16 -08:00
|
|
|
}
|
|
|
|
|
2018-05-29 11:24:40 -07:00
|
|
|
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
|
2018-08-09 19:24:33 +03:00
|
|
|
#[derive(Clone)]
|
2018-02-22 00:55:11 +01:00
|
|
|
pub struct Enum {
|
2018-05-21 07:29:34 -07:00
|
|
|
pub name: Ident,
|
2018-03-29 09:14:32 -07:00
|
|
|
pub variants: Vec<Variant>,
|
2018-06-15 11:20:56 -05:00
|
|
|
pub comments: Vec<String>,
|
2019-01-28 14:12:02 -08:00
|
|
|
pub hole: u32,
|
2018-03-07 16:28:42 -08:00
|
|
|
}
|
|
|
|
|
2018-05-29 11:24:40 -07:00
|
|
|
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
|
2018-08-09 19:24:33 +03:00
|
|
|
#[derive(Clone)]
|
2018-03-07 16:28:42 -08:00
|
|
|
pub struct Variant {
|
2018-05-21 07:29:34 -07:00
|
|
|
pub name: Ident,
|
2018-03-07 16:28:42 -08:00
|
|
|
pub value: u32,
|
2018-02-22 00:55:11 +01:00
|
|
|
}
|
|
|
|
|
2018-05-25 16:31:48 -07:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
2018-04-04 08:24:19 -07:00
|
|
|
pub enum TypeKind {
|
|
|
|
ByRef,
|
|
|
|
ByMutRef,
|
|
|
|
ByValue,
|
|
|
|
}
|
|
|
|
|
2018-05-25 16:31:48 -07:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
2018-04-04 08:24:19 -07:00
|
|
|
pub enum TypeLocation {
|
|
|
|
ImportArgument,
|
|
|
|
ImportRet,
|
|
|
|
ExportArgument,
|
|
|
|
ExportRet,
|
2017-12-18 12:39:14 -08:00
|
|
|
}
|
|
|
|
|
2018-07-17 16:28:44 +01:00
|
|
|
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq))]
|
2018-08-09 19:24:33 +03:00
|
|
|
#[derive(Clone)]
|
2018-07-13 04:16:19 +02:00
|
|
|
pub struct Const {
|
|
|
|
pub vis: syn::Visibility,
|
|
|
|
pub name: Ident,
|
2018-07-13 22:36:51 -07:00
|
|
|
pub class: Option<Ident>,
|
2018-07-13 04:16:19 +02:00
|
|
|
pub ty: syn::Type,
|
|
|
|
pub value: ConstValue,
|
|
|
|
}
|
|
|
|
|
2018-07-17 16:28:44 +01:00
|
|
|
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq))]
|
2018-08-09 19:24:33 +03:00
|
|
|
#[derive(Clone)]
|
2018-07-13 04:16:19 +02:00
|
|
|
/// same as webidl::ast::ConstValue
|
|
|
|
pub enum ConstValue {
|
|
|
|
BooleanLiteral(bool),
|
|
|
|
FloatLiteral(f64),
|
2018-07-14 14:48:04 +02:00
|
|
|
SignedIntegerLiteral(i64),
|
|
|
|
UnsignedIntegerLiteral(u64),
|
2018-07-13 04:16:19 +02:00
|
|
|
Null,
|
|
|
|
}
|
|
|
|
|
2018-08-14 10:16:18 -07:00
|
|
|
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Dictionary {
|
|
|
|
pub name: Ident,
|
|
|
|
pub fields: Vec<DictionaryField>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct DictionaryField {
|
2019-01-07 14:15:00 +00:00
|
|
|
pub rust_name: Ident,
|
|
|
|
pub js_name: String,
|
2018-08-14 10:16:18 -07:00
|
|
|
pub required: bool,
|
|
|
|
pub ty: syn::Type,
|
|
|
|
}
|
|
|
|
|
2018-02-07 16:41:33 -08:00
|
|
|
impl Export {
|
2018-07-24 17:37:49 +01:00
|
|
|
/// Mangles a rust -> javascript export, so that the created Ident will be unique over function
|
|
|
|
/// name and class name, if the function belongs to a javascript class.
|
2018-07-07 10:20:31 -07:00
|
|
|
pub(crate) fn rust_symbol(&self) -> Ident {
|
2018-07-15 12:43:55 -04:00
|
|
|
let mut generated_name = String::from("__wasm_bindgen_generated");
|
2018-11-05 12:29:14 -08:00
|
|
|
if let Some(class) = &self.js_class {
|
2018-02-07 16:41:33 -08:00
|
|
|
generated_name.push_str("_");
|
2018-11-05 12:29:14 -08:00
|
|
|
generated_name.push_str(class);
|
2017-12-18 12:39:14 -08:00
|
|
|
}
|
2018-02-07 16:41:33 -08:00
|
|
|
generated_name.push_str("_");
|
2018-05-21 07:29:34 -07:00
|
|
|
generated_name.push_str(&self.function.name.to_string());
|
|
|
|
Ident::new(&generated_name, Span::call_site())
|
2017-12-18 12:39:14 -08:00
|
|
|
}
|
|
|
|
|
2018-07-25 18:08:57 +01:00
|
|
|
/// This is the name of the shim function that gets exported and takes the raw
|
|
|
|
/// ABI form of its arguments and converts them back into their normal,
|
|
|
|
/// "high level" form before calling the actual function.
|
2018-07-07 10:20:31 -07:00
|
|
|
pub(crate) fn export_name(&self) -> String {
|
2018-05-21 07:29:34 -07:00
|
|
|
let fn_name = self.function.name.to_string();
|
2018-11-05 12:29:14 -08:00
|
|
|
match &self.js_class {
|
|
|
|
Some(class) => shared::struct_function_export_name(class, &fn_name),
|
2018-05-21 07:29:34 -07:00
|
|
|
None => shared::free_function_export_name(&fn_name),
|
Overhaul how type information gets to the CLI
This commit is a complete overhaul of how the `#[wasm_bindgen]` macro
communicates type information to the CLI tool, and it's done in a somewhat...
unconventional fashion.
Today we've got a problem where the generated JS needs to understand the types
of each function exported or imported. This understanding is what enables it to
generate the appropriate JS wrappers and such. We want to, however, be quite
flexible and extensible in types that are supported across the boundary, which
means that internally we rely on the trait system to resolve what's what.
Communicating the type information historically was done by creating a four byte
"descriptor" and using associated type projections to communicate that to the
CLI tool. Unfortunately four bytes isn't a lot of space to cram information like
arguments to a generic function, tuple types, etc. In general this just wasn't
flexible enough and the way custom references were treated was also already a
bit of a hack.
This commit takes a radical step of creating a **descriptor function** for each
function imported/exported. The really crazy part is that the `wasm-bindgen` CLI
tool now embeds a wasm interpreter and executes these functions when the CLI
tool is invoked. By allowing arbitrary functions to get executed it's now *much*
easier to inform `wasm-bindgen` about complicated structures of types. Rest
assured though that all these descriptor functions are automatically unexported
and gc'd away, so this should not have any impact on binary sizes
A new internal trait, `WasmDescribe`, is added to represent a description of all
types, sort of like a serialization of the structure of a type that
`wasm-bindgen` can understand. This works by calling a special exported function
with a `u32` value a bunch of times. This means that when we run a descriptor we
effectively get a `Vec<u32>` in the `wasm-bindgen` CLI tool. This list of
integers can then be parsed into a rich `enum` for the JS generation to work
with.
This commit currently only retains feature parity with the previous
implementation. I hope to soon solve issues like #123, #104, and #111 with this
support.
2018-04-13 07:33:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ImportKind {
|
2018-07-24 17:37:49 +01:00
|
|
|
/// Whether this type can be inside an `impl` block.
|
2018-05-11 08:28:09 -07:00
|
|
|
pub fn fits_on_impl(&self) -> bool {
|
|
|
|
match *self {
|
|
|
|
ImportKind::Function(_) => true,
|
|
|
|
ImportKind::Static(_) => false,
|
|
|
|
ImportKind::Type(_) => false,
|
2018-07-08 22:09:00 -04:00
|
|
|
ImportKind::Enum(_) => false,
|
2018-05-11 08:28:09 -07:00
|
|
|
}
|
|
|
|
}
|
2017-12-14 19:31:01 -08:00
|
|
|
}
|
2018-02-05 14:24:25 -08:00
|
|
|
|
2018-03-21 09:55:16 -07:00
|
|
|
impl ImportFunction {
|
2018-07-24 17:37:49 +01:00
|
|
|
/// If the rust object has a `fn xxx(&self) -> MyType` method, get the name for a getter in
|
|
|
|
/// javascript (in this case `xxx`, so you can write `val = obj.xxx`)
|
2018-08-26 15:43:33 -07:00
|
|
|
pub fn infer_getter_property(&self) -> &str {
|
|
|
|
&self.function.name
|
2018-02-14 13:16:02 -08:00
|
|
|
}
|
|
|
|
|
2018-07-24 17:37:49 +01:00
|
|
|
/// If the rust object has a `fn set_xxx(&mut self, MyType)` style method, get the name
|
|
|
|
/// for a setter in javascript (in this case `xxx`, so you can write `obj.xxx = val`)
|
2018-08-26 15:43:33 -07:00
|
|
|
pub fn infer_setter_property(&self) -> Result<String, Diagnostic> {
|
2018-05-21 07:29:34 -07:00
|
|
|
let name = self.function.name.to_string();
|
2018-09-21 17:34:41 -07:00
|
|
|
|
|
|
|
// if `#[wasm_bindgen(js_name = "...")]` is used then that explicitly
|
|
|
|
// because it was hand-written anyway.
|
|
|
|
if self.function.renamed_via_js_name {
|
2018-09-26 08:26:00 -07:00
|
|
|
return Ok(name);
|
2018-09-21 17:34:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise we infer names based on the Rust function name.
|
2018-08-10 13:15:12 -07:00
|
|
|
if !name.starts_with("set_") {
|
2018-09-21 17:29:50 -07:00
|
|
|
bail_span!(
|
|
|
|
syn::token::Pub(self.function.name_span),
|
|
|
|
"setters must start with `set_`, found: {}",
|
|
|
|
name,
|
|
|
|
);
|
2018-08-10 13:15:12 -07:00
|
|
|
}
|
2018-09-21 17:29:50 -07:00
|
|
|
Ok(name[4..].to_string())
|
2018-02-14 13:16:02 -08:00
|
|
|
}
|
2018-02-05 14:24:25 -08:00
|
|
|
}
|