source: webkit/trunk/JavaScriptCore/kjs/MathObject.cpp@ 37337

Last change on this file since 37337 was 36977, checked in by [email protected], 17 years ago

JavaScriptCore:

2008-09-26 Sam Weinig <[email protected]>

Reviewed by Darin Adler.

Patch for https://bugs.webkit.org/show_bug.cgi?id=21152
Speedup static property get/put

Convert getting/setting static property values to use static functions
instead of storing an integer and switching in getValueProperty/putValueProperty.

  • kjs/JSObject.cpp: (JSC::JSObject::deleteProperty): (JSC::JSObject::getPropertyAttributes):
  • kjs/MathObject.cpp: (JSC::MathObject::getOwnPropertySlot):
  • kjs/NumberConstructor.cpp: (JSC::numberConstructorNaNValue): (JSC::numberConstructorNegInfinity): (JSC::numberConstructorPosInfinity): (JSC::numberConstructorMaxValue): (JSC::numberConstructorMinValue):
  • kjs/PropertySlot.h: (JSC::PropertySlot::):
  • kjs/RegExpConstructor.cpp: (JSC::regExpConstructorDollar1): (JSC::regExpConstructorDollar2): (JSC::regExpConstructorDollar3): (JSC::regExpConstructorDollar4): (JSC::regExpConstructorDollar5): (JSC::regExpConstructorDollar6): (JSC::regExpConstructorDollar7): (JSC::regExpConstructorDollar8): (JSC::regExpConstructorDollar9): (JSC::regExpConstructorInput): (JSC::regExpConstructorMultiline): (JSC::regExpConstructorLastMatch): (JSC::regExpConstructorLastParen): (JSC::regExpConstructorLeftContext): (JSC::regExpConstructorRightContext): (JSC::setRegExpConstructorInput): (JSC::setRegExpConstructorMultiline): (JSC::RegExpConstructor::setInput): (JSC::RegExpConstructor::setMultiline): (JSC::RegExpConstructor::multiline):
  • kjs/RegExpConstructor.h:
  • kjs/RegExpObject.cpp: (JSC::regExpObjectGlobal): (JSC::regExpObjectIgnoreCase): (JSC::regExpObjectMultiline): (JSC::regExpObjectSource): (JSC::regExpObjectLastIndex): (JSC::setRegExpObjectLastIndex):
  • kjs/RegExpObject.h: (JSC::RegExpObject::setLastIndex): (JSC::RegExpObject::lastIndex): (JSC::RegExpObject::RegExpObjectData::RegExpObjectData):
  • kjs/StructureID.cpp: (JSC::StructureID::getEnumerablePropertyNames):
  • kjs/create_hash_table:
  • kjs/lexer.cpp: (JSC::Lexer::lex):
  • kjs/lookup.cpp: (JSC::HashTable::createTable): (JSC::HashTable::deleteTable): (JSC::setUpStaticFunctionSlot):
  • kjs/lookup.h: (JSC::HashEntry::initialize): (JSC::HashEntry::setKey): (JSC::HashEntry::key): (JSC::HashEntry::attributes): (JSC::HashEntry::function): (JSC::HashEntry::functionLength): (JSC::HashEntry::propertyGetter): (JSC::HashEntry::propertyPutter): (JSC::HashEntry::lexerValue): (JSC::HashEntry::): (JSC::HashTable::entry): (JSC::getStaticPropertySlot): (JSC::getStaticValueSlot): (JSC::lookupPut):

WebCore:

2008-09-26 Sam Weinig <[email protected]>

Reviewed by Darin Adler.

