source: webkit/trunk/JavaScriptCore/kjs/value.h@ 4363

Last change on this file since 4363 was 3373, checked in by darin, 22 years ago

More copyright fixes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.6 KB
Line 
1// -*- c-basic-offset: 2 -*-
2/*
3 * This file is part of the KDE libraries
4 * Copyright (C) 1999-2001 Harri Porten ([email protected])
5 * Copyright (C) 2001 Peter Kelly ([email protected])
6 * Copyright (C) 2003 Apple Computer, Inc.
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB. If not, write to
20 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
22 *
23 */
24
25#ifndef _KJS_VALUE_H_
26#define _KJS_VALUE_H_
27
28#ifndef NDEBUG // protection against problems if committing with KJS_VERBOSE on
29
30// Uncomment this to enable very verbose output from KJS
31//#define KJS_VERBOSE
32// Uncomment this to debug memory allocation and garbage collection
33//#define KJS_DEBUG_MEM
34
35#endif
36
37#include <stdlib.h> // Needed for size_t
38
39#include "ustring.h"
40
41#include "simple_number.h"
42
43// Primitive data types
44
45namespace KJS {
46
47 class Value;
48 class ValueImp;
49 class ValueImpPrivate;
50 class Undefined;
51 class UndefinedImp;
52 class Null;
53 class NullImp;
54 class Boolean;
55 class BooleanImp;
56 class String;
57 class StringImp;
58 class Number;
59 class NumberImp;
60 class Object;
61 class ObjectImp;
62 class Reference;
63 class ReferenceImp;
64 class List;
65 class ListImp;
66 class Completion;
67 class ExecState;
68
69 /**
70 * Primitive types
71 */
72 enum Type {
73 UnspecifiedType = 0,
74 UndefinedType = 1,
75 NullType = 2,
76 BooleanType = 3,
77 StringType = 4,
78 NumberType = 5,
79 ObjectType = 6
80 };
81
82 /**
83 * ValueImp is the base type for all primitives (Undefined, Null, Boolean,
84 * String, Number) and objects in ECMAScript.
85 *
86 * Note: you should never inherit from ValueImp as it is for primitive types
87 * only (all of which are provided internally by KJS). Instead, inherit from
88 * ObjectImp.
89 */
90 class ValueImp {
91 friend class Collector;
92 friend class Value;
93 friend class ContextImp;
94 public:
95 ValueImp();
96 virtual ~ValueImp();
97
98 ValueImp* ref() { if (!SimpleNumber::is(this)) refcount++; return this; }
99 bool deref() { if (SimpleNumber::is(this)) return false; else return (!--refcount); }
100
101 virtual void mark();
102 bool marked() const;
103 void* operator new(size_t);
104 void operator delete(void*);
105
106 /**
107 * @internal
108 *
109 * set by Object() so that the collector is allowed to delete us
110 */
111 void setGcAllowed();
112
113 // Will crash if called on a simple number.
114 void setGcAllowedFast() { _flags |= VI_GCALLOWED; }
115
116 int toInteger(ExecState *exec) const;
117 int toInt32(ExecState *exec) const;
118 unsigned int toUInt32(ExecState *exec) const;
119 unsigned short toUInt16(ExecState *exec) const;
120
121 // Dispatch wrappers that handle the special small number case
122
123 Type dispatchType() const;
124 Value dispatchToPrimitive(ExecState *exec, Type preferredType = UnspecifiedType) const;
125 bool dispatchToBoolean(ExecState *exec) const;
126 double dispatchToNumber(ExecState *exec) const;
127 UString dispatchToString(ExecState *exec) const;
128 bool dispatchToUInt32(unsigned&) const;
129 Object dispatchToObject(ExecState *exec) const;
130
131 unsigned short int refcount;
132
133 private:
134 unsigned short int _flags;
135
136 virtual Type type() const = 0;
137
138 // The conversion operations
139
140 virtual Value toPrimitive(ExecState *exec, Type preferredType = UnspecifiedType) const = 0;
141 virtual bool toBoolean(ExecState *exec) const = 0;
142 virtual double toNumber(ExecState *exec) const = 0;
143 virtual UString toString(ExecState *exec) const = 0;
144 virtual Object toObject(ExecState *exec) const = 0;
145 virtual bool toUInt32(unsigned&) const;
146
147 enum {
148 VI_MARKED = 1,
149 VI_GCALLOWED = 2,
150 VI_CREATED = 4
151 }; // VI means VALUEIMPL
152
153 // Give a compile time error if we try to copy one of these.
154 ValueImp(const ValueImp&);
155 ValueImp& operator=(const ValueImp&);
156 };
157
158 /**
159 * Value objects are act as wrappers ("smart pointers") around ValueImp
160 * objects and their descendents. Instead of using ValueImps
161 * (and derivatives) during normal program execution, you should use a
162 * Value-derived class.
163 *
164 * Value maintains a pointer to a ValueImp object and uses a reference
165 * counting scheme to ensure that the ValueImp object is not deleted or
166 * garbage collected.
167 *
168 * Note: The conversion operations all return values of various types -
169 * if an error occurs during conversion, an error object will instead
170 * be returned (where possible), and the execution state's exception
171 * will be set appropriately.
172 */
173 class Value {
174 public:
175 Value() : rep(0) { }
176 explicit Value(ValueImp *v);
177 Value(const Value &v);
178 ~Value();
179
180 Value& operator=(const Value &v);
181 bool isNull() const { return rep == 0; }
182 ValueImp *imp() const { return rep; }
183
184 /**
185 * Returns the type of value. This is one of UndefinedType, NullType,
186 * BooleanType, StringType, NumberType, or ObjectType.
187 *
188 * @return The type of value
189 */
190 Type type() const { return rep->dispatchType(); }
191
192 /**
193 * Checks whether or not the value is of a particular tpye
194 *
195 * @param The type to compare with
196 * @return true if the value is of the specified type, otherwise false
197 */
198 bool isA(Type t) const { return rep->dispatchType() == t; }
199
200 /**
201 * Performs the ToPrimitive type conversion operation on this value
202 * (ECMA 9.1)
203 */
204 Value toPrimitive(ExecState *exec,
205 Type preferredType = UnspecifiedType) const
206 { return rep->dispatchToPrimitive(exec, preferredType); }
207
208 /**
209 * Performs the ToBoolean type conversion operation on this value (ECMA 9.2)
210 */
211 bool toBoolean(ExecState *exec) const { return rep->dispatchToBoolean(exec); }
212
213 /**
214 * Performs the ToNumber type conversion operation on this value (ECMA 9.3)
215 */
216 double toNumber(ExecState *exec) const { return rep->dispatchToNumber(exec); }
217
218 /**
219 * Performs the ToInteger type conversion operation on this value (ECMA 9.4)
220 */
221 int toInteger(ExecState *exec) const { return rep->toInteger(exec); }
222
223 /**
224 * Performs the ToInt32 type conversion operation on this value (ECMA 9.5)
225 */
226 int toInt32(ExecState *exec) const { return rep->toInt32(exec); }
227
228 /**
229 * Performs the ToUint32 type conversion operation on this value (ECMA 9.6)
230 */
231 unsigned int toUInt32(ExecState *exec) const { return rep->toUInt32(exec); }
232
233 /**
234 * Performs the ToUint16 type conversion operation on this value (ECMA 9.7)
235 */
236 unsigned short toUInt16(ExecState *exec) const { return rep->toUInt16(exec); }
237
238 /**
239 * Performs the ToString type conversion operation on this value (ECMA 9.8)
240 */
241 UString toString(ExecState *exec) const { return rep->dispatchToString(exec); }
242
243 /**
244 * Performs the ToObject type conversion operation on this value (ECMA 9.9)
245 */
246 Object toObject(ExecState *exec) const;
247
248 /**
249 * Checks if we can do a lossless conversion to UInt32.
250 */
251 bool toUInt32(unsigned& i) const { return rep->dispatchToUInt32(i); }
252
253 protected:
254 ValueImp *rep;
255 };
256
257 // Primitive types
258
259 /**
260 * Represents an primitive Undefined value. All instances of this class
261 * share the same implementation object, so == will always return true
262 * for any comparison between two Undefined objects.
263 */
264 class Undefined : public Value {
265 public:
266 Undefined();
267
268 /**
269 * Converts a Value into an Undefined. If the value's type is not
270 * UndefinedType, a null object will be returned (i.e. one with it's
271 * internal pointer set to 0). If you do not know for sure whether the
272 * value is of type UndefinedType, you should check the @ref isNull()
273 * methods afterwards before calling any methods on the returned value.
274 *
275 * @return The value converted to an Undefined
276 */
277 static Undefined dynamicCast(const Value &v);
278 private:
279 friend class UndefinedImp;
280 explicit Undefined(UndefinedImp *v);
281
282 };
283
284 /**
285 * Represents an primitive Null value. All instances of this class
286 * share the same implementation object, so == will always return true
287 * for any comparison between two Null objects.
288 */
289 class Null : public Value {
290 public:
291 Null();
292
293 /**
294 * Converts a Value into an Null. If the value's type is not NullType,
295 * a null object will be returned (i.e. one with it's internal pointer set
296 * to 0). If you do not know for sure whether the value is of type
297 * NullType, you should check the @ref isNull() methods afterwards before
298 * calling any methods on the returned value.
299 *
300 * @return The value converted to a Null
301 */
302 static Null dynamicCast(const Value &v);
303 private:
304 friend class NullImp;
305 explicit Null(NullImp *v);
306 };
307
308 /**
309 * Represents an primitive Null value
310 */
311 class Boolean : public Value {
312 public:
313 Boolean(bool b = false);
314
315 /**
316 * Converts a Value into an Boolean. If the value's type is not BooleanType,
317 * a null object will be returned (i.e. one with it's internal pointer set
318 * to 0). If you do not know for sure whether the value is of type
319 * BooleanType, you should check the @ref isNull() methods afterwards before
320 * calling any methods on the returned value.
321 *
322 * @return The value converted to a Boolean
323 */
324 static Boolean dynamicCast(const Value &v);
325
326 bool value() const;
327 private:
328 friend class BooleanImp;
329 explicit Boolean(BooleanImp *v);
330 };
331
332 /**
333 * Represents an primitive Null value
334 */
335 class String : public Value {
336 public:
337 String(const UString &s = "");
338
339 /**
340 * Converts a Value into an String. If the value's type is not StringType,
341 * a null object will be returned (i.e. one with it's internal pointer set
342 * to 0). If you do not know for sure whether the value is of type
343 * StringType, you should check the @ref isNull() methods afterwards before
344 * calling any methods on the returned value.
345 *
346 * @return The value converted to a String
347 */
348 static String dynamicCast(const Value &v);
349
350 UString value() const;
351 private:
352 friend class StringImp;
353 explicit String(StringImp *v);
354 };
355
356 extern const double NaN;
357 extern const double Inf;
358
359 /**
360 * Represents an primitive Number value
361 */
362 class Number : public Value {
363 friend class ValueImp;
364 public:
365 Number(int i);
366 Number(unsigned int u);
367 Number(double d = 0.0);
368 Number(long int l);
369 Number(long unsigned int l);
370
371 double value() const;
372 int intValue() const;
373
374 bool isNaN() const;
375 bool isInf() const;
376
377 /**
378 * Converts a Value into an Number. If the value's type is not NumberType,
379 * a null object will be returned (i.e. one with it's internal pointer set
380 * to 0). If you do not know for sure whether the value is of type
381 * NumberType, you should check the @ref isNull() methods afterwards before
382 * calling any methods on the returned value.
383 *
384 * @return The value converted to a Number
385 */
386 static Number dynamicCast(const Value &v);
387 private:
388 friend class NumberImp;
389 explicit Number(NumberImp *v);
390 };
391
392}; // namespace
393
394#endif // _KJS_VALUE_H_
Note: See TracBrowser for help on using the repository browser.