11import { instrToMethodMap , type WatVisitor } from "./builder" ;
22import type {
33 WasmBlock ,
4+ WasmBlockType ,
45 WasmBr ,
56 WasmBrTable ,
67 WasmCall ,
@@ -25,6 +26,7 @@ import type {
2526 WasmModule ,
2627 WasmNop ,
2728 WasmNumericType ,
29+ WasmRaw ,
2830 WasmReturn ,
2931 WasmSelect ,
3032 WasmStart ,
@@ -79,18 +81,31 @@ export class WatGenerator implements WatVisitor {
7981 }
8082
8183 // Control visitor methods
84+ private visitBlockType ( type : WasmBlockType ) : string {
85+ const params = type . paramTypes . map ( ( param ) => `(param ${ param } )` ) . join ( " " ) ;
86+ const results = type . resultTypes
87+ . map ( ( result ) => `(result ${ result } )` )
88+ . join ( " " ) ;
89+ const locals =
90+ type . localTypes ?. map ( ( local ) => `(local ${ local } )` ) . join ( " " ) ?? "" ;
91+ return `${ params } ${ results } ${ locals } ` ;
92+ }
93+
8294 visitBlockOp ( instruction : WasmBlock ) : string {
8395 const label = instruction . label ?? "" ;
96+ const typeStr = this . visitBlockType ( instruction . blockType ) ;
8497 const body = instruction . body . map ( ( instr ) => this . visit ( instr ) ) . join ( " " ) ;
85- return `(${ instruction . op } ${ label } ${ body } )` ;
98+ return `(${ instruction . op } ${ label } ${ typeStr } ${ body } )` ;
8699 }
87100 visitLoopOp ( instruction : WasmLoop ) : string {
88101 const label = instruction . label ?? "" ;
102+ const typeStr = this . visitBlockType ( instruction . blockType ) ;
89103 const body = instruction . body . map ( ( instr ) => this . visit ( instr ) ) . join ( " " ) ;
90- return `(${ instruction . op } ${ label } ${ body } )` ;
104+ return `(${ instruction . op } ${ label } ${ typeStr } ${ body } )` ;
91105 }
92106 visitIfOp ( instruction : WasmIf ) : string {
93107 const label = instruction . label ?? "" ;
108+ const typeStr = this . visitBlockType ( instruction . blockType ) ;
94109 const condition = this . visit ( instruction . predicate ) ;
95110 const thenBody = instruction . thenBody
96111 . map ( ( instr ) => this . visit ( instr ) )
@@ -100,9 +115,9 @@ export class WatGenerator implements WatVisitor {
100115 . join ( " " ) ;
101116
102117 if ( elseBody ) {
103- return `(if ${ label } ${ condition } (then ${ thenBody } ) (else ${ elseBody } ))` ;
118+ return `(if ${ label } ${ typeStr } ${ condition } (then ${ thenBody } ) (else ${ elseBody } ))` ;
104119 } else {
105- return `(if ${ label } ${ condition } (then ${ thenBody } ))` ;
120+ return `(if ${ label } ${ typeStr } ${ condition } (then ${ thenBody } ))` ;
106121 }
107122 }
108123 visitUnreachableOp ( instruction : WasmUnreachable ) : string {
@@ -189,7 +204,9 @@ export class WatGenerator implements WatVisitor {
189204 . map ( ( [ name , type ] ) => `(param ${ name } ${ type } )` )
190205 . join ( " " ) ;
191206
192- const results = `(result ${ instruction . funcType . resultTypes . join ( " " ) } )` ;
207+ const results = instruction . funcType . resultTypes . length
208+ ? `(result ${ instruction . funcType . resultTypes . join ( " " ) } )`
209+ : "" ;
193210
194211 const locals = Object . entries ( instruction . funcType . localTypes )
195212 . map ( ( [ name , type ] ) => `(local ${ name } ${ type } )` )
@@ -239,4 +256,21 @@ export class WatGenerator implements WatVisitor {
239256
240257 return `(${ instruction . op } \n${ imports } \n${ globals } \n${ datas } \n${ funcs } \n${ startFunc } \n${ exports } )` ;
241258 }
259+
260+ visitRaw ( instruction : WasmRaw ) : string {
261+ let code = "" ;
262+ for ( let i = 0 ; i < instruction . interpolations . length ; i ++ ) {
263+ code += instruction . codeFragments [ i ] ;
264+ const interp = instruction . interpolations [ i ] ;
265+ if ( typeof interp === "string" || typeof interp === "number" ) {
266+ code += interp . toString ( ) ;
267+ } else if ( Array . isArray ( interp ) ) {
268+ code += interp . map ( ( instr ) => this . visit ( instr ) ) . join ( " " ) ;
269+ } else {
270+ code += this . visit ( interp ) ;
271+ }
272+ }
273+ code += instruction . codeFragments [ instruction . interpolations . length ] ;
274+ return code ;
275+ }
242276}
0 commit comments