1 // -*- c-basic-offset: 2 -*-
3 * This file is part of the KDE libraries
4 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
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.
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.
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.
23 #include "operations.h"
30 #include <wtf/MathExtras.h>
33 #if HAVE(FUNC_ISINF) || HAVE(FUNC_FINITE)
46 // FIXME: Should probably be inlined on non-Darwin platforms too, and controlled exclusively
47 // by HAVE macros rather than PLATFORM.
49 // FIXME: Merge with isnan in MathExtras.h and remove this one entirely.
55 return _isnan(d
) != 0;
61 // FIXME: Merge with isinf in MathExtras.h and remove this one entirely.
64 // FIXME: should be HAVE(_FPCLASS)
66 int fpClass
= _fpclass(d
);
67 return _FPCLASS_PINF
== fpClass
|| _FPCLASS_NINF
== fpClass
;
68 #elif HAVE(FUNC_ISINF) && !PLATFORM(SOLARIS_OS)
70 #elif HAVE(FUNC_FINITE)
71 return finite(d
) == 0 && d
== d
;
72 #elif HAVE(FUNC__FINITE)
73 return _finite(d
) == 0 && d
== d
;
79 bool isPosInf(double d
)
81 // FIXME: should be HAVE(_FPCLASS)
83 return _FPCLASS_PINF
== _fpclass(d
);
84 #elif HAVE(FUNC_ISINF) && !PLATFORM(SOLARIS_OS)
85 return (isinf(d
) == 1);
86 #elif HAVE(FUNC_FINITE)
87 return !finite(d
) && d
== d
; // ### can we distinguish between + and - ?
88 #elif HAVE(FUNC__FINITE)
89 return !_finite(d
) && d
== d
; // ###
95 bool isNegInf(double d
)
97 // FIXME: should be HAVE(_FPCLASS)
99 return _FPCLASS_NINF
== _fpclass(d
);
100 #elif HAVE(FUNC_ISINF) && !PLATFORM(SOLARIS_OS)
101 return (isinf(d
) == -1);
102 #elif HAVE(FUNC_FINITE)
103 return finite(d
) == 0 && d
== d
; // ###
104 #elif HAVE(FUNC__FINITE)
105 return _finite(d
) == 0 && d
== d
; // ###
114 bool equal(ExecState
*exec
, JSValue
*v1
, JSValue
*v2
)
116 JSType t1
= v1
->type();
117 JSType t2
= v2
->type();
120 if (t1
== UndefinedType
)
122 if (t2
== UndefinedType
)
125 if (t1
== BooleanType
)
127 if (t2
== BooleanType
)
130 if (t1
== NumberType
&& t2
== StringType
) {
132 } else if (t1
== StringType
&& t2
== NumberType
)
136 if ((t1
== StringType
|| t1
== NumberType
) && t2
>= ObjectType
)
137 return equal(exec
, v1
, v2
->toPrimitive(exec
));
138 if (t1
== NullType
&& t2
== ObjectType
)
139 return static_cast<JSObject
*>(v2
)->masqueradeAsUndefined();
140 if (t1
>= ObjectType
&& (t2
== StringType
|| t2
== NumberType
))
141 return equal(exec
, v1
->toPrimitive(exec
), v2
);
142 if (t1
== ObjectType
&& t2
== NullType
)
143 return static_cast<JSObject
*>(v1
)->masqueradeAsUndefined();
149 if (t1
== UndefinedType
|| t1
== NullType
)
152 if (t1
== NumberType
) {
153 double d1
= v1
->toNumber(exec
);
154 double d2
= v2
->toNumber(exec
);
158 if (t1
== StringType
)
159 return v1
->toString(exec
) == v2
->toString(exec
);
161 if (t1
== BooleanType
)
162 return v1
->toBoolean(exec
) == v2
->toBoolean(exec
);
168 bool strictEqual(ExecState
*exec
, JSValue
*v1
, JSValue
*v2
)
170 JSType t1
= v1
->type();
171 JSType t2
= v2
->type();
175 if (t1
== UndefinedType
|| t1
== NullType
)
177 if (t1
== NumberType
) {
178 double n1
= v1
->toNumber(exec
);
179 double n2
= v2
->toNumber(exec
);
183 } else if (t1
== StringType
)
184 return v1
->toString(exec
) == v2
->toString(exec
);
185 else if (t2
== BooleanType
)
186 return v1
->toBoolean(exec
) == v2
->toBoolean(exec
);
190 /* TODO: joined objects */
195 int relation(ExecState
*exec
, JSValue
*v1
, JSValue
*v2
)
201 bool wasNotString1
= v1
->getPrimitiveNumber(exec
, n1
, p1
);
202 if (exec
->hadException())
205 bool wasNotString2
= v2
->getPrimitiveNumber(exec
, n2
, p2
);
207 if (wasNotString1
|| wasNotString2
) {
212 return -1; // must be NaN, so undefined
215 assert(p1
->isString() && p2
->isString());
216 return static_cast<const StringImp
*>(p1
)->value() < static_cast<const StringImp
*>(p2
)->value() ? 1 : 0;
219 int relation(ExecState
*exec
, JSValue
*v1
, double n2
)
224 v1
->getPrimitiveNumber(exec
, n1
, p1
);
225 if (exec
->hadException())
232 return -1; // must be NaN, so undefined
235 int maxInt(int d1
, int d2
)
237 return (d1
> d2
) ? d1
: d2
;
240 int minInt(int d1
, int d2
)
242 return (d1
< d2
) ? d1
: d2
;