Skip to content

Commit

Permalink
Use Uint8Array::view to allow efficient data passing.
Browse files Browse the repository at this point in the history
In the past, we were relying on wasm-bindgen implementation details.
See rustwasm/wasm-bindgen#1079 for more information.
  • Loading branch information
mstange committed Jul 5, 2019
1 parent 00d0d11 commit 03507a7
Show file tree
Hide file tree
Showing 5 changed files with 336 additions and 137 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pdb = "0.3.0"
scroll = "0.9.2"
uuid = "0.7.2"
wasm-bindgen = "0.2.47"
js-sys = "0.3.24"

[dependencies.object]
version = "0.10.0"
Expand Down
47 changes: 21 additions & 26 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,15 @@

<script>

const { CompactSymbolTable } = wasm_bindgen;
const { CompactSymbolTable, WasmMemBuffer, get_compact_symbol_table } = wasm_bindgen;

const request = new Request('./profiler_get_symbols_bg.wasm', {
integrity: 'sha384-li3YiyXQ6ckoYyU6/y/pYMJJe4Zam0zHU/gGnbKhHY4W/sz2ZRtrXL+A6KHo1nkY'
integrity: 'sha384-M2W3W2BtD9YMqY7qazBXrFvc1L4vJNGtHGRS3fLMYi3erihmUmHX4PE5E+1eFJYL'
});

let wasm;

WebAssembly.compileStreaming(fetch(request)).then(module => {
return wasm_bindgen(module);
}).then(wasm_ => {
wasm = wasm_;
}, error => {
}).catch(error => {
console.error(error);
});

Expand All @@ -52,33 +48,32 @@
const binaryFileResult = await getFileResultOrUndefined(document.querySelector("#binary"));
const debugFileResult = await getFileResultOrUndefined(document.querySelector("#debug"));

const binaryData = new Uint8Array(binaryFileResult);
const binaryDataBufLen = binaryData.byteLength;
const binaryDataBufPtr = wasm.__wbindgen_malloc(binaryDataBufLen);
new Uint8Array(wasm.memory.buffer).set(binaryData, binaryDataBufPtr);
if (!binaryFileResult) {
return;
}

let debugDataBufLen = binaryDataBufLen;
let debugDataBufPtr = binaryDataBufPtr;
const binaryData = new WasmMemBuffer(binaryFileResult.byteLength, array => {
array.set(new Uint8Array(binaryFileResult));
});
let debugData = binaryData;
if (debugFileResult) {
const debugData = new Uint8Array(debugFileResult);
debugDataBufLen = debugData.byteLength;
debugDataBufPtr = wasm.__wbindgen_malloc(debugDataBufLen);
new Uint8Array(wasm.memory.buffer).set(debugData, debugDataBufPtr);
debugData = new WasmMemBuffer(debugFileResult.byteLength, array => {
array.set(new Uint8Array(debugFileResult));
});
}

const buf = new TextEncoder('utf-8').encode(document.querySelector("#breakpadId").value);
const breakpadIdLen = buf.length;
const breakpadIdPtr = wasm.__wbindgen_malloc(breakpadIdLen);
(new Uint8Array(wasm.memory.buffer)).set(buf, breakpadIdPtr);
console.log(binaryData, debugData);

const breakpadId = document.querySelector("#breakpadId").value;

const output = new CompactSymbolTable();

const succeeded =
wasm.get_compact_symbol_table(binaryDataBufPtr, binaryDataBufLen, debugDataBufPtr, debugDataBufLen, breakpadIdPtr, breakpadIdLen, output.ptr) !== 0;
wasm.__wbindgen_free(breakpadIdPtr, breakpadIdLen);
wasm.__wbindgen_free(binaryDataBufPtr, binaryDataBufLen);
if (debugDataBufPtr != binaryDataBufPtr) {
wasm.__wbindgen_free(debugDataBufPtr, debugDataBufLen);
get_compact_symbol_table(binaryData, debugData, breakpadId, output);

binaryData.free();
if (debugData != binaryData) {
debugData.free();
}

console.log(succeeded);
Expand Down
Loading

0 comments on commit 03507a7

Please sign in to comment.