mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-21 00:36:33 +00:00
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:
@ -217,7 +217,15 @@ impl Program {
|
||||
("free_functions", &|a| a.list(&self.free_functions, Function::wbg_literal)),
|
||||
("imports", &|a| a.list(&self.imports, Import::wbg_literal)),
|
||||
("imported_structs", &|a| a.list(&self.imported_structs, ImportStruct::wbg_literal)),
|
||||
("custom_type_names", &|a| a.list(&self.structs, |s, a| a.str(s.name.as_ref()))),
|
||||
("custom_type_names", &|a| {
|
||||
a.list(&self.structs, |s, a| {
|
||||
let val = shared::name_to_descriptor(s.name.as_ref());
|
||||
a.fields(&[
|
||||
("descriptor", &|a| a.char(val)),
|
||||
("name", &|a| a.str(s.name.as_ref()))
|
||||
]);
|
||||
})
|
||||
}),
|
||||
]);
|
||||
return a.cnt
|
||||
}
|
||||
@ -373,14 +381,14 @@ impl Type {
|
||||
Type::String => a.char(shared::TYPE_STRING),
|
||||
Type::ByValue(ref t) => {
|
||||
a.as_char(my_quote! {
|
||||
<#t as ::wasm_bindgen::convert::WasmBoundary>::DESCRIPTOR as u8
|
||||
<#t as ::wasm_bindgen::convert::WasmBoundary>::DESCRIPTOR
|
||||
});
|
||||
}
|
||||
Type::ByRef(ref ty) |
|
||||
Type::ByMutRef(ref ty) => {
|
||||
a.as_char(my_quote! {
|
||||
((<#ty as ::wasm_bindgen::convert::WasmBoundary>::DESCRIPTOR as u32) |
|
||||
::wasm_bindgen::convert::DESCRIPTOR_CUSTOM_REF_FLAG) as u8
|
||||
(<#ty as ::wasm_bindgen::convert::WasmBoundary>::DESCRIPTOR |
|
||||
::wasm_bindgen::convert::DESCRIPTOR_CUSTOM_REF_FLAG)
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -559,17 +567,17 @@ struct LiteralBuilder<'a> {
|
||||
}
|
||||
|
||||
impl<'a> LiteralBuilder<'a> {
|
||||
fn byte(&mut self, byte: u8) {
|
||||
fn char_lit(&mut self, c: char) {
|
||||
if self.cnt > 0 {
|
||||
::syn::token::Comma::default().to_tokens(self.dst);
|
||||
}
|
||||
self.cnt += 1;
|
||||
byte.to_tokens(self.dst);
|
||||
(c as u32).to_tokens(self.dst);
|
||||
}
|
||||
|
||||
fn append(&mut self, s: &str) {
|
||||
for byte in s.bytes() {
|
||||
self.byte(byte);
|
||||
for c in s.chars() {
|
||||
self.char_lit(c);
|
||||
}
|
||||
}
|
||||
|
||||
@ -588,21 +596,9 @@ impl<'a> LiteralBuilder<'a> {
|
||||
}
|
||||
|
||||
fn char(&mut self, s: char) {
|
||||
self.append("\"\\u");
|
||||
let s = s as u32;
|
||||
self.byte(to_hex((s >> 12) as u8));
|
||||
self.byte(to_hex((s >> 8) as u8));
|
||||
self.byte(to_hex((s >> 4) as u8));
|
||||
self.byte(to_hex((s >> 0) as u8));
|
||||
self.append("\"");
|
||||
|
||||
fn to_hex(a: u8) -> u8 {
|
||||
let a = a & 0xf;
|
||||
match a {
|
||||
0 ... 9 => b'0' + a,
|
||||
_ => b'a'+ a - 10,
|
||||
}
|
||||
}
|
||||
self.char_lit(s);
|
||||
self.append("\"");
|
||||
}
|
||||
|
||||
fn as_char(&mut self, tokens: Tokens) {
|
||||
|
@ -10,7 +10,6 @@ extern crate proc_macro2;
|
||||
extern crate serde_json;
|
||||
extern crate wasm_bindgen_shared as shared;
|
||||
|
||||
use std::char;
|
||||
use std::sync::atomic::*;
|
||||
|
||||
use proc_macro::TokenStream;
|
||||
@ -79,8 +78,8 @@ pub fn wasm_bindgen(input: TokenStream) -> TokenStream {
|
||||
for function in program.free_functions.iter() {
|
||||
bindgen_fn(function, &mut ret);
|
||||
}
|
||||
for (i, s) in program.structs.iter().enumerate() {
|
||||
bindgen_struct(i, s, &mut ret);
|
||||
for s in program.structs.iter() {
|
||||
bindgen_struct(s, &mut ret);
|
||||
}
|
||||
for i in program.imports.iter() {
|
||||
bindgen_import(i, &mut ret);
|
||||
@ -103,7 +102,7 @@ pub fn wasm_bindgen(input: TokenStream) -> TokenStream {
|
||||
(my_quote! {
|
||||
#[no_mangle]
|
||||
#[allow(non_upper_case_globals)]
|
||||
pub static #generated_static_name: [u8; #generated_static_length] =
|
||||
pub static #generated_static_name: [u32; #generated_static_length] =
|
||||
[#generated_static_value];
|
||||
}).to_tokens(&mut ret);
|
||||
|
||||
@ -121,7 +120,7 @@ fn bindgen_fn(function: &ast::Function, into: &mut Tokens) {
|
||||
into)
|
||||
}
|
||||
|
||||
fn bindgen_struct(idx: usize, s: &ast::Struct, into: &mut Tokens) {
|
||||
fn bindgen_struct(s: &ast::Struct, into: &mut Tokens) {
|
||||
for f in s.functions.iter() {
|
||||
bindgen_struct_fn(s, f, into);
|
||||
}
|
||||
@ -131,11 +130,11 @@ fn bindgen_struct(idx: usize, s: &ast::Struct, into: &mut Tokens) {
|
||||
|
||||
let name = &s.name;
|
||||
let free_fn = s.free_function();
|
||||
let c = char::from_u32(idx as u32 * 2 + shared::TYPE_CUSTOM_START);
|
||||
let c = shared::name_to_descriptor(name.as_ref()) as u32;
|
||||
(my_quote! {
|
||||
impl ::wasm_bindgen::convert::WasmBoundary for #name {
|
||||
type Js = u32;
|
||||
const DESCRIPTOR: char = #c;
|
||||
const DESCRIPTOR: u32 = #c;
|
||||
|
||||
fn into_js(self) -> u32 {
|
||||
Box::into_raw(Box::new(::wasm_bindgen::__rt::WasmRefCell::new(self))) as u32
|
||||
@ -387,7 +386,7 @@ fn bindgen_imported_struct(import: &ast::ImportStruct, tokens: &mut Tokens) {
|
||||
impl ::wasm_bindgen::convert::WasmBoundary for #name {
|
||||
type Js = <::wasm_bindgen::JsValue as
|
||||
::wasm_bindgen::convert::WasmBoundary>::Js;
|
||||
const DESCRIPTOR: char = <::wasm_bindgen::JsValue as
|
||||
const DESCRIPTOR: u32 = <::wasm_bindgen::JsValue as
|
||||
::wasm_bindgen::convert::WasmBoundary>::DESCRIPTOR;
|
||||
|
||||
fn into_js(self) -> Self::Js {
|
||||
|
Reference in New Issue
Block a user