source: webkit/trunk/JavaScriptCore/kjs/JSValue.h@ 35900

Last change on this file since 35900 was 35830, checked in by Darin Adler, 17 years ago

JavaScriptCore:

2008-08-12 Darin Adler <Darin Adler>

Reviewed by Geoff.

  • eliminate JSValue::type()

This will make it slightly easier to change the JSImmediate design without
having to touch so many call sites.

SunSpider says this change is a wash (looked like a slight speedup, but not
statistically significant).

  • API/JSStringRef.cpp: Removed include of JSType.h.
  • API/JSValueRef.cpp: Removed include of JSType.h. (JSValueGetType): Replaced use of JSValue::type() with JSValue::is functions.
  • VM/JSPropertyNameIterator.cpp: Removed type() implementation. (KJS::JSPropertyNameIterator::toPrimitive): Changed to take PreferredPrimitiveType argument instead of JSType.
  • VM/JSPropertyNameIterator.h: Ditto.
  • VM/Machine.cpp: (KJS::fastIsNumber): Updated for name change. (KJS::fastToInt32): Ditto. (KJS::fastToUInt32): Ditto. (KJS::jsAddSlowCase): Updated toPrimitive caller for change from JSType to PreferredPrimitiveType. (KJS::jsAdd): Replaced calls to JSValue::type() with calls to JSValue::isString(). (KJS::jsTypeStringForValue): Replaced calls to JSValue::type() with multiple calls to JSValue::is -- we could make this a virtual function instead if we want to have faster performance. (KJS::Machine::privateExecute): Renamed JSImmediate::toTruncatedUInt32 to JSImmediate::getTruncatedUInt32 for consistency with other functions. Changed two calls of JSValue::type() to JSValue::isString().
  • kjs/GetterSetter.cpp: (KJS::GetterSetter::toPrimitive): Changed to take PreferredPrimitiveType argument instead of JSType. (KJS::GetterSetter::isGetterSetter): Added.
  • kjs/GetterSetter.h:
  • kjs/JSCell.cpp: (KJS::JSCell::isString): Added. (KJS::JSCell::isGetterSetter): Added. (KJS::JSCell::isObject): Added.
  • kjs/JSCell.h: Eliminated type function. Added isGetterSetter. Made isString and isObject virtual. Changed toPrimitive to take PreferredPrimitiveType argument instead of JSType. (KJS::JSCell::isNumber): Use Heap::isNumber for faster performance. (KJS::JSValue::isGetterSetter): Added. (KJS::JSValue::toPrimitive): Changed to take PreferredPrimitiveType argument instead of JSType.
  • kjs/JSImmediate.h: Removed JSValue::type() and replaced JSValue::toTruncatedUInt32 with JSValue::getTruncatedUInt32. (KJS::JSImmediate::isEitherImmediate): Added.
  • kjs/JSNotAnObject.cpp: (KJS::JSNotAnObject::toPrimitive): Changed to take PreferredPrimitiveType argument instead of JSType.
  • kjs/JSNotAnObject.h: Ditto.
  • kjs/JSNumberCell.cpp: (KJS::JSNumberCell::toPrimitive): Ditto.
  • kjs/JSNumberCell.h: (KJS::JSNumberCell::toInt32): Renamed from fastToInt32. There's no other "slow" version of this once you have a JSNumberCell, so there's no need for "fast" in the name. It's a feature that this hides the base class toInt32, which does the same job less efficiently (and has an additional ExecState argument). (KJS::JSNumberCell::toUInt32): Ditto.
  • kjs/JSObject.cpp: (KJS::callDefaultValueFunction): Use isGetterSetter instead of type. (KJS::JSObject::getPrimitiveNumber): Use PreferredPrimitiveType. (KJS::JSObject::defaultValue): Ditto. (KJS::JSObject::defineGetter): Use isGetterSetter. (KJS::JSObject::defineSetter): Ditto. (KJS::JSObject::lookupGetter): Ditto. (KJS::JSObject::lookupSetter): Ditto. (KJS::JSObject::toNumber): Use PreferredPrimitiveType. (KJS::JSObject::toString): Ditto. (KJS::JSObject::isObject): Added.
  • kjs/JSObject.h: (KJS::JSObject::inherits): Call the isObject from JSCell; it's now hidden by our override of isObject. (KJS::JSObject::getOwnPropertySlotForWrite): Use isGetterSetter instead of type. (KJS::JSObject::getOwnPropertySlot): Ditto. (KJS::JSObject::toPrimitive): Use PreferredPrimitiveType.
  • kjs/JSString.cpp: (KJS::JSString::toPrimitive): Use PreferredPrimitiveType. (KJS::JSString::isString): Added.
  • kjs/JSString.h: Ditto.
  • kjs/JSValue.h: Removed type(), added isGetterSetter(). Added PreferredPrimitiveType enum and used it as the argument for the toPrimitive function. (KJS::JSValue::getBoolean): Simplified a bit an removed a branch.
  • kjs/collector.cpp: (KJS::typeName): Changed to use JSCell::is functions instead of calling JSCell::type.
  • kjs/collector.h: (KJS::Heap::isNumber): Renamed from fastIsNumber.
  • kjs/nodes.h: Added now-needed include of JSType, since the type is used here to record types of values in the tree.
  • kjs/operations.cpp: (KJS::equal): Rewrote to no longer depend on type(). (KJS::strictEqual): Ditto.

