RTI API Reference

WATConverter

Class for converting a JavaScript AST into WebAssembly Text (WAT).

Summary

Properties

arrays

Linear-memory array allocations by variable name.

classes

Declared classes by name, each with its instance fields, per-field byte offsets, methods and total size.

currentClass

Name of the class currently being converted, if any.

currentType

Current numeric WAT type for emitted operations.

instances

Instance variable names mapped to their class names.

loopCounter

Counter for unique loop label names.

loops

Stack of enclosing loop labels used as break/continue targets.

Methods

addClassField

Adds a class field at the next available byte offset.

allocatorSource

Emits the bump allocator: a heap pointer global and an $alloc function.

arrayAddressSource

Computes the byte address of baseOffset + elementIndex * 4.

arrayIndexSource

Produces an i32 source for the element index of an array/object member access.

AssignmentExpression

Converts an AssignmentExpression node to WAT.

atDepth

Runs a function with a temporarily shifted indentation depth.

BinaryExpression

Converts a BinaryExpression node to WAT.

BlockStatement

Converts a BlockStatement node to WAT.

bodySource

Converts a loop/conditional body, accepting either a block or a single statement.

BreakStatement

Converts a BreakStatement node to WAT.

CallExpression

Converts a CallExpression node to WAT.

classConstructorSource

Converts a class constructor to a $<Name>_new factory that allocates a slot in the bump heap and returns the instance pointer (as f32).

ClassDeclaration

Converts a ClassDeclaration node to its factory and method functions.

classFieldAddressSource

Produces the WAT source for the heap byte address of a class field.

classFieldLoadSource

Produces the WAT source for loading a class field from the heap.

classFieldStoreSource

Produces the WAT source for storing a value into a class field.

classMethodSource

Converts a class method to a $<Name>_<method> function taking $this first.

collectConstructorFields

Collects class fields assigned in a constructor from this.

collectLocalNames

Collects the names of all variables declared inside a function body.

CommentBlock

Converts a CommentBlock to WAT.

CommentLine

Converts a CommentLine to WAT.

commentToSource

Converts a comment to WAT.

ConditionalExpression

Converts a ConditionalExpression (ternary) node to WAT.

ContinueStatement

Converts a ContinueStatement node to WAT.

DoWhileStatement

Converts a DoWhileStatement node to WAT.

ExportNamedDeclaration

Converts an ExportNamedDeclaration node to WAT.

ExpressionStatement

Converts an ExpressionStatement node to WAT, dropping unused values.

f32DataBytes

Serializes the f32 values of an array into raw little-endian bytes for a data segment.

ForStatement

Converts a ForStatement node to WAT.

FunctionDeclaration

Converts a FunctionDeclaration node to WAT.

getFuncNumericType

