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 |
| arrayAddressSource | Computes the byte address of |
| 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 |
| 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 |
| collectConstructorFields | Collects class fields assigned in a constructor from |
| 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 |
| 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 |
| MemberExpression | Converts a MemberExpression node (array/object element read) to WAT. |
| NewExpression | Converts a NewExpression node to a $ |
| NumericLiteral | Converts a NumericLiteral node to WAT. |
| percentToSource | Converts a |
| 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 |
| 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
Linear-memory array allocations by variable name.
Declared classes by name, each with its instance fields, per-field byte offsets, methods and total size.
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
| cls | Object | The class layout. |
| name | string | The field name. |
allocatorSource() #
Emits the bump allocator: a heap pointer global and an $alloc function.
Parameters
Returns
stringWAT source of the heap global and allocator.
arrayAddressSource(arr, indexSrc) #
Computes the byte address of baseOffset + elementIndex * 4.
Parameters
| arr | Object | The registered array/object data. |
| indexSrc | string | The i32 element index source. |
Returns
stringWAT source that pushes the byte address.
arrayIndexSource(arr, member) #
Produces an i32 source for the element index of an array/object member access.
Parameters
| arr | Object | The registered array/object data. |
| member | import("@babel/types").MemberExpression | The member expression. |
Returns
stringWAT source that pushes the element index.
AssignmentExpression(node) #
Converts an AssignmentExpression node to WAT.
Parameters
| node | import("@babel/types").AssignmentExpression | The Babel AST node. |
Returns
stringWAT representation of the node.
atDepth(extra, fn) #
Runs a function with a temporarily shifted indentation depth.
Parameters
| extra | number | Additional indentation depth. |
| fn | () => string | The function to run. |
Returns
stringThe result of the function.
BinaryExpression(node) #
Converts a BinaryExpression node to WAT.
Parameters
| node | import("@babel/types").BinaryExpression | The Babel AST node. |
Returns
stringWAT representation of the node.
BlockStatement(node) #
Converts a BlockStatement node to WAT.
Parameters
| node | import("@babel/types").BlockStatement | The Babel AST node. |
Returns
stringWAT representation of the node.
bodySource(body) #
Converts a loop/conditional body, accepting either a block or a single statement.
Parameters
| body | Node | The body (BlockStatement or single statement). |
Returns
stringWAT source of the body.
BreakStatement(node) #
Converts a BreakStatement node to WAT.
Parameters
| node | import("@babel/types").BreakStatement | The Babel AST node. |
Returns
stringWAT representation of the node.
CallExpression(node) #
Converts a CallExpression node to WAT.
Parameters
| node | import("@babel/types").CallExpression | The Babel AST node. |
Returns
stringWAT 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
| node | import("@babel/types").ClassDeclaration | The class declaration. |
| cls | Object | The registered class layout. |
Returns
stringWAT source of the factory function.
ClassDeclaration(node) #
Converts a ClassDeclaration node to its factory and method functions.
Parameters
| node | import("@babel/types").ClassDeclaration | The class declaration. |
Returns
stringWAT representation of the class.
classFieldAddressSource(pointerName, fieldName, cls) #
Produces the WAT source for the heap byte address of a class field.
Parameters
| pointerName | string | The local holding the instance pointer. |
| fieldName | string | The field name. |
| cls | Object | The class layout. |
Returns
stringWAT source that pushes the byte address.
classFieldLoadSource(pointerName, fieldName, cls) #
Produces the WAT source for loading a class field from the heap.
Parameters
| pointerName | string | The local holding the instance pointer. |
| fieldName | string | The field name. |
| cls | Object | The class layout. |
Returns
stringWAT source that pushes the field value.
classFieldStoreSource(pointerName, valueNode, fieldName, cls) #
Produces the WAT source for storing a value into a class field.
Parameters
| pointerName | string | The local holding the instance pointer. |
| valueNode | Node | The value expression. |
| fieldName | string | The field name. |
| cls | Object | The class layout. |
Returns
stringWAT source that performs the store.
classMethodSource(clsName, method) #
Converts a class method to a $<Name>_<method> function taking $this first.
Parameters
| clsName | string | The class name. |
| method | import("@babel/types").ClassMethod | The method definition. |
Returns
stringWAT source of the method function.
collectConstructorFields(body, cls) #
Collects class fields assigned in a constructor from this.name = ... stores.
Parameters
| body | import("@babel/types").BlockStatement | The constructor body. |
| cls | Object | The class layout. |
collectLocalNames(body) #
Collects the names of all variables declared inside a function body.
Parameters
| body | import("@babel/types").BlockStatement | The function body. |
Returns
string[]Names of the declared locals.
CommentBlock(node) #
Converts a CommentBlock to WAT.
Parameters
| node | import("@babel/types").CommentBlock | The comment node. |
Returns
stringWAT representation of the comment.
CommentLine(node) #
Converts a CommentLine to WAT.
Parameters
| node | import("@babel/types").CommentLine | The comment node. |
Returns
stringWAT representation of the comment.
commentToSource(comment, pos) #
Converts a comment to WAT.
Parameters
| comment | import("@babel/types").Comment | The comment node. |
| pos | 'leading', 'trailing' | The position of the comment. |
Returns
stringWAT representation of the comment.
ConditionalExpression(node) #
Converts a ConditionalExpression (ternary) node to WAT.
Parameters
| node | import("@babel/types").ConditionalExpression | The Babel AST node. |
Returns
stringWAT representation of the node.
ContinueStatement(node) #
Converts a ContinueStatement node to WAT.
Parameters
| node | import("@babel/types").ContinueStatement | The Babel AST node. |
Returns
stringWAT representation of the node.
DoWhileStatement(node) #
Converts a DoWhileStatement node to WAT.
Parameters
| node | import("@babel/types").DoWhileStatement | The Babel AST node. |
Returns
stringWAT representation of the node.
ExportNamedDeclaration(node) #
Converts an ExportNamedDeclaration node to WAT.
Parameters
| node | import("@babel/types").ExportNamedDeclaration | The Babel AST node. |
Returns
stringWAT representation of the node.
ExpressionStatement(node) #
Converts an ExpressionStatement node to WAT, dropping unused values.
Parameters
| node | import("@babel/types").ExpressionStatement | The Babel AST node. |
Returns
stringWAT representation of the node.
f32DataBytes(values) #
Serializes the f32 values of an array into raw little-endian bytes for a data segment.
Parameters
| values | number[] | The f32 values. |
Returns
stringThe escaped byte string.
ForStatement(node) #
Converts a ForStatement node to WAT.
Parameters
| node | import("@babel/types").ForStatement | The Babel AST node. |
Returns
stringWAT representation of the node.
FunctionDeclaration(node) #
Converts a FunctionDeclaration node to WAT.
Parameters
| node | import("@babel/types").FunctionDeclaration | The Babel AST node. |
Returns
stringWAT 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
| node | import("@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
| expr | Node | The condition expression. |
Returns
stringWAT source that pushes an i32.
heapStart() #
Computes the first unused byte, after all module-level array/object data.
Parameters
Returns
numberThe heap start offset.
Identifier(node) #
Converts an Identifier node to WAT.
Parameters
| node | import("@babel/types").Identifier | The Babel AST node. |
Returns
stringWAT representation of the node.
IfStatement(node) #
Converts an IfStatement node to WAT.
Parameters
| node | import("@babel/types").IfStatement | The Babel AST node. |
Returns
stringWAT representation of the node.
instancePointerName(objectNode) #
Returns the WAT local name holding an instance pointer.
Parameters
| objectNode | Node | The object expression. |
Returns
string, nullThe 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
| expr | Node | The expression. |
Returns
booleanTrue if the expression yields an i32 test result.
isModuleDataDeclaration(stmt) #
Checks whether a statement is a module-level array/object constant declaration.
Parameters
| stmt | import("@babel/types").Statement | The statement. |
Returns
booleanTrue if the statement is a module-level data declaration.
jsTypeToWat(type) #
Maps a JSDoc type name to a WAT numeric type.
Parameters
| type | string | The JSDoc type name. |
Returns
'f32', 'f64', 'i32', 'i64', nullThe WAT type or null if not numeric.
leadingCommentsToSource(comments) #
Converts leading comments to WAT.
Parameters
| comments | import("@babel/types").Comment[] | The comment nodes. |
Returns
stringWAT representation of comments.
LogicalExpression(node) #
Lowers &&/|| to an if/then/else that short-circuits on the left operand.
Parameters
| node | import("@babel/types").LogicalExpression | The Babel AST node. |
Returns
stringWAT representation of the node.
MemberExpression(node) #
Converts a MemberExpression node (array/object element read) to WAT.
Parameters
| node | import("@babel/types").MemberExpression | The Babel AST node. |
Returns
stringWAT representation of the node.
NewExpression(node) #
Converts a NewExpression node to a $
Parameters
| node | import("@babel/types").NewExpression | The Babel AST node. |
Returns
stringWAT representation of the node.
NumericLiteral(node) #
Converts a NumericLiteral node to WAT.
Parameters
| node | import("@babel/types").NumericLiteral | The Babel AST node. |
Returns
stringWAT representation of the node.
percentToSource(node) #
Converts a % (modulo) expression, emulating float modulo via truncation.
Parameters
| node | import("@babel/types").BinaryExpression | The Babel AST node. |
Returns
stringWAT representation of the node.
popLoop() #
Removes the innermost loop from the stack.
Parameters
Returns
Object, undefinedThe popped loop labels.
Program(node) #
Converts a Program node to WAT.
Parameters
| node | import("@babel/types").Program | The Babel AST node. |
Returns
stringWAT 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. |
| update | Node | The for-loop update expression, regenerated inline at each continue. |
Returns
ObjectThe loop labels.
registerClass(node) #
Scans one class for instance fields, defaults and methods and stores its memory layout.
Parameters
| node | import("@babel/types").ClassDeclaration | The class declaration. |
registerClasses(body) #
Registers top-level class declarations and their instance memory layout.
Parameters
| body | import("@babel/types").Statement[] | Top-level statements. |
registerModuleData(body) #
Registers top-level array/object literals as flat memory data.
Parameters
| body | import("@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
| objectNode | Node | The member/call object expression. |
| propertyName | string | The accessed field or method name. |
Returns
string, nullThe class name.
resolveInstanceMethodClass(objectNode, methodName) #
Finds the class of the object a method call is dispatched on.
Parameters
| objectNode | Node | The call object expression. |
| methodName | string | The called method name. |
Returns
string, nullThe class name.
ReturnStatement(node) #
Converts a ReturnStatement node to WAT.
Parameters
| node | import("@babel/types").ReturnStatement | The Babel AST node. |
Returns
stringWAT representation of the node.
toI32(expr) #
Truncates a numeric index expression to i32, passing i32 values through.
Parameters
| expr | Node | The index expression. |
Returns
stringWAT source that pushes an i32 index.
toSource(node) #
Converts a Babel AST node to WAT source.
Parameters
| node | Node | The Babel AST node. |
Returns
stringWAT representation of the node.
toSource_(node) #
Dispatches a node to its handler method, emitting a warning comment for unhandled types.
Parameters
| node | Node | The Babel AST node. |
Returns
stringWAT representation of the node.
trailingCommentsToSource(comments) #
Converts trailing comments to WAT.
Parameters
| comments | import("@babel/types").Comment[] | The comment nodes. |
Returns
stringWAT representation of comments.
UnaryExpression(node) #
Converts an UnaryExpression node to WAT.
Parameters
| node | import("@babel/types").UnaryExpression | The Babel AST node. |
Returns
stringWAT representation of the node.
UpdateExpression(node) #
Converts an UpdateExpression node to WAT.
Parameters
| node | import("@babel/types").UpdateExpression | The Babel AST node. |
Returns
stringWAT representation of the node.
VariableDeclaration(node) #
Converts a VariableDeclaration node to WAT.
Parameters
| node | import("@babel/types").VariableDeclaration | The Babel AST node. |
Returns
stringWAT representation of the node.
VariableDeclarator(node) #
Converts a VariableDeclarator node to WAT.
Parameters
| node | import("@babel/types").VariableDeclarator | The Babel AST node. |
Returns
stringWAT representation of the node.
walk(node, fn) #
Walks a Babel node tree, invoking a callback for every node.
Parameters
| node | Node | The node to walk. |
| fn | (node: Node) => void | The callback. |