Merge pull request #901 from Blewzman/FixAggregateExceptionGetBaseException
[mono.git] / mcs / class / dlr / Runtime / Microsoft.Dynamic / Interpreter / Instructions / DynamicInstructionN.cs
1 /* ****************************************************************************
2  *
3  * Copyright (c) Microsoft Corporation. 
4  *
5  * This source code is subject to terms and conditions of the Apache License, Version 2.0. A 
6  * copy of the license can be found in the License.html file at the root of this distribution. If 
7  * you cannot locate the  Apache License, Version 2.0, please send an email to 
8  * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
9  * by the terms of the Apache License, Version 2.0.
10  *
11  * You must not remove this notice, or any other, from this software.
12  *
13  *
14  * ***************************************************************************/
15
16
17 using System;
18 using System.Linq;
19 using System.Collections.Generic;
20 using System.Runtime.CompilerServices;
21 using System.Reflection;
22
23 using Microsoft.Scripting.Utils;
24 using System.Security;
25
26 namespace Microsoft.Scripting.Interpreter {
27     internal sealed partial class DynamicInstructionN : Instruction {
28         private readonly CallInstruction _targetInvocationInstruction;
29         private readonly object _targetDelegate;
30         private readonly CallSite _site;
31         private readonly int _argumentCount;
32         private readonly bool _isVoid;
33
34         public DynamicInstructionN(Type delegateType, CallSite site) {
35             var methodInfo = delegateType.GetMethod("Invoke");
36             var parameters = methodInfo.GetParameters();
37
38             // <Delegate>.Invoke is ok to target by a delegate in partial trust (SecurityException is not thrown):
39             _targetInvocationInstruction = CallInstruction.Create(methodInfo, parameters);
40             _site = site;
41             _argumentCount = parameters.Length - 1;
42             _targetDelegate = site.GetType().GetInheritedFields("Target").First().GetValue(site);
43         }
44
45         public DynamicInstructionN(Type delegateType, CallSite site, bool isVoid)
46             : this(delegateType, site) {
47             _isVoid = isVoid;
48         }
49
50         public override int ProducedStack { get { return _isVoid ? 0 : 1; } }
51         public override int ConsumedStack { get { return _argumentCount; } }
52
53         public override int Run(InterpretedFrame frame) {
54             int first = frame.StackIndex - _argumentCount;
55             object[] args = new object[1 + _argumentCount];
56             args[0] = _site;
57             for (int i = 0; i < _argumentCount; i++) {
58                 args[1 + i] = frame.Data[first + i];
59             }
60
61             object ret = _targetInvocationInstruction.InvokeInstance(_targetDelegate, args);
62             if (_isVoid) {
63                 frame.StackIndex = first;
64             } else {
65                 frame.Data[first] = ret;
66                 frame.StackIndex = first + 1;
67             }
68
69             return 1;
70         }
71
72         public override string ToString() {
73             return "DynamicInstructionN(" + _site + ")";
74         }
75     }
76 }