From 28c3fdddf5e6fab9b318c0608c0d104093c1cf81 Mon Sep 17 00:00:00 2001
From: Alex Crichton <alex@alexcrichton.com>
Date: Wed, 28 Mar 2018 07:37:56 -0700
Subject: [PATCH] Remove --nodejs-runtime-detect

Instead use it by default and add a --browser argument to explicity
remove the shim.

Closes #79
---
 crates/test-support/src/lib.rs                |   2 -
 crates/wasm-bindgen-cli-support/src/js.rs     | 116 +++++++-----------
 crates/wasm-bindgen-cli-support/src/lib.rs    |   8 +-
 .../wasm-bindgen-cli/src/bin/wasm-bindgen.rs  |   8 +-
 4 files changed, 51 insertions(+), 83 deletions(-)

diff --git a/crates/test-support/src/lib.rs b/crates/test-support/src/lib.rs
index 04c789bd..ec6a0b6f 100644
--- a/crates/test-support/src/lib.rs
+++ b/crates/test-support/src/lib.rs
@@ -193,8 +193,6 @@ impl Project {
 
         cli::Bindgen::new()
             .input_path(&as_a_module)
-            .nodejs(true)
-            .nodejs_runtime_detect(self.detect_node)
             .typescript(true)
             .debug(self.debug)
             .generate(&root)
diff --git a/crates/wasm-bindgen-cli-support/src/js.rs b/crates/wasm-bindgen-cli-support/src/js.rs
index f8ddba53..12894da0 100644
--- a/crates/wasm-bindgen-cli-support/src/js.rs
+++ b/crates/wasm-bindgen-cli-support/src/js.rs
@@ -506,46 +506,19 @@ impl<'a> Context<'a> {
             return
         }
         self.required_internal_exports.insert("__wbindgen_malloc");
-        if self.config.nodejs_runtime_detect || self.config.nodejs {
-            self.globals.push_str(&format!("
-                function passStringToWasmNode(arg) {{
-                    if (typeof(arg) !== 'string')
-                        throw new Error('expected a string argument');
-                    const buf = Buffer.from(arg);
-                    const len = buf.length;
-                    const ptr = wasm.__wbindgen_malloc(len);
-                    buf.copy(Buffer.from(wasm.memory.buffer), ptr);
-                    return [ptr, len];
-                }}
-            "));
-        }
-        if self.config.nodejs_runtime_detect || !self.config.nodejs {
-            self.expose_text_encoder();
-            self.expose_uint8_memory();
-            self.globals.push_str(&format!("
-                function passStringToWasmBrowser(arg) {{
-                    if (typeof(arg) !== 'string')
-                        throw new Error('expected a string argument');
-                    const buf = textEncoder().encode(arg);
-                    const len = buf.length;
-                    const ptr = wasm.__wbindgen_malloc(len);
-                    getUint8Memory().set(buf, ptr);
-                    return [ptr, len];
-                }}
-            "));
-        }
-
-        if self.config.nodejs_runtime_detect {
-            self.globals.push_str("
-                let passStringToWasm = passStringToWasmBrowser;
-                if (typeof window === 'undefined')
-                    passStringToWasm = passStringToWasmNode;
-            ");
-        } else if self.config.nodejs {
-            self.globals.push_str("const passStringToWasm = passStringToWasmNode;\n");
-        } else {
-            self.globals.push_str("const passStringToWasm = passStringToWasmBrowser;\n");
-        }
+        self.expose_text_encoder();
+        self.expose_uint8_memory();
+        self.globals.push_str(&format!("
+            function passStringToWasm(arg) {{
+                if (typeof(arg) !== 'string')
+                    throw new Error('expected a string argument');
+                const buf = textEncoder().encode(arg);
+                const len = buf.length;
+                const ptr = wasm.__wbindgen_malloc(len);
+                getUint8Memory().set(buf, ptr);
+                return [ptr, len];
+            }}
+        "));
     }
 
     fn expose_pass_array8_to_wasm(&mut self) {
@@ -625,6 +598,16 @@ impl<'a> Context<'a> {
         if !self.exposed_globals.insert("text_encoder") {
             return
         }
+        if self.config.nodejs {
+            self.globals.push_str(&format!("
+                const TextEncoder = require('util');
+            "));
+        } else if !self.config.browser {
+            self.globals.push_str(&format!("
+                if (typeof window === 'undefined')
+                    var TextEncoder = require('util').TextEncoder;
+            "));
+        }
         self.globals.push_str(&format!("
             let cachedEncoder = null;
             function textEncoder() {{
@@ -640,6 +623,16 @@ impl<'a> Context<'a> {
         if !self.exposed_globals.insert("text_decoder") {
             return
         }
+        if self.config.nodejs {
+            self.globals.push_str(&format!("
+                const TextDecoder = require('util');
+            "));
+        } else if !self.config.browser {
+            self.globals.push_str(&format!("
+                if (typeof window === 'undefined')
+                    var TextDecoder = require('util').TextDecoder;
+            "));
+        }
         self.globals.push_str(&format!("
             let cachedDecoder = null;
             function textDecoder() {{
@@ -655,39 +648,16 @@ impl<'a> Context<'a> {
         if !self.exposed_globals.insert("get_string_from_wasm") {
             return
         }
-        if self.config.nodejs_runtime_detect || self.config.nodejs {
-            self.globals.push_str(&format!("
-                function getStringFromWasmNode(ptr, len) {{
-                    const buf = Buffer.from(wasm.memory.buffer).slice(ptr, ptr + len);
-                    const ret = buf.toString();
-                    return ret;
-                }}
-            "));
-        }
-        if self.config.nodejs_runtime_detect || !self.config.nodejs {
-            self.expose_text_decoder();
-            self.expose_uint8_memory();
-            self.globals.push_str(&format!("
-                function getStringFromWasmBrowser(ptr, len) {{
-                    const mem = getUint8Memory();
-                    const slice = mem.slice(ptr, ptr + len);
-                    const ret = textDecoder().decode(slice);
-                    return ret;
-                }}
-            "));
-        }
-
-        if self.config.nodejs_runtime_detect {
-            self.globals.push_str("
-                let getStringFromWasm = getStringFromWasmBrowser;
-                if (typeof window === 'undefined')
-                    getStringFromWasm = getStringFromWasmNode;
-            ");
-        } else if self.config.nodejs {
-            self.globals.push_str("const getStringFromWasm = getStringFromWasmNode;\n");
-        } else {
-            self.globals.push_str("const getStringFromWasm = getStringFromWasmBrowser;\n");
-        }
+        self.expose_text_decoder();
+        self.expose_uint8_memory();
+        self.globals.push_str(&format!("
+            function getStringFromWasm(ptr, len) {{
+                const mem = getUint8Memory();
+                const slice = mem.slice(ptr, ptr + len);
+                const ret = textDecoder().decode(slice);
+                return ret;
+            }}
+        "));
     }
 
     fn expose_get_array_js_value_from_wasm(&mut self) {
diff --git a/crates/wasm-bindgen-cli-support/src/lib.rs b/crates/wasm-bindgen-cli-support/src/lib.rs
index bd143ef5..4c7296cc 100644
--- a/crates/wasm-bindgen-cli-support/src/lib.rs
+++ b/crates/wasm-bindgen-cli-support/src/lib.rs
@@ -17,7 +17,7 @@ pub mod wasm2es6js;
 pub struct Bindgen {
     path: Option<PathBuf>,
     nodejs: bool,
-    nodejs_runtime_detect: bool,
+    browser: bool,
     debug: bool,
     typescript: bool,
 }
@@ -36,7 +36,7 @@ impl Bindgen {
         Bindgen {
             path: None,
             nodejs: false,
-            nodejs_runtime_detect: false,
+            browser: false,
             debug: false,
             typescript: false,
         }
@@ -52,8 +52,8 @@ impl Bindgen {
         self
     }
 
-    pub fn nodejs_runtime_detect(&mut self, detect: bool) -> &mut Bindgen {
-        self.nodejs_runtime_detect = detect;
+    pub fn browser(&mut self, browser: bool) -> &mut Bindgen {
+        self.browser = browser;
         self
     }
 
diff --git a/crates/wasm-bindgen-cli/src/bin/wasm-bindgen.rs b/crates/wasm-bindgen-cli/src/bin/wasm-bindgen.rs
index b9bc21b1..95baaad2 100644
--- a/crates/wasm-bindgen-cli/src/bin/wasm-bindgen.rs
+++ b/crates/wasm-bindgen-cli/src/bin/wasm-bindgen.rs
@@ -20,8 +20,8 @@ Usage:
 Options:
     -h --help                Show this screen.
     --out-dir DIR            Output directory
-    --nodejs                 Generate output for node.js, not the browser
-    --nodejs-runtime-detect  Detect at runtime whether we're in node or a browser
+    --nodejs                 Generate output that only works in node.js
+    --browser                Generate output that only works in a browser
     --typescript             Output a TypeScript definition file
     --debug                  Include otherwise-extraneous debug checks in output
     -V --version             Print the version number of wasm-bindgen
@@ -30,7 +30,7 @@ Options:
 #[derive(Debug, Deserialize)]
 struct Args {
     flag_nodejs: bool,
-    flag_nodejs_runtime_detect: bool,
+    flag_browser: bool,
     flag_typescript: bool,
     flag_out_dir: Option<PathBuf>,
     flag_debug: bool,
@@ -56,7 +56,7 @@ fn main() {
     let mut b = Bindgen::new();
     b.input_path(&input)
      .nodejs(args.flag_nodejs)
-     .nodejs_runtime_detect(args.flag_nodejs_runtime_detect)
+     .browser(args.browser)
      .debug(args.flag_debug)
      .typescript(args.flag_typescript);