• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
3  *  Copyright (C) 2001 Peter Kelly (pmk@post.com)
4  *  Copyright (C) 2003, 2007, 2008 Apple Inc. All rights reserved.
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 #include "config.h"
24 #include "JSValue.h"
25 
26 #include "JSFunction.h"
27 #include <wtf/MathExtras.h>
28 
29 namespace JSC {
30 
31 static const double D32 = 4294967296.0;
32 
33 // ECMA 9.4
toInteger(ExecState * exec) const34 double JSValuePtr::toInteger(ExecState* exec) const
35 {
36     if (isInt32Fast())
37         return getInt32Fast();
38     double d = toNumber(exec);
39     return isnan(d) ? 0.0 : trunc(d);
40 }
41 
toIntegerPreserveNaN(ExecState * exec) const42 double JSValuePtr::toIntegerPreserveNaN(ExecState* exec) const
43 {
44     if (isInt32Fast())
45         return getInt32Fast();
46     return trunc(toNumber(exec));
47 }
48 
toInt32SlowCase(double d,bool & ok)49 int32_t toInt32SlowCase(double d, bool& ok)
50 {
51     ok = true;
52 
53     if (d >= -D32 / 2 && d < D32 / 2)
54         return static_cast<int32_t>(d);
55 
56     if (isnan(d) || isinf(d)) {
57         ok = false;
58         return 0;
59     }
60 
61     double d32 = fmod(trunc(d), D32);
62     if (d32 >= D32 / 2)
63         d32 -= D32;
64     else if (d32 < -D32 / 2)
65         d32 += D32;
66     return static_cast<int32_t>(d32);
67 }
68 
toUInt32SlowCase(double d,bool & ok)69 uint32_t toUInt32SlowCase(double d, bool& ok)
70 {
71     ok = true;
72 
73     if (d >= 0.0 && d < D32)
74         return static_cast<uint32_t>(d);
75 
76     if (isnan(d) || isinf(d)) {
77         ok = false;
78         return 0;
79     }
80 
81     double d32 = fmod(trunc(d), D32);
82     if (d32 < 0)
83         d32 += D32;
84     return static_cast<uint32_t>(d32);
85 }
86 
87 } // namespace JSC
88