Skip to content

Commit

Permalink
Merge pull request rustwasm#1033 from alexcrichton/no-return-array
Browse files Browse the repository at this point in the history
Remove temporary object allocation
  • Loading branch information
alexcrichton committed Nov 13, 2018
2 parents 8520a54 + c915870 commit c20f80c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
2 changes: 1 addition & 1 deletion crates/cli-support/src/js/js2rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
format!("{}({})", func, name)
};
self.prelude(&format!(
"const [ptr{i}, len{i}] = {val};",
"const ptr{i} = {val};\nconst len{i} = WASM_VECTOR_LEN;",
i = i,
val = val,
));
Expand Down
26 changes: 19 additions & 7 deletions crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,8 @@ impl<'a> Context<'a> {
function(i, len_ptr) {
let obj = getObject(i);
if (typeof(obj) !== 'string') return 0;
const [ptr, len] = passStringToWasm(obj);
getUint32Memory()[len_ptr / 4] = len;
const ptr = passStringToWasm(obj);
getUint32Memory()[len_ptr / 4] = WASM_VECTOR_LEN;
return ptr;
}
",
Expand Down Expand Up @@ -368,9 +368,9 @@ impl<'a> Context<'a> {
Ok(String::from(
"
function(idx, ptrptr) {
const [ptr, len] = passStringToWasm(JSON.stringify(getObject(idx)));
const ptr = passStringToWasm(JSON.stringify(getObject(idx)));
getUint32Memory()[ptrptr / 4] = ptr;
return len;
return WASM_VECTOR_LEN;
}
",
))
Expand Down Expand Up @@ -994,13 +994,21 @@ impl<'a> Context<'a> {
));
}

fn expose_wasm_vector_len(&mut self) {
if !self.exposed_globals.insert("wasm_vector_len") {
return;
}
self.global("let WASM_VECTOR_LEN = 0;");
}

fn expose_pass_string_to_wasm(&mut self) -> Result<(), Error> {
if !self.exposed_globals.insert("pass_string_to_wasm") {
return Ok(());
}
self.require_internal_export("__wbindgen_malloc")?;
self.expose_text_encoder();
self.expose_uint8_memory();
self.expose_wasm_vector_len();
let debug = if self.config.debug {
"
if (typeof(arg) !== 'string') throw new Error('expected a string argument');
Expand All @@ -1015,7 +1023,8 @@ impl<'a> Context<'a> {
const buf = cachedTextEncoder.encode(arg);
const ptr = wasm.__wbindgen_malloc(buf.length);
getUint8Memory().set(buf, ptr);
return [ptr, buf.length];
WASM_VECTOR_LEN = buf.length;
return ptr;
}}
",
debug
Expand Down Expand Up @@ -1068,7 +1077,8 @@ impl<'a> Context<'a> {
for (let i = 0; i < array.length; i++) {
mem[ptr / 4 + i] = addHeapObject(array[i]);
}
return [ptr, array.length];
WASM_VECTOR_LEN = array.length;
return ptr;
}
",
Expand All @@ -1086,12 +1096,14 @@ impl<'a> Context<'a> {
return Ok(());
}
self.require_internal_export("__wbindgen_malloc")?;
self.expose_wasm_vector_len();
self.global(&format!(
"
function {}(arg) {{
const ptr = wasm.__wbindgen_malloc(arg.length * {size});
{}().set(arg, ptr / {size});
return [ptr, arg.length];
WASM_VECTOR_LEN = arg.length;
return ptr;
}}
",
name,
Expand Down
3 changes: 2 additions & 1 deletion crates/cli-support/src/js/rust2js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,8 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
self.ret_expr = format!(
"\
{}
const [retptr, retlen] = {};
const retptr = {};
const retlen = WASM_VECTOR_LEN;
const mem = getUint32Memory();
mem[ret / 4] = retptr;
mem[ret / 4 + 1] = retlen;
Expand Down

0 comments on commit c20f80c

Please sign in to comment.