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

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
  •     interface
  •     library
  •   kspeech
  •   ktexteditor
  • kabc
  • kate
  • kcmshell
  • kdecore
  • kded
  • kdefx
  • kdeprint
  • kdesu
  • kdeui
  • kdoctools
  • khtml
  • kimgio
  • kinit
  • kio
  •   bookmarks
  •   httpfilter
  •   kfile
  •   kio
  •   kioexec
  •   kpasswdserver
  •   kssl
  • kioslave
  •   http
  • kjs
  • kmdi
  •   kmdi
  • knewstuff
  • kparts
  • krandr
  • kresources
  • kspell2
  • kunittest
  • kutils
  • kwallet
  • libkmid
  • libkscreensaver
Generated for kjs by doxygen 1.8.1.2
This website is maintained by Timothy Pearson.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. |