source: webkit/trunk/JavaScriptCore/API/JSCallbackObjectFunctions.h@ 36578

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

2008-09-14 Maciej Stachowiak <[email protected]>

Reviewed by Cameron Zwarich.


  • split the "prototype" lookup for hasInstance into opcode stream so it can be cached


~5% speedup on v8 earley-boyer test

  • API/JSCallbackObject.h: Add a parameter for the pre-looked-up prototype.
  • API/JSCallbackObjectFunctions.h: (JSC::::hasInstance): Ditto.
  • API/JSValueRef.cpp: (JSValueIsInstanceOfConstructor): Look up and pass in prototype.
  • JavaScriptCore.exp:
  • VM/CTI.cpp: (JSC::CTI::privateCompileMainPass): Pass along prototype.
  • VM/CodeBlock.cpp: (JSC::CodeBlock::dump): Print third arg.
  • VM/CodeGenerator.cpp: (JSC::CodeGenerator::emitInstanceOf): Implement this, now that there is a third argument.
  • VM/CodeGenerator.h:
  • VM/Machine.cpp: (JSC::Machine::privateExecute): Pass along the prototype. (JSC::Machine::cti_op_instanceof): ditto
  • kjs/JSObject.cpp: (JSC::JSObject::hasInstance): Expect to get a pre-looked-up prototype.
  • kjs/JSObject.h:
  • kjs/nodes.cpp: (JSC::InstanceOfNode::emitCode): Emit a get_by_id of the prototype property and pass that register to instanceof.
  • kjs/nodes.h:
  • Property svn:eol-style set to native
