Skip to content

Commit 13946d9

Browse files
committed
Need to finish Parsing TypeSection.
1 parent 1dd80f9 commit 13946d9

File tree

6 files changed

+1987
-1639
lines changed

6 files changed

+1987
-1639
lines changed

lib/parse/assembly/buffer/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
21
import "allocator/arena";
3-
// import {Uint8Array} from "typedarray";
42

53
export class Buffer {
64
buffer: Uint8Array;

lib/parse/assembly/host/index.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
// Imported callbacks
22
export declare function _log<T>(x: T, size: usize): void;
3+
export declare function _log_str(x: string): void;
34

4-
export function log<T>(item: T) {
5-
_log(item, sizeof<T>());
5+
6+
export function log<T>(item: T, isStr: boolean = false):void {
7+
if (isStr){
8+
_log_str(changetype<string>(item));
9+
}else{
10+
_log(item, sizeof<T>());
11+
}
12+
}
13+
14+
export function log_str(item: string): void {
15+
log<string>(item, true);
616
}

lib/parse/assembly/index.ts

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ import {
1212
} from "../src/common";
1313

1414
export {memory}
15-
import {Buffer} from "./buffer";
16-
17-
import {Section} from './module';
18-
19-
declare function log<T>(t: T):void;
15+
import {Buffer} from './buffer';
16+
import {SectionHeader,
17+
TypeSection,
18+
Module} from './module';
19+
import {log} from './host';
2020

2121
declare function debug():void;
2222

@@ -54,21 +54,19 @@ declare function debug():void;
5454
//
5555
// }
5656

57+
let type: TypeSection;
5758

58-
class Module {
59-
sections: Section[];
60-
61-
push(s :Section): void {
62-
this.sections.push(s);
63-
}
59+
export function getType(): TypeSection {
60+
return type;
6461
}
6562

63+
6664
export class Parser {
6765
buf: Buffer;
6866
module: Module;
6967
constructor(buf: Buffer){
7068
this.buf = buf;
71-
this.module = new Module();
69+
this.module = new Module(buf);
7270
}
7371

7472
parseString(): String {
@@ -98,13 +96,13 @@ export class Parser {
9896
var mem_space_index: u32 = 0;
9997
var tbl_space_index: u32 = 0;
10098
while (this.buf.off < this.buf.length) {
101-
log<i32>(this.off);
102-
let section = Section.create(this.buf);
103-
this.module.push(section);
104-
this.off = section.payload_off + section.payload_len;
105-
log<i32>(this.off);
99+
// log<string>("parsing next section", true);
100+
let header: SectionHeader = new SectionHeader(this.buf);
101+
this.module.parseSection(header);
102+
this.off = header.end;
103+
// log<i32>(this.off);
106104
}
107-
log<i32>(this.buf.length);
105+
// log<i32>(this.buf.length);
108106
// if (this.off != this.buf.length) unreachable();
109107
}
110108

@@ -114,8 +112,12 @@ export function newParser(buf: Uint8Array): Parser {
114112
let buffer = new Buffer(buf);
115113
return new Parser(buffer);
116114
}
117-
118-
export function parse(p: Parser): Section[] {
115+
export function parse(p: Parser): Module {
119116
p.parse();
120-
return p.module.sections;
117+
log<i32>(p.module.Headers.length);
118+
let headers:SectionHeader[] = p.module.getID(SectionId.Type);
119+
let section = new TypeSection(headers[0]);
120+
type = (section).parse(p.module.buf);
121+
122+
return p.module;
121123
}

lib/parse/assembly/module/index.ts

Lines changed: 118 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,68 @@ import {
1010

1111
import {Buffer} from '../buffer';
1212

13+
import {log, log_str} from "../host";
14+
15+
type byte = u8;
16+
17+
18+
export class Module {
19+
Headers: SectionHeader[];
20+
buf: Buffer;
21+
// Custom: TypeSection[];
22+
23+
constructor(buf: Buffer){
24+
this.buf = buf;
25+
}
26+
27+
28+
get Type(): SectionHeader[]{
29+
return this.getID(SectionId.Type);
30+
}
31+
// Import: TypeSection[];
32+
// Function: TypeSection[];
33+
// Table: TypeSection[];
34+
// Memory: TypeSection[];
35+
// Global: TypeSection[];
36+
// Export: TypeSection[];
37+
// Start: TypeSection[];
38+
// Element: TypeSection[];
39+
// Code: TypeSection[];
40+
// Data: TypeSection[];
41+
42+
43+
parseSection(header: SectionHeader): void {
44+
this.Headers.push(header);
45+
switch (header.id){
46+
case SectionId.Type:
47+
// this._Type.push(header);
48+
break;
49+
default:
50+
}
51+
// log_str(header.name);
52+
}
53+
54+
public getID(id: SectionId): SectionHeader[] {
55+
let res: SectionHeader[] = [];
56+
log<i32>(42);
57+
let x: i32 = this.Headers.length;
58+
for (let i=0; i < x; i++){
59+
// log<u32>(i);
60+
if (this.Headers[i].id == id){
61+
res.push(this.Headers[i]);
62+
}
63+
}
64+
return res;
65+
}
66+
67+
getType(): TypeSection {
68+
let Types = this.Type;
69+
let section = new TypeSection(Types[0]);
70+
return section.parse(this.buf);
71+
}
72+
73+
}
74+
1375
function sectionName(s: SectionId): string {
1476
switch (s){
1577
case 0:
@@ -43,12 +105,12 @@ function sectionName(s: SectionId): string {
43105
return "";
44106
}
45107

46-
export class Section {
47-
ref: u32;
48-
id: u32;
49-
payload_len: u32;
50-
payload_off: u32;
51-
name: string = "";
108+
export class SectionHeader {
109+
public ref: u32;
110+
public id: u32;
111+
public payload_len: u32;
112+
public payload_off: u32;
113+
public name: string = "";
52114

53115
constructor (buf: Buffer){
54116
this.ref = buf.off;
@@ -58,7 +120,6 @@ export class Section {
58120
let before = buf.off;
59121
let name_len = buf.readVaruint(32);
60122
let name_off = buf.off;
61-
62123
this.name = "'" + String.fromUTF8(name_off, name_len) + "'";
63124
buf.off += name_len;
64125
this.payload_len -= buf.off - before;
@@ -70,40 +131,67 @@ export class Section {
70131
this.payload_off = buf.off;
71132
}
72133

73-
static create(buf: Buffer): Section {
74-
let off = buf.off;
75-
let id = buf.peekVaruint(7);
76-
assert (off == buf.off);
77-
switch (id){
78-
case SectionId.Type:
79-
return new TypeSection(buf);
80-
default:
81-
}
82-
return new Section(buf);
134+
get end(): u32{
135+
return this.payload_off + this.payload_len;
83136
}
84137

85138
}
86139

140+
class Section {
141+
constructor(public header: SectionHeader){}
142+
}
143+
// static create(buf: Buffer): Section {
144+
// // log<string>("Creating Section", true);
145+
// let header = new SectionHeader(buf);
146+
// let off = buf.off;
147+
// let id = buf.peekVaruint(7);
148+
// assert (off == buf.off);
149+
// switch (id){
150+
// case SectionId.Type:
151+
// let s = new TypeSection(header);
152+
// return (s.parse(buf));
153+
// default:
154+
// }
155+
// let s = (new BaseSection(header))
156+
// return s.parse(buf);
157+
// }
158+
// }
159+
//
160+
//
161+
// export class BaseSection extends Section {
162+
//
163+
// // constructor(public header: SectionHeader){}
164+
//
165+
// parse(buf: Buffer): BaseSection {
166+
// log<string>("SubClass", true);
167+
// return this;
168+
// }
169+
// }
170+
171+
87172
class FuncType {
88173
constructor(public index: u32, public form: i32, public parameters: i32[], public returnVals: i32[]){}
89174
}
90175

91-
class TypeSection extends Section {
176+
export class TypeSection {
177+
header: SectionHeader
92178
funcs: FuncType[];
93-
94-
constructor(buf: Buffer){
95-
super(buf);
96-
this.parse(buf);
97-
}
98-
99-
get end(): u32{
100-
return this.payload_off + this.payload_len;
179+
constructor(header: SectionHeader){
180+
this.header = header;
181+
this.funcs = [];
101182
}
102183

103-
parse(buf:Buffer): Section {
104-
buf.off = this.payload_off;
184+
// constructor(public header: SectionHeader){}
185+
parse(buf: Buffer): TypeSection {
186+
log<string>("parsing TypeSection", true);
187+
log<usize>(buf.off);
188+
log<string>(this.header.name, true);
189+
buf.off = this.header.payload_off;
190+
log<usize>(buf.off);
105191
let count = buf.readVaruint(32);
192+
log<usize>(count);
106193
for (let index: u32 = 0; index < count; ++index) {
194+
log<u32>(index);
107195
let form = buf.readVarint(7) & 0x7f;
108196
// opt.onType(
109197
// index,
@@ -133,7 +221,8 @@ class TypeSection extends Section {
133221
}
134222
this.funcs.push(new FuncType(index, form, parameters, returnVals));
135223
}
136-
buf.off = this.end;
224+
// buf.off = this.end;
225+
log<string>("Finished type section",true)
137226
return this;
138227
}
139228

0 commit comments

Comments
 (0)