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

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

Bug 40187 - Change function signature of NativeConstructor to match NativeFunction

Reviewed by Oliver Hunt.

Mostly for consistency, but constructor & args arguments are redundant,
and this will help if we wish to be able to JIT calls to more constructors.

JavaScriptCore:

  • API/JSCallbackConstructor.cpp:

(JSC::constructJSCallback):

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

(JSC::::construct):

  • interpreter/Interpreter.cpp:

(JSC::Interpreter::executeConstruct):

  • interpreter/Interpreter.h:
  • jit/JITStubs.cpp:

(JSC::DEFINE_STUB_FUNCTION):

  • runtime/ArrayConstructor.cpp:

(JSC::constructWithArrayConstructor):

  • runtime/BooleanConstructor.cpp:

(JSC::constructWithBooleanConstructor):

  • runtime/ConstructData.cpp:

(JSC::construct):

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

(JSC::constructWithDateConstructor):

  • runtime/Error.cpp:

(JSC::constructNativeError):
(JSC::Error::create):

  • runtime/ErrorConstructor.cpp:

(JSC::constructWithErrorConstructor):

  • runtime/FunctionConstructor.cpp:

(JSC::constructWithFunctionConstructor):

  • runtime/NativeErrorConstructor.cpp:

(JSC::constructWithNativeErrorConstructor):

  • runtime/NativeErrorConstructor.h:

(JSC::NativeErrorConstructor::errorStructure):

  • runtime/NumberConstructor.cpp:

(JSC::constructWithNumberConstructor):

  • runtime/ObjectConstructor.cpp:

(JSC::constructWithObjectConstructor):

  • runtime/RegExpConstructor.cpp:

(JSC::constructWithRegExpConstructor):

  • runtime/StringConstructor.cpp:

(JSC::constructWithStringConstructor):

WebCore:

  • bindings/js/JSArrayBufferConstructor.cpp:

(WebCore::constructCanvasArrayBuffer):

  • bindings/js/JSAudioConstructor.cpp:

(WebCore::constructAudio):

  • bindings/js/JSEventSourceConstructor.cpp:

(WebCore::constructEventSource):

  • bindings/js/JSFloatArrayConstructor.cpp:

(WebCore::constructCanvasFloatArray):

  • bindings/js/JSImageConstructor.cpp:

(WebCore::constructImage):

  • bindings/js/JSInt16ArrayConstructor.cpp:

(WebCore::constructCanvasShortArray):

  • bindings/js/JSInt32ArrayConstructor.cpp:

(WebCore::constructCanvasIntArray):

  • bindings/js/JSInt8ArrayConstructor.cpp:

(WebCore::constructCanvasByteArray):

  • bindings/js/JSMessageChannelConstructor.cpp:

(WebCore::JSMessageChannelConstructor::construct):

  • bindings/js/JSMessageChannelConstructor.h:
  • bindings/js/JSOptionConstructor.cpp:

(WebCore::constructHTMLOptionElement):

  • bindings/js/JSSharedWorkerConstructor.cpp:

(WebCore::constructSharedWorker):

  • bindings/js/JSUint16ArrayConstructor.cpp:

(WebCore::constructCanvasUnsignedShortArray):

  • bindings/js/JSUint32ArrayConstructor.cpp:

(WebCore::constructCanvasUnsignedIntArray):

  • bindings/js/JSUint8ArrayConstructor.cpp:

(WebCore::constructCanvasUnsignedByteArray):

  • bindings/js/JSWebKitCSSMatrixConstructor.cpp:

(WebCore::constructWebKitCSSMatrix):

  • bindings/js/JSWebKitPointConstructor.cpp:

(WebCore::constructWebKitPoint):

  • bindings/js/JSWebSocketConstructor.cpp:

(WebCore::constructWebSocket):

  • bindings/js/JSWorkerConstructor.cpp:

(WebCore::constructWorker):

  • bindings/js/JSXMLHttpRequestConstructor.cpp:

(WebCore::constructXMLHttpRequest):

  • bindings/js/JSXSLTProcessorConstructor.cpp:

(WebCore::constructXSLTProcessor):

  • bindings/scripts/CodeGeneratorJS.pm:
  • bridge/runtime_object.cpp:

(JSC::Bindings::callRuntimeConstructor):

  • Property svn:eol-style set to native
