Merge pull request #819 from brendanzagaeski/patch-1
[mono.git] / mcs / class / dlr / Runtime / Microsoft.Dynamic / Interpreter / Instructions / FieldOperations.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 using System;
17 using System.Collections.Generic;
18 using System.Diagnostics;
19 using System.Reflection;
20 using System.Runtime.CompilerServices;
21 using Microsoft.Scripting.Runtime;
22 using Microsoft.Scripting.Utils;
23
24 namespace Microsoft.Scripting.Interpreter {
25     internal sealed class LoadStaticFieldInstruction : Instruction {
26         private readonly FieldInfo _field;
27
28         public LoadStaticFieldInstruction(FieldInfo field) {
29             Debug.Assert(field.IsStatic);
30             _field = field;
31         }
32
33         public override int ProducedStack { get { return 1; } }
34
35         public override int Run(InterpretedFrame frame) {
36             frame.Push(_field.GetValue(null));
37             return +1;
38         }
39     }
40
41     internal sealed class LoadFieldInstruction : Instruction {
42         private readonly FieldInfo _field;
43
44         public LoadFieldInstruction(FieldInfo field) {
45             Assert.NotNull(field);
46             _field = field;
47         }
48
49         public override int ConsumedStack { get { return 1; } }
50         public override int ProducedStack { get { return 1; } }
51
52         public override int Run(InterpretedFrame frame) {
53             frame.Push(_field.GetValue(frame.Pop()));
54             return +1;
55         }
56     }
57
58     internal sealed class StoreFieldInstruction : Instruction {
59         private readonly FieldInfo _field;
60
61         public StoreFieldInstruction(FieldInfo field) {
62             Assert.NotNull(field);
63             _field = field;
64         }
65
66         public override int ConsumedStack { get { return 2; } }
67         public override int ProducedStack { get { return 0; } }
68
69         public override int Run(InterpretedFrame frame) {
70             object value = frame.Pop();
71             object self = frame.Pop();
72             _field.SetValue(self, value);
73             return +1;
74         }
75     }
76
77     internal sealed class StoreStaticFieldInstruction : Instruction {
78         private readonly FieldInfo _field;
79
80         public StoreStaticFieldInstruction(FieldInfo field) {
81             Assert.NotNull(field);
82             _field = field;
83         }
84
85         public override int ConsumedStack { get { return 1; } }
86         public override int ProducedStack { get { return 0; } }
87
88         public override int Run(InterpretedFrame frame) {
89             object value = frame.Pop();
90             _field.SetValue(null, value);
91             return +1;
92         }
93     }
94 }