]> git.saurik.com Git - apple/javascriptcore.git/blame - kjs/operations.cpp
JavaScriptCore-466.1.tar.gz
[apple/javascriptcore.git] / kjs / operations.cpp
CommitLineData
b37bf2e1
A
1// -*- c-basic-offset: 2 -*-
2/*
3 * This file is part of the KDE libraries
4 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 *
21 */
22
23#include "config.h"
24#include "operations.h"
25
26#include "internal.h"
27#include "object.h"
28#include <math.h>
29#include <stdio.h>
30#include <wtf/MathExtras.h>
31
32#if HAVE(FLOAT_H)
33#include <float.h>
34#endif
35
36namespace KJS {
37
38// ECMA 11.9.3
39bool equal(ExecState *exec, JSValue *v1, JSValue *v2)
40{
41 JSType t1 = v1->type();
42 JSType t2 = v2->type();
43
44 if (t1 != t2) {
45 if (t1 == UndefinedType)
46 t1 = NullType;
47 if (t2 == UndefinedType)
48 t2 = NullType;
49
50 if (t1 == BooleanType)
51 t1 = NumberType;
52 if (t2 == BooleanType)
53 t2 = NumberType;
54
55 if (t1 == NumberType && t2 == StringType) {
56 // use toNumber
57 } else if (t1 == StringType && t2 == NumberType)
58 t1 = NumberType;
59 // use toNumber
60 else {
61 if ((t1 == StringType || t1 == NumberType) && t2 == ObjectType) {
62 v2 = v2->toPrimitive(exec);
63 if (exec->hadException())
64 return false;
65 return equal(exec, v1, v2);
66 }
67 if (t1 == NullType && t2 == ObjectType)
68 return static_cast<JSObject *>(v2)->masqueradeAsUndefined();
69 if (t1 == ObjectType && (t2 == StringType || t2 == NumberType)) {
70 v1 = v1->toPrimitive(exec);
71 if (exec->hadException())
72 return false;
73 return equal(exec, v1, v2);
74 }
75 if (t1 == ObjectType && t2 == NullType)
76 return static_cast<JSObject *>(v1)->masqueradeAsUndefined();
77 if (t1 != t2)
78 return false;
79 }
80 }
81
82 if (t1 == UndefinedType || t1 == NullType)
83 return true;
84
85 if (t1 == NumberType) {
86 double d1 = v1->toNumber(exec);
87 double d2 = v2->toNumber(exec);
88 return d1 == d2;
89 }
90
91 if (t1 == StringType)
92 return static_cast<StringImp*>(v1)->value() == static_cast<StringImp*>(v2)->value();
93
94 if (t1 == BooleanType)
95 return v1->toBoolean(exec) == v2->toBoolean(exec);
96
97 // types are Object
98 return v1 == v2;
99}
100
101bool strictEqual(ExecState *exec, JSValue *v1, JSValue *v2)
102{
103 JSType t1 = v1->type();
104 JSType t2 = v2->type();
105
106 if (t1 != t2)
107 return false;
108 if (t1 == UndefinedType || t1 == NullType)
109 return true;
110 if (t1 == NumberType) {
111 double n1 = v1->toNumber(exec);
112 double n2 = v2->toNumber(exec);
113 if (n1 == n2)
114 return true;
115 return false;
116 } else if (t1 == StringType)
117 return v1->toString(exec) == v2->toString(exec);
118 else if (t2 == BooleanType)
119 return v1->toBoolean(exec) == v2->toBoolean(exec);
120
121 if (v1 == v2)
122 return true;
123 /* TODO: joined objects */
124
125 return false;
126}
127
128}