Merge pull request #4155 from BrzVlad/fix-tls-lmf-addr
[mono-project.git] / mcs / class / dlr / Runtime / Microsoft.Dynamic / Interpreter / Instructions / DynamicInstructionN.cs
blob705766951329cc2fadb2d0ada9ffb071b64bb19e
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 * ***************************************************************************/
17 using System;
18 using System.Linq;
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);
40 _site = site;
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) {
47 _isVoid = isVoid;
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];
56 args[0] = _site;
57 for (int i = 0; i < _argumentCount; i++) {
58 args[1 + i] = frame.Data[first + i];
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;
69 return 1;
72 public override string ToString() {
73 return "DynamicInstructionN(" + _site + ")";