source: webkit/trunk/JavaScriptCore/kjs/value.cpp@ 18837

Last change on this file since 18837 was 18715, checked in by ap, 18 years ago

2007-01-09 Mitz Pettel <[email protected]>

Reviewed by Darin.

JavaScriptCore:

  • JavaScriptCore.exp:
  • kjs/value.cpp: (KJS::JSValue::toInt32): Folded toInt32Inline into this method, which was its only caller. (KJS::JSValue::toUInt32): Added a variant that reports if the conversion has succeeded.
  • kjs/value.h:

WebCore:

  • bindings/js/kjs_html.cpp: (KJS::JSHTMLCollectionProtoFunc::callAsFunction): Changed item() to fall back to namedItem() if its argument does not convert to a number.

LayoutTests:

  • fast/dom/collection-namedItem-via-item-expected.txt: Added.
  • fast/dom/collection-namedItem-via-item.html: Added.
  • Property svn:eol-style set to native
File size: 4.1 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 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#include "config.h"
25#include "value.h"
26
27#include "error_object.h"
28#include "nodes.h"
29#include "operations.h"
30#include <stdio.h>
31#include <string.h>
32#include <wtf/MathExtras.h>
33
34namespace KJS {
35
36static const double D16 = 65536.0;
37static const double D32 = 4294967296.0;
38
39void *JSCell::operator new(size_t size)
40{
41 return Collector::allocate(size);
42}
43
44bool JSCell::getUInt32(unsigned&) const
45{
46 return false;
47}
48
49// ECMA 9.4
50double JSValue::toInteger(ExecState *exec) const
51{
52 uint32_t i;
53 if (getUInt32(i))
54 return i;
55 return roundValue(exec, const_cast<JSValue*>(this));
56}
57
58int32_t JSValue::toInt32(ExecState* exec) const
59{
60 bool ok;
61 return toInt32(exec, ok);
62}
63
64int32_t JSValue::toInt32(ExecState* exec, bool& ok) const
65{
66 ok = true;
67
68 uint32_t i;
69 if (getUInt32(i))
70 return i;
71
72 double d = roundValue(exec, const_cast<JSValue*>(this));
73 if (isNaN(d) || isInf(d)) {
74 ok = false;
75 return 0;
76 }
77 double d32 = fmod(d, D32);
78
79 if (d32 >= D32 / 2)
80 d32 -= D32;
81 else if (d32 < -D32 / 2)
82 d32 += D32;
83
84 return static_cast<int32_t>(d32);
85}
86
87uint32_t JSValue::toUInt32(ExecState* exec) const
88{
89 bool ok;
90 return toUInt32(exec, ok);
91}
92
93uint32_t JSValue::toUInt32(ExecState* exec, bool& ok) const
94{
95 ok = true;
96
97 uint32_t i;
98 if (getUInt32(i))
99 return i;
100
101 double d = roundValue(exec, const_cast<JSValue*>(this));
102 if (isNaN(d) || isInf(d)) {
103 ok = false;
104 return 0;
105 }
106 double d32 = fmod(d, D32);
107
108 if (d32 < 0)
109 d32 += D32;
110
111 return static_cast<uint32_t>(d32);
112}
113
114uint16_t JSValue::toUInt16(ExecState *exec) const
115{
116 uint32_t i;
117 if (getUInt32(i))
118 return static_cast<uint16_t>(i);
119
120 double d = roundValue(exec, const_cast<JSValue*>(this));
121 if (isNaN(d) || isInf(d))
122 return 0;
123 double d16 = fmod(d, D16);
124
125 if (d16 < 0)
126 d16 += D16;
127
128 return static_cast<uint16_t>(d16);
129}
130
131bool JSCell::getNumber(double &numericValue) const
132{
133 if (!isNumber())
134 return false;
135 numericValue = static_cast<const NumberImp *>(this)->value();
136 return true;
137}
138
139double JSCell::getNumber() const
140{
141 return isNumber() ? static_cast<const NumberImp *>(this)->value() : NaN;
142}
143
144bool JSCell::getString(UString &stringValue) const
145{
146 if (!isString())
147 return false;
148 stringValue = static_cast<const StringImp *>(this)->value();
149 return true;
150}
151
152UString JSCell::getString() const
153{
154 return isString() ? static_cast<const StringImp *>(this)->value() : UString();
155}
156
157JSObject *JSCell::getObject()
158{
159 return isObject() ? static_cast<JSObject *>(this) : 0;
160}
161
162const JSObject *JSCell::getObject() const
163{
164 return isObject() ? static_cast<const JSObject *>(this) : 0;
165}
166
167JSCell *jsString(const char *s)
168{
169 return new StringImp(s ? s : "");
170}
171
172JSCell *jsString(const UString &s)
173{
174 return s.isNull() ? new StringImp("") : new StringImp(s);
175}
176
177// This method includes a PIC branch to set up the NumberImp's vtable, so we quarantine
178// it in a separate function to keep the normal case speedy.
179JSValue *jsNumberCell(double d)
180{
181 return new NumberImp(d);
182}
183
184} // namespace KJS
Note: See TracBrowser for help on using the repository browser.