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

kjs

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