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

kjs

  • kjs
operations.cpp
1/*
2 * This file is part of the KDE libraries
3 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 *
20 */
21
22#ifdef HAVE_CONFIG_H
23#include "config.h"
24#endif
25#ifndef HAVE_FLOAT_H /* just for !Windows */
26#define HAVE_FLOAT_H 0
27#endif
28
29#include <stdio.h>
30#include <assert.h>
31#include <math.h>
32#include <stdlib.h>
33
34// For declaration of isinf on Sun C++
35#ifdef __SUNPRO_CC
36#include <sunmath.h>
37#endif
38
39#ifdef HAVE_IEEEFP_H
40#include <ieeefp.h>
41#endif
42
43#if HAVE_FLOAT_H
44#include <float.h>
45#endif
46
47#include "operations.h"
48#include "object.h"
49
50using namespace KJS;
51
52bool KJS::isNaN(double d)
53{
54 return isnan(d);
55}
56
57bool KJS::isInf(double d)
58{
59 return isinf(d);
60}
61
62bool KJS::isPosInf(double d)
63{
64 return ( isinf(d) && d > 0 );
65}
66
67bool KJS::isNegInf(double d)
68{
69 return ( isinf(d) && d < 0 );
70}
71
72// ECMA 11.9.3
73bool KJS::equal(ExecState *exec, const Value& v1, const Value& v2)
74{
75 Type t1 = v1.type();
76 Type t2 = v2.type();
77
78 if (t1 == t2) {
79 if (t1 == UndefinedType || t1 == NullType)
80 return true;
81 if (t1 == NumberType)
82 {
83 double d1 = v1.toNumber(exec);
84 double d2 = v2.toNumber(exec);
85 if ( isNaN( d1 ) || isNaN( d2 ) )
86 return false;
87 return ( d1 == d2 ); /* TODO: +0, -0 ? */
88 }
89 if (t1 == StringType)
90 return (v1.toString(exec) == v2.toString(exec));
91 if (t1 == BooleanType)
92 return (v1.toBoolean(exec) == v2.toBoolean(exec));
93
94 // types are Object
95 return (v1.imp() == v2.imp());
96 }
97
98 // different types
99 if ((t1 == NullType && t2 == UndefinedType) || (t1 == UndefinedType && t2 == NullType))
100 return true;
101 if (t1 == NumberType && t2 == StringType) {
102 Number n2 = v2.toNumber(exec);
103 return equal(exec,v1, n2);
104 }
105 if ((t1 == StringType && t2 == NumberType) || t1 == BooleanType) {
106 Number n1 = v1.toNumber(exec);
107 return equal(exec,n1, v2);
108 }
109 if (t2 == BooleanType) {
110 Number n2 = v2.toNumber(exec);
111 return equal(exec,v1, n2);
112 }
113 if ((t1 == StringType || t1 == NumberType) && t2 >= ObjectType) {
114 Value p2 = v2.toPrimitive(exec);
115 return equal(exec,v1, p2);
116 }
117 if (t1 >= ObjectType && (t2 == StringType || t2 == NumberType)) {
118 Value p1 = v1.toPrimitive(exec);
119 return equal(exec,p1, v2);
120 }
121
122 return false;
123}
124
125bool KJS::strictEqual(ExecState *exec, const Value &v1, const Value &v2)
126{
127 Type t1 = v1.type();
128 Type t2 = v2.type();
129
130 if (t1 != t2)
131 return false;
132 if (t1 == UndefinedType || t1 == NullType)
133 return true;
134 if (t1 == NumberType) {
135 double n1 = v1.toNumber(exec);
136 double n2 = v2.toNumber(exec);
137 if (isNaN(n1) || isNaN(n2))
138 return false;
139 if (n1 == n2)
140 return true;
141 /* TODO: +0 and -0 */
142 return false;
143 } else if (t1 == StringType) {
144 return v1.toString(exec) == v2.toString(exec);
145 } else if (t2 == BooleanType) {
146 return v1.toBoolean(exec) == v2.toBoolean(exec);
147 }
148 if (v1.imp() == v2.imp())
149 return true;
150 /* TODO: joined objects */
151
152 return false;
153}
154
155int KJS::relation(ExecState *exec, const Value& v1, const Value& v2)
156{
157 Value p1 = v1.toPrimitive(exec,NumberType);
158 Value p2 = v2.toPrimitive(exec,NumberType);
159
160 if (p1.type() == StringType && p2.type() == StringType)
161 return p1.toString(exec) < p2.toString(exec) ? 1 : 0;
162
163 double n1 = p1.toNumber(exec);
164 double n2 = p2.toNumber(exec);
165 if ( isNaN( n1 ) || isNaN( n2 ) )
166 return -1; // means undefined
167 if (n1 == n2)
168 return 0;
169 /* TODO: +0, -0 */
170 if ( isPosInf( n1 ) )
171 return 0;
172 if ( isPosInf( n2 ) )
173 return 1;
174 if ( isNegInf( n2 ) )
175 return 0;
176 if ( isNegInf( n1 ) )
177 return 1;
178 return (n1 < n2) ? 1 : 0;
179}
180
181int KJS::maxInt(int d1, int d2)
182{
183 return (d1 > d2) ? d1 : d2;
184}
185
186int KJS::minInt(int d1, int d2)
187{
188 return (d1 < d2) ? d1 : d2;
189}
190
191// ECMA 11.6
192Value KJS::add(ExecState *exec, const Value &v1, const Value &v2, char oper)
193{
194 // exception for the Date exception in defaultValue()
195 Type preferred = oper == '+' ? UnspecifiedType : NumberType;
196 Value p1 = v1.toPrimitive(exec, preferred);
197 Value p2 = v2.toPrimitive(exec, preferred);
198
199 if ((p1.type() == StringType || p2.type() == StringType) && oper == '+') {
200 UString s1 = p1.toString(exec);
201 UString s2 = p2.toString(exec);
202
203 return String(s1 + s2);
204 }
205
206 double n1 = p1.toNumber(exec);
207 double n2 = p2.toNumber(exec);
208
209 if (oper == '+')
210 return Number(n1 + n2);
211 else
212 return Number(n1 - n2);
213}
214
215// ECMA 11.5
216Value KJS::mult(ExecState *exec, const Value &v1, const Value &v2, char oper)
217{
218 double n1 = v1.toNumber(exec);
219 double n2 = v2.toNumber(exec);
220
221 double result;
222
223 if (oper == '*')
224 result = n1 * n2;
225 else if (oper == '/')
226 result = n1 / n2;
227 else
228 result = fmod(n1, n2);
229
230 return Number(result);
231}
KJS::ExecState
Represents the current state of script execution.
Definition: interpreter.h:438
KJS::Number
Represents an primitive Number value.
Definition: value.h:367
KJS::String
Represents an primitive String value.
Definition: value.h:340
KJS::UString
Unicode string class.
Definition: ustring.h:189
KJS::Value
Value objects are act as wrappers ("smart pointers") around ValueImp objects and their descendents.
Definition: value.h:167
KJS::Value::toString
UString toString(ExecState *exec) const
Performs the ToString type conversion operation on this value (ECMA 9.8)
Definition: value.h:246
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:209
KJS::Value::toBoolean
bool toBoolean(ExecState *exec) const
Performs the ToBoolean type conversion operation on this value (ECMA 9.2)
Definition: value.h:216
KJS::Value::type
Type type() const
Returns the type of value.
Definition: value.h:195
KJS::Value::toNumber
double toNumber(ExecState *exec) const
Performs the ToNumber type conversion operation on this value (ECMA 9.3)
Definition: value.h:221

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.9.4
This website is maintained by Timothy Pearson.