2017-12-14 19:31:01 -08:00
|
|
|
#[macro_use]
|
|
|
|
extern crate serde_derive;
|
2018-02-06 16:06:21 -08:00
|
|
|
extern crate fnv;
|
|
|
|
|
|
|
|
use std::hash::{Hash, Hasher};
|
2017-12-14 19:31:01 -08:00
|
|
|
|
2018-03-22 19:07:20 -07:00
|
|
|
pub const SCHEMA_VERSION: &str = "1";
|
2018-03-05 20:05:44 -08:00
|
|
|
|
2018-02-07 16:41:33 -08:00
|
|
|
#[derive(Deserialize)]
|
2017-12-18 12:39:14 -08:00
|
|
|
pub struct Program {
|
2018-02-07 16:41:33 -08:00
|
|
|
pub exports: Vec<Export>,
|
2018-02-22 12:01:38 +01:00
|
|
|
pub enums: Vec<Enum>,
|
2018-03-21 09:55:16 -07:00
|
|
|
pub imports: Vec<Import>,
|
2018-02-06 16:06:21 -08:00
|
|
|
pub custom_type_names: Vec<CustomTypeName>,
|
2018-03-01 19:32:05 -08:00
|
|
|
pub version: String,
|
2018-03-05 20:05:44 -08:00
|
|
|
pub schema_version: String,
|
2017-12-18 12:39:14 -08:00
|
|
|
}
|
|
|
|
|
2018-02-07 16:41:33 -08:00
|
|
|
#[derive(Deserialize)]
|
2018-02-06 07:56:14 -08:00
|
|
|
pub struct Import {
|
2018-03-21 09:55:16 -07:00
|
|
|
pub module: Option<String>,
|
2018-03-22 16:59:48 -07:00
|
|
|
pub js_namespace: Option<String>,
|
2018-03-21 09:55:16 -07:00
|
|
|
pub kind: ImportKind,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(tag = "kind", rename_all = "lowercase")]
|
|
|
|
pub enum ImportKind {
|
|
|
|
Function(ImportFunction),
|
|
|
|
Static(ImportStatic),
|
|
|
|
Type(ImportType),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct ImportFunction {
|
2018-03-22 19:05:14 -07:00
|
|
|
pub shim: String,
|
2018-02-05 14:24:25 -08:00
|
|
|
pub module: Option<String>,
|
2018-02-07 16:41:33 -08:00
|
|
|
pub catch: bool,
|
2018-02-06 07:56:14 -08:00
|
|
|
pub method: bool,
|
2018-02-06 15:19:47 -08:00
|
|
|
pub js_new: bool,
|
2018-03-22 17:37:27 -07:00
|
|
|
pub structural: bool,
|
2018-02-14 13:16:02 -08:00
|
|
|
pub getter: Option<String>,
|
|
|
|
pub setter: Option<String>,
|
2018-02-07 16:41:33 -08:00
|
|
|
pub class: Option<String>,
|
2018-02-06 07:56:14 -08:00
|
|
|
pub function: Function,
|
2018-02-05 14:24:25 -08:00
|
|
|
}
|
|
|
|
|
2018-03-21 08:09:59 -07:00
|
|
|
#[derive(Deserialize)]
|
2018-03-21 09:55:16 -07:00
|
|
|
pub struct ImportStatic {
|
2018-03-21 08:09:59 -07:00
|
|
|
pub module: Option<String>,
|
|
|
|
pub name: String,
|
2018-03-22 19:05:14 -07:00
|
|
|
pub shim: String,
|
2018-03-21 08:09:59 -07:00
|
|
|
}
|
|
|
|
|
2018-03-21 09:55:16 -07:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct ImportType {
|
|
|
|
}
|
|
|
|
|
2018-02-07 16:41:33 -08:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct Export {
|
|
|
|
pub class: Option<String>,
|
|
|
|
pub method: bool,
|
2017-12-18 12:39:14 -08:00
|
|
|
pub function: Function,
|
|
|
|
}
|
|
|
|
|
2018-02-22 12:01:38 +01:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct Enum {
|
|
|
|
pub name: String,
|
|
|
|
pub variants: Vec<EnumVariant>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct EnumVariant {
|
2018-02-23 14:17:53 +01:00
|
|
|
pub name: String,
|
|
|
|
pub value: u32
|
2018-02-22 12:01:38 +01:00
|
|
|
}
|
|
|
|
|
2018-02-07 16:41:33 -08:00
|
|
|
#[derive(Deserialize)]
|
2017-12-14 19:31:01 -08:00
|
|
|
pub struct Function {
|
|
|
|
pub name: String,
|
|
|
|
pub arguments: Vec<Type>,
|
|
|
|
pub ret: Option<Type>,
|
|
|
|
}
|
|
|
|
|
2018-02-07 16:41:33 -08:00
|
|
|
#[derive(Deserialize)]
|
2018-02-06 16:06:21 -08:00
|
|
|
pub struct CustomTypeName {
|
2018-03-31 07:57:47 -07:00
|
|
|
pub descriptor: u32,
|
2018-02-06 16:06:21 -08:00
|
|
|
pub name: String,
|
|
|
|
}
|
|
|
|
|
2018-03-28 01:22:31 +02:00
|
|
|
pub fn new_function(struct_name: &str) -> String {
|
|
|
|
let mut name = format!("__wbg_");
|
|
|
|
name.extend(struct_name
|
|
|
|
.chars()
|
|
|
|
.flat_map(|s| s.to_lowercase()));
|
|
|
|
name.push_str("_new");
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
2018-02-06 07:56:14 -08:00
|
|
|
pub fn free_function(struct_name: &str) -> String {
|
2018-02-06 11:54:40 -08:00
|
|
|
let mut name = format!("__wbg_");
|
2018-02-06 07:56:14 -08:00
|
|
|
name.extend(struct_name
|
|
|
|
.chars()
|
|
|
|
.flat_map(|s| s.to_lowercase()));
|
|
|
|
name.push_str("_free");
|
|
|
|
return name
|
2017-12-18 14:31:01 -08:00
|
|
|
}
|
|
|
|
|
2018-02-06 07:56:14 -08:00
|
|
|
pub fn free_function_export_name(function_name: &str) -> String {
|
|
|
|
function_name.to_string()
|
2017-12-18 14:31:01 -08:00
|
|
|
}
|
|
|
|
|
2018-02-06 07:56:14 -08:00
|
|
|
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
|
2017-12-14 21:55:21 -08:00
|
|
|
}
|
|
|
|
|
2018-03-31 07:57:47 -07:00
|
|
|
pub type Type = u32;
|
|
|
|
|
|
|
|
pub const TYPE_VECTOR_JSVALUE: u32 = 0;
|
|
|
|
pub const TYPE_ENUM: u32 = 1;
|
|
|
|
pub const TYPE_NUMBER: u32 = 2;
|
|
|
|
pub const TYPE_BORROWED_STR: u32 = 3;
|
|
|
|
pub const TYPE_STRING: u32 = 4;
|
|
|
|
pub const TYPE_BOOLEAN: u32 = 5;
|
|
|
|
pub const TYPE_SLICE_U8: u32 = 6;
|
|
|
|
pub const TYPE_VECTOR_U8: u32 = 7;
|
|
|
|
pub const TYPE_SLICE_I8: u32 = 8;
|
|
|
|
pub const TYPE_VECTOR_I8: u32 = 9;
|
|
|
|
pub const TYPE_SLICE_U16: u32 = 10;
|
|
|
|
pub const TYPE_VECTOR_U16: u32 = 11;
|
|
|
|
pub const TYPE_SLICE_I16: u32 = 12;
|
|
|
|
pub const TYPE_VECTOR_I16: u32 = 13;
|
|
|
|
pub const TYPE_SLICE_U32: u32 = 14;
|
|
|
|
pub const TYPE_VECTOR_U32: u32 = 15;
|
|
|
|
pub const TYPE_SLICE_I32: u32 = 16;
|
|
|
|
pub const TYPE_VECTOR_I32: u32 = 17;
|
2018-03-31 09:04:00 -07:00
|
|
|
pub const TYPE_SLICE_F32: u32 = 18;
|
|
|
|
pub const TYPE_VECTOR_F32: u32 = 19;
|
|
|
|
pub const TYPE_SLICE_F64: u32 = 20;
|
|
|
|
pub const TYPE_VECTOR_F64: u32 = 21;
|
2018-03-31 07:57:47 -07:00
|
|
|
pub const TYPE_JS_OWNED: u32 = 22;
|
|
|
|
pub const TYPE_JS_REF: u32 = 23;
|
2018-04-04 08:24:19 -07:00
|
|
|
pub const TYPE_STACK_FUNC0: u32 = 24;
|
|
|
|
pub const TYPE_STACK_FUNC1: u32 = 25;
|
2018-04-04 08:47:48 -07:00
|
|
|
pub const TYPE_STACK_FUNC2: u32 = 26;
|
|
|
|
pub const TYPE_STACK_FUNC3: u32 = 27;
|
|
|
|
pub const TYPE_STACK_FUNC4: u32 = 28;
|
|
|
|
pub const TYPE_STACK_FUNC5: u32 = 29;
|
|
|
|
pub const TYPE_STACK_FUNC6: u32 = 30;
|
|
|
|
pub const TYPE_STACK_FUNC7: u32 = 31;
|
2018-04-05 18:25:22 -07:00
|
|
|
pub const TYPE_FUNC: u32 = 32;
|
2018-04-04 08:47:48 -07:00
|
|
|
|
2018-04-05 18:25:22 -07:00
|
|
|
pub const TYPE_CUSTOM_START: u32 = 40;
|
2018-02-06 07:56:14 -08:00
|
|
|
pub const TYPE_CUSTOM_REF_FLAG: u32 = 1;
|
2018-02-06 16:06:21 -08:00
|
|
|
|
2018-03-31 07:57:47 -07:00
|
|
|
pub fn name_to_descriptor(name: &str) -> u32 {
|
|
|
|
const MAX: u32 = 10_000;
|
2018-02-06 16:06:21 -08:00
|
|
|
let mut h = fnv::FnvHasher::default();
|
|
|
|
name.hash(&mut h);
|
2018-03-31 09:04:00 -07:00
|
|
|
(((h.finish() as u32) % (MAX - TYPE_CUSTOM_START)) + TYPE_CUSTOM_START) & !1
|
2018-03-31 07:57:47 -07:00
|
|
|
|
2018-02-06 16:06:21 -08:00
|
|
|
}
|
2018-03-01 19:19:12 -08:00
|
|
|
|
|
|
|
pub fn version() -> String {
|
|
|
|
let mut v = env!("CARGO_PKG_VERSION").to_string();
|
|
|
|
if let Some(s) = option_env!("WBG_VERSION") {
|
|
|
|
v.push_str(" (");
|
|
|
|
v.push_str(s);
|
|
|
|
v.push_str(")");
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|