rustfmt all the things

This commit is contained in:
R. Andrew Ohana
2018-06-27 22:42:34 -07:00
parent a29e71ec49
commit 9127a0419f
62 changed files with 2724 additions and 1400 deletions

View File

@ -138,15 +138,15 @@ impl Descriptor {
pub fn is_number(&self) -> bool {
match *self {
Descriptor::I8 |
Descriptor::U8 |
Descriptor::I16 |
Descriptor::U16 |
Descriptor::I32 |
Descriptor::U32 |
Descriptor::F32 |
Descriptor::F64 |
Descriptor::Enum => true,
Descriptor::I8
| Descriptor::U8
| Descriptor::I16
| Descriptor::U16
| Descriptor::I32
| Descriptor::U32
| Descriptor::F32
| Descriptor::F64
| Descriptor::Enum => true,
_ => return false,
}
}
@ -191,19 +191,15 @@ impl Descriptor {
let inner = match *self {
Descriptor::String => return Some(VectorKind::String),
Descriptor::Vector(ref d) => &**d,
Descriptor::Ref(ref d) => {
match **d {
Descriptor::Slice(ref d) => &**d,
Descriptor::String => return Some(VectorKind::String),
_ => return None,
}
}
Descriptor::RefMut(ref d) => {
match **d {
Descriptor::Slice(ref d) => &**d,
_ => return None,
}
}
Descriptor::Ref(ref d) => match **d {
Descriptor::Slice(ref d) => &**d,
Descriptor::String => return Some(VectorKind::String),
_ => return None,
},
Descriptor::RefMut(ref d) => match **d {
Descriptor::Slice(ref d) => &**d,
_ => return None,
},
_ => return None,
};
match *inner {
@ -218,7 +214,7 @@ impl Descriptor {
Descriptor::F32 => Some(VectorKind::F32),
Descriptor::F64 => Some(VectorKind::F64),
Descriptor::Anyref => Some(VectorKind::Anyref),
_ => None
_ => None,
}
}
@ -248,8 +244,7 @@ impl Descriptor {
pub fn is_by_ref(&self) -> bool {
match *self {
Descriptor::Ref(_) |
Descriptor::RefMut(_) => true,
Descriptor::Ref(_) | Descriptor::RefMut(_) => true,
_ => false,
}
}

View File

@ -68,9 +68,11 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
/// passed should be `this.ptr`.
pub fn method(&mut self, method: bool) -> &mut Self {
if method {
self.prelude("if (this.ptr === 0) {
self.prelude(
"if (this.ptr === 0) {
throw new Error('Attempt to use a moved value');
}");
}",
);
self.rust_arguments.insert(0, "this.ptr".to_string());
}
self
@ -111,30 +113,46 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
let name = self.abi_arg();
if let Some(kind) = arg.vector_kind() {
self.js_arguments.push((name.clone(), kind.js_ty().to_string()));
self.js_arguments
.push((name.clone(), kind.js_ty().to_string()));
let func = self.cx.pass_to_wasm_function(kind)?;
self.prelude(&format!("\
const [ptr{i}, len{i}] = {func}({arg});\n\
", i = i, func = func, arg = name));
self.prelude(&format!(
"\
const [ptr{i}, len{i}] = {func}({arg});\n\
",
i = i,
func = func,
arg = name
));
if arg.is_by_ref() {
if arg.is_mut_ref() {
let get = self.cx.memview_function(kind);
self.finally(&format!("\
{arg}.set({get}().subarray(\
ptr{i} / {size}, \
ptr{i} / {size} + len{i}\
));\n\
", i = i, arg = name, get = get, size = kind.size()));
self.finally(&format!(
"\
{arg}.set({get}().subarray(\
ptr{i} / {size}, \
ptr{i} / {size} + len{i}\
));\n\
",
i = i,
arg = name,
get = get,
size = kind.size()
));
}
self.finally(&format!("\
wasm.__wbindgen_free(ptr{i}, len{i} * {size});\n\
", i = i, size = kind.size()));
self.finally(&format!(
"\
wasm.__wbindgen_free(ptr{i}, len{i} * {size});\n\
",
i = i,
size = kind.size()
));
self.cx.require_internal_export("__wbindgen_free")?;
}
self.rust_arguments.push(format!("ptr{}", i));
self.rust_arguments.push(format!("len{}", i));
return Ok(self)
return Ok(self);
}
if let Some(s) = arg.rust_struct() {
@ -142,24 +160,32 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
if self.cx.config.debug {
self.cx.expose_assert_class();
self.prelude(&format!("\
_assertClass({arg}, {struct_});\n\
", arg = name, struct_ = s));
self.prelude(&format!(
"\
_assertClass({arg}, {struct_});\n\
",
arg = name,
struct_ = s
));
}
if arg.is_by_ref() {
self.rust_arguments.push(format!("{}.ptr", name));
} else {
self.prelude(&format!("\
self.prelude(&format!(
"\
const ptr{i} = {arg}.ptr;\n\
if (ptr{i} === 0) {{
throw new Error('Attempt to use a moved value');
}}
{arg}.ptr = 0;\n\
", i = i, arg = name));
",
i = i,
arg = name
));
self.rust_arguments.push(format!("ptr{}", i));
}
return Ok(self)
return Ok(self);
}
if arg.is_number() {
@ -171,7 +197,7 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
}
self.rust_arguments.push(name);
return Ok(self)
return Ok(self);
}
if let Some(signed) = arg.get_64bit() {
@ -183,51 +209,55 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
self.cx.expose_uint32_memory();
self.cx.expose_global_argument_ptr()?;
self.js_arguments.push((name.clone(), "BigInt".to_string()));
self.prelude(&format!("\
{f}[0] = {name};\n\
const lo{i} = u32CvtShim[0];\n\
const hi{i} = u32CvtShim[1];\n\
",
self.prelude(&format!(
"\
{f}[0] = {name};\n\
const lo{i} = u32CvtShim[0];\n\
const hi{i} = u32CvtShim[1];\n\
",
i = i,
f = f,
name = name,
));
self.rust_arguments.push(format!("lo{}", i));
self.rust_arguments.push(format!("hi{}", i));
return Ok(self)
return Ok(self);
}
if arg.is_ref_anyref() {
self.js_arguments.push((name.clone(), "any".to_string()));
self.cx.expose_borrowed_objects();
self.finally("stack.pop();");
self.rust_arguments.push(format!("addBorrowedObject({})", name));
return Ok(self)
self.rust_arguments
.push(format!("addBorrowedObject({})", name));
return Ok(self);
}
match *arg {
Descriptor::Boolean => {
self.js_arguments.push((name.clone(), "boolean".to_string()));
self.js_arguments
.push((name.clone(), "boolean".to_string()));
if self.cx.config.debug {
self.cx.expose_assert_bool();
self.prelude(&format!("\
_assertBoolean({name});\n\
", name = name));
self.prelude(&format!(
"\
_assertBoolean({name});\n\
",
name = name
));
}
self.rust_arguments.push(format!("{} ? 1 : 0", name));
}
Descriptor::Char => {
self.js_arguments.push((name.clone(), "string".to_string()));
self.rust_arguments.push(format!("{}.codePointAt(0)", name))
},
}
Descriptor::Anyref => {
self.js_arguments.push((name.clone(), "any".to_string()));
self.cx.expose_add_heap_object();
self.rust_arguments.push(format!("addHeapObject({})", name));
}
_ => {
bail!("unsupported argument to rust function {:?}", arg)
}
_ => bail!("unsupported argument to rust function {:?}", arg),
}
Ok(self)
}
@ -238,7 +268,7 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
None => {
self.ret_ty = "void".to_string();
self.ret_expr = format!("return RET;");
return Ok(self)
return Ok(self);
}
};
@ -246,7 +276,7 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
self.ret_ty = "any".to_string();
self.cx.expose_get_object();
self.ret_expr = format!("return getObject(RET);");
return Ok(self)
return Ok(self);
}
if ty.is_by_ref() {
@ -261,28 +291,32 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
self.cx.require_internal_export("__wbindgen_free")?;
self.prelude("const retptr = globalArgumentPtr();");
self.rust_arguments.insert(0, "retptr".to_string());
self.ret_expr = format!("\
RET;\n\
const mem = getUint32Memory();\n\
const ptr = mem[retptr / 4];\n\
const len = mem[retptr / 4 + 1];\n\
const realRet = {}(ptr, len).slice();\n\
wasm.__wbindgen_free(ptr, len * {});\n\
return realRet;\n\
", f, ty.size());
return Ok(self)
self.ret_expr = format!(
"\
RET;\n\
const mem = getUint32Memory();\n\
const ptr = mem[retptr / 4];\n\
const len = mem[retptr / 4 + 1];\n\
const realRet = {}(ptr, len).slice();\n\
wasm.__wbindgen_free(ptr, len * {});\n\
return realRet;\n\
",
f,
ty.size()
);
return Ok(self);
}
if let Some(name) = ty.rust_struct() {
self.ret_ty = name.to_string();
self.ret_expr = format!("return {name}.__construct(RET);", name = name);
return Ok(self)
return Ok(self);
}
if ty.is_number() {
self.ret_ty = "number".to_string();
self.ret_expr = format!("return RET;");
return Ok(self)
return Ok(self);
}
if let Some(signed) = ty.get_64bit() {
@ -297,11 +331,14 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
};
self.prelude("const retptr = globalArgumentPtr();");
self.rust_arguments.insert(0, "retptr".to_string());
self.ret_expr = format!("\
RET;\n\
return {}()[retptr / 8];\n\
", f);
return Ok(self)
self.ret_expr = format!(
"\
RET;\n\
return {}()[retptr / 8];\n\
",
f
);
return Ok(self);
}
match *ty {
@ -333,7 +370,8 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
/// generated function shim and the second is a TypeScript signature of the
/// JS expression.
pub fn finish(&self, prefix: &str, invoc: &str) -> (String, String) {
let js_args = self.js_arguments
let js_args = self
.js_arguments
.iter()
.map(|s| &s.0[..])
.collect::<Vec<_>>()
@ -342,29 +380,35 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
js.push_str(&self.prelude);
let rust_args = self.rust_arguments.join(", ");
let invoc = self.ret_expr.replace("RET", &format!("{}({})", invoc, rust_args));
let invoc = self
.ret_expr
.replace("RET", &format!("{}({})", invoc, rust_args));
let invoc = if self.finally.len() == 0 {
invoc
} else {
format!("\
format!(
"\
try {{\n\
{}
\n}} finally {{\n\
{}
}}\n\
",
&invoc,
&self.finally,
&invoc, &self.finally,
)
};
js.push_str(&invoc);
js.push_str("\n}");
let ts_args = self.js_arguments
let ts_args = self
.js_arguments
.iter()
.map(|s| format!("{}: {}", s.0, s.1))
.collect::<Vec<_>>()
.join(", ");
let ts = format!("{} {}({}): {};\n", prefix, self.js_name, ts_args, self.ret_ty);
let ts = format!(
"{} {}({}): {};\n",
prefix, self.js_name, ts_args, self.ret_ty
);
(js, ts)
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -1,7 +1,7 @@
use failure::Error;
use descriptor::{Descriptor, Function};
use super::{Context, Js2Rust};
use descriptor::{Descriptor, Function};
/// Helper struct for manufacturing a shim in JS used to translate Rust types to
/// JS, then invoking an imported JS function.
@ -85,18 +85,27 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
if let Some(ty) = arg.vector_kind() {
let abi2 = self.shim_argument();
let f = self.cx.expose_get_vector_from_wasm(ty);
self.prelude(&format!("let v{0} = {func}({0}, {1});",
abi, abi2, func = f));
self.prelude(&format!(
"let v{0} = {func}({0}, {1});",
abi,
abi2,
func = f
));
if !arg.is_by_ref() {
self.prelude(&format!("\
v{0} = v{0}.slice();\n\
wasm.__wbindgen_free({0}, {1} * {size});\
", abi, abi2, size = ty.size()));
self.prelude(&format!(
"\
v{0} = v{0}.slice();\n\
wasm.__wbindgen_free({0}, {1} * {size});\
",
abi,
abi2,
size = ty.size()
));
self.cx.require_internal_export("__wbindgen_free")?;
}
self.js_arguments.push(format!("v{}", abi));
return Ok(())
return Ok(());
}
if let Some(signed) = arg.get_64bit() {
@ -107,18 +116,19 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
};
let hi = self.shim_argument();
let name = format!("n{}", abi);
self.prelude(&format!("\
u32CvtShim[0] = {lo};\n\
u32CvtShim[1] = {hi};\n\
const {name} = {f}[0];\n\
",
self.prelude(&format!(
"\
u32CvtShim[0] = {lo};\n\
u32CvtShim[1] = {hi};\n\
const {name} = {f}[0];\n\
",
lo = abi,
hi = hi,
f = f,
name = name,
));
self.js_arguments.push(name);
return Ok(())
return Ok(());
}
if let Some(class) = arg.rust_struct() {
@ -128,14 +138,15 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
let assign = format!("let c{0} = {1}.__construct({0});", abi, class);
self.prelude(&assign);
self.js_arguments.push(format!("c{}", abi));
return Ok(())
return Ok(());
}
if let Some((f, mutable)) = arg.stack_closure() {
let (js, _ts) = {
let mut builder = Js2Rust::new("", self.cx);
if mutable {
builder.prelude("let a = this.a;\n")
builder
.prelude("let a = this.a;\n")
.prelude("this.a = 0;\n")
.rust_argument("a")
.finally("this.a = a;\n");
@ -151,22 +162,28 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
self.cx.function_table_needed = true;
let next_global = self.global_idx();
self.global_idx();
self.prelude(&format!("\
let cb{0} = {js};\n\
cb{0}.f = wasm.__wbg_function_table.get({0});\n\
cb{0}.a = getGlobalArgument({next_global});\n\
cb{0}.b = getGlobalArgument({next_global} + 1);\n\
", abi, js = js, next_global = next_global));
self.prelude(&format!(
"\
let cb{0} = {js};\n\
cb{0}.f = wasm.__wbg_function_table.get({0});\n\
cb{0}.a = getGlobalArgument({next_global});\n\
cb{0}.b = getGlobalArgument({next_global} + 1);\n\
",
abi,
js = js,
next_global = next_global
));
self.finally(&format!("cb{0}.a = cb{0}.b = 0;", abi));
self.js_arguments.push(format!("cb{0}.bind(cb{0})", abi));
return Ok(())
return Ok(());
}
if let Some(closure) = arg.ref_closure() {
let (js, _ts) = {
let mut builder = Js2Rust::new("", self.cx);
if closure.mutable {
builder.prelude("let a = this.a;\n")
builder
.prelude("let a = this.a;\n")
.prelude("this.a = 0;\n")
.rust_argument("a")
.finally("this.a = a;\n");
@ -182,38 +199,40 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
self.cx.expose_uint32_memory();
self.cx.expose_add_heap_object();
self.cx.function_table_needed = true;
let reset_idx = format!("\
let cb{0} = {js};\n\
cb{0}.a = getGlobalArgument({a});\n\
cb{0}.b = getGlobalArgument({b});\n\
cb{0}.f = wasm.__wbg_function_table.get(getGlobalArgument({c}));\n\
let real = cb{0}.bind(cb{0});\n\
real.original = cb{0};\n\
idx{0} = getUint32Memory()[{0} / 4] = addHeapObject(real);\n\
",
let reset_idx = format!(
"\
let cb{0} = {js};\n\
cb{0}.a = getGlobalArgument({a});\n\
cb{0}.b = getGlobalArgument({b});\n\
cb{0}.f = wasm.__wbg_function_table.get(getGlobalArgument({c}));\n\
let real = cb{0}.bind(cb{0});\n\
real.original = cb{0};\n\
idx{0} = getUint32Memory()[{0} / 4] = addHeapObject(real);\n\
",
abi,
js = js,
a = self.global_idx(),
b = self.global_idx(),
c = self.global_idx(),
);
self.prelude(&format!("\
let idx{0} = getUint32Memory()[{0} / 4];\n\
if (idx{0} === 0xffffffff) {{\n\
{1}\
}}\n\
", abi, &reset_idx));
self.prelude(&format!(
"\
let idx{0} = getUint32Memory()[{0} / 4];\n\
if (idx{0} === 0xffffffff) {{\n\
{1}\
}}\n\
",
abi, &reset_idx
));
self.cx.expose_get_object();
self.js_arguments.push(format!("getObject(idx{})", abi));
return Ok(())
return Ok(());
}
let invoc_arg = match *arg {
ref d if d.is_number() => abi,
Descriptor::Boolean => format!("{} !== 0", abi),
Descriptor::Char => {
format!("String.fromCodePoint({})", abi)
}
Descriptor::Char => format!("String.fromCodePoint({})", abi),
Descriptor::Anyref => {
self.cx.expose_take_object();
format!("takeObject({})", abi)
@ -222,7 +241,10 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
self.cx.expose_get_object();
format!("getObject({})", abi)
}
_ => bail!("unimplemented argument type in imported function: {:?}", arg),
_ => bail!(
"unimplemented argument type in imported function: {:?}",
arg
),
};
self.js_arguments.push(invoc_arg);
Ok(())
@ -233,7 +255,7 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
Some(ref t) => t,
None => {
self.ret_expr = "JS;".to_string();
return Ok(())
return Ok(());
}
};
if ty.is_by_ref() {
@ -243,17 +265,20 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
let f = self.cx.pass_to_wasm_function(ty)?;
self.cx.expose_uint32_memory();
self.shim_arguments.insert(0, "ret".to_string());
self.ret_expr = format!("\
self.ret_expr = format!(
"\
const [retptr, retlen] = {}(JS);\n\
const mem = getUint32Memory();
mem[ret / 4] = retptr;
mem[ret / 4 + 1] = retlen;
", f);
return Ok(())
",
f
);
return Ok(());
}
if ty.is_number() {
self.ret_expr = "return JS;".to_string();
return Ok(())
return Ok(());
}
if let Some(signed) = ty.get_64bit() {
let f = if signed {
@ -264,11 +289,14 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
"getUint64Memory"
};
self.shim_arguments.insert(0, "ret".to_string());
self.ret_expr = format!("\
const val = JS;\n\
{}()[ret / 8] = val;\n\
", f);
return Ok(())
self.ret_expr = format!(
"\
const val = JS;\n\
{}()[ret / 8] = val;\n\
",
f
);
return Ok(());
}
self.ret_expr = match *ty {
Descriptor::Boolean => "return JS ? 1 : 0;".to_string(),
@ -301,33 +329,39 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
);
if self.catch {
let catch = "\
const view = getUint32Memory();\n\
view[exnptr / 4] = 1;\n\
view[exnptr / 4 + 1] = addHeapObject(e);\n\
";
const view = getUint32Memory();\n\
view[exnptr / 4] = 1;\n\
view[exnptr / 4 + 1] = addHeapObject(e);\n\
";
invoc = format!("\
invoc = format!(
"\
try {{\n\
{}
}} catch (e) {{\n\
{}
}}\
", &invoc, catch);
",
&invoc, catch
);
};
if self.finally.len() > 0 {
invoc = format!("\
invoc = format!(
"\
try {{\n\
{}
}} finally {{\n\
{}
}}\
", &invoc, &self.finally);
",
&invoc, &self.finally
);
}
ret.push_str(&invoc);
ret.push_str("\n}\n");
return ret
return ret;
}
fn global_idx(&mut self) -> usize {

View File

@ -17,8 +17,8 @@ use std::path::{Path, PathBuf};
use failure::{Error, ResultExt};
use parity_wasm::elements::*;
mod js;
mod descriptor;
mod js;
pub mod wasm2es6js;
pub struct Bindgen {
@ -142,11 +142,9 @@ impl Bindgen {
let mut v = MyExternals(Vec::new());
match instance.invoke_export(name, &[], &mut v) {
Ok(None) => Some(v.0),
Ok(Some(_)) => {
unreachable!(
"there is only one export, and we only return None from it"
)
},
Ok(Some(_)) => unreachable!(
"there is only one export, and we only return None from it"
),
// Allow missing exported describe functions. This can
// happen when a nested dependency crate exports things
// but the root crate doesn't use them.
@ -207,13 +205,16 @@ impl Bindgen {
shim.push_str(&format!("imports['{0}'] = require('{0}');\n", module));
}
shim.push_str(&format!("
shim.push_str(&format!(
"
const join = require('path').join;
const bytes = require('fs').readFileSync(join(__dirname, '{}'));
const wasmModule = new WebAssembly.Module(bytes);
const wasmInstance = new WebAssembly.Instance(wasmModule, imports);
module.exports = wasmInstance.exports;
", path.file_name().unwrap().to_str().unwrap()));
",
path.file_name().unwrap().to_str().unwrap()
));
reset_indentation(&shim)
}
@ -230,27 +231,25 @@ fn extract_programs(module: &mut Module) -> Result<Vec<shared::Program>, Error>
_ => continue,
};
if custom.name() != "__wasm_bindgen_unstable" {
continue
continue;
}
to_remove.push(i);
let mut payload = custom.payload();
while payload.len() > 0 {
let len =
((payload[0] as usize) << 0) |
((payload[1] as usize) << 8) |
((payload[2] as usize) << 16) |
((payload[3] as usize) << 24);
let len = ((payload[0] as usize) << 0)
| ((payload[1] as usize) << 8)
| ((payload[2] as usize) << 16)
| ((payload[3] as usize) << 24);
let (a, b) = payload[4..].split_at(len as usize);
payload = b;
let p: shared::ProgramOnlySchema = match serde_json::from_slice(&a) {
Ok(f) => f,
Err(e) => {
bail!("failed to decode what looked like wasm-bindgen data: {}", e)
}
Err(e) => bail!("failed to decode what looked like wasm-bindgen data: {}", e),
};
if p.schema_version != shared::SCHEMA_VERSION {
bail!("
bail!(
"
it looks like the Rust project used to create this wasm file was linked against
a different version of wasm-bindgen than this binary:
@ -272,13 +271,13 @@ or you can update the binary with
if this warning fails to go away though and you're not sure what to do feel free
to open an issue at https://github.com/alexcrichton/wasm-bindgen/issues!
",
p.version, version);
p.version,
version
);
}
let p: shared::Program = match serde_json::from_slice(&a) {
Ok(f) => f,
Err(e) => {
bail!("failed to decode what looked like wasm-bindgen data: {}", e)
}
Err(e) => bail!("failed to decode what looked like wasm-bindgen data: {}", e),
};
ret.push(p);
}
@ -297,13 +296,13 @@ impl wasmi::ImportResolver for MyResolver {
&self,
module_name: &str,
field_name: &str,
signature: &wasmi::Signature
signature: &wasmi::Signature,
) -> Result<wasmi::FuncRef, wasmi::Error> {
// Route our special "describe" export to 1 and everything else to 0.
// That way whenever the function 1 is invoked we know what to do and
// when 0 is invoked (by accident) we'll trap and produce an error.
let idx = (module_name == "__wbindgen_placeholder__" &&
field_name == "__wbindgen_describe") as usize;
let idx = (module_name == "__wbindgen_placeholder__" && field_name == "__wbindgen_describe")
as usize;
Ok(wasmi::FuncInstance::alloc_host(signature.clone(), idx))
}
@ -311,7 +310,7 @@ impl wasmi::ImportResolver for MyResolver {
&self,
_module_name: &str,
_field_name: &str,
descriptor: &wasmi::GlobalDescriptor
descriptor: &wasmi::GlobalDescriptor,
) -> Result<wasmi::GlobalRef, wasmi::Error> {
// dummy implementation to ensure instantiation succeeds
let val = match descriptor.value_type() {
@ -340,7 +339,7 @@ impl wasmi::ImportResolver for MyResolver {
&self,
_module_name: &str,
_field_name: &str,
descriptor: &wasmi::TableDescriptor
descriptor: &wasmi::TableDescriptor,
) -> Result<wasmi::TableRef, wasmi::Error> {
// dummy implementation to ensure instantiation succeeds
let initial = descriptor.initial();
@ -358,7 +357,7 @@ impl wasmi::Externals for MyExternals {
fn invoke_index(
&mut self,
index: usize,
args: wasmi::RuntimeArgs
args: wasmi::RuntimeArgs,
) -> Result<Option<wasmi::RuntimeValue>, wasmi::Trap> {
macro_rules! bail {
($($t:tt)*) => ({
@ -413,7 +412,12 @@ fn indent_recurse(mut lines: ::std::str::Lines, current_indent: usize) -> String
if trimmed.starts_with('?') || trimmed.starts_with(':') {
current_indent += 1;
}
format!("\n{}{}{}", " ".repeat(current_indent), &trimmed, &indent_recurse(lines, next_indent))
format!(
"\n{}{}{}",
" ".repeat(current_indent),
&trimmed,
&indent_recurse(lines, next_indent)
)
} else {
String::new()
}

View File

@ -1,13 +1,13 @@
extern crate base64;
extern crate tempfile;
use std::collections::{HashSet, HashMap};
use std::collections::{HashMap, HashSet};
use std::fs::File;
use std::io::{self, Write, Read};
use std::io::{self, Read, Write};
use std::process::Command;
use parity_wasm::elements::*;
use failure::{Error, ResultExt};
use parity_wasm::elements::*;
pub struct Config {
base64: bool,
@ -65,31 +65,36 @@ impl Output {
let mut exports = format!("/* tslint:disable */\n");
if let Some(i) = self.module.export_section() {
let imported_functions = self.module.import_section()
let imported_functions = self
.module
.import_section()
.map(|m| m.functions() as u32)
.unwrap_or(0);
for entry in i.entries() {
let idx = match *entry.internal() {
Internal::Function(i) => i - imported_functions,
Internal::Memory(_) => {
exports.push_str(&format!("
exports.push_str(&format!(
"
export const {}: WebAssembly.Memory;
", entry.field()));
continue
}
Internal::Table(_) => {
continue
}
Internal::Global(_) => {
continue
",
entry.field()
));
continue;
}
Internal::Table(_) => continue,
Internal::Global(_) => continue,
};
let functions = self.module.function_section()
let functions = self
.module
.function_section()
.expect("failed to find function section");
let idx = functions.entries()[idx as usize].type_ref();
let types = self.module.type_section()
let types = self
.module
.type_section()
.expect("failed to find type section");
let ty = match types.types()[idx as usize] {
Type::Function(ref f) => f,
@ -103,12 +108,17 @@ impl Output {
args.push_str(": number");
}
exports.push_str(&format!("
exports.push_str(&format!(
"
export function {name}({args}): {ret};
",
name = entry.field(),
args = args,
ret = if ty.return_type().is_some() { "number" } else { "void" },
ret = if ty.return_type().is_some() {
"number"
} else {
"void"
},
));
}
}
@ -117,7 +127,7 @@ impl Output {
exports.push_str("export const booted: Promise<boolean>;");
}
return exports
return exports;
}
pub fn js(self) -> Result<String, Error> {
@ -146,19 +156,23 @@ impl Output {
}
if !set.insert(entry.module()) {
continue
continue;
}
let name = (b'a' + (set.len() as u8)) as char;
js_imports.push_str(&format!("import * as import_{} from '{}';",
name,
entry.module()));
js_imports.push_str(&format!(
"import * as import_{} from '{}';",
name,
entry.module()
));
imports.push_str(&format!("'{}': import_{}, ", entry.module(), name));
}
}
if let Some(i) = self.module.export_section() {
let imported_functions = self.module.import_section()
let imported_functions = self
.module
.import_section()
.map(|m| m.functions() as u32)
.unwrap_or(0);
for entry in i.entries() {
@ -166,21 +180,21 @@ impl Output {
Internal::Function(i) => i - imported_functions,
Internal::Memory(_) => {
export_mem = true;
continue
}
Internal::Table(_) => {
continue
}
Internal::Global(_) => {
continue
continue;
}
Internal::Table(_) => continue,
Internal::Global(_) => continue,
};
let functions = self.module.function_section()
let functions = self
.module
.function_section()
.expect("failed to find function section");
let idx = functions.entries()[idx as usize].type_ref();
let types = self.module.type_section()
let types = self
.module
.type_section()
.expect("failed to find type section");
let ty = match types.types()[idx as usize] {
Type::Function(ref f) => f,
@ -193,50 +207,67 @@ impl Output {
args.push((b'a' + (i as u8)) as char);
}
exports.push_str(&format!("
exports.push_str(&format!(
"
export function {name}({args}) {{
{ret} wasm.exports.{name}({args});
}}
",
name = entry.field(),
args = args,
ret = if ty.return_type().is_some() { "return" } else { "" },
ret = if ty.return_type().is_some() {
"return"
} else {
""
},
));
}
}
let inst = format!("WebAssembly.instantiate(bytes,{{ {imports} }})
let inst = format!(
"WebAssembly.instantiate(bytes,{{ {imports} }})
.then(obj => {{
wasm = obj.instance;
{memory}
}})",
imports = imports,
memory = if export_mem { "memory = wasm.exports.memory;" } else { "" },
);
imports = imports,
memory = if export_mem {
"memory = wasm.exports.memory;"
} else {
""
},
);
let (bytes, booted) = if self.base64 {
let wasm = serialize(self.module)
.expect("failed to serialize");
let wasm = serialize(self.module).expect("failed to serialize");
(
format!("
format!(
"
let bytes;
const base64 = \"{base64}\";
if (typeof Buffer === 'undefined') {{
bytes = Uint8Array.from(atob(base64), c => c.charCodeAt(0));
}} else {{
bytes = Buffer.from(base64, 'base64');
}}", base64 = base64::encode(&wasm)),
inst
}}",
base64 = base64::encode(&wasm)
),
inst,
)
} else if let Some(ref path) = self.fetch_path {
(
String::new(),
format!("fetch('{path}')
format!(
"fetch('{path}')
.then(res => res.arrayBuffer())
.then(bytes => {inst})", path = path, inst = inst)
.then(bytes => {inst})",
path = path,
inst = inst
),
)
} else {
bail!("the option --base64 or --fetch is required");
};
Ok(format!("
Ok(format!(
"
{js_imports}
let wasm;
{bytes}
@ -273,16 +304,20 @@ impl Output {
let m = name_map.entry(entry.field()).or_insert(entry.module());
if *m != entry.module() {
bail!("the name `{}` is imported from two differnet \
modules which currently isn't supported in `wasm2asm` \
mode");
bail!(
"the name `{}` is imported from two differnet \
modules which currently isn't supported in `wasm2asm` \
mode"
);
}
let name = format!("import{}", i);
js_imports.push_str(&format!("import {{ {} as {} }} from '{}';\n",
entry.field(),
name,
entry.module()));
js_imports.push_str(&format!(
"import {{ {} as {} }} from '{}';\n",
entry.field(),
name,
entry.module()
));
imported_items.push((entry.field().to_string(), name));
}
}
@ -298,18 +333,17 @@ impl Output {
Internal::Global(_) => continue,
};
js_exports.push_str(&format!("export const {0} = ret.{0};\n",
entry.field()));
js_exports.push_str(&format!("export const {0} = ret.{0};\n", entry.field()));
}
if !export_mem {
bail!("the `wasm2asm` mode is currently only compatible with \
modules that export memory")
bail!(
"the `wasm2asm` mode is currently only compatible with \
modules that export memory"
)
}
}
let memory_size = self.module.memory_section()
.unwrap()
.entries()[0]
let memory_size = self.module.memory_section().unwrap().entries()[0]
.limits()
.initial();
@ -329,9 +363,7 @@ impl Output {
};
let base64 = base64::encode(entry.value());
js_init_mem.push_str(&format!("_assign({}, \"{}\");\n",
offset,
base64));
js_init_mem.push_str(&format!("_assign({}, \"{}\");\n", offset, base64));
}
}
@ -340,9 +372,7 @@ impl Output {
let wasm_file = td.as_ref().join("foo.wasm");
File::create(&wasm_file)
.and_then(|mut f| f.write_all(&wasm))
.with_context(|_| {
format!("failed to write wasm to `{}`", wasm_file.display())
})?;
.with_context(|_| format!("failed to write wasm to `{}`", wasm_file.display()))?;
let wast_file = td.as_ref().join("foo.wast");
run(
@ -364,19 +394,19 @@ impl Output {
let mut asm_func = String::new();
File::open(&js_file)
.and_then(|mut f| f.read_to_string(&mut asm_func))
.with_context(|_| {
format!("failed to read `{}`", js_file.display())
})?;
.with_context(|_| format!("failed to read `{}`", js_file.display()))?;
let mut make_imports = String::from("
let mut make_imports = String::from(
"
var imports = {};
");
",
);
for (name, import) in imported_items {
make_imports.push_str(&format!("imports['{}'] = {};\n", name, import));
}
Ok(format!("\
Ok(format!(
"\
{js_imports}
{asm_func}
@ -424,28 +454,31 @@ impl Output {
fn run(cmd: &mut Command, program: &str) -> Result<(), Error> {
let output = cmd.output().with_context(|e| {
if e.kind() == io::ErrorKind::NotFound {
format!("failed to execute `{}`, is the tool installed \
from the binaryen project?\ncommand line: {:?}",
program,
cmd)
format!(
"failed to execute `{}`, is the tool installed \
from the binaryen project?\ncommand line: {:?}",
program, cmd
)
} else {
format!("failed to execute: {:?}", cmd)
}
})?;
if output.status.success() {
return Ok(())
return Ok(());
}
let mut s = format!("failed to execute: {:?}\nstatus: {}\n",
cmd,
output.status);
let mut s = format!("failed to execute: {:?}\nstatus: {}\n", cmd, output.status);
if !output.stdout.is_empty() {
s.push_str(&format!("----- stdout ------\n{}\n",
String::from_utf8_lossy(&output.stdout)));
s.push_str(&format!(
"----- stdout ------\n{}\n",
String::from_utf8_lossy(&output.stdout)
));
}
if !output.stderr.is_empty() {
s.push_str(&format!("----- stderr ------\n{}\n",
String::from_utf8_lossy(&output.stderr)));
s.push_str(&format!(
"----- stderr ------\n{}\n",
String::from_utf8_lossy(&output.stderr)
));
}
bail!("{}", s)
}