mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-03-16 02:00:51 +00:00
Move some utility functions from the webidl crate into the backend crate
This commit is contained in:
parent
eb04d15a65
commit
8fbf478058
@ -1,8 +1,8 @@
|
||||
use proc_macro2::{Ident, Span, TokenStream, TokenTree};
|
||||
use quote::ToTokens;
|
||||
use shared;
|
||||
use std::iter::FromIterator;
|
||||
use syn;
|
||||
use util;
|
||||
|
||||
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
|
||||
#[derive(Default)]
|
||||
@ -416,20 +416,7 @@ impl Program {
|
||||
} else if let Some(cls) = wasm.opts.static_method_of() {
|
||||
let class = cls.to_string();
|
||||
let kind = MethodKind::Static;
|
||||
|
||||
let segments = syn::punctuated::Punctuated::from_iter(Some(syn::PathSegment {
|
||||
ident: cls.clone(),
|
||||
arguments: syn::PathArguments::None,
|
||||
}));
|
||||
|
||||
let ty = syn::Type::Path(syn::TypePath {
|
||||
qself: None,
|
||||
path: syn::Path {
|
||||
leading_colon: None,
|
||||
segments,
|
||||
},
|
||||
});
|
||||
|
||||
let ty = util::ident_ty(cls.clone());
|
||||
ImportFunctionKind::Method { class, ty, kind }
|
||||
} else if wasm.opts.constructor() {
|
||||
let class = match wasm.ret {
|
||||
|
@ -12,3 +12,4 @@ extern crate wasm_bindgen_shared as shared;
|
||||
|
||||
pub mod ast;
|
||||
mod codegen;
|
||||
pub mod util;
|
||||
|
69
crates/backend/src/util.rs
Normal file
69
crates/backend/src/util.rs
Normal file
@ -0,0 +1,69 @@
|
||||
use std::iter::FromIterator;
|
||||
|
||||
use ast;
|
||||
use proc_macro2::{self, Ident};
|
||||
use syn;
|
||||
|
||||
fn is_rust_keyword(name: &str) -> bool {
|
||||
match name {
|
||||
"abstract" | "alignof" | "as" | "become" | "box" | "break" | "const" | "continue"
|
||||
| "crate" | "do" | "else" | "enum" | "extern" | "false" | "final" | "fn" | "for" | "if"
|
||||
| "impl" | "in" | "let" | "loop" | "macro" | "match" | "mod" | "move" | "mut"
|
||||
| "offsetof" | "override" | "priv" | "proc" | "pub" | "pure" | "ref" | "return"
|
||||
| "Self" | "self" | "sizeof" | "static" | "struct" | "super" | "trait" | "true"
|
||||
| "type" | "typeof" | "unsafe" | "unsized" | "use" | "virtual" | "where" | "while"
|
||||
| "yield" | "bool" | "_" => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
// Create an `Ident`, possibly mangling it if it conflicts with a Rust keyword.
|
||||
pub fn rust_ident(name: &str) -> Ident {
|
||||
if is_rust_keyword(name) {
|
||||
Ident::new(&format!("{}_", name), proc_macro2::Span::call_site())
|
||||
} else {
|
||||
raw_ident(name)
|
||||
}
|
||||
}
|
||||
|
||||
// Create an `Ident` without checking to see if it conflicts with a Rust
|
||||
// keyword.
|
||||
pub fn raw_ident(name: &str) -> Ident {
|
||||
Ident::new(name, proc_macro2::Span::call_site())
|
||||
}
|
||||
|
||||
/// Create a path type from the given segments. For example an iterator yielding
|
||||
/// the idents `[foo, bar, baz]` will result in the path type `foo::bar::baz`.
|
||||
pub fn simple_path_ty<I>(segments: I) -> syn::Type
|
||||
where
|
||||
I: IntoIterator<Item = Ident>,
|
||||
{
|
||||
let segments: Vec<_> = segments
|
||||
.into_iter()
|
||||
.map(|i| syn::PathSegment {
|
||||
ident: i,
|
||||
arguments: syn::PathArguments::None,
|
||||
})
|
||||
.collect();
|
||||
|
||||
syn::TypePath {
|
||||
qself: None,
|
||||
path: syn::Path {
|
||||
leading_colon: None,
|
||||
segments: syn::punctuated::Punctuated::from_iter(segments),
|
||||
},
|
||||
}.into()
|
||||
}
|
||||
|
||||
pub fn ident_ty(ident: Ident) -> syn::Type {
|
||||
simple_path_ty(Some(ident))
|
||||
}
|
||||
|
||||
pub fn wrap_import_function(function: ast::ImportFunction) -> ast::Import {
|
||||
ast::Import {
|
||||
module: None,
|
||||
version: None,
|
||||
js_namespace: None,
|
||||
kind: ast::ImportKind::Function(function),
|
||||
}
|
||||
}
|
@ -18,19 +18,19 @@ extern crate syn;
|
||||
extern crate wasm_bindgen_backend as backend;
|
||||
extern crate webidl;
|
||||
|
||||
mod util;
|
||||
|
||||
use std::fs;
|
||||
use std::io::{self, Read};
|
||||
use std::iter;
|
||||
use std::path::Path;
|
||||
|
||||
use backend::util::{ident_ty, rust_ident, wrap_import_function};
|
||||
use failure::ResultExt;
|
||||
use quote::ToTokens;
|
||||
|
||||
mod util;
|
||||
|
||||
use util::{
|
||||
create_basic_method, create_function, create_getter, create_setter, ident_ty, rust_ident,
|
||||
webidl_ty_to_syn_ty, wrap_import_function, TypePosition,
|
||||
create_basic_method, create_function, create_getter, create_setter, webidl_ty_to_syn_ty,
|
||||
TypePosition,
|
||||
};
|
||||
|
||||
/// Either `Ok(t)` or `Err(failure::Error)`.
|
||||
|
@ -1,64 +1,12 @@
|
||||
use std::iter::{self, FromIterator};
|
||||
use std::iter;
|
||||
|
||||
use backend;
|
||||
use backend::util::{ident_ty, raw_ident, rust_ident, simple_path_ty};
|
||||
use heck::SnakeCase;
|
||||
use proc_macro2::{self, Ident};
|
||||
use proc_macro2::Ident;
|
||||
use syn;
|
||||
use webidl;
|
||||
|
||||
fn is_rust_keyword(name: &str) -> bool {
|
||||
match name {
|
||||
"abstract" | "alignof" | "as" | "become" | "box" | "break" | "const" | "continue"
|
||||
| "crate" | "do" | "else" | "enum" | "extern" | "false" | "final" | "fn" | "for" | "if"
|
||||
| "impl" | "in" | "let" | "loop" | "macro" | "match" | "mod" | "move" | "mut"
|
||||
| "offsetof" | "override" | "priv" | "proc" | "pub" | "pure" | "ref" | "return"
|
||||
| "Self" | "self" | "sizeof" | "static" | "struct" | "super" | "trait" | "true"
|
||||
| "type" | "typeof" | "unsafe" | "unsized" | "use" | "virtual" | "where" | "while"
|
||||
| "yield" | "bool" | "_" => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
// Create an `Ident`, possibly mangling it if it conflicts with a Rust keyword.
|
||||
pub fn rust_ident(name: &str) -> Ident {
|
||||
if is_rust_keyword(name) {
|
||||
Ident::new(&format!("{}_", name), proc_macro2::Span::call_site())
|
||||
} else {
|
||||
raw_ident(name)
|
||||
}
|
||||
}
|
||||
|
||||
// Create an `Ident` without checking to see if it conflicts with a Rust
|
||||
// keyword.
|
||||
fn raw_ident(name: &str) -> Ident {
|
||||
Ident::new(name, proc_macro2::Span::call_site())
|
||||
}
|
||||
|
||||
fn simple_path_ty<I>(segments: I) -> syn::Type
|
||||
where
|
||||
I: IntoIterator<Item = Ident>,
|
||||
{
|
||||
let segments: Vec<_> = segments
|
||||
.into_iter()
|
||||
.map(|i| syn::PathSegment {
|
||||
ident: i,
|
||||
arguments: syn::PathArguments::None,
|
||||
})
|
||||
.collect();
|
||||
|
||||
syn::TypePath {
|
||||
qself: None,
|
||||
path: syn::Path {
|
||||
leading_colon: None,
|
||||
segments: syn::punctuated::Punctuated::from_iter(segments),
|
||||
},
|
||||
}.into()
|
||||
}
|
||||
|
||||
pub fn ident_ty(ident: Ident) -> syn::Type {
|
||||
simple_path_ty(Some(ident))
|
||||
}
|
||||
|
||||
fn shared_ref(ty: syn::Type) -> syn::Type {
|
||||
syn::TypeReference {
|
||||
and_token: Default::default(),
|
||||
@ -337,12 +285,3 @@ pub fn create_setter(
|
||||
vec![backend::ast::BindgenAttr::Setter(Some(raw_ident(name)))],
|
||||
)
|
||||
}
|
||||
|
||||
pub fn wrap_import_function(function: backend::ast::ImportFunction) -> backend::ast::Import {
|
||||
backend::ast::Import {
|
||||
module: None,
|
||||
version: None,
|
||||
js_namespace: None,
|
||||
kind: backend::ast::ImportKind::Function(function),
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user