2017-12-14 19:31:01 -08:00
|
|
|
#[macro_use]
|
|
|
|
extern crate serde_derive;
|
|
|
|
|
2017-12-18 12:39:14 -08:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct Program {
|
|
|
|
pub structs: Vec<Struct>,
|
|
|
|
pub free_functions: Vec<Function>,
|
2018-01-29 21:20:38 -08:00
|
|
|
pub imports: Vec<(String, Function)>,
|
2017-12-18 12:39:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct Struct {
|
|
|
|
pub name: String,
|
|
|
|
pub functions: Vec<Function>,
|
|
|
|
pub methods: Vec<Method>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct Method {
|
|
|
|
pub mutable: bool,
|
|
|
|
pub function: Function,
|
|
|
|
}
|
|
|
|
|
2017-12-14 19:31:01 -08:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct Function {
|
|
|
|
pub name: String,
|
|
|
|
pub arguments: Vec<Type>,
|
|
|
|
pub ret: Option<Type>,
|
|
|
|
}
|
|
|
|
|
2017-12-18 14:31:01 -08:00
|
|
|
impl Struct {
|
|
|
|
pub fn free_function(&self) -> String {
|
|
|
|
let mut name = format!("__wbindgen_");
|
|
|
|
name.extend(self.name
|
|
|
|
.chars()
|
|
|
|
.flat_map(|s| s.to_lowercase()));
|
|
|
|
name.push_str("_free");
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Function {
|
|
|
|
pub fn free_function_export_name(&self) -> String {
|
|
|
|
self.name.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn struct_function_export_name(&self, struct_: &str) -> String {
|
|
|
|
let mut name = struct_
|
|
|
|
.chars()
|
|
|
|
.flat_map(|s| s.to_lowercase())
|
|
|
|
.collect::<String>();
|
|
|
|
name.push_str("_");
|
|
|
|
name.push_str(&self.name);
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 19:31:01 -08:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub enum Type {
|
|
|
|
Number,
|
2017-12-14 21:55:21 -08:00
|
|
|
BorrowedStr,
|
|
|
|
String,
|
2017-12-18 12:39:14 -08:00
|
|
|
ByValue(String),
|
|
|
|
ByRef(String),
|
|
|
|
ByMutRef(String),
|
2017-12-19 09:25:41 -08:00
|
|
|
JsObject,
|
|
|
|
JsObjectRef,
|
2017-12-20 10:22:18 -08:00
|
|
|
Boolean,
|
2017-12-14 21:55:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Type {
|
|
|
|
pub fn is_number(&self) -> bool {
|
|
|
|
match *self {
|
|
|
|
Type::Number => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
2017-12-14 19:31:01 -08:00
|
|
|
}
|