@@ -31,6 +31,11 @@ internal sealed class TypeMapper : IDisposable
3131 /// </summary>
3232 private const string ExternalObjectPropertyName = "_JavaScriptEngineSwitcher_externalObject" ;
3333
34+ /// <summary>
35+ /// Flag for whether to allow the usage of reflection API in the script code
36+ /// </summary>
37+ private readonly bool _allowReflection ;
38+
3439 /// <summary>
3540 /// Storage for lazy-initialized embedded objects
3641 /// </summary>
@@ -80,8 +85,11 @@ internal sealed class TypeMapper : IDisposable
8085 /// <summary>
8186 /// Constructs an instance of type mapper
8287 /// </summary>
83- public TypeMapper ( )
84- { }
88+ /// <param name="allowReflection">Flag for whether to allow the usage of reflection API in the script code</param>
89+ public TypeMapper ( bool allowReflection )
90+ {
91+ _allowReflection = allowReflection ;
92+ }
8593
8694
8795 /// <summary>
@@ -604,6 +612,11 @@ private void ProjectProperties(EmbeddedItem externalItem)
604612
605613 foreach ( PropertyInfo property in properties )
606614 {
615+ if ( ! IsAvailableProperty ( property ) )
616+ {
617+ continue ;
618+ }
619+
607620 string propertyName = property . Name ;
608621
609622 JsValue descriptorValue = JsValue . CreateObject ( ) ;
@@ -732,14 +745,13 @@ private void ProjectMethods(EmbeddedItem externalItem)
732745
733746 string typeName = type . FullName ;
734747 BindingFlags defaultBindingFlags = ReflectionHelpers . GetDefaultBindingFlags ( instance ) ;
735- IEnumerable < MethodInfo > methods = type . GetMethods ( defaultBindingFlags )
736- . Where ( ReflectionHelpers . IsFullyFledgedMethod ) ;
737- IEnumerable < IGrouping < string , MethodInfo > > methodGroups = methods . GroupBy ( m => m . Name ) ;
748+ MethodInfo [ ] methods = type . GetMethods ( defaultBindingFlags ) ;
749+ Dictionary < string , List < MethodInfo > > availableMethodGroups = GetAvailableMethodGroups ( methods ) ;
738750
739- foreach ( IGrouping < string , MethodInfo > methodGroup in methodGroups )
751+ foreach ( KeyValuePair < string , List < MethodInfo > > methodGroup in availableMethodGroups )
740752 {
741753 string methodName = methodGroup . Key ;
742- MethodInfo [ ] methodCandidates = methodGroup . ToArray ( ) ;
754+ MethodInfo [ ] methodCandidates = methodGroup . Value . ToArray ( ) ;
743755
744756 JsNativeFunction nativeFunction = ( callee , isConstructCall , args , argCount , callbackData ) =>
745757 {
@@ -808,6 +820,55 @@ private void ProjectMethods(EmbeddedItem externalItem)
808820 }
809821 }
810822
823+ [ MethodImpl ( ( MethodImplOptions ) 256 /* AggressiveInlining */ ) ]
824+ private bool IsAvailableProperty ( PropertyInfo property )
825+ {
826+ if ( _allowReflection )
827+ {
828+ return true ;
829+ }
830+
831+ bool isAvailable = ReflectionHelpers . IsAllowedProperty ( property ) ;
832+
833+ return isAvailable ;
834+ }
835+
836+ [ MethodImpl ( ( MethodImplOptions ) 256 /* AggressiveInlining */ ) ]
837+ private Dictionary < string , List < MethodInfo > > GetAvailableMethodGroups ( MethodInfo [ ] methods )
838+ {
839+ int methodCount = methods . Length ;
840+ if ( methodCount == 0 )
841+ {
842+ return new Dictionary < string , List < MethodInfo > > ( ) ;
843+ }
844+
845+ var availableMethodGroups = new Dictionary < string , List < MethodInfo > > ( methodCount ) ;
846+
847+ foreach ( MethodInfo method in methods )
848+ {
849+ if ( ! ReflectionHelpers . IsFullyFledgedMethod ( method )
850+ || ( ! _allowReflection && ! ReflectionHelpers . IsAllowedMethod ( method ) ) )
851+ {
852+ continue ;
853+ }
854+
855+ string methodName = method . Name ;
856+ List < MethodInfo > methodGroup ;
857+
858+ if ( availableMethodGroups . TryGetValue ( methodName , out methodGroup ) )
859+ {
860+ methodGroup . Add ( method ) ;
861+ }
862+ else
863+ {
864+ methodGroup = new List < MethodInfo > { method } ;
865+ availableMethodGroups . Add ( methodName , methodGroup ) ;
866+ }
867+ }
868+
869+ return availableMethodGroups ;
870+ }
871+
811872 private object [ ] GetHostItemMemberArguments ( JsValue [ ] args , int maxArgCount = - 1 )
812873 {
813874 if ( args == null )
0 commit comments