From 978b5ada63e2f57dbee0cab74d194e8f062026da Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Tue, 3 Apr 2018 12:44:09 -0700 Subject: [PATCH] Tighten up passStringToWasm a bit Only emit `throw` on debug mode primarily --- crates/cli-support/src/js.rs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/crates/cli-support/src/js.rs b/crates/cli-support/src/js.rs index 364b40e7..650cd7bb 100644 --- a/crates/cli-support/src/js.rs +++ b/crates/cli-support/src/js.rs @@ -545,17 +545,23 @@ impl<'a> Context<'a> { self.required_internal_exports.insert("__wbindgen_malloc"); self.expose_text_encoder(); self.expose_uint8_memory(); - self.globals.push_str(&format!(" - function passStringToWasm(arg) {{ + let debug = if self.config.debug { + " if (typeof(arg) !== 'string') throw new Error('expected a string argument'); + " + } else { + "" + }; + self.globals.push_str(&format!(" + function passStringToWasm(arg) {{ + {} const buf = textEncoder().encode(arg); - const len = buf.length; - const ptr = wasm.__wbindgen_malloc(len); - getUint8Memory().set(buf, ptr); + const ptr = wasm.__wbindgen_malloc(buf.length); + getUint8Memory().set(buf, buf.length); return [ptr, len]; }} - ")); + ", debug)); } fn expose_pass_array8_to_wasm(&mut self) {