@@ -256,9 +256,8 @@ impl Encoder<'_> {
256
256
257
257
if !list. is_empty ( ) {
258
258
let mut branch_hints = wasm_encoder:: BranchHints :: new ( ) ;
259
- let mut code_section = Vec :: new ( ) ;
259
+ let mut code_section = wasm_encoder :: CodeSection :: new ( ) ;
260
260
261
- list. len ( ) . encode ( & mut code_section) ;
262
261
for func in list. iter ( ) {
263
262
let hints = func. encode ( & mut code_section, dwarf. as_deref_mut ( ) ) ;
264
263
if !hints. is_empty ( ) {
@@ -274,13 +273,10 @@ impl Encoder<'_> {
274
273
}
275
274
276
275
// Finally, insert the Code section from the tmp buffer
277
- self . wasm . section ( & wasm_encoder:: RawSection {
278
- id : wasm_encoder:: SectionId :: Code as u8 ,
279
- data : & code_section,
280
- } ) ;
276
+ self . wasm . section ( & code_section) ;
281
277
282
278
if let Some ( dwarf) = & mut dwarf {
283
- dwarf. set_code_section_size ( code_section. len ( ) ) ;
279
+ dwarf. set_code_section_size ( code_section. byte_len ( ) ) ;
284
280
}
285
281
}
286
282
self . custom_sections ( CustomPlace :: After ( CustomPlaceAnchor :: Code ) ) ;
@@ -449,90 +445,13 @@ impl Encode for Option<Id<'_>> {
449
445
450
446
impl < ' a > Encode for ValType < ' a > {
451
447
fn encode ( & self , e : & mut Vec < u8 > ) {
452
- match self {
453
- ValType :: I32 => e. push ( 0x7f ) ,
454
- ValType :: I64 => e. push ( 0x7e ) ,
455
- ValType :: F32 => e. push ( 0x7d ) ,
456
- ValType :: F64 => e. push ( 0x7c ) ,
457
- ValType :: V128 => e. push ( 0x7b ) ,
458
- ValType :: Ref ( ty) => {
459
- ty. encode ( e) ;
460
- }
461
- }
448
+ wasm_encoder:: Encode :: encode ( & wasm_encoder:: ValType :: from ( * self ) , e)
462
449
}
463
450
}
464
451
465
452
impl < ' a > Encode for HeapType < ' a > {
466
453
fn encode ( & self , e : & mut Vec < u8 > ) {
467
- match self {
468
- HeapType :: Abstract { shared, ty } => {
469
- if * shared {
470
- e. push ( 0x65 ) ;
471
- }
472
- ty. encode ( e)
473
- }
474
- // Note that this is encoded as a signed leb128 so be sure to cast
475
- // to an i64 first
476
- HeapType :: Concrete ( Index :: Num ( n, _) ) => i64:: from ( * n) . encode ( e) ,
477
- HeapType :: Concrete ( Index :: Id ( n) ) => {
478
- panic ! ( "unresolved index in emission: {:?}" , n)
479
- }
480
- }
481
- }
482
- }
483
-
484
- impl < ' a > Encode for AbstractHeapType {
485
- fn encode ( & self , e : & mut Vec < u8 > ) {
486
- use AbstractHeapType :: * ;
487
- match self {
488
- Func => e. push ( 0x70 ) ,
489
- Extern => e. push ( 0x6f ) ,
490
- Cont => e. push ( 0x68 ) ,
491
- Exn => e. push ( 0x69 ) ,
492
- Any => e. push ( 0x6e ) ,
493
- Eq => e. push ( 0x6d ) ,
494
- Struct => e. push ( 0x6b ) ,
495
- Array => e. push ( 0x6a ) ,
496
- I31 => e. push ( 0x6c ) ,
497
- NoFunc => e. push ( 0x73 ) ,
498
- NoExtern => e. push ( 0x72 ) ,
499
- NoExn => e. push ( 0x74 ) ,
500
- NoCont => e. push ( 0x75 ) ,
501
- None => e. push ( 0x71 ) ,
502
- }
503
- }
504
- }
505
-
506
- impl < ' a > Encode for RefType < ' a > {
507
- fn encode ( & self , e : & mut Vec < u8 > ) {
508
- match self {
509
- // Binary abbreviations (i.e., short form), for when the ref is
510
- // nullable.
511
- RefType {
512
- nullable : true ,
513
- heap : heap @ HeapType :: Abstract { .. } ,
514
- } => {
515
- heap. encode ( e) ;
516
- }
517
-
518
- // Generic 'ref null <heaptype>' encoding (i.e., long form).
519
- RefType {
520
- nullable : true ,
521
- heap,
522
- } => {
523
- e. push ( 0x63 ) ;
524
- heap. encode ( e) ;
525
- }
526
-
527
- // Generic 'ref <heaptype>' encoding.
528
- RefType {
529
- nullable : false ,
530
- heap,
531
- } => {
532
- e. push ( 0x64 ) ;
533
- heap. encode ( e) ;
534
- }
535
- }
454
+ wasm_encoder:: Encode :: encode ( & wasm_encoder:: HeapType :: from ( * self ) , e)
536
455
}
537
456
}
538
457
@@ -672,7 +591,7 @@ impl SectionItem for Table<'_> {
672
591
ty,
673
592
init_expr : Some ( init_expr) ,
674
593
} => {
675
- section. table_with_init ( ty. to_table_type ( ) , & init_expr. to_const_expr ( None ) ) ;
594
+ section. table_with_init ( ty. to_table_type ( ) , & init_expr. to_const_expr ( ) ) ;
676
595
}
677
596
_ => panic ! ( "TableKind should be normal during encoding" ) ,
678
597
}
@@ -701,7 +620,7 @@ impl SectionItem for Global<'_> {
701
620
fn encode ( & self , section : & mut wasm_encoder:: GlobalSection ) {
702
621
assert ! ( self . exports. names. is_empty( ) ) ;
703
622
let init = match & self . kind {
704
- GlobalKind :: Inline ( expr) => expr. to_const_expr ( None ) ,
623
+ GlobalKind :: Inline ( expr) => expr. to_const_expr ( ) ,
705
624
_ => panic ! ( "GlobalKind should be inline during encoding" ) ,
706
625
} ;
707
626
section. global ( self . ty . to_global_type ( ) , & init) ;
@@ -730,14 +649,14 @@ impl SectionItem for Elem<'_> {
730
649
}
731
650
ElemPayload :: Exprs { exprs, ty } => Elements :: Expressions (
732
651
( * ty) . into ( ) ,
733
- Cow :: Owned ( exprs. iter ( ) . map ( |e| e. to_const_expr ( None ) ) . collect ( ) ) ,
652
+ Cow :: Owned ( exprs. iter ( ) . map ( |e| e. to_const_expr ( ) ) . collect ( ) ) ,
734
653
) ,
735
654
} ;
736
655
match & self . kind {
737
656
ElemKind :: Active { table, offset } => {
738
657
section. active (
739
658
table. map ( |t| t. unwrap_u32 ( ) ) ,
740
- & offset. to_const_expr ( None ) ,
659
+ & offset. to_const_expr ( ) ,
741
660
elements,
742
661
) ;
743
662
}
@@ -765,7 +684,7 @@ impl SectionItem for Data<'_> {
765
684
section. passive ( data) ;
766
685
}
767
686
DataKind :: Active { memory, offset } => {
768
- section. active ( memory. unwrap_u32 ( ) , & offset. to_const_expr ( None ) , data) ;
687
+ section. active ( memory. unwrap_u32 ( ) , & offset. to_const_expr ( ) , data) ;
769
688
}
770
689
}
771
690
}
@@ -779,7 +698,7 @@ impl Func<'_> {
779
698
/// for each instruction.
780
699
fn encode (
781
700
& self ,
782
- e : & mut Vec < u8 > ,
701
+ section : & mut wasm_encoder :: CodeSection ,
783
702
mut dwarf : Option < & mut dwarf:: Dwarf > ,
784
703
) -> Vec < wasm_encoder:: BranchHint > {
785
704
assert ! ( self . exports. names. is_empty( ) ) ;
@@ -799,55 +718,41 @@ impl Func<'_> {
799
718
// Encode the function into a temporary vector because functions are
800
719
// prefixed with their length. The temporary vector, when encoded,
801
720
// encodes its length first then the body.
802
- let mut tmp = Vec :: new ( ) ;
803
- locals. encode ( & mut tmp) ;
804
- let branch_hints = expr. encode ( & mut tmp, dwarf. as_deref_mut ( ) ) ;
805
- tmp. encode ( e) ;
721
+ let mut func =
722
+ wasm_encoder:: Function :: new_with_locals_types ( locals. iter ( ) . map ( |t| t. ty . into ( ) ) ) ;
723
+ let branch_hints = expr. encode ( & mut func, dwarf. as_deref_mut ( ) ) ;
724
+ let func_size = func. byte_len ( ) ;
725
+ section. function ( & func) ;
806
726
807
727
if let Some ( dwarf) = & mut dwarf {
808
- dwarf. end_func ( tmp . len ( ) , e . len ( ) ) ;
728
+ dwarf. end_func ( func_size , section . byte_len ( ) ) ;
809
729
}
810
730
811
731
branch_hints
812
732
}
813
733
}
814
734
815
- impl Encode for Box < [ Local < ' _ > ] > {
816
- fn encode ( & self , e : & mut Vec < u8 > ) {
817
- let mut locals_compressed = Vec :: < ( u32 , ValType ) > :: new ( ) ;
818
- for local in self . iter ( ) {
819
- if let Some ( ( cnt, prev) ) = locals_compressed. last_mut ( ) {
820
- if * prev == local. ty {
821
- * cnt += 1 ;
822
- continue ;
823
- }
824
- }
825
- locals_compressed. push ( ( 1 , local. ty ) ) ;
826
- }
827
- locals_compressed. encode ( e) ;
828
- }
829
- }
830
-
831
735
impl Expression < ' _ > {
832
736
/// Encodes this expression into `e` and optionally tracks debugging
833
737
/// information for each instruction in `dwarf`.
834
738
///
835
739
/// Returns all branch hints, if any, found while parsing this function.
836
740
fn encode (
837
741
& self ,
838
- e : & mut Vec < u8 > ,
742
+ func : & mut wasm_encoder :: Function ,
839
743
mut dwarf : Option < & mut dwarf:: Dwarf > ,
840
744
) -> Vec < wasm_encoder:: BranchHint > {
841
745
let mut hints = Vec :: with_capacity ( self . branch_hints . len ( ) ) ;
842
746
let mut next_hint = self . branch_hints . iter ( ) . peekable ( ) ;
747
+ let mut tmp = Vec :: new ( ) ;
843
748
844
749
for ( i, instr) in self . instrs . iter ( ) . enumerate ( ) {
845
750
// Branch hints are stored in order of increasing `instr_index` so
846
751
// check to see if the next branch hint matches this instruction's
847
752
// index.
848
753
if let Some ( hint) = next_hint. next_if ( |h| h. instr_index == i) {
849
754
hints. push ( wasm_encoder:: BranchHint {
850
- branch_func_offset : u32:: try_from ( e . len ( ) ) . unwrap ( ) ,
755
+ branch_func_offset : u32:: try_from ( func . byte_len ( ) + tmp . len ( ) ) . unwrap ( ) ,
851
756
branch_hint_value : hint. value ,
852
757
} ) ;
853
758
}
@@ -856,22 +761,24 @@ impl Expression<'_> {
856
761
// and source location.
857
762
if let Some ( dwarf) = & mut dwarf {
858
763
if let Some ( span) = self . instr_spans . as_ref ( ) . map ( |s| s[ i] ) {
859
- dwarf. instr ( e . len ( ) , span) ;
764
+ dwarf. instr ( func . byte_len ( ) + tmp . len ( ) , span) ;
860
765
}
861
766
}
862
767
863
768
// Finally emit the instruction and move to the next.
864
- instr. encode ( e ) ;
769
+ instr. encode ( & mut tmp ) ;
865
770
}
866
- e. push ( 0x0b ) ;
771
+ func. raw ( tmp. iter ( ) . copied ( ) ) ;
772
+ func. instruction ( & wasm_encoder:: Instruction :: End ) ;
867
773
868
774
hints
869
775
}
870
776
871
- fn to_const_expr ( & self , dwarf : Option < & mut dwarf :: Dwarf > ) -> wasm_encoder:: ConstExpr {
777
+ fn to_const_expr ( & self ) -> wasm_encoder:: ConstExpr {
872
778
let mut tmp = Vec :: new ( ) ;
873
- self . encode ( & mut tmp, dwarf) ;
874
- tmp. pop ( ) ; // remove trailing 0x0b byte which wasm-encoder doesn't want
779
+ for instr in self . instrs . iter ( ) {
780
+ instr. encode ( & mut tmp) ;
781
+ }
875
782
wasm_encoder:: ConstExpr :: raw ( tmp)
876
783
}
877
784
}
0 commit comments