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 Steet, Fifth Floor,
|
---|
20 | * Boston, MA 02110-1301, USA.
|
---|
21 | *
|
---|
22 | */
|
---|
23 |
|
---|
24 | #include "value.h"
|
---|
25 |
|
---|
26 | #include "object.h"
|
---|
27 | #include "types.h"
|
---|
28 | #include "interpreter.h"
|
---|
29 |
|
---|
30 | #include <math.h>
|
---|
31 | #include <stdio.h>
|
---|
32 | #include <string.h>
|
---|
33 |
|
---|
34 | #include "internal.h"
|
---|
35 | #include "collector.h"
|
---|
36 | #include "operations.h"
|
---|
37 | #include "error_object.h"
|
---|
38 | #include "nodes.h"
|
---|
39 |
|
---|
40 | namespace KJS {
|
---|
41 |
|
---|
42 | AllocatedValueImp *ConstantValues::undefined = NULL;
|
---|
43 | AllocatedValueImp *ConstantValues::null = NULL;
|
---|
44 | AllocatedValueImp *ConstantValues::jsTrue = NULL;
|
---|
45 | AllocatedValueImp *ConstantValues::jsFalse = NULL;
|
---|
46 | AllocatedValueImp *ConstantValues::NaN = NULL;
|
---|
47 |
|
---|
48 | static const double D16 = 65536;
|
---|
49 | static const double D32 = 4294967296.0;
|
---|
50 |
|
---|
51 | void *AllocatedValueImp::operator new(size_t size)
|
---|
52 | {
|
---|
53 | return Collector::allocate(size);
|
---|
54 | }
|
---|
55 |
|
---|
56 | bool AllocatedValueImp::getUInt32(unsigned&) const
|
---|
57 | {
|
---|
58 | return false;
|
---|
59 | }
|
---|
60 |
|
---|
61 | // ECMA 9.4
|
---|
62 | double ValueImp::toInteger(ExecState *exec) const
|
---|
63 | {
|
---|
64 | uint32_t i;
|
---|
65 | if (getUInt32(i))
|
---|
66 | return i;
|
---|
67 | return roundValue(exec, const_cast<ValueImp*>(this));
|
---|
68 | }
|
---|
69 |
|
---|
70 | int32_t ValueImp::toInt32(ExecState *exec) const
|
---|
71 | {
|
---|
72 | uint32_t i;
|
---|
73 | if (getUInt32(i))
|
---|
74 | return i;
|
---|
75 |
|
---|
76 | double d = roundValue(exec, const_cast<ValueImp*>(this));
|
---|
77 | if (isNaN(d) || isInf(d))
|
---|
78 | return 0;
|
---|
79 | double d32 = fmod(d, D32);
|
---|
80 |
|
---|
81 | if (d32 >= D32 / 2)
|
---|
82 | d32 -= D32;
|
---|
83 | else if (d32 < -D32 / 2)
|
---|
84 | d32 += D32;
|
---|
85 |
|
---|
86 | return static_cast<int32_t>(d32);
|
---|
87 | }
|
---|
88 |
|
---|
89 | uint32_t ValueImp::toUInt32(ExecState *exec) const
|
---|
90 | {
|
---|
91 | uint32_t i;
|
---|
92 | if (getUInt32(i))
|
---|
93 | return i;
|
---|
94 |
|
---|
95 | double d = roundValue(exec, const_cast<ValueImp*>(this));
|
---|
96 | if (isNaN(d) || isInf(d))
|
---|
97 | return 0;
|
---|
98 | double d32 = fmod(d, D32);
|
---|
99 |
|
---|
100 | if (d32 < 0)
|
---|
101 | d32 += D32;
|
---|
102 |
|
---|
103 | return static_cast<uint32_t>(d32);
|
---|
104 | }
|
---|
105 |
|
---|
106 | uint16_t ValueImp::toUInt16(ExecState *exec) const
|
---|
107 | {
|
---|
108 | uint32_t i;
|
---|
109 | if (getUInt32(i))
|
---|
110 | return i;
|
---|
111 |
|
---|
112 | double d = roundValue(exec, const_cast<ValueImp*>(this));
|
---|
113 | if (isNaN(d) || isInf(d))
|
---|
114 | return 0;
|
---|
115 | double d16 = fmod(d, D16);
|
---|
116 |
|
---|
117 | if (d16 < 0)
|
---|
118 | d16 += D16;
|
---|
119 |
|
---|
120 | return static_cast<uint16_t>(d16);
|
---|
121 | }
|
---|
122 |
|
---|
123 | ObjectImp *ValueImp::toObject(ExecState *exec) const
|
---|
124 | {
|
---|
125 | if (SimpleNumber::is(this))
|
---|
126 | return static_cast<const NumberImp *>(this)->NumberImp::toObject(exec);
|
---|
127 | return downcast()->toObject(exec);
|
---|
128 | }
|
---|
129 |
|
---|
130 | bool AllocatedValueImp::getBoolean(bool &booleanValue) const
|
---|
131 | {
|
---|
132 | if (!isBoolean())
|
---|
133 | return false;
|
---|
134 | booleanValue = static_cast<const BooleanImp *>(this)->value();
|
---|
135 | return true;
|
---|
136 | }
|
---|
137 |
|
---|
138 | bool AllocatedValueImp::getNumber(double &numericValue) const
|
---|
139 | {
|
---|
140 | if (!isNumber())
|
---|
141 | return false;
|
---|
142 | numericValue = static_cast<const NumberImp *>(this)->value();
|
---|
143 | return true;
|
---|
144 | }
|
---|
145 |
|
---|
146 | double AllocatedValueImp::getNumber() const
|
---|
147 | {
|
---|
148 | return isNumber() ? static_cast<const NumberImp *>(this)->value() : NaN;
|
---|
149 | }
|
---|
150 |
|
---|
151 | bool AllocatedValueImp::getString(UString &stringValue) const
|
---|
152 | {
|
---|
153 | if (!isString())
|
---|
154 | return false;
|
---|
155 | stringValue = static_cast<const StringImp *>(this)->value();
|
---|
156 | return true;
|
---|
157 | }
|
---|
158 |
|
---|
159 | UString AllocatedValueImp::getString() const
|
---|
160 | {
|
---|
161 | return isString() ? static_cast<const StringImp *>(this)->value() : UString();
|
---|
162 | }
|
---|
163 |
|
---|
164 | ObjectImp *AllocatedValueImp::getObject()
|
---|
165 | {
|
---|
166 | return isObject() ? static_cast<ObjectImp *>(this) : 0;
|
---|
167 | }
|
---|
168 |
|
---|
169 | const ObjectImp *AllocatedValueImp::getObject() const
|
---|
170 | {
|
---|
171 | return isObject() ? static_cast<const ObjectImp *>(this) : 0;
|
---|
172 | }
|
---|
173 |
|
---|
174 | AllocatedValueImp *jsString(const char *s)
|
---|
175 | {
|
---|
176 | return new StringImp(s ? s : "");
|
---|
177 | }
|
---|
178 |
|
---|
179 | AllocatedValueImp *jsString(const UString &s)
|
---|
180 | {
|
---|
181 | return s.isNull() ? new StringImp("") : new StringImp(s);
|
---|
182 | }
|
---|
183 |
|
---|
184 | ValueImp *jsNumber(int i)
|
---|
185 | {
|
---|
186 | return SimpleNumber::fits(i) ? SimpleNumber::make(i) : new NumberImp(static_cast<double>(i));
|
---|
187 | }
|
---|
188 |
|
---|
189 | ValueImp *jsNumber(unsigned i)
|
---|
190 | {
|
---|
191 | return SimpleNumber::fits(i) ? SimpleNumber::make(i) : new NumberImp(static_cast<double>(i));
|
---|
192 | }
|
---|
193 |
|
---|
194 | ValueImp *jsNumber(long i)
|
---|
195 | {
|
---|
196 | return SimpleNumber::fits(i) ? SimpleNumber::make(i) : new NumberImp(static_cast<double>(i));
|
---|
197 | }
|
---|
198 |
|
---|
199 | ValueImp *jsNumber(unsigned long i)
|
---|
200 | {
|
---|
201 | return SimpleNumber::fits(i) ? SimpleNumber::make(i) : new NumberImp(static_cast<double>(i));
|
---|
202 | }
|
---|
203 |
|
---|
204 | ValueImp *jsNumber(double d)
|
---|
205 | {
|
---|
206 | return SimpleNumber::fits(d)
|
---|
207 | ? SimpleNumber::make(static_cast<long>(d))
|
---|
208 | : (isNaN(d) ? jsNaN() : new NumberImp(d));
|
---|
209 | }
|
---|
210 |
|
---|
211 | ValueImp *jsNumber(double d, bool knownToBeInteger)
|
---|
212 | {
|
---|
213 | return (knownToBeInteger ? SimpleNumber::integerFits(d) : SimpleNumber::fits(d))
|
---|
214 | ? SimpleNumber::make(static_cast<long>(d))
|
---|
215 | : ((!knownToBeInteger && isNaN(d)) ? jsNaN() : new NumberImp(d));
|
---|
216 | }
|
---|
217 |
|
---|
218 | void ConstantValues::init()
|
---|
219 | {
|
---|
220 | undefined = new UndefinedImp();
|
---|
221 | null = new NullImp();
|
---|
222 | jsTrue = new BooleanImp(true);
|
---|
223 | jsFalse = new BooleanImp(false);
|
---|
224 | NaN = new NumberImp(::KJS::NaN);
|
---|
225 | }
|
---|
226 |
|
---|
227 | void ConstantValues::clear()
|
---|
228 | {
|
---|
229 | undefined = NULL;
|
---|
230 | null = NULL;
|
---|
231 | jsTrue = NULL;
|
---|
232 | jsFalse = NULL;
|
---|
233 | NaN = NULL;
|
---|
234 | }
|
---|
235 |
|
---|
236 | void ConstantValues::mark()
|
---|
237 | {
|
---|
238 | if (AllocatedValueImp *v = undefined)
|
---|
239 | if (!v->marked())
|
---|
240 | v->mark();
|
---|
241 | if (AllocatedValueImp *v = null)
|
---|
242 | if (!v->marked())
|
---|
243 | v->mark();
|
---|
244 | if (AllocatedValueImp *v = jsTrue)
|
---|
245 | if (!v->marked())
|
---|
246 | v->mark();
|
---|
247 | if (AllocatedValueImp *v = jsFalse)
|
---|
248 | if (!v->marked())
|
---|
249 | v->mark();
|
---|
250 | if (AllocatedValueImp *v = NaN)
|
---|
251 | if (!v->marked())
|
---|
252 | v->mark();
|
---|
253 | }
|
---|
254 |
|
---|
255 | }
|
---|