1 | /*
|
---|
2 | * This file is part of the KDE libraries
|
---|
3 | * Copyright (C) 1999-2000 Harri Porten ([email protected])
|
---|
4 | *
|
---|
5 | * This library is free software; you can redistribute it and/or
|
---|
6 | * modify it under the terms of the GNU Library General Public
|
---|
7 | * License as published by the Free Software Foundation; either
|
---|
8 | * version 2 of the License, or (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This library is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
13 | * Library General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU Library General Public License
|
---|
16 | * along with this library; see the file COPYING.LIB. If not, write to
|
---|
17 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
---|
18 | * Boston, MA 02110-1301, USA.
|
---|
19 | *
|
---|
20 | */
|
---|
21 |
|
---|
22 | #ifndef _KJS_OPERATIONS_H_
|
---|
23 | #define _KJS_OPERATIONS_H_
|
---|
24 |
|
---|
25 | #include "JSImmediate.h"
|
---|
26 | #include "JSNumberCell.h"
|
---|
27 | #include "JSString.h"
|
---|
28 |
|
---|
29 | namespace JSC {
|
---|
30 |
|
---|
31 | // ECMA 11.9.3
|
---|
32 | bool equal(ExecState*, JSValue*, JSValue*);
|
---|
33 | bool equalSlowCase(ExecState*, JSValue*, JSValue*);
|
---|
34 |
|
---|
35 | ALWAYS_INLINE bool equalSlowCaseInline(ExecState* exec, JSValue* v1, JSValue* v2)
|
---|
36 | {
|
---|
37 | ASSERT(!JSImmediate::areBothImmediateNumbers(v1, v2));
|
---|
38 |
|
---|
39 | do {
|
---|
40 | if (v1->isNumber() && v2->isNumber())
|
---|
41 | return v1->uncheckedGetNumber() == v2->uncheckedGetNumber();
|
---|
42 |
|
---|
43 | bool s1 = v1->isString();
|
---|
44 | bool s2 = v2->isString();
|
---|
45 | if (s1 && s2)
|
---|
46 | return asString(v1)->value() == asString(v2)->value();
|
---|
47 |
|
---|
48 | if (v1->isUndefinedOrNull()) {
|
---|
49 | if (v2->isUndefinedOrNull())
|
---|
50 | return true;
|
---|
51 | if (JSImmediate::isImmediate(v2))
|
---|
52 | return false;
|
---|
53 | return v2->asCell()->structureID()->typeInfo().masqueradesAsUndefined();
|
---|
54 | }
|
---|
55 |
|
---|
56 | if (v2->isUndefinedOrNull()) {
|
---|
57 | if (JSImmediate::isImmediate(v1))
|
---|
58 | return false;
|
---|
59 | return v1->asCell()->structureID()->typeInfo().masqueradesAsUndefined();
|
---|
60 | }
|
---|
61 |
|
---|
62 | if (v1->isObject()) {
|
---|
63 | if (v2->isObject())
|
---|
64 | return v1 == v2;
|
---|
65 | JSValue* p1 = v1->toPrimitive(exec);
|
---|
66 | if (exec->hadException())
|
---|
67 | return false;
|
---|
68 | v1 = p1;
|
---|
69 | if (JSImmediate::areBothImmediateNumbers(v1, v2))
|
---|
70 | return v1 == v2;
|
---|
71 | continue;
|
---|
72 | }
|
---|
73 |
|
---|
74 | if (v2->isObject()) {
|
---|
75 | JSValue* p2 = v2->toPrimitive(exec);
|
---|
76 | if (exec->hadException())
|
---|
77 | return false;
|
---|
78 | v2 = p2;
|
---|
79 | if (JSImmediate::areBothImmediateNumbers(v1, v2))
|
---|
80 | return v1 == v2;
|
---|
81 | continue;
|
---|
82 | }
|
---|
83 |
|
---|
84 | if (s1 || s2) {
|
---|
85 | double d1 = v1->toNumber(exec);
|
---|
86 | double d2 = v2->toNumber(exec);
|
---|
87 | return d1 == d2;
|
---|
88 | }
|
---|
89 |
|
---|
90 | if (v1->isBoolean()) {
|
---|
91 | if (v2->isNumber())
|
---|
92 | return static_cast<double>(v1->getBoolean()) == v2->uncheckedGetNumber();
|
---|
93 | } else if (v2->isBoolean()) {
|
---|
94 | if (v1->isNumber())
|
---|
95 | return v1->uncheckedGetNumber() == static_cast<double>(v2->getBoolean());
|
---|
96 | }
|
---|
97 |
|
---|
98 | return v1 == v2;
|
---|
99 | } while (true);
|
---|
100 | }
|
---|
101 |
|
---|
102 |
|
---|
103 | bool strictEqual(JSValue*, JSValue*);
|
---|
104 | bool strictEqualSlowCase(JSValue*, JSValue*);
|
---|
105 |
|
---|
106 | inline bool strictEqualSlowCaseInline(JSValue* v1, JSValue* v2)
|
---|
107 | {
|
---|
108 | ASSERT(!JSImmediate::areBothImmediate(v1, v2));
|
---|
109 |
|
---|
110 | if (JSImmediate::isEitherImmediate(v1, v2)) {
|
---|
111 | ASSERT(v1 == JSImmediate::zeroImmediate() || v2 == JSImmediate::zeroImmediate());
|
---|
112 | ASSERT(v1 != v2);
|
---|
113 |
|
---|
114 | // The reason we can't just return false here is that 0 === -0,
|
---|
115 | // and while the former is an immediate number, the latter is not.
|
---|
116 | if (v1 == JSImmediate::zeroImmediate())
|
---|
117 | return asCell(v2)->isNumber() && asNumberCell(v2)->value() == 0;
|
---|
118 | return asCell(v1)->isNumber() && asNumberCell(v1)->value() == 0;
|
---|
119 | }
|
---|
120 |
|
---|
121 | if (asCell(v1)->isNumber()) {
|
---|
122 | return asCell(v2)->isNumber()
|
---|
123 | && asNumberCell(v1)->value() == asNumberCell(v2)->value();
|
---|
124 | }
|
---|
125 |
|
---|
126 | if (asCell(v1)->isString()) {
|
---|
127 | return asCell(v2)->isString()
|
---|
128 | && asString(v1)->value() == asString(v2)->value();
|
---|
129 | }
|
---|
130 |
|
---|
131 | return v1 == v2;
|
---|
132 | }
|
---|
133 |
|
---|
134 | JSValue* throwOutOfMemoryError(ExecState*);
|
---|
135 | }
|
---|
136 |
|
---|
137 | #endif
|
---|