JavaScriptGlue:

2008-08-12 Darin Adler <Darin Adler>

Reviewed by Geoff.

  • eliminate JSValue::type()
  • JSUtils.cpp: (KJSValueToCFTypeInternal): Replaced uses of JSValue::type() with JSValue::is functions.
  • UserObjectImp.cpp: (UserObjectImp::getOwnPropertySlot): Ditto. (UserObjectImp::toPrimitive): Take PreferredPrimitiveType argument instead of JSType argument.
  • UserObjectImp.h: Ditto.

WebCore:

2008-08-12 Darin Adler <Darin Adler>

Reviewed by Geoff.

  • eliminate JSValue::type()
  • bridge/c/c_instance.cpp: (KJS::Bindings::CInstance::defaultValue): Take PreferredPrimitiveType argument instead of JSType argument. Removed unneeded code to handle boolean, since that's never passed.
  • bridge/c/c_instance.h: Ditto.
  • bridge/c/c_utility.cpp: (KJS::Bindings::convertValueToNPVariant): Use JSValue::is functions instead of JSValue::type(). Removed unneeded code to handle "unspecified".
  • bridge/jni/jni_instance.cpp: (JavaInstance::defaultValue): Take PreferredPrimitiveType argument instead of JSType argument. Removed unneeded code to handle boolean.
  • bridge/jni/jni_instance.h: Ditto.
  • bridge/jni/jni_jsobject.mm: (JavaJSObject::convertValueToJObject): Use JSValue::is functions instead of JSValue::type().
  • bridge/objc/objc_instance.h: Take PreferredPrimitiveType argument instead of JSType argument. Removed unused argument.
  • bridge/objc/objc_instance.mm: (ObjcInstance::getValueOfUndefinedField): Removed unused argument. (ObjcInstance::defaultValue): Take PreferredPrimitiveType argument instead of JSType argument. Removed unneeded code to handle boolean and another dead code path for unknown types.
  • bridge/objc/objc_runtime.h: Take PreferredPrimitiveType argument instead of JSType argument. Removed override of type() that caused the fallback object to return "UndefinedType" when there is no invokeUndefinedMethodFromWebScript:withArguments: method defined. That didn't accomplish much, since most checks for undefined don't ever call type().
  • bridge/objc/objc_runtime.mm: (ObjcFallbackObjectImp::defaultValue): Ditto.
  • bridge/qt/qt_instance.cpp: (KJS::Bindings::QtInstance::defaultValue): Take PreferredPrimitiveType argument instead of JSType argument. Removed unneeded code to handle boolean.
  • bridge/qt/qt_instance.h: Ditto.
  • bridge/runtime.h: (KJS::Bindings::Instance::getValueOfUndefinedField): Removed unsed argument.
  • bridge/runtime_object.cpp: (RuntimeObjectImp::defaultValue): Take PreferredPrimitiveType argument instead of JSType argument.
  • bridge/runtime_object.h: Ditto.

WebKit/mac:

2008-08-12 Darin Adler <Darin Adler>

