mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-28 04:01:33 +00:00
Greatly simplify handling of types in Rust
Push the compiler to do trait resolution to figure out what each type is bound with in JS, and that way we can accept effectively all types (so long as they implement a trait).
This commit is contained in:
@ -5,8 +5,9 @@ extern crate serde_derive;
|
||||
pub struct Program {
|
||||
pub structs: Vec<Struct>,
|
||||
pub free_functions: Vec<Function>,
|
||||
pub imports: Vec<(String, Function)>,
|
||||
pub imports: Vec<Import>,
|
||||
pub imported_structs: Vec<ImportStruct>,
|
||||
pub custom_type_names: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
@ -16,11 +17,23 @@ pub struct Struct {
|
||||
pub methods: Vec<Method>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Import {
|
||||
pub module: String,
|
||||
pub function: Function,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct ImportStruct {
|
||||
pub module: Option<String>,
|
||||
pub name: String,
|
||||
pub functions: Vec<(bool, Function)>,
|
||||
pub functions: Vec<ImportStructFunction>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct ImportStructFunction {
|
||||
pub method: bool,
|
||||
pub function: Function,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
@ -36,58 +49,66 @@ pub struct Function {
|
||||
pub ret: Option<Type>,
|
||||
}
|
||||
|
||||
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
|
||||
pub fn free_function(struct_name: &str) -> String {
|
||||
let mut name = format!("__wbindgen_");
|
||||
name.extend(struct_name
|
||||
.chars()
|
||||
.flat_map(|s| s.to_lowercase()));
|
||||
name.push_str("_free");
|
||||
return name
|
||||
}
|
||||
|
||||
pub fn free_function_export_name(function_name: &str) -> String {
|
||||
function_name.to_string()
|
||||
}
|
||||
|
||||
pub fn struct_function_export_name(struct_: &str, f: &str) -> String {
|
||||
let mut name = struct_
|
||||
.chars()
|
||||
.flat_map(|s| s.to_lowercase())
|
||||
.collect::<String>();
|
||||
name.push_str("_");
|
||||
name.push_str(f);
|
||||
return name
|
||||
}
|
||||
|
||||
pub fn mangled_import_name(struct_: Option<&str>, f: &str) -> String {
|
||||
match struct_ {
|
||||
Some(s) => format!("__wbg_s_{}_{}", s, f),
|
||||
None => format!("__wbg_f_{}", f),
|
||||
}
|
||||
}
|
||||
|
||||
impl Function {
|
||||
pub fn free_function_export_name(&self) -> String {
|
||||
self.name.clone()
|
||||
}
|
||||
pub type Type = char;
|
||||
|
||||
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
|
||||
}
|
||||
pub const TYPE_NUMBER: char = '\u{5e}';
|
||||
pub const TYPE_BORROWED_STR: char = '\u{5f}';
|
||||
pub const TYPE_STRING: char = '\u{60}';
|
||||
pub const TYPE_BOOLEAN: char = '\u{61}';
|
||||
pub const TYPE_JS_OWNED: char = '\u{62}';
|
||||
pub const TYPE_JS_REF: char = '\u{63}';
|
||||
|
||||
pub fn mangled_import_name(&self, struct_: Option<&str>) -> String {
|
||||
match struct_ {
|
||||
Some(s) => format!("__wbg_s_{}_{}", s, self.name),
|
||||
None => format!("__wbg_f_{}", self.name),
|
||||
}
|
||||
}
|
||||
}
|
||||
pub const TYPE_CUSTOM_START: u32 = 0x64;
|
||||
pub const TYPE_CUSTOM_REF_FLAG: u32 = 1;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub enum Type {
|
||||
Number,
|
||||
BorrowedStr,
|
||||
String,
|
||||
ByValue(String),
|
||||
ByRef(String),
|
||||
ByMutRef(String),
|
||||
JsObject,
|
||||
JsObjectRef,
|
||||
Boolean,
|
||||
}
|
||||
// #[derive(Serialize, Deserialize)]
|
||||
// pub enum Type {
|
||||
// Number,
|
||||
// BorrowedStr,
|
||||
// String,
|
||||
// ByValue(String), // wrapper class
|
||||
// ByRef(String), // wrapper class
|
||||
// ByMutRef(String), // wrapper class
|
||||
// JsObject,
|
||||
// JsObjectRef,
|
||||
// Boolean,
|
||||
// }
|
||||
|
||||
impl Type {
|
||||
pub fn is_number(&self) -> bool {
|
||||
match *self {
|
||||
Type::Number => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
// impl Type {
|
||||
// pub fn is_number(&self) -> bool {
|
||||
// match *self {
|
||||
// Type::Number => true,
|
||||
// _ => false,
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
Reference in New Issue
Block a user