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;
|
|
|
|
|
2017-12-24 15:32:40 -08:00
|
|
|
use std::collections::HashMap;
|
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};
|
2017-12-14 19:31:01 -08:00
|
|
|
|
|
|
|
use failure::{Error, ResultExt};
|
|
|
|
use parity_wasm::elements::*;
|
|
|
|
|
2017-12-19 19:06:48 -08:00
|
|
|
mod ts;
|
2017-12-24 15:32:40 -08:00
|
|
|
mod mapped;
|
|
|
|
|
|
|
|
use mapped::Mapped;
|
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,
|
2017-12-24 15:32:40 -08:00
|
|
|
uglify: bool,
|
2017-12-14 19:31:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Object {
|
2017-12-24 15:32:40 -08:00
|
|
|
module: Mapped,
|
2017-12-18 12:39:14 -08:00
|
|
|
program: shared::Program,
|
2017-12-14 21:55:21 -08:00
|
|
|
nodejs: bool,
|
2017-12-19 09:25:41 -08:00
|
|
|
debug: 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,
|
2017-12-24 15:32:40 -08:00
|
|
|
uglify: 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
|
|
|
|
}
|
|
|
|
|
2017-12-24 15:32:40 -08:00
|
|
|
pub fn uglify_wasm_names(&mut self, uglify: bool) -> &mut Bindgen {
|
|
|
|
self.uglify = uglify;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2017-12-14 19:31:01 -08:00
|
|
|
pub fn generate(&mut self) -> Result<Object, Error> {
|
|
|
|
let input = match self.path {
|
|
|
|
Some(ref path) => path,
|
|
|
|
None => panic!("must have a path input for now"),
|
|
|
|
};
|
|
|
|
let mut module = parity_wasm::deserialize_file(input).map_err(|e| {
|
|
|
|
format_err!("{:?}", e)
|
|
|
|
})?;
|
2017-12-18 12:39:14 -08:00
|
|
|
let program = extract_program(&mut module);
|
2017-12-24 15:32:40 -08:00
|
|
|
let mut mapped = Mapped {
|
2017-12-14 19:31:01 -08:00
|
|
|
module,
|
2017-12-24 15:32:40 -08:00
|
|
|
imports: HashMap::new(),
|
|
|
|
exports: HashMap::new(),
|
|
|
|
};
|
|
|
|
if self.uglify {
|
|
|
|
mapped.uglify(&program);
|
|
|
|
}
|
|
|
|
Ok(Object {
|
|
|
|
module: mapped,
|
2017-12-18 12:39:14 -08:00
|
|
|
program,
|
2017-12-14 21:55:21 -08:00
|
|
|
nodejs: self.nodejs,
|
2017-12-19 09:25:41 -08:00
|
|
|
debug: self.debug,
|
2017-12-14 19:31:01 -08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Object {
|
2017-12-19 19:06:48 -08:00
|
|
|
pub fn write_ts_to<P: AsRef<Path>>(&self, path: P) -> Result<(), Error> {
|
|
|
|
self._write_ts_to(path.as_ref())
|
2017-12-14 19:31:01 -08:00
|
|
|
}
|
|
|
|
|
2017-12-19 19:06:48 -08:00
|
|
|
fn _write_ts_to(&self, path: &Path) -> Result<(), Error> {
|
|
|
|
let ts = self.generate_ts();
|
2017-12-14 19:31:01 -08:00
|
|
|
let mut f = File::create(path).with_context(|_| {
|
|
|
|
format!("failed to create file at {:?}", path)
|
|
|
|
})?;
|
2017-12-19 19:06:48 -08:00
|
|
|
f.write_all(ts.as_bytes()).with_context(|_| {
|
2017-12-14 19:31:01 -08:00
|
|
|
format!("failed to write file at {:?}", path)
|
|
|
|
})?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-01-24 19:14:40 -08:00
|
|
|
pub fn write_js_to<P: AsRef<Path>>(&self, path: P) -> Result<(), Error> {
|
|
|
|
self._write_js_to(path.as_ref())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn _write_js_to(&self, path: &Path) -> Result<(), Error> {
|
|
|
|
let js = self.generate_js();
|
|
|
|
let mut f = File::create(path).with_context(|_| {
|
|
|
|
format!("failed to create file at {:?}", path)
|
|
|
|
})?;
|
|
|
|
f.write_all(js.as_bytes()).with_context(|_| {
|
|
|
|
format!("failed to write file at {:?}", path)
|
|
|
|
})?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-12-14 19:31:01 -08:00
|
|
|
pub fn write_wasm_to<P: AsRef<Path>>(self, path: P) -> Result<(), Error> {
|
|
|
|
self._write_wasm_to(path.as_ref())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn _write_wasm_to(self, path: &Path) -> Result<(), Error> {
|
2017-12-24 15:32:40 -08:00
|
|
|
parity_wasm::serialize_to_file(path, self.module.module).map_err(|e| {
|
2017-12-14 19:31:01 -08:00
|
|
|
format_err!("{:?}", e)
|
|
|
|
})?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-12-19 19:06:48 -08:00
|
|
|
pub fn generate_ts(&self) -> String {
|
|
|
|
let mut ts = ts::Js::default();
|
|
|
|
ts.nodejs = self.nodejs;
|
|
|
|
ts.debug = self.debug;
|
2018-01-24 19:14:40 -08:00
|
|
|
ts.ts = true;
|
|
|
|
ts.generate_program(&self.program, &self.module);
|
|
|
|
ts.to_string(&self.module, &self.program)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn generate_js(&self) -> String {
|
|
|
|
let mut ts = ts::Js::default();
|
|
|
|
ts.nodejs = self.nodejs;
|
|
|
|
ts.debug = self.debug;
|
|
|
|
ts.ts = false;
|
2017-12-19 19:06:48 -08:00
|
|
|
ts.generate_program(&self.program, &self.module);
|
2017-12-20 11:34:53 -08:00
|
|
|
ts.to_string(&self.module, &self.program)
|
2017-12-14 19:31:01 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-18 12:39:14 -08:00
|
|
|
fn extract_program(module: &mut Module) -> 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
|
|
|
|
|
|
|
let mut ret = shared::Program {
|
|
|
|
structs: Vec::new(),
|
|
|
|
free_functions: Vec::new(),
|
2017-12-18 21:43:16 -08:00
|
|
|
imports: Vec::new(),
|
2017-12-18 12:39:14 -08:00
|
|
|
};
|
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
|
|
|
};
|
|
|
|
|
|
|
|
for i in (0..data.entries().len()).rev() {
|
|
|
|
{
|
|
|
|
let value = data.entries()[i].value();
|
|
|
|
if !value.starts_with(b"wbg:") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
let json = &value[4..];
|
2017-12-18 12:39:14 -08:00
|
|
|
let p = match serde_json::from_slice(json) {
|
2017-12-14 19:31:01 -08:00
|
|
|
Ok(f) => f,
|
|
|
|
Err(_) => continue,
|
|
|
|
};
|
2017-12-18 21:43:16 -08:00
|
|
|
let shared::Program { structs, free_functions, imports } = p;
|
2017-12-18 12:39:14 -08:00
|
|
|
ret.structs.extend(structs);
|
|
|
|
ret.free_functions.extend(free_functions);
|
2017-12-18 21:43:16 -08:00
|
|
|
ret.imports.extend(imports);
|
2017-12-14 19:31:01 -08:00
|
|
|
}
|
|
|
|
data.entries_mut().remove(i);
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|