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

Last change on this file since 48836 was 48836, checked in by [email protected], 16 years ago

NotNullPassRefPtr: smart pointer optimized for passing references that are not null
https://bugs.webkit.org/show_bug.cgi?id=29822

Patch by Geoffrey Garen <[email protected]> on 2009-09-28
Reviewed by Darin Adler.

JavaScriptCore:

Added NotNullPassRefPtr, and deployed it in all places that initialize
JavaScript objects.

2.2% speedup on bench-allocate-nonretained.js.

  • API/JSCallbackConstructor.cpp:

(JSC::JSCallbackConstructor::JSCallbackConstructor):

  • API/JSCallbackConstructor.h:
  • API/JSCallbackObject.h:
  • API/JSCallbackObjectFunctions.h:

(JSC::JSCallbackObject::JSCallbackObject):

(JSC::CodeBlock::addFunctionDecl):
(JSC::CodeBlock::addFunctionExpr):

  • runtime/ArrayConstructor.cpp:

(JSC::ArrayConstructor::ArrayConstructor):

  • runtime/ArrayConstructor.h:
  • runtime/ArrayPrototype.cpp:

(JSC::ArrayPrototype::ArrayPrototype):

  • runtime/ArrayPrototype.h:
  • runtime/BooleanConstructor.cpp:

(JSC::BooleanConstructor::BooleanConstructor):

  • runtime/BooleanConstructor.h:
  • runtime/BooleanObject.cpp:

(JSC::BooleanObject::BooleanObject):

  • runtime/BooleanObject.h:
  • runtime/BooleanPrototype.cpp:

(JSC::BooleanPrototype::BooleanPrototype):

  • runtime/BooleanPrototype.h:
  • runtime/DateConstructor.cpp:

(JSC::DateConstructor::DateConstructor):

  • runtime/DateConstructor.h:
  • runtime/DateInstance.cpp:

(JSC::DateInstance::DateInstance):

  • runtime/DateInstance.h:
  • runtime/DatePrototype.cpp:

(JSC::DatePrototype::DatePrototype):

  • runtime/DatePrototype.h:
  • runtime/ErrorConstructor.cpp:

(JSC::ErrorConstructor::ErrorConstructor):

  • runtime/ErrorConstructor.h:
  • runtime/ErrorInstance.cpp:

(JSC::ErrorInstance::ErrorInstance):

  • runtime/ErrorInstance.h:
  • runtime/ErrorPrototype.cpp:

(JSC::ErrorPrototype::ErrorPrototype):

  • runtime/ErrorPrototype.h:
  • runtime/FunctionConstructor.cpp:

(JSC::FunctionConstructor::FunctionConstructor):

  • runtime/FunctionConstructor.h:
  • runtime/FunctionPrototype.cpp:

(JSC::FunctionPrototype::FunctionPrototype):

  • runtime/FunctionPrototype.h:
  • runtime/GlobalEvalFunction.cpp:

(JSC::GlobalEvalFunction::GlobalEvalFunction):

  • runtime/GlobalEvalFunction.h:
  • runtime/InternalFunction.cpp:

(JSC::InternalFunction::InternalFunction):

  • runtime/InternalFunction.h:

(JSC::InternalFunction::InternalFunction):

  • runtime/JSActivation.cpp:

(JSC::JSActivation::JSActivation):

  • runtime/JSActivation.h:

(JSC::JSActivation::JSActivationData::JSActivationData):

  • runtime/JSArray.cpp:

(JSC::JSArray::JSArray):

  • runtime/JSArray.h:
  • runtime/JSByteArray.cpp:

(JSC::JSByteArray::JSByteArray):

  • runtime/JSByteArray.h:
  • runtime/JSFunction.cpp:

(JSC::JSFunction::JSFunction):

  • runtime/JSFunction.h:
  • runtime/JSGlobalObject.h:

(JSC::JSGlobalObject::JSGlobalObject):

  • runtime/JSONObject.h:

