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

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

Incorrect use of JavaScriptCore API in DumpRenderTree
https://bugs.webkit.org/show_bug.cgi?id=31577

Reviewed by Maciej Stachowiak

Return undefined rather than a literal null.

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