1 | /*
|
---|
2 | * Copyright (C) 1999-2001 Harri Porten ([email protected])
|
---|
3 | * Copyright (C) 2001 Peter Kelly ([email protected])
|
---|
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 KJS {
|
---|
30 |
|
---|
31 | static const double D32 = 4294967296.0;
|
---|
32 |
|
---|
33 | // ECMA 9.4
|
---|
34 | double JSValue::toInteger(ExecState* exec) const
|
---|
35 | {
|
---|
36 | int32_t i;
|
---|
37 | if (getTruncatedInt32(i))
|
---|
38 | return i;
|
---|
39 | double d = toNumber(exec);
|
---|
40 | return isnan(d) ? 0.0 : trunc(d);
|
---|
41 | }
|
---|
42 |
|
---|
43 | double JSValue::toIntegerPreserveNaN(ExecState* exec) const
|
---|
44 | {
|
---|
45 | int32_t i;
|
---|
46 | if (getTruncatedInt32(i))
|
---|
47 | return i;
|
---|
48 | return trunc(toNumber(exec));
|
---|
49 | }
|
---|
50 |
|
---|
51 | int32_t JSValue::toInt32SlowCase(double d, bool& ok)
|
---|
52 | {
|
---|
53 | ok = true;
|
---|
54 |
|
---|
55 | if (d >= -D32 / 2 && d < D32 / 2)
|
---|
56 | return static_cast<int32_t>(d);
|
---|
57 |
|
---|
58 | if (isnan(d) || isinf(d)) {
|
---|
59 | ok = false;
|
---|
60 | return 0;
|
---|
61 | }
|
---|
62 |
|
---|
63 | double d32 = fmod(trunc(d), D32);
|
---|
64 | if (d32 >= D32 / 2)
|
---|
65 | d32 -= D32;
|
---|
66 | else if (d32 < -D32 / 2)
|
---|
67 | d32 += D32;
|
---|
68 | return static_cast<int32_t>(d32);
|
---|
69 | }
|
---|
70 |
|
---|
71 | int32_t JSValue::toInt32SlowCase(ExecState* exec, bool& ok) const
|
---|
72 | {
|
---|
73 | return JSValue::toInt32SlowCase(toNumber(exec), ok);
|
---|
74 | }
|
---|
75 |
|
---|
76 | uint32_t JSValue::toUInt32SlowCase(double d, bool& ok)
|
---|
77 | {
|
---|
78 | ok = true;
|
---|
79 |
|
---|
80 | if (d >= 0.0 && d < D32)
|
---|
81 | return static_cast<uint32_t>(d);
|
---|
82 |
|
---|
83 | if (isnan(d) || isinf(d)) {
|
---|
84 | ok = false;
|
---|
85 | return 0;
|
---|
86 | }
|
---|
87 |
|
---|
88 | double d32 = fmod(trunc(d), D32);
|
---|
89 | if (d32 < 0)
|
---|
90 | d32 += D32;
|
---|
91 | return static_cast<uint32_t>(d32);
|
---|
92 | }
|
---|
93 |
|
---|
94 | uint32_t JSValue::toUInt32SlowCase(ExecState* exec, bool& ok) const
|
---|
95 | {
|
---|
96 | return JSValue::toUInt32SlowCase(toNumber(exec), ok);
|
---|
97 | }
|
---|
98 |
|
---|
99 | float JSValue::toFloat(ExecState* exec) const
|
---|
100 | {
|
---|
101 | return static_cast<float>(toNumber(exec));
|
---|
102 | }
|
---|
103 |
|
---|
104 | } // namespace KJS
|
---|