コード例 #1
0
        internal static void CacheMethod(
            MethodInformation mi,
            object target,
            string methodName,
            object[] arguments,
            CallsiteCacheEntryFlags flags)
        {
            Type targetType = (flags & (CallsiteCacheEntryFlags.Static | CallsiteCacheEntryFlags.Constructor)) == CallsiteCacheEntryFlags.None ? target.GetType() : (Type)target;

            if (targetType == typeof(PSObject) || targetType == typeof(PSCustomObject))
            {
                return;
            }
            CallsiteSignature  signature = new CallsiteSignature(targetType, arguments, flags);
            CallsiteCacheEntry key       = new CallsiteCacheEntry(methodName, signature);

            lock (Adapter.callsiteCache)
            {
                if (Adapter.callsiteCache.ContainsKey(key))
                {
                    return;
                }
                if (Adapter.callsiteCache.Count > 2048)
                {
                    Adapter.callsiteCache.Clear();
                }
                Adapter.callsiteCache[key] = mi;
            }
        }
コード例 #2
0
 internal CallsiteSignature(Type targetType, object[] arguments, CallsiteCacheEntryFlags flags)
     : this(targetType, new Type[arguments.Length], flags)
 {
     for (int index = 0; index < arguments.Length; ++index)
     {
         this.argumentTypes[index] = Adapter.EffectiveArgumentType(arguments[index]);
     }
 }
コード例 #3
0
 internal CallsiteSignature(
     Type targetType,
     Type[] argumentTypes,
     CallsiteCacheEntryFlags flags)
 {
     this.targetType    = targetType;
     this.argumentTypes = argumentTypes;
     this.flags         = flags;
 }
コード例 #4
0
 internal CallsiteSignature(Type targetType, PSMethodInvocationConstraints invocationConstraints, object[] arguments, CallsiteCacheEntryFlags flags)
 {
     this.targetType            = targetType;
     this.invocationConstraints = invocationConstraints;
     this.flags                  = flags;
     this.argumentTypes          = new Type[arguments.Length];
     this.effectiveArgumentTypes = new Type[arguments.Length];
     for (int i = 0; i < arguments.Length; i++)
     {
         this.argumentTypes[i]          = (arguments[i] == null) ? typeof(LanguagePrimitives.Null) : arguments[i].GetType();
         this.effectiveArgumentTypes[i] = Adapter.EffectiveArgumentType(arguments[i]);
     }
 }
コード例 #5
0
        internal static MethodInformation FindCachedMethod(
            Type targetType,
            string methodName,
            object[] arguments,
            CallsiteCacheEntryFlags flags)
        {
            if (targetType == typeof(PSObject) || targetType == typeof(PSCustomObject))
            {
                return((MethodInformation)null);
            }
            CallsiteSignature  signature         = new CallsiteSignature(targetType, arguments, flags);
            CallsiteCacheEntry key               = new CallsiteCacheEntry(methodName, signature);
            MethodInformation  methodInformation = (MethodInformation)null;

            lock (Adapter.callsiteCache)
                Adapter.callsiteCache.TryGetValue(key, out methodInformation);
            return(methodInformation);
        }
