1 /* ****************************************************************************
3 * Copyright (c) Microsoft Corporation.
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.
11 * You must not remove this notice, or any other, from this software.
14 * ***************************************************************************/
19 using System
.Collections
.Generic
;
20 using System
.Runtime
.CompilerServices
;
21 using System
.Reflection
;
23 using Microsoft
.Scripting
.Utils
;
24 using System
.Security
;
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
;
34 public DynamicInstructionN(Type delegateType
, CallSite site
) {
35 var methodInfo
= delegateType
.GetMethod("Invoke");
36 var parameters
= methodInfo
.GetParameters();
38 // <Delegate>.Invoke is ok to target by a delegate in partial trust (SecurityException is not thrown):
39 _targetInvocationInstruction
= CallInstruction
.Create(methodInfo
, parameters
);
41 _argumentCount
= parameters
.Length
- 1;
42 _targetDelegate
= site
.GetType().GetInheritedFields("Target").First().GetValue(site
);
45 public DynamicInstructionN(Type delegateType
, CallSite site
, bool isVoid
)
46 : this(delegateType
, site
) {
50 public override int ProducedStack { get { return _isVoid ? 0 : 1; }
}
51 public override int ConsumedStack { get { return _argumentCount; }
}
53 public override int Run(InterpretedFrame frame
) {
54 int first
= frame
.StackIndex
- _argumentCount
;
55 object[] args
= new object[1 + _argumentCount
];
57 for (int i
= 0; i
< _argumentCount
; i
++) {
58 args
[1 + i
] = frame
.Data
[first
+ i
];
61 object ret
= _targetInvocationInstruction
.InvokeInstance(_targetDelegate
, args
);
63 frame
.StackIndex
= first
;
65 frame
.Data
[first
] = ret
;
66 frame
.StackIndex
= first
+ 1;
72 public override string ToString() {
73 return "DynamicInstructionN(" + _site
+ ")";