@@ -12,6 +12,8 @@ import {Buffer} from '../buffer';
12
12
13
13
import { log , log_str } from "../host" ;
14
14
15
+ import { itoa } from 'internal/number' ;
16
+
15
17
type byte = u8 ;
16
18
17
19
@@ -22,6 +24,7 @@ export class Module {
22
24
23
25
constructor ( buf : Buffer ) {
24
26
this . buf = buf ;
27
+ this . Headers = [ ] ;
25
28
}
26
29
27
30
@@ -72,6 +75,28 @@ export class Module {
72
75
73
76
}
74
77
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
+
75
100
function sectionName ( s : SectionId ) : string {
76
101
switch ( s ) {
77
102
case 0 :
@@ -170,7 +195,25 @@ class Section {
170
195
171
196
172
197
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
+ }
174
217
}
175
218
176
219
export class TypeSection {
@@ -183,22 +226,28 @@ export class TypeSection {
183
226
184
227
// constructor(public header: SectionHeader){}
185
228
parse ( buf : Buffer ) : TypeSection {
186
- log < string > ( "parsing TypeSection" , true ) ;
229
+ log_str ( "parsing TypeSection" ) ;
187
230
log < usize > ( buf . off ) ;
188
- log < string > ( this . header . name , true ) ;
231
+ log_str ( this . header . name ) ;
189
232
buf . off = this . header . payload_off ;
190
233
log < usize > ( buf . off ) ;
191
234
let count = buf . readVaruint ( 32 ) ;
192
235
log < usize > ( count ) ;
236
+ // let func: FuncType;
237
+ let parameters : i32 [ ] ;
193
238
for ( let index : u32 = 0 ; index < count ; ++ index ) {
194
239
log < u32 > ( index ) ;
195
240
let form = buf . readVarint ( 7 ) & 0x7f ;
241
+ // func = new FuncType(index, form);
196
242
// opt.onType(
197
243
// index,
198
244
// form
199
245
// );
200
246
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 ) ;
202
251
for ( let paramIndex : u32 = 0 ; paramIndex < paramCount ; ++ paramIndex ) {
203
252
let paramType = buf . readVarint ( 7 ) & 0x7f ;
204
253
// opt.onTypeParam(
@@ -208,24 +257,33 @@ export class TypeSection {
208
257
// );
209
258
parameters . push ( paramType )
210
259
}
260
+ log < i32 > ( parameters . length ) ;
211
261
let returnCount = buf . readVaruint ( 1 ) ; // MVP
212
- let returnVals : i32 [ ] = [ ] ;
213
262
for ( let returnIndex : u32 = 0 ; returnIndex < returnCount ; ++ returnIndex ) {
214
263
let returnType = buf . readVarint ( 7 ) & 0x7f ;
215
264
// opt.onTypeReturn(
216
265
// index,
217
266
// returnIndex,
218
267
// returnType
219
268
// );
220
- returnVals . push ( returnType ) ;
269
+ // func. returnVals.push(returnType);
221
270
}
222
- this . funcs . push ( new FuncType ( index , form , parameters , returnVals ) ) ;
271
+ // log_str(func.toString());
272
+ // this.funcs.push(func);
223
273
}
224
274
// buf.off = this.end;
225
- log < string > ( "Finished type section" , true )
275
+ log_str ( "Finished type section" ) ;
226
276
return this ;
227
277
}
228
278
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
+
229
287
}
230
288
231
289
/*
0 commit comments