5 // Cesar Lopez Nataren (cesar@ciencias.unam.mx)
7 // (C) 2003, 2004 Cesar Lopez Nataren
8 // (C) 2005, Novell Inc, (http://novell.com)
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:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
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.
34 using System.Diagnostics;
35 using System.Reflection.Emit;
37 namespace Microsoft.JScript {
39 public class Equality : BinaryOp {
41 internal Equality (AST parent, AST left, AST right, JSToken op, Location location)
42 : base (parent, left, right, op, location)
46 public Equality (int i)
47 : base (null, null, null, (JSToken) i, null)
51 [DebuggerStepThroughAttribute]
52 [DebuggerHiddenAttribute]
53 public bool EvaluateEquality (object v1, object v2)
55 IConvertible ic1 = v1 as IConvertible;
56 IConvertible ic2 = v2 as IConvertible;
58 TypeCode tc1 = Convert.GetTypeCode (v1, ic1);
59 TypeCode tc2 = Convert.GetTypeCode (v2, ic2);
61 bool both_numbers = Convert.IsNumberTypeCode (tc1) && Convert.IsNumberTypeCode (tc2);
62 if ((tc1 == tc2) || both_numbers) {
68 case TypeCode.Boolean:
69 return ic1.ToBoolean (null) == ic2.ToBoolean (null);
72 return ic1.ToString (null) == ic2.ToString (null);
75 if (v1 is ScriptFunction && v2 is ScriptFunction)
76 return v1 == v2 || v1.Equals (v2);
83 if (Convert.IsFloatTypeCode (tc1))
84 num1 = ic1.ToDouble (null);
86 num1 = (double) ic1.ToInt64 (null);
89 if (Convert.IsFloatTypeCode (tc2))
90 num2 = ic2.ToDouble (null);
92 num2 = (double) ic2.ToInt64 (null);
103 case TypeCode.DBNull:
104 if (tc2 == TypeCode.Empty)
108 case TypeCode.String:
109 if (Convert.IsNumberTypeCode (tc2))
110 return EvaluateEquality (Convert.ToNumber (v1), v2);
113 case TypeCode.Boolean:
114 return EvaluateEquality (Convert.ToNumber (v1), v2);
116 case TypeCode.Object:
117 if (tc2 == TypeCode.String || Convert.IsNumberTypeCode (tc2))
118 return EvaluateEquality (Convert.ToPrimitive (v1, null), v2);
129 ic1 = v1 as IConvertible;
130 ic2 = v2 as IConvertible;
132 tc1 = Convert.GetTypeCode (v1, ic1);
133 tc2 = Convert.GetTypeCode (v2, ic2);
141 public static bool JScriptEquals (object v1, object v2)
143 throw new NotImplementedException ();
146 internal override bool Resolve (IdentificationTable context)
150 r &= left.Resolve (context);
152 r &= right.Resolve (context);
156 internal override bool Resolve (IdentificationTable context, bool no_effect)
158 this.no_effect = no_effect;
159 return Resolve (context);
162 internal override void Emit (EmitContext ec)
164 ILGenerator ig = ec.ig;
165 LocalBuilder local_builder;
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);
181 CodeGenerator.EmitBox (ig, left);
185 CodeGenerator.EmitBox (ig, right);
188 if (op == JSToken.Equal || op == JSToken.NotEqual) {
189 ig.Emit (OpCodes.Call, typeof (Equality).GetMethod ("EvaluateEquality"));
192 Label t_lbl = ig.DefineLabel ();
193 Label f_lbl = ig.DefineLabel ();
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);
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);