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

Last change on this file since 1791 was 1791, checked in by darin, 23 years ago

JavaScriptCore:

Some string speedups. Makes sony.com cached 11% faster.

  • kjs/ustring.h: Made it possible for UChar objects to be uninitialized, which gives a speed boost. Inlined CString's +=, UString's destructor, +=, and +.
  • kjs/ustring.cpp: (UString::UString): Optimize const char * version, which showed up heavily in performance analysis. Added new two-UString version, which makes the + operator fast. (UString::ascii): Remove thread safety changes. Change static buffer to remember its size, and to always be at least 4096 bytes long; that way we never have to reallocate unless it's for a long string. Also make code to extract the characters significantly faster by getting rid of two pointer dereferences per character. (UString::is8Bit): Avoid one pointer dereference per character. (UString::toDouble): Use ascii() instead of cstring() to avoid copying the string.
  • kjs/collector.cpp: Remove unneeded APPLE_CHANGES.
  • kjs/regexp.cpp: Remove ifdefs around some APPLE_CHANGES that we want to keep, because they just fix warnings.
  • kjs/value.h: Remove obsolete APPLE_CHANGES comment.
  • JavaScriptCore.pbproj/project.pbxproj: Project Builder decided to move a line around in the file.

WebCore:

  • force-clean-timestamp: JavaScriptCore headers changed that require a full build here.
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.9 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 *
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., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, 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 <stdlib.h> // Needed for size_t
37
38#include "ustring.h"
39
40// Primitive data types
41
42namespace KJS {
43
44 class Value;
45 class ValueImp;
46 class ValueImpPrivate;
47 class Undefined;
48 class UndefinedImp;
49 class Null;
50 class NullImp;
51 class Boolean;
52 class BooleanImp;
53 class String;
54 class StringImp;
55 class Number;
56 class NumberImp;
57 class Object;
58 class ObjectImp;
59 class Reference;
60 class ReferenceImp;
61 class List;
62 class ListImp;
63 class Completion;
64 class CompletionImp;
65 class ExecState;
66
67 /**
68 * Primitive types
69 */
70 enum Type {
71 UnspecifiedType = 0,
72 UndefinedType = 1,
73 NullType = 2,
74 BooleanType = 3,
75 StringType = 4,
76 NumberType = 5,
77 ObjectType = 6,
78 ReferenceType = 7,
79 ListType = 8,
80 CompletionType = 9
81 };
82
83 /**
84 * ValueImp is the base type for all primitives (Undefined, Null, Boolean,
85 * String, Number) and objects in ECMAScript.
86 *
87 * Note: you should never inherit from ValueImp as it is for primitive types
88 * only (all of which are provided internally by KJS). Instead, inherit from
89 * ObjectImp.
90 */
91 class ValueImp {
92 friend class Collector;
93 public:
94 ValueImp();
95 virtual ~ValueImp();
96
97 inline ValueImp* ref() { refcount++; return this; }
98 inline bool deref() { return (!--refcount); }
99 unsigned int 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 virtual Type type() const = 0;
114
115 // The conversion operations
116
117 virtual Value toPrimitive(ExecState *exec,
118 Type preferredType = UnspecifiedType) const = 0;
119 virtual bool toBoolean(ExecState *exec) const = 0;
120 virtual double toNumber(ExecState *exec) const = 0;
121 virtual int toInteger(ExecState *exec) const;
122 virtual int toInt32(ExecState *exec) const;
123 virtual unsigned int toUInt32(ExecState *exec) const;
124 virtual unsigned short toUInt16(ExecState *exec) const;
125 virtual UString toString(ExecState *exec) const = 0;
126 virtual Object toObject(ExecState *exec) const = 0;
127
128 // Reference operations
129
130 virtual Value getBase(ExecState *exec) const;
131 virtual UString getPropertyName(ExecState *exec) const;
132 virtual Value getValue(ExecState *exec) const;
133 virtual void putValue(ExecState *exec, const Value w);
134
135 private:
136 enum {
137 VI_MARKED = 1,
138 VI_GCALLOWED = 2,
139 VI_CREATED = 4,
140 VI_DESTRUCTED = 8
141 }; // VI means VALUEIMPL
142
143 ValueImpPrivate *_vd;
144 unsigned int _flags;
145 };
146
147 /**
148 * Value objects are act as wrappers ("smart pointers") around ValueImp
149 * objects and their descendents. Instead of using ValueImps
150 * (and derivatives) during normal program execution, you should use a
151 * Value-derived class.
152 *
153 * Value maintains a pointer to a ValueImp object and uses a reference
154 * counting scheme to ensure that the ValueImp object is not deleted or
155 * garbage collected.
156 *
157 * Note: The conversion operations all return values of various types -
158 * if an error occurs during conversion, an error object will instead
159 * be returned (where possible), and the execution state's exception
160 * will be set appropriately.
161 */
162 class Value {
163 public:
164 Value();
165 explicit Value(ValueImp *v);
166 Value(const Value &v);
167 virtual ~Value();
168
169 Value& operator=(const Value &v);
170 bool isNull() const;
171 ValueImp *imp() const;
172
173 /**
174 * Returns the type of value. This is one of UndefinedType, NullType,
175 * BooleanType, StringType NumberType, ObjectType, ReferenceType,
176 * ListType or CompletionType.
177 *
178 * @return The type of value
179 */
180 Type type() const;
181
182 /**
183 * Checks whether or not the value is of a particular tpye
184 *
185 * @param The type to compare with
186 * @return true if the value is of the specified type, otherwise false
187 */
188 bool isA(Type t) const;
189
190 /**
191 * Performs the ToPrimitive type conversion operation on this value
192 * (ECMA 9.1)
193 */
194 Value toPrimitive(ExecState *exec,
195 Type preferredType = UnspecifiedType) const;
196
197 /**
198 * Performs the ToBoolean type conversion operation on this value (ECMA 9.2)
199 */
200 bool toBoolean(ExecState *exec) const;
201
202 /**
203 * Performs the ToNumber type conversion operation on this value (ECMA 9.3)
204 */
205 double toNumber(ExecState *exec) const;
206
207 /**
208 * Performs the ToInteger type conversion operation on this value (ECMA 9.4)
209 */
210 int toInteger(ExecState *exec) const;
211
212 /**
213 * Performs the ToInt32 type conversion operation on this value (ECMA 9.5)
214 */
215 int toInt32(ExecState *exec) const;
216
217 /**
218 * Performs the ToUint32 type conversion operation on this value (ECMA 9.6)
219 */
220 unsigned int toUInt32(ExecState *exec) const;
221
222 /**
223 * Performs the ToUint16 type conversion operation on this value (ECMA 9.7)
224 */
225 unsigned short toUInt16(ExecState *exec) const;
226
227 /**
228 * Performs the ToString type conversion operation on this value (ECMA 9.8)
229 */
230 UString toString(ExecState *exec) const;
231
232 /**
233 * Performs the ToObject type conversion operation on this value (ECMA 9.9)
234 */
235 Object toObject(ExecState *exec) const;
236
237 /**
238 * Performs the GetBase type conversion operation on this value (ECMA 8.7)
239 *
240 * Since references are supposed to have an Object or null as their base,
241 * this method is guaranteed to return either Null() or an Object value.
242 */
243 Value getBase(ExecState *exec) const;
244
245 /**
246 * Performs the GetPropertyName type conversion operation on this value
247 * (ECMA 8.7)
248 */
249 UString getPropertyName(ExecState *exec) const;
250
251 /**
252 * Performs the GetValue type conversion operation on this value
253 * (ECMA 8.7.1)
254 */
255 Value getValue(ExecState *exec) const;
256
257 /**
258 * Performs the PutValue type conversion operation on this value
259 * (ECMA 8.7.1)
260 */
261 void putValue(ExecState *exec, const Value w);
262
263 protected:
264 ValueImp *rep;
265 };
266
267 bool operator==(const Value &v1, const Value &v2);
268 bool operator!=(const Value &v1, const Value &v2);
269
270 // Primitive types
271
272 /**
273 * Represents an primitive Undefined value. All instances of this class
274 * share the same implementation object, so == will always return true
275 * for any comparison between two Undefined objects.
276 */
277 class Undefined : public Value {
278 public:
279 Undefined();
280 Undefined(const Undefined &v);
281 virtual ~Undefined();
282
283 Undefined& operator=(const Undefined &v);
284
285 /**
286 * Converts a Value into an Undefined. If the value's type is not
287 * UndefinedType, a null object will be returned (i.e. one with it's
288 * internal pointer set to 0). If you do not know for sure whether the
289 * value is of type UndefinedType, you should check the @ref isNull()
290 * methods afterwards before calling any methods on the returned value.
291 *
292 * @return The value converted to an Undefined
293 */
294 static Undefined dynamicCast(const Value &v);
295 private:
296 friend class UndefinedImp;
297 explicit Undefined(UndefinedImp *v);
298
299 };
300
301 /**
302 * Represents an primitive Null value. All instances of this class
303 * share the same implementation object, so == will always return true
304 * for any comparison between two Null objects.
305 */
306 class Null : public Value {
307 public:
308 Null();
309 Null(const Null &v);
310 virtual ~Null();
311
312 Null& operator=(const Null &v);
313
314 /**
315 * Converts a Value into an Null. If the value's type is not NullType,
316 * a null object will be returned (i.e. one with it's internal pointer set
317 * to 0). If you do not know for sure whether the value is of type
318 * NullType, you should check the @ref isNull() methods afterwards before
319 * calling any methods on the returned value.
320 *
321 * @return The value converted to a Null
322 */
323 static Null dynamicCast(const Value &v);
324 private:
325 friend class NullImp;
326 explicit Null(NullImp *v);
327 };
328
329 /**
330 * Represents an primitive Null value
331 */
332 class Boolean : public Value {
333 public:
334 Boolean(bool b = false);
335 Boolean(const Boolean &v);
336 virtual ~Boolean();
337
338 Boolean& operator=(const Boolean &v);
339
340 /**
341 * Converts a Value into an Boolean. If the value's type is not BooleanType,
342 * a null object will be returned (i.e. one with it's internal pointer set
343 * to 0). If you do not know for sure whether the value is of type
344 * BooleanType, you should check the @ref isNull() methods afterwards before
345 * calling any methods on the returned value.
346 *
347 * @return The value converted to a Boolean
348 */
349 static Boolean dynamicCast(const Value &v);
350
351 bool value() const;
352 private:
353 friend class BooleanImp;
354 explicit Boolean(BooleanImp *v);
355 };
356
357 /**
358 * Represents an primitive Null value
359 */
360 class String : public Value {
361 public:
362 String(const UString &s = "");
363 String(const String &v);
364 virtual ~String();
365
366 String& operator=(const String &v);
367
368 /**
369 * Converts a Value into an String. If the value's type is not StringType,
370 * a null object will be returned (i.e. one with it's internal pointer set
371 * to 0). If you do not know for sure whether the value is of type
372 * StringType, you should check the @ref isNull() methods afterwards before
373 * calling any methods on the returned value.
374 *
375 * @return The value converted to a String
376 */
377 static String dynamicCast(const Value &v);
378
379 UString value() const;
380 private:
381 friend class StringImp;
382 explicit String(StringImp *v);
383 };
384
385 extern const double NaN;
386 extern const double Inf;
387
388 /**
389 * Represents an primitive Number value
390 */
391 class Number : public Value {
392 public:
393 Number(int i);
394 Number(unsigned int u);
395 Number(double d = 0.0);
396 Number(long int l);
397 Number(long unsigned int l);
398 Number(const Number &v);
399 virtual ~Number();
400
401 Number& operator=(const Number &v);
402
403 double value() const;
404 int intValue() const;
405
406 bool isNaN() const;
407 bool isInf() const;
408
409 /**
410 * Converts a Value into an Number. If the value's type is not NumberType,
411 * a null object will be returned (i.e. one with it's internal pointer set
412 * to 0). If you do not know for sure whether the value is of type
413 * NumberType, you should check the @ref isNull() methods afterwards before
414 * calling any methods on the returned value.
415 *
416 * @return The value converted to a Number
417 */
418 static Number dynamicCast(const Value &v);
419 private:
420 friend class NumberImp;
421 explicit Number(NumberImp *v);
422 };
423
424}; // namespace
425
426#endif // _KJS_VALUE_H_
Note: See TracBrowser for help on using the repository browser.