(JSC::JSONObject::JSONObject):

  • runtime/JSObject.h:

(JSC::JSObject::JSObject):
(JSC::JSObject::setStructure):

  • runtime/JSVariableObject.h:

(JSC::JSVariableObject::JSVariableObject):

  • runtime/JSWrapperObject.h:

(JSC::JSWrapperObject::JSWrapperObject):

  • runtime/MathObject.cpp:

(JSC::MathObject::MathObject):

  • runtime/MathObject.h:
  • runtime/NativeErrorConstructor.cpp:

(JSC::NativeErrorConstructor::NativeErrorConstructor):

  • runtime/NativeErrorConstructor.h:
  • runtime/NativeErrorPrototype.cpp:

(JSC::NativeErrorPrototype::NativeErrorPrototype):

  • runtime/NativeErrorPrototype.h:
  • runtime/NumberConstructor.cpp:

(JSC::NumberConstructor::NumberConstructor):

  • runtime/NumberConstructor.h:
  • runtime/NumberObject.cpp:

(JSC::NumberObject::NumberObject):

  • runtime/NumberObject.h:
  • runtime/NumberPrototype.cpp:

(JSC::NumberPrototype::NumberPrototype):

  • runtime/NumberPrototype.h:
  • runtime/ObjectConstructor.cpp:

(JSC::ObjectConstructor::ObjectConstructor):

  • runtime/ObjectConstructor.h:
  • runtime/ObjectPrototype.cpp:

(JSC::ObjectPrototype::ObjectPrototype):

  • runtime/ObjectPrototype.h:
  • runtime/PropertyNameArray.h:

(JSC::PropertyNameArrayData::setCachedPrototypeChain):

  • runtime/PrototypeFunction.cpp:

(JSC::PrototypeFunction::PrototypeFunction):

  • runtime/PrototypeFunction.h:
  • runtime/RegExpConstructor.cpp:

(JSC::RegExpConstructor::RegExpConstructor):

  • runtime/RegExpConstructor.h:
  • runtime/RegExpObject.cpp:

(JSC::RegExpObject::RegExpObject):

  • runtime/RegExpObject.h:

(JSC::RegExpObject::RegExpObjectData::RegExpObjectData):

  • runtime/RegExpPrototype.cpp:

(JSC::RegExpPrototype::RegExpPrototype):

  • runtime/RegExpPrototype.h:
  • runtime/StringConstructor.cpp:

(JSC::StringConstructor::StringConstructor):

  • runtime/StringConstructor.h:
  • runtime/StringObject.cpp:

(JSC::StringObject::StringObject):

  • runtime/StringObject.h:
  • runtime/StringObjectThatMasqueradesAsUndefined.h:

(JSC::StringObjectThatMasqueradesAsUndefined::StringObjectThatMasqueradesAsUndefined):

  • runtime/StringPrototype.cpp:

(JSC::StringPrototype::StringPrototype):

  • runtime/StringPrototype.h:
  • wtf/PassRefPtr.h:

(WTF::NotNullPassRefPtr::NotNullPassRefPtr):
(WTF::NotNullPassRefPtr::~NotNullPassRefPtr):
(WTF::NotNullPassRefPtr::get):
(WTF::NotNullPassRefPtr::clear):
(WTF::NotNullPassRefPtr::releaseRef):
(WTF::NotNullPassRefPtr::operator*):
(WTF::NotNullPassRefPtr::operator->):
(WTF::NotNullPassRefPtr::operator!):
(WTF::NotNullPassRefPtr::operator UnspecifiedBoolType):

  • wtf/RefPtr.h:

(WTF::RefPtr::RefPtr):
(WTF::operator==):

WebCore:

Added NotNullPassRefPtr, and deployed it in all places that initialize
JavaScript objects.

  • bindings/js/DOMObjectWithSVGContext.h:

(WebCore::DOMObjectWithSVGContext::DOMObjectWithSVGContext):

  • bindings/js/JSDOMBinding.cpp:

