• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • kjs
 

kjs

  • kjs
operations.cpp
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 #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 // For declaration of isinf on Sun C++
37 #ifdef __SUNPRO_CC
38 #include <sunmath.h>
39 #endif
40 
41 #ifndef HAVE_FUNC_ISINF
42 #ifdef HAVE_IEEEFP_H
43 #include <ieeefp.h>
44 #endif
45 #endif /* HAVE_FUNC_ISINF */
46 
47 #if HAVE_FLOAT_H
48 #include <float.h>
49 #endif
50 
51 #include "operations.h"
52 #include "object.h"
53 
54 using namespace KJS;
55 
56 bool KJS::isNaN(double d)
57 {
58 #ifdef HAVE_FUNC_ISNAN
59  return isnan(d);
60 #elif defined HAVE_FLOAT_H
61  return _isnan(d) != 0;
62 #else
63  return !(d == d);
64 #endif
65 }
66 
67 bool KJS::isInf(double d)
68 {
69 #if defined(HAVE_FUNC_ISINF)
70  return isinf(d);
71 #elif HAVE_FUNC_FINITE
72  return finite(d) == 0 && d == d;
73 #elif HAVE_FUNC__FINITE
74  return _finite(d) == 0 && d == d;
75 #else
76  return false;
77 #endif
78 }
79 
80 bool KJS::isPosInf(double d)
81 {
82 #if defined(HAVE_FUNC_ISINF)
83  return (isinf(d) == 1);
84 #elif HAVE_FUNC_FINITE
85  return finite(d) == 0 && d == d; // ### can we distinguish between + and - ?
86 #elif HAVE_FUNC__FINITE
87  return _finite(d) == 0 && d == d; // ###
88 #else
89  return false;
90 #endif
91 }
92 
93 bool KJS::isNegInf(double d)
94 {
95 #if defined(HAVE_FUNC_ISINF)
96  return (isinf(d) == -1);
97 #elif HAVE_FUNC_FINITE
98  return finite(d) == 0 && d == d; // ###
99 #elif HAVE_FUNC__FINITE
100  return _finite(d) == 0 && d == d; // ###
101 #else
102  return false;
103 #endif
104 }
105 
106 // ECMA 11.9.3
107 bool KJS::equal(ExecState *exec, const Value& v1, const Value& v2)
108 {
109  Type t1 = v1.type();
110  Type t2 = v2.type();
111 
112  if (t1 == t2) {
113  if (t1 == UndefinedType || t1 == NullType)
114  return true;
115  if (t1 == NumberType)
116  {
117  double d1 = v1.toNumber(exec);
118  double d2 = v2.toNumber(exec);
119  if ( isNaN( d1 ) || isNaN( d2 ) )
120  return false;
121  return ( d1 == d2 ); /* TODO: +0, -0 ? */
122  }
123  if (t1 == StringType)
124  return (v1.toString(exec) == v2.toString(exec));
125  if (t1 == BooleanType)
126  return (v1.toBoolean(exec) == v2.toBoolean(exec));
127 
128  // types are Object
129  return (v1.imp() == v2.imp());
130  }
131 
132  // different types
133  if ((t1 == NullType && t2 == UndefinedType) || (t1 == UndefinedType && t2 == NullType))
134  return true;
135  if (t1 == NumberType && t2 == StringType) {
136  Number n2 = v2.toNumber(exec);
137  return equal(exec,v1, n2);
138  }
139  if ((t1 == StringType && t2 == NumberType) || t1 == BooleanType) {
140  Number n1 = v1.toNumber(exec);
141  return equal(exec,n1, v2);
142  }
143  if (t2 == BooleanType) {
144  Number n2 = v2.toNumber(exec);
145  return equal(exec,v1, n2);
146  }
147  if ((t1 == StringType || t1 == NumberType) && t2 >= ObjectType) {
148  Value p2 = v2.toPrimitive(exec);
149  return equal(exec,v1, p2);
150  }
151  if (t1 >= ObjectType && (t2 == StringType || t2 == NumberType)) {
152  Value p1 = v1.toPrimitive(exec);
153  return equal(exec,p1, v2);
154  }
155 
156  return false;
157 }
158 
159 bool KJS::strictEqual(ExecState *exec, const Value &v1, const Value &v2)
160 {
161  Type t1 = v1.type();
162  Type t2 = v2.type();
163 
164  if (t1 != t2)
165  return false;
166  if (t1 == UndefinedType || t1 == NullType)
167  return true;
168  if (t1 == NumberType) {
169  double n1 = v1.toNumber(exec);
170  double n2 = v2.toNumber(exec);
171  if (isNaN(n1) || isNaN(n2))
172  return false;
173  if (n1 == n2)
174  return true;
175  /* TODO: +0 and -0 */
176  return false;
177  } else if (t1 == StringType) {
178  return v1.toString(exec) == v2.toString(exec);
179  } else if (t2 == BooleanType) {
180  return v1.toBoolean(exec) == v2.toBoolean(exec);
181  }
182  if (v1.imp() == v2.imp())
183  return true;
184  /* TODO: joined objects */
185 
186  return false;
187 }
188 
189 int KJS::relation(ExecState *exec, const Value& v1, const Value& v2)
190 {
191  Value p1 = v1.toPrimitive(exec,NumberType);
192  Value p2 = v2.toPrimitive(exec,NumberType);
193 
194  if (p1.type() == StringType && p2.type() == StringType)
195  return p1.toString(exec) < p2.toString(exec) ? 1 : 0;
196 
197  double n1 = p1.toNumber(exec);
198  double n2 = p2.toNumber(exec);
199  if ( isNaN( n1 ) || isNaN( n2 ) )
200  return -1; // means undefined
201  if (n1 == n2)
202  return 0;
203  /* TODO: +0, -0 */
204  if ( isPosInf( n1 ) )
205  return 0;
206  if ( isPosInf( n2 ) )
207  return 1;
208  if ( isNegInf( n2 ) )
209  return 0;
210  if ( isNegInf( n1 ) )
211  return 1;
212  return (n1 < n2) ? 1 : 0;
213 }
214 
215 int KJS::maxInt(int d1, int d2)
216 {
217  return (d1 > d2) ? d1 : d2;
218 }
219 
220 int KJS::minInt(int d1, int d2)
221 {
222  return (d1 < d2) ? d1 : d2;
223 }
224 
225 // ECMA 11.6
226 Value KJS::add(ExecState *exec, const Value &v1, const Value &v2, char oper)
227 {
228  // exception for the Date exception in defaultValue()
229  Type preferred = oper == '+' ? UnspecifiedType : NumberType;
230  Value p1 = v1.toPrimitive(exec, preferred);
231  Value p2 = v2.toPrimitive(exec, preferred);
232 
233  if ((p1.type() == StringType || p2.type() == StringType) && oper == '+') {
234  UString s1 = p1.toString(exec);
235  UString s2 = p2.toString(exec);
236 
237  return String(s1 + s2);
238  }
239 
240  double n1 = p1.toNumber(exec);
241  double n2 = p2.toNumber(exec);
242 
243  if (oper == '+')
244  return Number(n1 + n2);
245  else
246  return Number(n1 - n2);
247 }
248 
249 // ECMA 11.5
250 Value KJS::mult(ExecState *exec, const Value &v1, const Value &v2, char oper)
251 {
252  double n1 = v1.toNumber(exec);
253  double n2 = v2.toNumber(exec);
254 
255  double result;
256 
257  if (oper == '*')
258  result = n1 * n2;
259  else if (oper == '/')
260  result = n1 / n2;
261  else
262  result = fmod(n1, n2);
263 
264  return Number(result);
265 }
KJS::Value
Value objects are act as wrappers ("smart pointers") around ValueImp objects and their descendents...
Definition: value.h:168
KJS::Value::type
Type type() const
Returns the type of value.
Definition: value.h:196
KJS::Number
Represents an primitive Number value.
Definition: value.h:368
KJS::Value::toBoolean
bool toBoolean(ExecState *exec) const
Performs the ToBoolean type conversion operation on this value (ECMA 9.2)
Definition: value.h:217
KJS::Value::toNumber
double toNumber(ExecState *exec) const
Performs the ToNumber type conversion operation on this value (ECMA 9.3)
Definition: value.h:222
KJS::UString
Unicode string class.
Definition: ustring.h:190
KJS::String
Represents an primitive String value.
Definition: value.h:341
KJS
Definition: array_instance.h:28
KJS::Value::toPrimitive
Value toPrimitive(ExecState *exec, Type preferredType=UnspecifiedType) const
Performs the ToPrimitive type conversion operation on this value (ECMA 9.1)
Definition: value.h:210
KJS::Value::toString
UString toString(ExecState *exec) const
Performs the ToString type conversion operation on this value (ECMA 9.8)
Definition: value.h:247
KJS::ExecState
Represents the current state of script execution.
Definition: interpreter.h:439

kjs

Skip menu "kjs"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

kjs

Skip menu "kjs"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for kjs by doxygen 1.8.11
This website is maintained by Timothy Pearson.