From 3a83b02de0bf95759c84f81e127bc33904fdb653 Mon Sep 17 00:00:00 2001 From: Sharad Chand Date: Wed, 4 Apr 2018 20:06:53 +0545 Subject: [PATCH 01/12] Added umd switch --- crates/cli-support/src/lib.rs | 7 +++++++ crates/cli/src/bin/wasm-bindgen.rs | 13 ++++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/crates/cli-support/src/lib.rs b/crates/cli-support/src/lib.rs index ad0e3b62..d43233b3 100644 --- a/crates/cli-support/src/lib.rs +++ b/crates/cli-support/src/lib.rs @@ -17,6 +17,7 @@ pub struct Bindgen { path: Option, nodejs: bool, browser: bool, + umd: bool, debug: bool, typescript: bool, } @@ -36,6 +37,7 @@ impl Bindgen { path: None, nodejs: false, browser: false, + umd: false, debug: false, typescript: false, } @@ -56,6 +58,11 @@ impl Bindgen { self } + pub fn umd(&mut self, umd: bool) -> &mut Bindgen { + self.umd = umd; + self + } + pub fn debug(&mut self, debug: bool) -> &mut Bindgen { self.debug = debug; self diff --git a/crates/cli/src/bin/wasm-bindgen.rs b/crates/cli/src/bin/wasm-bindgen.rs index b2a00f8f..f491dba1 100644 --- a/crates/cli/src/bin/wasm-bindgen.rs +++ b/crates/cli/src/bin/wasm-bindgen.rs @@ -22,6 +22,7 @@ Options: --out-dir DIR Output directory --nodejs Generate output that only works in node.js --browser Generate output that only works in a browser + --umd Generate output that works both in browser and node.js --typescript Output a TypeScript definition file --debug Include otherwise-extraneous debug checks in output -V --version Print the version number of wasm-bindgen @@ -31,6 +32,7 @@ Options: struct Args { flag_nodejs: bool, flag_browser: bool, + flag_umd: bool, flag_typescript: bool, flag_out_dir: Option, flag_debug: bool, @@ -45,7 +47,7 @@ fn main() { if args.flag_version { println!("wasm-bindgen {}", wasm_bindgen_shared::version()); - return + return; } let input = match args.arg_input { @@ -55,10 +57,11 @@ fn main() { let mut b = Bindgen::new(); b.input_path(&input) - .nodejs(args.flag_nodejs) - .browser(args.flag_browser) - .debug(args.flag_debug) - .typescript(args.flag_typescript); + .nodejs(args.flag_nodejs) + .browser(args.flag_browser) + .umd(args.flag_umd) + .debug(args.flag_debug) + .typescript(args.flag_typescript); let out_dir = match args.flag_out_dir { Some(ref p) => p, From 2877d0bdb6ca53c080c02a2091413b87c6141ed1 Mon Sep 17 00:00:00 2001 From: Sharad Chand Date: Thu, 5 Apr 2018 19:50:26 +0545 Subject: [PATCH 02/12] UMD import added --- crates/cli-support/src/js.rs | 82 ++++++++++++++++++++++++++++++------ 1 file changed, 69 insertions(+), 13 deletions(-) diff --git a/crates/cli-support/src/js.rs b/crates/cli-support/src/js.rs index b7f4d6d3..d4f3cdf3 100644 --- a/crates/cli-support/src/js.rs +++ b/crates/cli-support/src/js.rs @@ -47,6 +47,8 @@ impl<'a> Context<'a> { let contents = contents.trim(); let global = if self.config.nodejs { format!("module.exports.{} = {};\n", name, contents) + } else if self.config.umd { + format!("__exports.{} = {}\n", name, contents) } else { if contents.starts_with("function") { format!("export function {} {}\n", name, &contents[8..]) @@ -227,23 +229,77 @@ impl<'a> Context<'a> { self.footer.push_str(&format!("wasm = require('./{}_bg');", module_name)); format!("var wasm;") + } else if self.config.umd { + format!(" + if(typeof window === 'undefined' && typeof process === 'object') {{ + const fs = require('fs'); + const path = require('path'); + const wasm_path = path.join(__dirname, '{module}_bg.wasm'); + const buffer = fs.readFileSync(wasm_path); + const wasm = new WebAssembly.Module(buffer); + + return __initialize(wasm, false); + }} else {{ + return fetch('{module}_bg.wasm') + .then(response => response.arrayBuffer()) + .then(bytes => WebAssembly.compile(bytes)) + .then(wasm => __initialize(wasm, true)); + }}", module = module_name) } else { format!("import * as wasm from './{}_bg';", module_name) }; - let js = format!(" - /* tslint:disable */ - {import_wasm} - {imports} + let js = if self.config.umd { + format!(" + (function (root, factory) {{ + if (typeof define === 'function' && define.amd) {{ + define([], factory); + }} else if (typeof module === 'object' && module.exports) {{ + module.exports = factory(); + }} else {{ + root.{module} = factory(); + }} + }}(typeof self !== 'undefined' ? self : this, function() {{ + function __initialize(wasm, __load_async) {{ + const __js_exports = {{}}; + const __exports = {{}}; + {globals} + __js_exports['./{module}'] = __exports; - {globals} - {footer} - ", - import_wasm = import_wasm, - globals = self.globals, - imports = self.imports, - footer = self.footer, - ); + if (__load_async) {{ + return WebAssembly.instantiate(wasm, __js_exports) + .then(instance => {{ + return instance.exports; + }}) + .catch(error => {{ + console.log('Error loading wasm module `{module}`:', error); + throw error; + }}); + }} else {{ + const instance = new WebAssembly.Instance(wasm, __js_exports); + return instance.exports; + }} + }} + {import_wasm} + }})) + ", + module = module_name, + import_wasm = import_wasm, + ) + } else { + format!(" + /* tslint:disable */ + {import_wasm} + {imports} + + {globals} + {footer}", + import_wasm = import_wasm, + globals = self.globals, + imports = self.imports, + footer = self.footer, + ) + }; self.unexport_unused_internal_exports(); @@ -1623,7 +1679,7 @@ enum VectorKind { U32, F32, F64, - JsValue + JsValue, } impl VectorType { From 1a428d69daa7fd24a613159ca41dc3f099d729c5 Mon Sep 17 00:00:00 2001 From: Sharad Chand Date: Thu, 5 Apr 2018 20:30:58 +0545 Subject: [PATCH 03/12] {global} not referenced --- crates/cli-support/src/js.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/cli-support/src/js.rs b/crates/cli-support/src/js.rs index d4f3cdf3..dbed199d 100644 --- a/crates/cli-support/src/js.rs +++ b/crates/cli-support/src/js.rs @@ -284,6 +284,7 @@ impl<'a> Context<'a> { }})) ", module = module_name, + globals = self.globals, import_wasm = import_wasm, ) } else { From 2b9af530308b44c066515184c130352f72949842 Mon Sep 17 00:00:00 2001 From: Sharad Chand Date: Fri, 6 Apr 2018 12:30:38 +0545 Subject: [PATCH 04/12] wasm is compiled, mod in uncompiled --- crates/cli-support/src/js.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/crates/cli-support/src/js.rs b/crates/cli-support/src/js.rs index dbed199d..02de0952 100644 --- a/crates/cli-support/src/js.rs +++ b/crates/cli-support/src/js.rs @@ -236,14 +236,14 @@ impl<'a> Context<'a> { const path = require('path'); const wasm_path = path.join(__dirname, '{module}_bg.wasm'); const buffer = fs.readFileSync(wasm_path); - const wasm = new WebAssembly.Module(buffer); + const mod = new WebAssembly.Module(buffer); - return __initialize(wasm, false); + return __initialize(mod, false); }} else {{ return fetch('{module}_bg.wasm') .then(response => response.arrayBuffer()) .then(bytes => WebAssembly.compile(bytes)) - .then(wasm => __initialize(wasm, true)); + .then(mod => __initialize(mod, true)); }}", module = module_name) } else { format!("import * as wasm from './{}_bg';", module_name) @@ -260,15 +260,18 @@ impl<'a> Context<'a> { root.{module} = factory(); }} }}(typeof self !== 'undefined' ? self : this, function() {{ - function __initialize(wasm, __load_async) {{ + let wasm; + {import_wasm} + function __initialize(__mod, __load_async) {{ const __js_exports = {{}}; const __exports = {{}}; {globals} __js_exports['./{module}'] = __exports; if (__load_async) {{ - return WebAssembly.instantiate(wasm, __js_exports) + return WebAssembly.instantiate(__mod, __js_exports) .then(instance => {{ + wasm = instance.exports; return instance.exports; }}) .catch(error => {{ @@ -276,11 +279,11 @@ impl<'a> Context<'a> { throw error; }}); }} else {{ - const instance = new WebAssembly.Instance(wasm, __js_exports); + const instance = new WebAssembly.Instance(__mod, __js_exports); + wasm = instance.exports; return instance.exports; }} }} - {import_wasm} }})) ", module = module_name, From 0bd8713bd54003982f0e8cd57afc3854be9a4815 Mon Sep 17 00:00:00 2001 From: Sharad Chand Date: Sat, 7 Apr 2018 13:06:36 +0545 Subject: [PATCH 05/12] Dropped node support in umd -> amd --- crates/cli-support/src/js.rs | 159 ++++++++++++----------------- crates/cli-support/src/lib.rs | 8 +- crates/cli/src/bin/wasm-bindgen.rs | 4 +- 3 files changed, 74 insertions(+), 97 deletions(-) diff --git a/crates/cli-support/src/js.rs b/crates/cli-support/src/js.rs index 02de0952..1026b124 100644 --- a/crates/cli-support/src/js.rs +++ b/crates/cli-support/src/js.rs @@ -47,7 +47,7 @@ impl<'a> Context<'a> { let contents = contents.trim(); let global = if self.config.nodejs { format!("module.exports.{} = {};\n", name, contents) - } else if self.config.umd { + } else if self.config.amd { format!("__exports.{} = {}\n", name, contents) } else { if contents.starts_with("function") { @@ -66,7 +66,7 @@ impl<'a> Context<'a> { { let mut bind = |name: &str, f: &Fn(&mut Self) -> String| { if !self.wasm_import_needed(name) { - return + return; } let contents = f(self); let contents = contents.trim(); @@ -229,61 +229,39 @@ impl<'a> Context<'a> { self.footer.push_str(&format!("wasm = require('./{}_bg');", module_name)); format!("var wasm;") - } else if self.config.umd { + } else if self.config.amd { format!(" - if(typeof window === 'undefined' && typeof process === 'object') {{ - const fs = require('fs'); - const path = require('path'); - const wasm_path = path.join(__dirname, '{module}_bg.wasm'); - const buffer = fs.readFileSync(wasm_path); - const mod = new WebAssembly.Module(buffer); - - return __initialize(mod, false); - }} else {{ - return fetch('{module}_bg.wasm') - .then(response => response.arrayBuffer()) - .then(bytes => WebAssembly.compile(bytes)) - .then(mod => __initialize(mod, true)); - }}", module = module_name) + return fetch('{module}_bg.wasm') + .then(response => response.arrayBuffer()) + .then(buffer => WebAssembly.instantiate(buffer, __js_exports)) + .then(({{instance}}) => {{ + wasm = instance.exports; + return wasm; + }}) + .catch(error => {{ + console.log('Error loading wasm module `{module}`:', error); + throw error; + }}); + ", module = module_name) } else { format!("import * as wasm from './{}_bg';", module_name) }; - let js = if self.config.umd { + let js = if self.config.amd { format!(" (function (root, factory) {{ if (typeof define === 'function' && define.amd) {{ define([], factory); - }} else if (typeof module === 'object' && module.exports) {{ - module.exports = factory(); }} else {{ root.{module} = factory(); }} }}(typeof self !== 'undefined' ? self : this, function() {{ let wasm; + const __js_exports = {{}}; + const __exports = {{}}; + {globals} + __js_exports['./{module}'] = __exports; {import_wasm} - function __initialize(__mod, __load_async) {{ - const __js_exports = {{}}; - const __exports = {{}}; - {globals} - __js_exports['./{module}'] = __exports; - - if (__load_async) {{ - return WebAssembly.instantiate(__mod, __js_exports) - .then(instance => {{ - wasm = instance.exports; - return instance.exports; - }}) - .catch(error => {{ - console.log('Error loading wasm module `{module}`:', error); - throw error; - }}); - }} else {{ - const instance = new WebAssembly.Instance(__mod, __js_exports); - wasm = instance.exports; - return instance.exports; - }} - }} }})) ", module = module_name, @@ -380,7 +358,7 @@ impl<'a> Context<'a> { } fn _rewrite_imports(&mut self, module_name: &str) - -> Vec<(String, String)> + -> Vec<(String, String)> { let mut math_imports = Vec::new(); let imports = self.module.sections_mut() @@ -398,11 +376,11 @@ impl<'a> Context<'a> { import.module_mut().truncate(0); import.module_mut().push_str("./"); import.module_mut().push_str(module_name); - continue + continue; } if import.module() != "env" { - continue + continue; } let renamed_import = format!("__wbindgen_{}", import.field()); @@ -477,7 +455,7 @@ impl<'a> Context<'a> { fn expose_drop_ref(&mut self) { if !self.exposed_globals.insert("drop_ref") { - return + return; } self.expose_global_slab(); self.expose_global_slab_next(); @@ -520,7 +498,7 @@ impl<'a> Context<'a> { fn expose_global_stack(&mut self) { if !self.exposed_globals.insert("stack") { - return + return; } self.globals.push_str(&format!(" let stack = []; @@ -529,14 +507,14 @@ impl<'a> Context<'a> { fn expose_global_slab(&mut self) { if !self.exposed_globals.insert("slab") { - return + return; } self.globals.push_str(&format!("let slab = [];")); } fn expose_global_slab_next(&mut self) { if !self.exposed_globals.insert("slab_next") { - return + return; } self.globals.push_str(&format!(" let slab_next = 0; @@ -545,7 +523,7 @@ impl<'a> Context<'a> { fn expose_get_object(&mut self) { if !self.exposed_globals.insert("get_object") { - return + return; } self.expose_global_stack(); self.expose_global_slab(); @@ -575,7 +553,7 @@ impl<'a> Context<'a> { fn expose_check_token(&mut self) { if !self.exposed_globals.insert("check_token") { - return + return; } self.globals.push_str(&format!(" const token = Symbol('foo'); @@ -588,7 +566,7 @@ impl<'a> Context<'a> { fn expose_assert_num(&mut self) { if !self.exposed_globals.insert("assert_num") { - return + return; } self.globals.push_str(&format!(" function _assertNum(n) {{ @@ -600,7 +578,7 @@ impl<'a> Context<'a> { fn expose_assert_bool(&mut self) { if !self.exposed_globals.insert("assert_bool") { - return + return; } self.globals.push_str(&format!(" function _assertBoolean(n) {{ @@ -612,7 +590,7 @@ impl<'a> Context<'a> { fn expose_pass_string_to_wasm(&mut self) { if !self.exposed_globals.insert("pass_string_to_wasm") { - return + return; } self.required_internal_exports.insert("__wbindgen_malloc"); self.expose_text_encoder(); @@ -638,7 +616,7 @@ impl<'a> Context<'a> { fn expose_pass_array8_to_wasm(&mut self) { if !self.exposed_globals.insert("pass_array8_to_wasm") { - return + return; } self.required_internal_exports.insert("__wbindgen_malloc"); self.expose_uint8_memory(); @@ -653,7 +631,7 @@ impl<'a> Context<'a> { fn expose_pass_array16_to_wasm(&mut self) { if !self.exposed_globals.insert("pass_array16_to_wasm") { - return + return; } self.required_internal_exports.insert("__wbindgen_malloc"); self.expose_uint16_memory(); @@ -668,7 +646,7 @@ impl<'a> Context<'a> { fn expose_pass_array32_to_wasm(&mut self) { if !self.exposed_globals.insert("pass_array32_to_wasm") { - return + return; } self.required_internal_exports.insert("__wbindgen_malloc"); self.expose_uint32_memory(); @@ -683,7 +661,7 @@ impl<'a> Context<'a> { fn expose_pass_array_f32_to_wasm(&mut self) { if !self.exposed_globals.insert("pass_array_f32_to_wasm") { - return + return; } self.required_internal_exports.insert("__wbindgen_malloc"); self.globals.push_str(&format!(" @@ -697,7 +675,7 @@ impl<'a> Context<'a> { fn expose_pass_array_f64_to_wasm(&mut self) { if !self.exposed_globals.insert("pass_array_f64_to_wasm") { - return + return; } self.required_internal_exports.insert("__wbindgen_malloc"); self.globals.push_str(&format!(" @@ -711,7 +689,7 @@ impl<'a> Context<'a> { fn expose_text_encoder(&mut self) { if !self.exposed_globals.insert("text_encoder") { - return + return; } if self.config.nodejs { self.globals.push_str(&format!(" @@ -731,7 +709,7 @@ impl<'a> Context<'a> { fn expose_text_decoder(&mut self) { if !self.exposed_globals.insert("text_decoder") { - return + return; } if self.config.nodejs { self.globals.push_str(&format!(" @@ -751,7 +729,7 @@ impl<'a> Context<'a> { fn expose_get_string_from_wasm(&mut self) { if !self.exposed_globals.insert("get_string_from_wasm") { - return + return; } self.expose_text_decoder(); self.expose_uint8_memory(); @@ -764,7 +742,7 @@ impl<'a> Context<'a> { fn expose_get_array_js_value_from_wasm(&mut self) { if !self.exposed_globals.insert("get_array_js_value_from_wasm") { - return + return; } self.expose_get_array_u32_from_wasm(); self.expose_get_object(); @@ -784,7 +762,7 @@ impl<'a> Context<'a> { fn expose_get_array_i8_from_wasm(&mut self) { self.expose_uint8_memory(); if !self.exposed_globals.insert("get_array_i8_from_wasm") { - return + return; } self.globals.push_str(&format!(" function getArrayI8FromWasm(ptr, len) {{ @@ -798,7 +776,7 @@ impl<'a> Context<'a> { fn expose_get_array_u8_from_wasm(&mut self) { self.expose_uint8_memory(); if !self.exposed_globals.insert("get_array_u8_from_wasm") { - return + return; } self.globals.push_str(&format!(" function getArrayU8FromWasm(ptr, len) {{ @@ -812,7 +790,7 @@ impl<'a> Context<'a> { fn expose_get_array_i16_from_wasm(&mut self) { self.expose_uint16_memory(); if !self.exposed_globals.insert("get_array_i16_from_wasm") { - return + return; } self.globals.push_str(&format!(" function getArrayI16FromWasm(ptr, len) {{ @@ -826,7 +804,7 @@ impl<'a> Context<'a> { fn expose_get_array_u16_from_wasm(&mut self) { self.expose_uint16_memory(); if !self.exposed_globals.insert("get_array_u16_from_wasm") { - return + return; } self.globals.push_str(&format!(" function getArrayU16FromWasm(ptr, len) {{ @@ -840,7 +818,7 @@ impl<'a> Context<'a> { fn expose_get_array_i32_from_wasm(&mut self) { self.expose_uint32_memory(); if !self.exposed_globals.insert("get_array_i32_from_wasm") { - return + return; } self.globals.push_str(&format!(" function getArrayI32FromWasm(ptr, len) {{ @@ -854,7 +832,7 @@ impl<'a> Context<'a> { fn expose_get_array_u32_from_wasm(&mut self) { self.expose_uint32_memory(); if !self.exposed_globals.insert("get_array_u32_from_wasm") { - return + return; } self.globals.push_str(&format!(" function getArrayU32FromWasm(ptr, len) {{ @@ -867,7 +845,7 @@ impl<'a> Context<'a> { fn expose_get_array_f32_from_wasm(&mut self) { if !self.exposed_globals.insert("get_array_f32_from_wasm") { - return + return; } self.globals.push_str(&format!(" function getArrayF32FromWasm(ptr, len) {{ @@ -880,7 +858,7 @@ impl<'a> Context<'a> { fn expose_get_array_f64_from_wasm(&mut self) { if !self.exposed_globals.insert("get_array_f64_from_wasm") { - return + return; } self.globals.push_str(&format!(" function getArrayF64FromWasm(ptr, len) {{ @@ -893,7 +871,7 @@ impl<'a> Context<'a> { fn expose_uint8_memory(&mut self) { if !self.exposed_globals.insert("uint8_memory") { - return + return; } self.globals.push_str(&format!(" let cachedUint8Memory = null; @@ -908,7 +886,7 @@ impl<'a> Context<'a> { fn expose_uint16_memory(&mut self) { if !self.exposed_globals.insert("uint16_memory") { - return + return; } self.globals.push_str(&format!(" let cachedUint16Memory = null; @@ -923,7 +901,7 @@ impl<'a> Context<'a> { fn expose_uint32_memory(&mut self) { if !self.exposed_globals.insert("uint32_memory") { - return + return; } self.globals.push_str(&format!(" let cachedUint32Memory = null; @@ -938,7 +916,7 @@ impl<'a> Context<'a> { fn expose_assert_class(&mut self) { if !self.exposed_globals.insert("assert_class") { - return + return; } self.globals.push_str(&format!(" function _assertClass(instance, klass) {{ @@ -951,7 +929,7 @@ impl<'a> Context<'a> { fn expose_borrowed_objects(&mut self) { if !self.exposed_globals.insert("borrowed_objects") { - return + return; } self.expose_global_stack(); self.globals.push_str(&format!(" @@ -964,7 +942,7 @@ impl<'a> Context<'a> { fn expose_take_object(&mut self) { if !self.exposed_globals.insert("take_object") { - return + return; } self.expose_get_object(); self.expose_drop_ref(); @@ -979,7 +957,7 @@ impl<'a> Context<'a> { fn expose_add_heap_object(&mut self) { if !self.exposed_globals.insert("add_heap_object") { - return + return; } self.expose_global_slab(); self.expose_global_slab_next(); @@ -1100,7 +1078,7 @@ impl<'a> Context<'a> { fn expose_set_global_argument(&mut self) { if !self.exposed_globals.insert("set_global_argument") { - return + return; } self.expose_uint32_memory(); self.expose_global_argument_ptr(); @@ -1114,7 +1092,7 @@ impl<'a> Context<'a> { fn expose_get_global_argument(&mut self) { if !self.exposed_globals.insert("get_global_argument") { - return + return; } self.expose_uint32_memory(); self.expose_global_argument_ptr(); @@ -1128,7 +1106,7 @@ impl<'a> Context<'a> { fn expose_global_argument_ptr(&mut self) { if !self.exposed_globals.insert("global_argument_ptr") { - return + return; } self.required_internal_exports.insert("__wbindgen_global_argument_ptr"); self.globals.push_str(" @@ -1157,7 +1135,7 @@ impl<'a, 'b> SubContext<'a, 'b> { pub fn generate_export(&mut self, export: &shared::Export) { if let Some(ref class) = export.class { - return self.generate_export_for_class(class, export) + return self.generate_export_for_class(class, export); } let (js, ts) = self.generate_function("function", &export.function.name, @@ -1237,8 +1215,7 @@ impl<'a, 'b> SubContext<'a, 'b> { arg_conversions.push_str(&format!("\ _assertBoolean({name}); ", name = name)); - } else { - } + } else {} pass(&format!("arg{i} ? 1 : 0", i = i)) } shared::TYPE_JS_OWNED => { @@ -1381,9 +1358,9 @@ impl<'a, 'b> SubContext<'a, 'b> { const ret = wasm.{}({passed}); {convert_ret} ", - f = wasm_name, - passed = passed_args, - convert_ret = convert_ret, + f = wasm_name, + passed = passed_args, + convert_ret = convert_ret, )); } else { dst.push_str(&format!("\ @@ -1394,10 +1371,10 @@ impl<'a, 'b> SubContext<'a, 'b> { {destructors} }} ", - f = wasm_name, - passed = passed_args, - destructors = destructors, - convert_ret = convert_ret, + f = wasm_name, + passed = passed_args, + destructors = destructors, + convert_ret = convert_ret, )); } dst.push_str("}"); diff --git a/crates/cli-support/src/lib.rs b/crates/cli-support/src/lib.rs index d43233b3..404ef852 100644 --- a/crates/cli-support/src/lib.rs +++ b/crates/cli-support/src/lib.rs @@ -17,7 +17,7 @@ pub struct Bindgen { path: Option, nodejs: bool, browser: bool, - umd: bool, + amd: bool, debug: bool, typescript: bool, } @@ -37,7 +37,7 @@ impl Bindgen { path: None, nodejs: false, browser: false, - umd: false, + amd: false, debug: false, typescript: false, } @@ -58,8 +58,8 @@ impl Bindgen { self } - pub fn umd(&mut self, umd: bool) -> &mut Bindgen { - self.umd = umd; + pub fn amd(&mut self, amd: bool) -> &mut Bindgen { + self.amd = amd; self } diff --git a/crates/cli/src/bin/wasm-bindgen.rs b/crates/cli/src/bin/wasm-bindgen.rs index f491dba1..f70f0be2 100644 --- a/crates/cli/src/bin/wasm-bindgen.rs +++ b/crates/cli/src/bin/wasm-bindgen.rs @@ -32,7 +32,7 @@ Options: struct Args { flag_nodejs: bool, flag_browser: bool, - flag_umd: bool, + flag_amd: bool, flag_typescript: bool, flag_out_dir: Option, flag_debug: bool, @@ -59,7 +59,7 @@ fn main() { b.input_path(&input) .nodejs(args.flag_nodejs) .browser(args.flag_browser) - .umd(args.flag_umd) + .amd(args.flag_amd) .debug(args.flag_debug) .typescript(args.flag_typescript); From e8f4b7ed86052649d00594111c070f22885f7a12 Mon Sep 17 00:00:00 2001 From: Sharad Chand Date: Sat, 7 Apr 2018 13:08:57 +0545 Subject: [PATCH 06/12] Skip window assertions --- crates/cli-support/src/js.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/cli-support/src/js.rs b/crates/cli-support/src/js.rs index 1026b124..cee55da2 100644 --- a/crates/cli-support/src/js.rs +++ b/crates/cli-support/src/js.rs @@ -695,7 +695,7 @@ impl<'a> Context<'a> { self.globals.push_str(&format!(" const TextEncoder = require('util').TextEncoder; ")); - } else if !self.config.browser { + } else if !(self.config.browser && self.config.amd) { self.globals.push_str(&format!(" const TextEncoder = typeof window === 'object' && window.TextEncoder ? window.TextEncoder @@ -715,7 +715,7 @@ impl<'a> Context<'a> { self.globals.push_str(&format!(" const TextDecoder = require('util').TextDecoder; ")); - } else if !self.config.browser { + } else if !(self.config.browser && self.config.amd) { self.globals.push_str(&format!(" const TextDecoder = typeof window === 'object' && window.TextDecoder ? window.TextDecoder From b9192592a9b0569013fb18c24ad2fd0c1e340cc0 Mon Sep 17 00:00:00 2001 From: Sharad Chand Date: Sat, 7 Apr 2018 13:17:56 +0545 Subject: [PATCH 07/12] Updated docopts --- crates/cli/src/bin/wasm-bindgen.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/cli/src/bin/wasm-bindgen.rs b/crates/cli/src/bin/wasm-bindgen.rs index f70f0be2..1db50f8c 100644 --- a/crates/cli/src/bin/wasm-bindgen.rs +++ b/crates/cli/src/bin/wasm-bindgen.rs @@ -22,7 +22,7 @@ Options: --out-dir DIR Output directory --nodejs Generate output that only works in node.js --browser Generate output that only works in a browser - --umd Generate output that works both in browser and node.js + --amd 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 From ee1e3abd45a1e9e2abbb8dbe63a33b273ef8c37d Mon Sep 17 00:00:00 2001 From: Sharad Chand Date: Sat, 7 Apr 2018 13:22:03 +0545 Subject: [PATCH 08/12] Am an idiot --- crates/cli-support/src/js.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/cli-support/src/js.rs b/crates/cli-support/src/js.rs index cee55da2..acf4596f 100644 --- a/crates/cli-support/src/js.rs +++ b/crates/cli-support/src/js.rs @@ -695,7 +695,7 @@ impl<'a> Context<'a> { self.globals.push_str(&format!(" const TextEncoder = require('util').TextEncoder; ")); - } else if !(self.config.browser && self.config.amd) { + } else if !(self.config.browser || self.config.amd) { self.globals.push_str(&format!(" const TextEncoder = typeof window === 'object' && window.TextEncoder ? window.TextEncoder @@ -715,7 +715,7 @@ impl<'a> Context<'a> { self.globals.push_str(&format!(" const TextDecoder = require('util').TextDecoder; ")); - } else if !(self.config.browser && self.config.amd) { + } else if !(self.config.browser || self.config.amd) { self.globals.push_str(&format!(" const TextDecoder = typeof window === 'object' && window.TextDecoder ? window.TextDecoder From 8c935d5d94578844cc7cf2ac8028a9e0432063c2 Mon Sep 17 00:00:00 2001 From: Sharad Chand Date: Wed, 11 Apr 2018 13:59:58 +0545 Subject: [PATCH 09/12] Change flag to `--no-modules` --- crates/cli-support/src/js.rs | 10 +++++----- crates/cli-support/src/lib.rs | 8 ++++---- crates/cli/src/bin/wasm-bindgen.rs | 6 +++--- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/crates/cli-support/src/js.rs b/crates/cli-support/src/js.rs index acf4596f..fcbebbaf 100644 --- a/crates/cli-support/src/js.rs +++ b/crates/cli-support/src/js.rs @@ -47,7 +47,7 @@ impl<'a> Context<'a> { let contents = contents.trim(); let global = if self.config.nodejs { format!("module.exports.{} = {};\n", name, contents) - } else if self.config.amd { + } else if self.config.no_modules { format!("__exports.{} = {}\n", name, contents) } else { if contents.starts_with("function") { @@ -229,7 +229,7 @@ impl<'a> Context<'a> { self.footer.push_str(&format!("wasm = require('./{}_bg');", module_name)); format!("var wasm;") - } else if self.config.amd { + } else if self.config.no_modules { format!(" return fetch('{module}_bg.wasm') .then(response => response.arrayBuffer()) @@ -247,7 +247,7 @@ impl<'a> Context<'a> { format!("import * as wasm from './{}_bg';", module_name) }; - let js = if self.config.amd { + let js = if self.config.no_modules { format!(" (function (root, factory) {{ if (typeof define === 'function' && define.amd) {{ @@ -695,7 +695,7 @@ impl<'a> Context<'a> { self.globals.push_str(&format!(" const TextEncoder = require('util').TextEncoder; ")); - } else if !(self.config.browser || self.config.amd) { + } else if !(self.config.browser || self.config.no_modules) { self.globals.push_str(&format!(" const TextEncoder = typeof window === 'object' && window.TextEncoder ? window.TextEncoder @@ -715,7 +715,7 @@ impl<'a> Context<'a> { self.globals.push_str(&format!(" const TextDecoder = require('util').TextDecoder; ")); - } else if !(self.config.browser || self.config.amd) { + } else if !(self.config.browser || self.config.no_modules) { self.globals.push_str(&format!(" const TextDecoder = typeof window === 'object' && window.TextDecoder ? window.TextDecoder diff --git a/crates/cli-support/src/lib.rs b/crates/cli-support/src/lib.rs index 404ef852..a197f8b8 100644 --- a/crates/cli-support/src/lib.rs +++ b/crates/cli-support/src/lib.rs @@ -17,7 +17,7 @@ pub struct Bindgen { path: Option, nodejs: bool, browser: bool, - amd: bool, + no_modules: bool, debug: bool, typescript: bool, } @@ -37,7 +37,7 @@ impl Bindgen { path: None, nodejs: false, browser: false, - amd: false, + no_modules: false, debug: false, typescript: false, } @@ -58,8 +58,8 @@ impl Bindgen { self } - pub fn amd(&mut self, amd: bool) -> &mut Bindgen { - self.amd = amd; + pub fn no_modules(&mut self, no_modules: bool) -> &mut Bindgen { + self.no_modules = no_modules; self } diff --git a/crates/cli/src/bin/wasm-bindgen.rs b/crates/cli/src/bin/wasm-bindgen.rs index 1db50f8c..41fc491b 100644 --- a/crates/cli/src/bin/wasm-bindgen.rs +++ b/crates/cli/src/bin/wasm-bindgen.rs @@ -22,7 +22,7 @@ Options: --out-dir DIR Output directory --nodejs Generate output that only works in node.js --browser Generate output that only works in a browser - --amd Generate output that only works in a browser + --no-modules Generate output that only works in a browser (without modules) --typescript Output a TypeScript definition file --debug Include otherwise-extraneous debug checks in output -V --version Print the version number of wasm-bindgen @@ -32,7 +32,7 @@ Options: struct Args { flag_nodejs: bool, flag_browser: bool, - flag_amd: bool, + flag_no_modules: bool, flag_typescript: bool, flag_out_dir: Option, flag_debug: bool, @@ -59,7 +59,7 @@ fn main() { b.input_path(&input) .nodejs(args.flag_nodejs) .browser(args.flag_browser) - .amd(args.flag_amd) + .no_modules(args.flag_no_modules) .debug(args.flag_debug) .typescript(args.flag_typescript); From aa6487b6f1279c5cc20b20ae938cd966d496158d Mon Sep 17 00:00:00 2001 From: Sharad Chand Date: Wed, 11 Apr 2018 14:22:20 +0545 Subject: [PATCH 10/12] panic when modules used --- crates/cli-support/src/js.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/crates/cli-support/src/js.rs b/crates/cli-support/src/js.rs index fcbebbaf..34bdcd34 100644 --- a/crates/cli-support/src/js.rs +++ b/crates/cli-support/src/js.rs @@ -1622,6 +1622,10 @@ impl<'a, 'b> SubContext<'a, 'b> { fn import_name(&mut self, import: &shared::Import, item: &str) -> String { if let Some(ref module) = import.module { + if self.cx.config.no_modules { + panic!("import from `{}` module not allowed in `--no-modules`. use `--nodejs` or `--browser` instead", module); + } + let name = import.js_namespace.as_ref().map(|s| &**s).unwrap_or(item); if self.cx.imported_names.insert(name.to_string()) { From ffdb8a6a32037ef455df68214763c69b92451842 Mon Sep 17 00:00:00 2001 From: Sharad Chand Date: Thu, 12 Apr 2018 10:31:13 +0545 Subject: [PATCH 11/12] Simplified the preamble --- crates/cli-support/src/js.rs | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/crates/cli-support/src/js.rs b/crates/cli-support/src/js.rs index 34bdcd34..e333cc69 100644 --- a/crates/cli-support/src/js.rs +++ b/crates/cli-support/src/js.rs @@ -231,9 +231,9 @@ impl<'a> Context<'a> { format!("var wasm;") } else if self.config.no_modules { format!(" - return fetch('{module}_bg.wasm') + window.{module} = fetch('{module}_bg.wasm') .then(response => response.arrayBuffer()) - .then(buffer => WebAssembly.instantiate(buffer, __js_exports)) + .then(buffer => WebAssembly.instantiate(buffer, {{ './{module}': __exports }})) .then(({{instance}}) => {{ wasm = instance.exports; return wasm; @@ -249,22 +249,11 @@ impl<'a> Context<'a> { let js = if self.config.no_modules { format!(" - (function (root, factory) {{ - if (typeof define === 'function' && define.amd) {{ - define([], factory); - }} else {{ - root.{module} = factory(); - }} - }}(typeof self !== 'undefined' ? self : this, function() {{ - let wasm; - const __js_exports = {{}}; - const __exports = {{}}; - {globals} - __js_exports['./{module}'] = __exports; - {import_wasm} - }})) + let wasm; + const __exports = {{}}; + {globals} + {import_wasm} ", - module = module_name, globals = self.globals, import_wasm = import_wasm, ) From 85c9f2319c500523e08d84f36621fbbc5fe6f3c5 Mon Sep 17 00:00:00 2001 From: Sharad Chand Date: Fri, 13 Apr 2018 14:25:27 +0545 Subject: [PATCH 12/12] Expose on `window.wasm_bindgen` --- crates/cli-support/src/js.rs | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/crates/cli-support/src/js.rs b/crates/cli-support/src/js.rs index e333cc69..fc87c314 100644 --- a/crates/cli-support/src/js.rs +++ b/crates/cli-support/src/js.rs @@ -231,17 +231,19 @@ impl<'a> Context<'a> { format!("var wasm;") } else if self.config.no_modules { format!(" - window.{module} = fetch('{module}_bg.wasm') - .then(response => response.arrayBuffer()) - .then(buffer => WebAssembly.instantiate(buffer, {{ './{module}': __exports }})) - .then(({{instance}}) => {{ - wasm = instance.exports; - return wasm; - }}) - .catch(error => {{ - console.log('Error loading wasm module `{module}`:', error); - throw error; - }}); + window.wasm_bindgen.init = function(__wasm_path) {{ + return fetch(__wasm_path) + .then(response => response.arrayBuffer()) + .then(buffer => WebAssembly.instantiate(buffer, {{ './{module}': __exports }})) + .then(({{instance}}) => {{ + wasm = instance.exports; + return; + }}) + .catch(error => {{ + console.log('Error loading wasm module `{module}`:', error); + throw error; + }}); + }}; ", module = module_name) } else { format!("import * as wasm from './{}_bg';", module_name) @@ -249,10 +251,13 @@ impl<'a> Context<'a> { let js = if self.config.no_modules { format!(" - let wasm; - const __exports = {{}}; - {globals} - {import_wasm} + (function() {{ + let wasm; + const __exports = {{}}; + {globals} + window.wasm_bindgen = Object.assign({{}}, __exports); + {import_wasm} + }})(); ", globals = self.globals, import_wasm = import_wasm,