SYNOPSIS
A small toolkit for converting wasm binaries into json and back.
INSTALL
npm install wasm-json-toolkit
USAGE
const fs = const wasm2json = wasm2json const wasm = fsconst json = console
CLI
Install -g
global for cli usage.
wasm2json [FILE]
given a file containing a wasm module produces a json representation
json2wasm [FILE]
given a file containing a json representation produces a wasm module
API
wasm2json
converts a wasm binary into a json representation
Parameters
Buffer
- The Webassembly Binaryfilter
-Set
containing the name of sections to parse. If no filter is given all sections will be parsed
Returns Object
json2wasm
converts a json representation to a wasm binary
Parameters
Object
Returns Buffer
text2json
converts text to json. The only text accepted is a simple list of opcode name and immediates
Parameters
String
Examples
const codeStr = `i64.const 1i64.const 2i64.add`const json =
Returns Object
iterator
The Module Iterator allows for iteration over a webassembly module's sections. A section is wrapped in a section class. A section class instance allows you append entries to a given section
Examples
const it = wasmfor const section of it console const json = section
wasm
if the orignal wasm module was modified then this will return the modified wasm module
iterator
Iterates through the module's sections return {Iterator.}
Section
The section class is always internal created by the Module class. And return through the Module's iternator
toJSON
Parses the section and return the JSON repesentation of it returns {Object}
appendEntries
Appends an array of entries to this section. NOTE: this will modify the parent wasm module.
Parameters
entries
Arrayy<Buffer>
exammple json output
wast
(module
(func $addTwo (param i32 i32) (result i32)
(i32.add
(get_local 0)
(get_local 1)))
(export "addTwo" (func $addTwo)))
wasm
0x010661646454776f00000a09010700200020016a0b
json
[
{
"name": "preramble",
"magic": [0,97,115,109],
"version": [13,0,0,0]
},
{
"name": "type",
"entries": [
{
"form": "func",
"params": ["i32","i32"],
"return_type": "i32"
}
]
},
{
"name": "function",
"entries": [0]
},
{
"name": "export",
"entries": [
{
"field_str": "addTwo",
"kind": "Function",
"index": 0
}
]
},
{
"name": "code",
"entries": [
{
"locals": [],
"code": [
{
"name": "get_local",
"immediaties": "0"
},
{
"name": "get_local",
"immediaties": "1"
},
{
"return_type": "i32",
"name": "add"
},
{
"name": "end"
}
]
}
]
}
]