Change schemes for encoding custom type names

Store JSON a utf-32, map hashes of names to a `char` and store that name
in the map, and then do a reverse mapping when generating JS
This commit is contained in:
Alex Crichton
2018-02-06 16:06:21 -08:00
parent 8312f3ae9f
commit 43ee52bcbf
8 changed files with 124 additions and 57 deletions

View File

@ -1,5 +1,9 @@
#[macro_use]
extern crate serde_derive;
extern crate fnv;
use std::char;
use std::hash::{Hash, Hasher};
#[derive(Serialize, Deserialize)]
pub struct Program {
@ -7,7 +11,7 @@ pub struct Program {
pub free_functions: Vec<Function>,
pub imports: Vec<Import>,
pub imported_structs: Vec<ImportStruct>,
pub custom_type_names: Vec<String>,
pub custom_type_names: Vec<CustomTypeName>,
}
#[derive(Serialize, Deserialize)]
@ -50,6 +54,12 @@ pub struct Function {
pub ret: Option<Type>,
}
#[derive(Serialize, Deserialize)]
pub struct CustomTypeName {
pub descriptor: char,
pub name: String,
}
pub fn free_function(struct_name: &str) -> String {
let mut name = format!("__wbg_");
name.extend(struct_name
@ -91,3 +101,19 @@ pub const TYPE_JS_REF: char = '\u{63}';
pub const TYPE_CUSTOM_START: u32 = 0x64;
pub const TYPE_CUSTOM_REF_FLAG: u32 = 1;
pub fn name_to_descriptor(name: &str) -> char {
const CHAR_MAX: u32 = 0x10ffff;
const CHAR_HOLE_START: u32 = 0xd800;
const CHAR_HOLE_END: u32 = 0xe000;
let mut h = fnv::FnvHasher::default();
name.hash(&mut h);
let val = h.finish();
let range = (CHAR_MAX - (CHAR_HOLE_END - CHAR_HOLE_START) - TYPE_CUSTOM_START) / 2;
let idx = (val % (range as u64)) as u32;
let mut ret = TYPE_CUSTOM_START + idx * 2;
if CHAR_HOLE_START <= ret && ret < CHAR_HOLE_END {
ret += CHAR_HOLE_END - CHAR_HOLE_START;
}
char::from_u32(ret).unwrap()
}