Reviewed by Geoff.

  • eliminate JSValue::type()
  • WebView/WebView.mm: (aeDescFromJSValue): Rewrite to use the JSValue::is functions instead of a switch on JSValue::type().

LayoutTests:

2008-08-12 Darin Adler <Darin Adler>

Reviewed by Geoff.

  • added a test since I had to rewrite the == and === operators
  • fast/js/equality-expected.txt: Added.
  • fast/js/equality.html: Added.
  • fast/js/resources/equality.js: Added.
  • Property svn:eol-style set to native
File size: 7.8 KB
Line 
1/*
2 * Copyright (C) 1999-2001 Harri Porten ([email protected])
3 * Copyright (C) 2001 Peter Kelly ([email protected])
4 * Copyright (C) 2003, 2004, 2005, 2007, 2008 Apple Inc. All rights reserved.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 *
21 */
22
23#ifndef JSValue_h
24#define JSValue_h
25
26#include "CallData.h"
27#include "ConstructData.h"
28#include "JSImmediate.h"
29#include "ustring.h"
30#include <stddef.h> // for size_t
31
32namespace KJS {
33
34 class ExecState;
35 class Identifier;
36 class JSCell;
37 class JSObject;
38 class JSString;
39 class PropertySlot;
40 struct ClassInfo;
41 struct Instruction;
42
43 /**
44 * JSValue is the base type for all primitives (Undefined, Null, Boolean,
45 * String, Number) and objects in ECMAScript.
46 *
47 * Note: you should never inherit from JSValue as it is for primitive types
48 * only (all of which are provided internally by KJS). Instead, inherit from
49 * JSObject.
50 */
51 class JSValue : Noncopyable {
52 friend class JSCell; // so it can derive from this class
53 friend class Heap; // so it can call asCell()
54 friend class Machine; // so it can call asCell()
55 private:
56 JSValue();
57 virtual ~JSValue();
58
59 public:
60 // Querying the type.
61 bool isUndefined() const;
62 bool isNull() const;
63 bool isUndefinedOrNull() const;
64 bool isBoolean() const;
65 bool isNumber() const;
66 bool isString() const;
67 bool isGetterSetter() const;
68 bool isObject() const;
69 bool isObject(const ClassInfo*) const; // FIXME: Merge with inherits.
70
71 // Extracting the value.
72 bool getBoolean(bool&) const;
73 bool getBoolean() const; // false if not a boolean
74 bool getNumber(double&) const;
75 double getNumber() const; // NaN if not a number
76 double uncheckedGetNumber() const;
77 bool getString(UString&) const;
78 UString getString() const; // null string if not a string
79 JSObject* getObject(); // NULL if not an object
80 const JSObject* getObject() const; // NULL if not an object
81
82 CallType getCallData(CallData&);
83 ConstructType getConstructData(ConstructData&);
84
85 // Extracting integer values.
86 bool getUInt32(uint32_t&) const;
87 bool getTruncatedInt32(int32_t&) const;
88 bool getTruncatedUInt32(uint32_t&) const;
89
90 // Basic conversions.
91 enum PreferredPrimitiveType { NoPreference, PreferNumber, PreferString };
92 JSValue* toPrimitive(ExecState*, PreferredPrimitiveType = NoPreference) const;
93 bool getPrimitiveNumber(ExecState*, double& number, JSValue*&);
94
95 bool toBoolean(ExecState*) const;
96
97 // toNumber conversion is expected to be side effect free if an exception has
98 // been set in the ExecState already.
99 double toNumber(ExecState*) const;
100 JSValue* toJSNumber(ExecState*) const; // Fast path for when you expect that the value is an immediate number.
101 UString toString(ExecState*) const;
102 JSObject* toObject(ExecState*) const;
103
104 // Integer conversions.
105 double toInteger(ExecState*) const;
106 double toIntegerPreserveNaN(ExecState*) const;
107 int32_t toInt32(ExecState*) const;
108 int32_t toInt32(ExecState*, bool& ok) const;
109 uint32_t toUInt32(ExecState*) const;
110 uint32_t toUInt32(ExecState*, bool& ok) const;
111
112 // These are identical logic to above, and faster than jsNumber(number)->toInt32(exec)
113 static int32_t toInt32(double);
114 static uint32_t toUInt32(double);
115
116 // Floating point conversions.
117 float toFloat(ExecState*) const;
118
119 // Garbage collection.
120 void mark();
121 bool marked() const;
122
123 static int32_t toInt32SlowCase(double, bool& ok);
124 static uint32_t toUInt32SlowCase(double, bool& ok);
125
126 // Object operations, with the toObject operation included.
127 JSValue* get(ExecState*, const Identifier& propertyName) const;
128 JSValue* get(ExecState*, unsigned propertyName) const;
129 void put(ExecState*, const Identifier& propertyName, JSValue*);
130 void put(ExecState*, unsigned propertyName, JSValue*);
131 bool deleteProperty(ExecState*, const Identifier& propertyName);
132 bool deleteProperty(ExecState*, unsigned propertyName);
133
134 JSObject* toThisObject(ExecState*) const;
135 UString toThisString(ExecState*) const;
136 JSString* toThisJSString(ExecState*);
137
138 JSValue* getJSNumber(); // 0 if this is not a JSNumber or number object
139
140 private:
141 bool getPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&);
142 bool getPropertySlot(ExecState*, unsigned propertyName, PropertySlot&);
143 int32_t toInt32SlowCase(ExecState*, bool& ok) const;
144 uint32_t toUInt32SlowCase(ExecState*, bool& ok) const;
145
146 // Implementation details.
147 JSCell* asCell();
148 const JSCell* asCell() const;
149 };
150
151 inline JSValue::JSValue()
152 {
153 }
154
155 inline JSValue::~JSValue()
156 {
157 }
158
159 inline bool JSValue::isUndefined() const
160 {
161 return this == jsUndefined();
162 }
163
164 inline bool JSValue::isNull() const
165 {
166 return this == jsNull();
167 }
168
169 inline bool JSValue::isUndefinedOrNull() const
170 {
171 return JSImmediate::isUndefinedOrNull(this);
172 }
173
174 inline bool JSValue::isBoolean() const
175 {
176 return JSImmediate::isBoolean(this);
177 }
178
179 inline bool JSValue::getBoolean(bool& v) const
180 {
181 if (JSImmediate::isBoolean(this)) {
182 v = JSImmediate::toBoolean(this);
183 return true;
184 }
185
186 return false;
187 }
188
189 inline bool JSValue::getBoolean() const
190 {
191 return this == jsBoolean(true);
192 }
193
194 ALWAYS_INLINE int32_t JSValue::toInt32(ExecState* exec) const
195 {
196 int32_t i;
197 if (getTruncatedInt32(i))
198 return i;
199 bool ok;
200 return toInt32SlowCase(exec, ok);
201 }
202
203 inline uint32_t JSValue::toUInt32(ExecState* exec) const
204 {
205 uint32_t i;
206 if (getTruncatedUInt32(i))
207 return i;
208 bool ok;
209 return toUInt32SlowCase(exec, ok);
210 }
211
212 inline int32_t JSValue::toInt32(double val)
213 {
214 if (!(val >= -2147483648.0 && val < 2147483648.0)) {
215 bool ignored;
216 return toInt32SlowCase(val, ignored);
217 }
218 return static_cast<int32_t>(val);
219 }
220
221 inline uint32_t JSValue::toUInt32(double val)
222 {
223 if (!(val >= 0.0 && val < 4294967296.0)) {
224 bool ignored;
225 return toUInt32SlowCase(val, ignored);
226 }
227 return static_cast<uint32_t>(val);
228 }
229
230 inline int32_t JSValue::toInt32(ExecState* exec, bool& ok) const
231 {
232 int32_t i;
233 if (getTruncatedInt32(i)) {
234 ok = true;
235 return i;
236 }
237 return toInt32SlowCase(exec, ok);
238 }
239
240 inline uint32_t JSValue::toUInt32(ExecState* exec, bool& ok) const
241 {
242 uint32_t i;
243 if (getTruncatedUInt32(i)) {
244 ok = true;
245 return i;
246 }
247 return toUInt32SlowCase(exec, ok);
248 }
249
250} // namespace KJS
251
252#endif // JSValue_h
Note: See TracBrowser for help on using the repository browser.