File size: 23.4 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 "APIShims.h"
28#include "APICast.h"
29#include "Error.h"
30#include "JSCallbackFunction.h"
31#include "JSClassRef.h"
32#include "JSFunction.h"
33#include "JSGlobalObject.h"
34#include "JSLock.h"
35#include "JSObjectRef.h"
36#include "JSString.h"
37#include "JSStringRef.h"
38#include "OpaqueJSString.h"
39#include "PropertyNameArray.h"
40#include <wtf/Vector.h>
41
42namespace JSC {
43
44template <class Base>
45inline JSCallbackObject<Base>* JSCallbackObject<Base>::asCallbackObject(JSValue value)
46{
47 ASSERT(asObject(value)->inherits(&info));
48 return static_cast<JSCallbackObject*>(asObject(value));
49}
50
51template <class Base>
52JSCallbackObject<Base>::JSCallbackObject(ExecState* exec, JSGlobalObject* globalObject, NonNullPassRefPtr<Structure> structure, JSClassRef jsClass, void* data)
53 : Base(globalObject, structure)
54 , m_callbackObjectData(new JSCallbackObjectData(data, jsClass))
55{
56 init(exec);
57}
58
59// Global object constructor.
60// FIXME: Move this into a separate JSGlobalCallbackObject class derived from this one.
61template <class Base>
62JSCallbackObject<Base>::JSCallbackObject(JSClassRef jsClass, NonNullPassRefPtr<Structure> structure)
63 : Base(structure)
64 , m_callbackObjectData(new JSCallbackObjectData(0, jsClass))
65{
66 ASSERT(Base::isGlobalObject());
67 init(static_cast<JSGlobalObject*>(this)->globalExec());
68}
69
70template <class Base>
71void JSCallbackObject<Base>::init(ExecState* exec)
72{
73 ASSERT(exec);
74
75 Vector<JSObjectInitializeCallback, 16> initRoutines;
76 JSClassRef jsClass = classRef();
77 do {
78 if (JSObjectInitializeCallback initialize = jsClass->initialize)
79 initRoutines.append(initialize);
80 } while ((jsClass = jsClass->parentClass));
81
82 // initialize from base to derived
83 for (int i = static_cast<int>(initRoutines.size()) - 1; i >= 0; i--) {
84 APICallbackShim callbackShim(exec);
85 JSObjectInitializeCallback initialize = initRoutines[i];
86 initialize(toRef(exec), toRef(this));
87 }
88}
89
90template <class Base>
91JSCallbackObject<Base>::~JSCallbackObject()
92{
93 JSObjectRef thisRef = toRef(this);
94
95 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass)
96 if (JSObjectFinalizeCallback finalize = jsClass->finalize)
97 finalize(thisRef);
98}
99
100template <class Base>
101UString JSCallbackObject<Base>::className() const
102{
103 UString thisClassName = classRef()->className();
104 if (!thisClassName.isEmpty())
105 return thisClassName;
106
107 return Base::className();
108}
109
110template <class Base>
111bool JSCallbackObject<Base>::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
112{
113 JSContextRef ctx = toRef(exec);
114 JSObjectRef thisRef = toRef(this);
115 RefPtr<OpaqueJSString> propertyNameRef;
116
117 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass) {
118 // optional optimization to bypass getProperty in cases when we only need to know if the property exists
119 if (JSObjectHasPropertyCallback hasProperty = jsClass->hasProperty) {
120 if (!propertyNameRef)
121 propertyNameRef = OpaqueJSString::create(propertyName.ustring());
122 APICallbackShim callbackShim(exec);
123 if (hasProperty(ctx, thisRef, propertyNameRef.get())) {
124 slot.setCustom(this, callbackGetter);
125 return true;
126 }
127 } else if (JSObjectGetPropertyCallback getProperty = jsClass->getProperty) {
128 if (!propertyNameRef)
129 propertyNameRef = OpaqueJSString::create(propertyName.ustring());
130 JSValueRef exception = 0;
131 JSValueRef value;
132 {
133 APICallbackShim callbackShim(exec);
134 value = getProperty(ctx, thisRef, propertyNameRef.get(), &exception);
135 }
136 if (exception) {
137 exec->setException(toJS(exec, exception));
138 slot.setValue(jsUndefined());
139 return true;
140 }
141 if (value) {
142 slot.setValue(toJS(exec, value));
143 return true;
144 }
145 }
146
147 if (OpaqueJSClassStaticValuesTable* staticValues = jsClass->staticValues(exec)) {
148 if (staticValues->contains(propertyName.ustring().rep())) {
149 slot.setCustom(this, staticValueGetter);
150 return true;
151 }
152 }
153
154 if (OpaqueJSClassStaticFunctionsTable* staticFunctions = jsClass->staticFunctions(exec)) {
155 if (staticFunctions->contains(propertyName.ustring().rep())) {
156 slot.setCustom(this, staticFunctionGetter);
157 return true;
158 }
159 }
160 }
161
162 return Base::getOwnPropertySlot(exec, propertyName, slot);
163}
164
165template <class Base>
166bool JSCallbackObject<Base>::getOwnPropertySlot(ExecState* exec, unsigned propertyName, PropertySlot& slot)
167{
168 return getOwnPropertySlot(exec, Identifier::from(exec, propertyName), slot);
169}
170
171template <class Base>
172bool JSCallbackObject<Base>::getOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
173{
174 PropertySlot slot;
175 if (getOwnPropertySlot(exec, propertyName, slot)) {
176 // Ideally we should return an access descriptor, but returning a value descriptor is better than nothing.
177 JSValue value = slot.getValue(exec, propertyName);
178 if (!exec->hadException())
179 descriptor.setValue(value);
180 // We don't know whether the property is configurable, but assume it is.
181 descriptor.setConfigurable(true);
182 // We don't know whether the property is enumerable (we could call getOwnPropertyNames() to find out), but assume it isn't.
183 descriptor.setEnumerable(false);
184 return true;
185 }
186
187 return Base::getOwnPropertyDescriptor(exec, propertyName, descriptor);
188}
189
190template <class Base>
191void JSCallbackObject<Base>::put(ExecState* exec, const Identifier& propertyName, JSValue value, PutPropertySlot& slot)
192{
193 JSContextRef ctx = toRef(exec);
194 JSObjectRef thisRef = toRef(this);
195 RefPtr<OpaqueJSString> propertyNameRef;
196 JSValueRef valueRef = toRef(exec, value);
197
198 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass) {
199 if (JSObjectSetPropertyCallback setProperty = jsClass->setProperty) {
200 if (!propertyNameRef)
201 propertyNameRef = OpaqueJSString::create(propertyName.ustring());
202 JSValueRef exception = 0;
203 bool result;
204 {
205 APICallbackShim callbackShim(exec);
206 result = setProperty(ctx, thisRef, propertyNameRef.get(), valueRef, &exception);
207 }
208 if (exception)
209 exec->setException(toJS(exec, exception));
210 if (result || exception)
211 return;
212 }
213
214 if (OpaqueJSClassStaticValuesTable* staticValues = jsClass->staticValues(exec)) {
215 if (StaticValueEntry* entry = staticValues->get(propertyName.ustring().rep())) {
216 if (entry->attributes & kJSPropertyAttributeReadOnly)
217 return;
218 if (JSObjectSetPropertyCallback setProperty = entry->setProperty) {
219 if (!propertyNameRef)
220 propertyNameRef = OpaqueJSString::create(propertyName.ustring());
221 JSValueRef exception = 0;
222 bool result;
223 {
224 APICallbackShim callbackShim(exec);
225 result = setProperty(ctx, thisRef, propertyNameRef.get(), valueRef, &exception);
226 }
227 if (exception)
228 exec->setException(toJS(exec, exception));
229 if (result || exception)
230 return;
231 } else
232 throwError(exec, ReferenceError, "Attempt to set a property that is not settable.");
233 }
234 }
235
236 if (OpaqueJSClassStaticFunctionsTable* staticFunctions = jsClass->staticFunctions(exec)) {
237 if (StaticFunctionEntry* entry = staticFunctions->get(propertyName.ustring().rep())) {
238 if (entry->attributes & kJSPropertyAttributeReadOnly)
239 return;
240 JSCallbackObject<Base>::putDirect(propertyName, value); // put as override property
241 return;
242 }
243 }
244 }
245
246 return Base::put(exec, propertyName, value, slot);
247}
248
249template <class Base>
250bool JSCallbackObject<Base>::deleteProperty(ExecState* exec, const Identifier& propertyName)
251{
252 JSContextRef ctx = toRef(exec);
253 JSObjectRef thisRef = toRef(this);
254 RefPtr<OpaqueJSString> propertyNameRef;
255
256 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass) {
257 if (JSObjectDeletePropertyCallback deleteProperty = jsClass->deleteProperty) {
258 if (!propertyNameRef)
259 propertyNameRef = OpaqueJSString::create(propertyName.ustring());
260 JSValueRef exception = 0;
261 bool result;
262 {
263 APICallbackShim callbackShim(exec);
264 result = deleteProperty(ctx, thisRef, propertyNameRef.get(), &exception);
265 }
266 if (exception)
267 exec->setException(toJS(exec, exception));
268 if (result || exception)
269 return true;
270 }
271
272 if (OpaqueJSClassStaticValuesTable* staticValues = jsClass->staticValues(exec)) {
273 if (StaticValueEntry* entry = staticValues->get(propertyName.ustring().rep())) {
274 if (entry->attributes & kJSPropertyAttributeDontDelete)
275 return false;
276 return true;
277 }
278 }
279
280 if (OpaqueJSClassStaticFunctionsTable* staticFunctions = jsClass->staticFunctions(exec)) {
281 if (StaticFunctionEntry* entry = staticFunctions->get(propertyName.ustring().rep())) {
282 if (entry->attributes & kJSPropertyAttributeDontDelete)
283 return false;
284 return true;
285 }
286 }
287 }
288
289 return Base::deleteProperty(exec, propertyName);
290}
291
292template <class Base>
293bool JSCallbackObject<Base>::deleteProperty(ExecState* exec, unsigned propertyName)
294{
295 return deleteProperty(exec, Identifier::from(exec, propertyName));
296}
297
298template <class Base>
299ConstructType JSCallbackObject<Base>::getConstructData(ConstructData& constructData)
300{
301 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass) {
302 if (jsClass->callAsConstructor) {
303 constructData.native.function = construct;
304 return ConstructTypeHost;
305 }
306 }
307 return ConstructTypeNone;
308}
309
310template <class Base>
311EncodedJSValue JSCallbackObject<Base>::construct(ExecState* exec)
312{
313 JSObject* constructor = exec->callee();
314 JSContextRef execRef = toRef(exec);
315 JSObjectRef constructorRef = toRef(constructor);
316
317 for (JSClassRef jsClass = static_cast<JSCallbackObject<Base>*>(constructor)->classRef(); jsClass; jsClass = jsClass->parentClass) {
318 if (JSObjectCallAsConstructorCallback callAsConstructor = jsClass->callAsConstructor) {
319 int argumentCount = static_cast<int>(exec->argumentCount());
320 Vector<JSValueRef, 16> arguments(argumentCount);
321 for (int i = 0; i < argumentCount; i++)
322 arguments[i] = toRef(exec, exec->argument(i));
323 JSValueRef exception = 0;
324 JSObject* result;
325 {
326 APICallbackShim callbackShim(exec);
327 result = toJS(callAsConstructor(execRef, constructorRef, argumentCount, arguments.data(), &exception));
328 }
329 if (exception)
330 exec->setException(toJS(exec, exception));
331 return JSValue::encode(result);
332 }
333 }
334
335 ASSERT_NOT_REACHED(); // getConstructData should prevent us from reaching here
336 return JSValue::encode(JSValue());
337}
338
339template <class Base>
340bool JSCallbackObject<Base>::hasInstance(ExecState* exec, JSValue value, JSValue)
341{
342 JSContextRef execRef = toRef(exec);
343 JSObjectRef thisRef = toRef(this);
344
345 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass) {
346 if (JSObjectHasInstanceCallback hasInstance = jsClass->hasInstance) {
347 JSValueRef valueRef = toRef(exec, value);
348 JSValueRef exception = 0;
349 bool result;
350 {
351 APICallbackShim callbackShim(exec);
352 result = hasInstance(execRef, thisRef, valueRef, &exception);
353 }
354 if (exception)
355 exec->setException(toJS(exec, exception));
356 return result;
357 }
358 }
359 return false;
360}
361
362template <class Base>
363CallType JSCallbackObject<Base>::getCallData(CallData& callData)
364{
365 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass) {
366 if (jsClass->callAsFunction) {
367 callData.native.function = call;
368 return CallTypeHost;
369 }
370 }
371 return CallTypeNone;
372}
373
374template <class Base>
375EncodedJSValue JSCallbackObject<Base>::call(ExecState* exec)
376{
377 JSContextRef execRef = toRef(exec);
378 JSObjectRef functionRef = toRef(exec->callee());
379 JSObjectRef thisObjRef = toRef(exec->hostThisValue().toThisObject(exec));
380
381 for (JSClassRef jsClass = static_cast<JSCallbackObject<Base>*>(toJS(functionRef))->classRef(); jsClass; jsClass = jsClass->parentClass) {
382 if (JSObjectCallAsFunctionCallback callAsFunction = jsClass->callAsFunction) {
383 int argumentCount = static_cast<int>(exec->argumentCount());
384 Vector<JSValueRef, 16> arguments(argumentCount);
385 for (int i = 0; i < argumentCount; i++)
386 arguments[i] = toRef(exec, exec->argument(i));
387 JSValueRef exception = 0;
388 JSValue result;
389 {
390 APICallbackShim callbackShim(exec);
391 result = toJS(exec, callAsFunction(execRef, functionRef, thisObjRef, argumentCount, arguments.data(), &exception));
392 }
393 if (exception)
394 exec->setException(toJS(exec, exception));
395 return JSValue::encode(result);
396 }
397 }
398
399 ASSERT_NOT_REACHED(); // getCallData should prevent us from reaching here
400 return JSValue::encode(JSValue());
401}
402
403template <class Base>
404void JSCallbackObject<Base>::getOwnPropertyNames(ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode mode)
405{
406 JSContextRef execRef = toRef(exec);
407 JSObjectRef thisRef = toRef(this);
408
409 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass) {
410 if (JSObjectGetPropertyNamesCallback getPropertyNames = jsClass->getPropertyNames) {
411 APICallbackShim callbackShim(exec);
412 getPropertyNames(execRef, thisRef, toRef(&propertyNames));
413 }
414
415 if (OpaqueJSClassStaticValuesTable* staticValues = jsClass->staticValues(exec)) {
416 typedef OpaqueJSClassStaticValuesTable::const_iterator iterator;
417 iterator end = staticValues->end();
418 for (iterator it = staticValues->begin(); it != end; ++it) {
419 UString::Rep* name = it->first.get();
420 StaticValueEntry* entry = it->second;
421 if (entry->getProperty && (!(entry->attributes & kJSPropertyAttributeDontEnum) || (mode == IncludeDontEnumProperties)))
422 propertyNames.add(Identifier(exec, name));
423 }
424 }
425
426 if (OpaqueJSClassStaticFunctionsTable* staticFunctions = jsClass->staticFunctions(exec)) {
427 typedef OpaqueJSClassStaticFunctionsTable::const_iterator iterator;
428 iterator end = staticFunctions->end();
429 for (iterator it = staticFunctions->begin(); it != end; ++it) {
430 UString::Rep* name = it->first.get();
431 StaticFunctionEntry* entry = it->second;
432 if (!(entry->attributes & kJSPropertyAttributeDontEnum) || (mode == IncludeDontEnumProperties))
433 propertyNames.add(Identifier(exec, name));
434 }
435 }
436 }
437
438 Base::getOwnPropertyNames(exec, propertyNames, mode);
439}
440
441template <class Base>
442double JSCallbackObject<Base>::toNumber(ExecState* exec) const
443{
444 // We need this check to guard against the case where this object is rhs of
445 // a binary expression where lhs threw an exception in its conversion to
446 // primitive
447 if (exec->hadException())
448 return NaN;
449 JSContextRef ctx = toRef(exec);
450 JSObjectRef thisRef = toRef(this);
451
452 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass)
453 if (JSObjectConvertToTypeCallback convertToType = jsClass->convertToType) {
454 JSValueRef exception = 0;
455 JSValueRef value;
456 {
457 APICallbackShim callbackShim(exec);
458 value = convertToType(ctx, thisRef, kJSTypeNumber, &exception);
459 }
460 if (exception) {
461 exec->setException(toJS(exec, exception));
462 return 0;
463 }
464
465 double dValue;
466 if (value)
467 return toJS(exec, value).getNumber(dValue) ? dValue : NaN;
468 }
469
470 return Base::toNumber(exec);
471}
472
473template <class Base>
474UString JSCallbackObject<Base>::toString(ExecState* exec) const
475{
476 JSContextRef ctx = toRef(exec);
477 JSObjectRef thisRef = toRef(this);
478
479 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass)
480 if (JSObjectConvertToTypeCallback convertToType = jsClass->convertToType) {
481 JSValueRef exception = 0;
482 JSValueRef value;
483 {
484 APICallbackShim callbackShim(exec);
485 value = convertToType(ctx, thisRef, kJSTypeString, &exception);
486 }
487 if (exception) {
488 exec->setException(toJS(exec, exception));
489 return "";
490 }
491 if (value)
492 return toJS(exec, value).getString(exec);
493 }
494
495 return Base::toString(exec);
496}
497
498template <class Base>
499void JSCallbackObject<Base>::setPrivate(void* data)
500{
501 m_callbackObjectData->privateData = data;
502}
503
504template <class Base>
505void* JSCallbackObject<Base>::getPrivate()
506{
507 return m_callbackObjectData->privateData;
508}
509
510template <class Base>
511bool JSCallbackObject<Base>::inherits(JSClassRef c) const
512{
513 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass)
514 if (jsClass == c)
515 return true;
516
517 return false;
518}
519
520template <class Base>
521JSValue JSCallbackObject<Base>::staticValueGetter(ExecState* exec, JSValue slotBase, const Identifier& propertyName)
522{
523 JSCallbackObject* thisObj = asCallbackObject(slotBase);
524
525 JSObjectRef thisRef = toRef(thisObj);
526 RefPtr<OpaqueJSString> propertyNameRef;
527
528 for (JSClassRef jsClass = thisObj->classRef(); jsClass; jsClass = jsClass->parentClass)
529 if (OpaqueJSClassStaticValuesTable* staticValues = jsClass->staticValues(exec))
530 if (StaticValueEntry* entry = staticValues->get(propertyName.ustring().rep()))
531 if (JSObjectGetPropertyCallback getProperty = entry->getProperty) {
532 if (!propertyNameRef)
533 propertyNameRef = OpaqueJSString::create(propertyName.ustring());
534 JSValueRef exception = 0;
535 JSValueRef value;
536 {
537 APICallbackShim callbackShim(exec);
538 value = getProperty(toRef(exec), thisRef, propertyNameRef.get(), &exception);
539 }
540 if (exception) {
541 exec->setException(toJS(exec, exception));
542 return jsUndefined();
543 }
544 if (value)
545 return toJS(exec, value);
546 }
547
548 return throwError(exec, ReferenceError, "Static value property defined with NULL getProperty callback.");
549}
550
551template <class Base>
552JSValue JSCallbackObject<Base>::staticFunctionGetter(ExecState* exec, JSValue slotBase, const Identifier& propertyName)
553{
554 JSCallbackObject* thisObj = asCallbackObject(slotBase);
555
556 // Check for cached or override property.
557 PropertySlot slot2(thisObj);
558 if (thisObj->Base::getOwnPropertySlot(exec, propertyName, slot2))
559 return slot2.getValue(exec, propertyName);
560
561 for (JSClassRef jsClass = thisObj->classRef(); jsClass; jsClass = jsClass->parentClass) {
562 if (OpaqueJSClassStaticFunctionsTable* staticFunctions = jsClass->staticFunctions(exec)) {
563 if (StaticFunctionEntry* entry = staticFunctions->get(propertyName.ustring().rep())) {
564 if (JSObjectCallAsFunctionCallback callAsFunction = entry->callAsFunction) {
565
566 JSObject* o = new (exec) JSCallbackFunction(exec, asGlobalObject(thisObj->getAnonymousValue(0)), callAsFunction, propertyName);
567 thisObj->putDirect(propertyName, o, entry->attributes);
568 return o;
569 }
570 }
571 }
572 }
573
574 return throwError(exec, ReferenceError, "Static function property defined with NULL callAsFunction callback.");
575}
576
577template <class Base>
578JSValue JSCallbackObject<Base>::callbackGetter(ExecState* exec, JSValue slotBase, const Identifier& propertyName)
579{
580 JSCallbackObject* thisObj = asCallbackObject(slotBase);
581
582 JSObjectRef thisRef = toRef(thisObj);
583 RefPtr<OpaqueJSString> propertyNameRef;
584
585 for (JSClassRef jsClass = thisObj->classRef(); jsClass; jsClass = jsClass->parentClass)
586 if (JSObjectGetPropertyCallback getProperty = jsClass->getProperty) {
587 if (!propertyNameRef)
588 propertyNameRef = OpaqueJSString::create(propertyName.ustring());
589 JSValueRef exception = 0;
590 JSValueRef value;
591 {
592 APICallbackShim callbackShim(exec);
593 value = getProperty(toRef(exec), thisRef, propertyNameRef.get(), &exception);
594 }
595 if (exception) {
596 exec->setException(toJS(exec, exception));
597 return jsUndefined();
598 }
599 if (value)
600 return toJS(exec, value);
601 }
602
603 return throwError(exec, ReferenceError, "hasProperty callback returned true for a property that doesn't exist.");
604}
605
606} // namespace JSC
Note: See TracBrowser for help on using the repository browser.