Patch for https://bugs.webkit.org/show_bug.cgi?id=21152
Speedup static property get/put

  • DerivedSources.make:
  • GNUmakefile.am:
  • WebCore.pro:
  • WebCore.vcproj/WebCore.vcproj:
  • WebCore.xcodeproj/project.pbxproj:
  • bindings/js/JSDOMBinding.cpp:
  • bindings/js/JSDOMBinding.h:
  • bindings/js/JSDOMWindowBase.cpp: (WebCore::getDOMConstructor): (WebCore::jsDOMWindowBaseCrypto): (WebCore::jsDOMWindowBaseEvent): (WebCore::jsDOMWindowBaseImage): (WebCore::jsDOMWindowBaseMessageChannel): (WebCore::jsDOMWindowBaseOption): (WebCore::jsDOMWindowBaseXMLHttpRequest): (WebCore::jsDOMWindowBaseAudio): (WebCore::jsDOMWindowBaseXSLTProcessor): (WebCore::jsDOMWindowBaseOnabort): (WebCore::jsDOMWindowBaseOnblur): (WebCore::jsDOMWindowBaseOnchange): (WebCore::jsDOMWindowBaseOnclick): (WebCore::jsDOMWindowBaseOndblclick): (WebCore::jsDOMWindowBaseOnerror): (WebCore::jsDOMWindowBaseOnfocus): (WebCore::jsDOMWindowBaseOnkeydown): (WebCore::jsDOMWindowBaseOnkeypress): (WebCore::jsDOMWindowBaseOnkeyup): (WebCore::jsDOMWindowBaseOnload): (WebCore::jsDOMWindowBaseOnmousedown): (WebCore::jsDOMWindowBaseOnmousemove): (WebCore::jsDOMWindowBaseOnmouseout): (WebCore::jsDOMWindowBaseOnmouseover): (WebCore::jsDOMWindowBaseOnmouseup): (WebCore::jsDOMWindowBaseOnMouseWheel): (WebCore::jsDOMWindowBaseOnreset): (WebCore::jsDOMWindowBaseOnresize): (WebCore::jsDOMWindowBaseOnscroll): (WebCore::jsDOMWindowBaseOnsearch): (WebCore::jsDOMWindowBaseOnselect): (WebCore::jsDOMWindowBaseOnsubmit): (WebCore::jsDOMWindowBaseOnbeforeunload): (WebCore::jsDOMWindowBaseOnunload): (WebCore::jsDOMWindowBaseOnWebKitAnimationStart): (WebCore::jsDOMWindowBaseOnWebKitAnimationIteration): (WebCore::jsDOMWindowBaseOnWebKitAnimationEnd): (WebCore::jsDOMWindowBaseOnWebKitTransitionEnd): (WebCore::JSDOMWindowBase::getOwnPropertySlot): (WebCore::JSDOMWindowBase::put): (WebCore::setJSDOMWindowBaseOnabort): (WebCore::setJSDOMWindowBaseOnblur): (WebCore::setJSDOMWindowBaseOnchange): (WebCore::setJSDOMWindowBaseOnclick): (WebCore::setJSDOMWindowBaseOndblclick): (WebCore::setJSDOMWindowBaseOnerror): (WebCore::setJSDOMWindowBaseOnfocus): (WebCore::setJSDOMWindowBaseOnkeydown): (WebCore::setJSDOMWindowBaseOnkeypress): (WebCore::setJSDOMWindowBaseOnkeyup): (WebCore::setJSDOMWindowBaseOnload): (WebCore::setJSDOMWindowBaseOnmousedown): (WebCore::setJSDOMWindowBaseOnmousemove): (WebCore::setJSDOMWindowBaseOnmouseout): (WebCore::setJSDOMWindowBaseOnmouseover): (WebCore::setJSDOMWindowBaseOnmouseup): (WebCore::setJSDOMWindowBaseOnMouseWheel): (WebCore::setJSDOMWindowBaseOnreset): (WebCore::setJSDOMWindowBaseOnresize): (WebCore::setJSDOMWindowBaseOnscroll): (WebCore::setJSDOMWindowBaseOnsearch): (WebCore::setJSDOMWindowBaseOnselect): (WebCore::setJSDOMWindowBaseOnsubmit): (WebCore::setJSDOMWindowBaseOnbeforeunload): (WebCore::setJSDOMWindowBaseOnunload): (WebCore::setJSDOMWindowBaseOnWebKitAnimationStart): (WebCore::setJSDOMWindowBaseOnWebKitAnimationIteration): (WebCore::setJSDOMWindowBaseOnWebKitAnimationEnd): (WebCore::setJSDOMWindowBaseOnWebKitTransitionEnd): (WebCore::setJSDOMWindowBaseEvent): (WebCore::setJSDOMWindowBaseAudio): (WebCore::setJSDOMWindowBaseImage): (WebCore::setJSDOMWindowBaseMessageChannel): (WebCore::setJSDOMWindowBaseOption): (WebCore::setJSDOMWindowBaseXMLHttpRequest): (WebCore::setJSDOMWindowBaseXSLTProcessor):
  • bindings/js/JSDOMWindowBase.h:
  • bindings/js/JSDOMWindowCustom.cpp: (WebCore::nonCachingStaticCloseFunctionGetter): (WebCore::nonCachingStaticBlurFunctionGetter): (WebCore::nonCachingStaticFocusFunctionGetter): (WebCore::nonCachingStaticPostMessageFunctionGetter):
  • bindings/js/JSDOMWindowCustom.h: (WebCore::JSDOMWindow::customGetOwnPropertySlot):
  • bindings/js/JSEventTargetBase.cpp:
  • bindings/js/JSEventTargetBase.h: (WebCore::JSEventTargetBasePrototype::getOwnPropertySlot): (WebCore::JSEventTargetBasePrototype::classInfo):
  • bindings/js/JSEventTargetNode.cpp: (WebCore::): (WebCore::JSEventTargetNode::getOwnPropertySlot): (WebCore::JSEventTargetNode::put):
  • bindings/js/JSEventTargetNode.h: (WebCore::JSEventTargetNode::classInfo): (WebCore::JSEventTargetNode::prototypeClassName):
  • bindings/js/JSEventTargetSVGElementInstance.cpp: (WebCore::): (WebCore::JSEventTargetSVGElementInstance::getOwnPropertySlot): (WebCore::JSEventTargetSVGElementInstance::put):
  • bindings/js/JSEventTargetSVGElementInstance.h: (WebCore::JSEventTargetSVGElementInstance::classInfo): (WebCore::JSEventTargetSVGElementInstance::prototypeClassName):
  • bindings/js/JSHTMLInputElementCustom.cpp: (WebCore::JSHTMLInputElement::customGetOwnPropertySlot):
  • bindings/js/JSHistoryCustom.cpp: (WebCore::nonCachingStaticBackFunctionGetter): (WebCore::nonCachingStaticForwardFunctionGetter): (WebCore::nonCachingStaticGoFunctionGetter): (WebCore::JSHistory::customGetOwnPropertySlot):
  • bindings/js/JSLocationCustom.cpp: (WebCore::nonCachingStaticReplaceFunctionGetter): (WebCore::nonCachingStaticReloadFunctionGetter): (WebCore::nonCachingStaticAssignFunctionGetter): (WebCore::JSLocation::customGetOwnPropertySlot): (WebCore::JSLocation::customPut):
  • bindings/scripts/CodeGeneratorJS.pm:
  • Property svn:eol-style set to native
