source: webkit/trunk/JavaScriptCore/runtime/JSValue.cpp@ 67583

Last change on this file since 67583 was 65698, checked in by [email protected], 15 years ago

2010-08-19 Andreas Kling <[email protected]>

Reviewed by Geoffrey Garen.

JSC: Move the static_cast into to(U)Int32 fast case
https://bugs.webkit.org/show_bug.cgi?id=44037

Do the static_cast<(u)int32_t> inline to avoid the function call overhead
for easily converted values (within (u)int32_t range.)

  • runtime/JSValue.cpp: (JSC::toInt32SlowCase): (JSC::toUInt32SlowCase):
  • runtime/JSValue.h: (JSC::JSValue::toInt32): (JSC::JSValue::toUInt32):
  • Property svn:eol-style set to native
File size: 4.9 KB
Line 
1/*
2 * Copyright (C) 1999-2001 Harri Porten ([email protected])
3 * Copyright (C) 2001 Peter Kelly ([email protected])
4 * Copyright (C) 2003, 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#include "config.h"
24#include "JSValue.h"
25
26#include "BooleanConstructor.h"
27#include "BooleanPrototype.h"
28#include "Error.h"
29#include "ExceptionHelpers.h"
30#include "JSGlobalObject.h"
31#include "JSFunction.h"
32#include "JSNotAnObject.h"
33#include "NumberObject.h"
34#include <wtf/MathExtras.h>
35#include <wtf/StringExtras.h>
36
37namespace JSC {
38
39static const double D32 = 4294967296.0;
40
41// ECMA 9.4
42double JSValue::toInteger(ExecState* exec) const
43{
44 if (isInt32())
45 return asInt32();
46 double d = toNumber(exec);
47 return isnan(d) ? 0.0 : trunc(d);
48}
49
50double JSValue::toIntegerPreserveNaN(ExecState* exec) const
51{
52 if (isInt32())
53 return asInt32();
54 return trunc(toNumber(exec));
55}
56
57JSObject* JSValue::toObjectSlowCase(ExecState* exec) const
58{
59 ASSERT(!isCell());
60
61 if (isInt32() || isDouble())
62 return constructNumber(exec, asValue());
63 if (isTrue() || isFalse())
64 return constructBooleanFromImmediateBoolean(exec, asValue());
65 ASSERT(isUndefinedOrNull());
66 JSNotAnObjectErrorStub* exception = createNotAnObjectErrorStub(exec, isNull());
67 throwError(exec, exception);
68 return new (exec) JSNotAnObject(exec, exception);
69}
70
71JSObject* JSValue::toThisObjectSlowCase(ExecState* exec) const
72{
73 ASSERT(!isCell());
74
75 if (isInt32() || isDouble())
76 return constructNumber(exec, asValue());
77 if (isTrue() || isFalse())
78 return constructBooleanFromImmediateBoolean(exec, asValue());
79 ASSERT(isUndefinedOrNull());
80 return exec->globalThisValue();
81}
82
83JSObject* JSValue::synthesizeObject(ExecState* exec) const
84{
85 ASSERT(!isCell());
86 if (isNumber())
87 return constructNumber(exec, asValue());
88 if (isBoolean())
89 return constructBooleanFromImmediateBoolean(exec, asValue());
90
91 JSNotAnObjectErrorStub* exception = createNotAnObjectErrorStub(exec, isNull());
92 throwError(exec, exception);
93 return new (exec) JSNotAnObject(exec, exception);
94}
95
96JSObject* JSValue::synthesizePrototype(ExecState* exec) const
97{
98 ASSERT(!isCell());
99 if (isNumber())
100 return exec->lexicalGlobalObject()->numberPrototype();
101 if (isBoolean())
102 return exec->lexicalGlobalObject()->booleanPrototype();
103
104 JSNotAnObjectErrorStub* exception = createNotAnObjectErrorStub(exec, isNull());
105 throwError(exec, exception);
106 return new (exec) JSNotAnObject(exec, exception);
107}
108
109#ifndef NDEBUG
110char* JSValue::description()
111{
112 static const size_t size = 32;
113 static char description[size];
114
115 if (!*this)
116 snprintf(description, size, "<JSValue()>");
117 else if (isInt32())
118 snprintf(description, size, "Int32: %d", asInt32());
119 else if (isDouble())
120 snprintf(description, size, "Double: %lf", asDouble());
121 else if (isCell())
122 snprintf(description, size, "Cell: %p", asCell());
123 else if (isTrue())
124 snprintf(description, size, "True");
125 else if (isFalse())
126 snprintf(description, size, "False");
127 else if (isNull())
128 snprintf(description, size, "Null");
129 else if (isUndefined())
130 snprintf(description, size, "Undefined");
131 else
132 snprintf(description, size, "INVALID");
133
134 return description;
135}
136#endif
137
138int32_t toInt32SlowCase(double d, bool& ok)
139{
140 if (isnan(d) || isinf(d)) {
141 ok = false;
142 return 0;
143 }
144
145 ok = true;
146
147 double d32 = fmod(trunc(d), D32);
148 if (d32 >= D32 / 2)
149 d32 -= D32;
150 else if (d32 < -D32 / 2)
151 d32 += D32;
152 return static_cast<int32_t>(d32);
153}
154
155uint32_t toUInt32SlowCase(double d, bool& ok)
156{
157 if (isnan(d) || isinf(d)) {
158 ok = false;
159 return 0;
160 }
161
162 ok = true;
163
164 double d32 = fmod(trunc(d), D32);
165 if (d32 < 0)
166 d32 += D32;
167 return static_cast<uint32_t>(d32);
168}
169
170NEVER_INLINE double nonInlineNaN()
171{
172#if OS(SYMBIAN)
173 return nanval();
174#else
175 return std::numeric_limits<double>::quiet_NaN();
176#endif
177}
178
179bool JSValue::isValidCallee()
180{
181 return asObject(asObject(asCell())->getAnonymousValue(0))->isGlobalObject();
182}
183
184} // namespace JSC
Note: See TracBrowser for help on using the repository browser.