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

Last change on this file since 24244 was 23892, checked in by weinig, 18 years ago

JavaScriptCore:

Reviewed by Darin.

Second pass at fixing implicit 64-32 bit conversion errors.
<rdar://problem/5292262>

  • Add a toFloat() method to JSValue for float conversion.
  • JavaScriptCore.exp:
  • kjs/value.cpp: (KJS::JSValue::toFloat):
  • kjs/value.h:

WebCore:

Reviewed by Darin.

Second pass at fixing implicit 64-32 bit conversion errors.
<rdar://problem/5292262>

  • Use new JSValue::toFloat() method instead of toNumber() where appropriate.
  • bindings/js/JSCanvasRenderingContext2DCustom.cpp: (WebCore::JSCanvasRenderingContext2D::setFillColor): (WebCore::JSCanvasRenderingContext2D::setStrokeColor): (WebCore::JSCanvasRenderingContext2D::strokeRect): (WebCore::JSCanvasRenderingContext2D::drawImage): (WebCore::JSCanvasRenderingContext2D::drawImageFromRect): (WebCore::JSCanvasRenderingContext2D::setShadow):
  • bindings/js/JSHTMLSelectElementCustom.cpp: (WebCore::JSHTMLSelectElement::remove):
  • bindings/js/JSSVGMatrixCustom.cpp: (WebCore::JSSVGMatrix::rotateFromVector):
  • bindings/js/kjs_events.cpp: (KJS::JSClipboardPrototypeFunction::callAsFunction):
  • bindings/scripts/CodeGeneratorJS.pm:
  • ksvg2/svg/SVGScriptElement.cpp: (WebCore::SVGScriptElement::executeScript):
  • Property svn:eol-style set to native
