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

Last change on this file since 37681 was 37681, checked in by Darin Adler, 17 years ago

JavaScriptCore:

2008-10-18 Darin Adler <Darin Adler>

Reviewed by Oliver Hunt.

Remove casts from JSValue* to derived classes, replacing them with
calls to inline casting functions. These functions are also a bit
better than aidrect cast because they also do a runtime assertion.

Removed use of 0 as for JSValue*, changing call sites to use a
noValue() function instead.

Move things needed by classes derived from JSValue out of the class,
since the classes won't be deriving from JSValue any more soon.

I did most of these changes by changing JSValue to not be JSValue* any
more, then fixing a lot of the compilation problems, then rolling out
the JSValue change.

1.011x as fast on SunSpider (presumably due to some of the Machine.cpp changes)

  • API/APICast.h: Removed unneeded forward declarations.
  • API/JSCallbackObject.h: Added an asCallbackObject function for casting.
  • API/JSCallbackObjectFunctions.h: (JSC::JSCallbackObject::asCallbackObject): Added. (JSC::JSCallbackObject::getOwnPropertySlot): Use asObject. (JSC::JSCallbackObject::call): Use noValue. (JSC::JSCallbackObject::staticValueGetter): Use asCallbackObject. (JSC::JSCallbackObject::staticFunctionGetter): Ditto. (JSC::JSCallbackObject::callbackGetter): Ditto.
  • JavaScriptCore.xcodeproj/project.pbxproj: Added RegExpMatchesArray.h.
  • VM/CTI.cpp: (JSC::CTI::asInteger): Added. For use casting a JSValue to an integer. (JSC::CTI::emitGetArg): Use asInteger. (JSC::CTI::emitGetPutArg): Ditto. (JSC::CTI::getConstantImmediateNumericArg): Ditto. Also use noValue. (JSC::CTI::emitInitRegister): Use asInteger. (JSC::CTI::getDeTaggedConstantImmediate): Ditto. (JSC::CTI::compileOpCallInitializeCallFrame): Ditto. (JSC::CTI::compileOpCall): Ditto. (JSC::CTI::compileOpStrictEq): Ditto. (JSC::CTI::privateCompileMainPass): Ditto. (JSC::CTI::privateCompileGetByIdProto): Ditto. (JSC::CTI::privateCompileGetByIdChain): Ditto. (JSC::CTI::privateCompilePutByIdTransition): Ditto.
  • VM/CTI.h: Rewrite the ARG-related macros to use C++ casts instead of C casts and get rid of some extra parentheses. Addd declaration of asInteger.
  • VM/CodeGenerator.cpp: (JSC::CodeGenerator::emitEqualityOp): Use asString. (JSC::CodeGenerator::emitLoad): Use noValue. (JSC::CodeGenerator::findScopedProperty): Change globalObject argument to JSObject* instead of JSValue*. (JSC::CodeGenerator::emitResolve): Remove unneeded cast. (JSC::CodeGenerator::emitGetScopedVar): Use asCell. (JSC::CodeGenerator::emitPutScopedVar): Ditto.
  • VM/CodeGenerator.h: Changed out argument of findScopedProperty. Also change the JSValueMap to use PtrHash explicitly instead of getting it from DefaultHash.
  • VM/JSPropertyNameIterator.cpp: (JSC::JSPropertyNameIterator::toPrimitive): Use noValue.
  • VM/JSPropertyNameIterator.h: (JSC::JSPropertyNameIterator::next): Ditto.
  • VM/Machine.cpp: (JSC::fastIsNumber): Moved isImmediate check here instead of checking for 0 inside Heap::isNumber. Use asCell and asNumberCell. (JSC::fastToInt32): Ditto. (JSC::fastToUInt32): Ditto. (JSC::jsLess): Use asString. (JSC::jsLessEq): Ditto. (JSC::jsAdd): Ditto. (JSC::jsTypeStringForValue): Use asObject. (JSC::jsIsObjectType): Ditto. (JSC::jsIsFunctionType): Ditto. (JSC::inlineResolveBase): Use noValue. (JSC::Machine::callEval): Use asString. Initialize result to undefined, not 0. (JSC::Machine::Machine): Remove unneeded casts to JSCell*. (JSC::Machine::throwException): Use asObject. (JSC::Machine::debug): Remove explicit calls to the DebuggerCallFrame constructor. (JSC::Machine::checkTimeout): Use noValue. (JSC::cachePrototypeChain): Use asObject. (JSC::Machine::tryCachePutByID): Use asCell. (JSC::Machine::tryCacheGetByID): Use aCell and asObject. (JSC::Machine::privateExecute): Use noValue, asCell, asObject, asString, asArray, asActivation, asFunction. Changed code that creates call frames for host functions to pass 0 for the function pointer -- the call frame needs a JSFunction* and a host function object is not one. This was caught by the assertions in the casting functions. Also remove some unneeded casts in cases where two values are compared. (JSC::Machine::retrieveLastCaller): Use noValue. (JSC::Machine::tryCTICachePutByID): Use asCell. (JSC::Machine::tryCTICacheGetByID): Use aCell and asObject. (JSC::setUpThrowTrampolineReturnAddress): Added this function to restore the PIC-branch-avoidance that was recently lost. (JSC::Machine::cti_op_add): Use asString. (JSC::Machine::cti_op_instanceof): Use asCell and asObject. (JSC::Machine::cti_op_call_JSFunction): Use asFunction. (JSC::Machine::cti_op_call_NotJSFunction): Changed code to pass 0 for the function pointer, since we don't have a JSFunction. Use asObject. (JSC::Machine::cti_op_tear_off_activation): Use asActivation. (JSC::Machine::cti_op_construct_JSConstruct): Use asFunction and asObject. (JSC::Machine::cti_op_construct_NotJSConstruct): use asObject. (JSC::Machine::cti_op_get_by_val): Use asArray and asString. (JSC::Machine::cti_op_resolve_func): Use asPointer; this helps prepare us for a situation where JSValue is not a pointer. (JSC::Machine::cti_op_put_by_val): Use asArray. (JSC::Machine::cti_op_put_by_val_array): Ditto. (JSC::Machine::cti_op_resolve_global): Use asGlobalObject. (JSC::Machine::cti_op_post_inc): Change VM_CHECK_EXCEPTION_2 to VM_CHECK_EXCEPTION_AT_END, since there's no observable work done after that point. Also use asPointer. (JSC::Machine::cti_op_resolve_with_base): Use asPointer. (JSC::Machine::cti_op_post_dec): Change VM_CHECK_EXCEPTION_2 to VM_CHECK_EXCEPTION_AT_END, since there's no observable work done after that point. Also use asPointer. (JSC::Machine::cti_op_call_eval): Use asObject, noValue, and change VM_CHECK_EXCEPTION_ARG to VM_THROW_EXCEPTION_AT_END. (JSC::Machine::cti_op_throw): Change return value to a JSValue*. (JSC::Machine::cti_op_in): Use asObject. (JSC::Machine::cti_op_switch_char): Use asString. (JSC::Machine::cti_op_switch_string): Ditto. (JSC::Machine::cti_op_put_getter): Use asObject. (JSC::Machine::cti_op_put_setter): Ditto. (JSC::Machine::cti_vm_throw): Change return value to a JSValue*. Use noValue.
  • VM/Machine.h: Change return values of both cti_op_throw and cti_vm_throw to JSValue*.
  • VM/Register.h: Remove nullJSValue, which is the same thing as noValue(). Also removed unneeded definition of JSValue.
  • kjs/ArgList.h: Removed unneeded definition of JSValue.
  • kjs/Arguments.h: (JSC::asArguments): Added.
  • kjs/ArrayPrototype.cpp: (JSC::getProperty): Use noValue. (JSC::arrayProtoFuncToString): Use asArray. (JSC::arrayProtoFuncToLocaleString): Ditto. (JSC::arrayProtoFuncConcat): Ditto. (JSC::arrayProtoFuncPop): Ditto. Also removed unneeded initialization of the result, which is set in both sides of the branch. (JSC::arrayProtoFuncPush): Ditto. (JSC::arrayProtoFuncShift): Removed unneeded initialization of the result, which is set in both sides of the branch. (JSC::arrayProtoFuncSort): Use asArray.
  • kjs/BooleanObject.h: (JSC::asBooleanObject): Added.
  • kjs/BooleanPrototype.cpp: (JSC::booleanProtoFuncToString): Use asBooleanObject. (JSC::booleanProtoFuncValueOf): Ditto.
  • kjs/CallData.cpp: (JSC::call): Use asObject and asFunction.
  • kjs/ConstructData.cpp: (JSC::construct): Ditto.
  • kjs/DateConstructor.cpp: (JSC::constructDate): Use asDateInstance.
  • kjs/DateInstance.h: (JSC::asDateInstance): Added.
  • kjs/DatePrototype.cpp: (JSC::dateProtoFuncToString): Use asDateInstance. (JSC::dateProtoFuncToUTCString): Ditto. (JSC::dateProtoFuncToDateString): Ditto. (JSC::dateProtoFuncToTimeString): Ditto. (JSC::dateProtoFuncToLocaleString): Ditto. (JSC::dateProtoFuncToLocaleDateString): Ditto. (JSC::dateProtoFuncToLocaleTimeString): Ditto. (JSC::dateProtoFuncValueOf): Ditto. (JSC::dateProtoFuncGetTime): Ditto. (JSC::dateProtoFuncGetFullYear): Ditto. (JSC::dateProtoFuncGetUTCFullYear): Ditto. (JSC::dateProtoFuncToGMTString): Ditto. (JSC::dateProtoFuncGetMonth): Ditto. (JSC::dateProtoFuncGetUTCMonth): Ditto. (JSC::dateProtoFuncGetDate): Ditto. (JSC::dateProtoFuncGetUTCDate): Ditto. (JSC::dateProtoFuncGetDay): Ditto. (JSC::dateProtoFuncGetUTCDay): Ditto. (JSC::dateProtoFuncGetHours): Ditto. (JSC::dateProtoFuncGetUTCHours): Ditto. (JSC::dateProtoFuncGetMinutes): Ditto. (JSC::dateProtoFuncGetUTCMinutes): Ditto. (JSC::dateProtoFuncGetSeconds): Ditto. (JSC::dateProtoFuncGetUTCSeconds): Ditto. (JSC::dateProtoFuncGetMilliSeconds): Ditto. (JSC::dateProtoFuncGetUTCMilliseconds): Ditto. (JSC::dateProtoFuncGetTimezoneOffset): Ditto. (JSC::dateProtoFuncSetTime): Ditto. (JSC::setNewValueFromTimeArgs): Ditto. (JSC::setNewValueFromDateArgs): Ditto. (JSC::dateProtoFuncSetYear): Ditto. (JSC::dateProtoFuncGetYear): Ditto.
  • kjs/DebuggerCallFrame.cpp: (JSC::DebuggerCallFrame::thisObject): Use asObject. (JSC::DebuggerCallFrame::evaluate): Use noValue.
  • kjs/DebuggerCallFrame.h: Added a constructor that takes only a callFrame.
  • kjs/ExecState.h: (JSC::ExecState::clearException): Use noValue.
  • kjs/FunctionPrototype.cpp: (JSC::functionProtoFuncToString): Use asFunction. (JSC::functionProtoFuncApply): Use asArguments and asArray.
  • kjs/GetterSetter.cpp: (JSC::GetterSetter::getPrimitiveNumber): Use noValue.
  • kjs/GetterSetter.h: (JSC::asGetterSetter): Added.
  • kjs/InternalFunction.cpp: (JSC::InternalFunction::name): Use asString.
  • kjs/InternalFunction.h: (JSC::asInternalFunction): Added.
  • kjs/JSActivation.cpp: (JSC::JSActivation::argumentsGetter): Use asActivation.
  • kjs/JSActivation.h: (JSC::asActivation): Added.
  • kjs/JSArray.cpp: (JSC::JSArray::putSlowCase): Use noValue. (JSC::JSArray::deleteProperty): Ditto. (JSC::JSArray::increaseVectorLength): Ditto. (JSC::JSArray::setLength): Ditto. (JSC::JSArray::pop): Ditto. (JSC::JSArray::sort): Ditto. (JSC::JSArray::compactForSorting): Ditto.
  • kjs/JSArray.h: (JSC::asArray): Added.
  • kjs/JSCell.cpp: (JSC::JSCell::getJSNumber): Use noValue.
  • kjs/JSCell.h: (JSC::asCell): Added. (JSC::JSValue::asCell): Changed to not preserve const. Given the wide use of JSValue* and JSCell*, it's not really useful to use const. (JSC::JSValue::isNumber): Use asValue. (JSC::JSValue::isString): Ditto. (JSC::JSValue::isGetterSetter): Ditto. (JSC::JSValue::isObject): Ditto. (JSC::JSValue::getNumber): Ditto. (JSC::JSValue::getString): Ditto. (JSC::JSValue::getObject): Ditto. (JSC::JSValue::getCallData): Ditto. (JSC::JSValue::getConstructData): Ditto. (JSC::JSValue::getUInt32): Ditto. (JSC::JSValue::getTruncatedInt32): Ditto. (JSC::JSValue::getTruncatedUInt32): Ditto. (JSC::JSValue::mark): Ditto. (JSC::JSValue::marked): Ditto. (JSC::JSValue::toPrimitive): Ditto. (JSC::JSValue::getPrimitiveNumber): Ditto. (JSC::JSValue::toBoolean): Ditto. (JSC::JSValue::toNumber): Ditto. (JSC::JSValue::toString): Ditto. (JSC::JSValue::toObject): Ditto. (JSC::JSValue::toThisObject): Ditto. (JSC::JSValue::needsThisConversion): Ditto. (JSC::JSValue::toThisString): Ditto. (JSC::JSValue::getJSNumber): Ditto.
  • kjs/JSFunction.cpp: (JSC::JSFunction::argumentsGetter): Use asFunction. (JSC::JSFunction::callerGetter): Ditto. (JSC::JSFunction::lengthGetter): Ditto. (JSC::JSFunction::construct): Use asObject.
  • kjs/JSFunction.h: (JSC::asFunction): Added.
  • kjs/JSGlobalObject.cpp: (JSC::lastInPrototypeChain): Use asObject.
  • kjs/JSGlobalObject.h: (JSC::asGlobalObject): Added. (JSC::ScopeChainNode::globalObject): Use asGlobalObject.
  • kjs/JSImmediate.h: Added noValue, asPointer, and makeValue functions. Use rawValue, makeValue, and noValue consistently instead of doing reinterpret_cast in various functions.
  • kjs/JSNumberCell.h: (JSC::asNumberCell): Added. (JSC::JSValue::uncheckedGetNumber): Use asValue and asNumberCell. (JSC::JSValue::toJSNumber): Use asValue.
  • kjs/JSObject.cpp: (JSC::JSObject::put): Use asObject and asGetterSetter. (JSC::callDefaultValueFunction): Use noValue. (JSC::JSObject::defineGetter): Use asGetterSetter. (JSC::JSObject::defineSetter): Ditto. (JSC::JSObject::lookupGetter): Ditto. Also use asObject. (JSC::JSObject::lookupSetter): Ditto. (JSC::JSObject::hasInstance): Use asObject. (JSC::JSObject::fillGetterPropertySlot): Use asGetterSetter.
  • kjs/JSObject.h: (JSC::JSObject::getDirect): Use noValue. (JSC::asObject): Added. (JSC::JSValue::isObject): Use asValue. (JSC::JSObject::get): Removed unneeded const_cast. (JSC::JSObject::getPropertySlot): Use asObject. (JSC::JSValue::get): Removed unneeded const_cast. Use asValue, asCell, and asObject. (JSC::JSValue::put): Ditto. (JSC::JSObject::allocatePropertyStorageInline): Fixed spelling of "oldPropertStorage".
  • kjs/JSString.cpp: (JSC::JSString::getOwnPropertySlot): Use asObject.
  • kjs/JSString.h: (JSC::asString): Added. (JSC::JSValue::toThisJSString): Use asValue.
  • kjs/JSValue.h: Make PreferredPrimitiveType a top level enum instead of a member of JSValue. Added an asValue function that returns this. Removed overload of asCell for const. Use asValue instead of getting right at this.
  • kjs/ObjectPrototype.cpp: (JSC::objectProtoFuncIsPrototypeOf): Use asObject. (JSC::objectProtoFuncDefineGetter): Ditto. (JSC::objectProtoFuncDefineSetter): Ditto.
  • kjs/PropertySlot.h: (JSC::PropertySlot::PropertySlot): Take a const JSValue* so the callers don't have to worry about const. (JSC::PropertySlot::clearBase): Use noValue. (JSC::PropertySlot::clearValue): Ditto.
  • kjs/RegExpConstructor.cpp: (JSC::regExpConstructorDollar1): Use asRegExpConstructor. (JSC::regExpConstructorDollar2): Ditto. (JSC::regExpConstructorDollar3): Ditto. (JSC::regExpConstructorDollar4): Ditto. (JSC::regExpConstructorDollar5): Ditto. (JSC::regExpConstructorDollar6): Ditto. (JSC::regExpConstructorDollar7): Ditto. (JSC::regExpConstructorDollar8): Ditto. (JSC::regExpConstructorDollar9): Ditto. (JSC::regExpConstructorInput): Ditto. (JSC::regExpConstructorMultiline): Ditto. (JSC::regExpConstructorLastMatch): Ditto. (JSC::regExpConstructorLastParen): Ditto. (JSC::regExpConstructorLeftContext): Ditto. (JSC::regExpConstructorRightContext): Ditto. (JSC::setRegExpConstructorInput): Ditto. (JSC::setRegExpConstructorMultiline): Ditto. (JSC::constructRegExp): Use asObject.
  • kjs/RegExpConstructor.h: (JSC::asRegExpConstructor): Added.
  • kjs/RegExpObject.cpp: (JSC::regExpObjectGlobal): Use asRegExpObject. (JSC::regExpObjectIgnoreCase): Ditto. (JSC::regExpObjectMultiline): Ditto. (JSC::regExpObjectSource): Ditto. (JSC::regExpObjectLastIndex): Ditto. (JSC::setRegExpObjectLastIndex): Ditto. (JSC::callRegExpObject): Ditto.
  • kjs/RegExpObject.h: (JSC::asRegExpObject): Added.
  • kjs/RegExpPrototype.cpp: (JSC::regExpProtoFuncTest): Use asRegExpObject. (JSC::regExpProtoFuncExec): Ditto. (JSC::regExpProtoFuncCompile): Ditto. (JSC::regExpProtoFuncToString): Ditto.
  • kjs/StringObject.h: (JSC::StringObject::internalValue): Use asString. (JSC::asStringObject): Added.
  • kjs/StringPrototype.cpp: (JSC::stringProtoFuncReplace): Use asRegExpObject. (JSC::stringProtoFuncToString): Ue asStringObject. (JSC::stringProtoFuncMatch): Use asRegExpObject. (JSC::stringProtoFuncSearch): Ditto. (JSC::stringProtoFuncSplit): Ditto.
  • kjs/StructureID.cpp: (JSC::StructureID::getEnumerablePropertyNames): Use asObject. (JSC::StructureID::createCachedPrototypeChain): Ditto. (JSC::StructureIDChain::StructureIDChain): Use asCell and asObject.
  • kjs/collector.h: (JSC::Heap::isNumber): Removed null handling. This can only be called on valid cells. (JSC::Heap::cellBlock): Removed overload for const and non-const. Whether the JSCell* is const or not really should have no effect on whether you can modify the collector block it's in.
  • kjs/interpreter.cpp: (JSC::Interpreter::evaluate): Use noValue and noObject.
  • kjs/nodes.cpp: (JSC::FunctionCallResolveNode::emitCode): Use JSObject for the global object rather than JSValue. (JSC::PostfixResolveNode::emitCode): Ditto. (JSC::PrefixResolveNode::emitCode): Ditto. (JSC::ReadModifyResolveNode::emitCode): Ditto. (JSC::AssignResolveNode::emitCode): Ditto.
  • kjs/operations.h: (JSC::equalSlowCaseInline): Use asString, asCell, asNumberCell, (JSC::strictEqualSlowCaseInline): Ditto.

