9ae600ca5884f3409a1f25192e6f350101595ca1
[mono.git] / mcs / class / corlib / System.Runtime.CompilerServices / ContractHelper.cs
1 //
2 // ContractHelper.cs
3 //
4 // Authors:
5 //    Chris Bacon (chrisbacon76@gmail.com)
6 //
7 // Copyright 2010 Novell (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 #if NET_4_5
30
31 using System;
32 using System.Text;
33 using System.Reflection;
34 using System.Runtime.ConstrainedExecution;
35 using System.Diagnostics.Contracts;
36 using System.Diagnostics;
37
38 namespace System.Runtime.CompilerServices
39 {
40         public static class ContractHelper
41         {
42 #if MOONLIGHT
43                 const string SystemWindowsBrowser = ", System.Windows.Browser, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e";
44                 const string HtmlPage = "System.Windows.Browser.HtmlPage" + SystemWindowsBrowser;
45                 const string HtmlWindow = "System.Windows.Browser.HtmlWindow" + SystemWindowsBrowser;
46                 static MethodInfo alert;
47                 static object window;
48
49                 static ContractHelper ()
50                 {
51                         Type htmlpage = Type.GetType (HtmlPage);
52                         MethodInfo get_window = htmlpage.GetMethod ("get_Window", BindingFlags.Static | BindingFlags.Public);
53                         window = get_window.Invoke (null, null);
54                         Type htmlwindow = Type.GetType (HtmlWindow);
55                         alert = htmlwindow.GetMethod ("Alert", BindingFlags.Instance | BindingFlags.Public);
56                 }
57 #endif
58
59                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
60                 [DebuggerNonUserCode]
61                 public static string RaiseContractFailedEvent (ContractFailureKind failureKind, string userMessage, string conditionText, Exception innerException)
62                 {
63
64                         StringBuilder msg = new StringBuilder (60);
65                         switch (failureKind) {
66                         case ContractFailureKind.Assert:
67                                 msg.Append ("Assertion failed");
68                                 break;
69                         case ContractFailureKind.Assume:
70                                 msg.Append ("Assumption failed");
71                                 break;
72                         case ContractFailureKind.Invariant:
73                                 msg.Append ("Invariant failed");
74                                 break;
75                         case ContractFailureKind.Postcondition:
76                                 msg.Append ("Postcondition failed");
77                                 break;
78                         case ContractFailureKind.PostconditionOnException:
79                                 msg.Append ("Postcondition failed after throwing an exception");
80                                 break;
81                         case ContractFailureKind.Precondition:
82                                 msg.Append ("Precondition failed");
83                                 break;
84                         default:
85                                 throw new NotSupportedException ("Not supported: " + failureKind);
86                         }
87                         if (conditionText != null) {
88                                 msg.Append (": ");
89                                 msg.Append (conditionText);
90                         } else {
91                                 msg.Append ('.');
92                         }
93                         if (userMessage != null) {
94                                 msg.Append ("  ");
95                                 msg.Append (userMessage);
96                         }
97                         string msgString = msg.ToString ();
98
99                         Exception handlerException = null;
100                         bool unwind = false, handled = false;
101
102                         var contractFailed = Contract.InternalContractFailedEvent;
103                         if (contractFailed != null) {
104                                 // Execute all event handlers
105                                 var handlers = contractFailed.GetInvocationList ();
106                                 var e = new ContractFailedEventArgs (failureKind, msgString, conditionText, innerException);
107                                 foreach (var handler in handlers) {
108                                         try {
109                                                 handler.DynamicInvoke (null, e);
110                                         } catch (Exception ex) {
111                                                 e.SetUnwind ();
112                                                 // If multiple handlers throw an exception then the specification states that it
113                                                 // is undetermined which one becomes the InnerException.
114                                                 handlerException = ex.InnerException;
115                                         }
116                                 }
117                                 unwind = e.Unwind;
118                                 handled = e.Handled;
119                         }
120
121                         if (unwind) {
122                                 Exception ex = innerException ?? handlerException;
123                                 throw new ContractException (msgString, failureKind, conditionText, userMessage, ex);
124                         }
125
126                         return handled ? null : msgString;
127                 }
128
129                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
130                 [DebuggerNonUserCode]
131                 public static void TriggerFailure (ContractFailureKind kind, string displayMessage, string userMessage, string conditionText, Exception innerException)
132                 {
133                         StringBuilder msg = new StringBuilder (50);
134
135                         if (conditionText != null) {
136                                 msg.Append ("Expression: ");
137                                 msg.AppendLine (conditionText);
138                         }
139                         msg.Append ("Description: ");
140                         if (displayMessage != null) {
141                                 msg.Append (displayMessage);
142                         }
143 #if MOONLIGHT
144                         // Silverlight shows a dialog that let you Abort (kill process/browser), Retry or Ignore
145                         // Moonlight will simply warn and ignore (at least until FailFast is implemented)
146                         // using reflection into System.Windows.Browser to popup an browser alert
147                         alert.Invoke (window, new object [] { msg.ToString () });
148 #else
149                         if (Environment.UserInteractive) {
150                                 // FIXME: This should trigger an assertion.
151                                 // But code will never get here at the moment, as Environment.UserInteractive currently
152                                 // always returns false.
153                                 throw new ContractShouldAssertException (msg.ToString ());
154                         } else {
155                                 // Note that FailFast() currently throws a NotImplementedException()
156                                 Environment.FailFast(msg.ToString()/*, new ExecutionEngineException()*/);
157                         }
158 #endif
159                 }
160
161         }
162
163 }
164
165 #endif