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-2005 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 | #ifndef KJS_VALUE_H
|
---|
25 | #define KJS_VALUE_H
|
---|
26 |
|
---|
27 | #ifndef NDEBUG // protection against problems if committing with KJS_VERBOSE on
|
---|
28 |
|
---|
29 | // Uncomment this to enable very verbose output from KJS
|
---|
30 | //#define KJS_VERBOSE
|
---|
31 | // Uncomment this to debug memory allocation and garbage collection
|
---|
32 | //#define KJS_DEBUG_MEM
|
---|
33 |
|
---|
34 | #endif
|
---|
35 |
|
---|
36 | #include <assert.h>
|
---|
37 | #include <stdlib.h> // for size_t
|
---|
38 | #include "simple_number.h"
|
---|
39 | #include "ustring.h"
|
---|
40 |
|
---|
41 | namespace KJS {
|
---|
42 |
|
---|
43 | class ClassInfo;
|
---|
44 | class ExecState;
|
---|
45 | class ObjectImp;
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Primitive types
|
---|
49 | */
|
---|
50 | enum Type {
|
---|
51 | UnspecifiedType = 0,
|
---|
52 | UndefinedType = 1,
|
---|
53 | NullType = 2,
|
---|
54 | BooleanType = 3,
|
---|
55 | StringType = 4,
|
---|
56 | NumberType = 5,
|
---|
57 | ObjectType = 6
|
---|
58 | };
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * ValueImp is the base type for all primitives (Undefined, Null, Boolean,
|
---|
62 | * String, Number) and objects in ECMAScript.
|
---|
63 | *
|
---|
64 | * Note: you should never inherit from ValueImp as it is for primitive types
|
---|
65 | * only (all of which are provided internally by KJS). Instead, inherit from
|
---|
66 | * ObjectImp.
|
---|
67 | */
|
---|
68 | class ValueImp {
|
---|
69 | friend class AllocatedValueImp; // so it can derive from this class
|
---|
70 | friend class ProtectedValues; // so it can call downcast()
|
---|
71 |
|
---|
72 | private:
|
---|
73 | ValueImp();
|
---|
74 | ~ValueImp();
|
---|
75 |
|
---|
76 | public:
|
---|
77 | // Querying the type.
|
---|
78 | Type type() const;
|
---|
79 | bool isUndefined() const;
|
---|
80 | bool isNull() const;
|
---|
81 | bool isUndefinedOrNull() const;
|
---|
82 | bool isBoolean() const;
|
---|
83 | bool isNumber() const;
|
---|
84 | bool isString() const;
|
---|
85 | bool isObject() const;
|
---|
86 | bool isObject(const ClassInfo *) const;
|
---|
87 |
|
---|
88 | // Extracting the value.
|
---|
89 | bool getBoolean(bool&) const;
|
---|
90 | bool getNumber(double&) const;
|
---|
91 | double getNumber() const; // NaN if not a number
|
---|
92 | bool getString(UString&) const;
|
---|
93 | UString getString() const; // null string if not a string
|
---|
94 | ObjectImp *getObject(); // NULL if not an object
|
---|
95 | const ObjectImp *getObject() const; // NULL if not an object
|
---|
96 |
|
---|
97 | // Extracting integer values.
|
---|
98 | bool getUInt32(uint32_t&) const;
|
---|
99 |
|
---|
100 | // Basic conversions.
|
---|
101 | ValueImp *toPrimitive(ExecState *exec, Type preferredType = UnspecifiedType) const;
|
---|
102 | bool toBoolean(ExecState *exec) const;
|
---|
103 | double toNumber(ExecState *exec) const;
|
---|
104 | double toNumber(ExecState *exec, bool& knownToBeInteger) const;
|
---|
105 | UString toString(ExecState *exec) const;
|
---|
106 | ObjectImp *toObject(ExecState *exec) const;
|
---|
107 |
|
---|
108 | // Integer conversions.
|
---|
109 | double toInteger(ExecState *exec) const;
|
---|
110 | int32_t toInt32(ExecState *exec) const;
|
---|
111 | uint32_t toUInt32(ExecState *exec) const;
|
---|
112 | uint16_t toUInt16(ExecState *exec) const;
|
---|
113 |
|
---|
114 | // Garbage collection.
|
---|
115 | void mark();
|
---|
116 | bool marked() const;
|
---|
117 |
|
---|
118 | private:
|
---|
119 | // Implementation details.
|
---|
120 | AllocatedValueImp *downcast();
|
---|
121 | const AllocatedValueImp *downcast() const;
|
---|
122 |
|
---|
123 | // Give a compile time error if we try to copy one of these.
|
---|
124 | ValueImp(const ValueImp&);
|
---|
125 | ValueImp& operator=(const ValueImp&);
|
---|
126 | };
|
---|
127 |
|
---|
128 | class AllocatedValueImp : public ValueImp {
|
---|
129 | friend class Collector;
|
---|
130 | friend class UndefinedImp;
|
---|
131 | friend class NullImp;
|
---|
132 | friend class BooleanImp;
|
---|
133 | friend class NumberImp;
|
---|
134 | friend class StringImp;
|
---|
135 | friend class ObjectImp;
|
---|
136 | private:
|
---|
137 | AllocatedValueImp();
|
---|
138 | virtual ~AllocatedValueImp();
|
---|
139 | public:
|
---|
140 | // Querying the type.
|
---|
141 | virtual Type type() const = 0;
|
---|
142 | bool isBoolean() const;
|
---|
143 | bool isNumber() const;
|
---|
144 | bool isString() const;
|
---|
145 | bool isObject() const;
|
---|
146 | bool isObject(const ClassInfo *) const;
|
---|
147 |
|
---|
148 | // Extracting the value.
|
---|
149 | bool getBoolean(bool&) const;
|
---|
150 | bool getNumber(double&) const;
|
---|
151 | double getNumber() const; // NaN if not a number
|
---|
152 | bool getString(UString&) const;
|
---|
153 | UString getString() const; // null string if not a string
|
---|
154 | ObjectImp *getObject(); // NULL if not an object
|
---|
155 | const ObjectImp *getObject() const; // NULL if not an object
|
---|
156 |
|
---|
157 | // Extracting integer values.
|
---|
158 | virtual bool getUInt32(uint32_t&) const;
|
---|
159 |
|
---|
160 | // Basic conversions.
|
---|
161 | virtual ValueImp *toPrimitive(ExecState *exec, Type preferredType = UnspecifiedType) const = 0;
|
---|
162 | virtual bool toBoolean(ExecState *exec) const = 0;
|
---|
163 | virtual double toNumber(ExecState *exec) const = 0;
|
---|
164 | double toNumber(ExecState *exec, bool& knownToBeInteger) const;
|
---|
165 | virtual UString toString(ExecState *exec) const = 0;
|
---|
166 | virtual ObjectImp *toObject(ExecState *exec) const = 0;
|
---|
167 |
|
---|
168 | // Garbage collection.
|
---|
169 | void *operator new(size_t);
|
---|
170 | virtual void mark();
|
---|
171 | bool marked() const;
|
---|
172 |
|
---|
173 | private:
|
---|
174 | bool m_marked;
|
---|
175 | };
|
---|
176 |
|
---|
177 | AllocatedValueImp *jsUndefined();
|
---|
178 | AllocatedValueImp *jsNull();
|
---|
179 |
|
---|
180 | AllocatedValueImp *jsBoolean(bool = false);
|
---|
181 |
|
---|
182 | ValueImp *jsNumber(double);
|
---|
183 | ValueImp *jsNumber(double, bool knownToBeInteger);
|
---|
184 | AllocatedValueImp *jsNaN();
|
---|
185 | ValueImp *jsZero();
|
---|
186 | ValueImp *jsOne();
|
---|
187 | ValueImp *jsTwo();
|
---|
188 | ValueImp *jsNumber(int);
|
---|
189 | ValueImp *jsNumber(unsigned);
|
---|
190 | ValueImp *jsNumber(long);
|
---|
191 | ValueImp *jsNumber(unsigned long);
|
---|
192 |
|
---|
193 | AllocatedValueImp *jsString(const UString &); // returns empty string if passed null string
|
---|
194 | AllocatedValueImp *jsString(const char * = ""); // returns empty string if passed 0
|
---|
195 |
|
---|
196 | extern const double NaN;
|
---|
197 | extern const double Inf;
|
---|
198 |
|
---|
199 | class ConstantValues {
|
---|
200 | public:
|
---|
201 | static AllocatedValueImp *undefined;
|
---|
202 | static AllocatedValueImp *null;
|
---|
203 | static AllocatedValueImp *jsFalse;
|
---|
204 | static AllocatedValueImp *jsTrue;
|
---|
205 | static AllocatedValueImp *NaN;
|
---|
206 |
|
---|
207 | static void init();
|
---|
208 | static void clear();
|
---|
209 | static void mark();
|
---|
210 | };
|
---|
211 |
|
---|
212 | inline AllocatedValueImp *jsUndefined()
|
---|
213 | {
|
---|
214 | return ConstantValues::undefined;
|
---|
215 | }
|
---|
216 |
|
---|
217 | inline AllocatedValueImp *jsNull()
|
---|
218 | {
|
---|
219 | return ConstantValues::null;
|
---|
220 | }
|
---|
221 |
|
---|
222 | inline AllocatedValueImp *jsBoolean(bool b)
|
---|
223 | {
|
---|
224 | return b ? ConstantValues::jsTrue : ConstantValues::jsFalse;
|
---|
225 | }
|
---|
226 |
|
---|
227 | inline AllocatedValueImp *jsNaN()
|
---|
228 | {
|
---|
229 | return ConstantValues::NaN;
|
---|
230 | }
|
---|
231 |
|
---|
232 | inline ValueImp::ValueImp()
|
---|
233 | {
|
---|
234 | }
|
---|
235 |
|
---|
236 | inline ValueImp::~ValueImp()
|
---|
237 | {
|
---|
238 | }
|
---|
239 |
|
---|
240 | inline AllocatedValueImp::AllocatedValueImp()
|
---|
241 | : m_marked(false)
|
---|
242 | {
|
---|
243 | }
|
---|
244 |
|
---|
245 | inline AllocatedValueImp::~AllocatedValueImp()
|
---|
246 | {
|
---|
247 | }
|
---|
248 |
|
---|
249 | inline bool AllocatedValueImp::isBoolean() const
|
---|
250 | {
|
---|
251 | return type() == BooleanType;
|
---|
252 | }
|
---|
253 |
|
---|
254 | inline bool AllocatedValueImp::isNumber() const
|
---|
255 | {
|
---|
256 | return type() == NumberType;
|
---|
257 | }
|
---|
258 |
|
---|
259 | inline bool AllocatedValueImp::isString() const
|
---|
260 | {
|
---|
261 | return type() == StringType;
|
---|
262 | }
|
---|
263 |
|
---|
264 | inline bool AllocatedValueImp::isObject() const
|
---|
265 | {
|
---|
266 | return type() == ObjectType;
|
---|
267 | }
|
---|
268 |
|
---|
269 | inline bool AllocatedValueImp::marked() const
|
---|
270 | {
|
---|
271 | return m_marked;
|
---|
272 | }
|
---|
273 |
|
---|
274 | inline void AllocatedValueImp::mark()
|
---|
275 | {
|
---|
276 | m_marked = true;
|
---|
277 | }
|
---|
278 |
|
---|
279 | inline AllocatedValueImp *ValueImp::downcast()
|
---|
280 | {
|
---|
281 | assert(!SimpleNumber::is(this));
|
---|
282 | return static_cast<AllocatedValueImp *>(this);
|
---|
283 | }
|
---|
284 |
|
---|
285 | inline const AllocatedValueImp *ValueImp::downcast() const
|
---|
286 | {
|
---|
287 | assert(!SimpleNumber::is(this));
|
---|
288 | return static_cast<const AllocatedValueImp *>(this);
|
---|
289 | }
|
---|
290 |
|
---|
291 | inline bool ValueImp::isUndefined() const
|
---|
292 | {
|
---|
293 | return this == jsUndefined();
|
---|
294 | }
|
---|
295 |
|
---|
296 | inline bool ValueImp::isNull() const
|
---|
297 | {
|
---|
298 | return this == jsNull();
|
---|
299 | }
|
---|
300 |
|
---|
301 | inline bool ValueImp::isUndefinedOrNull() const
|
---|
302 | {
|
---|
303 | return this == jsUndefined() || this == jsNull();
|
---|
304 | }
|
---|
305 |
|
---|
306 | inline bool ValueImp::isBoolean() const
|
---|
307 | {
|
---|
308 | return !SimpleNumber::is(this) && downcast()->isBoolean();
|
---|
309 | }
|
---|
310 |
|
---|
311 | inline bool ValueImp::isNumber() const
|
---|
312 | {
|
---|
313 | return SimpleNumber::is(this) || downcast()->isNumber();
|
---|
314 | }
|
---|
315 |
|
---|
316 | inline bool ValueImp::isString() const
|
---|
317 | {
|
---|
318 | return !SimpleNumber::is(this) && downcast()->isString();
|
---|
319 | }
|
---|
320 |
|
---|
321 | inline bool ValueImp::isObject() const
|
---|
322 | {
|
---|
323 | return !SimpleNumber::is(this) && downcast()->isObject();
|
---|
324 | }
|
---|
325 |
|
---|
326 | inline bool ValueImp::isObject(const ClassInfo *c) const
|
---|
327 | {
|
---|
328 | return !SimpleNumber::is(this) && downcast()->isObject(c);
|
---|
329 | }
|
---|
330 |
|
---|
331 | inline bool ValueImp::getBoolean(bool& v) const
|
---|
332 | {
|
---|
333 | return !SimpleNumber::is(this) && downcast()->getBoolean(v);
|
---|
334 | }
|
---|
335 |
|
---|
336 | inline bool ValueImp::getNumber(double& v) const
|
---|
337 | {
|
---|
338 | if (SimpleNumber::is(this)) {
|
---|
339 | v = SimpleNumber::value(this);
|
---|
340 | return true;
|
---|
341 | }
|
---|
342 | return downcast()->getNumber(v);
|
---|
343 | }
|
---|
344 |
|
---|
345 | inline double ValueImp::getNumber() const
|
---|
346 | {
|
---|
347 | return SimpleNumber::is(this) ? SimpleNumber::value(this) : downcast()->getNumber();
|
---|
348 | }
|
---|
349 |
|
---|
350 | inline bool ValueImp::getString(UString& s) const
|
---|
351 | {
|
---|
352 | return !SimpleNumber::is(this) && downcast()->getString(s);
|
---|
353 | }
|
---|
354 |
|
---|
355 | inline UString ValueImp::getString() const
|
---|
356 | {
|
---|
357 | return SimpleNumber::is(this) ? UString() : downcast()->getString();
|
---|
358 | }
|
---|
359 |
|
---|
360 | inline ObjectImp *ValueImp::getObject()
|
---|
361 | {
|
---|
362 | return SimpleNumber::is(this) ? 0 : downcast()->getObject();
|
---|
363 | }
|
---|
364 |
|
---|
365 | inline const ObjectImp *ValueImp::getObject() const
|
---|
366 | {
|
---|
367 | return SimpleNumber::is(this) ? 0 : downcast()->getObject();
|
---|
368 | }
|
---|
369 |
|
---|
370 | inline bool ValueImp::getUInt32(uint32_t& v) const
|
---|
371 | {
|
---|
372 | if (SimpleNumber::is(this)) {
|
---|
373 | long i = SimpleNumber::value(this);
|
---|
374 | if (i < 0)
|
---|
375 | return false;
|
---|
376 | v = i;
|
---|
377 | return true;
|
---|
378 | }
|
---|
379 | return downcast()->getUInt32(v);
|
---|
380 | }
|
---|
381 |
|
---|
382 | inline void ValueImp::mark()
|
---|
383 | {
|
---|
384 | if (!SimpleNumber::is(this))
|
---|
385 | downcast()->mark();
|
---|
386 | }
|
---|
387 |
|
---|
388 | inline bool ValueImp::marked() const
|
---|
389 | {
|
---|
390 | return SimpleNumber::is(this) || downcast()->marked();
|
---|
391 | }
|
---|
392 |
|
---|
393 | inline Type ValueImp::type() const
|
---|
394 | {
|
---|
395 | return SimpleNumber::is(this) ? NumberType : downcast()->type();
|
---|
396 | }
|
---|
397 |
|
---|
398 | inline ValueImp *ValueImp::toPrimitive(ExecState *exec, Type preferredType) const
|
---|
399 | {
|
---|
400 | return SimpleNumber::is(this) ? const_cast<ValueImp *>(this) : downcast()->toPrimitive(exec, preferredType);
|
---|
401 | }
|
---|
402 |
|
---|
403 | inline bool ValueImp::toBoolean(ExecState *exec) const
|
---|
404 | {
|
---|
405 | return SimpleNumber::is(this) ? SimpleNumber::value(this) : downcast()->toBoolean(exec);
|
---|
406 | }
|
---|
407 |
|
---|
408 | inline double ValueImp::toNumber(ExecState *exec) const
|
---|
409 | {
|
---|
410 | return SimpleNumber::is(this) ? SimpleNumber::value(this) : downcast()->toNumber(exec);
|
---|
411 | }
|
---|
412 |
|
---|
413 | inline double ValueImp::toNumber(ExecState *exec, bool& knownToBeInteger) const
|
---|
414 | {
|
---|
415 | if (SimpleNumber::is(this)) {
|
---|
416 | knownToBeInteger = true;
|
---|
417 | return SimpleNumber::value(this);
|
---|
418 | }
|
---|
419 | knownToBeInteger = false;
|
---|
420 | return downcast()->toNumber(exec);
|
---|
421 | }
|
---|
422 |
|
---|
423 | inline UString ValueImp::toString(ExecState *exec) const
|
---|
424 | {
|
---|
425 | return SimpleNumber::is(this) ? UString::from(SimpleNumber::value(this)) : downcast()->toString(exec);
|
---|
426 | }
|
---|
427 |
|
---|
428 | inline ValueImp *jsZero()
|
---|
429 | {
|
---|
430 | return SimpleNumber::make(0);
|
---|
431 | }
|
---|
432 |
|
---|
433 | inline ValueImp *jsOne()
|
---|
434 | {
|
---|
435 | return SimpleNumber::make(1);
|
---|
436 | }
|
---|
437 |
|
---|
438 | inline ValueImp *jsTwo()
|
---|
439 | {
|
---|
440 | return SimpleNumber::make(2);
|
---|
441 | }
|
---|
442 |
|
---|
443 | // compatibility names so we don't have to change so much code
|
---|
444 |
|
---|
445 | inline AllocatedValueImp *Undefined() { return jsUndefined(); }
|
---|
446 | inline AllocatedValueImp *Null() { return jsNull(); }
|
---|
447 | inline AllocatedValueImp *Boolean(bool b) { return jsBoolean(b); }
|
---|
448 | inline ValueImp *Number(double n) { return jsNumber(n); }
|
---|
449 | inline ValueImp *Number(int n) { return jsNumber(n); }
|
---|
450 | inline ValueImp *Number(unsigned n) { return jsNumber(n); }
|
---|
451 | inline ValueImp *Number(long n) { return jsNumber(n); }
|
---|
452 | inline ValueImp *Number(unsigned long n) { return jsNumber(n); }
|
---|
453 | inline AllocatedValueImp *String(const UString& s) { return jsString(s); }
|
---|
454 | inline AllocatedValueImp *String(const char *s) { return jsString(s); }
|
---|
455 |
|
---|
456 | } // namespace
|
---|
457 |
|
---|
458 | #endif // KJS_VALUE_H
|
---|