1 | /*
|
---|
2 | * This file is part of the KDE libraries
|
---|
3 | * Copyright (C) 1999-2001 Harri Porten ([email protected])
|
---|
4 | * Copyright (C) 2001 Peter Kelly ([email protected])
|
---|
5 | * Copyright (C) 2003 Apple Computer, Inc.
|
---|
6 | *
|
---|
7 | * This library is free software; you can redistribute it and/or
|
---|
8 | * modify it under the terms of the GNU Library General Public
|
---|
9 | * License as published by the Free Software Foundation; either
|
---|
10 | * version 2 of the License, or (at your option) any later version.
|
---|
11 | *
|
---|
12 | * This library is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
15 | * Library General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU Library General Public License
|
---|
18 | * along with this library; see the file COPYING.LIB. If not, write to
|
---|
19 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
---|
20 | * Boston, MA 02110-1301, USA.
|
---|
21 | *
|
---|
22 | */
|
---|
23 |
|
---|
24 | #include "config.h"
|
---|
25 | #include "value.h"
|
---|
26 |
|
---|
27 | #include "error_object.h"
|
---|
28 | #include "nodes.h"
|
---|
29 | #include "operations.h"
|
---|
30 | #include <stdio.h>
|
---|
31 | #include <string.h>
|
---|
32 | #include <wtf/MathExtras.h>
|
---|
33 |
|
---|
34 | namespace KJS {
|
---|
35 |
|
---|
36 | static const double D16 = 65536.0;
|
---|
37 | static const double D32 = 4294967296.0;
|
---|
38 |
|
---|
39 | void *JSCell::operator new(size_t size)
|
---|
40 | {
|
---|
41 | return Collector::allocate(size);
|
---|
42 | }
|
---|
43 |
|
---|
44 | bool JSCell::getUInt32(unsigned&) const
|
---|
45 | {
|
---|
46 | return false;
|
---|
47 | }
|
---|
48 |
|
---|
49 | // ECMA 9.4
|
---|
50 | double JSValue::toInteger(ExecState *exec) const
|
---|
51 | {
|
---|
52 | uint32_t i;
|
---|
53 | if (getUInt32(i))
|
---|
54 | return i;
|
---|
55 | return roundValue(exec, const_cast<JSValue*>(this));
|
---|
56 | }
|
---|
57 |
|
---|
58 | inline int32_t JSValue::toInt32Inline(ExecState* exec, bool& ok) const
|
---|
59 | {
|
---|
60 | ok = true;
|
---|
61 |
|
---|
62 | uint32_t i;
|
---|
63 | if (getUInt32(i))
|
---|
64 | return i;
|
---|
65 |
|
---|
66 | double d = roundValue(exec, const_cast<JSValue*>(this));
|
---|
67 | if (isNaN(d) || isInf(d)) {
|
---|
68 | ok = false;
|
---|
69 | return 0;
|
---|
70 | }
|
---|
71 | double d32 = fmod(d, D32);
|
---|
72 |
|
---|
73 | if (d32 >= D32 / 2)
|
---|
74 | d32 -= D32;
|
---|
75 | else if (d32 < -D32 / 2)
|
---|
76 | d32 += D32;
|
---|
77 |
|
---|
78 | return static_cast<int32_t>(d32);
|
---|
79 | }
|
---|
80 |
|
---|
81 | int32_t JSValue::toInt32(ExecState* exec) const
|
---|
82 | {
|
---|
83 | bool ok;
|
---|
84 | return toInt32(exec, ok);
|
---|
85 | }
|
---|
86 |
|
---|
87 | int32_t JSValue::toInt32(ExecState* exec, bool& ok) const
|
---|
88 | {
|
---|
89 | return toInt32Inline(exec, ok);
|
---|
90 | }
|
---|
91 |
|
---|
92 | uint32_t JSValue::toUInt32(ExecState *exec) const
|
---|
93 | {
|
---|
94 | uint32_t i;
|
---|
95 | if (getUInt32(i))
|
---|
96 | return i;
|
---|
97 |
|
---|
98 | double d = roundValue(exec, const_cast<JSValue*>(this));
|
---|
99 | if (isNaN(d) || isInf(d))
|
---|
100 | return 0;
|
---|
101 | double d32 = fmod(d, D32);
|
---|
102 |
|
---|
103 | if (d32 < 0)
|
---|
104 | d32 += D32;
|
---|
105 |
|
---|
106 | return static_cast<uint32_t>(d32);
|
---|
107 | }
|
---|
108 |
|
---|
109 | uint16_t JSValue::toUInt16(ExecState *exec) const
|
---|
110 | {
|
---|
111 | uint32_t i;
|
---|
112 | if (getUInt32(i))
|
---|
113 | return static_cast<uint16_t>(i);
|
---|
114 |
|
---|
115 | double d = roundValue(exec, const_cast<JSValue*>(this));
|
---|
116 | if (isNaN(d) || isInf(d))
|
---|
117 | return 0;
|
---|
118 | double d16 = fmod(d, D16);
|
---|
119 |
|
---|
120 | if (d16 < 0)
|
---|
121 | d16 += D16;
|
---|
122 |
|
---|
123 | return static_cast<uint16_t>(d16);
|
---|
124 | }
|
---|
125 |
|
---|
126 | bool JSCell::getNumber(double &numericValue) const
|
---|
127 | {
|
---|
128 | if (!isNumber())
|
---|
129 | return false;
|
---|
130 | numericValue = static_cast<const NumberImp *>(this)->value();
|
---|
131 | return true;
|
---|
132 | }
|
---|
133 |
|
---|
134 | double JSCell::getNumber() const
|
---|
135 | {
|
---|
136 | return isNumber() ? static_cast<const NumberImp *>(this)->value() : NaN;
|
---|
137 | }
|
---|
138 |
|
---|
139 | bool JSCell::getString(UString &stringValue) const
|
---|
140 | {
|
---|
141 | if (!isString())
|
---|
142 | return false;
|
---|
143 | stringValue = static_cast<const StringImp *>(this)->value();
|
---|
144 | return true;
|
---|
145 | }
|
---|
146 |
|
---|
147 | UString JSCell::getString() const
|
---|
148 | {
|
---|
149 | return isString() ? static_cast<const StringImp *>(this)->value() : UString();
|
---|
150 | }
|
---|
151 |
|
---|
152 | JSObject *JSCell::getObject()
|
---|
153 | {
|
---|
154 | return isObject() ? static_cast<JSObject *>(this) : 0;
|
---|
155 | }
|
---|
156 |
|
---|
157 | const JSObject *JSCell::getObject() const
|
---|
158 | {
|
---|
159 | return isObject() ? static_cast<const JSObject *>(this) : 0;
|
---|
160 | }
|
---|
161 |
|
---|
162 | JSCell *jsString(const char *s)
|
---|
163 | {
|
---|
164 | return new StringImp(s ? s : "");
|
---|
165 | }
|
---|
166 |
|
---|
167 | JSCell *jsString(const UString &s)
|
---|
168 | {
|
---|
169 | return s.isNull() ? new StringImp("") : new StringImp(s);
|
---|
170 | }
|
---|
171 |
|
---|
172 | // This method includes a PIC branch to set up the NumberImp's vtable, so we quarantine
|
---|
173 | // it in a separate function to keep the normal case speedy.
|
---|
174 | JSValue *jsNumberCell(double d)
|
---|
175 | {
|
---|
176 | return new NumberImp(d);
|
---|
177 | }
|
---|
178 |
|
---|
179 | } // namespace KJS
|
---|