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

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

Reviewed by Geoff Garen.

Bring back shared JSGlobalData and implicit locking, because too many clients rely on it.

  • Property svn:eol-style set to native
File size: 20.7 KB
Line 
1// -*- mode: c++; c-basic-offset: 4 -*-
2/*
3 * Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
4 * Copyright (C) 2007 Eric Seidel <[email protected]>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "APICast.h"
29#include "Error.h"
30#include "JSCallbackFunction.h"
31#include "JSClassRef.h"
32#include "JSGlobalObject.h"
33#include "JSLock.h"
34#include "JSObjectRef.h"
35#include "JSString.h"
36#include "JSStringRef.h"
37#include "OpaqueJSString.h"
38#include "PropertyNameArray.h"
39#include <wtf/Vector.h>
40
41namespace KJS {
42
43template <class Base>
44JSCallbackObject<Base>::JSCallbackObject(ExecState* exec, JSClassRef jsClass, JSValue* prototype, void* data)
45 : Base(prototype)
46 , m_callbackObjectData(new JSCallbackObjectData(data, jsClass))
47{
48 init(exec);
49}
50
51// Global object constructor.
52// FIXME: Move this into a separate JSGlobalCallbackObject class derived from this one.
53template <class Base>
54JSCallbackObject<Base>::JSCallbackObject(JSClassRef jsClass)
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)
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);
198}
199
200template <class Base>
201void JSCallbackObject<Base>::put(ExecState* exec, unsigned propertyName, JSValue* value)
202{
203 return put(exec, Identifier::from(exec, propertyName), value);
204}
205
206template <class Base>
207bool JSCallbackObject<Base>::deleteProperty(ExecState* exec, const Identifier& propertyName)
208{
209 JSContextRef ctx = toRef(exec);
210 JSObjectRef thisRef = toRef(this);
211 RefPtr<OpaqueJSString> propertyNameRef;
212
213 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass) {
214 if (JSObjectDeletePropertyCallback deleteProperty = jsClass->deleteProperty) {
215 if (!propertyNameRef)
216 propertyNameRef = OpaqueJSString::create(propertyName.ustring());
217 JSLock::DropAllLocks dropAllLocks(exec);
218 if (deleteProperty(ctx, thisRef, propertyNameRef.get(), toRef(exec->exceptionSlot())))
219 return true;
220 }
221
222 if (OpaqueJSClassStaticValuesTable* staticValues = jsClass->staticValues(exec)) {
223 if (StaticValueEntry* entry = staticValues->get(propertyName.ustring().rep())) {
224 if (entry->attributes & kJSPropertyAttributeDontDelete)
225 return false;
226 return true;
227 }
228 }
229
230 if (OpaqueJSClassStaticFunctionsTable* staticFunctions = jsClass->staticFunctions(exec)) {
231 if (StaticFunctionEntry* entry = staticFunctions->get(propertyName.ustring().rep())) {
232 if (entry->attributes & kJSPropertyAttributeDontDelete)
233 return false;
234 return true;
235 }
236 }
237 }
238
239 return Base::deleteProperty(exec, propertyName);
240}
241
242template <class Base>
243bool JSCallbackObject<Base>::deleteProperty(ExecState* exec, unsigned propertyName)
244{
245 return deleteProperty(exec, Identifier::from(exec, propertyName));
246}
247
248template <class Base>
249ConstructType JSCallbackObject<Base>::getConstructData(ConstructData& constructData)
250{
251 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass) {
252 if (jsClass->callAsConstructor) {
253 constructData.native.function = construct;
254 return ConstructTypeHost;
255 }
256 }
257 return ConstructTypeNone;
258}
259
260template <class Base>
261JSObject* JSCallbackObject<Base>::construct(ExecState* exec, JSObject* constructor, const ArgList& args)
262{
263 JSContextRef execRef = toRef(exec);
264 JSObjectRef constructorRef = toRef(constructor);
265
266 for (JSClassRef jsClass = static_cast<JSCallbackObject<Base>*>(constructor)->classRef(); jsClass; jsClass = jsClass->parentClass) {
267 if (JSObjectCallAsConstructorCallback callAsConstructor = jsClass->callAsConstructor) {
268 int argumentCount = static_cast<int>(args.size());
269 Vector<JSValueRef, 16> arguments(argumentCount);
270 for (int i = 0; i < argumentCount; i++)
271 arguments[i] = toRef(args.at(exec, i));
272 JSLock::DropAllLocks dropAllLocks(exec);
273 return toJS(callAsConstructor(execRef, constructorRef, argumentCount, arguments.data(), toRef(exec->exceptionSlot())));
274 }
275 }
276
277 ASSERT_NOT_REACHED(); // getConstructData should prevent us from reaching here
278 return 0;
279}
280
281template <class Base>
282bool JSCallbackObject<Base>::implementsHasInstance() const
283{
284 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass)
285 if (jsClass->hasInstance)
286 return true;
287
288 return false;
289}
290
291template <class Base>
292bool JSCallbackObject<Base>::hasInstance(ExecState *exec, JSValue *value)
293{
294 JSContextRef execRef = toRef(exec);
295 JSObjectRef thisRef = toRef(this);
296
297 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass) {
298 if (JSObjectHasInstanceCallback hasInstance = jsClass->hasInstance) {
299 JSLock::DropAllLocks dropAllLocks(exec);
300 return hasInstance(execRef, thisRef, toRef(value), toRef(exec->exceptionSlot()));
301 }
302 }
303 ASSERT_NOT_REACHED(); // implementsHasInstance should prevent us from reaching here
304 return 0;
305}
306
307template <class Base>
308CallType JSCallbackObject<Base>::getCallData(CallData& callData)
309{
310 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass) {
311 if (jsClass->callAsFunction) {
312 callData.native.function = call;
313 return CallTypeHost;
314 }
315 }
316 return CallTypeNone;
317}
318
319template <class Base>
320JSValue* JSCallbackObject<Base>::call(ExecState* exec, JSObject* functionObject, JSValue* thisValue, const ArgList& args)
321{
322 JSContextRef execRef = toRef(exec);
323 JSObjectRef functionRef = toRef(functionObject);
324 JSObjectRef thisObjRef = toRef(thisValue->toThisObject(exec));
325
326 for (JSClassRef jsClass = static_cast<JSCallbackObject<Base>*>(functionObject)->classRef(); jsClass; jsClass = jsClass->parentClass) {
327 if (JSObjectCallAsFunctionCallback callAsFunction = jsClass->callAsFunction) {
328 int argumentCount = static_cast<int>(args.size());
329 Vector<JSValueRef, 16> arguments(argumentCount);
330 for (int i = 0; i < argumentCount; i++)
331 arguments[i] = toRef(args.at(exec, i));
332 JSLock::DropAllLocks dropAllLocks(exec);
333 return toJS(callAsFunction(execRef, functionRef, thisObjRef, argumentCount, arguments.data(), toRef(exec->exceptionSlot())));
334 }
335 }
336
337 ASSERT_NOT_REACHED(); // getCallData should prevent us from reaching here
338 return 0;
339}
340
341template <class Base>
342void JSCallbackObject<Base>::getPropertyNames(ExecState* exec, PropertyNameArray& propertyNames)
343{
344 JSContextRef execRef = toRef(exec);
345 JSObjectRef thisRef = toRef(this);
346
347 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass) {
348 if (JSObjectGetPropertyNamesCallback getPropertyNames = jsClass->getPropertyNames) {
349 JSLock::DropAllLocks dropAllLocks(exec);
350 getPropertyNames(execRef, thisRef, toRef(&propertyNames));
351 }
352
353 if (OpaqueJSClassStaticValuesTable* staticValues = jsClass->staticValues(exec)) {
354 typedef OpaqueJSClassStaticValuesTable::const_iterator iterator;
355 iterator end = staticValues->end();
356 for (iterator it = staticValues->begin(); it != end; ++it) {
357 UString::Rep* name = it->first.get();
358 StaticValueEntry* entry = it->second;
359 if (entry->getProperty && !(entry->attributes & kJSPropertyAttributeDontEnum))
360 propertyNames.add(Identifier(exec, name));
361 }
362 }
363
364 if (OpaqueJSClassStaticFunctionsTable* staticFunctions = jsClass->staticFunctions(exec)) {
365 typedef OpaqueJSClassStaticFunctionsTable::const_iterator iterator;
366 iterator end = staticFunctions->end();
367 for (iterator it = staticFunctions->begin(); it != end; ++it) {
368 UString::Rep* name = it->first.get();
369 StaticFunctionEntry* entry = it->second;
370 if (!(entry->attributes & kJSPropertyAttributeDontEnum))
371 propertyNames.add(Identifier(exec, name));
372 }
373 }
374 }
375
376 Base::getPropertyNames(exec, propertyNames);
377}
378
379template <class Base>
380double JSCallbackObject<Base>::toNumber(ExecState* exec) const
381{
382 // We need this check to guard against the case where this object is rhs of
383 // a binary expression where lhs threw an exception in its conversion to
384 // primitive
385 if (exec->hadException())
386 return NaN;
387 JSContextRef ctx = toRef(exec);
388 JSObjectRef thisRef = toRef(this);
389
390 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass)
391 if (JSObjectConvertToTypeCallback convertToType = jsClass->convertToType) {
392 JSLock::DropAllLocks dropAllLocks(exec);
393 if (JSValueRef value = convertToType(ctx, thisRef, kJSTypeNumber, toRef(exec->exceptionSlot())))
394 return toJS(value)->getNumber();
395 }
396
397 return Base::toNumber(exec);
398}
399
400template <class Base>
401UString JSCallbackObject<Base>::toString(ExecState* exec) const
402{
403 JSContextRef ctx = toRef(exec);
404 JSObjectRef thisRef = toRef(this);
405
406 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass)
407 if (JSObjectConvertToTypeCallback convertToType = jsClass->convertToType) {
408 JSValueRef value;
409 {
410 JSLock::DropAllLocks dropAllLocks(exec);
411 value = convertToType(ctx, thisRef, kJSTypeString, toRef(exec->exceptionSlot()));
412 }
413 if (value)
414 return toJS(value)->getString();
415 }
416
417 return Base::toString(exec);
418}
419
420template <class Base>
421void JSCallbackObject<Base>::setPrivate(void* data)
422{
423 m_callbackObjectData->privateData = data;
424}
425
426template <class Base>
427void* JSCallbackObject<Base>::getPrivate()
428{
429 return m_callbackObjectData->privateData;
430}
431
432template <class Base>
433bool JSCallbackObject<Base>::inherits(JSClassRef c) const
434{
435 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass)
436 if (jsClass == c)
437 return true;
438
439 return false;
440}
441
442template <class Base>
443JSValue* JSCallbackObject<Base>::cachedValueGetter(ExecState*, const Identifier&, const PropertySlot& slot)
444{
445 JSValue* v = slot.slotBase();
446 ASSERT(v);
447 return v;
448}
449
450template <class Base>
451JSValue* JSCallbackObject<Base>::staticValueGetter(ExecState* exec, const Identifier& propertyName, const PropertySlot& slot)
452{
453 ASSERT(slot.slotBase()->isObject(&JSCallbackObject::info));
454 JSCallbackObject* thisObj = static_cast<JSCallbackObject*>(slot.slotBase());
455
456 JSObjectRef thisRef = toRef(thisObj);
457 RefPtr<OpaqueJSString> propertyNameRef;
458
459 for (JSClassRef jsClass = thisObj->classRef(); jsClass; jsClass = jsClass->parentClass)
460 if (OpaqueJSClassStaticValuesTable* staticValues = jsClass->staticValues(exec))
461 if (StaticValueEntry* entry = staticValues->get(propertyName.ustring().rep()))
462 if (JSObjectGetPropertyCallback getProperty = entry->getProperty) {
463 if (!propertyNameRef)
464 propertyNameRef = OpaqueJSString::create(propertyName.ustring());
465 JSLock::DropAllLocks dropAllLocks(exec);
466 if (JSValueRef value = getProperty(toRef(exec), thisRef, propertyNameRef.get(), toRef(exec->exceptionSlot())))
467 return toJS(value);
468 }
469
470 return throwError(exec, ReferenceError, "Static value property defined with NULL getProperty callback.");
471}
472
473template <class Base>
474JSValue* JSCallbackObject<Base>::staticFunctionGetter(ExecState* exec, const Identifier& propertyName, const PropertySlot& slot)
475{
476 ASSERT(slot.slotBase()->isObject(&JSCallbackObject::info));
477 JSCallbackObject* thisObj = static_cast<JSCallbackObject*>(slot.slotBase());
478
479 // Check for cached or override property.
480 PropertySlot slot2(thisObj);
481 if (thisObj->Base::getOwnPropertySlot(exec, propertyName, slot2))
482 return slot2.getValue(exec, propertyName);
483
484 for (JSClassRef jsClass = thisObj->classRef(); jsClass; jsClass = jsClass->parentClass) {
485 if (OpaqueJSClassStaticFunctionsTable* staticFunctions = jsClass->staticFunctions(exec)) {
486 if (StaticFunctionEntry* entry = staticFunctions->get(propertyName.ustring().rep())) {
487 if (JSObjectCallAsFunctionCallback callAsFunction = entry->callAsFunction) {
488 JSObject* o = new (exec) JSCallbackFunction(exec, callAsFunction, propertyName);
489 thisObj->putDirect(propertyName, o, entry->attributes);
490 return o;
491 }
492 }
493 }
494 }
495
496 return throwError(exec, ReferenceError, "Static function property defined with NULL callAsFunction callback.");
497}
498
499template <class Base>
500JSValue* JSCallbackObject<Base>::callbackGetter(ExecState* exec, const Identifier& propertyName, const PropertySlot& slot)
501{
502 ASSERT(slot.slotBase()->isObject(&JSCallbackObject::info));
503 JSCallbackObject* thisObj = static_cast<JSCallbackObject*>(slot.slotBase());
504
505 JSObjectRef thisRef = toRef(thisObj);
506 RefPtr<OpaqueJSString> propertyNameRef;
507
508 for (JSClassRef jsClass = thisObj->classRef(); jsClass; jsClass = jsClass->parentClass)
509 if (JSObjectGetPropertyCallback getProperty = jsClass->getProperty) {
510 if (!propertyNameRef)
511 propertyNameRef = OpaqueJSString::create(propertyName.ustring());
512 JSLock::DropAllLocks dropAllLocks(exec);
513 if (JSValueRef value = getProperty(toRef(exec), thisRef, propertyNameRef.get(), toRef(exec->exceptionSlot())))
514 return toJS(value);
515 }
516
517 return throwError(exec, ReferenceError, "hasProperty callback returned true for a property that doesn't exist.");
518}
519
520} // namespace KJS
Note: See TracBrowser for help on using the repository browser.