11import { injectable } from "inversify" ;
22import { WasmBinary } from "./WasmBinary" ;
33import { BytesReader } from "./BytesReader" ;
4- import { WasmSection , WasmTypeSectionPayload , WasmSectionPayload , WasmExportSectionPayload , WasmCodeSectionPayload } from "./WasmSection" ;
5- import { WasmSectionType , WasmType , WasmValueType , getWasmValueType , getExternalType } from "./wasmTypes" ;
4+ import { WasmSection , WasmTypeSectionPayload , WasmSectionPayload , WasmExportSectionPayload , WasmCodeSectionPayload , WasmImportSectionPayload } from "./WasmSection" ;
5+ import { WasmSectionType , WasmType , WasmValueType , getWasmValueType , getExternalType , WasmExternalKind } from "./wasmTypes" ;
66import { FuncType } from "./FuncType" ;
77import { FunctionBody , FunctionLocal , formatOpcodes } from "./FunctionBody" ;
88import { WasmOpcode , WasmOpcodeDefinition , WasmOpcodes , Immediate } from "./WasmOpcodes" ;
@@ -21,6 +21,7 @@ export class WasmBinaryParser {
2121 this . sectionParsers . set ( WasmSectionType . Type , this . parseTypeSection )
2222 this . sectionParsers . set ( WasmSectionType . Export , this . parseExportSection )
2323 this . sectionParsers . set ( WasmSectionType . Code , this . parseCodeSection )
24+ this . sectionParsers . set ( WasmSectionType . Import , this . parseImportSection )
2425 }
2526
2627 parse ( binary : Buffer ) : WasmBinary {
@@ -56,11 +57,16 @@ export class WasmBinaryParser {
5657 const sec = sections . find ( section => {
5758 return section . sectionType . toString ( ) == WasmSectionType [ WasmSectionType . Code . toString ( ) ]
5859 } ) ;
59- const outp : WasmCodeSectionPayload = sec . payload as WasmCodeSectionPayload ;
60+
61+ const imp = sections . find ( section => {
62+ return section . sectionType . toString ( ) == WasmSectionType [ WasmSectionType . Import . toString ( ) ]
63+ } ) ;
64+ console . log ( JSON . stringify ( imp ) )
65+ // const outp: WasmCodeSectionPayload = sec.payload as WasmCodeSectionPayload;
6066 // console.log(JSON.stringify(outp.functions[1]))
61- const mapp = outp . functions [ 3 ] . opcodes . map ( p => `[0x${ p . opcode . code . toString ( 16 ) } ] ${ p . opcode . name } ${ p . immediates } ` )
67+ // const mapp = outp.functions[3].opcodes.map(p => `[0x${p.opcode.code.toString(16)}] ${p.opcode.name} ${p.immediates}`)
6268 // console.log(mapp)
63- console . log ( outp . functions [ 2 ] . formattedOpcodes )
69+ // console.log(outp.functions[2].formattedOpcodes)
6470 // console.log(JSON.stringify(wasmSections))
6571 return {
6672 sections
@@ -116,6 +122,69 @@ export class WasmBinaryParser {
116122 return exportSection
117123 }
118124
125+ parseImportSection ( payload : Buffer , self : any ) : WasmImportSectionPayload {
126+ const reader = new BytesReader ( payload )
127+ const numberOfElements = reader . readVarUint32 ( )
128+ let importsCounter = 0
129+ const importsSection : WasmImportSectionPayload = {
130+ imports : [ ]
131+ }
132+ while ( importsCounter < numberOfElements ) {
133+ const moduleNameLength = reader . readVarUint32 ( )
134+ const moduleName = reader . readBytesToUtf8String ( moduleNameLength )
135+ const fieldNameLength = reader . readVarUint32 ( )
136+ const fieldName = reader . readBytesToUtf8String ( fieldNameLength )
137+ const importKind = reader . readBytesToNumber ( 1 )
138+ const kind = getExternalType ( importKind . toString ( ) )
139+ let kindType : any
140+ if ( kind === getExternalType ( WasmExternalKind . Function . toString ( ) ) ) {
141+ kindType = reader . readVarUint32 ( )
142+ } else if ( kind === getExternalType ( WasmExternalKind . Table . toString ( ) ) ) {
143+ const tableElemType = reader . readVarUint32 ( )
144+ const limits = self . parseLimits ( reader )
145+ kindType = {
146+ tableElemType,
147+ limits
148+ }
149+ } else if ( kind === getExternalType ( WasmExternalKind . Table . toString ( ) ) ) {
150+ kindType = self . parseLimits ( reader )
151+ } else if ( kind === getExternalType ( WasmExternalKind . Table . toString ( ) ) ) {
152+ const globalValType = reader . readBytesToNumber ( 1 )
153+ const mutability = reader . readBytesToNumber ( 1 )
154+ kindType = {
155+ globalValType,
156+ mutability
157+ }
158+ }
159+
160+ importsSection . imports . push ( {
161+ moduleName,
162+ fieldName,
163+ kind,
164+ kindType
165+ } )
166+ importsCounter ++
167+ }
168+ return importsSection
169+ }
170+
171+ // improve limits
172+ parseLimits ( reader : BytesReader ) : any {
173+ const limitsHasMax = reader . readBytesToNumber ( 1 )
174+ const limitMin = reader . readVarUint32 ( )
175+ let limits : any = {
176+ limitMin
177+ }
178+ if ( limitsHasMax === 1 ) {
179+ const limitMax = reader . readVarUint32 ( )
180+ limits = {
181+ ...limits ,
182+ limitMax
183+ }
184+ }
185+ return limits
186+ }
187+
119188 parseCodeSection ( payload : Buffer , self : any ) : WasmCodeSectionPayload {
120189 const reader = new BytesReader ( payload )
121190 const numberOfElements = reader . readVarUint32 ( )
0 commit comments