File size: 20.6 KB
Line 
1/*
2 * Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2007 Eric Seidel <[email protected]>
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "APICast.h"
28#include "Error.h"
29#include "JSCallbackFunction.h"
30#include "JSClassRef.h"
31#include "JSGlobalObject.h"
32#include "JSLock.h"
33#include "JSObjectRef.h"
34#include "JSString.h"
35#include "JSStringRef.h"
36#include "OpaqueJSString.h"
37#include "PropertyNameArray.h"
38#include <wtf/Vector.h>
39
40namespace JSC {
41
42template <class Base>
43JSCallbackObject<Base>::JSCallbackObject(ExecState* exec, JSClassRef jsClass, JSObject* prototype, void* data)
44 : Base(prototype)
45 , m_callbackObjectData(new JSCallbackObjectData(data, jsClass))
46{
47 init(exec);
48}
49
50// Global object constructor.
51// FIXME: Move this into a separate JSGlobalCallbackObject class derived from this one.
52template <class Base>
53JSCallbackObject<Base>::JSCallbackObject(JSGlobalData* globalData, JSClassRef jsClass)
54 : Base(globalData)
55 , m_callbackObjectData(new JSCallbackObjectData(0, jsClass))
56{
57 ASSERT(Base::isGlobalObject());
58 init(static_cast<JSGlobalObject*>(this)->globalExec());
59}
60
61template <class Base>
62void JSCallbackObject<Base>::init(ExecState* exec)
63{
64 ASSERT(exec);
65
66 Vector<JSObjectInitializeCallback, 16> initRoutines;
67 JSClassRef jsClass = classRef();
68 do {
69 if (JSObjectInitializeCallback initialize = jsClass->initialize)
70 initRoutines.append(initialize);
71 } while ((jsClass = jsClass->parentClass));
72
73 // initialize from base to derived
74 for (int i = static_cast<int>(initRoutines.size()) - 1; i >= 0; i--) {
75 JSLock::DropAllLocks dropAllLocks(exec);
76 JSObjectInitializeCallback initialize = initRoutines[i];
77 initialize(toRef(exec), toRef(this));
78 }
79}
80
81template <class Base>
82JSCallbackObject<Base>::~JSCallbackObject()
83{
84 JSObjectRef thisRef = toRef(this);
85
86 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass)
87 if (JSObjectFinalizeCallback finalize = jsClass->finalize)
88 finalize(thisRef);
89}
90
91template <class Base>
92UString JSCallbackObject<Base>::className() const
93{
94 UString thisClassName = classRef()->className();
95 if (!thisClassName.isNull())
96 return thisClassName;
97
98 return Base::className();
99}
100
101template <class Base>
102bool JSCallbackObject<Base>::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
103{
104 JSContextRef ctx = toRef(exec);
105 JSObjectRef thisRef = toRef(this);
106 RefPtr<OpaqueJSString> propertyNameRef;
107
108 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass) {
109 // optional optimization to bypass getProperty in cases when we only need to know if the property exists
110 if (JSObjectHasPropertyCallback hasProperty = jsClass->hasProperty) {
111 if (!propertyNameRef)
112 propertyNameRef = OpaqueJSString::create(propertyName.ustring());
113 JSLock::DropAllLocks dropAllLocks(exec);
114 if (hasProperty(ctx, thisRef, propertyNameRef.get())) {
115 slot.setCustom(this, callbackGetter);
116 return true;
117 }
118 } else if (JSObjectGetPropertyCallback getProperty = jsClass->getProperty) {
119 if (!propertyNameRef)
120 propertyNameRef = OpaqueJSString::create(propertyName.ustring());
121 JSLock::DropAllLocks dropAllLocks(exec);
122 if (JSValueRef value = getProperty(ctx, thisRef, propertyNameRef.get(), toRef(exec->exceptionSlot()))) {
123 // cache the value so we don't have to compute it again
124 // FIXME: This violates the PropertySlot design a little bit.
125 // We should either use this optimization everywhere, or nowhere.
126 slot.setCustom(reinterpret_cast<JSObject*>(toJS(value)), cachedValueGetter);
127 return true;
128 }
129 }
130
131 if (OpaqueJSClassStaticValuesTable* staticValues = jsClass->staticValues(exec)) {
132 if (staticValues->contains(propertyName.ustring().rep())) {
133 slot.setCustom(this, staticValueGetter);
134 return true;
135 }
136 }
137
138 if (OpaqueJSClassStaticFunctionsTable* staticFunctions = jsClass->staticFunctions(exec)) {
139 if (staticFunctions->contains(propertyName.ustring().rep())) {
140 slot.setCustom(this, staticFunctionGetter);
141 return true;
142 }
143 }
144 }
145
146 return Base::getOwnPropertySlot(exec, propertyName, slot);
147}
148
149template <class Base>
150bool JSCallbackObject<Base>::getOwnPropertySlot(ExecState* exec, unsigned propertyName, PropertySlot& slot)
151{
152 return getOwnPropertySlot(exec, Identifier::from(exec, propertyName), slot);
153}
154
155template <class Base>
156void JSCallbackObject<Base>::put(ExecState* exec, const Identifier& propertyName, JSValue* value, PutPropertySlot& slot)
157{
158 JSContextRef ctx = toRef(exec);
159 JSObjectRef thisRef = toRef(this);
160 RefPtr<OpaqueJSString> propertyNameRef;
161 JSValueRef valueRef = toRef(value);
162
163 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass) {
164 if (JSObjectSetPropertyCallback setProperty = jsClass->setProperty) {
165 if (!propertyNameRef)
166 propertyNameRef = OpaqueJSString::create(propertyName.ustring());
167 JSLock::DropAllLocks dropAllLocks(exec);
168 if (setProperty(ctx, thisRef, propertyNameRef.get(), valueRef, toRef(exec->exceptionSlot())))
169 return;
170 }
171
172 if (OpaqueJSClassStaticValuesTable* staticValues = jsClass->staticValues(exec)) {
173 if (StaticValueEntry* entry = staticValues->get(propertyName.ustring().rep())) {
174 if (entry->attributes & kJSPropertyAttributeReadOnly)
175 return;
176 if (JSObjectSetPropertyCallback setProperty = entry->setProperty) {
177 if (!propertyNameRef)
178 propertyNameRef = OpaqueJSString::create(propertyName.ustring());
179 JSLock::DropAllLocks dropAllLocks(exec);
180 if (setProperty(ctx, thisRef, propertyNameRef.get(), valueRef, toRef(exec->exceptionSlot())))
181 return;
182 } else
183 throwError(exec, ReferenceError, "Attempt to set a property that is not settable.");
184 }
185 }
186
187 if (OpaqueJSClassStaticFunctionsTable* staticFunctions = jsClass->staticFunctions(exec)) {
188 if (StaticFunctionEntry* entry = staticFunctions->get(propertyName.ustring().rep())) {
189 if (entry->attributes & kJSPropertyAttributeReadOnly)
190 return;
191 JSCallbackObject<Base>::putDirect(propertyName, value); // put as override property
192 return;
193 }
194 }
195 }
196
197 return Base::put(exec, propertyName, value, slot);
198}
199
200template <class Base>
201bool JSCallbackObject<Base>::deleteProperty(ExecState* exec, const Identifier& propertyName)
202{
203 JSContextRef ctx = toRef(exec);
204 JSObjectRef thisRef = toRef(this);
205 RefPtr<OpaqueJSString> propertyNameRef;
206
207 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass) {
208 if (JSObjectDeletePropertyCallback deleteProperty = jsClass->deleteProperty) {
209 if (!propertyNameRef)
210 propertyNameRef = OpaqueJSString::create(propertyName.ustring());
211 JSLock::DropAllLocks dropAllLocks(exec);
212 if (deleteProperty(ctx, thisRef, propertyNameRef.get(), toRef(exec->exceptionSlot())))
213 return true;
214 }
215
216 if (OpaqueJSClassStaticValuesTable* staticValues = jsClass->staticValues(exec)) {
217 if (StaticValueEntry* entry = staticValues->get(propertyName.ustring().rep())) {
218 if (entry->attributes & kJSPropertyAttributeDontDelete)
219 return false;
220 return true;
221 }
222 }
223
224 if (OpaqueJSClassStaticFunctionsTable* staticFunctions = jsClass->staticFunctions(exec)) {
225 if (StaticFunctionEntry* entry = staticFunctions->get(propertyName.ustring().rep())) {
226 if (entry->attributes & kJSPropertyAttributeDontDelete)
227 return false;
228 return true;
229 }
230 }
231 }
232
233 return Base::deleteProperty(exec, propertyName);
234}
235
236template <class Base>
237bool JSCallbackObject<Base>::deleteProperty(ExecState* exec, unsigned propertyName)
238{
239 return deleteProperty(exec, Identifier::from(exec, propertyName));
240}
241
242template <class Base>
243ConstructType JSCallbackObject<Base>::getConstructData(ConstructData& constructData)
244{
245 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass) {
246 if (jsClass->callAsConstructor) {
247 constructData.native.function = construct;
248 return ConstructTypeHost;
249 }
250 }
251 return ConstructTypeNone;
252}
253
254template <class Base>
255JSObject* JSCallbackObject<Base>::construct(ExecState* exec, JSObject* constructor, const ArgList& args)
256{
257 JSContextRef execRef = toRef(exec);
258 JSObjectRef constructorRef = toRef(constructor);
259
260 for (JSClassRef jsClass = static_cast<JSCallbackObject<Base>*>(constructor)->classRef(); jsClass; jsClass = jsClass->parentClass) {
261 if (JSObjectCallAsConstructorCallback callAsConstructor = jsClass->callAsConstructor) {
262 int argumentCount = static_cast<int>(args.size());
263 Vector<JSValueRef, 16> arguments(argumentCount);
264 for (int i = 0; i < argumentCount; i++)
265 arguments[i] = toRef(args.at(exec, i));
266 JSLock::DropAllLocks dropAllLocks(exec);
267 return toJS(callAsConstructor(execRef, constructorRef, argumentCount, arguments.data(), toRef(exec->exceptionSlot())));
268 }
269 }
270
271 ASSERT_NOT_REACHED(); // getConstructData should prevent us from reaching here
272 return 0;
273}
274
275template <class Base>
276bool JSCallbackObject<Base>::implementsHasInstance() const
277{
278 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass)
279 if (jsClass->hasInstance)
280 return true;
281
282 return false;
283}
284
285template <class Base>
286bool JSCallbackObject<Base>::hasInstance(ExecState* exec, JSValue* value, JSValue*)
287{
288 JSContextRef execRef = toRef(exec);
289 JSObjectRef thisRef = toRef(this);
290
291 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass) {
292 if (JSObjectHasInstanceCallback hasInstance = jsClass->hasInstance) {
293 JSLock::DropAllLocks dropAllLocks(exec);
294 return hasInstance(execRef, thisRef, toRef(value), toRef(exec->exceptionSlot()));
295 }
296 }
297 ASSERT_NOT_REACHED(); // implementsHasInstance should prevent us from reaching here
298 return 0;
299}
300
301template <class Base>
302CallType JSCallbackObject<Base>::getCallData(CallData& callData)
303{
304 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass) {
305 if (jsClass->callAsFunction) {
306 callData.native.function = call;
307 return CallTypeHost;
308 }
309 }
310 return CallTypeNone;
311}
312
313template <class Base>
314JSValue* JSCallbackObject<Base>::call(ExecState* exec, JSObject* functionObject, JSValue* thisValue, const ArgList& args)
315{
316 JSContextRef execRef = toRef(exec);
317 JSObjectRef functionRef = toRef(functionObject);
318 JSObjectRef thisObjRef = toRef(thisValue->toThisObject(exec));
319
320 for (JSClassRef jsClass = static_cast<JSCallbackObject<Base>*>(functionObject)->classRef(); jsClass; jsClass = jsClass->parentClass) {
321 if (JSObjectCallAsFunctionCallback callAsFunction = jsClass->callAsFunction) {
322 int argumentCount = static_cast<int>(args.size());
323 Vector<JSValueRef, 16> arguments(argumentCount);
324 for (int i = 0; i < argumentCount; i++)
325 arguments[i] = toRef(args.at(exec, i));
326 JSLock::DropAllLocks dropAllLocks(exec);
327 return toJS(callAsFunction(execRef, functionRef, thisObjRef, argumentCount, arguments.data(), toRef(exec->exceptionSlot())));
328 }
329 }
330
331 ASSERT_NOT_REACHED(); // getCallData should prevent us from reaching here
332 return 0;
333}
334
335template <class Base>
336void JSCallbackObject<Base>::getPropertyNames(ExecState* exec, PropertyNameArray& propertyNames)
337{
338 JSContextRef execRef = toRef(exec);
339 JSObjectRef thisRef = toRef(this);
340
341 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass) {
342 if (JSObjectGetPropertyNamesCallback getPropertyNames = jsClass->getPropertyNames) {
343 JSLock::DropAllLocks dropAllLocks(exec);
344 getPropertyNames(execRef, thisRef, toRef(&propertyNames));
345 }
346
347 if (OpaqueJSClassStaticValuesTable* staticValues = jsClass->staticValues(exec)) {
348 typedef OpaqueJSClassStaticValuesTable::const_iterator iterator;
349 iterator end = staticValues->end();
350 for (iterator it = staticValues->begin(); it != end; ++it) {
351 UString::Rep* name = it->first.get();
352 StaticValueEntry* entry = it->second;
353 if (entry->getProperty && !(entry->attributes & kJSPropertyAttributeDontEnum))
354 propertyNames.add(Identifier(exec, name));
355 }
356 }
357
358 if (OpaqueJSClassStaticFunctionsTable* staticFunctions = jsClass->staticFunctions(exec)) {
359 typedef OpaqueJSClassStaticFunctionsTable::const_iterator iterator;
360 iterator end = staticFunctions->end();
361 for (iterator it = staticFunctions->begin(); it != end; ++it) {
362 UString::Rep* name = it->first.get();
363 StaticFunctionEntry* entry = it->second;
364 if (!(entry->attributes & kJSPropertyAttributeDontEnum))
365 propertyNames.add(Identifier(exec, name));
366 }
367 }
368 }
369
370 Base::getPropertyNames(exec, propertyNames);
371}
372
373template <class Base>
374double JSCallbackObject<Base>::toNumber(ExecState* exec) const
375{
376 // We need this check to guard against the case where this object is rhs of
377 // a binary expression where lhs threw an exception in its conversion to
378 // primitive
379 if (exec->hadException())
380 return NaN;
381 JSContextRef ctx = toRef(exec);
382 JSObjectRef thisRef = toRef(this);
383
384 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass)
385 if (JSObjectConvertToTypeCallback convertToType = jsClass->convertToType) {
386 JSLock::DropAllLocks dropAllLocks(exec);
387 if (JSValueRef value = convertToType(ctx, thisRef, kJSTypeNumber, toRef(exec->exceptionSlot())))
388 return toJS(value)->getNumber();
389 }
390
391 return Base::toNumber(exec);
392}
393
394template <class Base>
395UString JSCallbackObject<Base>::toString(ExecState* exec) const
396{
397 JSContextRef ctx = toRef(exec);
398 JSObjectRef thisRef = toRef(this);
399
400 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass)
401 if (JSObjectConvertToTypeCallback convertToType = jsClass->convertToType) {
402 JSValueRef value;
403 {
404 JSLock::DropAllLocks dropAllLocks(exec);
405 value = convertToType(ctx, thisRef, kJSTypeString, toRef(exec->exceptionSlot()));
406 }
407 if (value)
408 return toJS(value)->getString();
409 }
410
411 return Base::toString(exec);
412}
413
414template <class Base>
415void JSCallbackObject<Base>::setPrivate(void* data)
416{
417 m_callbackObjectData->privateData = data;
418}
419
420template <class Base>
421void* JSCallbackObject<Base>::getPrivate()
422{
423 return m_callbackObjectData->privateData;
424}
425
426template <class Base>
427bool JSCallbackObject<Base>::inherits(JSClassRef c) const
428{
429 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass)
430 if (jsClass == c)
431 return true;
432
433 return false;
434}
435
436template <class Base>
437JSValue* JSCallbackObject<Base>::cachedValueGetter(ExecState*, const Identifier&, const PropertySlot& slot)
438{
439 JSValue* v = slot.slotBase();
440 ASSERT(v);
441 return v;
442}
443
444template <class Base>
445JSValue* JSCallbackObject<Base>::staticValueGetter(ExecState* exec, const Identifier& propertyName, const PropertySlot& slot)
446{
447 ASSERT(slot.slotBase()->isObject(&JSCallbackObject::info));
448 JSCallbackObject* thisObj = static_cast<JSCallbackObject*>(slot.slotBase());
449
450 JSObjectRef thisRef = toRef(thisObj);
451 RefPtr<OpaqueJSString> propertyNameRef;
452
453 for (JSClassRef jsClass = thisObj->classRef(); jsClass; jsClass = jsClass->parentClass)
454 if (OpaqueJSClassStaticValuesTable* staticValues = jsClass->staticValues(exec))
455 if (StaticValueEntry* entry = staticValues->get(propertyName.ustring().rep()))
456 if (JSObjectGetPropertyCallback getProperty = entry->getProperty) {
457 if (!propertyNameRef)
458 propertyNameRef = OpaqueJSString::create(propertyName.ustring());
459 JSLock::DropAllLocks dropAllLocks(exec);
460 if (JSValueRef value = getProperty(toRef(exec), thisRef, propertyNameRef.get(), toRef(exec->exceptionSlot())))
461 return toJS(value);
462 }
463
464 return throwError(exec, ReferenceError, "Static value property defined with NULL getProperty callback.");
465}
466
467template <class Base>
468JSValue* JSCallbackObject<Base>::staticFunctionGetter(ExecState* exec, const Identifier& propertyName, const PropertySlot& slot)
469{
470 ASSERT(slot.slotBase()->isObject(&JSCallbackObject::info));
471 JSCallbackObject* thisObj = static_cast<JSCallbackObject*>(slot.slotBase());
472
473 // Check for cached or override property.
474 PropertySlot slot2(thisObj);
475 if (thisObj->Base::getOwnPropertySlot(exec, propertyName, slot2))
476 return slot2.getValue(exec, propertyName);
477
478 for (JSClassRef jsClass = thisObj->classRef(); jsClass; jsClass = jsClass->parentClass) {
479 if (OpaqueJSClassStaticFunctionsTable* staticFunctions = jsClass->staticFunctions(exec)) {
480 if (StaticFunctionEntry* entry = staticFunctions->get(propertyName.ustring().rep())) {
481 if (JSObjectCallAsFunctionCallback callAsFunction = entry->callAsFunction) {
482 JSObject* o = new (exec) JSCallbackFunction(exec, callAsFunction, propertyName);
483 thisObj->putDirect(propertyName, o, entry->attributes);
484 return o;
485 }
486 }
487 }
488 }
489
490 return throwError(exec, ReferenceError, "Static function property defined with NULL callAsFunction callback.");
491}
492
493template <class Base>
494JSValue* JSCallbackObject<Base>::callbackGetter(ExecState* exec, const Identifier& propertyName, const PropertySlot& slot)
495{
496 ASSERT(slot.slotBase()->isObject(&JSCallbackObject::info));
497 JSCallbackObject* thisObj = static_cast<JSCallbackObject*>(slot.slotBase());
498
499 JSObjectRef thisRef = toRef(thisObj);
500 RefPtr<OpaqueJSString> propertyNameRef;
501
502 for (JSClassRef jsClass = thisObj->classRef(); jsClass; jsClass = jsClass->parentClass)
503 if (JSObjectGetPropertyCallback getProperty = jsClass->getProperty) {
504 if (!propertyNameRef)
505 propertyNameRef = OpaqueJSString::create(propertyName.ustring());
506 JSLock::DropAllLocks dropAllLocks(exec);
507 if (JSValueRef value = getProperty(toRef(exec), thisRef, propertyNameRef.get(), toRef(exec->exceptionSlot())))
508 return toJS(value);
509 }
510
511 return throwError(exec, ReferenceError, "hasProperty callback returned true for a property that doesn't exist.");
512}
513
514} // namespace JSC
Note: See TracBrowser for help on using the repository browser.