What is @webassemblyjs/ast?
The @webassemblyjs/ast package is a toolchain for WebAssembly manipulation which allows you to parse, validate, and generate WebAssembly code. It provides an Abstract Syntax Tree (AST) representation of WebAssembly modules, enabling programmatic analysis and transformation of Wasm code.
What are @webassemblyjs/ast's main functionalities?
Parsing WebAssembly binary
This feature allows you to parse a WebAssembly binary into an AST. The code sample demonstrates how to use the wasm-parser submodule to decode a binary representation of a WebAssembly module.
"use strict";\nconst { decode } = require('@webassemblyjs/wasm-parser');\nconst binary = new Uint8Array([0x00, 0x61, 0x73, 0x6d, ...]);\nconst ast = decode(binary);\nconsole.log(ast);
Generating WebAssembly binary
This feature enables you to generate a WebAssembly binary from an AST. The code sample shows how to create an empty WebAssembly module using the ast submodule and then encode it into a binary with wasm-gen.
"use strict";\nconst { encode } = require('@webassemblyjs/wasm-gen');\nconst t = require('@webassemblyjs/ast');\nconst module = t.module([], []);\nconst binary = encode(module);\nconsole.log(new Uint8Array(binary));
Traversing and modifying the AST
This feature is for traversing the AST and applying modifications. The code sample illustrates how to traverse an existing AST and log each ModuleImport node encountered.
"use strict";\nconst { traverse } = require('@webassemblyjs/ast');\nconst ast = ...; // Assume this is an existing AST\ntraverse(ast, {\n ModuleImport(path) {\n console.log(path.node);\n }\n});
Other packages similar to @webassemblyjs/ast
wast-parser
The wast-parser package is similar to @webassemblyjs/ast in that it can parse WebAssembly text format (WAT) into an AST. However, it focuses on the text format rather than the binary format and does not provide binary generation capabilities.
binaryen
Binaryen is a compiler and toolchain infrastructure library for WebAssembly, written in C++. It has bindings for JavaScript and provides similar functionalities such as parsing, optimization, and generation of WebAssembly code. Compared to @webassemblyjs/ast, Binaryen offers a broader range of optimization tools and is implemented in a different programming language.
wabt
WABT (The WebAssembly Binary Toolkit) is a suite of tools for WebAssembly, including a decoder, an interpreter, and a binary format validator. It is similar to @webassemblyjs/ast in that it allows manipulation of WebAssembly binaries but is a standalone toolkit rather than an npm package.
@webassemblyjs/ast
AST utils for webassemblyjs
Installation
npm install @webassemblyjs/ast
Usage
Traverse
import { traverse } from "@webassemblyjs/ast";
traverse(ast, {
Module(path) {
console.log(path.node);
}
});
Instruction signatures
import { signatures } from "@webassemblyjs/ast";
console.log(signatures);
AST Nodes
- function
signature(object, name)
- function
identifier(value)
- function
valtype(name)
- function
stringLiteral(value)
- function
program(body)
- function
module(id, fields)
- function
binaryModule(id, blob)
- function
quoteModule(id, string)
- function
moduleExport(name, type, id)
- function
func(name, params, result, body)
- function
objectInstruction(id, object, args, namedArgs)
- function
instruction(id, args, namedArgs)
- function
loopInstruction(label, resulttype, instr)
- function
blockInstruction(label, instr, result)
- function
numberLiteral(rawValue, instructionType)
- function
callInstruction(index, instrArgs)
- function
ifInstruction(testLabel, result, test, consequent, alternate)
- function
withLoc(n, end, start)
- function
moduleImport(module, name, descr)
- function
globalImportDescr(valtype, mutability)
- function
funcParam(valtype, id)
- function
funcImportDescr(id, params, results)
- function
table(elementType, limits, name, elements)
- function
limits(min, max)
- function
memory(limits, id)
- function
data(memoryIndex, offset, init)
- function
global(globalType, init, name)
- function
globalType(valtype, mutability)
- function
byteArray(values)
- function
leadingComment(value)
- function
blockComment(value)
- function
indexLiteral(value)
- function
memIndexLiteral(value)
- function
typeInstructionFunc(params, result, id)
- function
callIndirectInstruction(params, results, intrs)
- function
start(index)
- function
elem(table, offset, funcs)