2010-01-20 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mcs / class / corlib / System.Runtime.CompilerServices / RuntimeHelpers.cs
1 // System.Runtime.CompilerServices.RuntimeHelpers
2 //
3 // Sean MacIsaac (macisaac@ximian.com)
4 // Paolo Molaro (lupus@ximian.com)
5 //
6 // (C) Ximian, Inc. 2001
7
8 //
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.Runtime.ConstrainedExecution;
32 using System.Reflection;
33
34 namespace System.Runtime.CompilerServices
35 {
36         public static class RuntimeHelpers
37         {
38                 public delegate void TryCode (Object userData);
39
40                 public delegate void CleanupCode (Object userData, bool exceptionThrown);
41
42                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
43                 static extern void InitializeArray (Array array, IntPtr fldHandle);
44
45                 public static void InitializeArray (Array array, RuntimeFieldHandle fldHandle)
46                 {
47                         if ((array == null) || (fldHandle.Value == IntPtr.Zero))
48                                 throw new ArgumentNullException ();
49
50                         InitializeArray (array, fldHandle.Value);
51                 }
52
53                 public static extern int OffsetToStringData {
54                         [MethodImpl (MethodImplOptions.InternalCall)]
55                         get;
56                 }
57
58                 public static int GetHashCode (object o) {
59                         return Object.InternalGetHashCode (o);
60                 }
61
62                 public static new bool Equals (object o1, object o2) {
63                         // LAMESPEC: According to MSDN, this is equivalent to 
64                         // Object::Equals (). But the MS version of Object::Equals()
65                         // includes the functionality of ValueType::Equals(), while
66                         // our version does not.
67                         if (o1 == o2)
68                                 return true;
69                         if ((o1 == null) || (o2 == null))
70                                 return false;
71                         if (o1 is ValueType)
72                                 return ValueType.DefaultEquals (o1, o2);
73                         else
74                                 return Object.Equals (o1, o2);
75                 }
76
77                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
78                 public static extern object GetObjectValue (object obj);
79
80                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
81                 static extern void RunClassConstructor (IntPtr type);
82
83                 public static void RunClassConstructor (RuntimeTypeHandle type)
84                 {
85                         if (type.Value == IntPtr.Zero)
86                                 throw new ArgumentException ("Handle is not initialized.", "type");
87
88                         RunClassConstructor (type.Value);
89                 }
90
91                 [MonoTODO("Currently a no-op")]
92                 public static void ExecuteCodeWithGuaranteedCleanup (TryCode code, CleanupCode backoutCode, Object userData)
93                 {
94                 }
95
96                 [MonoTODO("Currently a no-op")]
97                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
98                 public static void PrepareConstrainedRegions ()
99                 {
100                 }
101
102                 [MonoTODO("Currently a no-op")]
103                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
104                 public static void PrepareConstrainedRegionsNoOP ()
105                 {
106                 }
107
108                 [MonoTODO("Currently a no-op")]
109                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
110                 public static void ProbeForSufficientStack()
111                 {
112                 }
113
114                 [MonoTODO("Currently a no-op")]
115                 public static void PrepareDelegate (Delegate d)
116                 {
117                         if (d == null)
118                                 throw new ArgumentNullException ("d");
119                 }
120
121                 [MonoTODO("Currently a no-op")]
122                 public static void PrepareMethod (RuntimeMethodHandle method)
123                 {
124                 }
125
126                 [MonoTODO("Currently a no-op")]
127                 public static void PrepareMethod (RuntimeMethodHandle method, RuntimeTypeHandle[] instantiation)
128                 {
129                 }
130
131                 public static void RunModuleConstructor (ModuleHandle module)
132                 {
133                         if (module == ModuleHandle.EmptyHandle)
134                                 throw new ArgumentException ("Handle is not initialized.", "module");
135
136                         RunModuleConstructor (module.Value);
137                 }
138
139                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
140                 public static extern void RunModuleConstructor (IntPtr module);
141         }
142 }