Leverage new rustc wasm features

This commit leverages two new attributes in the Rust compiler,
`#[wasm_custom_section]` and `#[wasm_import_module]`. These two attributes allow
removing a lot of hacks found in wasm-bindgen and also allows removing the
requirement of `wasm-opt` to remove the unused data sections.

This does require two new nightly features but we already required the
`proc_macro` nightly feature and these will hopefully be stabilized before that
feature!
This commit is contained in:
Alex Crichton
2018-03-14 14:33:53 -07:00
parent dc03e6c84a
commit 02b7021053
26 changed files with 110 additions and 170 deletions

View File

@ -404,12 +404,13 @@ impl Program {
};
let cnt = cnt as u32;
(quote! {
0x30d97887,
0xd4182f61,
#cnt,
(#cnt >> 0) as u8,
(#cnt >> 8) as u8,
(#cnt >> 16) as u8,
(#cnt >> 24) as u8
}).to_tokens(dst);
tmp.to_tokens(dst);
(cnt as usize) + 3
(cnt as usize) + 4
}
}

View File

@ -110,9 +110,9 @@ impl ToTokens for ast::Program {
let generated_static_length = self.literal(&mut generated_static_value);
(my_quote! {
#[no_mangle]
#[allow(non_upper_case_globals)]
pub static #generated_static_name: [u32; #generated_static_length] =
#[wasm_custom_section = "__wasm_bindgen_unstable"]
const #generated_static_name: [u8; #generated_static_length] =
[#generated_static_value];
}).to_tokens(tokens);
}
@ -565,6 +565,7 @@ impl ToTokens for ast::ImportFunction {
#[allow(bad_style)]
#vis extern #fn_token #rust_name(#me #(#arguments),*) #ret {
::wasm_bindgen::__rt::link_this_library();
#[wasm_import_module = "__wbindgen_placeholder__"]
extern {
fn #import_name(#(#abi_arguments),*) -> #abi_ret;
}
@ -638,6 +639,7 @@ impl ToTokens for ast::ImportStatic {
#[allow(bad_style)]
#vis static #name: ::wasm_bindgen::JsStatic<#ty> = {
fn init() -> #ty {
#[wasm_import_module = "__wbindgen_placeholder__"]
extern {
fn #shim_name() -> u32;
}

View File

@ -21,12 +21,18 @@ impl<'a> LiteralBuilder<'a> {
self.cnt
}
fn char_lit(&mut self, c: char) {
if self.cnt > 0 {
::syn::token::Comma::default().to_tokens(self.dst);
}
fn byte(&mut self, b: u8) {
::syn::token::Comma::default().to_tokens(self.dst);
self.cnt += 1;
(c as u32).to_tokens(self.dst);
b.to_tokens(self.dst);
}
fn char_lit(&mut self, c: char) {
let c = c as u32;
self.byte(c as u8);
self.byte((c >> 8) as u8);
self.byte((c >> 16) as u8);
self.byte((c >> 24) as u8);
}
fn append(&mut self, s: &str) {
@ -57,9 +63,13 @@ impl<'a> LiteralBuilder<'a> {
fn as_char(&mut self, tokens: Tokens) {
self.append("\"");
::syn::token::Comma::default().to_tokens(self.dst);
tokens.to_tokens(self.dst);
self.cnt += 1;
(quote! {
, (((#tokens) as u32) >> 0) as u8
, (((#tokens) as u32) >> 8) as u8
, (((#tokens) as u32) >> 16) as u8
, (((#tokens) as u32) >> 24) as u8
}).to_tokens(self.dst);
self.cnt += 4;
self.append("\"");
}