|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Reflection; |
| 5 | +using System.Runtime.CompilerServices; |
| 6 | + |
| 7 | +using OriginalTypeResolver = Jint.Runtime.Interop.TypeResolver; |
| 8 | + |
| 9 | +namespace JavaScriptEngineSwitcher.Jint |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// Interop strategies for different values of the <see cref="JintSettings.AllowReflection"/> configuration property |
| 13 | + /// </summary> |
| 14 | + internal static class CustomTypeResolvers |
| 15 | + { |
| 16 | + private static readonly PropertyInfo[] _disallowedProperties = |
| 17 | + { |
| 18 | + typeof(Delegate).GetProperty("Method"), |
| 19 | + typeof(Exception).GetProperty("TargetSite") |
| 20 | + }; |
| 21 | + |
| 22 | + private static readonly MethodInfo[] _disallowedMethods = |
| 23 | + { |
| 24 | + typeof(object).GetMethod("GetType"), |
| 25 | + typeof(Exception).GetMethod("GetType") |
| 26 | + }; |
| 27 | + |
| 28 | + private static readonly Lazy<OriginalTypeResolver> _allowingReflection = new Lazy<OriginalTypeResolver>( |
| 29 | + () => new OriginalTypeResolver() { MemberFilter = _ => true }); |
| 30 | + |
| 31 | + private static readonly Lazy<OriginalTypeResolver> _disallowingReflection = new Lazy<OriginalTypeResolver>( |
| 32 | + () => new OriginalTypeResolver() { MemberFilter = IsAllowedMember }); |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Gets a interop strategy that allows the usage of reflection API in the script code |
| 36 | + /// </summary> |
| 37 | + public static OriginalTypeResolver AllowingReflection => _allowingReflection.Value; |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Gets a interop strategy that disallows the usage of reflection API in the script code |
| 41 | + /// </summary> |
| 42 | + public static OriginalTypeResolver DisallowingReflection => _disallowingReflection.Value; |
| 43 | + |
| 44 | + |
| 45 | + private static bool IsAllowedMember(MemberInfo member) |
| 46 | + { |
| 47 | + bool isAllowed = true; |
| 48 | + |
| 49 | + if (member is PropertyInfo) |
| 50 | + { |
| 51 | + isAllowed = IsAllowedProperty((PropertyInfo)member); |
| 52 | + } |
| 53 | + else if (member is MethodInfo) |
| 54 | + { |
| 55 | + isAllowed = IsAllowedMethod((MethodInfo)member); |
| 56 | + } |
| 57 | + |
| 58 | + return isAllowed; |
| 59 | + } |
| 60 | + |
| 61 | + [MethodImpl((MethodImplOptions)256 /* AggressiveInlining */)] |
| 62 | + private static bool IsAllowedProperty(PropertyInfo property) |
| 63 | + { |
| 64 | + bool isAllowed = !_disallowedProperties.Contains(property, MemberComparer<PropertyInfo>.Instance); |
| 65 | + |
| 66 | + return isAllowed; |
| 67 | + } |
| 68 | + |
| 69 | + [MethodImpl((MethodImplOptions)256 /* AggressiveInlining */)] |
| 70 | + private static bool IsAllowedMethod(MethodInfo method) |
| 71 | + { |
| 72 | + bool isAllowed = !_disallowedMethods.Contains(method, MemberComparer<MethodInfo>.Instance); |
| 73 | + |
| 74 | + return isAllowed; |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | + private sealed class MemberComparer<T> : EqualityComparer<T> |
| 79 | + where T : MemberInfo |
| 80 | + { |
| 81 | + public static MemberComparer<T> Instance { get; } = new MemberComparer<T>(); |
| 82 | + |
| 83 | + |
| 84 | + private MemberComparer() |
| 85 | + { } |
| 86 | + |
| 87 | + |
| 88 | + #region MemberComparer overrides |
| 89 | + |
| 90 | + public override bool Equals(T x, T y) |
| 91 | + { |
| 92 | + return x.Module == y.Module && x.MetadataToken == y.MetadataToken; |
| 93 | + } |
| 94 | + |
| 95 | + public override int GetHashCode(T obj) |
| 96 | + { |
| 97 | + return obj != null ? obj.GetHashCode() : 0; |
| 98 | + } |
| 99 | + |
| 100 | + #endregion |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments