Merge pull request #134 from rustwasm/better-formatting

Work a little harder to better format the output JS
This commit is contained in:
Alex Crichton 2018-04-16 09:59:41 -05:00 committed by GitHub
commit f61e12af91
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 120 additions and 85 deletions

View File

@ -78,13 +78,19 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
/// Add extra processing to the prelude of this shim.
pub fn prelude(&mut self, s: &str) -> &mut Self {
self.prelude.push_str(s);
for line in s.lines() {
self.prelude.push_str(line);
self.prelude.push_str("\n");
}
self
}
/// Add extra processing to the finally block of this shim.
pub fn finally(&mut self, s: &str) -> &mut Self {
self.finally.push_str(s);
for line in s.lines() {
self.finally.push_str(line);
self.finally.push_str("\n");
}
self
}
@ -105,12 +111,12 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
let func = self.cx.pass_to_wasm_function(kind);
self.cx.expose_set_global_argument();
let global_idx = self.global_idx();
self.prelude.push_str(&format!("\
const [ptr{i}, len{i}] = {func}({arg});
setGlobalArgument(len{i}, {global_idx});
self.prelude(&format!("\
const [ptr{i}, len{i}] = {func}({arg});\n\
setGlobalArgument(len{i}, {global_idx});\n\
", i = i, func = func, arg = name, global_idx = global_idx));
if arg.is_by_ref() {
self.finally.push_str(&format!("\n\
self.finally(&format!("\
wasm.__wbindgen_free(ptr{i}, len{i} * {size});\n\
", i = i, size = kind.size()));
self.cx.required_internal_exports.insert(
@ -126,17 +132,17 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
if self.cx.config.debug {
self.cx.expose_assert_class();
self.prelude.push_str(&format!("\
_assertClass({arg}, {struct_});
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.push_str(&format!("\
const ptr{i} = {arg}.ptr;
{arg}.ptr = 0;
self.prelude(&format!("\
const ptr{i} = {arg}.ptr;\n\
{arg}.ptr = 0;\n\
", i = i, arg = name));
self.rust_arguments.push(format!("ptr{}", i));
}
@ -148,7 +154,7 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
if self.cx.config.debug {
self.cx.expose_assert_num();
self.prelude.push_str(&format!("_assertNum({});\n", name));
self.prelude(&format!("_assertNum({});", name));
}
self.rust_arguments.push(name);
@ -158,7 +164,7 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
if arg.is_ref_anyref() {
self.js_arguments.push((name.clone(), "any".to_string()));
self.cx.expose_borrowed_objects();
self.finally.push_str("stack.pop();\n");
self.finally("stack.pop();");
self.rust_arguments.push(format!("addBorrowedObject({})", name));
return
}
@ -168,8 +174,8 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
self.js_arguments.push((name.clone(), "boolean".to_string()));
if self.cx.config.debug {
self.cx.expose_assert_bool();
self.prelude.push_str(&format!("\
_assertBoolean({name});
self.prelude(&format!("\
_assertBoolean({name});\n\
", name = name));
}
self.rust_arguments.push(format!("arg{i} ? 1 : 0", i = i));
@ -211,12 +217,12 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
let f = self.cx.expose_get_vector_from_wasm(ty);
self.cx.expose_get_global_argument();
self.cx.required_internal_exports.insert("__wbindgen_free");
self.ret_expr = format!("
const ret = RET;
const len = getGlobalArgument(0);
const realRet = {}(ret, len);
wasm.__wbindgen_free(ret, len * {});
return realRet;
self.ret_expr = format!("\
const ret = RET;\n\
const len = getGlobalArgument(0);\n\
const realRet = {}(ret, len);\n\
wasm.__wbindgen_free(ret, len * {});\n\
return realRet;\n\
", f, ty.size());
return
}
@ -263,24 +269,25 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
.collect::<Vec<_>>()
.join(", ");
let mut js = format!("{}({}) {{\n", prefix, js_args);
js.push_str(&self.prelude);
js.push_str(&indent(&self.prelude));
let rust_args = self.rust_arguments.join(", ");
let invoc = self.ret_expr.replace("RET", &format!("{}({})", invoc, rust_args));
if self.finally.len() == 0 {
js.push_str(&invoc);
let invoc = if self.finally.len() == 0 {
invoc
} else {
js.push_str(&format!("\
try {{
{}
}} finally {{
{}
}}
format!("\
try {{\n\
{}\
}} finally {{\n\
{}\
}}\n\
",
invoc,
self.finally,
));
}
indent(&invoc),
indent(&self.finally),
)
};
js.push_str(&indent(&invoc));
js.push_str("}");
let ts_args = self.js_arguments
@ -298,3 +305,13 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
ret
}
}
fn indent(s: &str) -> String {
let mut ret = String::new();
for line in s.lines() {
ret.push_str(" ");
ret.push_str(line);
ret.push_str("\n");
}
return ret
}

View File

@ -56,7 +56,7 @@ impl<'a> Context<'a> {
format!("export const {} = {};\n", name, contents)
}
};
self.globals.push_str(&global);
self.global(&global);
}
pub fn finalize(&mut self, module_name: &str) -> (String, String) {
@ -262,7 +262,7 @@ impl<'a> Context<'a> {
self.rewrite_imports(module_name);
let js = if self.config.no_modules {
let mut js = if self.config.no_modules {
format!("
(function() {{
var wasm;
@ -292,12 +292,12 @@ impl<'a> Context<'a> {
format!("import * as wasm from './{}_bg';", module_name)
};
format!("
/* tslint:disable */
{import_wasm}
{imports}
format!("\
/* tslint:disable */\n\
{import_wasm}\n\
{imports}\n\
{globals}
{globals}\n\
{footer}",
import_wasm = import_wasm,
globals = self.globals,
@ -309,6 +309,10 @@ impl<'a> Context<'a> {
self.export_table();
self.gc();
while js.contains("\n\n\n") {
js = js.replace("\n\n\n", "\n\n");
}
(js, self.typescript.clone())
}
@ -539,7 +543,7 @@ impl<'a> Context<'a> {
return;
")
};
self.globals.push_str(&format!("
self.global(&format!("
function dropRef(idx) {{
{}
@ -557,7 +561,7 @@ impl<'a> Context<'a> {
if !self.exposed_globals.insert("stack") {
return;
}
self.globals.push_str(&format!("
self.global(&format!("
let stack = [];
"));
}
@ -566,14 +570,14 @@ impl<'a> Context<'a> {
if !self.exposed_globals.insert("slab") {
return;
}
self.globals.push_str(&format!("let slab = [];"));
self.global(&format!("let slab = [];"));
}
fn expose_global_slab_next(&mut self) {
if !self.exposed_globals.insert("slab_next") {
return;
}
self.globals.push_str(&format!("
self.global(&format!("
let slab_next = 0;
"));
}
@ -596,7 +600,7 @@ impl<'a> Context<'a> {
return val.obj;
")
};
self.globals.push_str(&format!("
self.global(&format!("
function getObject(idx) {{
if ((idx & 1) === 1) {{
return stack[idx >> 1];
@ -612,7 +616,7 @@ impl<'a> Context<'a> {
if !self.exposed_globals.insert("assert_num") {
return;
}
self.globals.push_str(&format!("
self.global(&format!("
function _assertNum(n) {{
if (typeof(n) !== 'number')
throw new Error('expected a number argument');
@ -624,7 +628,7 @@ impl<'a> Context<'a> {
if !self.exposed_globals.insert("assert_bool") {
return;
}
self.globals.push_str(&format!("
self.global(&format!("
function _assertBoolean(n) {{
if (typeof(n) !== 'boolean')
throw new Error('expected a boolean argument');
@ -647,7 +651,7 @@ impl<'a> Context<'a> {
} else {
""
};
self.globals.push_str(&format!("
self.global(&format!("
function passStringToWasm(arg) {{
{}
const buf = cachedEncoder.encode(arg);
@ -664,7 +668,7 @@ impl<'a> Context<'a> {
}
self.required_internal_exports.insert("__wbindgen_malloc");
self.expose_uint8_memory();
self.globals.push_str(&format!("
self.global(&format!("
function passArray8ToWasm(arg) {{
const ptr = wasm.__wbindgen_malloc(arg.byteLength);
getUint8Memory().set(arg, ptr);
@ -679,7 +683,7 @@ impl<'a> Context<'a> {
}
self.required_internal_exports.insert("__wbindgen_malloc");
self.expose_uint16_memory();
self.globals.push_str(&format!("
self.global(&format!("
function passArray16ToWasm(arg) {{
const ptr = wasm.__wbindgen_malloc(arg.byteLength);
getUint16Memory().set(arg, ptr / 2);
@ -694,7 +698,7 @@ impl<'a> Context<'a> {
}
self.required_internal_exports.insert("__wbindgen_malloc");
self.expose_uint32_memory();
self.globals.push_str(&format!("
self.global(&format!("
function passArray32ToWasm(arg) {{
const ptr = wasm.__wbindgen_malloc(arg.byteLength);
getUint32Memory().set(arg, ptr / 4);
@ -708,7 +712,7 @@ impl<'a> Context<'a> {
return;
}
self.required_internal_exports.insert("__wbindgen_malloc");
self.globals.push_str(&format!("
self.global(&format!("
function passArrayF32ToWasm(arg) {{
const ptr = wasm.__wbindgen_malloc(arg.byteLength);
new Float32Array(wasm.memory.buffer).set(arg, ptr / 4);
@ -722,7 +726,7 @@ impl<'a> Context<'a> {
return;
}
self.required_internal_exports.insert("__wbindgen_malloc");
self.globals.push_str(&format!("
self.global(&format!("
function passArrayF64ToWasm(arg) {{
const ptr = wasm.__wbindgen_malloc(arg.byteLength);
new Float64Array(wasm.memory.buffer).set(arg, ptr / 8);
@ -736,17 +740,17 @@ impl<'a> Context<'a> {
return;
}
if self.config.nodejs {
self.globals.push_str(&format!("
self.global(&format!("
const TextEncoder = require('util').TextEncoder;
"));
} else if !(self.config.browser || self.config.no_modules) {
self.globals.push_str(&format!("
self.global(&format!("
const TextEncoder = typeof window === 'object' && window.TextEncoder
? window.TextEncoder
: require('util').TextEncoder;
"));
}
self.globals.push_str(&format!("
self.global(&format!("
let cachedEncoder = new TextEncoder('utf-8');
"));
}
@ -756,17 +760,17 @@ impl<'a> Context<'a> {
return;
}
if self.config.nodejs {
self.globals.push_str(&format!("
self.global(&format!("
const TextDecoder = require('util').TextDecoder;
"));
} else if !(self.config.browser || self.config.no_modules) {
self.globals.push_str(&format!("
self.global(&format!("
const TextDecoder = typeof window === 'object' && window.TextDecoder
? window.TextDecoder
: require('util').TextDecoder;
"));
}
self.globals.push_str(&format!("
self.global(&format!("
let cachedDecoder = new TextDecoder('utf-8');
"));
}
@ -776,7 +780,7 @@ impl<'a> Context<'a> {
return;
}
self.globals.push_str("
self.global("
class ConstructorToken {
constructor(ptr) {
this.ptr = ptr;
@ -791,7 +795,7 @@ impl<'a> Context<'a> {
}
self.expose_text_decoder();
self.expose_uint8_memory();
self.globals.push_str(&format!("
self.global(&format!("
function getStringFromWasm(ptr, len) {{
return cachedDecoder.decode(getUint8Memory().slice(ptr, ptr + len));
}}
@ -804,7 +808,7 @@ impl<'a> Context<'a> {
}
self.expose_get_array_u32_from_wasm();
self.expose_get_object();
self.globals.push_str(&format!("
self.global(&format!("
function getArrayJsValueFromWasm(ptr, len) {{
const mem = getUint32Memory();
const slice = mem.slice(ptr / 4, ptr / 4 + len);
@ -822,7 +826,7 @@ impl<'a> Context<'a> {
if !self.exposed_globals.insert("get_array_i8_from_wasm") {
return;
}
self.globals.push_str(&format!("
self.global(&format!("
function getArrayI8FromWasm(ptr, len) {{
const mem = getUint8Memory();
const slice = mem.slice(ptr, ptr + len);
@ -836,7 +840,7 @@ impl<'a> Context<'a> {
if !self.exposed_globals.insert("get_array_u8_from_wasm") {
return;
}
self.globals.push_str(&format!("
self.global(&format!("
function getArrayU8FromWasm(ptr, len) {{
const mem = getUint8Memory();
const slice = mem.slice(ptr, ptr + len);
@ -850,7 +854,7 @@ impl<'a> Context<'a> {
if !self.exposed_globals.insert("get_array_i16_from_wasm") {
return;
}
self.globals.push_str(&format!("
self.global(&format!("
function getArrayI16FromWasm(ptr, len) {{
const mem = getUint16Memory();
const slice = mem.slice(ptr / 2, ptr / 2 + len);
@ -864,7 +868,7 @@ impl<'a> Context<'a> {
if !self.exposed_globals.insert("get_array_u16_from_wasm") {
return;
}
self.globals.push_str(&format!("
self.global(&format!("
function getArrayU16FromWasm(ptr, len) {{
const mem = getUint16Memory();
const slice = mem.slice(ptr / 2, ptr / 2 + len);
@ -878,7 +882,7 @@ impl<'a> Context<'a> {
if !self.exposed_globals.insert("get_array_i32_from_wasm") {
return;
}
self.globals.push_str(&format!("
self.global(&format!("
function getArrayI32FromWasm(ptr, len) {{
const mem = getUint32Memory();
const slice = mem.slice(ptr / 4, ptr / 4 + len);
@ -892,7 +896,7 @@ impl<'a> Context<'a> {
if !self.exposed_globals.insert("get_array_u32_from_wasm") {
return;
}
self.globals.push_str(&format!("
self.global(&format!("
function getArrayU32FromWasm(ptr, len) {{
const mem = getUint32Memory();
const slice = mem.slice(ptr / 4, ptr / 4 + len);
@ -905,7 +909,7 @@ impl<'a> Context<'a> {
if !self.exposed_globals.insert("get_array_f32_from_wasm") {
return;
}
self.globals.push_str(&format!("
self.global(&format!("
function getArrayF32FromWasm(ptr, len) {{
const mem = new Float32Array(wasm.memory.buffer);
const slice = mem.slice(ptr / 4, ptr / 4 + len);
@ -918,7 +922,7 @@ impl<'a> Context<'a> {
if !self.exposed_globals.insert("get_array_f64_from_wasm") {
return;
}
self.globals.push_str(&format!("
self.global(&format!("
function getArrayF64FromWasm(ptr, len) {{
const mem = new Float64Array(wasm.memory.buffer);
const slice = mem.slice(ptr / 8, ptr / 8 + len);
@ -931,7 +935,7 @@ impl<'a> Context<'a> {
if !self.exposed_globals.insert("uint8_memory") {
return;
}
self.globals.push_str(&format!("
self.global(&format!("
let cachedUint8Memory = null;
function getUint8Memory() {{
if (cachedUint8Memory === null ||
@ -946,7 +950,7 @@ impl<'a> Context<'a> {
if !self.exposed_globals.insert("uint16_memory") {
return;
}
self.globals.push_str(&format!("
self.global(&format!("
let cachedUint16Memory = null;
function getUint16Memory() {{
if (cachedUint16Memory === null ||
@ -961,7 +965,7 @@ impl<'a> Context<'a> {
if !self.exposed_globals.insert("uint32_memory") {
return;
}
self.globals.push_str(&format!("
self.global(&format!("
let cachedUint32Memory = null;
function getUint32Memory() {{
if (cachedUint32Memory === null ||
@ -976,7 +980,7 @@ impl<'a> Context<'a> {
if !self.exposed_globals.insert("assert_class") {
return;
}
self.globals.push_str(&format!("
self.global(&format!("
function _assertClass(instance, klass) {{
if (!(instance instanceof klass))
throw new Error(`expected instance of ${{klass.name}}`);
@ -990,7 +994,7 @@ impl<'a> Context<'a> {
return;
}
self.expose_global_stack();
self.globals.push_str(&format!("
self.global(&format!("
function addBorrowedObject(obj) {{
stack.push(obj);
return ((stack.length - 1) << 1) | 1;
@ -1004,7 +1008,7 @@ impl<'a> Context<'a> {
}
self.expose_get_object();
self.expose_drop_ref();
self.globals.push_str(&format!("
self.global(&format!("
function takeObject(idx) {{
const ret = getObject(idx);
dropRef(idx);
@ -1030,7 +1034,7 @@ impl<'a> Context<'a> {
slab_next = next;
")
};
self.globals.push_str(&format!("
self.global(&format!("
function addHeapObject(obj) {{
if (slab_next === slab.length)
slab.push(slab.length + 1);
@ -1140,7 +1144,7 @@ impl<'a> Context<'a> {
}
self.expose_uint32_memory();
self.expose_global_argument_ptr();
self.globals.push_str("
self.global("
function setGlobalArgument(arg, i) {
const idx = globalArgumentPtr() / 4 + i;
getUint32Memory()[idx] = arg;
@ -1154,7 +1158,7 @@ impl<'a> Context<'a> {
}
self.expose_uint32_memory();
self.expose_global_argument_ptr();
self.globals.push_str("
self.global("
function getGlobalArgument(arg) {
const idx = globalArgumentPtr() / 4 + arg;
return getUint32Memory()[idx];
@ -1167,7 +1171,7 @@ impl<'a> Context<'a> {
return;
}
self.required_internal_exports.insert("__wbindgen_global_argument_ptr");
self.globals.push_str("
self.global("
let cachedGlobalArgumentPtr = null;
function globalArgumentPtr() {
if (cachedGlobalArgumentPtr === null)
@ -1190,7 +1194,7 @@ impl<'a> Context<'a> {
// As a result we have a small helper here which will walk the prototype
// chain looking for a descriptor. For some more information on this see
// #109
self.globals.push_str("
self.global("
function GetOwnOrInheritedPropertyDescriptor(obj, id) {
while (obj) {
let desc = Object.getOwnPropertyDescriptor(obj, id);
@ -1248,6 +1252,20 @@ impl<'a> Context<'a> {
_ => panic!("unimplemented return from JS to Rust: {:?}", ty),
}
}
fn global(&mut self, s: &str) {
let amt_to_strip = s.lines()
.filter(|l| !l.trim().is_empty())
.map(|s| s.len() - s.trim_left().len())
.min()
.unwrap_or(0);
for line in s.lines() {
if !line.trim().is_empty() {
self.globals.push_str(&line[amt_to_strip..]);
}
self.globals.push_str("\n");
}
}
}
impl<'a, 'b> SubContext<'a, 'b> {
@ -1529,14 +1547,14 @@ impl<'a, 'b> SubContext<'a, 'b> {
format!("{}.prototype.{}", class, function_name)
}
};
self.cx.globals.push_str(&format!("
self.cx.global(&format!("
const {}_target = {};
", import.shim, target));
format!("{}_target.call", import.shim)
}
Some(ref class) => {
let class = self.import_name(info, class);
self.cx.globals.push_str(&format!("
self.cx.global(&format!("
const {}_target = {}.{};
", import.shim, class, function_name));
format!("{}_target", import.shim)
@ -1544,7 +1562,7 @@ impl<'a, 'b> SubContext<'a, 'b> {
None => {
let name = self.import_name(info, function_name);
if name.contains(".") {
self.cx.globals.push_str(&format!("
self.cx.global(&format!("
const {}_target = {};
", import.shim, name));
format!("{}_target", import.shim)