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

View File

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