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

@ -76,7 +76,7 @@ wasm-bindgen = "0.1"
Next up our actual code! We'll write this in `src/lib.rs`:
```rust
#![feature(proc_macro)]
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
@ -261,7 +261,7 @@ Much more! Here's a taste of various features you can use in this project:
```rust
// src/lib.rs
#![feature(proc_macro)]
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;

View File

@ -16,7 +16,6 @@ pub struct Context<'a> {
pub required_internal_exports: HashSet<&'static str>,
pub config: &'a Bindgen,
pub module: &'a mut Module,
pub imports_to_rewrite: HashSet<String>,
pub custom_type_names: HashMap<char, String>,
pub imported_names: HashSet<String>,
pub exported_classes: HashMap<String, ExportedClass>,
@ -288,18 +287,7 @@ impl<'a> Context<'a> {
.flat_map(|s| s.entries_mut());
for import in imports {
if import.field().starts_with("__wbindgen") {
import.module_mut().truncate(0);
import.module_mut().push_str("./");
import.module_mut().push_str(module_name);
continue
}
// rustc doesn't have support for importing from anything other
// than the module `env` so let's use the metadata here to
// rewrite the imports if they import from `env` until it's
// fixed upstream.
if self.imports_to_rewrite.contains(import.field()) {
if import.module() == "__wbindgen_placeholder__" {
import.module_mut().truncate(0);
import.module_mut().push_str("./");
import.module_mut().push_str(module_name);
@ -954,7 +942,7 @@ impl<'a> Context<'a> {
};
imports.entries().iter().any(|i| {
i.module() == "env" && i.field() == name
i.module() == "__wbindgen_placeholder__" && i.field() == name
})
}
@ -1336,7 +1324,6 @@ impl<'a, 'b> SubContext<'a, 'b> {
info: &shared::Import,
import: &shared::ImportStatic) {
// TODO: should support more types to import here
self.cx.imports_to_rewrite.insert(import.shim.clone());
let obj = self.import_name(info, &import.name);
self.cx.expose_add_heap_object();
self.cx.globals.push_str(&format!("
@ -1349,8 +1336,6 @@ impl<'a, 'b> SubContext<'a, 'b> {
pub fn generate_import_function(&mut self,
info: &shared::Import,
import: &shared::ImportFunction) {
self.cx.imports_to_rewrite.insert(import.shim.clone());
let mut dst = String::new();
dst.push_str(&format!("function {}(", import.shim));

View File

@ -6,7 +6,6 @@ extern crate wasm_gc;
use std::char;
use std::fs::File;
use std::io::Write;
use std::ops::{Deref, DerefMut};
use std::path::{Path, PathBuf};
use std::slice;
@ -90,7 +89,6 @@ impl Bindgen {
typescript: format!("/* tslint:disable */\n"),
exposed_globals: Default::default(),
required_internal_exports: Default::default(),
imports_to_rewrite: Default::default(),
custom_type_names: Default::default(),
imported_names: Default::default(),
exported_classes: Default::default(),
@ -133,40 +131,33 @@ impl Bindgen {
fn extract_programs(module: &mut Module) -> Vec<shared::Program> {
let version = shared::version();
let data = module.sections_mut()
.iter_mut()
.filter_map(|s| {
match *s {
Section::Data(ref mut s) => Some(s),
_ => None,
}
})
.next();
let mut ret = Vec::new();
let data = match data {
Some(data) => data,
None => return ret,
};
for entry in data.entries_mut() {
let mut value = bytes_to_u32(entry.value_mut());
let mut tmp = &mut *value;
loop {
let value = tmp;
let start = match value.iter().position(|i| i.0 == 0x30d97887) {
Some(i) => i,
None => break,
};
if value.get(start + 1).map(|c| c.0) != Some(0xd4182f61) {
tmp = &mut value[1..];
continue
}
let cnt = value[start + 2].0 as usize;
let (a, b) = value[start..].split_at_mut(cnt + 3);
tmp = b;
let json = a[3..].iter()
.map(|i| char::from_u32(i.0).unwrap())
#[repr(packed)]
struct Unaligned(u32);
module.sections_mut().retain(|s| {
let custom = match *s {
Section::Custom(ref s) => s,
_ => return true,
};
if custom.name() != "__wasm_bindgen_unstable" {
return true
}
assert!(custom.payload().len() % 4 == 0);
let mut payload = unsafe {
slice::from_raw_parts(custom.payload().as_ptr() as *const Unaligned,
custom.payload().len() / 4)
};
while payload.len() > 0 {
let len = payload[0].0.to_le();
assert!(len % 4 == 0);
let (a, b) = payload[1..].split_at((len / 4) as usize);
payload = b;
let json = a.iter()
.map(|i| char::from_u32(i.0.to_le()).unwrap())
.collect::<String>();
let p: shared::Program = match serde_json::from_str(&json) {
Ok(f) => f,
@ -200,57 +191,9 @@ to open an issue at https://github.com/alexcrichton/wasm-bindgen/issues!
p.version, version);
}
ret.push(p);
for slot in a {
slot.0 = 0;
}
}
}
false
});
return ret
}
#[repr(packed)]
struct Unaligned(u32);
struct FutzWithAlign<'a> {
data: &'a mut Vec<u8>,
len: usize,
}
fn bytes_to_u32(a: &mut Vec<u8>) -> FutzWithAlign {
let prev_len = a.len();
// Data implicitly contains zeros after it and it looks like LLD exploits
// this. Pad our view into the vector with zeros to make sure that we read
// off everything when we iterate. After we're done iterating though we put
// this back as we found it, hence the newtype wrapper w/ a dtor.
while a.len() % 4 != 0 {
a.push(0);
}
FutzWithAlign { data: a, len: prev_len }
}
impl<'a> Deref for FutzWithAlign<'a> {
type Target = [Unaligned];
fn deref(&self) -> &[Unaligned] {
unsafe {
slice::from_raw_parts(self.data.as_ptr() as *const Unaligned,
self.data.len() / 4)
}
}
}
impl<'a> DerefMut for FutzWithAlign<'a> {
fn deref_mut(&mut self) -> &mut [Unaligned] {
unsafe {
slice::from_raw_parts_mut(self.data.as_mut_ptr() as *mut Unaligned,
self.data.len() / 4)
}
}
}
impl<'a> Drop for FutzWithAlign<'a> {
fn drop(&mut self) {
self.data.truncate(self.len);
}
}

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("\"");
}

View File

@ -1,4 +1,4 @@
#![feature(proc_macro)]
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;

View File

@ -1,4 +1,4 @@
#![feature(proc_macro)]
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;

View File

@ -1,4 +1,4 @@
#![feature(proc_macro)]
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;

View File

@ -1,4 +1,4 @@
#![feature(proc_macro)]
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;

View File

@ -1,4 +1,4 @@
#![feature(proc_macro)]
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
extern crate humantime;

View File

@ -1,4 +1,4 @@
#![feature(proc_macro)]
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;

View File

@ -1,4 +1,4 @@
#![feature(proc_macro)]
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;

View File

@ -5,7 +5,7 @@
//! this crate and this crate also provides JS bindings through the `JsValue`
//! interface.
#![feature(use_extern_macros, try_reserve)]
#![feature(use_extern_macros, wasm_import_module, try_reserve)]
extern crate wasm_bindgen_macro;
@ -213,6 +213,7 @@ macro_rules! numbers {
numbers! { i8 u8 i16 u16 i32 u32 f32 f64 }
#[wasm_import_module = "__wbindgen_placeholder__"]
extern {
fn __wbindgen_object_clone_ref(idx: u32) -> u32;
fn __wbindgen_object_drop_ref(idx: u32);
@ -228,6 +229,7 @@ extern {
fn __wbindgen_symbol_new(ptr: *const u8, len: usize) -> u32;
fn __wbindgen_is_symbol(idx: u32) -> u32;
fn __wbindgen_string_get(idx: u32, len: *mut usize) -> *mut u8;
fn __wbindgen_throw(a: *const u8, b: usize) -> !;
}
impl Clone for JsValue {
@ -298,9 +300,6 @@ impl<T: WasmBoundary> Deref for JsStatic<T> {
#[cold]
#[inline(never)]
pub fn throw(s: &str) -> ! {
extern {
fn __wbindgen_throw(a: *const u8, b: usize) -> !;
}
unsafe {
__wbindgen_throw(s.as_ptr(), s.len());
}

View File

@ -5,7 +5,7 @@ fn works() {
test_support::project()
.detect_node(true)
.file("src/lib.rs", r#"
#![feature(proc_macro)]
#![feature(proc_macro, wasm_custom_section)]
extern crate wasm_bindgen;

View File

@ -4,7 +4,7 @@ extern crate test_support;
fn simple() {
test_support::project()
.file("src/lib.rs", r#"
#![feature(proc_macro)]
#![feature(proc_macro, wasm_custom_section)]
extern crate wasm_bindgen;
@ -56,7 +56,7 @@ fn simple() {
fn strings() {
test_support::project()
.file("src/lib.rs", r#"
#![feature(proc_macro)]
#![feature(proc_macro, wasm_custom_section)]
extern crate wasm_bindgen;
@ -114,7 +114,7 @@ fn strings() {
fn exceptions() {
test_support::project()
.file("src/lib.rs", r#"
#![feature(proc_macro)]
#![feature(proc_macro, wasm_custom_section)]
extern crate wasm_bindgen;
@ -179,7 +179,7 @@ fn exceptions() {
fn pass_one_to_another() {
test_support::project()
.file("src/lib.rs", r#"
#![feature(proc_macro)]
#![feature(proc_macro, wasm_custom_section)]
extern crate wasm_bindgen;
@ -229,7 +229,7 @@ fn pass_one_to_another() {
fn pass_into_js() {
test_support::project()
.file("src/lib.rs", r#"
#![feature(proc_macro)]
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
@ -276,7 +276,7 @@ fn pass_into_js() {
fn issue_27() {
test_support::project()
.file("src/lib.rs", r#"
#![feature(proc_macro)]
#![feature(proc_macro, wasm_custom_section)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;

View File

@ -6,7 +6,7 @@ fn dependencies_work() {
.file(
"src/lib.rs",
r#"
#![feature(proc_macro)]
#![feature(proc_macro, wasm_custom_section)]
extern crate wasm_bindgen;
extern crate dependency;
use wasm_bindgen::prelude::*;
@ -51,7 +51,7 @@ fn dependencies_work() {
.file(
"vendor/dependency/src/lib.rs",
r#"
#![feature(proc_macro)]
#![feature(proc_macro, wasm_custom_section)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;

View File

@ -4,7 +4,7 @@ extern crate test_support;
fn c_style_enum() {
test_support::project()
.file("src/lib.rs", r#"
#![feature(proc_macro)]
#![feature(proc_macro, wasm_custom_section)]
extern crate wasm_bindgen;
@ -46,7 +46,7 @@ fn c_style_enum() {
fn c_style_enum_with_custom_values() {
test_support::project()
.file("src/lib.rs", r#"
#![feature(proc_macro)]
#![feature(proc_macro, wasm_custom_section)]
extern crate wasm_bindgen;

View File

@ -4,7 +4,7 @@ extern crate test_support;
fn simple() {
test_support::project()
.file("src/lib.rs", r#"
#![feature(proc_macro)]
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
@ -44,7 +44,7 @@ fn simple() {
fn import_class() {
test_support::project()
.file("src/lib.rs", r#"
#![feature(proc_macro)]
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
@ -87,7 +87,7 @@ fn import_class() {
fn construct() {
test_support::project()
.file("src/lib.rs", r#"
#![feature(proc_macro)]
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
@ -159,7 +159,7 @@ fn construct() {
fn new_constructors() {
test_support::project()
.file("src/lib.rs", r#"
#![feature(proc_macro)]
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
@ -204,7 +204,7 @@ fn new_constructors() {
fn switch_methods() {
test_support::project()
.file("src/lib.rs", r#"
#![feature(proc_macro)]
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
@ -283,7 +283,7 @@ fn switch_methods() {
fn properties() {
test_support::project()
.file("src/lib.rs", r#"
#![feature(proc_macro)]
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
@ -340,7 +340,7 @@ fn properties() {
fn rename_setter_getter() {
test_support::project()
.file("src/lib.rs", r#"
#![feature(proc_macro)]
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;

View File

@ -4,7 +4,7 @@ extern crate test_support;
fn simple() {
test_support::project()
.file("src/lib.rs", r#"
#![feature(proc_macro)]
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
@ -86,7 +86,7 @@ fn simple() {
fn unused() {
test_support::project()
.file("src/lib.rs", r#"
#![feature(proc_macro)]
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
@ -116,7 +116,7 @@ fn unused() {
fn strings() {
test_support::project()
.file("src/lib.rs", r#"
#![feature(proc_macro)]
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
@ -157,7 +157,7 @@ fn strings() {
fn exceptions() {
test_support::project()
.file("src/lib.rs", r#"
#![feature(proc_macro)]
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
@ -215,7 +215,7 @@ fn exceptions() {
fn exn_caught() {
test_support::project()
.file("src/lib.rs", r#"
#![feature(proc_macro)]
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
@ -253,7 +253,7 @@ fn exn_caught() {
fn free_imports() {
test_support::project()
.file("src/lib.rs", r#"
#![feature(proc_macro)]
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
@ -283,7 +283,7 @@ fn free_imports() {
fn import_a_field() {
test_support::project()
.file("src/lib.rs", r#"
#![feature(proc_macro)]
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
@ -315,7 +315,7 @@ fn import_a_field() {
fn rename() {
test_support::project()
.file("src/lib.rs", r#"
#![feature(proc_macro)]
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;

View File

@ -4,7 +4,7 @@ extern crate test_support;
fn simple() {
test_support::project()
.file("src/lib.rs", r#"
#![feature(proc_macro)]
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
@ -45,7 +45,7 @@ fn simple() {
fn owned() {
test_support::project()
.file("src/lib.rs", r#"
#![feature(proc_macro)]
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
@ -86,7 +86,7 @@ fn owned() {
fn clone() {
test_support::project()
.file("src/lib.rs", r#"
#![feature(proc_macro)]
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
@ -133,7 +133,7 @@ fn clone() {
fn promote() {
test_support::project()
.file("src/lib.rs", r#"
#![feature(proc_macro)]
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
@ -177,7 +177,7 @@ fn promote() {
fn returning_vector() {
test_support::project()
.file("src/lib.rs", r#"
#![feature(proc_macro)]
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;

View File

@ -4,7 +4,7 @@ extern crate test_support;
fn auto_bind_math() {
test_support::project()
.file("src/lib.rs", r#"
#![feature(proc_macro)]
#![feature(proc_macro, wasm_custom_section)]
extern crate wasm_bindgen;

View File

@ -5,7 +5,7 @@ fn works() {
test_support::project()
.debug(false)
.file("src/lib.rs", r#"
#![feature(proc_macro)]
#![feature(proc_macro, wasm_custom_section)]
extern crate wasm_bindgen;

View File

@ -4,7 +4,7 @@ extern crate test_support;
fn add() {
test_support::project()
.file("src/lib.rs", r#"
#![feature(proc_macro)]
#![feature(proc_macro, wasm_custom_section)]
extern crate wasm_bindgen;
@ -57,7 +57,7 @@ fn add() {
fn string_arguments() {
test_support::project()
.file("src/lib.rs", r#"
#![feature(proc_macro)]
#![feature(proc_macro, wasm_custom_section)]
extern crate wasm_bindgen;
@ -89,7 +89,7 @@ fn string_arguments() {
fn return_a_string() {
test_support::project()
.file("src/lib.rs", r#"
#![feature(proc_macro)]
#![feature(proc_macro, wasm_custom_section)]
extern crate wasm_bindgen;
@ -123,7 +123,7 @@ fn return_a_string() {
fn exceptions() {
test_support::project()
.file("src/lib.rs", r#"
#![feature(proc_macro)]
#![feature(proc_macro, wasm_custom_section)]
extern crate wasm_bindgen;
@ -154,7 +154,7 @@ fn exceptions() {
// fn other_imports() {
// test_support::project()
// .file("src/lib.rs", r#"
// #![feature(proc_macro)]
// #![feature(proc_macro, wasm_custom_section)]
//
// extern crate wasm_bindgen;
//

View File

@ -4,7 +4,7 @@ extern crate test_support;
fn export() {
test_support::project()
.file("src/lib.rs", r#"
#![feature(proc_macro)]
#![feature(proc_macro, wasm_custom_section)]
extern crate wasm_bindgen;
@ -84,7 +84,7 @@ fn export() {
fn import() {
test_support::project()
.file("src/lib.rs", r#"
#![feature(proc_macro)]
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;

View File

@ -5,7 +5,7 @@ fn works() {
test_support::project()
.detect_node(true)
.file("src/lib.rs", r#"
#![feature(proc_macro)]
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;