@@ -10,6 +10,68 @@ import {
10
10
11
11
import { Buffer } from '../buffer' ;
12
12
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
+
13
75
function sectionName ( s : SectionId ) : string {
14
76
switch ( s ) {
15
77
case 0 :
@@ -43,12 +105,12 @@ function sectionName(s: SectionId): string {
43
105
return "" ;
44
106
}
45
107
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 = "" ;
52
114
53
115
constructor ( buf : Buffer ) {
54
116
this . ref = buf . off ;
@@ -58,7 +120,6 @@ export class Section {
58
120
let before = buf . off ;
59
121
let name_len = buf . readVaruint ( 32 ) ;
60
122
let name_off = buf . off ;
61
-
62
123
this . name = "'" + String . fromUTF8 ( name_off , name_len ) + "'" ;
63
124
buf . off += name_len ;
64
125
this . payload_len -= buf . off - before ;
@@ -70,40 +131,67 @@ export class Section {
70
131
this . payload_off = buf . off ;
71
132
}
72
133
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 ;
83
136
}
84
137
85
138
}
86
139
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
+
87
172
class FuncType {
88
173
constructor ( public index : u32 , public form : i32 , public parameters : i32 [ ] , public returnVals : i32 [ ] ) { }
89
174
}
90
175
91
- class TypeSection extends Section {
176
+ export class TypeSection {
177
+ header : SectionHeader
92
178
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 = [ ] ;
101
182
}
102
183
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 ) ;
105
191
let count = buf . readVaruint ( 32 ) ;
192
+ log < usize > ( count ) ;
106
193
for ( let index : u32 = 0 ; index < count ; ++ index ) {
194
+ log < u32 > ( index ) ;
107
195
let form = buf . readVarint ( 7 ) & 0x7f ;
108
196
// opt.onType(
109
197
// index,
@@ -133,7 +221,8 @@ class TypeSection extends Section {
133
221
}
134
222
this . funcs . push ( new FuncType ( index , form , parameters , returnVals ) ) ;
135
223
}
136
- buf . off = this . end ;
224
+ // buf.off = this.end;
225
+ log < string > ( "Finished type section" , true )
137
226
return this ;
138
227
}
139
228
0 commit comments