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

kjs

  • kjs
math_object.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 Lesser 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  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  */
20 
21 #include <math.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <assert.h>
25 #include <time.h>
26 
27 #include "value.h"
28 #include "object.h"
29 #include "types.h"
30 #include "interpreter.h"
31 #include "operations.h"
32 #include "math_object.h"
33 
34 #include "math_object.lut.h"
35 
36 #ifndef M_PI
37 #define M_PI 3.14159265358979323846
38 #endif /* M_PI */
39 
40 #ifndef signbit
41 #define signbit(x) ((x) < 0.0 || IS_NEGATIVE_ZERO(x))
42 #endif
43 
44 using namespace KJS;
45 
46 // ------------------------------ MathObjectImp --------------------------------
47 
48 const ClassInfo MathObjectImp::info = { "Math", 0, &mathTable, 0 };
49 
50 /* Source for math_object.lut.h
51 @begin mathTable 31
52  E MathObjectImp::Euler DontEnum|DontDelete|ReadOnly
53  LN2 MathObjectImp::Ln2 DontEnum|DontDelete|ReadOnly
54  LN10 MathObjectImp::Ln10 DontEnum|DontDelete|ReadOnly
55  LOG2E MathObjectImp::Log2E DontEnum|DontDelete|ReadOnly
56  LOG10E MathObjectImp::Log10E DontEnum|DontDelete|ReadOnly
57  PI MathObjectImp::Pi DontEnum|DontDelete|ReadOnly
58  SQRT1_2 MathObjectImp::Sqrt1_2 DontEnum|DontDelete|ReadOnly
59  SQRT2 MathObjectImp::Sqrt2 DontEnum|DontDelete|ReadOnly
60  abs MathObjectImp::Abs DontEnum|Function 1
61  acos MathObjectImp::ACos DontEnum|Function 1
62  asin MathObjectImp::ASin DontEnum|Function 1
63  atan MathObjectImp::ATan DontEnum|Function 1
64  atan2 MathObjectImp::ATan2 DontEnum|Function 2
65  ceil MathObjectImp::Ceil DontEnum|Function 1
66  cos MathObjectImp::Cos DontEnum|Function 1
67  exp MathObjectImp::Exp DontEnum|Function 1
68  floor MathObjectImp::Floor DontEnum|Function 1
69  log MathObjectImp::Log DontEnum|Function 1
70  max MathObjectImp::Max DontEnum|Function 2
71  min MathObjectImp::Min DontEnum|Function 2
72  pow MathObjectImp::Pow DontEnum|Function 2
73  random MathObjectImp::Random DontEnum|Function 0
74  round MathObjectImp::Round DontEnum|Function 1
75  sin MathObjectImp::Sin DontEnum|Function 1
76  sqrt MathObjectImp::Sqrt DontEnum|Function 1
77  tan MathObjectImp::Tan DontEnum|Function 1
78 @end
79 */
80 
81 MathObjectImp::MathObjectImp(ExecState * /*exec*/,
82  ObjectPrototypeImp *objProto)
83  : ObjectImp(objProto)
84 {
85  unsigned int seed = time(NULL);
86  ::srand(seed);
87 }
88 
89 // ECMA 15.8
90 Value MathObjectImp::get(ExecState *exec, const Identifier &propertyName) const
91 {
92  return lookupGet<MathFuncImp, MathObjectImp, ObjectImp>( exec, propertyName, &mathTable, this );
93 }
94 
95 Value MathObjectImp::getValueProperty(ExecState *, int token) const
96 {
97  double d = -42; // ;)
98  switch (token) {
99  case Euler:
100  d = exp(1.0);
101  break;
102  case Ln2:
103  d = log(2.0);
104  break;
105  case Ln10:
106  d = log(10.0);
107  break;
108  case Log2E:
109  d = 1.0/log(2.0);
110  break;
111  case Log10E:
112  d = 1.0/log(10.0);
113  break;
114  case Pi:
115  d = M_PI;
116  break;
117  case Sqrt1_2:
118  d = sqrt(0.5);
119  break;
120  case Sqrt2:
121  d = sqrt(2.0);
122  break;
123  default:
124  fprintf( stderr, "[math_object] Internal error in MathObjectImp: unhandled token %d\n", token );
125  break;
126  }
127 
128  return Number(d);
129 }
130 
131 // ------------------------------ MathObjectImp --------------------------------
132 
133 MathFuncImp::MathFuncImp(ExecState *exec, int i, int l)
134  : InternalFunctionImp(
135  static_cast<FunctionPrototypeImp*>(exec->lexicalInterpreter()->builtinFunctionPrototype().imp())
136  ), id(i)
137 {
138  Value protect(this);
139  putDirect(lengthPropertyName, l, DontDelete|ReadOnly|DontEnum);
140 }
141 
142 bool MathFuncImp::implementsCall() const
143 {
144  return true;
145 }
146 
147 Value MathFuncImp::call(ExecState *exec, Object &/*thisObj*/, const List &args)
148 {
149  double arg = args[0].toNumber(exec);
150  double arg2 = args[1].toNumber(exec);
151  double result;
152 
153  switch (id) {
154  case MathObjectImp::Abs:
155  result = ( arg < 0 || arg == -0) ? (-arg) : arg;
156  break;
157  case MathObjectImp::ACos:
158  result = ::acos(arg);
159  break;
160  case MathObjectImp::ASin:
161  result = ::asin(arg);
162  break;
163  case MathObjectImp::ATan:
164  result = ::atan(arg);
165  break;
166  case MathObjectImp::ATan2:
167  result = ::atan2(arg, arg2);
168  break;
169  case MathObjectImp::Ceil:
170  result = ::ceil(arg);
171  break;
172  case MathObjectImp::Cos:
173  result = ::cos(arg);
174  break;
175  case MathObjectImp::Exp:
176  result = ::exp(arg);
177  break;
178  case MathObjectImp::Floor:
179  result = ::floor(arg);
180  break;
181  case MathObjectImp::Log:
182  result = ::log(arg);
183  break;
184  case MathObjectImp::Max: {
185  unsigned int argsCount = args.size();
186  result = -Inf;
187  for ( unsigned int k = 0 ; k < argsCount ; ++k ) {
188  double val = args[k].toNumber(exec);
189  if ( isNaN( val ) )
190  {
191  result = NaN;
192  break;
193  }
194  if ( val > result || (val == 0 && result == 0 && !signbit(val)) )
195  result = val;
196  }
197  break;
198  }
199  case MathObjectImp::Min: {
200  unsigned int argsCount = args.size();
201  result = +Inf;
202  for ( unsigned int k = 0 ; k < argsCount ; ++k ) {
203  double val = args[k].toNumber(exec);
204  if ( isNaN( val ) )
205  {
206  result = NaN;
207  break;
208  }
209  if ( val < result || (val == 0 && result == 0 && signbit(val)) )
210  result = val;
211  }
212  break;
213  }
214  case MathObjectImp::Pow:
215  // ECMA 15.8.2.1.13 (::pow takes care of most of the critera)
216  if (KJS::isNaN(arg2))
217  result = NaN;
218 #ifndef APPLE_CHANGES
219  else if (arg2 == 0)
220  result = 1;
221 #endif
222  else if (KJS::isNaN(arg) && arg2 != 0)
223  result = NaN;
224 #ifndef APPLE_CHANGES
225  else if (::fabs(arg) > 1 && KJS::isPosInf(arg2))
226  result = Inf;
227  else if (::fabs(arg) > 1 && KJS::isNegInf(arg2))
228  result = +0;
229 #endif
230  else if (::fabs(arg) == 1 && KJS::isInf(arg2))
231  result = NaN;
232 #ifndef APPLE_CHANGES
233  else if (::fabs(arg) < 1 && KJS::isPosInf(arg2))
234  result = +0;
235  else if (::fabs(arg) < 1 && KJS::isNegInf(arg2))
236  result = Inf;
237 #endif
238  else
239  result = ::pow(arg, arg2);
240  break;
241  case MathObjectImp::Random:
242  result = ::rand();
243  result = result / RAND_MAX;
244  break;
245  case MathObjectImp::Round:
246  if (signbit(arg) && arg >= -0.5)
247  result = -0.0;
248  else
249  result = ::floor(arg + 0.5);
250  break;
251  case MathObjectImp::Sin:
252  result = ::sin(arg);
253  break;
254  case MathObjectImp::Sqrt:
255  result = ::sqrt(arg);
256  break;
257  case MathObjectImp::Tan:
258  result = ::tan(arg);
259  break;
260 
261  default:
262  result = 0.0;
263  assert(0);
264  }
265 
266  return Number(result);
267 }
KJS::Value
Value objects are act as wrappers ("smart pointers") around ValueImp objects and their descendents...
Definition: value.h:167
KJS::InternalFunctionImp
Base class for all function objects.
Definition: function.h:40
KJS::Number
Represents an primitive Number value.
Definition: value.h:367
KJS::FunctionPrototypeImp
The initial value of Function.prototype (and thus all objects created with the Function constructor) ...
Definition: function_object.h:34
KJS::Object
Represents an Object.
Definition: object.h:81
KJS
Definition: array_instance.h:27
KJS::List::size
int size() const
Definition: list.h:90
KJS::List
Native list type.
Definition: list.h:48
KJS::ClassInfo
Class Information.
Definition: object.h:58
KJS::ExecState
Represents the current state of script execution.
Definition: interpreter.h:438
KJS::Identifier
Represents an Identifier for a Javascript object.
Definition: identifier.h:32

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
  •     tdecore
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  • tdeioslave
  •   http
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for kjs by doxygen 1.8.8
This website is maintained by Timothy Pearson.