(WebCore::cacheDOMStructure):

  • bindings/js/JSDOMBinding.h:

(WebCore::DOMObject::DOMObject):
(WebCore::DOMObjectWithGlobalPointer::DOMObjectWithGlobalPointer):
(WebCore::DOMConstructorObject::DOMConstructorObject):
(WebCore::DOMConstructorWithDocument::DOMConstructorWithDocument):

  • bindings/js/JSDOMGlobalObject.cpp:

(WebCore::JSDOMGlobalObject::JSDOMGlobalObject):

  • bindings/js/JSDOMGlobalObject.h:
  • bindings/js/JSDOMWindowBase.cpp:

(WebCore::JSDOMWindowBase::JSDOMWindowBase):

  • bindings/js/JSDOMWindowBase.h:
  • bindings/js/JSHTMLAllCollection.h:

(WebCore::JSHTMLAllCollection::JSHTMLAllCollection):

  • bindings/js/JSInspectedObjectWrapper.cpp:

(WebCore::JSInspectedObjectWrapper::JSInspectedObjectWrapper):

  • bindings/js/JSInspectedObjectWrapper.h:
  • bindings/js/JSInspectorCallbackWrapper.cpp:

(WebCore::JSInspectorCallbackWrapper::JSInspectorCallbackWrapper):

  • bindings/js/JSInspectorCallbackWrapper.h:
  • bindings/js/JSQuarantinedObjectWrapper.cpp:

(WebCore::JSQuarantinedObjectWrapper::JSQuarantinedObjectWrapper):

  • bindings/js/JSQuarantinedObjectWrapper.h:
  • bindings/js/JSWorkerContextBase.cpp:

(WebCore::JSWorkerContextBase::JSWorkerContextBase):

  • bindings/js/JSWorkerContextBase.h:
  • bindings/scripts/CodeGeneratorJS.pm:
  • bridge/runtime_object.cpp:

(JSC::RuntimeObjectImp::RuntimeObjectImp):

  • bridge/runtime_object.h:
  • Property svn:eol-style set to native
