1 | // -*- c-basic-offset: 2 -*-
|
---|
2 | /*
|
---|
3 | * This file is part of the KDE libraries
|
---|
4 | * Copyright (C) 1999-2000 Harri Porten ([email protected])
|
---|
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 |
|
---|
36 | namespace KJS {
|
---|
37 |
|
---|
38 | // ECMA 11.9.3
|
---|
39 | bool 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<JSString*>(v1)->value() == static_cast<JSString*>(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 |
|
---|
101 | bool strictEqual(JSValue* v1, JSValue* v2)
|
---|
102 | {
|
---|
103 | JSType t1 = v1->type();
|
---|
104 | JSType t2 = v2->type();
|
---|
105 |
|
---|
106 | if (t1 != t2)
|
---|
107 | return false;
|
---|
108 |
|
---|
109 | if (t1 == NumberType)
|
---|
110 | return v1->getNumber() == v2->getNumber();
|
---|
111 |
|
---|
112 | if (t1 == StringType)
|
---|
113 | return static_cast<JSString*>(v1)->value() == static_cast<JSString*>(v2)->value();
|
---|
114 |
|
---|
115 | return v1 == v2; // covers object, boolean, null, and undefined types
|
---|
116 | }
|
---|
117 |
|
---|
118 | JSValue* throwOutOfMemoryError(ExecState* exec)
|
---|
119 | {
|
---|
120 | JSObject* error = Error::create(exec, GeneralError, "Out of memory");
|
---|
121 | exec->setException(error);
|
---|
122 | return error;
|
---|
123 | }
|
---|
124 |
|
---|
125 | }
|
---|