2005-08-19 Cesar Lopez Nataren <cnataren@novell.com>
[mono.git] / mcs / class / Microsoft.JScript / Microsoft.JScript / Equality.cs
1 //
2 // Equality.cs:
3 //
4 // Author:
5 //      Cesar Lopez Nataren (cesar@ciencias.unam.mx)
6 //
7 // (C) 2003, 2004 Cesar Lopez Nataren
8 // (C) 2005, Novell Inc, (http://novell.com)
9 //
10
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System;
33 using System.Text;
34 using System.Diagnostics;
35 using System.Reflection.Emit;
36
37 namespace Microsoft.JScript {
38
39         public class Equality : BinaryOp {
40
41                 internal Equality (AST parent, AST left, AST right, JSToken op, Location location)
42                         : base (parent, left, right, op, location)
43                 {
44                 }
45
46                 public Equality (int i)
47                         : base (null, null, null, (JSToken) i, null)
48                 {
49                 }
50
51                 [DebuggerStepThroughAttribute]
52                 [DebuggerHiddenAttribute]
53                 public bool EvaluateEquality (object v1, object v2)
54                 {
55                         IConvertible ic1 = v1 as IConvertible;
56                         IConvertible ic2 = v2 as IConvertible;
57
58                         TypeCode tc1 = Convert.GetTypeCode (v1, ic1);
59                         TypeCode tc2 = Convert.GetTypeCode (v2, ic2);
60
61                         bool both_numbers = Convert.IsNumberTypeCode (tc1) && Convert.IsNumberTypeCode (tc2);
62                         if ((tc1 == tc2) || both_numbers) {
63                                 switch (tc1) {
64                                 case TypeCode.DBNull:
65                                 case TypeCode.Empty:
66                                         return true;
67
68                                 case TypeCode.Boolean:
69                                         return ic1.ToBoolean (null) == ic2.ToBoolean (null);
70
71                                 case TypeCode.String:
72                                         return ic1.ToString (null) == ic2.ToString (null);
73
74                                 case TypeCode.Object:
75                                         if (v1 is ScriptFunction && v2 is ScriptFunction)
76                                                 return v1 == v2 || v1.Equals (v2);
77                                         else
78                                                 return v1 == v2;
79
80                                 default:
81                                         if (both_numbers) {
82                                                 double num1;
83                                                 if (Convert.IsFloatTypeCode (tc1))
84                                                         num1 = ic1.ToDouble (null);
85                                                 else
86                                                         num1 = (double) ic1.ToInt64 (null);
87
88                                                 double num2;
89                                                 if (Convert.IsFloatTypeCode (tc2))
90                                                         num2 = ic2.ToDouble (null);
91                                                 else
92                                                         num2 = (double) ic2.ToInt64 (null);
93
94                                                 return num1 == num2;
95                                         } else
96                                                 return false;
97                                 }
98                         } else {
99                                 bool swapped = false;
100
101                         redo:
102                                 switch (tc1) {
103                                 case TypeCode.DBNull:
104                                         if (tc2 == TypeCode.Empty)
105                                                 return true;
106                                         break;
107
108                                 case TypeCode.String:
109                                         if (Convert.IsNumberTypeCode (tc2))
110                                                 return EvaluateEquality (Convert.ToNumber (v1), v2);
111                                         break;
112
113                                 case TypeCode.Boolean:
114                                         return EvaluateEquality (Convert.ToNumber (v1), v2);
115
116                                 case TypeCode.Object:
117                                         if (tc2 == TypeCode.String || Convert.IsNumberTypeCode (tc2))
118                                                 return EvaluateEquality (Convert.ToPrimitive (v1, null), v2);
119                                         break;
120                                 }
121
122                                 if (!swapped) {
123                                         swapped = true;
124
125                                         object vt = v1;
126                                         v1 = v2;
127                                         v2 = vt;
128
129                                         ic1 = v1 as IConvertible;
130                                         ic2 = v2 as IConvertible;
131
132                                         tc1 = Convert.GetTypeCode (v1, ic1);
133                                         tc2 = Convert.GetTypeCode (v2, ic2);
134
135                                         goto redo;
136                                 } else
137                                         return false;
138                         }
139                 }
140
141                 public static bool JScriptEquals (object v1, object v2)
142                 {
143                         throw new NotImplementedException ();
144                 }
145
146                 internal override bool Resolve (IdentificationTable context)
147                 {
148                         bool r = true;
149                         if (left != null)
150                                 r &= left.Resolve (context);
151                         if (right != null)
152                                 r &= right.Resolve (context);
153                         return r;
154                 }
155
156                 internal override bool Resolve (IdentificationTable context, bool no_effect)
157                 {
158                         this.no_effect = no_effect;
159                         return Resolve (context);
160                 }
161
162                 internal override void Emit (EmitContext ec)
163                 {
164                         ILGenerator ig = ec.ig;
165                         LocalBuilder local_builder;
166
167                         if (op != JSToken.None) {
168                                 Type t = typeof (Equality);
169                                 local_builder = ig.DeclareLocal (t);
170                                 if (op == JSToken.Equal)
171                                         ig.Emit (OpCodes.Ldc_I4_S, (byte) 53);
172                                 else if (op == JSToken.NotEqual)
173                                         ig.Emit (OpCodes.Ldc_I4_S, (byte) 54);
174                                 ig.Emit (OpCodes.Newobj, t.GetConstructor (new Type [] { typeof (int) }));
175                                 ig.Emit (OpCodes.Stloc, local_builder);
176                                 ig.Emit (OpCodes.Ldloc, local_builder);
177                         }
178
179                         if (left != null) {
180                                 left.Emit (ec);
181                                 CodeGenerator.EmitBox (ig, left);
182                         }
183                         if (right != null) {
184                                 right.Emit (ec);
185                                 CodeGenerator.EmitBox (ig, right);
186                         }
187
188                         if (op == JSToken.Equal || op == JSToken.NotEqual) {
189                                 ig.Emit (OpCodes.Call, typeof (Equality).GetMethod ("EvaluateEquality"));
190
191                                 if (no_effect) {
192                                         Label t_lbl = ig.DefineLabel ();
193                                         Label f_lbl = ig.DefineLabel ();
194
195                                         if (op == JSToken.Equal)
196                                                 ig.Emit (OpCodes.Brtrue_S, t_lbl);
197                                         else if (op == JSToken.NotEqual)
198                                                 ig.Emit (OpCodes.Brfalse_S, t_lbl);
199
200                                         ig.Emit (OpCodes.Ldc_I4_0);
201                                         ig.Emit (OpCodes.Br_S, f_lbl);
202                                         ig.MarkLabel (t_lbl);
203                                         ig.Emit (OpCodes.Ldc_I4_1);
204                                         ig.MarkLabel (f_lbl);
205                                         ig.Emit (OpCodes.Pop);
206                                 }
207                         }
208                 }
209         }
210 }