138 lines
3.4 KiB
Rust
Raw Normal View History

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;
use std::path::{Path, PathBuf};
use std::fs::File;
use std::io::Write;
use failure::{Error, ResultExt};
use parity_wasm::elements::*;
2017-12-18 12:39:14 -08:00
mod js;
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-14 19:31:01 -08:00
}
pub struct Object {
module: Module,
2017-12-18 12:39:14 -08:00
program: shared::Program,
2017-12-14 21:55:21 -08:00
nodejs: 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-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-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-14 19:31:01 -08:00
Ok(Object {
module,
2017-12-18 12:39:14 -08:00
program,
2017-12-14 21:55:21 -08:00
nodejs: self.nodejs,
2017-12-14 19:31:01 -08:00
})
}
}
impl Object {
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(())
}
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> {
parity_wasm::serialize_to_file(path, self.module).map_err(|e| {
format_err!("{:?}", e)
})?;
Ok(())
}
2017-12-14 21:55:21 -08:00
pub fn generate_js(&self) -> String {
2017-12-18 12:39:14 -08:00
let mut js = js::Js::default();
2017-12-18 14:31:01 -08:00
js.nodejs = self.nodejs;
2017-12-18 12:39:14 -08:00
js.generate_program(&self.program);
js.to_string()
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(),
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,
};
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);
ret.imports.extend(imports);
2017-12-14 19:31:01 -08:00
}
data.entries_mut().remove(i);
}
return ret
}