fix up some strings that looked funky after rustfmt

This commit is contained in:
R. Andrew Ohana
2018-06-15 23:39:51 -07:00
parent 9127a0419f
commit 7626b55d00
5 changed files with 193 additions and 180 deletions

View File

@ -137,10 +137,13 @@ impl<'a> Context<'a> {
self.bind("__wbindgen_object_drop_ref", &|me| { self.bind("__wbindgen_object_drop_ref", &|me| {
me.expose_drop_ref(); me.expose_drop_ref();
Ok("function(i) { Ok(String::from(
"
function(i) {
dropRef(i); dropRef(i);
}" }
.to_string()) ",
))
})?; })?;
self.bind("__wbindgen_string_new", &|me| { self.bind("__wbindgen_string_new", &|me| {
@ -158,33 +161,37 @@ impl<'a> Context<'a> {
self.bind("__wbindgen_number_new", &|me| { self.bind("__wbindgen_number_new", &|me| {
me.expose_add_heap_object(); me.expose_add_heap_object();
Ok(String::from( Ok(String::from(
"function(i) { "
function(i) {
return addHeapObject(i); return addHeapObject(i);
}", }
",
)) ))
})?; })?;
self.bind("__wbindgen_number_get", &|me| { self.bind("__wbindgen_number_get", &|me| {
me.expose_get_object(); me.expose_get_object();
me.expose_uint8_memory(); me.expose_uint8_memory();
Ok(format!( Ok(String::from(
" "
function(n, invalid) {{ function(n, invalid) {
let obj = getObject(n); let obj = getObject(n);
if (typeof(obj) === 'number') return obj; if (typeof(obj) === 'number') return obj;
getUint8Memory()[invalid] = 1; getUint8Memory()[invalid] = 1;
return 0; return 0;
}} }
" ",
)) ))
})?; })?;
self.bind("__wbindgen_undefined_new", &|me| { self.bind("__wbindgen_undefined_new", &|me| {
me.expose_add_heap_object(); me.expose_add_heap_object();
Ok(String::from( Ok(String::from(
"function() { "
function() {
return addHeapObject(undefined); return addHeapObject(undefined);
}", }
",
)) ))
})?; })?;
@ -251,18 +258,18 @@ impl<'a> Context<'a> {
self.bind("__wbindgen_symbol_new", &|me| { self.bind("__wbindgen_symbol_new", &|me| {
me.expose_get_string_from_wasm(); me.expose_get_string_from_wasm();
me.expose_add_heap_object(); me.expose_add_heap_object();
Ok(format!( Ok(String::from(
" "
function(ptr, len) {{ function(ptr, len) {
let a; let a;
if (ptr === 0) {{ if (ptr === 0) {
a = Symbol(); a = Symbol();
}} else {{ } else {
a = Symbol(getStringFromWasm(ptr, len)); a = Symbol(getStringFromWasm(ptr, len));
}} }
return addHeapObject(a); return addHeapObject(a);
}} }
" ",
)) ))
})?; })?;
@ -365,19 +372,20 @@ impl<'a> Context<'a> {
// isn't gc'd). // isn't gc'd).
self.bind("__wbindgen_throw", &|me| { self.bind("__wbindgen_throw", &|me| {
me.expose_get_string_from_wasm(); me.expose_get_string_from_wasm();
Ok(format!( Ok(String::from(
" "
function(ptr, len) {{ function(ptr, len) {
throw new Error(getStringFromWasm(ptr, len)); throw new Error(getStringFromWasm(ptr, len));
}} }
" ",
)) ))
})?; })?;
self.rewrite_imports(module_name); self.rewrite_imports(module_name);
let mut js = if self.config.no_modules { let mut js = if self.config.no_modules {
format!(" format!(
"
(function() {{ (function() {{
var wasm; var wasm;
const __exports = {{}}; const __exports = {{}};
@ -808,11 +816,11 @@ impl<'a> Context<'a> {
return; return;
} }
self.expose_global_slab(); self.expose_global_slab();
self.global(&format!( self.global(
" "
let slab_next = slab.length; let slab_next = slab.length;
" ",
)); );
} }
fn expose_get_object(&mut self) { fn expose_get_object(&mut self) {
@ -969,25 +977,25 @@ impl<'a> Context<'a> {
return; return;
} }
if self.config.nodejs { if self.config.nodejs {
self.global(&format!( self.global(
" "
const TextEncoder = require('util').TextEncoder; const TextEncoder = require('util').TextEncoder;
" ",
)); );
} else if !(self.config.browser || self.config.no_modules) { } else if !(self.config.browser || self.config.no_modules) {
self.global(&format!( self.global(
" "
const TextEncoder = typeof self === 'object' && self.TextEncoder const TextEncoder = typeof self === 'object' && self.TextEncoder
? self.TextEncoder ? self.TextEncoder
: require('util').TextEncoder; : require('util').TextEncoder;
" ",
)); );
} }
self.global(&format!( self.global(
" "
let cachedEncoder = new TextEncoder('utf-8'); let cachedEncoder = new TextEncoder('utf-8');
" ",
)); );
} }
fn expose_text_decoder(&mut self) { fn expose_text_decoder(&mut self) {
@ -995,25 +1003,25 @@ impl<'a> Context<'a> {
return; return;
} }
if self.config.nodejs { if self.config.nodejs {
self.global(&format!( self.global(
" "
const TextDecoder = require('util').TextDecoder; const TextDecoder = require('util').TextDecoder;
" ",
)); );
} else if !(self.config.browser || self.config.no_modules) { } else if !(self.config.browser || self.config.no_modules) {
self.global(&format!( self.global(
" "
const TextDecoder = typeof self === 'object' && self.TextDecoder const TextDecoder = typeof self === 'object' && self.TextDecoder
? self.TextDecoder ? self.TextDecoder
: require('util').TextDecoder; : require('util').TextDecoder;
" ",
)); );
} }
self.global(&format!( self.global(
" "
let cachedDecoder = new TextDecoder('utf-8'); let cachedDecoder = new TextDecoder('utf-8');
" ",
)); );
} }
fn expose_constructor_token(&mut self) { fn expose_constructor_token(&mut self) {
@ -1038,13 +1046,13 @@ impl<'a> Context<'a> {
} }
self.expose_text_decoder(); self.expose_text_decoder();
self.expose_uint8_memory(); self.expose_uint8_memory();
self.global(&format!( self.global(
" "
function getStringFromWasm(ptr, len) {{ function getStringFromWasm(ptr, len) {
return cachedDecoder.decode(getUint8Memory().subarray(ptr, ptr + len)); return cachedDecoder.decode(getUint8Memory().subarray(ptr, ptr + len));
}} }
" ",
)); );
} }
fn expose_get_array_js_value_from_wasm(&mut self) { fn expose_get_array_js_value_from_wasm(&mut self) {
@ -1053,19 +1061,19 @@ impl<'a> Context<'a> {
} }
self.expose_get_array_u32_from_wasm(); self.expose_get_array_u32_from_wasm();
self.expose_take_object(); self.expose_take_object();
self.global(&format!( self.global(
" "
function getArrayJsValueFromWasm(ptr, len) {{ function getArrayJsValueFromWasm(ptr, len) {
const mem = getUint32Memory(); const mem = getUint32Memory();
const slice = mem.subarray(ptr / 4, ptr / 4 + len); const slice = mem.subarray(ptr / 4, ptr / 4 + len);
const result = []; const result = [];
for (let i = 0; i < slice.length; i++) {{ for (let i = 0; i < slice.length; i++) {
result.push(takeObject(slice[i])); result.push(takeObject(slice[i]));
}} }
return result; return result;
}} }
" ",
)); );
} }
fn expose_get_array_i8_from_wasm(&mut self) { fn expose_get_array_i8_from_wasm(&mut self) {
@ -1250,16 +1258,16 @@ impl<'a> Context<'a> {
if !self.exposed_globals.insert("assert_class") { if !self.exposed_globals.insert("assert_class") {
return; return;
} }
self.global(&format!( self.global(
" "
function _assertClass(instance, klass) {{ function _assertClass(instance, klass) {
if (!(instance instanceof klass)) {{ if (!(instance instanceof klass)) {
throw new Error(`expected instance of ${{klass.name}}`); throw new Error(`expected instance of ${klass.name}`);
}} }
return instance.ptr; return instance.ptr;
}} }
" ",
)); );
} }
fn expose_borrowed_objects(&mut self) { fn expose_borrowed_objects(&mut self) {
@ -1267,14 +1275,14 @@ impl<'a> Context<'a> {
return; return;
} }
self.expose_global_stack(); self.expose_global_stack();
self.global(&format!( self.global(
" "
function addBorrowedObject(obj) {{ function addBorrowedObject(obj) {
stack.push(obj); stack.push(obj);
return ((stack.length - 1) << 1) | 1; return ((stack.length - 1) << 1) | 1;
}} }
" ",
)); );
} }
fn expose_take_object(&mut self) { fn expose_take_object(&mut self) {
@ -1283,15 +1291,15 @@ impl<'a> Context<'a> {
} }
self.expose_get_object(); self.expose_get_object();
self.expose_drop_ref(); self.expose_drop_ref();
self.global(&format!( self.global(
" "
function takeObject(idx) {{ function takeObject(idx) {
const ret = getObject(idx); const ret = getObject(idx);
dropRef(idx); dropRef(idx);
return ret; return ret;
}} }
" ",
)); );
} }
fn expose_add_heap_object(&mut self) { fn expose_add_heap_object(&mut self) {
@ -1455,9 +1463,9 @@ impl<'a> Context<'a> {
" "
let cachedGlobalArgumentPtr = null; let cachedGlobalArgumentPtr = null;
function globalArgumentPtr() { function globalArgumentPtr() {
if (cachedGlobalArgumentPtr === null) {{ if (cachedGlobalArgumentPtr === null) {
cachedGlobalArgumentPtr = wasm.__wbindgen_global_argument_ptr(); cachedGlobalArgumentPtr = wasm.__wbindgen_global_argument_ptr();
}} }
return cachedGlobalArgumentPtr; return cachedGlobalArgumentPtr;
} }
", ",

View File

@ -224,11 +224,13 @@ impl Output {
} }
} }
let inst = format!( let inst = format!(
"WebAssembly.instantiate(bytes,{{ {imports} }}) "
WebAssembly.instantiate(bytes,{{ {imports} }})
.then(obj => {{ .then(obj => {{
wasm = obj.instance; wasm = obj.instance;
{memory} {memory}
}})", }})
",
imports = imports, imports = imports,
memory = if export_mem { memory = if export_mem {
"memory = wasm.exports.memory;" "memory = wasm.exports.memory;"
@ -247,7 +249,8 @@ impl Output {
bytes = Uint8Array.from(atob(base64), c => c.charCodeAt(0)); bytes = Uint8Array.from(atob(base64), c => c.charCodeAt(0));
}} else {{ }} else {{
bytes = Buffer.from(base64, 'base64'); bytes = Buffer.from(base64, 'base64');
}}", }}
",
base64 = base64::encode(&wasm) base64 = base64::encode(&wasm)
), ),
inst, inst,
@ -256,9 +259,11 @@ impl Output {
( (
String::new(), String::new(),
format!( format!(
"fetch('{path}') "
fetch('{path}')
.then(res => res.arrayBuffer()) .then(res => res.arrayBuffer())
.then(bytes => {inst})", .then(bytes => {inst})
",
path = path, path = path,
inst = inst inst = inst
), ),