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., 59 Temple Place - Suite 330,
|
---|
19 | * Boston, MA 02111-1307, USA.
|
---|
20 | *
|
---|
21 | */
|
---|
22 |
|
---|
23 | #ifdef HAVE_CONFIG_H
|
---|
24 | #include "config.h"
|
---|
25 | #endif
|
---|
26 | #ifndef HAVE_FLOAT_H /* just for !Windows */
|
---|
27 | #define HAVE_FLOAT_H 0
|
---|
28 | #define HAVE_FUNC__FINITE 0
|
---|
29 | #endif
|
---|
30 |
|
---|
31 | #include <stdio.h>
|
---|
32 | #include <assert.h>
|
---|
33 | #include <math.h>
|
---|
34 | #include <stdlib.h>
|
---|
35 |
|
---|
36 | #ifndef HAVE_FUNC_ISINF
|
---|
37 | #ifdef HAVE_IEEEFP_H
|
---|
38 | #include <ieeefp.h>
|
---|
39 | #endif
|
---|
40 | #endif /* HAVE_FUNC_ISINF */
|
---|
41 |
|
---|
42 | #if HAVE_FLOAT_H
|
---|
43 | #include <float.h>
|
---|
44 | #endif
|
---|
45 |
|
---|
46 | #include "operations.h"
|
---|
47 | #include "object.h"
|
---|
48 |
|
---|
49 | using namespace KJS;
|
---|
50 |
|
---|
51 | bool KJS::isNaN(double d)
|
---|
52 | {
|
---|
53 | #ifdef HAVE_FUNC_ISNAN
|
---|
54 | return isnan(d);
|
---|
55 | #elif defined HAVE_FLOAT_H
|
---|
56 | return _isnan(d) != 0;
|
---|
57 | #else
|
---|
58 | return !(d == d);
|
---|
59 | #endif
|
---|
60 | }
|
---|
61 |
|
---|
62 | bool KJS::isInf(double d)
|
---|
63 | {
|
---|
64 | #if defined(HAVE_FUNC_ISINF)
|
---|
65 | return isinf(d);
|
---|
66 | #elif HAVE_FUNC_FINITE
|
---|
67 | return finite(d) == 0 && d == d;
|
---|
68 | #elif HAVE_FUNC__FINITE
|
---|
69 | return _finite(d) == 0 && d == d;
|
---|
70 | #else
|
---|
71 | return false;
|
---|
72 | #endif
|
---|
73 | }
|
---|
74 |
|
---|
75 | bool KJS::isPosInf(double d)
|
---|
76 | {
|
---|
77 | #if defined(HAVE_FUNC_ISINF)
|
---|
78 | return (isinf(d) == 1);
|
---|
79 | #elif HAVE_FUNC_FINITE
|
---|
80 | return finite(d) == 0 && d == d; // ### can we distinguish between + and - ?
|
---|
81 | #elif HAVE_FUNC__FINITE
|
---|
82 | return _finite(d) == 0 && d == d; // ###
|
---|
83 | #else
|
---|
84 | return false;
|
---|
85 | #endif
|
---|
86 | }
|
---|
87 |
|
---|
88 | bool KJS::isNegInf(double d)
|
---|
89 | {
|
---|
90 | #if defined(HAVE_FUNC_ISINF)
|
---|
91 | return (isinf(d) == -1);
|
---|
92 | #elif HAVE_FUNC_FINITE
|
---|
93 | return finite(d) == 0 && d == d; // ###
|
---|
94 | #elif HAVE_FUNC__FINITE
|
---|
95 | return _finite(d) == 0 && d == d; // ###
|
---|
96 | #else
|
---|
97 | return false;
|
---|
98 | #endif
|
---|
99 | }
|
---|
100 |
|
---|
101 | // ECMA 11.9.3
|
---|
102 | bool KJS::equal(ExecState *exec, const Value& v1, const Value& v2)
|
---|
103 | {
|
---|
104 | Type t1 = v1.type();
|
---|
105 | Type t2 = v2.type();
|
---|
106 |
|
---|
107 | if (t1 == t2) {
|
---|
108 | if (t1 == UndefinedType || t1 == NullType)
|
---|
109 | return true;
|
---|
110 | if (t1 == NumberType)
|
---|
111 | {
|
---|
112 | double d1 = v1.toNumber(exec);
|
---|
113 | double d2 = v2.toNumber(exec);
|
---|
114 | if ( isNaN( d1 ) || isNaN( d2 ) )
|
---|
115 | return false;
|
---|
116 | return ( d1 == d2 ); /* TODO: +0, -0 ? */
|
---|
117 | }
|
---|
118 | if (t1 == StringType)
|
---|
119 | return (v1.toString(exec) == v2.toString(exec));
|
---|
120 | if (t1 == BooleanType)
|
---|
121 | return (v1.toBoolean(exec) == v2.toBoolean(exec));
|
---|
122 |
|
---|
123 | // types are Object
|
---|
124 | return (v1.imp() == v2.imp());
|
---|
125 | }
|
---|
126 |
|
---|
127 | // different types
|
---|
128 | if ((t1 == NullType && t2 == UndefinedType) || (t1 == UndefinedType && t2 == NullType))
|
---|
129 | return true;
|
---|
130 | if (t1 == NumberType && t2 == StringType) {
|
---|
131 | Number n2 = v2.toNumber(exec);
|
---|
132 | return equal(exec,v1, n2);
|
---|
133 | }
|
---|
134 | if ((t1 == StringType && t2 == NumberType) || t1 == BooleanType) {
|
---|
135 | Number n1 = v1.toNumber(exec);
|
---|
136 | return equal(exec,n1, v2);
|
---|
137 | }
|
---|
138 | if (t2 == BooleanType) {
|
---|
139 | Number n2 = v2.toNumber(exec);
|
---|
140 | return equal(exec,v1, n2);
|
---|
141 | }
|
---|
142 | if ((t1 == StringType || t1 == NumberType) && t2 >= ObjectType) {
|
---|
143 | Value p2 = v2.toPrimitive(exec);
|
---|
144 | return equal(exec,v1, p2);
|
---|
145 | }
|
---|
146 | if (t1 >= ObjectType && (t2 == StringType || t2 == NumberType)) {
|
---|
147 | Value p1 = v1.toPrimitive(exec);
|
---|
148 | return equal(exec,p1, v2);
|
---|
149 | }
|
---|
150 |
|
---|
151 | return false;
|
---|
152 | }
|
---|
153 |
|
---|
154 | bool KJS::strictEqual(ExecState *exec, const Value &v1, const Value &v2)
|
---|
155 | {
|
---|
156 | Type t1 = v1.type();
|
---|
157 | Type t2 = v2.type();
|
---|
158 |
|
---|
159 | if (t1 != t2)
|
---|
160 | return false;
|
---|
161 | if (t1 == UndefinedType || t1 == NullType)
|
---|
162 | return true;
|
---|
163 | if (t1 == NumberType) {
|
---|
164 | double n1 = v1.toNumber(exec);
|
---|
165 | double n2 = v2.toNumber(exec);
|
---|
166 | if (isNaN(n1) || isNaN(n2))
|
---|
167 | return false;
|
---|
168 | if (n1 == n2)
|
---|
169 | return true;
|
---|
170 | /* TODO: +0 and -0 */
|
---|
171 | return false;
|
---|
172 | } else if (t1 == StringType) {
|
---|
173 | return v1.toString(exec) == v2.toString(exec);
|
---|
174 | } else if (t2 == BooleanType) {
|
---|
175 | return v1.toBoolean(exec) == v2.toBoolean(exec);
|
---|
176 | }
|
---|
177 | if (v1.imp() == v2.imp())
|
---|
178 | return true;
|
---|
179 | /* TODO: joined objects */
|
---|
180 |
|
---|
181 | return false;
|
---|
182 | }
|
---|
183 |
|
---|
184 | int KJS::relation(ExecState *exec, const Value& v1, const Value& v2)
|
---|
185 | {
|
---|
186 | Value p1 = v1.toPrimitive(exec,NumberType);
|
---|
187 | Value p2 = v2.toPrimitive(exec,NumberType);
|
---|
188 |
|
---|
189 | if (p1.type() == StringType && p2.type() == StringType)
|
---|
190 | return p1.toString(exec) < p2.toString(exec) ? 1 : 0;
|
---|
191 |
|
---|
192 | double n1 = p1.toNumber(exec);
|
---|
193 | double n2 = p2.toNumber(exec);
|
---|
194 | if ( isNaN( n1 ) || isNaN( n2 ) )
|
---|
195 | return -1; // means undefined
|
---|
196 | if (n1 == n2)
|
---|
197 | return 0;
|
---|
198 | /* TODO: +0, -0 */
|
---|
199 | if ( isPosInf( n1 ) )
|
---|
200 | return 0;
|
---|
201 | if ( isPosInf( n2 ) )
|
---|
202 | return 1;
|
---|
203 | if ( isNegInf( n2 ) )
|
---|
204 | return 0;
|
---|
205 | if ( isNegInf( n1 ) )
|
---|
206 | return 1;
|
---|
207 | return (n1 < n2) ? 1 : 0;
|
---|
208 | }
|
---|
209 |
|
---|
210 | int KJS::maxInt(int d1, int d2)
|
---|
211 | {
|
---|
212 | return (d1 > d2) ? d1 : d2;
|
---|
213 | }
|
---|
214 |
|
---|
215 | int KJS::minInt(int d1, int d2)
|
---|
216 | {
|
---|
217 | return (d1 < d2) ? d1 : d2;
|
---|
218 | }
|
---|
219 |
|
---|
220 | // ECMA 11.6
|
---|
221 | Value KJS::add(ExecState *exec, const Value &v1, const Value &v2, char oper)
|
---|
222 | {
|
---|
223 | // exception for the Date exception in defaultValue()
|
---|
224 | Type preferred = oper == '+' ? UnspecifiedType : NumberType;
|
---|
225 | Value p1 = v1.toPrimitive(exec, preferred);
|
---|
226 | Value p2 = v2.toPrimitive(exec, preferred);
|
---|
227 |
|
---|
228 | if ((p1.type() == StringType || p2.type() == StringType) && oper == '+') {
|
---|
229 | UString s1 = p1.toString(exec);
|
---|
230 | UString s2 = p2.toString(exec);
|
---|
231 |
|
---|
232 | return String(s1 + s2);
|
---|
233 | }
|
---|
234 |
|
---|
235 | double n1 = p1.toNumber(exec);
|
---|
236 | double n2 = p2.toNumber(exec);
|
---|
237 |
|
---|
238 | if (oper == '+')
|
---|
239 | return Number(n1 + n2);
|
---|
240 | else
|
---|
241 | return Number(n1 - n2);
|
---|
242 | }
|
---|
243 |
|
---|
244 | // ECMA 11.5
|
---|
245 | Value KJS::mult(ExecState *exec, const Value &v1, const Value &v2, char oper)
|
---|
246 | {
|
---|
247 | Number n1 = v1.toNumber(exec);
|
---|
248 | Number n2 = v2.toNumber(exec);
|
---|
249 |
|
---|
250 | double result;
|
---|
251 |
|
---|
252 | if (oper == '*')
|
---|
253 | result = n1.value() * n2.value();
|
---|
254 | else if (oper == '/')
|
---|
255 | result = n1.value() / n2.value();
|
---|
256 | else
|
---|
257 | result = fmod(n1.value(), n2.value());
|
---|
258 |
|
---|
259 | return Number(result);
|
---|
260 | }
|
---|