Skip to content

Commit 92afc76

Browse files
committed
Added better printing.
1 parent 13946d9 commit 92afc76

File tree

5 files changed

+3467
-2018
lines changed

5 files changed

+3467
-2018
lines changed

lib/parse/assembly/host/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ export declare function _log<T>(x: T, size: usize): void;
33
export declare function _log_str(x: string): void;
44

55

6-
export function log<T>(item: T, isStr: boolean = false):void {
7-
if (isStr){
6+
export function log<T>(item: T, size:usize = 4, isStr: boolean = false):void {
7+
if (isStr) {
88
_log_str(changetype<string>(item));
9-
}else{
10-
_log(item, sizeof<T>());
9+
} else {
10+
_log<T>(item, size);
1111
}
1212
}
1313

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

lib/parse/assembly/index.ts

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

5757
let type: TypeSection;
5858

59-
export function getType(): TypeSection {
60-
return type;
59+
export function getType(): string {
60+
return type.toString();
6161
}
6262

63+
export function toString(t:TypeSection): string {
64+
return t.toString();
65+
}
6366

6467
export class Parser {
6568
buf: Buffer;
@@ -118,6 +121,7 @@ export function parse(p: Parser): Module {
118121
let headers:SectionHeader[] = p.module.getID(SectionId.Type);
119122
let section = new TypeSection(headers[0]);
120123
type = (section).parse(p.module.buf);
121-
124+
log<i32>(offsetof<TypeSection>());
125+
log<TypeSection>(type, offsetof<TypeSection>());
122126
return p.module;
123127
}

lib/parse/assembly/module/index.ts

Lines changed: 66 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import {Buffer} from '../buffer';
1212

1313
import {log, log_str} from "../host";
1414

15+
import {itoa} from 'internal/number';
16+
1517
type byte = u8;
1618

1719

@@ -22,6 +24,7 @@ export class Module {
2224

2325
constructor(buf: Buffer){
2426
this.buf = buf;
27+
this.Headers = [];
2528
}
2629

2730

@@ -72,6 +75,28 @@ export class Module {
7275

7376
}
7477

78+
function typeName(t: Type): string{
79+
switch (t){
80+
case 0x7f:
81+
return 'i32';
82+
case 0x7e:
83+
return 'i64';
84+
case 0x7d:
85+
return 'f32';
86+
case 0x7c:
87+
return 'f64';
88+
case 0x70:
89+
return 'anyfunc';
90+
case 0x60:
91+
return 'func';
92+
case 0x40:
93+
return 'none';
94+
default:
95+
unreachable();
96+
}
97+
return ""
98+
}
99+
75100
function sectionName(s: SectionId): string {
76101
switch (s){
77102
case 0:
@@ -170,7 +195,25 @@ class Section {
170195

171196

172197
class FuncType {
173-
constructor(public index: u32, public form: i32, public parameters: i32[], public returnVals: i32[]){}
198+
public parameters: i32[];
199+
public returnVals: i32[];
200+
constructor(public index: u32, public form: i32){
201+
this.parameters = [];
202+
this.returnVals = [];
203+
}
204+
205+
206+
207+
toString(): string {
208+
let index = itoa<u32>(this.index);
209+
let form = typeName(this.form);
210+
let parameters:string[] = [];
211+
for (let i = 0; i< this.parameters.length; i++){
212+
parameters.push(typeName(this.parameters[i]));
213+
}
214+
let returnVal = this.returnVals.length == 1 ? typeName(this.returnVals[0]) : "";
215+
return "index: " + index + ", " + "form: " + form + ", parameters: " + parameters.join(", ") + " returnVal: " + returnVal;
216+
}
174217
}
175218

176219
export class TypeSection {
@@ -183,22 +226,28 @@ export class TypeSection {
183226

184227
// constructor(public header: SectionHeader){}
185228
parse(buf: Buffer): TypeSection {
186-
log<string>("parsing TypeSection", true);
229+
log_str("parsing TypeSection");
187230
log<usize>(buf.off);
188-
log<string>(this.header.name, true);
231+
log_str(this.header.name);
189232
buf.off = this.header.payload_off;
190233
log<usize>(buf.off);
191234
let count = buf.readVaruint(32);
192235
log<usize>(count);
236+
// let func: FuncType;
237+
let parameters: i32[];
193238
for (let index: u32 = 0; index < count; ++index) {
194239
log<u32>(index);
195240
let form = buf.readVarint(7) & 0x7f;
241+
// func = new FuncType(index, form);
196242
// opt.onType(
197243
// index,
198244
// form
199245
// );
200246
let paramCount = buf.readVaruint(32);
201-
let parameters: i32[] = [];
247+
log_str("param count.");
248+
log<u32>(paramCount);
249+
parameters = [];
250+
log<u32>(parameters.length);
202251
for (let paramIndex: u32 = 0; paramIndex < paramCount; ++paramIndex) {
203252
let paramType = buf.readVarint(7) & 0x7f;
204253
// opt.onTypeParam(
@@ -208,24 +257,33 @@ export class TypeSection {
208257
// );
209258
parameters.push(paramType)
210259
}
260+
log<i32>(parameters.length);
211261
let returnCount = buf.readVaruint(1); // MVP
212-
let returnVals: i32[] = [];
213262
for (let returnIndex: u32 = 0; returnIndex < returnCount; ++returnIndex) {
214263
let returnType = buf.readVarint(7) & 0x7f;
215264
// opt.onTypeReturn(
216265
// index,
217266
// returnIndex,
218267
// returnType
219268
// );
220-
returnVals.push(returnType);
269+
// func.returnVals.push(returnType);
221270
}
222-
this.funcs.push(new FuncType(index, form, parameters, returnVals));
271+
// log_str(func.toString());
272+
// this.funcs.push(func);
223273
}
224274
// buf.off = this.end;
225-
log<string>("Finished type section",true)
275+
log_str("Finished type section");
226276
return this;
227277
}
228278

279+
toString(): string {
280+
let strs: string[] = [];
281+
for (let i=0; i< this.funcs.length; i++){
282+
strs.push(this.funcs[i].toString());
283+
}
284+
return strs.join('\n');
285+
}
286+
229287
}
230288

231289
/*

0 commit comments

Comments
 (0)