コード例 #6
0
        internal static object CallMethod(IScriptExtent errorPosition, object target, string methodName, PSMethodInvocationConstraints invocationConstraints, object[] paramArray, bool callStatic, object valueToSet)
        {
            PSMethodInfo      staticCLRMember   = null;
            MethodInformation methodInformation = null;
            object            obj2 = null;
            PSObject          obj3 = null;
            Type   type;
            object obj4;

            if (LanguagePrimitives.IsNull(target))
            {
                throw InterpreterError.NewInterpreterException(methodName, typeof(RuntimeException), errorPosition, "InvokeMethodOnNull", ParserStrings.InvokeMethodOnNull, new object[0]);
            }
            obj2 = PSObject.Base(target);
            obj3 = PSObject.AsPSObject(target);
            CallsiteCacheEntryFlags none = CallsiteCacheEntryFlags.None;

            if (callStatic)
            {
                none |= CallsiteCacheEntryFlags.Static;
                type  = (Type)obj2;
            }
            else
            {
                type = obj2.GetType();
            }
            if (valueToSet != AutomationNull.Value)
            {
                none |= CallsiteCacheEntryFlags.ParameterizedSetter;
            }
            if (!obj3.isDeserialized)
            {
                methodInformation = Adapter.FindCachedMethod(type, methodName, invocationConstraints, paramArray, none);
            }
            if (methodInformation == null)
            {
                if (callStatic)
                {
                    staticCLRMember = PSObject.GetStaticCLRMember(target, methodName) as PSMethod;
                }
                else
                {
                    staticCLRMember = obj3.Members[methodName] as PSMethodInfo;
                }
                if (staticCLRMember == null)
                {
                    string fullName = null;
                    if (callStatic)
                    {
                        fullName = type.FullName;
                    }
                    else
                    {
                        fullName = GetTypeFullName(target);
                    }
                    if (valueToSet == AutomationNull.Value)
                    {
                        throw InterpreterError.NewInterpreterException(methodName, typeof(RuntimeException), errorPosition, "MethodNotFound", ParserStrings.MethodNotFound, new object[] { fullName, methodName });
                    }
                    throw InterpreterError.NewInterpreterException(methodName, typeof(RuntimeException), errorPosition, "ParameterizedPropertyAssignmentFailed", ParserStrings.ParameterizedPropertyAssignmentFailed, new object[] { fullName, methodName });
                }
            }
            try
            {
                if (methodInformation != null)
                {
                    object[] objArray;
                    PSObject.memberResolution.WriteLine("cache hit, Calling Method: {0}", new object[] { methodInformation.methodDefinition });
                    if (valueToSet != AutomationNull.Value)
                    {
                        DotNetAdapter.ParameterizedPropertyInvokeSet(methodName, obj2, valueToSet, new MethodInformation[] { methodInformation }, paramArray, false);
                        return(valueToSet);
                    }
                    MethodInformation[] methods = new MethodInformation[] { methodInformation };
                    Adapter.GetBestMethodAndArguments(methodName, methods, paramArray, out objArray);
                    return(DotNetAdapter.AuxiliaryMethodInvoke(obj2, objArray, methodInformation, paramArray));
                }
                if (valueToSet != AutomationNull.Value)
                {
                    PSParameterizedProperty property = staticCLRMember as PSParameterizedProperty;
                    if (property == null)
                    {
                        throw InterpreterError.NewInterpreterException(methodName, typeof(RuntimeException), errorPosition, "ParameterizedPropertyAssignmentFailed", ParserStrings.ParameterizedPropertyAssignmentFailed, new object[] { GetTypeFullName(target), methodName });
                    }
                    property.InvokeSet(valueToSet, paramArray);
                    return(valueToSet);
                }
                PSMethod method = staticCLRMember as PSMethod;
                if (method != null)
                {
                    return(method.Invoke(invocationConstraints, paramArray));
                }
                obj4 = staticCLRMember.Invoke(paramArray);
            }
            catch (MethodInvocationException exception)
            {
                if (exception.ErrorRecord.InvocationInfo == null)
                {
                    exception.ErrorRecord.SetInvocationInfo(new InvocationInfo(null, errorPosition));
                }
                throw;
            }
            catch (RuntimeException exception2)
            {
                if (exception2.ErrorRecord.InvocationInfo == null)
                {
                    exception2.ErrorRecord.SetInvocationInfo(new InvocationInfo(null, errorPosition));
                }
                throw;
            }
            catch (FlowControlException)
            {
                throw;
            }
            catch (ScriptCallDepthException)
            {
                throw;
            }
            catch (Exception exception3)
            {
                CommandProcessorBase.CheckForSevereException(exception3);
                throw InterpreterError.NewInterpreterExceptionByMessage(typeof(RuntimeException), errorPosition, exception3.Message, "MethodInvocationException", exception3);
            }
            return(obj4);
        }