-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
213f17f
commit 88c901f
Showing
9 changed files
with
232 additions
and
211 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// @format | ||
// Functionality for parsing the names section of a WebAssembly Module. | ||
|
||
const assert = require('assert'); | ||
const { TextDecoder } = require('util'); | ||
|
||
// TODO: Clean this up. | ||
function getNames(module) { | ||
const nameSections = WebAssembly.Module.customSections(module, 'name'); | ||
if (nameSections.length === 0) { | ||
console.log('No name sections provided'); | ||
return; | ||
} | ||
const [names] = nameSections; | ||
const data = new DataView(names); | ||
const decoder = new TextDecoder('utf-8'); | ||
|
||
const PAYLOAD_SIZE_OFFSET = 1; | ||
const { results, bytesRead: metadataSize } = readVarUint32( | ||
data, | ||
PAYLOAD_SIZE_OFFSET, | ||
2, | ||
); | ||
const [payloadLen, entryCount] = results; | ||
|
||
let entryId = 0; | ||
let entryOffset = PAYLOAD_SIZE_OFFSET + metadataSize; | ||
const nameMap = new Map(); | ||
|
||
while (entryId < entryCount) { | ||
const { results, bytesRead } = readVarUint32(data, entryOffset, 2); | ||
const [namingId, nameSize] = results; | ||
assert.equal(namingId, entryId, 'Correctly identified entry id'); | ||
|
||
const nameOffset = entryOffset + bytesRead; | ||
const name = decoder.decode(new Uint8Array(names, nameOffset, nameSize)); | ||
nameMap.set(namingId, name); | ||
|
||
entryOffset = nameOffset + nameSize; | ||
entryId += 1; | ||
} | ||
|
||
return nameMap; | ||
} | ||
|
||
// TODO: Wrap this in a generator. | ||
function readVarUint32(view, offset = 0, intsToRead = 1) { | ||
const results = []; | ||
let bytesRead = 0; | ||
|
||
for (let i = 0; i < intsToRead; i += 1) { | ||
let value = 0; | ||
let j = 0; | ||
|
||
while (true) { | ||
const byte = view.getUint8(offset + bytesRead); | ||
const shift = j * 7; | ||
value |= (byte & 0x7f) << shift; | ||
bytesRead += 1; | ||
j += 1; | ||
|
||
// Last byte (in little endian) starts with a 0. | ||
if ((byte & 0x80) === 0) { | ||
break; | ||
} | ||
} | ||
|
||
results.push(value); | ||
} | ||
|
||
return { results, bytesRead }; | ||
} | ||
|
||
module.exports = { getNames }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// @format | ||
|
||
// Execute a .wasm file in Node.js. | ||
// `node runWasm.js arithmetic.wasm add1 10` | ||
|
||
const fs = require('fs'); | ||
const assert = require('assert'); | ||
const { getNames } = require('./names.js'); | ||
const { readBuffer } = require('./tracer.js'); | ||
|
||
assert('WebAssembly' in global, 'WebAssembly global object not detected'); | ||
|
||
// Compile and run a WebAssembly module, given a path. | ||
// Based on: | ||
// https://gist.github.com/kanaka/3c9caf38bc4da2ecec38f41ba24b77df | ||
// https://gist.github.com/kanaka/3c9caf38bc4da2ecec38f41ba24b77df#gistcomment-2564224 | ||
function compileAndRun(bytes, func, ...args) { | ||
return WebAssembly.compile(bytes) | ||
.then(module => { | ||
const instance = new WebAssembly.Instance(module, { | ||
memory: new WebAssembly.Memory({ initial: 256 }), | ||
table: new WebAssembly.Table({ initial: 0, element: 'anyfunc' }), | ||
}); | ||
const names = getNames(module); | ||
return { instance, names }; | ||
}) | ||
.then(({ instance, names }) => { | ||
const { exports } = instance; | ||
assert(exports, 'no exports found'); | ||
assert( | ||
func in exports, | ||
`${func} not found in wasm exports: ${Object.keys(exports)}`, | ||
); | ||
|
||
console.log( | ||
'Invoking exported function', | ||
func, | ||
'with arguments', | ||
args, | ||
'...', | ||
); | ||
|
||
const result = exports[func](...args); | ||
|
||
return { result, exports, names }; | ||
}); | ||
} | ||
|
||
function validateArgs(_, __, wasmFile, funcName, args) { | ||
assert(wasmFile && funcName, 'Usage: ./runwasm.js prog.wasm func INT_ARG...'); | ||
const parsedArgs = args.split(' ').map(x => parseInt(x, 10)); | ||
return [wasmFile, funcName, ...parsedArgs]; | ||
} | ||
|
||
function main(argv) { | ||
const [wasmFile, funcName, ...args] = validateArgs(...argv); | ||
const bytes = fs.readFileSync(wasmFile); | ||
|
||
return compileAndRun(bytes, funcName, ...args) | ||
.then(({ result, exports, names }) => { | ||
console.log('Result of function call:', result); | ||
readBuffer(exports, names); | ||
return { result, exports, names }; | ||
}) | ||
.catch(console.error); | ||
} | ||
|
||
if (module.parent) { | ||
// Module is being imported, rather than invoked standalone. | ||
module.exports = { | ||
compileAndRun, | ||
}; | ||
module.exports.default = main; | ||
} else { | ||
// Script is invoked from the terminal, compile and log result. | ||
main(process.argv).catch(console.error); | ||
} |
Oops, something went wrong.