File size: 22.0 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, NonNullPassRefPtr<Structure> 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(JSClassRef jsClass)
61 : Base()
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.isEmpty())
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 JSValueRef exception = 0;
129 JSValueRef value;
130 {
131 JSLock::DropAllLocks dropAllLocks(exec);
132 value = getProperty(ctx, thisRef, propertyNameRef.get(), &exception);
133 }
134 exec->setException(toJS(exec, exception));
135 if (value) {
136 slot.setValue(toJS(exec, value));
137 return true;
138 }
139 if (exception) {
140 slot.setValue(jsUndefined());
141 return true;
142 }
143 }
144
145 if (OpaqueJSClassStaticValuesTable* staticValues = jsClass->staticValues(exec)) {
146 if (staticValues->contains(propertyName.ustring().rep())) {
147 slot.setCustom(this, staticValueGetter);
148 return true;
149 }
150 }
151
152 if (OpaqueJSClassStaticFunctionsTable* staticFunctions = jsClass->staticFunctions(exec)) {
153 if (staticFunctions->contains(propertyName.ustring().rep())) {
154 slot.setCustom(this, staticFunctionGetter);
155 return true;
156 }
157 }
158 }
159
160 return Base::getOwnPropertySlot(exec, propertyName, slot);
161}
162
163template <class Base>
164bool JSCallbackObject<Base>::getOwnPropertySlot(ExecState* exec, unsigned propertyName, PropertySlot& slot)
165{
166 return getOwnPropertySlot(exec, Identifier::from(exec, propertyName), slot);
167}
168
169template <class Base>
170void JSCallbackObject<Base>::put(ExecState* exec, const Identifier& propertyName, JSValue value, PutPropertySlot& slot)
171{
172 JSContextRef ctx = toRef(exec);
173 JSObjectRef thisRef = toRef(this);
174 RefPtr<OpaqueJSString> propertyNameRef;
175 JSValueRef valueRef = toRef(exec, value);
176
177 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass) {
178 if (JSObjectSetPropertyCallback setProperty = jsClass->setProperty) {
179 if (!propertyNameRef)
180 propertyNameRef = OpaqueJSString::create(propertyName.ustring());
181 JSValueRef exception = 0;
182 bool result;
183 {
184 JSLock::DropAllLocks dropAllLocks(exec);
185 result = setProperty(ctx, thisRef, propertyNameRef.get(), valueRef, &exception);
186 }
187 exec->setException(toJS(exec, exception));
188 if (result || exception)
189 return;
190 }
191
192 if (OpaqueJSClassStaticValuesTable* staticValues = jsClass->staticValues(exec)) {
193 if (StaticValueEntry* entry = staticValues->get(propertyName.ustring().rep())) {
194 if (entry->attributes & kJSPropertyAttributeReadOnly)
195 return;
196 if (JSObjectSetPropertyCallback setProperty = entry->setProperty) {
197 if (!propertyNameRef)
198 propertyNameRef = OpaqueJSString::create(propertyName.ustring());
199 JSValueRef exception = 0;
200 bool result;
201 {
202 JSLock::DropAllLocks dropAllLocks(exec);
203 result = setProperty(ctx, thisRef, propertyNameRef.get(), valueRef, &exception);
204 }
205 exec->setException(toJS(exec, exception));
206 if (result || exception)
207 return;
208 } else
209 throwError(exec, ReferenceError, "Attempt to set a property that is not settable.");
210 }
211 }
212
213 if (OpaqueJSClassStaticFunctionsTable* staticFunctions = jsClass->staticFunctions(exec)) {
214 if (StaticFunctionEntry* entry = staticFunctions->get(propertyName.ustring().rep())) {
215 if (entry->attributes & kJSPropertyAttributeReadOnly)
216 return;
217 JSCallbackObject<Base>::putDirect(propertyName, value); // put as override property
218 return;
219 }
220 }
221 }
222
223 return Base::put(exec, propertyName, value, slot);
224}
225
226template <class Base>
227bool JSCallbackObject<Base>::deleteProperty(ExecState* exec, const Identifier& propertyName)
228{
229 JSContextRef ctx = toRef(exec);
230 JSObjectRef thisRef = toRef(this);
231 RefPtr<OpaqueJSString> propertyNameRef;
232
233 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass) {
234 if (JSObjectDeletePropertyCallback deleteProperty = jsClass->deleteProperty) {
235 if (!propertyNameRef)
236 propertyNameRef = OpaqueJSString::create(propertyName.ustring());
237 JSValueRef exception = 0;
238 bool result;
239 {
240 JSLock::DropAllLocks dropAllLocks(exec);
241 result = deleteProperty(ctx, thisRef, propertyNameRef.get(), &exception);
242 }
243 exec->setException(toJS(exec, exception));
244 if (result || exception)
245 return true;
246 }
247
248 if (OpaqueJSClassStaticValuesTable* staticValues = jsClass->staticValues(exec)) {
249 if (StaticValueEntry* entry = staticValues->get(propertyName.ustring().rep())) {
250 if (entry->attributes & kJSPropertyAttributeDontDelete)
251 return false;
252 return true;
253 }
254 }
255
256 if (OpaqueJSClassStaticFunctionsTable* staticFunctions = jsClass->staticFunctions(exec)) {
257 if (StaticFunctionEntry* entry = staticFunctions->get(propertyName.ustring().rep())) {
258 if (entry->attributes & kJSPropertyAttributeDontDelete)
259 return false;
260 return true;
261 }
262 }
263 }
264
265 return Base::deleteProperty(exec, propertyName);
266}
267
268template <class Base>
269bool JSCallbackObject<Base>::deleteProperty(ExecState* exec, unsigned propertyName)
270{
271 return deleteProperty(exec, Identifier::from(exec, propertyName));
272}
273
274template <class Base>
275ConstructType JSCallbackObject<Base>::getConstructData(ConstructData& constructData)
276{
277 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass) {
278 if (jsClass->callAsConstructor) {
279 constructData.native.function = construct;
280 return ConstructTypeHost;
281 }
282 }
283 return ConstructTypeNone;
284}
285
286template <class Base>
287JSObject* JSCallbackObject<Base>::construct(ExecState* exec, JSObject* constructor, const ArgList& args)
288{
289 JSContextRef execRef = toRef(exec);
290 JSObjectRef constructorRef = toRef(constructor);
291
292 for (JSClassRef jsClass = static_cast<JSCallbackObject<Base>*>(constructor)->classRef(); jsClass; jsClass = jsClass->parentClass) {
293 if (JSObjectCallAsConstructorCallback callAsConstructor = jsClass->callAsConstructor) {
294 int argumentCount = static_cast<int>(args.size());
295 Vector<JSValueRef, 16> arguments(argumentCount);
296 for (int i = 0; i < argumentCount; i++)
297 arguments[i] = toRef(exec, args.at(i));
298 JSValueRef exception = 0;
299 JSObject* result;
300 {
301 JSLock::DropAllLocks dropAllLocks(exec);
302 result = toJS(callAsConstructor(execRef, constructorRef, argumentCount, arguments.data(), &exception));
303 }
304 exec->setException(toJS(exec, exception));
305 return result;
306 }
307 }
308
309 ASSERT_NOT_REACHED(); // getConstructData should prevent us from reaching here
310 return 0;
311}
312
313template <class Base>
314bool JSCallbackObject<Base>::hasInstance(ExecState* exec, JSValue value, JSValue)
315{
316 JSContextRef execRef = toRef(exec);
317 JSObjectRef thisRef = toRef(this);
318
319 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass) {
320 if (JSObjectHasInstanceCallback hasInstance = jsClass->hasInstance) {
321 JSValueRef valueRef = toRef(exec, value);
322 JSValueRef exception = 0;
323 bool result;
324 {
325 JSLock::DropAllLocks dropAllLocks(exec);
326 result = hasInstance(execRef, thisRef, valueRef, &exception);
327 }
328 exec->setException(toJS(exec, exception));
329 return result;
330 }
331 }
332 return false;
333}
334
335template <class Base>
336CallType JSCallbackObject<Base>::getCallData(CallData& callData)
337{
338 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass) {
339 if (jsClass->callAsFunction) {
340 callData.native.function = call;
341 return CallTypeHost;
342 }
343 }
344 return CallTypeNone;
345}
346
347template <class Base>
348JSValue JSCallbackObject<Base>::call(ExecState* exec, JSObject* functionObject, JSValue thisValue, const ArgList& args)
349{
350 JSContextRef execRef = toRef(exec);
351 JSObjectRef functionRef = toRef(functionObject);
352 JSObjectRef thisObjRef = toRef(thisValue.toThisObject(exec));
353
354 for (JSClassRef jsClass = static_cast<JSCallbackObject<Base>*>(functionObject)->classRef(); jsClass; jsClass = jsClass->parentClass) {
355 if (JSObjectCallAsFunctionCallback callAsFunction = jsClass->callAsFunction) {
356 int argumentCount = static_cast<int>(args.size());
357 Vector<JSValueRef, 16> arguments(argumentCount);
358 for (int i = 0; i < argumentCount; i++)
359 arguments[i] = toRef(exec, args.at(i));
360 JSValueRef exception = 0;
361 JSValue result;
362 {
363 JSLock::DropAllLocks dropAllLocks(exec);
364 result = toJS(exec, callAsFunction(execRef, functionRef, thisObjRef, argumentCount, arguments.data(), &exception));
365 }
366 exec->setException(toJS(exec, exception));
367 return result;
368 }
369 }
370
371 ASSERT_NOT_REACHED(); // getCallData should prevent us from reaching here
372 return JSValue();
373}
374
375template <class Base>
376void JSCallbackObject<Base>::getOwnPropertyNames(ExecState* exec, PropertyNameArray& propertyNames)
377{
378 JSContextRef execRef = toRef(exec);
379 JSObjectRef thisRef = toRef(this);
380
381 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass) {
382 if (JSObjectGetPropertyNamesCallback getPropertyNames = jsClass->getPropertyNames) {
383 JSLock::DropAllLocks dropAllLocks(exec);
384 getPropertyNames(execRef, thisRef, toRef(&propertyNames));
385 }
386
387 if (OpaqueJSClassStaticValuesTable* staticValues = jsClass->staticValues(exec)) {
388 typedef OpaqueJSClassStaticValuesTable::const_iterator iterator;
389 iterator end = staticValues->end();
390 for (iterator it = staticValues->begin(); it != end; ++it) {
391 UString::Rep* name = it->first.get();
392 StaticValueEntry* entry = it->second;
393 if (entry->getProperty && !(entry->attributes & kJSPropertyAttributeDontEnum))
394 propertyNames.add(Identifier(exec, name));
395 }
396 }
397
398 if (OpaqueJSClassStaticFunctionsTable* staticFunctions = jsClass->staticFunctions(exec)) {
399 typedef OpaqueJSClassStaticFunctionsTable::const_iterator iterator;
400 iterator end = staticFunctions->end();
401 for (iterator it = staticFunctions->begin(); it != end; ++it) {
402 UString::Rep* name = it->first.get();
403 StaticFunctionEntry* entry = it->second;
404 if (!(entry->attributes & kJSPropertyAttributeDontEnum))
405 propertyNames.add(Identifier(exec, name));
406 }
407 }
408 }
409
410 Base::getOwnPropertyNames(exec, propertyNames);
411}
412
413template <class Base>
414double JSCallbackObject<Base>::toNumber(ExecState* exec) const
415{
416 // We need this check to guard against the case where this object is rhs of
417 // a binary expression where lhs threw an exception in its conversion to
418 // primitive
419 if (exec->hadException())
420 return NaN;
421 JSContextRef ctx = toRef(exec);
422 JSObjectRef thisRef = toRef(this);
423
424 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass)
425 if (JSObjectConvertToTypeCallback convertToType = jsClass->convertToType) {
426 JSValueRef exception = 0;
427 JSValueRef value;
428 {
429 JSLock::DropAllLocks dropAllLocks(exec);
430 value = convertToType(ctx, thisRef, kJSTypeNumber, &exception);
431 }
432 if (exception) {
433 exec->setException(toJS(exec, exception));
434 return 0;
435 }
436
437 double dValue;
438 return toJS(exec, value).getNumber(dValue) ? dValue : NaN;
439 }
440
441 return Base::toNumber(exec);
442}
443
444template <class Base>
445UString JSCallbackObject<Base>::toString(ExecState* exec) const
446{
447 JSContextRef ctx = toRef(exec);
448 JSObjectRef thisRef = toRef(this);
449
450 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass)
451 if (JSObjectConvertToTypeCallback convertToType = jsClass->convertToType) {
452 JSValueRef exception = 0;
453 JSValueRef value;
454 {
455 JSLock::DropAllLocks dropAllLocks(exec);
456 value = convertToType(ctx, thisRef, kJSTypeString, &exception);
457 }
458 if (exception) {
459 exec->setException(toJS(exec, exception));
460 return "";
461 }
462 return toJS(exec, value).getString();
463 }
464
465 return Base::toString(exec);
466}
467
468template <class Base>
469void JSCallbackObject<Base>::setPrivate(void* data)
470{
471 m_callbackObjectData->privateData = data;
472}
473
474template <class Base>
475void* JSCallbackObject<Base>::getPrivate()
476{
477 return m_callbackObjectData->privateData;
478}
479
480template <class Base>
481bool JSCallbackObject<Base>::inherits(JSClassRef c) const
482{
483 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass)
484 if (jsClass == c)
485 return true;
486
487 return false;
488}
489
490template <class Base>
491JSValue JSCallbackObject<Base>::staticValueGetter(ExecState* exec, const Identifier& propertyName, const PropertySlot& slot)
492{
493 JSCallbackObject* thisObj = asCallbackObject(slot.slotBase());
494
495 JSObjectRef thisRef = toRef(thisObj);
496 RefPtr<OpaqueJSString> propertyNameRef;
497
498 for (JSClassRef jsClass = thisObj->classRef(); jsClass; jsClass = jsClass->parentClass)
499 if (OpaqueJSClassStaticValuesTable* staticValues = jsClass->staticValues(exec))
500 if (StaticValueEntry* entry = staticValues->get(propertyName.ustring().rep()))
501 if (JSObjectGetPropertyCallback getProperty = entry->getProperty) {
502 if (!propertyNameRef)
503 propertyNameRef = OpaqueJSString::create(propertyName.ustring());
504 JSValueRef exception = 0;
505 JSValueRef value;
506 {
507 JSLock::DropAllLocks dropAllLocks(exec);
508 value = getProperty(toRef(exec), thisRef, propertyNameRef.get(), &exception);
509 }
510 exec->setException(toJS(exec, exception));
511 if (value)
512 return toJS(exec, value);
513 if (exception)
514 return jsUndefined();
515 }
516
517 return throwError(exec, ReferenceError, "Static value property defined with NULL getProperty callback.");
518}
519
520template <class Base>
521JSValue JSCallbackObject<Base>::staticFunctionGetter(ExecState* exec, const Identifier& propertyName, const PropertySlot& slot)
522{
523 JSCallbackObject* thisObj = asCallbackObject(slot.slotBase());
524
525 // Check for cached or override property.
526 PropertySlot slot2(thisObj);
527 if (thisObj->Base::getOwnPropertySlot(exec, propertyName, slot2))
528 return slot2.getValue(exec, propertyName);
529
530 for (JSClassRef jsClass = thisObj->classRef(); jsClass; jsClass = jsClass->parentClass) {
531 if (OpaqueJSClassStaticFunctionsTable* staticFunctions = jsClass->staticFunctions(exec)) {
532 if (StaticFunctionEntry* entry = staticFunctions->get(propertyName.ustring().rep())) {
533 if (JSObjectCallAsFunctionCallback callAsFunction = entry->callAsFunction) {
534 JSObject* o = new (exec) JSCallbackFunction(exec, callAsFunction, propertyName);
535 thisObj->putDirect(propertyName, o, entry->attributes);
536 return o;
537 }
538 }
539 }
540 }
541
542 return throwError(exec, ReferenceError, "Static function property defined with NULL callAsFunction callback.");
543}
544
545template <class Base>
546JSValue JSCallbackObject<Base>::callbackGetter(ExecState* exec, const Identifier& propertyName, const PropertySlot& slot)
547{
548 JSCallbackObject* thisObj = asCallbackObject(slot.slotBase());
549
550 JSObjectRef thisRef = toRef(thisObj);
551 RefPtr<OpaqueJSString> propertyNameRef;
552
553 for (JSClassRef jsClass = thisObj->classRef(); jsClass; jsClass = jsClass->parentClass)
554 if (JSObjectGetPropertyCallback getProperty = jsClass->getProperty) {
555 if (!propertyNameRef)
556 propertyNameRef = OpaqueJSString::create(propertyName.ustring());
557 JSValueRef exception = 0;
558 JSValueRef value;
559 {
560 JSLock::DropAllLocks dropAllLocks(exec);
561 value = getProperty(toRef(exec), thisRef, propertyNameRef.get(), &exception);
562 }
563 exec->setException(toJS(exec, exception));
564 if (value)
565 return toJS(exec, value);
566 if (exception)
567 return jsUndefined();
568 }
569
570 return throwError(exec, ReferenceError, "hasProperty callback returned true for a property that doesn't exist.");
571}
572
573} // namespace JSC
Note: See TracBrowser for help on using the repository browser.