Esempio n. 1
0
        private void ProcessClrCall(ClrCallContext currentFrame)
        {
            var mb = currentFrame.Source.Rtimpl;

            var formal = mb.GetParameters().Select(pi => pi.ParameterType);
            var head = mb.IsStatic || mb.IsConstructor ? 0 : 1;
            var body = formal.Skip(head).Reverse().Skip(1).Reverse();
            var tailType = mb.IsVarargs() ? formal.Last().GetElementType() : formal.Last();
            var tailIterations = currentFrame.Args.Length - body.Count();
            var tail = Enumerable.Repeat(tailType, tailIterations);
            var sigArgs = body.Concat(tail).ToArray();

            var marshalledThis = mb.IsConstructor || mb.IsStatic ? null :
                mb.DeclaringType == currentFrame.This.GetType() ? currentFrame.This : VM.Marshaller.Marshal(currentFrame.This);
            var marshalledArgs = currentFrame.Args.Zip(sigArgs,
                (elf, pi) => typeof(IElfObject).IsAssignableFrom(pi) ? elf : VM.Marshaller.Marshal(elf)).ToArray();

            if (mb.IsVarargs())
            {
                var packedTail = Array.CreateInstance(tailType, tailIterations);
                marshalledArgs.Skip(body.Count()).ForEach(packedTail.SetValue);
                marshalledArgs = marshalledArgs.Take(body.Count()).Concat(packedTail.AsArray()).ToArray();
            }

            object retval;
            try
            {
                retval = mb.Invoke(marshalledThis, marshalledArgs);
            }
            catch(Exception ex)
            {
                if (ex is TargetInvocationException)
                {
                    throw;
                }
                else
                {
                    throw new ErroneousScriptRuntimeException(ElfExceptionType.OperandsDontSuitMethod, VM, ex);
                }
            }

            IElfObject retvalMarshalled;
            if (!mb.IsConstructor)
            {
                var mi = (MethodInfo)mb;
                retvalMarshalled = mi.ReturnType == typeof(void) ?
                    new ElfVoid() : VM.Marshaller.Unmarshal(retval);
            }
            else
            {
                retvalMarshalled = (IElfObject)retval;
            }

            Ctx.CallStack.Peek().Stack.Push(retvalMarshalled);
            Ctx.PendingClrCall = null;
        }
 public ArgsDontSuitTheFunctionException(IBranch offendingBranch, ClrCallContext callContext, Exception innerException)
     : base(offendingBranch, String.Empty, innerException) 
 {
     CallContext = callContext;
 }
 public ArgsDontSuitTheFunctionException(ClrCallContext callContext, Exception innerException)
     : base(String.Empty, innerException) 
 {
     CallContext = callContext;
 }