@@ -426,19 +426,19 @@ impl Instance {
426426 }
427427
428428 // Validate data segments (bounds check, defer writes)
429- let mut pending_data: Vec < ( u32 , Vec < u8 > ) > = Vec :: new ( ) ;
429+ let mut pending_data: Vec < ( u32 , usize , usize ) > = Vec :: new ( ) ;
430430 if let Some ( mem) = & inst. memory {
431431 for seg in & module. data_segments {
432432 let mut ip = seg. initializer_offset ;
433433 let offset = Instance :: eval_const ( & module, & mut ip, & inst. globals ) ?. as_u32 ( ) ;
434- let bytes_vec = module . bytes [ seg. data_range . clone ( ) ] . to_vec ( ) ;
434+ let data_len = seg. data_range . end - seg . data_range . start ;
435435 let m = mem. borrow ( ) ;
436- let end = ( offset as usize ) . saturating_add ( bytes_vec . len ( ) ) ;
436+ let end = ( offset as usize ) . saturating_add ( data_len ) ;
437437 if end > ( m. size ( ) as usize ) * ( WasmMemory :: PAGE_SIZE as usize ) {
438438 return Err ( Error :: link ( DATA_SEG_DNF ) ) ;
439439 }
440440 drop ( m) ;
441- pending_data. push ( ( offset, bytes_vec ) ) ;
441+ pending_data. push ( ( offset, seg . data_range . start , seg . data_range . end ) ) ;
442442 }
443443 }
444444
@@ -466,8 +466,8 @@ impl Instance {
466466 // Apply data segments (writes), after elements
467467 if let Some ( mem) = & inst. memory {
468468 let mut m = mem. borrow_mut ( ) ;
469- for ( offset, bytes_vec ) in & pending_data {
470- m. write_bytes ( * offset, bytes_vec ) . map_err ( Error :: trap) ?;
469+ for & ( offset, start , end ) in & pending_data {
470+ m. write_bytes ( offset, & module . bytes [ start..end ] ) . map_err ( Error :: trap) ?;
471471 }
472472 }
473473
@@ -723,29 +723,25 @@ impl Instance {
723723 }
724724 macro_rules! shift {
725725 ( u32 , $op: tt) => { {
726- let b = pop_val!( ) . as_u32( ) % 32 ;
727- let a = pop_val!( ) . as_u32( ) ;
728- stack. push( WasmValue :: from_u32( a $op b) ) ;
726+ let ( a, b) = peek_two!( u32 ) ;
727+ overwrite!( WasmValue :: from_u32( a $op ( b % 32 ) ) ) ;
729728 } } ;
730729 ( u64 , $op: tt) => { {
731- let b = pop_val!( ) . as_u64( ) % 64 ;
732- let a = pop_val!( ) . as_u64( ) ;
733- stack. push( WasmValue :: from_u64( a $op b) ) ;
730+ let ( a, b) = peek_two!( u64 ) ;
731+ overwrite!( WasmValue :: from_u64( a $op ( b % 64 ) ) ) ;
734732 } } ;
735733 }
736734 macro_rules! rotate {
737735 ( u32 , $dir: ident) => { {
738- let b = pop_val!( ) . as_u32( ) ;
739- let a = pop_val!( ) . as_u32( ) ;
736+ let ( a, b) = peek_two!( u32 ) ;
740737 paste! {
741- stack . push ( WasmValue :: from_u32( a. [ <rotate_ $dir>] ( b % 32 ) ) ) ;
738+ overwrite! ( WasmValue :: from_u32( a. [ <rotate_ $dir>] ( b % 32 ) ) ) ;
742739 }
743740 } } ;
744741 ( u64 , $dir: ident) => { {
745- let b = pop_val!( ) . as_u64( ) ;
746- let a = pop_val!( ) . as_u64( ) ;
742+ let ( a, b) = peek_two!( u64 ) ;
747743 paste! {
748- stack . push ( WasmValue :: from_u64( a. [ <rotate_ $dir>] ( ( b % 64 ) as u32 ) ) ) ;
744+ overwrite! ( WasmValue :: from_u64( a. [ <rotate_ $dir>] ( ( b % 64 ) as u32 ) ) ) ;
749745 }
750746 } } ;
751747 }
@@ -762,8 +758,7 @@ impl Instance {
762758 ( $type: ident, max) => { { minmax!( @impl $type, max, false ) } } ;
763759 ( @impl $type: ident, $op: ident, $want_negative: literal) => { {
764760 paste! {
765- let b = pop_val!( ) . [ <as_ $type>] ( ) ;
766- let a = pop_val!( ) . [ <as_ $type>] ( ) ;
761+ let ( a, b) = peek_two!( $type) ;
767762
768763 let result = if a. is_nan( ) {
769764 a
@@ -777,25 +772,23 @@ impl Instance {
777772 a. $op( b)
778773 } ;
779774
780- stack . push ( WasmValue :: [ <from_ $type>] ( result) ) ;
775+ overwrite! ( WasmValue :: [ <from_ $type>] ( result) ) ;
781776 }
782777 } } ;
783778 }
784779 macro_rules! shr_s {
785780 ( $int_type: ident, $uint_type: ident, $bits: literal) => { {
786781 paste! {
787- let b = pop_val!( ) . [ <as_ $uint_type>] ( ) % $bits;
788- let a = pop_val!( ) . [ <as_ $int_type>] ( ) ;
789- stack. push( WasmValue :: [ <from_ $int_type>] ( a >> b) ) ;
782+ let ( a, b) = peek_two!( $int_type) ;
783+ overwrite!( WasmValue :: [ <from_ $int_type>] ( a >> ( b as $uint_type % $bits) ) ) ;
790784 }
791785 } } ;
792786 }
793787 macro_rules! copysign {
794788 ( $type: ident) => { {
795789 paste! {
796- let b = pop_val!( ) . [ <as_ $type>] ( ) ;
797- let a = pop_val!( ) . [ <as_ $type>] ( ) ;
798- stack. push( WasmValue :: [ <from_ $type>] ( a. copysign( b) ) ) ;
790+ let ( a, b) = peek_two!( $type) ;
791+ overwrite!( WasmValue :: [ <from_ $type>] ( a. copysign( b) ) ) ;
799792 }
800793 } } ;
801794 }
@@ -851,45 +844,38 @@ impl Instance {
851844 macro_rules! div_s {
852845 ( $int_type: ident) => { {
853846 paste! {
854- let b = pop_val!( ) . [ <as_ $int_type>] ( ) ;
855- let a = pop_val!( ) . [ <as_ $int_type>] ( ) ;
847+ let ( a, b) = peek_two!( $int_type) ;
856848 if b == 0 { return Err ( Error :: trap( DIVIDE_BY_ZERO ) ) ; }
857849 if a == $int_type:: MIN && b == -1 { return Err ( Error :: trap( INTEGER_OVERFLOW ) ) ; }
858- stack . push ( WasmValue :: [ <from_ $int_type>] ( a / b) ) ;
850+ overwrite! ( WasmValue :: [ <from_ $int_type>] ( a / b) ) ;
859851 }
860852 } } ;
861853 }
862854 macro_rules! div_u {
863855 ( $uint_type: ident) => { {
864856 paste! {
865- let b = pop_val!( ) . [ <as_ $uint_type>] ( ) ;
866- let a = pop_val!( ) . [ <as_ $uint_type>] ( ) ;
857+ let ( a, b) = peek_two!( $uint_type) ;
867858 if b == 0 { return Err ( Error :: trap( DIVIDE_BY_ZERO ) ) ; }
868- stack . push ( WasmValue :: [ <from_ $uint_type>] ( a / b) ) ;
859+ overwrite! ( WasmValue :: [ <from_ $uint_type>] ( a / b) ) ;
869860 }
870861 } } ;
871862 }
872863 macro_rules! rem_s {
873864 ( $int_type: ident) => { {
874865 paste! {
875- let b = pop_val!( ) . [ <as_ $int_type>] ( ) ;
876- let a = pop_val!( ) . [ <as_ $int_type>] ( ) ;
866+ let ( a, b) = peek_two!( $int_type) ;
877867 if b == 0 { return Err ( Error :: trap( DIVIDE_BY_ZERO ) ) ; }
878- if a == $int_type:: MIN && b == -1 {
879- stack. push( WasmValue :: [ <from_ $int_type>] ( 0 ) ) ;
880- } else {
881- stack. push( WasmValue :: [ <from_ $int_type>] ( a % b) ) ;
882- }
868+ let result = if a == $int_type:: MIN && b == -1 { 0 } else { a % b } ;
869+ overwrite!( WasmValue :: [ <from_ $int_type>] ( result) ) ;
883870 }
884871 } } ;
885872 }
886873 macro_rules! rem_u {
887874 ( $uint_type: ident) => { {
888875 paste! {
889- let b = pop_val!( ) . [ <as_ $uint_type>] ( ) ;
890- let a = pop_val!( ) . [ <as_ $uint_type>] ( ) ;
876+ let ( a, b) = peek_two!( $uint_type) ;
891877 if b == 0 { return Err ( Error :: trap( DIVIDE_BY_ZERO ) ) ; }
892- stack . push ( WasmValue :: [ <from_ $uint_type>] ( a % b) ) ;
878+ overwrite! ( WasmValue :: [ <from_ $uint_type>] ( a % b) ) ;
893879 }
894880 } } ;
895881 }
0 commit comments