Skip to content

Commit 6c2bf40

Browse files
committed
Parse import section
1 parent f23f93b commit 6c2bf40

File tree

4 files changed

+87
-6
lines changed

4 files changed

+87
-6
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { WasmExternalKind } from "./wasmTypes";
2+
3+
export interface ImportEntry {
4+
moduleName: string
5+
fieldName: string
6+
kind: WasmExternalKind
7+
kindType: any
8+
}

src/api/bytecode/ewasm/WasmBinaryParser.ts

Lines changed: 74 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { injectable } from "inversify";
22
import { WasmBinary } from "./WasmBinary";
33
import { 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";
66
import { FuncType } from "./FuncType";
77
import { FunctionBody, FunctionLocal, formatOpcodes } from "./FunctionBody";
88
import { 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()

src/api/bytecode/ewasm/WasmSection.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { WasmSectionType } from "./wasmTypes";
22
import { FuncType } from "./FuncType";
33
import { ExportEntry } from "./ExportEntry";
44
import { FunctionBody } from "./FunctionBody";
5+
import { ImportEntry } from "./ImportEntry";
56

67
export interface WasmSection {
78
sectionType: WasmSectionType
@@ -21,6 +22,10 @@ export interface WasmExportSectionPayload extends WasmSectionPayload {
2122
exports: ExportEntry[]
2223
}
2324

25+
export interface WasmImportSectionPayload extends WasmSectionPayload {
26+
imports: ImportEntry[]
27+
}
28+
2429
export interface WasmCodeSectionPayload extends WasmSectionPayload {
2530
functions: FunctionBody[]
2631
}

src/api/service/service/EwasmService.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const execSync = require('child_process').execSync
99
var tmp = require('tmp')
1010
var fs = require('fs')
1111

12-
1312
@injectable()
1413
export class EwasmService {
1514

0 commit comments

Comments
 (0)