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

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

JavaScriptCore:

2009-05-02 Sam Weinig <[email protected]>

Roll JSC API number marshaling back in one last time (I hope).

WebCore:

2009-05-03 Sam Weinig <[email protected]>

Roll JSC API number marshaling back in one last time (I hope).

  • 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, PassRefPtr<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 exception = 0;
322 bool result;
323 {
324 JSLock::DropAllLocks dropAllLocks(exec);
325 result = hasInstance(execRef, thisRef, toRef(exec, value), &exception);
326 }
327 exec->setException(toJS(exec, exception));
328 return result;
329 }
330 }
331 return false;
332}
333
334template <class Base>
335CallType JSCallbackObject<Base>::getCallData(CallData& callData)
336{
337 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass) {
338 if (jsClass->callAsFunction) {
339 callData.native.function = call;
340 return CallTypeHost;
341 }
342 }
343 return CallTypeNone;
344}
345
346template <class Base>
347JSValue JSCallbackObject<Base>::call(ExecState* exec, JSObject* functionObject, JSValue thisValue, const ArgList& args)
348{
349 JSContextRef execRef = toRef(exec);
350 JSObjectRef functionRef = toRef(functionObject);
351 JSObjectRef thisObjRef = toRef(thisValue.toThisObject(exec));
352
353 for (JSClassRef jsClass = static_cast<JSCallbackObject<Base>*>(functionObject)->classRef(); jsClass; jsClass = jsClass->parentClass) {
354 if (JSObjectCallAsFunctionCallback callAsFunction = jsClass->callAsFunction) {
355 int argumentCount = static_cast<int>(args.size());
356 Vector<JSValueRef, 16> arguments(argumentCount);
357 for (int i = 0; i < argumentCount; i++)
358 arguments[i] = toRef(exec, args.at(i));
359 JSValueRef exception = 0;
360 JSValue result;
361 {
362 JSLock::DropAllLocks dropAllLocks(exec);
363 result = toJS(exec, callAsFunction(execRef, functionRef, thisObjRef, argumentCount, arguments.data(), &exception));
364 }
365 exec->setException(toJS(exec, exception));
366 return result;
367 }
368 }
369
370 ASSERT_NOT_REACHED(); // getCallData should prevent us from reaching here
371 return JSValue();
372}
373
374template <class Base>
375void JSCallbackObject<Base>::getPropertyNames(ExecState* exec, PropertyNameArray& propertyNames)
376{
377 JSContextRef execRef = toRef(exec);
378 JSObjectRef thisRef = toRef(this);
379
380 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass) {
381 if (JSObjectGetPropertyNamesCallback getPropertyNames = jsClass->getPropertyNames) {
382 JSLock::DropAllLocks dropAllLocks(exec);
383 getPropertyNames(execRef, thisRef, toRef(&propertyNames));
384 }
385
386 if (OpaqueJSClassStaticValuesTable* staticValues = jsClass->staticValues(exec)) {
387 typedef OpaqueJSClassStaticValuesTable::const_iterator iterator;
388 iterator end = staticValues->end();
389 for (iterator it = staticValues->begin(); it != end; ++it) {
390 UString::Rep* name = it->first.get();
391 StaticValueEntry* entry = it->second;
392 if (entry->getProperty && !(entry->attributes & kJSPropertyAttributeDontEnum))
393 propertyNames.add(Identifier(exec, name));
394 }
395 }
396
397 if (OpaqueJSClassStaticFunctionsTable* staticFunctions = jsClass->staticFunctions(exec)) {
398 typedef OpaqueJSClassStaticFunctionsTable::const_iterator iterator;
399 iterator end = staticFunctions->end();
400 for (iterator it = staticFunctions->begin(); it != end; ++it) {
401 UString::Rep* name = it->first.get();
402 StaticFunctionEntry* entry = it->second;
403 if (!(entry->attributes & kJSPropertyAttributeDontEnum))
404 propertyNames.add(Identifier(exec, name));
405 }
406 }
407 }
408
409 Base::getPropertyNames(exec, propertyNames);
410}
411
412template <class Base>
413double JSCallbackObject<Base>::toNumber(ExecState* exec) const
414{
415 // We need this check to guard against the case where this object is rhs of
416 // a binary expression where lhs threw an exception in its conversion to
417 // primitive
418 if (exec->hadException())
419 return NaN;
420 JSContextRef ctx = toRef(exec);
421 JSObjectRef thisRef = toRef(this);
422
423 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass)
424 if (JSObjectConvertToTypeCallback convertToType = jsClass->convertToType) {
425 JSValueRef exception = 0;
426 JSValueRef value;
427 {
428 JSLock::DropAllLocks dropAllLocks(exec);
429 value = convertToType(ctx, thisRef, kJSTypeNumber, &exception);
430 }
431 exec->setException(toJS(exec, exception));
432 if (value) {
433 double dValue;
434 return toJS(exec, value).getNumber(dValue) ? dValue : NaN;
435 }
436 }
437
438 return Base::toNumber(exec);
439}
440
441template <class Base>
442UString JSCallbackObject<Base>::toString(ExecState* exec) const
443{
444 JSContextRef ctx = toRef(exec);
445 JSObjectRef thisRef = toRef(this);
446
447 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass)
448 if (JSObjectConvertToTypeCallback convertToType = jsClass->convertToType) {
449 JSValueRef exception = 0;
450 JSValueRef value;
451 {
452 JSLock::DropAllLocks dropAllLocks(exec);
453 value = convertToType(ctx, thisRef, kJSTypeString, &exception);
454 }
455 exec->setException(toJS(exec, exception));
456 if (value)
457 return toJS(exec, value).getString();
458 if (exception)
459 return "";
460 }
461
462 return Base::toString(exec);
463}
464
465template <class Base>
466void JSCallbackObject<Base>::setPrivate(void* data)
467{
468 m_callbackObjectData->privateData = data;
469}
470
471template <class Base>
472void* JSCallbackObject<Base>::getPrivate()
473{
474 return m_callbackObjectData->privateData;
475}
476
477template <class Base>
478bool JSCallbackObject<Base>::inherits(JSClassRef c) const
479{
480 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass)
481 if (jsClass == c)
482 return true;
483
484 return false;
485}
486
487template <class Base>
488JSValue JSCallbackObject<Base>::staticValueGetter(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 (OpaqueJSClassStaticValuesTable* staticValues = jsClass->staticValues(exec))
497 if (StaticValueEntry* entry = staticValues->get(propertyName.ustring().rep()))
498 if (JSObjectGetPropertyCallback getProperty = entry->getProperty) {
499 if (!propertyNameRef)
500 propertyNameRef = OpaqueJSString::create(propertyName.ustring());
501 JSValueRef exception = 0;
502 JSValueRef value;
503 {
504 JSLock::DropAllLocks dropAllLocks(exec);
505 value = getProperty(toRef(exec), thisRef, propertyNameRef.get(), &exception);
506 }
507 exec->setException(toJS(exec, exception));
508 if (value)
509 return toJS(exec, value);
510 if (exception)
511 return jsUndefined();
512 }
513
514 return throwError(exec, ReferenceError, "Static value property defined with NULL getProperty callback.");
515}
516
517template <class Base>
518JSValue JSCallbackObject<Base>::staticFunctionGetter(ExecState* exec, const Identifier& propertyName, const PropertySlot& slot)
519{
520 JSCallbackObject* thisObj = asCallbackObject(slot.slotBase());
521
522 // Check for cached or override property.
523 PropertySlot slot2(thisObj);
524 if (thisObj->Base::getOwnPropertySlot(exec, propertyName, slot2))
525 return slot2.getValue(exec, propertyName);
526
527 for (JSClassRef jsClass = thisObj->classRef(); jsClass; jsClass = jsClass->parentClass) {
528 if (OpaqueJSClassStaticFunctionsTable* staticFunctions = jsClass->staticFunctions(exec)) {
529 if (StaticFunctionEntry* entry = staticFunctions->get(propertyName.ustring().rep())) {
530 if (JSObjectCallAsFunctionCallback callAsFunction = entry->callAsFunction) {
531 JSObject* o = new (exec) JSCallbackFunction(exec, callAsFunction, propertyName);
532 thisObj->putDirect(propertyName, o, entry->attributes);
533 return o;
534 }
535 }
536 }
537 }
538
539 return throwError(exec, ReferenceError, "Static function property defined with NULL callAsFunction callback.");
540}
541
542template <class Base>
543JSValue JSCallbackObject<Base>::callbackGetter(ExecState* exec, const Identifier& propertyName, const PropertySlot& slot)
544{
545 JSCallbackObject* thisObj = asCallbackObject(slot.slotBase());
546
547 JSObjectRef thisRef = toRef(thisObj);
548 RefPtr<OpaqueJSString> propertyNameRef;
549
550 for (JSClassRef jsClass = thisObj->classRef(); jsClass; jsClass = jsClass->parentClass)
551 if (JSObjectGetPropertyCallback getProperty = jsClass->getProperty) {
552 if (!propertyNameRef)
553 propertyNameRef = OpaqueJSString::create(propertyName.ustring());
554 JSValueRef exception = 0;
555 JSValueRef value;
556 {
557 JSLock::DropAllLocks dropAllLocks(exec);
558 value = getProperty(toRef(exec), thisRef, propertyNameRef.get(), &exception);
559 }
560 exec->setException(toJS(exec, exception));
561 if (value)
562 return toJS(exec, value);
563 if (exception)
564 return jsUndefined();
565 }
566
567 return throwError(exec, ReferenceError, "hasProperty callback returned true for a property that doesn't exist.");
568}
569
570} // namespace JSC
Note: See TracBrowser for help on using the repository browser.