11const fs = require ( 'fs' ) . promises ;
22
3- const CLS_ID_TABLE = {
4- 0 : 'I' ,
5- 1 : 'K' ,
6- 2 : 'S'
7- }
8-
93Promise . all ( [
104 fs . readFile ( './stack-machine.wasm' ) ,
115 fs . readFile ( './program.wasm' )
@@ -21,29 +15,39 @@ Promise.all([
2115 }
2216 ) )
2317 . then ( result => {
18+ const STACK_SIZE = result . instance . exports . STACK_SIZE . value ;
19+ const STACK_FRAME_SIZE = result . instance . exports . STACK_FRAME_SIZE . value ;
20+
21+ const CLS_SIZE = result . instance . exports . CLS_SIZE . value ;
22+ const CLS_HEAP_ADDR = result . instance . exports . CLS_HEAP_ADDR . value ;
23+
2424 const workingMemoryOffset = result . instance . exports . WM_ADDR . value ;
2525 console . log ( `Working Memory address: ${ workingMemoryOffset } ` ) ;
2626 const programMem = new Uint8Array ( result . instance . exports . mem . buffer , 0 , workingMemoryOffset ) ;
27+
2728 const stackTopPointer = result . instance . exports . stack_top_pointer . value ;
2829 console . log ( `Stack Top pointer: ${ stackTopPointer } ` ) ;
2930 const stackMem = new Uint8Array ( result . instance . exports . mem . buffer , workingMemoryOffset , stackTopPointer - workingMemoryOffset ) ;
30- const heapMem = new Uint8Array ( result . instance . exports . mem . buffer , workingMemoryOffset + 1024 , 13 * 20 ) ;
31+
32+ const clsHeapPointer = result . instance . exports . cls_heap_pointer . value ;
33+ const closureMemoryTotal = clsHeapPointer - CLS_HEAP_ADDR ;
34+ console . log ( `Heap pointer: ${ clsHeapPointer } , ${ closureMemoryTotal / CLS_SIZE } closures ⨉ ${ CLS_SIZE } bytes` ) ;
35+
36+ const heapMem = new Uint8Array ( result . instance . exports . mem . buffer , workingMemoryOffset + STACK_SIZE , closureMemoryTotal ) ;
3137 console . log ( `Program Memory: ${ programMem } \n` ) ;
3238
3339 const stackClss = [ ] ;
3440
35- for ( let i = 0 ; i < stackMem . length ; i += 4 ) {
36- const buffer = Buffer . from ( stackMem . subarray ( i , i + 4 ) ) ;
37- const clsId = buffer . readUIntLE ( 0 , 4 ) ;
41+ for ( let i = 0 ; i < stackMem . length ; i += STACK_FRAME_SIZE ) {
42+ const buffer = Buffer . from ( stackMem . subarray ( i , i + STACK_FRAME_SIZE ) ) ;
43+ const clsId = buffer . readUIntLE ( 0 , buffer . length ) ;
3844 stackClss . push ( clsId ) ;
3945 }
4046
41- console . log ( `Stack:\n ${ stackClss . map ( ( id , index ) => `#${ index } ${ CLS_ID_TABLE [ id ] } ` ) } \n` ) ;
42-
4347 const heapClss = [ ] ;
4448
45- for ( let i = 0 ; i < heapMem . length ; i += 13 ) {
46- const buffer = Buffer . from ( heapMem . subarray ( i , i + 13 ) ) ;
49+ for ( let i = 0 ; i < heapMem . length ; i += CLS_SIZE ) {
50+ const buffer = Buffer . from ( heapMem . subarray ( i , i + CLS_SIZE ) ) ;
4751 const clsId = buffer . readUIntLE ( 0 , 4 ) ;
4852 const tag = buffer . readUIntLE ( 4 , 1 ) ;
4953 const data1 = buffer . readUIntLE ( 5 , 4 )
@@ -55,6 +59,8 @@ Promise.all([
5559 for ( const heapCls of heapClss ) {
5660 console . log ( ` ${ formatHeapCLS ( heapCls ) } ` )
5761 }
62+
63+ console . log ( `\nStack:\n ${ stackClss . map ( ( closureIndex , id ) => formatStackFrame ( heapClss , id , closureIndex ) ) } ` ) ;
5864 } )
5965)
6066
@@ -77,3 +83,7 @@ function formatHeapCLS({ clsId, tag, data1, data2 }) {
7783 }
7884 return `#${ clsId } ${ formatCLS ( ) } `
7985}
86+
87+ function formatStackFrame ( closures , id , closureIndex ) {
88+ return `#${ id } ${ formatHeapCLS ( closures [ closureIndex ] ) } `
89+ }
0 commit comments