Extracts the numeric WAT type from JSDoc comments (e.

getTestSource

Produces a WAT i32 test expression for an arbitrary numeric condition.

heapStart

Computes the first unused byte, after all module-level array/object data.

Identifier

Converts an Identifier node to WAT.

IfStatement

Converts an IfStatement node to WAT.

instancePointerName

Returns the WAT local name holding an instance pointer.

isBooleanI32

Checks whether an expression already produces an i32 boolean test result, so it can be fed to if/br_if without a truthiness conversion.

isModuleDataDeclaration

Checks whether a statement is a module-level array/object constant declaration.

jsTypeToWat

Maps a JSDoc type name to a WAT numeric type.

leadingCommentsToSource

Converts leading comments to WAT.

LogicalExpression

Lowers &&/|| to an if/then/else that short-circuits on the left operand.

MemberExpression

Converts a MemberExpression node (array/object element read) to WAT.

NewExpression

Converts a NewExpression node to a $_new factory call.

NumericLiteral

Converts a NumericLiteral node to WAT.

percentToSource

Converts a % (modulo) expression, emulating float modulo via truncation.

popLoop

Removes the innermost loop from the stack.

Program

Converts a Program node to WAT.

pushLoop

Registers the current loop as innermost and returns its label names.

registerClass

Scans one class for instance fields, defaults and methods and stores its memory layout.

registerClasses

Registers top-level class declarations and their instance memory layout.

registerModuleData

Registers top-level array/object literals as flat memory data.

resolveInstanceClass

Finds the class of an instance via this, a tracked new variable, or a unique field-name match.

resolveInstanceMethodClass

Finds the class of the object a method call is dispatched on.

ReturnStatement

Converts a ReturnStatement node to WAT.

toI32

Truncates a numeric index expression to i32, passing i32 values through.

toSource

Converts a Babel AST node to WAT source.

toSource_

Dispatches a node to its handler method, emitting a warning comment for unhandled types.

trailingCommentsToSource

Converts trailing comments to WAT.

UnaryExpression

Converts an UnaryExpression node to WAT.

UpdateExpression

Converts an UpdateExpression node to WAT.

VariableDeclaration

Converts a VariableDeclaration node to WAT.

VariableDeclarator

Converts a VariableDeclarator node to WAT.

walk

Walks a Babel node tree, invoking a callback for every node.

WhileStatement

Converts a WhileStatement node to WAT.

Details

Constructor

WATConverter() #

Properties

{ [string]: {fields: Array<string>, offsets: Object<string, number>, defaults: Object<string, Node>, methods: Set<string>, size: number} }classes #

Declared classes by name, each with its instance fields, per-field byte offsets, methods and total size.

string, nullcurrentClass #

Name of the class currently being converted, if any.

'f32', 'f64', 'i32', 'i64'currentType #

Current numeric WAT type for emitted operations.

{ [string]: string }instances #

Instance variable names mapped to their class names.

numberloopCounter #

Counter for unique loop label names.

{exit: string, top: string, updateSrc: string}[]loops #

Stack of enclosing loop labels used as break/continue targets.

Methods

addClassField(cls, name) #

Adds a class field at the next available byte offset.

Parameters

clsObject

The class layout.

namestring

The field name.

allocatorSource() #

Emits the bump allocator: a heap pointer global and an $alloc function.

Parameters

Returns

string

WAT source of the heap global and allocator.

arrayAddressSource(arr, indexSrc) #

Computes the byte address of baseOffset + elementIndex * 4.

Parameters

arrObject

The registered array/object data.

indexSrcstring

The i32 element index source.

Returns

string

WAT source that pushes the byte address.

arrayIndexSource(arr, member) #

Produces an i32 source for the element index of an array/object member access.

Parameters

arrObject

The registered array/object data.

memberimport("@babel/types").MemberExpression

The member expression.

Returns

string

WAT source that pushes the element index.

AssignmentExpression(node) #

Converts an AssignmentExpression node to WAT.

Parameters

nodeimport("@babel/types").AssignmentExpression

The Babel AST node.

Returns

string

WAT representation of the node.

atDepth(extra, fn) #

Runs a function with a temporarily shifted indentation depth.

Parameters

extranumber

Additional indentation depth.

fn() => string

The function to run.

Returns

string

The result of the function.

BinaryExpression(node) #

Converts a BinaryExpression node to WAT.

Parameters

nodeimport("@babel/types").BinaryExpression

The Babel AST node.

Returns

string

WAT representation of the node.

BlockStatement(node) #

Converts a BlockStatement node to WAT.

Parameters

nodeimport("@babel/types").BlockStatement

The Babel AST node.

Returns

string

WAT representation of the node.

bodySource(body) #

Converts a loop/conditional body, accepting either a block or a single statement.

Parameters

bodyNode

The body (BlockStatement or single statement).

Returns

string

WAT source of the body.

BreakStatement(node) #

Converts a BreakStatement node to WAT.

Parameters

nodeimport("@babel/types").BreakStatement

The Babel AST node.

Returns

string

WAT representation of the node.

CallExpression(node) #

Converts a CallExpression node to WAT.

Parameters

nodeimport("@babel/types").CallExpression

The Babel AST node.

Returns

string

WAT representation of the node.

classConstructorSource(node, cls) #

Converts a class constructor to a $<Name>_new factory that allocates a slot in the bump heap and returns the instance pointer (as f32).

Parameters

nodeimport("@babel/types").ClassDeclaration

The class declaration.

clsObject

The registered class layout.

Returns

string

WAT source of the factory function.

ClassDeclaration(node) #

Converts a ClassDeclaration node to its factory and method functions.

Parameters

nodeimport("@babel/types").ClassDeclaration

The class declaration.

Returns

string

WAT representation of the class.

classFieldAddressSource(pointerName, fieldName, cls) #

Produces the WAT source for the heap byte address of a class field.

Parameters

pointerNamestring

The local holding the instance pointer.

fieldNamestring

The field name.

clsObject

The class layout.

Returns

string

WAT source that pushes the byte address.

classFieldLoadSource(pointerName, fieldName, cls) #

Produces the WAT source for loading a class field from the heap.

Parameters

pointerNamestring

The local holding the instance pointer.

fieldNamestring

The field name.

clsObject

The class layout.

Returns

string

WAT source that pushes the field value.

classFieldStoreSource(pointerName, valueNode, fieldName, cls) #

Produces the WAT source for storing a value into a class field.

Parameters

pointerNamestring

The local holding the instance pointer.

valueNodeNode

The value expression.

fieldNamestring

The field name.

clsObject

The class layout.

Returns

string

WAT source that performs the store.

classMethodSource(clsName, method) #

Converts a class method to a $<Name>_<method> function taking $this first.

Parameters

clsNamestring

The class name.

methodimport("@babel/types").ClassMethod

The method definition.

Returns

string

WAT source of the method function.

collectConstructorFields(body, cls) #

Collects class fields assigned in a constructor from this.name = ... stores.

Parameters

bodyimport("@babel/types").BlockStatement

The constructor body.

clsObject

The class layout.

collectLocalNames(body) #

Collects the names of all variables declared inside a function body.

Parameters

bodyimport("@babel/types").BlockStatement

The function body.

Returns

string[]

Names of the declared locals.

CommentBlock(node) #

Converts a CommentBlock to WAT.

Parameters

nodeimport("@babel/types").CommentBlock

The comment node.

Returns

string

WAT representation of the comment.

CommentLine(node) #

Converts a CommentLine to WAT.

Parameters

nodeimport("@babel/types").CommentLine

The comment node.

Returns

string

WAT representation of the comment.

commentToSource(comment, pos) #

Converts a comment to WAT.

Parameters

commentimport("@babel/types").Comment

The comment node.

pos'leading', 'trailing'

The position of the comment.

Returns

string

WAT representation of the comment.

ConditionalExpression(node) #

Converts a ConditionalExpression (ternary) node to WAT.

Parameters

nodeimport("@babel/types").ConditionalExpression

The Babel AST node.

Returns

string

WAT representation of the node.

ContinueStatement(node) #

Converts a ContinueStatement node to WAT.

Parameters

nodeimport("@babel/types").ContinueStatement

The Babel AST node.

Returns

string

WAT representation of the node.

DoWhileStatement(node) #

Converts a DoWhileStatement node to WAT.

Parameters

nodeimport("@babel/types").DoWhileStatement

The Babel AST node.

Returns

string

WAT representation of the node.

ExportNamedDeclaration(node) #

Converts an ExportNamedDeclaration node to WAT.

Parameters

nodeimport("@babel/types").ExportNamedDeclaration

The Babel AST node.

Returns

string

WAT representation of the node.

ExpressionStatement(node) #

Converts an ExpressionStatement node to WAT, dropping unused values.

Parameters

nodeimport("@babel/types").ExpressionStatement

The Babel AST node.

Returns

string

WAT representation of the node.

f32DataBytes(values) #

Serializes the f32 values of an array into raw little-endian bytes for a data segment.

Parameters

valuesnumber[]

The f32 values.

Returns

string

The escaped byte string.

ForStatement(node) #

Converts a ForStatement node to WAT.

Parameters

nodeimport("@babel/types").ForStatement

The Babel AST node.

Returns

string

WAT representation of the node.

FunctionDeclaration(node) #

Converts a FunctionDeclaration node to WAT.

Parameters

nodeimport("@babel/types").FunctionDeclaration

The Babel AST node.

Returns

string

WAT representation of the node.

getFuncNumericType(node) #

Extracts the numeric WAT type from JSDoc comments (e.g. @param {i32} n). Looks at the function itself and its ancestors, since Babel often attaches the comment to the wrapping ExportNamedDeclaration instead.

Parameters

nodeimport("@babel/types").FunctionDeclaration

The function declaration.

Returns

'f32', 'f64', 'i32', 'i64'

The numeric type.

getTestSource(expr) #

Produces a WAT i32 test expression for an arbitrary numeric condition.

Parameters

exprNode

The condition expression.

Returns

string

WAT source that pushes an i32.

heapStart() #

Computes the first unused byte, after all module-level array/object data.

Parameters

Returns

number

The heap start offset.

Identifier(node) #

Converts an Identifier node to WAT.

Parameters

nodeimport("@babel/types").Identifier

The Babel AST node.

Returns

string

WAT representation of the node.

IfStatement(node) #

Converts an IfStatement node to WAT.

Parameters

nodeimport("@babel/types").IfStatement

The Babel AST node.

Returns

string

WAT representation of the node.

instancePointerName(objectNode) #

Returns the WAT local name holding an instance pointer.

Parameters

objectNodeNode

The object expression.

Returns

string, null

The local name.

isBooleanI32(expr) #

Checks whether an expression already produces an i32 boolean test result, so it can be fed to if/br_if without a truthiness conversion.

Parameters

exprNode

The expression.

Returns

boolean

True if the expression yields an i32 test result.

isModuleDataDeclaration(stmt) #

Checks whether a statement is a module-level array/object constant declaration.

Parameters

stmtimport("@babel/types").Statement

The statement.

Returns

boolean

True if the statement is a module-level data declaration.

jsTypeToWat(type) #

Maps a JSDoc type name to a WAT numeric type.

Parameters

typestring

The JSDoc type name.

Returns

'f32', 'f64', 'i32', 'i64', null

The WAT type or null if not numeric.

leadingCommentsToSource(comments) #

Converts leading comments to WAT.

Parameters

commentsimport("@babel/types").Comment[]

The comment nodes.

Returns

string

WAT representation of comments.

LogicalExpression(node) #

Lowers &&/|| to an if/then/else that short-circuits on the left operand.

Parameters

nodeimport("@babel/types").LogicalExpression

The Babel AST node.

Returns

string

WAT representation of the node.

MemberExpression(node) #

Converts a MemberExpression node (array/object element read) to WAT.

Parameters

nodeimport("@babel/types").MemberExpression

The Babel AST node.

Returns

string

WAT representation of the node.

NewExpression(node) #

Converts a NewExpression node to a $_new factory call.

Parameters

nodeimport("@babel/types").NewExpression

The Babel AST node.

Returns

string

WAT representation of the node.

NumericLiteral(node) #

Converts a NumericLiteral node to WAT.

Parameters

nodeimport("@babel/types").NumericLiteral

The Babel AST node.

Returns

string

WAT representation of the node.

percentToSource(node) #

Converts a % (modulo) expression, emulating float modulo via truncation.

Parameters

nodeimport("@babel/types").BinaryExpression

The Babel AST node.

Returns

string

WAT representation of the node.

popLoop() #

Removes the innermost loop from the stack.

Parameters

Returns

Object, undefined

The popped loop labels.

Program(node) #

Converts a Program node to WAT.

Parameters

nodeimport("@babel/types").Program

The Babel AST node.

Returns

string

WAT representation of the node.

pushLoop(kind, [update]) #

Registers the current loop as innermost and returns its label names.

Parameters

kind'while', 'for', 'do'

The loop kind.

updateNode

The for-loop update expression, regenerated inline at each continue.

Returns

Object

The loop labels.

registerClass(node) #

Scans one class for instance fields, defaults and methods and stores its memory layout.

Parameters

nodeimport("@babel/types").ClassDeclaration

The class declaration.

registerClasses(body) #

Registers top-level class declarations and their instance memory layout.

Parameters

bodyimport("@babel/types").Statement[]

Top-level statements.

registerModuleData(body) #

Registers top-level array/object literals as flat memory data.

Parameters

bodyimport("@babel/types").Statement[]

Top-level statements.

resolveInstanceClass(objectNode, propertyName) #

Finds the class of an instance via this, a tracked new variable, or a unique field-name match.

Parameters

objectNodeNode

The member/call object expression.

propertyNamestring

The accessed field or method name.

Returns

string, null

The class name.

resolveInstanceMethodClass(objectNode, methodName) #

Finds the class of the object a method call is dispatched on.

Parameters

objectNodeNode

The call object expression.

methodNamestring

The called method name.

Returns

string, null

The class name.

ReturnStatement(node) #

Converts a ReturnStatement node to WAT.

Parameters

nodeimport("@babel/types").ReturnStatement

The Babel AST node.

Returns

string

WAT representation of the node.

toI32(expr) #

Truncates a numeric index expression to i32, passing i32 values through.

Parameters

exprNode

The index expression.

Returns

string

WAT source that pushes an i32 index.

toSource(node) #

Converts a Babel AST node to WAT source.

Parameters

nodeNode

The Babel AST node.

Returns

string

WAT representation of the node.

toSource_(node) #

Dispatches a node to its handler method, emitting a warning comment for unhandled types.

Parameters

nodeNode

The Babel AST node.

Returns

string

WAT representation of the node.

trailingCommentsToSource(comments) #

Converts trailing comments to WAT.

Parameters

commentsimport("@babel/types").Comment[]

The comment nodes.

Returns

string

WAT representation of comments.

UnaryExpression(node) #

Converts an UnaryExpression node to WAT.

Parameters

nodeimport("@babel/types").UnaryExpression

The Babel AST node.

Returns

string

WAT representation of the node.

UpdateExpression(node) #

Converts an UpdateExpression node to WAT.

Parameters

nodeimport("@babel/types").UpdateExpression

The Babel AST node.

Returns

string

WAT representation of the node.

VariableDeclaration(node) #

Converts a VariableDeclaration node to WAT.

Parameters

nodeimport("@babel/types").VariableDeclaration

The Babel AST node.

Returns

string

WAT representation of the node.

VariableDeclarator(node) #

Converts a VariableDeclarator node to WAT.

Parameters

nodeimport("@babel/types").VariableDeclarator

The Babel AST node.

Returns

string

WAT representation of the node.

walk(node, fn) #

Walks a Babel node tree, invoking a callback for every node.

Parameters

nodeNode

The node to walk.

fn(node: Node) => void

The callback.

WhileStatement(node) #

Converts a WhileStatement node to WAT.

Parameters

nodeimport("@babel/types").WhileStatement

The Babel AST node.

Returns

string

WAT representation of the node.