bdd8e8d9bf8ed9d8f54f1f4d00ebe2077b902269
[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 #if NET_4_0
92                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
93                 static extern bool SufficientExecutionStack ();
94
95                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
96                 public static void EnsureSufficientExecutionStack ()
97                 {
98                         if (SufficientExecutionStack ())
99                                 return;
100                         throw new InsufficientExecutionStackException ();
101                 }
102 #endif
103
104                 [MonoTODO("Currently a no-op")]
105                 public static void ExecuteCodeWithGuaranteedCleanup (TryCode code, CleanupCode backoutCode, Object userData)
106                 {
107                 }
108
109                 [MonoTODO("Currently a no-op")]
110                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
111                 public static void PrepareConstrainedRegions ()
112                 {
113                 }
114
115                 [MonoTODO("Currently a no-op")]
116                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
117                 public static void PrepareConstrainedRegionsNoOP ()
118                 {
119                 }
120
121                 [MonoTODO("Currently a no-op")]
122                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
123                 public static void ProbeForSufficientStack()
124                 {
125                 }
126
127                 [MonoTODO("Currently a no-op")]
128                 public static void PrepareDelegate (Delegate d)
129                 {
130                         if (d == null)
131                                 throw new ArgumentNullException ("d");
132                 }
133
134                 [MonoTODO("Currently a no-op")]
135                 public static void PrepareMethod (RuntimeMethodHandle method)
136                 {
137                 }
138
139                 [MonoTODO("Currently a no-op")]
140                 public static void PrepareMethod (RuntimeMethodHandle method, RuntimeTypeHandle[] instantiation)
141                 {
142                 }
143
144                 public static void RunModuleConstructor (ModuleHandle module)
145                 {
146                         if (module == ModuleHandle.EmptyHandle)
147                                 throw new ArgumentException ("Handle is not initialized.", "module");
148
149                         RunModuleConstructor (module.Value);
150                 }
151
152                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
153                 static extern void RunModuleConstructor (IntPtr module);
154         }
155 }