@@ -211,47 +211,35 @@ pub fn offset_ip(ip: Ip, offset: BranchOffset) -> Ip {
211211 unsafe { ip. offset ( i32:: from ( offset) as isize ) }
212212}
213213
214- pub fn resolve_func ( instance : NonNull < InstanceEntity > , func : index:: Func ) -> Func {
215- let inst = unsafe { instance. as_ref ( ) } ;
216- let Some ( func) = inst. get_func ( u32:: from ( func) ) else {
217- unreachable ! ( "missing func at: {}" , u32 :: from( func) )
218- } ;
219- func
220- }
221-
222- pub fn resolve_global ( instance : NonNull < InstanceEntity > , global : index:: Global ) -> Global {
223- let inst = unsafe { instance. as_ref ( ) } ;
224- let Some ( global) = inst. get_global ( u32:: from ( global) ) else {
225- unreachable ! ( "missing global at: {}" , u32 :: from( global) )
226- } ;
227- global
228- }
229-
230- pub fn resolve_memory ( instance : NonNull < InstanceEntity > , memory : index:: Memory ) -> Memory {
231- let inst = unsafe { instance. as_ref ( ) } ;
232- let Some ( memory) = inst. get_memory ( u32:: from ( u16:: from ( memory) ) ) else {
233- unreachable ! ( "missing memory at: {}" , u16 :: from( memory) )
234- } ;
235- memory
236- }
237-
238- pub fn resolve_table ( instance : NonNull < InstanceEntity > , table : index:: Table ) -> Table {
239- let inst = unsafe { instance. as_ref ( ) } ;
240- let Some ( table) = inst. get_table ( u32:: from ( table) ) else {
241- unreachable ! ( "missing table at: {}" , u32 :: from( table) )
214+ macro_rules! impl_resolve_entity {
215+ (
216+ $( fn $fn: ident( $param: ident: $ty: ty) -> $ret: ty = $getter: expr ) ;* $( ; ) ?
217+ ) => {
218+ $(
219+ pub fn $fn( instance: NonNull <InstanceEntity >, $param: $ty) -> $ret {
220+ let instance = unsafe { instance. as_ref( ) } ;
221+ let index = :: core:: primitive:: u32 :: from( $param) ;
222+ let Some ( $param) = $getter( instance, index) else {
223+ unsafe {
224+ $crate:: engine:: utils:: unreachable_unchecked!(
225+ :: core:: concat!( "missing " , :: core:: stringify!( $param) , " at: {:?}" ) ,
226+ index,
227+ )
228+ }
229+ } ;
230+ $param
231+ }
232+ ) *
242233 } ;
243- table
244234}
245-
246- pub fn resolve_func_type_dedup (
247- instance : NonNull < InstanceEntity > ,
248- func_type : index:: FuncType ,
249- ) -> DedupFuncType {
250- let inst = unsafe { instance. as_ref ( ) } ;
251- let Some ( func_type) = inst. get_signature ( u32:: from ( func_type) ) else {
252- unreachable ! ( "missing func type at: {}" , u32 :: from( func_type) )
235+ impl_resolve_entity ! {
236+ fn resolve_func( func: index:: Func ) -> Func = InstanceEntity :: get_func;
237+ fn resolve_global( func: index:: Global ) -> Global = InstanceEntity :: get_global;
238+ fn resolve_memory( func: index:: Memory ) -> Memory = InstanceEntity :: get_memory;
239+ fn resolve_table( func: index:: Table ) -> Table = InstanceEntity :: get_table;
240+ fn resolve_func_type_dedup( func_type: index:: FuncType ) -> DedupFuncType = {
241+ |instance: & InstanceEntity , index: u32 | instance. get_signature( index) . copied( )
253242 } ;
254- * func_type
255243}
256244
257245pub fn resolve_indirect_func (
0 commit comments