File size: 9.5 KB
Line 
1/*
2 * Copyright (C) 1999-2000 Harri Porten ([email protected])
3 * Copyright (C) 2007, 2008 Apple Inc. All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 */
20
21#include "config.h"
22#include "MathObject.h"
23
24#include "ObjectPrototype.h"
25#include "operations.h"
26#include <time.h>
27#include <wtf/Assertions.h>
28#include <wtf/MathExtras.h>
29
30namespace JSC {
31
32ASSERT_CLASS_FITS_IN_CELL(MathObject);
33
34static JSValue* mathProtoFuncAbs(ExecState*, JSObject*, JSValue*, const ArgList&);
35static JSValue* mathProtoFuncACos(ExecState*, JSObject*, JSValue*, const ArgList&);
36static JSValue* mathProtoFuncASin(ExecState*, JSObject*, JSValue*, const ArgList&);
37static JSValue* mathProtoFuncATan(ExecState*, JSObject*, JSValue*, const ArgList&);
38static JSValue* mathProtoFuncATan2(ExecState*, JSObject*, JSValue*, const ArgList&);
39static JSValue* mathProtoFuncCeil(ExecState*, JSObject*, JSValue*, const ArgList&);
40static JSValue* mathProtoFuncCos(ExecState*, JSObject*, JSValue*, const ArgList&);
41static JSValue* mathProtoFuncExp(ExecState*, JSObject*, JSValue*, const ArgList&);
42static JSValue* mathProtoFuncFloor(ExecState*, JSObject*, JSValue*, const ArgList&);
43static JSValue* mathProtoFuncLog(ExecState*, JSObject*, JSValue*, const ArgList&);
44static JSValue* mathProtoFuncMax(ExecState*, JSObject*, JSValue*, const ArgList&);
45static JSValue* mathProtoFuncMin(ExecState*, JSObject*, JSValue*, const ArgList&);
46static JSValue* mathProtoFuncPow(ExecState*, JSObject*, JSValue*, const ArgList&);
47static JSValue* mathProtoFuncRandom(ExecState*, JSObject*, JSValue*, const ArgList&);
48static JSValue* mathProtoFuncRound(ExecState*, JSObject*, JSValue*, const ArgList&);
49static JSValue* mathProtoFuncSin(ExecState*, JSObject*, JSValue*, const ArgList&);
50static JSValue* mathProtoFuncSqrt(ExecState*, JSObject*, JSValue*, const ArgList&);
51static JSValue* mathProtoFuncTan(ExecState*, JSObject*, JSValue*, const ArgList&);
52
53}
54
55#include "MathObject.lut.h"
56
57namespace JSC {
58
59// ------------------------------ MathObject --------------------------------
60
61const ClassInfo MathObject::info = { "Math", 0, 0, ExecState::mathTable };
62
63/* Source for MathObject.lut.h
64@begin mathTable
65 abs mathProtoFuncAbs DontEnum|Function 1
66 acos mathProtoFuncACos DontEnum|Function 1
67 asin mathProtoFuncASin DontEnum|Function 1
68 atan mathProtoFuncATan DontEnum|Function 1
69 atan2 mathProtoFuncATan2 DontEnum|Function 2
70 ceil mathProtoFuncCeil DontEnum|Function 1
71 cos mathProtoFuncCos DontEnum|Function 1
72 exp mathProtoFuncExp DontEnum|Function 1
73 floor mathProtoFuncFloor DontEnum|Function 1
74 log mathProtoFuncLog DontEnum|Function 1
75 max mathProtoFuncMax DontEnum|Function 2
76 min mathProtoFuncMin DontEnum|Function 2
77 pow mathProtoFuncPow DontEnum|Function 2
78 random mathProtoFuncRandom DontEnum|Function 0
79 round mathProtoFuncRound DontEnum|Function 1
80 sin mathProtoFuncSin DontEnum|Function 1
81 sqrt mathProtoFuncSqrt DontEnum|Function 1
82 tan mathProtoFuncTan DontEnum|Function 1
83@end
84*/
85
86MathObject::MathObject(ExecState* exec, PassRefPtr<StructureID> structure)
87 : JSObject(structure)
88{
89 putDirect(Identifier(exec, "E"), jsNumber(exec, exp(1.0)), DontDelete | DontEnum | ReadOnly);
90 putDirect(Identifier(exec, "LN2"), jsNumber(exec, log(2.0)), DontDelete | DontEnum | ReadOnly);
91 putDirect(Identifier(exec, "LN10"), jsNumber(exec, log(10.0)), DontDelete | DontEnum | ReadOnly);
92 putDirect(Identifier(exec, "LOG2E"), jsNumber(exec, 1.0 / log(2.0)), DontDelete | DontEnum | ReadOnly);
93 putDirect(Identifier(exec, "LOG10E"), jsNumber(exec, 1.0 / log(10.0)), DontDelete | DontEnum | ReadOnly);
94 putDirect(Identifier(exec, "PI"), jsNumber(exec, piDouble), DontDelete | DontEnum | ReadOnly);
95 putDirect(Identifier(exec, "SQRT1_2"), jsNumber(exec, sqrt(0.5)), DontDelete | DontEnum | ReadOnly);
96 putDirect(Identifier(exec, "SQRT2"), jsNumber(exec, sqrt(2.0)), DontDelete | DontEnum | ReadOnly);
97}
98
99// ECMA 15.8
100
101bool MathObject::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot &slot)
102{
103 const HashEntry* entry = ExecState::mathTable(exec)->entry(exec, propertyName);
104
105 if (!entry)
106 return JSObject::getOwnPropertySlot(exec, propertyName, slot);
107
108 ASSERT(entry->attributes() & Function);
109 setUpStaticFunctionSlot(exec, entry, this, propertyName, slot);
110 return true;
111}
112
113// ------------------------------ Functions --------------------------------
114
115JSValue* mathProtoFuncAbs(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
116{
117 double arg = args.at(exec, 0)->toNumber(exec);
118 return signbit(arg) ? jsNumber(exec, -arg) : jsNumber(exec, arg);
119}
120
121JSValue* mathProtoFuncACos(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
122{
123 return jsNumber(exec, acos(args.at(exec, 0)->toNumber(exec)));
124}
125
126JSValue* mathProtoFuncASin(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
127{
128 return jsNumber(exec, asin(args.at(exec, 0)->toNumber(exec)));
129}
130
131JSValue* mathProtoFuncATan(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
132{
133 return jsNumber(exec, atan(args.at(exec, 0)->toNumber(exec)));
134}
135
136JSValue* mathProtoFuncATan2(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
137{
138 return jsNumber(exec, atan2(args.at(exec, 0)->toNumber(exec), args.at(exec, 1)->toNumber(exec)));
139}
140
141JSValue* mathProtoFuncCeil(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
142{
143 double arg = args.at(exec, 0)->toNumber(exec);
144 if (signbit(arg) && arg > -1.0)
145 return jsNumber(exec, -0.0);
146 return jsNumber(exec, ceil(arg));
147}
148
149JSValue* mathProtoFuncCos(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
150{
151 return jsNumber(exec, cos(args.at(exec, 0)->toNumber(exec)));
152}
153
154JSValue* mathProtoFuncExp(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
155{
156 return jsNumber(exec, exp(args.at(exec, 0)->toNumber(exec)));
157}
158
159JSValue* mathProtoFuncFloor(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
160{
161 double arg = args.at(exec, 0)->toNumber(exec);
162 if (signbit(arg) && arg == 0.0)
163 return jsNumber(exec, -0.0);
164 return jsNumber(exec, floor(arg));
165}
166
167JSValue* mathProtoFuncLog(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
168{
169 return jsNumber(exec, log(args.at(exec, 0)->toNumber(exec)));
170}
171
172JSValue* mathProtoFuncMax(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
173{
174 unsigned argsCount = args.size();
175 double result = -Inf;
176 for (unsigned k = 0; k < argsCount; ++k) {
177 double val = args.at(exec, k)->toNumber(exec);
178 if (isnan(val)) {
179 result = NaN;
180 break;
181 }
182 if (val > result || (val == 0 && result == 0 && !signbit(val)))
183 result = val;
184 }
185 return jsNumber(exec, result);
186}
187
188JSValue* mathProtoFuncMin(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
189{
190 unsigned argsCount = args.size();
191 double result = +Inf;
192 for (unsigned k = 0; k < argsCount; ++k) {
193 double val = args.at(exec, k)->toNumber(exec);
194 if (isnan(val)) {
195 result = NaN;
196 break;
197 }
198 if (val < result || (val == 0 && result == 0 && signbit(val)))
199 result = val;
200 }
201 return jsNumber(exec, result);
202}
203
204JSValue* mathProtoFuncPow(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
205{
206 // ECMA 15.8.2.1.13
207
208 double arg = args.at(exec, 0)->toNumber(exec);
209 double arg2 = args.at(exec, 1)->toNumber(exec);
210
211 if (isnan(arg2))
212 return jsNaN(exec);
213 if (isinf(arg2) && fabs(arg) == 1)
214 return jsNaN(exec);
215 return jsNumber(exec, pow(arg, arg2));
216}
217
218JSValue* mathProtoFuncRandom(ExecState* exec, JSObject*, JSValue*, const ArgList&)
219{
220#if !ENABLE(JSC_MULTIPLE_THREADS)
221 static bool didInitRandom;
222 if (!didInitRandom) {
223 wtf_random_init();
224 didInitRandom = true;
225 }
226#endif
227
228 return jsNumber(exec, wtf_random());
229}
230
231JSValue* mathProtoFuncRound(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
232{
233 double arg = args.at(exec, 0)->toNumber(exec);
234 if (signbit(arg) && arg >= -0.5)
235 return jsNumber(exec, -0.0);
236 return jsNumber(exec, floor(arg + 0.5));
237}
238
239JSValue* mathProtoFuncSin(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
240{
241 return jsNumber(exec, sin(args.at(exec, 0)->toNumber(exec)));
242}
243
244JSValue* mathProtoFuncSqrt(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
245{
246 return jsNumber(exec, sqrt(args.at(exec, 0)->toNumber(exec)));
247}
248
249JSValue* mathProtoFuncTan(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
250{
251 return jsNumber(exec, tan(args.at(exec, 0)->toNumber(exec)));
252}
253
254} // namespace JSC
Note: See TracBrowser for help on using the repository browser.