Add an option to detect node at runtime

Sometimes builds are done once and used in both the browser and in node, so add
an option to do runtime detection if necessary
This commit is contained in:
Alex Crichton
2018-03-07 08:50:56 -08:00
parent 48c0f290f9
commit 8254d9f516
5 changed files with 59 additions and 14 deletions

View File

@ -517,9 +517,9 @@ impl<'a> Context<'a> {
return
}
self.required_internal_exports.insert("__wbindgen_malloc");
if self.config.nodejs {
if self.config.nodejs_runtime_detect || self.config.nodejs {
self.globals.push_str(&format!("
function passStringToWasm(arg) {{
function passStringToWasmNode(arg) {{
if (typeof(arg) !== 'string')
throw new Error('expected a string argument');
const buf = Buffer.from(arg);
@ -529,11 +529,12 @@ impl<'a> Context<'a> {
return [ptr, len];
}}
"));
} else {
}
if self.config.nodejs_runtime_detect || !self.config.nodejs {
self.expose_text_encoder();
self.expose_uint8_memory();
self.globals.push_str(&format!("
function passStringToWasm(arg) {{
function passStringToWasmBrowser(arg) {{
if (typeof(arg) !== 'string')
throw new Error('expected a string argument');
const buf = textEncoder().encode(arg);
@ -544,6 +545,18 @@ impl<'a> Context<'a> {
}}
"));
}
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");
}
}
fn expose_pass_array8_to_wasm(&mut self) {
@ -653,19 +666,20 @@ impl<'a> Context<'a> {
if !self.exposed_globals.insert("get_string_from_wasm") {
return
}
if self.config.nodejs {
if self.config.nodejs_runtime_detect || self.config.nodejs {
self.globals.push_str(&format!("
function getStringFromWasm(ptr, len) {{
function getStringFromWasmNode(ptr, len) {{
const buf = Buffer.from(wasm.memory.buffer).slice(ptr, ptr + len);
const ret = buf.toString();
return ret;
}}
"));
} else {
}
if self.config.nodejs_runtime_detect || !self.config.nodejs {
self.expose_text_decoder();
self.expose_uint8_memory();
self.globals.push_str(&format!("
function getStringFromWasm(ptr, len) {{
function getStringFromWasmBrowser(ptr, len) {{
const mem = getUint8Memory();
const slice = mem.slice(ptr, ptr + len);
const ret = textDecoder().decode(slice);
@ -673,6 +687,18 @@ impl<'a> Context<'a> {
}}
"));
}
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");
}
}
fn expose_get_array_js_value_from_wasm(&mut self) {

View File

@ -17,6 +17,7 @@ pub mod wasm2es6js;
pub struct Bindgen {
path: Option<PathBuf>,
nodejs: bool,
nodejs_runtime_detect: bool,
debug: bool,
typescript: bool,
}
@ -35,6 +36,7 @@ impl Bindgen {
Bindgen {
path: None,
nodejs: false,
nodejs_runtime_detect: false,
debug: false,
typescript: false,
}
@ -50,6 +52,11 @@ impl Bindgen {
self
}
pub fn nodejs_runtime_detect(&mut self, detect: bool) -> &mut Bindgen {
self.nodejs_runtime_detect = detect;
self
}
pub fn debug(&mut self, debug: bool) -> &mut Bindgen {
self.debug = debug;
self