skia2/experimental/wasm-skp-debugger/helper.js
Nathaniel Nifong 298390e487 Add helper for loading files, use let
Bug: skia:
Change-Id: I2da7325f5cfbd451358e183e24dcf21278615eb0
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/206200
Commit-Queue: Nathaniel Nifong <nifong@google.com>
Reviewed-by: Kevin Lubick <kjlubick@google.com>
2019-04-05 18:31:53 +00:00

25 lines
1.0 KiB
JavaScript

// Adds compile-time JS functions to augment the DebuggerView interface.
(function(DebuggerView){
DebuggerView.SkpFilePlayer = function(file_arraybuf) {
// Create the instance of SkpDebugPlayer
var player = new this.SkpDebugPlayer();
// Convert file (an ArrayBuffer) into a typedarray,
// otherwise fileMem.set() below seems to have no effect.
var fileContents = new Uint8Array(file_arraybuf);
var size = fileContents.byteLength;
// Allocate memory in wasm to hold the skp file selected by the user.
var fileMemPtr = this._malloc(size);
// Make a typed array view of that memory
var fileMem = new Uint8Array(this.buffer, fileMemPtr, size);
// Copy the file into it
fileMem.set(fileContents);
// Hand off pointer to wasm
player.loadSkp(fileMemPtr, size);
// Free the memory that was used to hold the file, since it is now represented as an SkPicture
this._free(fileMemPtr)
return player;
}
}(Module)); // When this file is loaded in, the high level object is "Module";