@@ -139,9 +139,18 @@ const build = {
139
139
// (v128.load_lane_zero)
140
140
if ( opCode <= 0x5b ) immed . push ( ...uleb ( args . shift ( ) ) )
141
141
}
142
- // (v128.const i32x4), (i8x16.shuffle 0 1 ... 15 a b)
143
- else if ( opCode === 0x0c || opCode === 0x0d ) {
144
- immed . push ( ...consumeConst ( op , args ) )
142
+ // (i8x16.shuffle 0 1 ... 15 a b)
143
+ else if ( opCode === 0x0d ) {
144
+ // i8, i16, i32 - bypass the encoding
145
+ for ( let i = 0 ; i < 16 ; i ++ ) immed . push ( encode . i32 . parse ( args . shift ( ) ) )
146
+ }
147
+ // (v128.const i32x4)
148
+ else if ( opCode === 0x0c ) {
149
+ // immed.push(...consumeConst(op, args))
150
+ // FIXME: find more elegant way here
151
+ args . unshift ( op )
152
+ immed = initGlobal ( args , ctx )
153
+ immed . pop ( )
145
154
}
146
155
// (i8x16.extract_lane_s 0 ...)
147
156
else if ( opCode >= 0x15 && opCode <= 0x22 ) {
@@ -311,7 +320,7 @@ const build = {
311
320
let name = args [ 0 ] [ 0 ] === '$' && args . shift ( )
312
321
if ( name ) ctx . global [ name ] = ctx . global . length
313
322
let [ type , init ] = args , mut = type [ 0 ] === 'mut' ? 1 : 0
314
- ctx . global . push ( [ TYPE [ mut ? type [ 1 ] : type ] , mut , ...initGlobal ( init ) ] )
323
+ ctx . global . push ( [ TYPE [ mut ? type [ 1 ] : type ] , mut , ...initGlobal ( [ ... init ] , ctx ) ] )
315
324
} ,
316
325
317
326
// (table 1 2? funcref)
@@ -326,7 +335,7 @@ const build = {
326
335
// (elem (i32.const 0) $f1 $f2), (elem (global.get 0) $f1 $f2)
327
336
elem ( [ , offset , ...elems ] , ctx ) {
328
337
const tableIdx = 0 // FIXME: table index can be defined
329
- ctx . elem . push ( [ tableIdx , ...initGlobal ( offset , ctx ) , ...uleb ( elems . length ) , ...elems . flatMap ( el => uleb ( el [ 0 ] === '$' ? ctx . func [ el ] : el ) ) ] )
338
+ ctx . elem . push ( [ tableIdx , ...initGlobal ( [ ... offset ] , ctx ) , ...uleb ( elems . length ) , ...elems . flatMap ( el => uleb ( el [ 0 ] === '$' ? ctx . func [ el ] : el ) ) ] )
330
339
} ,
331
340
332
341
// (export "name" (kind $name|idx))
@@ -379,7 +388,7 @@ const build = {
379
388
if ( ! offset && ! mem ) offset = inits . shift ( )
380
389
if ( ! offset ) offset = [ 'i32.const' , 0 ]
381
390
382
- ctx . data . push ( [ 0 , ...initGlobal ( offset , ctx ) , ...str ( inits . map ( i => i [ 0 ] === '"' ? i . slice ( 1 , - 1 ) : i ) . join ( '' ) ) ] )
391
+ ctx . data . push ( [ 0 , ...initGlobal ( [ ... offset ] , ctx ) , ...str ( inits . map ( i => i [ 0 ] === '"' ? i . slice ( 1 , - 1 ) : i ) . join ( '' ) ) ] )
383
392
} ,
384
393
385
394
// (start $main)
@@ -389,25 +398,27 @@ const build = {
389
398
}
390
399
391
400
// (i32.const 0), (global.get idx) - instantiation time initializer
392
- const initGlobal = ( [ op , literal , ...args ] , ctx ) => {
393
- if ( op === 'global.get' ) return [ 0x23 , ...uleb ( literal [ 0 ] === '$' ? ctx . global [ literal ] : literal ) , 0x0b ]
401
+ const initGlobal = ( args , ctx ) => {
402
+ let op = args . shift ( )
403
+
404
+ if ( op === 'global.get' ) return [ 0x23 , ...uleb ( args [ 0 ] [ 0 ] === '$' ? ctx . global [ args [ 0 ] ] : args [ 0 ] ) , 0x0b ]
394
405
395
406
// (v128.const i32x4 1 2 3 4), (i32.add a b) etc
396
407
return [
397
408
...( op === 'v128.const' ? [ 0xfd , 0x0c ] : [ 0x41 + [ 'i32.const' , 'i64.const' , 'f32.const' , 'f64.const' ] . indexOf ( op ) ] ) ,
398
- ...consumeConst ( op , [ literal , ... args ] ) , 0x0b
409
+ ...consumeConst ( op , args ) , 0x0b
399
410
]
400
411
}
401
412
402
413
// consume cost, no op type
403
414
const consumeConst = ( op , args ) => {
404
- if ( op === 'global.get' ) return [ 0x23 , ...uleb ( literal [ 0 ] === '$' ? ctx . global [ literal ] : literal ) , 0x0b ]
415
+ // if (op === 'global.get') return [0x23, ...uleb(literal[0] === '$' ? ctx.global[literal] : literal), 0x0b]
405
416
406
417
const [ type ] = op . split ( '.' )
407
418
408
- // (v128.const i32x4 1 2 3 4), (i8x16.shuffle 1 2 ... 15)
409
- if ( type === 'v128' || type === 'i8x16' ) {
410
- let [ t , n ] = ( type === 'v128' ? args . shift ( ) : type ) . split ( 'x' ) ,
419
+ // (v128.const i32x4 1 2 3 4)
420
+ if ( type === 'v128' ) {
421
+ let [ t , n ] = args . shift ( ) . split ( 'x' ) ,
411
422
stride = t . slice ( 1 ) >>> 3 // i16 -> 2, f32 -> 4
412
423
413
424
n = + n
0 commit comments