Skip to content

Commit c823422

Browse files
committed
Type Section parses. Array allocation is weird. Printing better too!
1 parent 92afc76 commit c823422

File tree

6 files changed

+6845
-4103
lines changed

6 files changed

+6845
-4103
lines changed

lib/parse/assembly/buffer/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ export class Buffer {
7171
return select<u32>(val | (~0 << shl), val, shl < size && (byt & 0x40) != 0);
7272
}
7373

74+
readVarint8(size: u32): i8 {
75+
return changetype<i8>(this.readVarint(size));
76+
}
77+
7478
/** Reads a LEB128-encoded signed 64-bit integer from memory. */
7579
readVarint64(): i64 {
7680
var val: u64 = 0;

lib/parse/assembly/host/index.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
11
// Imported callbacks
22
export declare function _log<T>(x: T, size: usize): void;
33
export declare function _log_str(x: string): void;
4+
export declare function _logi(x: i32): void;
5+
export declare function _logf(x: f32): void;
46

57

6-
export function log<T>(item: T, size:usize = 4, isStr: boolean = false):void {
7-
if (isStr) {
8-
_log_str(changetype<string>(item));
8+
export function log<T>(item: T):void {
9+
if (isReference<T>()) {
10+
if (isString<T>()) {
11+
_log_str(changetype<string>(item));
12+
} else {
13+
_log(changetype<i32>(item), offsetof<T>());
14+
}
915
} else {
10-
_log<T>(item, size);
16+
if (isInteger<T>()) {
17+
_logi(<i32>item);
18+
} else {
19+
_logf(<f32>item);
20+
}
1121
}
1222
}
1323

1424
export function log_str(item: string): void {
15-
log<string>(item, sizeof<string>(), true);
25+
log<string>(item);
1626
}

lib/parse/assembly/index.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ declare function debug():void;
5656

5757
let type: TypeSection;
5858

59-
export function getType(): string {
59+
export function getType(m: Module): string {
60+
let headers:SectionHeader[] = m.getID(SectionId.Type);
61+
let section = new TypeSection(headers[0]);
62+
type = section.parse(m.buf);
6063
return type.toString();
6164
}
6265

@@ -117,11 +120,5 @@ export function newParser(buf: Uint8Array): Parser {
117120
}
118121
export function parse(p: Parser): Module {
119122
p.parse();
120-
log<i32>(p.module.Headers.length);
121-
let headers:SectionHeader[] = p.module.getID(SectionId.Type);
122-
let section = new TypeSection(headers[0]);
123-
type = (section).parse(p.module.buf);
124-
log<i32>(offsetof<TypeSection>());
125-
log<TypeSection>(type, offsetof<TypeSection>());
126123
return p.module;
127124
}

lib/parse/assembly/module/index.ts

Lines changed: 18 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,8 @@ export class Module {
5656

5757
public getID(id: SectionId): SectionHeader[] {
5858
let res: SectionHeader[] = [];
59-
log<i32>(42);
6059
let x: i32 = this.Headers.length;
6160
for (let i=0; i < x; i++){
62-
// log<u32>(i);
6361
if (this.Headers[i].id == id){
6462
res.push(this.Headers[i]);
6563
}
@@ -197,22 +195,21 @@ class Section {
197195
class FuncType {
198196
public parameters: i32[];
199197
public returnVals: i32[];
200-
constructor(public index: u32, public form: i32){
201-
this.parameters = [];
202-
this.returnVals = [];
198+
constructor(public index: u32, public form: i8){
203199
}
204200

205201

206202

207203
toString(): string {
208204
let index = itoa<u32>(this.index);
209205
let form = typeName(this.form);
210-
let parameters:string[] = [];
206+
let parameters:string = "";
211207
for (let i = 0; i< this.parameters.length; i++){
212-
parameters.push(typeName(this.parameters[i]));
208+
parameters += typeName(this.parameters[i]);
209+
parameters = i < this.parameters.length -1 ? parameters += ", ": parameters;
213210
}
214-
let returnVal = this.returnVals.length == 1 ? typeName(this.returnVals[0]) : "";
215-
return "index: " + index + ", " + "form: " + form + ", parameters: " + parameters.join(", ") + " returnVal: " + returnVal;
211+
let returnVal = this.returnVals.length == 1 ? typeName(this.returnVals[0]) : "void";
212+
return "index: " + index + ", " + "form: " + form + ", (" + parameters + ") => " + returnVal;
216213
}
217214
}
218215

@@ -226,53 +223,28 @@ export class TypeSection {
226223

227224
// constructor(public header: SectionHeader){}
228225
parse(buf: Buffer): TypeSection {
229-
log_str("parsing TypeSection");
230-
log<usize>(buf.off);
231-
log_str(this.header.name);
232226
buf.off = this.header.payload_off;
233-
log<usize>(buf.off);
234227
let count = buf.readVaruint(32);
235-
log<usize>(count);
236-
// let func: FuncType;
237-
let parameters: i32[];
228+
let func: FuncType[] = new Array<FuncType>(count);
238229
for (let index: u32 = 0; index < count; ++index) {
239-
log<u32>(index);
240-
let form = buf.readVarint(7) & 0x7f;
241-
// func = new FuncType(index, form);
242-
// opt.onType(
243-
// index,
244-
// form
245-
// );
230+
let form = buf.readVarint8(7) & 0x7f;
231+
let fun = new FuncType(index, form);
232+
func[index] = fun;
246233
let paramCount = buf.readVaruint(32);
247-
log_str("param count.");
248-
log<u32>(paramCount);
249-
parameters = [];
250-
log<u32>(parameters.length);
234+
fun.parameters = new Array<i32>(paramCount);
251235
for (let paramIndex: u32 = 0; paramIndex < paramCount; ++paramIndex) {
252-
let paramType = buf.readVarint(7) & 0x7f;
253-
// opt.onTypeParam(
254-
// index,
255-
// paramIndex,
256-
// paramType
257-
// );
258-
parameters.push(paramType)
236+
let paramType = buf.readVarint8(7) & 0x7f;
237+
func[index].parameters[paramIndex]=paramType;
259238
}
260-
log<i32>(parameters.length);
239+
261240
let returnCount = buf.readVaruint(1); // MVP
241+
func[index].returnVals = new Array<i32>(returnCount);
262242
for (let returnIndex: u32 = 0; returnIndex < returnCount; ++returnIndex) {
263-
let returnType = buf.readVarint(7) & 0x7f;
264-
// opt.onTypeReturn(
265-
// index,
266-
// returnIndex,
267-
// returnType
268-
// );
269-
// func.returnVals.push(returnType);
243+
let returnType = buf.readVarint8(7) & 0x7f;
244+
func[index].returnVals[returnIndex] = returnType;
270245
}
271-
// log_str(func.toString());
272-
// this.funcs.push(func);
246+
log(func[index].toString());
273247
}
274-
// buf.off = this.end;
275-
log_str("Finished type section");
276248
return this;
277249
}
278250

0 commit comments

Comments
 (0)