@@ -147,7 +147,24 @@ x is not CompilerException
147147 {
148148 try
149149 {
150- return ( Instance < TExports > ) constructor . Invoke ( [ imports ] ) ;
150+ Func < string , string , RuntimeImport > findImport = ( module , field ) =>
151+ {
152+ #if NETSTANDARD
153+ if ( module == null )
154+ throw new ArgumentNullException ( nameof ( module ) ) ;
155+ if ( field == null )
156+ throw new ArgumentNullException ( nameof ( field ) ) ;
157+ #else
158+ ArgumentNullException . ThrowIfNull ( module , nameof ( module ) ) ;
159+ ArgumentNullException . ThrowIfNull ( field , nameof ( field ) ) ;
160+ #endif
161+
162+ return ! imports . TryGetValue ( module , out var fields ) || ! fields . TryGetValue ( field , out var import )
163+ ? throw new ImportException ( $ "Missing import for { module } ::{ field } .")
164+ : import ;
165+ } ;
166+
167+ return ( Instance < TExports > ) constructor . Invoke ( [ findImport ] ) ;
151168 }
152169 catch ( TargetInvocationException x )
153170#if DEBUG
@@ -319,8 +336,9 @@ private static ConstructorInfo FromBinary(
319336 var instanceConstructor = exportsBuilder. DefineConstructor(
320337 ConstructorAttributes ,
321338 CallingConventions . Standard ,
322- [ typeof ( IDictionary < string , IDictionary < string , RuntimeImport > > ) ]
339+ [ typeof ( Func < string , string , RuntimeImport > ) ]
323340 ) ;
341+ instanceConstructor . DefineParameter ( 1 , ParameterAttributes . None , "findImport ") ;
324342 instanceConstructorIL = instanceConstructor . GetILGenerator ( ) ;
325343 {
326344 if ( exportContainer is TypeBuilder buildableExportContainer )
@@ -642,7 +660,7 @@ private static ConstructorInfo FromBinary(
642660 var instanceConstructor = instanceBuilder. DefineConstructor(
643661 ConstructorAttributes,
644662 CallingConventions. Standard,
645- [ typeof ( IDictionary < string , IDictionary < string , RuntimeImport> >) ]
663+ [ typeof ( Func < string , string , RuntimeImport> ) ]
646664 ) ;
647665 var il = instanceConstructor. GetILGenerator( ) ;
648666 var memoryAllocated = checked ( memoryPagesMaximum * Memory. PageSize) ;
@@ -704,6 +722,7 @@ private static (
704722 var functionImportTypes = new List < Signature > ( count ) ;
705723 var globalImports = new List < GlobalInfo > ( count ) ;
706724 var missingDelegates = new List < MissingDelegateType > ( ) ;
725+ var importFinderInvoke = typeof ( Func < string , string , RuntimeImport > ) . GetMethod ( "Invoke" ) ! ;
707726
708727 for ( var i = 0 ; i < count ; i++ )
709728 {
@@ -759,12 +778,10 @@ private static (
759778 instanceConstructorIL . Emit ( OpCodes . Ldarg_1 ) ;
760779 instanceConstructorIL . Emit ( OpCodes . Ldstr , moduleName ) ;
761780 instanceConstructorIL . Emit ( OpCodes . Ldstr , fieldName ) ;
762- instanceConstructorIL . Emit ( OpCodes . Call ,
763- typeof ( Helpers )
764- . GetTypeInfo ( )
765- . GetDeclaredMethod ( nameof ( Helpers . FindImport ) ) !
766- . MakeGenericMethod ( typeof ( FunctionImport ) )
767- ) ;
781+ instanceConstructorIL . Emit ( OpCodes . Callvirt , importFinderInvoke ) ;
782+
783+ ImportException . EmitTryCast ( instanceConstructorIL , typeof ( FunctionImport ) ) ;
784+
768785 instanceConstructorIL . Emit ( OpCodes . Callvirt ,
769786 typeof ( FunctionImport )
770787 . GetTypeInfo ( )
@@ -794,12 +811,10 @@ private static (
794811 instanceConstructorIL. Emit ( OpCodes . Ldarg_1 ) ;
795812 instanceConstructorIL. Emit ( OpCodes . Ldstr , moduleName ) ;
796813 instanceConstructorIL. Emit ( OpCodes . Ldstr , fieldName ) ;
797- instanceConstructorIL. Emit ( OpCodes . Call ,
798- typeof ( Helpers )
799- . GetTypeInfo ( )
800- . GetDeclaredMethod ( nameof ( Helpers . FindImport ) ) !
801- . MakeGenericMethod ( typeof ( FunctionTable ) )
802- ) ;
814+ instanceConstructorIL. Emit ( OpCodes . Callvirt , importFinderInvoke ) ;
815+
816+ ImportException. EmitTryCast ( instanceConstructorIL , typeof ( FunctionTable ) ) ;
817+
803818 instanceConstructorIL. Emit ( OpCodes . Stfld , functionTable ) ;
804819 }
805820 break;
@@ -829,12 +844,10 @@ private static (
829844 instanceConstructorIL. Emit ( OpCodes . Ldarg_1 ) ;
830845 instanceConstructorIL. Emit ( OpCodes . Ldstr , moduleName ) ;
831846 instanceConstructorIL. Emit ( OpCodes . Ldstr , fieldName ) ;
832- instanceConstructorIL. Emit ( OpCodes . Call ,
833- typeof ( Helpers )
834- . GetTypeInfo ( )
835- . GetDeclaredMethod ( nameof ( Helpers . FindImport ) ) !
836- . MakeGenericMethod ( typeof ( MemoryImport ) )
837- ) ;
847+ instanceConstructorIL. Emit ( OpCodes . Callvirt , importFinderInvoke ) ;
848+
849+ ImportException. EmitTryCast ( instanceConstructorIL , typeof ( MemoryImport ) ) ;
850+
838851 instanceConstructorIL. Emit ( OpCodes . Callvirt ,
839852 typeof ( MemoryImport )
840853 . GetTypeInfo ( )
@@ -877,12 +890,10 @@ private static (
877890 instanceConstructorIL . Emit ( OpCodes . Ldarg_1 ) ;
878891 instanceConstructorIL . Emit ( OpCodes . Ldstr , moduleName ) ;
879892 instanceConstructorIL . Emit ( OpCodes . Ldstr , fieldName ) ;
880- instanceConstructorIL . Emit ( OpCodes . Call ,
881- typeof ( Helpers )
882- . GetTypeInfo ( )
883- . GetDeclaredMethod ( nameof ( Helpers . FindImport ) ) !
884- . MakeGenericMethod ( typeof ( GlobalImport ) )
885- ) ;
893+ instanceConstructorIL . Emit ( OpCodes . Callvirt , importFinderInvoke ) ;
894+
895+ ImportException . EmitTryCast ( instanceConstructorIL , typeof ( GlobalImport ) ) ;
896+
886897 instanceConstructorIL . Emit ( OpCodes . Callvirt ,
887898 typeof ( GlobalImport )
888899 . GetTypeInfo ( )
@@ -921,12 +932,10 @@ private static (
921932 instanceConstructorIL . Emit ( OpCodes . Ldarg_1 ) ;
922933 instanceConstructorIL . Emit ( OpCodes . Ldstr , moduleName ) ;
923934 instanceConstructorIL . Emit ( OpCodes . Ldstr , fieldName ) ;
924- instanceConstructorIL . Emit ( OpCodes . Call ,
925- typeof ( Helpers )
926- . GetTypeInfo ( )
927- . GetDeclaredMethod ( nameof ( Helpers . FindImport ) ) !
928- . MakeGenericMethod ( typeof ( GlobalImport ) )
929- ) ;
935+ instanceConstructorIL . Emit ( OpCodes . Callvirt , importFinderInvoke ) ;
936+
937+ ImportException . EmitTryCast ( instanceConstructorIL , typeof ( GlobalImport ) ) ;
938+
930939 instanceConstructorIL . Emit ( OpCodes . Callvirt ,
931940 typeof ( GlobalImport )
932941 . GetTypeInfo ( )
0 commit comments