WebCore:

2008-10-18 Darin Adler <Darin Adler>

Reviewed by Oliver Hunt.

Update for change to make PreferredPrimitiveType no longer
a member of JSValue.

  • bridge/c/c_instance.cpp: (JSC::Bindings::CInstance::defaultValue): Removed JSValue:: prefix.
  • bridge/jni/jni_instance.cpp: (JavaInstance::defaultValue): Ditto.
  • bridge/objc/objc_instance.mm: (ObjcInstance::defaultValue): Ditto.
  • bridge/qt/qt_instance.cpp: (JSC::Bindings::QtInstance::defaultValue): Ditto.
  • bridge/runtime.h: Ditto. Also removed typedef.
  • Property svn:eol-style set to native
File size: 20.3 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>
43inline JSCallbackObject<Base>* JSCallbackObject<Base>::asCallbackObject(JSValue* value)
44{
45 ASSERT(asObject(value)->inherits(&info));
46 return static_cast<JSCallbackObject*>(asObject(value));
47}
48
49template <class Base>
50JSCallbackObject<Base>::JSCallbackObject(ExecState* exec, PassRefPtr<StructureID> structure, JSClassRef jsClass, void* data)
51 : Base(structure)
52 , m_callbackObjectData(new JSCallbackObjectData(data, jsClass))
53{
54 init(exec);
55}
56
57// Global object constructor.
58// FIXME: Move this into a separate JSGlobalCallbackObject class derived from this one.
59template <class Base>
60JSCallbackObject<Base>::JSCallbackObject(JSGlobalData* globalData, JSClassRef jsClass)
61 : Base(globalData)
62 , m_callbackObjectData(new JSCallbackObjectData(0, jsClass))
63{
64 ASSERT(Base::isGlobalObject());
65 init(static_cast<JSGlobalObject*>(this)->globalExec());
66}
67
68template <class Base>
69void JSCallbackObject<Base>::init(ExecState* exec)
70{
71 ASSERT(exec);
72
73 Vector<JSObjectInitializeCallback, 16> initRoutines;
74 JSClassRef jsClass = classRef();
75 do {
76 if (JSObjectInitializeCallback initialize = jsClass->initialize)
77 initRoutines.append(initialize);
78 } while ((jsClass = jsClass->parentClass));
79
80 // initialize from base to derived
81 for (int i = static_cast<int>(initRoutines.size()) - 1; i >= 0; i--) {
82 JSLock::DropAllLocks dropAllLocks(exec);
83 JSObjectInitializeCallback initialize = initRoutines[i];
84 initialize(toRef(exec), toRef(this));
85 }
86}
87
88template <class Base>
89JSCallbackObject<Base>::~JSCallbackObject()
90{
91 JSObjectRef thisRef = toRef(this);
92
93 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass)
94 if (JSObjectFinalizeCallback finalize = jsClass->finalize)
95 finalize(thisRef);
96}
97
98template <class Base>
99UString JSCallbackObject<Base>::className() const
100{
101 UString thisClassName = classRef()->className();
102 if (!thisClassName.isNull())
103 return thisClassName;
104
105 return Base::className();
106}
107
108template <class Base>
109bool JSCallbackObject<Base>::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
110{
111 JSContextRef ctx = toRef(exec);
112 JSObjectRef thisRef = toRef(this);
113 RefPtr<OpaqueJSString> propertyNameRef;
114
115 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass) {
116 // optional optimization to bypass getProperty in cases when we only need to know if the property exists
117 if (JSObjectHasPropertyCallback hasProperty = jsClass->hasProperty) {
118 if (!propertyNameRef)
119 propertyNameRef = OpaqueJSString::create(propertyName.ustring());
120 JSLock::DropAllLocks dropAllLocks(exec);
121 if (hasProperty(ctx, thisRef, propertyNameRef.get())) {
122 slot.setCustom(this, callbackGetter);
123 return true;
124 }
125 } else if (JSObjectGetPropertyCallback getProperty = jsClass->getProperty) {
126 if (!propertyNameRef)
127 propertyNameRef = OpaqueJSString::create(propertyName.ustring());
128 JSLock::DropAllLocks dropAllLocks(exec);
129 if (JSValueRef value = getProperty(ctx, thisRef, propertyNameRef.get(), toRef(exec->exceptionSlot()))) {
130 // cache the value so we don't have to compute it again
131 // FIXME: This violates the PropertySlot design a little bit.
132 // We should either use this optimization everywhere, or nowhere.
133 slot.setCustom(asObject(toJS(value)), cachedValueGetter);
134 return true;
135 }
136 }
137
138 if (OpaqueJSClassStaticValuesTable* staticValues = jsClass->staticValues(exec)) {
139 if (staticValues->contains(propertyName.ustring().rep())) {
140 slot.setCustom(this, staticValueGetter);
141 return true;
142 }
143 }
144
145 if (OpaqueJSClassStaticFunctionsTable* staticFunctions = jsClass->staticFunctions(exec)) {
146 if (staticFunctions->contains(propertyName.ustring().rep())) {
147 slot.setCustom(this, staticFunctionGetter);
148 return true;
149 }
150 }
151 }
152
153 return Base::getOwnPropertySlot(exec, propertyName, slot);
154}
155
156template <class Base>
157bool JSCallbackObject<Base>::getOwnPropertySlot(ExecState* exec, unsigned propertyName, PropertySlot& slot)
158{
159 return getOwnPropertySlot(exec, Identifier::from(exec, propertyName), slot);
160}
161
162template <class Base>
163void JSCallbackObject<Base>::put(ExecState* exec, const Identifier& propertyName, JSValue* value, PutPropertySlot& slot)
164{
165 JSContextRef ctx = toRef(exec);
166 JSObjectRef thisRef = toRef(this);
167 RefPtr<OpaqueJSString> propertyNameRef;
168 JSValueRef valueRef = toRef(value);
169
170 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass) {
171 if (JSObjectSetPropertyCallback setProperty = jsClass->setProperty) {
172 if (!propertyNameRef)
173 propertyNameRef = OpaqueJSString::create(propertyName.ustring());
174 JSLock::DropAllLocks dropAllLocks(exec);
175 if (setProperty(ctx, thisRef, propertyNameRef.get(), valueRef, toRef(exec->exceptionSlot())))
176 return;
177 }
178
179 if (OpaqueJSClassStaticValuesTable* staticValues = jsClass->staticValues(exec)) {
180 if (StaticValueEntry* entry = staticValues->get(propertyName.ustring().rep())) {
181 if (entry->attributes & kJSPropertyAttributeReadOnly)
182 return;
183 if (JSObjectSetPropertyCallback setProperty = entry->setProperty) {
184 if (!propertyNameRef)
185 propertyNameRef = OpaqueJSString::create(propertyName.ustring());
186 JSLock::DropAllLocks dropAllLocks(exec);
187 if (setProperty(ctx, thisRef, propertyNameRef.get(), valueRef, toRef(exec->exceptionSlot())))
188 return;
189 } else
190 throwError(exec, ReferenceError, "Attempt to set a property that is not settable.");
191 }
192 }
193
194 if (OpaqueJSClassStaticFunctionsTable* staticFunctions = jsClass->staticFunctions(exec)) {
195 if (StaticFunctionEntry* entry = staticFunctions->get(propertyName.ustring().rep())) {
196 if (entry->attributes & kJSPropertyAttributeReadOnly)
197 return;
198 JSCallbackObject<Base>::putDirect(propertyName, value); // put as override property
199 return;
200 }
201 }
202 }
203
204 return Base::put(exec, propertyName, value, slot);
205}
206
207template <class Base>
208bool JSCallbackObject<Base>::deleteProperty(ExecState* exec, const Identifier& propertyName)
209{
210 JSContextRef ctx = toRef(exec);
211 JSObjectRef thisRef = toRef(this);
212 RefPtr<OpaqueJSString> propertyNameRef;
213
214 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass) {
215 if (JSObjectDeletePropertyCallback deleteProperty = jsClass->deleteProperty) {
216 if (!propertyNameRef)
217 propertyNameRef = OpaqueJSString::create(propertyName.ustring());
218 JSLock::DropAllLocks dropAllLocks(exec);
219 if (deleteProperty(ctx, thisRef, propertyNameRef.get(), toRef(exec->exceptionSlot())))
220 return true;
221 }
222
223 if (OpaqueJSClassStaticValuesTable* staticValues = jsClass->staticValues(exec)) {
224 if (StaticValueEntry* entry = staticValues->get(propertyName.ustring().rep())) {
225 if (entry->attributes & kJSPropertyAttributeDontDelete)
226 return false;
227 return true;
228 }
229 }
230
231 if (OpaqueJSClassStaticFunctionsTable* staticFunctions = jsClass->staticFunctions(exec)) {
232 if (StaticFunctionEntry* entry = staticFunctions->get(propertyName.ustring().rep())) {
233 if (entry->attributes & kJSPropertyAttributeDontDelete)
234 return false;
235 return true;
236 }
237 }
238 }
239
240 return Base::deleteProperty(exec, propertyName);
241}
242
243template <class Base>
244bool JSCallbackObject<Base>::deleteProperty(ExecState* exec, unsigned propertyName)
245{
246 return deleteProperty(exec, Identifier::from(exec, propertyName));
247}
248
249template <class Base>
250ConstructType JSCallbackObject<Base>::getConstructData(ConstructData& constructData)
251{
252 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass) {
253 if (jsClass->callAsConstructor) {
254 constructData.native.function = construct;
255 return ConstructTypeHost;
256 }
257 }
258 return ConstructTypeNone;
259}
260
261template <class Base>
262JSObject* JSCallbackObject<Base>::construct(ExecState* exec, JSObject* constructor, const ArgList& args)
263{
264 JSContextRef execRef = toRef(exec);
265 JSObjectRef constructorRef = toRef(constructor);
266
267 for (JSClassRef jsClass = static_cast<JSCallbackObject<Base>*>(constructor)->classRef(); jsClass; jsClass = jsClass->parentClass) {
268 if (JSObjectCallAsConstructorCallback callAsConstructor = jsClass->callAsConstructor) {
269 int argumentCount = static_cast<int>(args.size());
270 Vector<JSValueRef, 16> arguments(argumentCount);
271 for (int i = 0; i < argumentCount; i++)
272 arguments[i] = toRef(args.at(exec, i));
273 JSLock::DropAllLocks dropAllLocks(exec);
274 return toJS(callAsConstructor(execRef, constructorRef, argumentCount, arguments.data(), toRef(exec->exceptionSlot())));
275 }
276 }
277
278 ASSERT_NOT_REACHED(); // getConstructData should prevent us from reaching here
279 return 0;
280}
281
282template <class Base>
283bool JSCallbackObject<Base>::hasInstance(ExecState* exec, JSValue* value, JSValue*)
284{
285 JSContextRef execRef = toRef(exec);
286 JSObjectRef thisRef = toRef(this);
287
288 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass) {
289 if (JSObjectHasInstanceCallback hasInstance = jsClass->hasInstance) {
290 JSLock::DropAllLocks dropAllLocks(exec);
291 return hasInstance(execRef, thisRef, toRef(value), toRef(exec->exceptionSlot()));
292 }
293 }
294 return false;
295}
296
297template <class Base>
298CallType JSCallbackObject<Base>::getCallData(CallData& callData)
299{
300 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass) {
301 if (jsClass->callAsFunction) {
302 callData.native.function = call;
303 return CallTypeHost;
304 }
305 }
306 return CallTypeNone;
307}
308
309template <class Base>
310JSValue* JSCallbackObject<Base>::call(ExecState* exec, JSObject* functionObject, JSValue* thisValue, const ArgList& args)
311{
312 JSContextRef execRef = toRef(exec);
313 JSObjectRef functionRef = toRef(functionObject);
314 JSObjectRef thisObjRef = toRef(thisValue->toThisObject(exec));
315
316 for (JSClassRef jsClass = static_cast<JSCallbackObject<Base>*>(functionObject)->classRef(); jsClass; jsClass = jsClass->parentClass) {
317 if (JSObjectCallAsFunctionCallback callAsFunction = jsClass->callAsFunction) {
318 int argumentCount = static_cast<int>(args.size());
319 Vector<JSValueRef, 16> arguments(argumentCount);
320 for (int i = 0; i < argumentCount; i++)
321 arguments[i] = toRef(args.at(exec, i));
322 JSLock::DropAllLocks dropAllLocks(exec);
323 return toJS(callAsFunction(execRef, functionRef, thisObjRef, argumentCount, arguments.data(), toRef(exec->exceptionSlot())));
324 }
325 }
326
327 ASSERT_NOT_REACHED(); // getCallData should prevent us from reaching here
328 return noValue();
329}
330
331template <class Base>
332void JSCallbackObject<Base>::getPropertyNames(ExecState* exec, PropertyNameArray& propertyNames)
333{
334 JSContextRef execRef = toRef(exec);
335 JSObjectRef thisRef = toRef(this);
336
337 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass) {
338 if (JSObjectGetPropertyNamesCallback getPropertyNames = jsClass->getPropertyNames) {
339 JSLock::DropAllLocks dropAllLocks(exec);
340 getPropertyNames(execRef, thisRef, toRef(&propertyNames));
341 }
342
343 if (OpaqueJSClassStaticValuesTable* staticValues = jsClass->staticValues(exec)) {
344 typedef OpaqueJSClassStaticValuesTable::const_iterator iterator;
345 iterator end = staticValues->end();
346 for (iterator it = staticValues->begin(); it != end; ++it) {
347 UString::Rep* name = it->first.get();
348 StaticValueEntry* entry = it->second;
349 if (entry->getProperty && !(entry->attributes & kJSPropertyAttributeDontEnum))
350 propertyNames.add(Identifier(exec, name));
351 }
352 }
353
354 if (OpaqueJSClassStaticFunctionsTable* staticFunctions = jsClass->staticFunctions(exec)) {
355 typedef OpaqueJSClassStaticFunctionsTable::const_iterator iterator;
356 iterator end = staticFunctions->end();
357 for (iterator it = staticFunctions->begin(); it != end; ++it) {
358 UString::Rep* name = it->first.get();
359 StaticFunctionEntry* entry = it->second;
360 if (!(entry->attributes & kJSPropertyAttributeDontEnum))
361 propertyNames.add(Identifier(exec, name));
362 }
363 }
364 }
365
366 Base::getPropertyNames(exec, propertyNames);
367}
368
369template <class Base>
370double JSCallbackObject<Base>::toNumber(ExecState* exec) const
371{
372 // We need this check to guard against the case where this object is rhs of
373 // a binary expression where lhs threw an exception in its conversion to
374 // primitive
375 if (exec->hadException())
376 return NaN;
377 JSContextRef ctx = toRef(exec);
378 JSObjectRef thisRef = toRef(this);
379
380 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass)
381 if (JSObjectConvertToTypeCallback convertToType = jsClass->convertToType) {
382 JSLock::DropAllLocks dropAllLocks(exec);
383 if (JSValueRef value = convertToType(ctx, thisRef, kJSTypeNumber, toRef(exec->exceptionSlot())))
384 return toJS(value)->getNumber();
385 }
386
387 return Base::toNumber(exec);
388}
389
390template <class Base>
391UString JSCallbackObject<Base>::toString(ExecState* exec) const
392{
393 JSContextRef ctx = toRef(exec);
394 JSObjectRef thisRef = toRef(this);
395
396 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass)
397 if (JSObjectConvertToTypeCallback convertToType = jsClass->convertToType) {
398 JSValueRef value;
399 {
400 JSLock::DropAllLocks dropAllLocks(exec);
401 value = convertToType(ctx, thisRef, kJSTypeString, toRef(exec->exceptionSlot()));
402 }
403 if (value)
404 return toJS(value)->getString();
405 }
406
407 return Base::toString(exec);
408}
409
410template <class Base>
411void JSCallbackObject<Base>::setPrivate(void* data)
412{
413 m_callbackObjectData->privateData = data;
414}
415
416template <class Base>
417void* JSCallbackObject<Base>::getPrivate()
418{
419 return m_callbackObjectData->privateData;
420}
421
422template <class Base>
423bool JSCallbackObject<Base>::inherits(JSClassRef c) const
424{
425 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass)
426 if (jsClass == c)
427 return true;
428
429 return false;
430}
431
432template <class Base>
433JSValue* JSCallbackObject<Base>::cachedValueGetter(ExecState*, const Identifier&, const PropertySlot& slot)
434{
435 JSValue* v = slot.slotBase();
436 ASSERT(v);
437 return v;
438}
439
440template <class Base>
441JSValue* JSCallbackObject<Base>::staticValueGetter(ExecState* exec, const Identifier& propertyName, const PropertySlot& slot)
442{
443 JSCallbackObject* thisObj = asCallbackObject(slot.slotBase());
444
445 JSObjectRef thisRef = toRef(thisObj);
446 RefPtr<OpaqueJSString> propertyNameRef;
447
448 for (JSClassRef jsClass = thisObj->classRef(); jsClass; jsClass = jsClass->parentClass)
449 if (OpaqueJSClassStaticValuesTable* staticValues = jsClass->staticValues(exec))
450 if (StaticValueEntry* entry = staticValues->get(propertyName.ustring().rep()))
451 if (JSObjectGetPropertyCallback getProperty = entry->getProperty) {
452 if (!propertyNameRef)
453 propertyNameRef = OpaqueJSString::create(propertyName.ustring());
454 JSLock::DropAllLocks dropAllLocks(exec);
455 if (JSValueRef value = getProperty(toRef(exec), thisRef, propertyNameRef.get(), toRef(exec->exceptionSlot())))
456 return toJS(value);
457 }
458
459 return throwError(exec, ReferenceError, "Static value property defined with NULL getProperty callback.");
460}
461
462template <class Base>
463JSValue* JSCallbackObject<Base>::staticFunctionGetter(ExecState* exec, const Identifier& propertyName, const PropertySlot& slot)
464{
465 JSCallbackObject* thisObj = asCallbackObject(slot.slotBase());
466
467 // Check for cached or override property.
468 PropertySlot slot2(thisObj);
469 if (thisObj->Base::getOwnPropertySlot(exec, propertyName, slot2))
470 return slot2.getValue(exec, propertyName);
471
472 for (JSClassRef jsClass = thisObj->classRef(); jsClass; jsClass = jsClass->parentClass) {
473 if (OpaqueJSClassStaticFunctionsTable* staticFunctions = jsClass->staticFunctions(exec)) {
474 if (StaticFunctionEntry* entry = staticFunctions->get(propertyName.ustring().rep())) {
475 if (JSObjectCallAsFunctionCallback callAsFunction = entry->callAsFunction) {
476 JSObject* o = new (exec) JSCallbackFunction(exec, callAsFunction, propertyName);
477 thisObj->putDirect(propertyName, o, entry->attributes);
478 return o;
479 }
480 }
481 }
482 }
483
484 return throwError(exec, ReferenceError, "Static function property defined with NULL callAsFunction callback.");
485}
486
487template <class Base>
488JSValue* JSCallbackObject<Base>::callbackGetter(ExecState* exec, const Identifier& propertyName, const PropertySlot& slot)
489{
490 JSCallbackObject* thisObj = asCallbackObject(slot.slotBase());
491
492 JSObjectRef thisRef = toRef(thisObj);
493 RefPtr<OpaqueJSString> propertyNameRef;
494
495 for (JSClassRef jsClass = thisObj->classRef(); jsClass; jsClass = jsClass->parentClass)
496 if (JSObjectGetPropertyCallback getProperty = jsClass->getProperty) {
497 if (!propertyNameRef)
498 propertyNameRef = OpaqueJSString::create(propertyName.ustring());
499 JSLock::DropAllLocks dropAllLocks(exec);
500 if (JSValueRef value = getProperty(toRef(exec), thisRef, propertyNameRef.get(), toRef(exec->exceptionSlot())))
501 return toJS(value);
502 }
503
504 return throwError(exec, ReferenceError, "hasProperty callback returned true for a property that doesn't exist.");
505}
506
507} // namespace JSC
Note: See TracBrowser for help on using the repository browser.