File size: 9.7 KB
Line 
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 Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 *
22 */
23
24#ifndef KJS_VALUE_H
25#define KJS_VALUE_H
26
27#include "JSImmediate.h"
28#include "collector.h"
29#include "ustring.h"
30#include <stddef.h> // for size_t
31
32#ifndef NDEBUG // protection against problems if committing with KJS_VERBOSE on
33
34// Uncomment this to enable very verbose output from KJS
35//#define KJS_VERBOSE
36// Uncomment this to debug memory allocation and garbage collection
37//#define KJS_DEBUG_MEM
38
39#endif
40
41namespace KJS {
42
43class ExecState;
44class JSObject;
45class JSCell;
46
47struct ClassInfo;
48
49/**
50 * JSValue is the base type for all primitives (Undefined, Null, Boolean,
51 * String, Number) and objects in ECMAScript.
52 *
53 * Note: you should never inherit from JSValue as it is for primitive types
54 * only (all of which are provided internally by KJS). Instead, inherit from
55 * JSObject.
56 */
57class JSValue : Noncopyable {
58 friend class JSCell; // so it can derive from this class
59 friend class Collector; // so it can call asCell()
60
61private:
62 JSValue();
63 virtual ~JSValue();
64
65public:
66 // Querying the type.
67 JSType type() const;
68 bool isUndefined() const;
69 bool isNull() const;
70 bool isUndefinedOrNull() const;
71 bool isBoolean() const;
72 bool isNumber() const;
73 bool isString() const;
74 bool isObject() const;
75 bool isObject(const ClassInfo *) const;
76
77 // Extracting the value.
78 bool getBoolean(bool&) const;
79 bool getBoolean() const; // false if not a boolean
80 bool getNumber(double&) const;
81 double getNumber() const; // NaN if not a number
82 bool getString(UString&) const;
83 UString getString() const; // null string if not a string
84 JSObject *getObject(); // NULL if not an object
85 const JSObject *getObject() const; // NULL if not an object
86
87 // Extracting integer values.
88 bool getUInt32(uint32_t&) const;
89
90 // Basic conversions.
91 JSValue *toPrimitive(ExecState *exec, JSType preferredType = UnspecifiedType) const;
92 bool toBoolean(ExecState *exec) const;
93 double toNumber(ExecState *exec) const;
94 UString toString(ExecState *exec) const;
95 JSObject *toObject(ExecState *exec) const;
96
97 // Integer conversions.
98 double toInteger(ExecState*) const;
99 int32_t toInt32(ExecState*) const;
100 int32_t toInt32(ExecState*, bool& ok) const;
101 uint32_t toUInt32(ExecState*) const;
102 uint32_t toUInt32(ExecState*, bool& ok) const;
103 uint16_t toUInt16(ExecState*) const;
104
105 // Floating point conversions.
106 float toFloat(ExecState*) const;
107
108 // Garbage collection.
109 void mark();
110 bool marked() const;
111
112private:
113 // Implementation details.
114 JSCell *asCell();
115 const JSCell *asCell() const;
116
117 // Give a compile time error if we try to copy one of these.
118 JSValue(const JSValue&);
119 JSValue& operator=(const JSValue&);
120};
121
122class JSCell : public JSValue {
123 friend class Collector;
124 friend class NumberImp;
125 friend class StringImp;
126 friend class JSObject;
127 friend class GetterSetterImp;
128private:
129 JSCell();
130 virtual ~JSCell();
131public:
132 // Querying the type.
133 virtual JSType type() const = 0;
134 bool isNumber() const;
135 bool isString() const;
136 bool isObject() const;
137 bool isObject(const ClassInfo *) const;
138
139 // Extracting the value.
140 bool getNumber(double&) const;
141 double getNumber() const; // NaN if not a number
142 bool getString(UString&) const;
143 UString getString() const; // null string if not a string
144 JSObject *getObject(); // NULL if not an object
145 const JSObject *getObject() const; // NULL if not an object
146
147 // Extracting integer values.
148 virtual bool getUInt32(uint32_t&) const;
149
150 // Basic conversions.
151 virtual JSValue *toPrimitive(ExecState *exec, JSType preferredType = UnspecifiedType) const = 0;
152 virtual bool toBoolean(ExecState *exec) const = 0;
153 virtual double toNumber(ExecState *exec) const = 0;
154 virtual UString toString(ExecState *exec) const = 0;
155 virtual JSObject *toObject(ExecState *exec) const = 0;
156
157 // Garbage collection.
158 void *operator new(size_t);
159 virtual void mark();
160 bool marked() const;
161};
162
163JSValue *jsNumberCell(double);
164
165JSCell *jsString(const UString &); // returns empty string if passed null string
166JSCell *jsString(const char * = ""); // returns empty string if passed 0
167
168extern const double NaN;
169extern const double Inf;
170
171
172inline JSValue *jsUndefined()
173{
174 return JSImmediate::undefinedImmediate();
175}
176
177inline JSValue *jsNull()
178{
179 return JSImmediate::nullImmediate();
180}
181
182inline JSValue *jsNaN()
183{
184 return JSImmediate::NaNImmediate();
185}
186
187inline JSValue *jsBoolean(bool b)
188{
189 return b ? JSImmediate::trueImmediate() : JSImmediate::falseImmediate();
190}
191
192inline JSValue *jsNumber(double d)
193{
194 JSValue *v = JSImmediate::fromDouble(d);
195 return v ? v : jsNumberCell(d);
196}
197
198inline JSValue::JSValue()
199{
200}
201
202inline JSValue::~JSValue()
203{
204}
205
206inline JSCell::JSCell()
207{
208}
209
210inline JSCell::~JSCell()
211{
212}
213
214inline bool JSCell::isNumber() const
215{
216 return type() == NumberType;
217}
218
219inline bool JSCell::isString() const
220{
221 return type() == StringType;
222}
223
224inline bool JSCell::isObject() const
225{
226 return type() == ObjectType;
227}
228
229inline bool JSCell::marked() const
230{
231 return Collector::isCellMarked(this);
232}
233
234inline void JSCell::mark()
235{
236 return Collector::markCell(this);
237}
238
239inline JSCell *JSValue::asCell()
240{
241 ASSERT(!JSImmediate::isImmediate(this));
242 return static_cast<JSCell *>(this);
243}
244
245inline const JSCell *JSValue::asCell() const
246{
247 ASSERT(!JSImmediate::isImmediate(this));
248 return static_cast<const JSCell *>(this);
249}
250
251inline bool JSValue::isUndefined() const
252{
253 return this == jsUndefined();
254}
255
256inline bool JSValue::isNull() const
257{
258 return this == jsNull();
259}
260
261inline bool JSValue::isUndefinedOrNull() const
262{
263 return JSImmediate::isUndefinedOrNull(this);
264}
265
266inline bool JSValue::isBoolean() const
267{
268 return JSImmediate::isBoolean(this);
269}
270
271inline bool JSValue::isNumber() const
272{
273 return JSImmediate::isNumber(this) || !JSImmediate::isImmediate(this) && asCell()->isNumber();
274}
275
276inline bool JSValue::isString() const
277{
278 return !JSImmediate::isImmediate(this) && asCell()->isString();
279}
280
281inline bool JSValue::isObject() const
282{
283 return !JSImmediate::isImmediate(this) && asCell()->isObject();
284}
285
286inline bool JSValue::getBoolean(bool& v) const
287{
288 if (JSImmediate::isBoolean(this)) {
289 v = JSImmediate::toBoolean(this);
290 return true;
291 }
292
293 return false;
294}
295
296inline bool JSValue::getBoolean() const
297{
298 return JSImmediate::isBoolean(this) ? JSImmediate::toBoolean(this) : false;
299}
300
301inline bool JSValue::getNumber(double& v) const
302{
303 if (JSImmediate::isImmediate(this)) {
304 v = JSImmediate::toDouble(this);
305 return true;
306 }
307 return asCell()->getNumber(v);
308}
309
310inline double JSValue::getNumber() const
311{
312 return JSImmediate::isImmediate(this) ? JSImmediate::toDouble(this) : asCell()->getNumber();
313}
314
315inline bool JSValue::getString(UString& s) const
316{
317 return !JSImmediate::isImmediate(this) && asCell()->getString(s);
318}
319
320inline UString JSValue::getString() const
321{
322 return JSImmediate::isImmediate(this) ? UString() : asCell()->getString();
323}
324
325inline JSObject *JSValue::getObject()
326{
327 return JSImmediate::isImmediate(this) ? 0 : asCell()->getObject();
328}
329
330inline const JSObject *JSValue::getObject() const
331{
332 return JSImmediate::isImmediate(this) ? 0 : asCell()->getObject();
333}
334
335inline bool JSValue::getUInt32(uint32_t& v) const
336{
337 if (JSImmediate::isImmediate(this)) {
338 double d = JSImmediate::toDouble(this);
339 if (!(d >= 0) || d > 0xFFFFFFFFUL) // true for NaN
340 return false;
341 v = static_cast<uint32_t>(d);
342 return JSImmediate::isNumber(this);
343 }
344 return asCell()->getUInt32(v);
345}
346
347inline void JSValue::mark()
348{
349 ASSERT(!JSImmediate::isImmediate(this)); // callers should check !marked() before calling mark()
350 asCell()->mark();
351}
352
353inline bool JSValue::marked() const
354{
355 return JSImmediate::isImmediate(this) || asCell()->marked();
356}
357
358inline JSType JSValue::type() const
359{
360 return JSImmediate::isImmediate(this) ? JSImmediate::type(this) : asCell()->type();
361}
362
363inline JSValue *JSValue::toPrimitive(ExecState *exec, JSType preferredType) const
364{
365 return JSImmediate::isImmediate(this) ? const_cast<JSValue *>(this) : asCell()->toPrimitive(exec, preferredType);
366}
367
368inline bool JSValue::toBoolean(ExecState *exec) const
369{
370 return JSImmediate::isImmediate(this) ? JSImmediate::toBoolean(this) : asCell()->toBoolean(exec);
371}
372
373inline double JSValue::toNumber(ExecState *exec) const
374{
375 return JSImmediate::isImmediate(this) ? JSImmediate::toDouble(this) : asCell()->toNumber(exec);
376}
377
378inline UString JSValue::toString(ExecState *exec) const
379{
380 return JSImmediate::isImmediate(this) ? JSImmediate::toString(this) : asCell()->toString(exec);
381}
382
383inline JSObject* JSValue::toObject(ExecState* exec) const
384{
385 return JSImmediate::isImmediate(this) ? JSImmediate::toObject(this, exec) : asCell()->toObject(exec);
386}
387
388} // namespace
389
390#endif // KJS_VALUE_H
Note: See TracBrowser for help on using the repository browser.