2017-12-14 19:31:01 -08:00
|
|
|
#[macro_use]
|
|
|
|
extern crate failure;
|
|
|
|
extern crate parity_wasm;
|
|
|
|
extern crate wasm_bindgen_shared as shared;
|
|
|
|
extern crate serde_json;
|
2018-02-06 08:58:15 -08:00
|
|
|
extern crate wasm_gc;
|
2017-12-14 19:31:01 -08:00
|
|
|
|
2018-02-06 16:06:21 -08:00
|
|
|
use std::char;
|
2017-12-14 19:31:01 -08:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::Write;
|
2017-12-24 15:32:40 -08:00
|
|
|
use std::path::{Path, PathBuf};
|
2018-02-06 16:06:21 -08:00
|
|
|
use std::slice;
|
2017-12-14 19:31:01 -08:00
|
|
|
|
2018-01-29 21:20:38 -08:00
|
|
|
use failure::Error;
|
2017-12-14 19:31:01 -08:00
|
|
|
use parity_wasm::elements::*;
|
|
|
|
|
2018-01-29 21:20:38 -08:00
|
|
|
mod js;
|
|
|
|
pub mod wasm2es6js;
|
2017-12-18 12:39:14 -08:00
|
|
|
|
2017-12-14 19:31:01 -08:00
|
|
|
pub struct Bindgen {
|
|
|
|
path: Option<PathBuf>,
|
2017-12-14 21:55:21 -08:00
|
|
|
nodejs: bool,
|
2017-12-19 09:25:41 -08:00
|
|
|
debug: bool,
|
2018-01-29 21:20:38 -08:00
|
|
|
typescript: bool,
|
2017-12-14 19:31:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Bindgen {
|
|
|
|
pub fn new() -> Bindgen {
|
|
|
|
Bindgen {
|
|
|
|
path: None,
|
2017-12-14 21:55:21 -08:00
|
|
|
nodejs: false,
|
2017-12-19 09:25:41 -08:00
|
|
|
debug: false,
|
2018-01-29 21:20:38 -08:00
|
|
|
typescript: false,
|
2017-12-14 19:31:01 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn input_path<P: AsRef<Path>>(&mut self, path: P) -> &mut Bindgen {
|
|
|
|
self.path = Some(path.as_ref().to_path_buf());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2017-12-14 21:55:21 -08:00
|
|
|
pub fn nodejs(&mut self, node: bool) -> &mut Bindgen {
|
|
|
|
self.nodejs = node;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2017-12-19 09:25:41 -08:00
|
|
|
pub fn debug(&mut self, debug: bool) -> &mut Bindgen {
|
|
|
|
self.debug = debug;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-01-29 21:20:38 -08:00
|
|
|
pub fn typescript(&mut self, typescript: bool) -> &mut Bindgen {
|
|
|
|
self.typescript = typescript;
|
2017-12-24 15:32:40 -08:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-01-29 21:20:38 -08:00
|
|
|
pub fn generate<P: AsRef<Path>>(&mut self, path: P) -> Result<(), Error> {
|
|
|
|
self._generate(path.as_ref())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn _generate(&mut self, out_dir: &Path) -> Result<(), Error> {
|
2017-12-14 19:31:01 -08:00
|
|
|
let input = match self.path {
|
|
|
|
Some(ref path) => path,
|
|
|
|
None => panic!("must have a path input for now"),
|
|
|
|
};
|
2018-01-29 21:20:38 -08:00
|
|
|
let stem = input.file_stem().unwrap().to_str().unwrap();
|
2017-12-14 19:31:01 -08:00
|
|
|
let mut module = parity_wasm::deserialize_file(input).map_err(|e| {
|
|
|
|
format_err!("{:?}", e)
|
|
|
|
})?;
|
2018-02-06 15:52:44 -08:00
|
|
|
let programs = extract_programs(&mut module);
|
|
|
|
|
|
|
|
let (js, ts) = {
|
|
|
|
let mut cx = js::Context {
|
|
|
|
globals: String::new(),
|
|
|
|
imports: String::new(),
|
|
|
|
typescript: format!("/* tslint:disable */\n"),
|
|
|
|
exposed_globals: Default::default(),
|
|
|
|
required_internal_exports: Default::default(),
|
|
|
|
imports_to_rewrite: Default::default(),
|
2018-02-06 16:06:21 -08:00
|
|
|
custom_type_names: Default::default(),
|
2018-02-06 15:52:44 -08:00
|
|
|
config: &self,
|
|
|
|
module: &mut module,
|
|
|
|
};
|
2018-02-06 16:06:21 -08:00
|
|
|
for program in programs.iter() {
|
|
|
|
cx.add_custom_type_names(program);
|
|
|
|
}
|
2018-02-06 15:52:44 -08:00
|
|
|
for program in programs.iter() {
|
|
|
|
js::SubContext {
|
|
|
|
program,
|
|
|
|
cx: &mut cx,
|
|
|
|
}.generate();
|
|
|
|
}
|
|
|
|
cx.finalize(stem)
|
|
|
|
};
|
2018-01-29 21:20:38 -08:00
|
|
|
|
|
|
|
let js_path = out_dir.join(stem).with_extension("js");
|
|
|
|
File::create(&js_path).unwrap()
|
|
|
|
.write_all(js.as_bytes()).unwrap();
|
|
|
|
|
|
|
|
if self.typescript {
|
|
|
|
let ts_path = out_dir.join(stem).with_extension("d.ts");
|
|
|
|
File::create(&ts_path).unwrap()
|
|
|
|
.write_all(ts.as_bytes()).unwrap();
|
|
|
|
}
|
2017-12-14 19:31:01 -08:00
|
|
|
|
2018-01-29 21:20:38 -08:00
|
|
|
let wasm_path = out_dir.join(format!("{}_wasm", stem)).with_extension("wasm");
|
2018-02-06 08:58:15 -08:00
|
|
|
let wasm_bytes = parity_wasm::serialize(module).map_err(|e| {
|
2017-12-14 19:31:01 -08:00
|
|
|
format_err!("{:?}", e)
|
|
|
|
})?;
|
2018-02-06 08:58:15 -08:00
|
|
|
let bytes = wasm_gc::Config::new()
|
|
|
|
.demangle(false)
|
|
|
|
.gc(&wasm_bytes)?;
|
|
|
|
File::create(&wasm_path)?.write_all(&bytes)?;
|
2017-12-14 19:31:01 -08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-06 15:52:44 -08:00
|
|
|
fn extract_programs(module: &mut Module) -> Vec<shared::Program> {
|
2017-12-14 19:31:01 -08:00
|
|
|
let data = module.sections_mut()
|
|
|
|
.iter_mut()
|
|
|
|
.filter_map(|s| {
|
|
|
|
match *s {
|
|
|
|
Section::Data(ref mut s) => Some(s),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.next();
|
2017-12-18 12:39:14 -08:00
|
|
|
|
2018-02-06 15:52:44 -08:00
|
|
|
let mut ret = Vec::new();
|
2017-12-14 19:31:01 -08:00
|
|
|
let data = match data {
|
|
|
|
Some(data) => data,
|
2017-12-18 12:39:14 -08:00
|
|
|
None => return ret,
|
2017-12-14 19:31:01 -08:00
|
|
|
};
|
|
|
|
|
2018-02-06 16:06:21 -08:00
|
|
|
'outer:
|
2017-12-14 19:31:01 -08:00
|
|
|
for i in (0..data.entries().len()).rev() {
|
|
|
|
{
|
2018-02-06 16:06:21 -08:00
|
|
|
let mut value = bytes_to_u32(data.entries()[i].value());
|
|
|
|
loop {
|
|
|
|
match value.iter().position(|i| i.0 == (b'w' as u32)) {
|
|
|
|
Some(i) => value = &value[i + 1..],
|
|
|
|
None => continue 'outer,
|
|
|
|
}
|
|
|
|
match value.iter().position(|i| i.0 == (b'b' as u32)) {
|
|
|
|
Some(i) => value = &value[i + 1..],
|
|
|
|
None => continue 'outer,
|
|
|
|
}
|
|
|
|
match value.iter().position(|i| i.0 == (b'g' as u32)) {
|
|
|
|
Some(i) => value = &value[i + 1..],
|
|
|
|
None => continue 'outer,
|
|
|
|
}
|
|
|
|
match value.iter().position(|i| i.0 == (b':' as u32)) {
|
|
|
|
Some(i) => value = &value[i + 1..],
|
|
|
|
None => continue 'outer,
|
|
|
|
}
|
|
|
|
break
|
2017-12-14 19:31:01 -08:00
|
|
|
}
|
2018-02-06 16:06:21 -08:00
|
|
|
// TODO: shouldn't take the rest of the value
|
|
|
|
let json = value.iter()
|
|
|
|
.map(|i| char::from_u32(i.0).unwrap())
|
|
|
|
.collect::<String>();
|
|
|
|
let p = match serde_json::from_str(&json) {
|
2017-12-14 19:31:01 -08:00
|
|
|
Ok(f) => f,
|
2018-02-06 07:56:14 -08:00
|
|
|
Err(e) => {
|
|
|
|
panic!("failed to decode what looked like wasm-bindgen data: {}", e)
|
|
|
|
}
|
2017-12-14 19:31:01 -08:00
|
|
|
};
|
2018-02-06 15:52:44 -08:00
|
|
|
ret.push(p);
|
2017-12-14 19:31:01 -08:00
|
|
|
}
|
|
|
|
data.entries_mut().remove(i);
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
2018-02-06 16:06:21 -08:00
|
|
|
|
|
|
|
#[repr(packed)]
|
|
|
|
struct Unaligned(u32);
|
|
|
|
|
|
|
|
fn bytes_to_u32(a: &[u8]) -> &[Unaligned] {
|
|
|
|
unsafe {
|
|
|
|
slice::from_raw_parts(a.as_ptr() as *const Unaligned, a.len() / 4)
|
|
|
|
}
|
|
|
|
}
|