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

Last change on this file since 27022 was 27022, checked in by eseidel, 18 years ago

2007-10-24 Eric Seidel <[email protected]>

Reviewed by Maciej.


Add a JSGlobalObject class and remove the InterpreterMap
http://bugs.webkit.org/show_bug.cgi?id=15681


This required making JSCallbackObject a template class to allow for
JSGlobalObjects with JSCallbackObject functionality.


SunSpider claims this was a 0.5% speedup.

  • API/JSCallbackObject.cpp: (KJS::):
  • API/JSCallbackObject.h:
  • API/JSCallbackObjectFunctions.h: Copied from API/JSCallbackObject.cpp. (KJS::::JSCallbackObject): (KJS::::init): (KJS::::~JSCallbackObject): (KJS::::initializeIfNeeded): (KJS::::className): (KJS::::getOwnPropertySlot): (KJS::::put): (KJS::::deleteProperty): (KJS::::implementsConstruct): (KJS::::construct): (KJS::::implementsHasInstance): (KJS::::hasInstance): (KJS::::implementsCall): (KJS::::callAsFunction): (KJS::::getPropertyNames): (KJS::::toNumber): (KJS::::toString): (KJS::::setPrivate): (KJS::::getPrivate): (KJS::::inherits): (KJS::::cachedValueGetter): (KJS::::staticValueGetter): (KJS::::staticFunctionGetter): (KJS::::callbackGetter):
  • API/JSClassRef.cpp: (OpaqueJSClass::prototype):
  • API/JSContextRef.cpp: (JSGlobalContextCreate):
  • API/JSObjectRef.cpp: (JSObjectMake): (JSObjectGetPrivate): (JSObjectSetPrivate):
  • API/JSValueRef.cpp: (JSValueIsObjectOfClass):
  • JavaScriptCore.exp:
  • JavaScriptCore.xcodeproj/project.pbxproj:
  • bindings/c/c_utility.cpp: (KJS::Bindings::convertValueToNPVariant):
  • bindings/jni/jni_jsobject.cpp:
  • bindings/objc/objc_utility.mm: (KJS::Bindings::convertValueToObjcValue):
  • kjs/Context.cpp: (KJS::Context::Context):
  • kjs/ExecState.cpp: (KJS::ExecState::lexicalInterpreter):
  • kjs/JSGlobalObject.h: Added. (KJS::JSGlobalObject::JSGlobalObject): (KJS::JSGlobalObject::isGlobalObject): (KJS::JSGlobalObject::interpreter): (KJS::JSGlobalObject::setInterpreter):
  • kjs/array_instance.cpp:
  • kjs/context.h:
  • kjs/function.cpp: (KJS::FunctionImp::callAsFunction): (KJS::GlobalFuncImp::callAsFunction):
  • kjs/interpreter.cpp: (KJS::Interpreter::Interpreter): (KJS::Interpreter::init): (KJS::Interpreter::~Interpreter): (KJS::Interpreter::globalObject): (KJS::Interpreter::initGlobalObject): (KJS::Interpreter::evaluate):
  • kjs/interpreter.h:
  • kjs/lookup.h: (KJS::cacheGlobalObject):
  • kjs/object.h: (KJS::JSObject::isGlobalObject):
  • kjs/testkjs.cpp:
File size: 19.0 KB
Line 
1// -*- mode: c++; c-basic-offset: 4 -*-
2/*
3 * Copyright (C) 2006 Apple Computer, 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 <wtf/Platform.h>
29#include "APICast.h"
30#include "JSCallbackFunction.h"
31#include "JSClassRef.h"
32#include "JSObjectRef.h"
33#include "JSGlobalObject.h"
34#include "JSStringRef.h"
35#include "PropertyNameArray.h"
36#include "internal.h"
37#include <wtf/Vector.h>
38
39namespace KJS {
40
41template <class Base>
42JSCallbackObject<Base>::JSCallbackObject(ExecState* exec, JSClassRef jsClass, JSValue* prototype, void* data)
43 : Base(prototype)
44 , m_class(0)
45 , m_isInitialized(false)
46{
47 init(exec, jsClass, data);
48}
49
50template <class Base>
51void JSCallbackObject<Base>::init(ExecState* exec, JSClassRef jsClass, void* data)
52{
53 m_privateData = data;
54 JSClassRef oldClass = m_class;
55 m_class = JSClassRetain(jsClass);
56 if (oldClass)
57 JSClassRelease(oldClass);
58
59 if (!exec)
60 return;
61
62 Vector<JSObjectInitializeCallback, 16> initRoutines;
63 do {
64 if (JSObjectInitializeCallback initialize = jsClass->initialize)
65 initRoutines.append(initialize);
66 } while ((jsClass = jsClass->parentClass));
67
68 // initialize from base to derived
69 for (int i = static_cast<int>(initRoutines.size()) - 1; i >= 0; i--) {
70 JSLock::DropAllLocks dropAllLocks;
71 JSObjectInitializeCallback initialize = initRoutines[i];
72 initialize(toRef(exec), toRef(this));
73 }
74 m_isInitialized = true;
75}
76
77template <class Base>
78JSCallbackObject<Base>::~JSCallbackObject()
79{
80 JSObjectRef thisRef = toRef(this);
81
82 for (JSClassRef jsClass = m_class; jsClass; jsClass = jsClass->parentClass)
83 if (JSObjectFinalizeCallback finalize = jsClass->finalize) {
84 finalize(thisRef);
85 }
86
87 JSClassRelease(m_class);
88}
89
90template <class Base>
91void JSCallbackObject<Base>::initializeIfNeeded(ExecState* exec)
92{
93 if (m_isInitialized)
94 return;
95 init(exec, m_class, m_privateData);
96}
97
98template <class Base>
99UString JSCallbackObject<Base>::className() const
100{
101 if (!m_class->className.isNull())
102 return m_class->className;
103
104 return JSObject::className();
105}
106
107template <class Base>
108bool JSCallbackObject<Base>::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
109{
110 JSContextRef ctx = toRef(exec);
111 JSObjectRef thisRef = toRef(this);
112 JSStringRef propertyNameRef = toRef(propertyName.ustring().rep());
113
114 for (JSClassRef jsClass = m_class; jsClass; jsClass = jsClass->parentClass) {
115 // optional optimization to bypass getProperty in cases when we only need to know if the property exists
116 if (JSObjectHasPropertyCallback hasProperty = jsClass->hasProperty) {
117 JSLock::DropAllLocks dropAllLocks;
118 if (hasProperty(ctx, thisRef, propertyNameRef)) {
119 slot.setCustom(this, callbackGetter);
120 return true;
121 }
122 } else if (JSObjectGetPropertyCallback getProperty = jsClass->getProperty) {
123 JSLock::DropAllLocks dropAllLocks;
124 if (JSValueRef value = getProperty(ctx, thisRef, propertyNameRef, toRef(exec->exceptionSlot()))) {
125 // cache the value so we don't have to compute it again
126 // FIXME: This violates the PropertySlot design a little bit.
127 // We should either use this optimization everywhere, or nowhere.
128 slot.setCustom(reinterpret_cast<JSObject*>(toJS(value)), cachedValueGetter);
129 return true;
130 }
131 }
132
133 if (OpaqueJSClass::StaticValuesTable* staticValues = jsClass->staticValues) {
134 if (staticValues->contains(propertyName.ustring().rep())) {
135 slot.setCustom(this, staticValueGetter);
136 return true;
137 }
138 }
139
140 if (OpaqueJSClass::StaticFunctionsTable* staticFunctions = jsClass->staticFunctions) {
141 if (staticFunctions->contains(propertyName.ustring().rep())) {
142 slot.setCustom(this, staticFunctionGetter);
143 return true;
144 }
145 }
146 }
147
148 return JSObject::getOwnPropertySlot(exec, propertyName, slot);
149}
150
151template <class Base>
152bool JSCallbackObject<Base>::getOwnPropertySlot(ExecState* exec, unsigned propertyName, PropertySlot& slot)
153{
154 return getOwnPropertySlot(exec, Identifier::from(propertyName), slot);
155}
156
157template <class Base>
158void JSCallbackObject<Base>::put(ExecState* exec, const Identifier& propertyName, JSValue* value, int attr)
159{
160 JSContextRef ctx = toRef(exec);
161 JSObjectRef thisRef = toRef(this);
162 JSStringRef propertyNameRef = toRef(propertyName.ustring().rep());
163 JSValueRef valueRef = toRef(value);
164
165 for (JSClassRef jsClass = m_class; jsClass; jsClass = jsClass->parentClass) {
166 if (JSObjectSetPropertyCallback setProperty = jsClass->setProperty) {
167 JSLock::DropAllLocks dropAllLocks;
168 if (setProperty(ctx, thisRef, propertyNameRef, valueRef, toRef(exec->exceptionSlot())))
169 return;
170 }
171
172 if (OpaqueJSClass::StaticValuesTable* staticValues = jsClass->staticValues) {
173 if (StaticValueEntry* entry = staticValues->get(propertyName.ustring().rep())) {
174 if (entry->attributes & kJSPropertyAttributeReadOnly)
175 return;
176 if (JSObjectSetPropertyCallback setProperty = entry->setProperty) {
177 JSLock::DropAllLocks dropAllLocks;
178 if (setProperty(ctx, thisRef, propertyNameRef, valueRef, toRef(exec->exceptionSlot())))
179 return;
180 } else
181 throwError(exec, ReferenceError, "Attempt to set a property that is not settable.");
182 }
183 }
184
185 if (OpaqueJSClass::StaticFunctionsTable* staticFunctions = jsClass->staticFunctions) {
186 if (StaticFunctionEntry* entry = staticFunctions->get(propertyName.ustring().rep())) {
187 if (entry->attributes & kJSPropertyAttributeReadOnly)
188 return;
189 JSCallbackObject<Base>::putDirect(propertyName, value, attr); // put as override property
190 return;
191 }
192 }
193 }
194
195 return JSObject::put(exec, propertyName, value, attr);
196}
197
198template <class Base>
199void JSCallbackObject<Base>::put(ExecState* exec, unsigned propertyName, JSValue* value, int attr)
200{
201 return put(exec, Identifier::from(propertyName), value, attr);
202}
203
204template <class Base>
205bool JSCallbackObject<Base>::deleteProperty(ExecState* exec, const Identifier& propertyName)
206{
207 JSContextRef ctx = toRef(exec);
208 JSObjectRef thisRef = toRef(this);
209 JSStringRef propertyNameRef = toRef(propertyName.ustring().rep());
210
211 for (JSClassRef jsClass = m_class; jsClass; jsClass = jsClass->parentClass) {
212 if (JSObjectDeletePropertyCallback deleteProperty = jsClass->deleteProperty) {
213 JSLock::DropAllLocks dropAllLocks;
214 if (deleteProperty(ctx, thisRef, propertyNameRef, toRef(exec->exceptionSlot())))
215 return true;
216 }
217
218 if (OpaqueJSClass::StaticValuesTable* staticValues = jsClass->staticValues) {
219 if (StaticValueEntry* entry = staticValues->get(propertyName.ustring().rep())) {
220 if (entry->attributes & kJSPropertyAttributeDontDelete)
221 return false;
222 return true;
223 }
224 }
225
226 if (OpaqueJSClass::StaticFunctionsTable* staticFunctions = jsClass->staticFunctions) {
227 if (StaticFunctionEntry* entry = staticFunctions->get(propertyName.ustring().rep())) {
228 if (entry->attributes & kJSPropertyAttributeDontDelete)
229 return false;
230 return true;
231 }
232 }
233 }
234
235 return JSObject::deleteProperty(exec, propertyName);
236}
237
238template <class Base>
239bool JSCallbackObject<Base>::deleteProperty(ExecState* exec, unsigned propertyName)
240{
241 return deleteProperty(exec, Identifier::from(propertyName));
242}
243
244template <class Base>
245bool JSCallbackObject<Base>::implementsConstruct() const
246{
247 for (JSClassRef jsClass = m_class; jsClass; jsClass = jsClass->parentClass)
248 if (jsClass->callAsConstructor)
249 return true;
250
251 return false;
252}
253
254template <class Base>
255JSObject* JSCallbackObject<Base>::construct(ExecState* exec, const List& args)
256{
257 JSContextRef execRef = toRef(exec);
258 JSObjectRef thisRef = toRef(this);
259
260 for (JSClassRef jsClass = m_class; jsClass; jsClass = jsClass->parentClass) {
261 if (JSObjectCallAsConstructorCallback callAsConstructor = jsClass->callAsConstructor) {
262 int argumentCount = static_cast<int>(args.size());
263 Vector<JSValueRef, 16> arguments(argumentCount);
264 for (int i = 0; i < argumentCount; i++)
265 arguments[i] = toRef(args[i]);
266 JSLock::DropAllLocks dropAllLocks;
267 return toJS(callAsConstructor(execRef, thisRef, argumentCount, arguments.data(), toRef(exec->exceptionSlot())));
268 }
269 }
270
271 ASSERT(0); // implementsConstruct should prevent us from reaching here
272 return 0;
273}
274
275template <class Base>
276bool JSCallbackObject<Base>::implementsHasInstance() const
277{
278 for (JSClassRef jsClass = m_class; jsClass; jsClass = jsClass->parentClass)
279 if (jsClass->hasInstance)
280 return true;
281
282 return false;
283}
284
285template <class Base>
286bool JSCallbackObject<Base>::hasInstance(ExecState *exec, JSValue *value)
287{
288 JSContextRef execRef = toRef(exec);
289 JSObjectRef thisRef = toRef(this);
290
291 for (JSClassRef jsClass = m_class; jsClass; jsClass = jsClass->parentClass)
292 if (JSObjectHasInstanceCallback hasInstance = jsClass->hasInstance) {
293 JSLock::DropAllLocks dropAllLocks;
294 return hasInstance(execRef, thisRef, toRef(value), toRef(exec->exceptionSlot()));
295 }
296
297 ASSERT_NOT_REACHED(); // implementsHasInstance should prevent us from reaching here
298 return 0;
299}
300
301
302template <class Base>
303bool JSCallbackObject<Base>::implementsCall() const
304{
305 for (JSClassRef jsClass = m_class; jsClass; jsClass = jsClass->parentClass)
306 if (jsClass->callAsFunction)
307 return true;
308
309 return false;
310}
311
312template <class Base>
313JSValue* JSCallbackObject<Base>::callAsFunction(ExecState* exec, JSObject* thisObj, const List &args)
314{
315 JSContextRef execRef = toRef(exec);
316 JSObjectRef thisRef = toRef(this);
317 JSObjectRef thisObjRef = toRef(thisObj);
318
319 for (JSClassRef jsClass = m_class; jsClass; jsClass = jsClass->parentClass) {
320 if (JSObjectCallAsFunctionCallback callAsFunction = jsClass->callAsFunction) {
321 int argumentCount = static_cast<int>(args.size());
322 Vector<JSValueRef, 16> arguments(argumentCount);
323 for (int i = 0; i < argumentCount; i++)
324 arguments[i] = toRef(args[i]);
325 JSLock::DropAllLocks dropAllLocks;
326 return toJS(callAsFunction(execRef, thisRef, thisObjRef, argumentCount, arguments.data(), toRef(exec->exceptionSlot())));
327 }
328 }
329
330 ASSERT_NOT_REACHED(); // implementsCall should prevent us from reaching here
331 return 0;
332}
333
334template <class Base>
335void JSCallbackObject<Base>::getPropertyNames(ExecState* exec, PropertyNameArray& propertyNames)
336{
337 JSContextRef execRef = toRef(exec);
338 JSObjectRef thisRef = toRef(this);
339
340 for (JSClassRef jsClass = m_class; jsClass; jsClass = jsClass->parentClass) {
341 if (JSObjectGetPropertyNamesCallback getPropertyNames = jsClass->getPropertyNames) {
342 JSLock::DropAllLocks dropAllLocks;
343 getPropertyNames(execRef, thisRef, toRef(&propertyNames));
344 }
345
346 if (OpaqueJSClass::StaticValuesTable* staticValues = jsClass->staticValues) {
347 typedef OpaqueJSClass::StaticValuesTable::const_iterator iterator;
348 iterator end = staticValues->end();
349 for (iterator it = staticValues->begin(); it != end; ++it) {
350 UString::Rep* name = it->first.get();
351 StaticValueEntry* entry = it->second;
352 if (entry->getProperty && !(entry->attributes & kJSPropertyAttributeDontEnum))
353 propertyNames.add(Identifier(name));
354 }
355 }
356
357 if (OpaqueJSClass::StaticFunctionsTable* staticFunctions = jsClass->staticFunctions) {
358 typedef OpaqueJSClass::StaticFunctionsTable::const_iterator iterator;
359 iterator end = staticFunctions->end();
360 for (iterator it = staticFunctions->begin(); it != end; ++it) {
361 UString::Rep* name = it->first.get();
362 StaticFunctionEntry* entry = it->second;
363 if (!(entry->attributes & kJSPropertyAttributeDontEnum))
364 propertyNames.add(Identifier(name));
365 }
366 }
367 }
368
369 JSObject::getPropertyNames(exec, propertyNames);
370}
371
372template <class Base>
373double JSCallbackObject<Base>::toNumber(ExecState* exec) const
374{
375 JSContextRef ctx = toRef(exec);
376 JSObjectRef thisRef = toRef(this);
377
378 for (JSClassRef jsClass = m_class; jsClass; jsClass = jsClass->parentClass)
379 if (JSObjectConvertToTypeCallback convertToType = jsClass->convertToType) {
380 JSLock::DropAllLocks dropAllLocks;
381 if (JSValueRef value = convertToType(ctx, thisRef, kJSTypeNumber, toRef(exec->exceptionSlot())))
382 return toJS(value)->getNumber();
383 }
384
385 return JSObject::toNumber(exec);
386}
387
388template <class Base>
389UString JSCallbackObject<Base>::toString(ExecState* exec) const
390{
391 JSContextRef ctx = toRef(exec);
392 JSObjectRef thisRef = toRef(this);
393
394 for (JSClassRef jsClass = m_class; jsClass; jsClass = jsClass->parentClass)
395 if (JSObjectConvertToTypeCallback convertToType = jsClass->convertToType) {
396 JSLock::DropAllLocks dropAllLocks;
397 if (JSValueRef value = convertToType(ctx, thisRef, kJSTypeString, toRef(exec->exceptionSlot())))
398 return toJS(value)->getString();
399 }
400
401 return JSObject::toString(exec);
402}
403
404template <class Base>
405void JSCallbackObject<Base>::setPrivate(void* data)
406{
407 m_privateData = data;
408}
409
410template <class Base>
411void* JSCallbackObject<Base>::getPrivate()
412{
413 return m_privateData;
414}
415
416template <class Base>
417bool JSCallbackObject<Base>::inherits(JSClassRef c) const
418{
419 for (JSClassRef jsClass = m_class; jsClass; jsClass = jsClass->parentClass)
420 if (jsClass == c)
421 return true;
422
423 return false;
424}
425
426template <class Base>
427JSValue* JSCallbackObject<Base>::cachedValueGetter(ExecState*, JSObject*, const Identifier&, const PropertySlot& slot)
428{
429 JSValue* v = slot.slotBase();
430 ASSERT(v);
431 return v;
432}
433
434template <class Base>
435JSValue* JSCallbackObject<Base>::staticValueGetter(ExecState* exec, JSObject*, const Identifier& propertyName, const PropertySlot& slot)
436{
437 ASSERT(slot.slotBase()->inherits(&JSCallbackObject::info));
438 JSCallbackObject* thisObj = static_cast<JSCallbackObject*>(slot.slotBase());
439
440 JSObjectRef thisRef = toRef(thisObj);
441 JSStringRef propertyNameRef = toRef(propertyName.ustring().rep());
442
443 for (JSClassRef jsClass = thisObj->m_class; jsClass; jsClass = jsClass->parentClass)
444 if (OpaqueJSClass::StaticValuesTable* staticValues = jsClass->staticValues)
445 if (StaticValueEntry* entry = staticValues->get(propertyName.ustring().rep()))
446 if (JSObjectGetPropertyCallback getProperty = entry->getProperty) {
447 JSLock::DropAllLocks dropAllLocks;
448 if (JSValueRef value = getProperty(toRef(exec), thisRef, propertyNameRef, toRef(exec->exceptionSlot())))
449 return toJS(value);
450 }
451
452 return throwError(exec, ReferenceError, "Static value property defined with NULL getProperty callback.");
453}
454
455template <class Base>
456JSValue* JSCallbackObject<Base>::staticFunctionGetter(ExecState* exec, JSObject*, const Identifier& propertyName, const PropertySlot& slot)
457{
458 ASSERT(slot.slotBase()->inherits(&JSCallbackObject::info));
459 JSCallbackObject* thisObj = static_cast<JSCallbackObject*>(slot.slotBase());
460
461 if (JSValue* cachedOrOverrideValue = thisObj->getDirect(propertyName))
462 return cachedOrOverrideValue;
463
464 for (JSClassRef jsClass = thisObj->m_class; jsClass; jsClass = jsClass->parentClass) {
465 if (OpaqueJSClass::StaticFunctionsTable* staticFunctions = jsClass->staticFunctions) {
466 if (StaticFunctionEntry* entry = staticFunctions->get(propertyName.ustring().rep())) {
467 if (JSObjectCallAsFunctionCallback callAsFunction = entry->callAsFunction) {
468 JSObject* o = new JSCallbackFunction(exec, callAsFunction, propertyName);
469 thisObj->putDirect(propertyName, o, entry->attributes);
470 return o;
471 }
472 }
473 }
474 }
475
476 return throwError(exec, ReferenceError, "Static function property defined with NULL callAsFunction callback.");
477}
478
479template <class Base>
480JSValue* JSCallbackObject<Base>::callbackGetter(ExecState* exec, JSObject*, const Identifier& propertyName, const PropertySlot& slot)
481{
482 ASSERT(slot.slotBase()->inherits(&JSCallbackObject::info));
483 JSCallbackObject* thisObj = static_cast<JSCallbackObject*>(slot.slotBase());
484
485 JSObjectRef thisRef = toRef(thisObj);
486 JSStringRef propertyNameRef = toRef(propertyName.ustring().rep());
487
488 for (JSClassRef jsClass = thisObj->m_class; jsClass; jsClass = jsClass->parentClass)
489 if (JSObjectGetPropertyCallback getProperty = jsClass->getProperty) {
490 JSLock::DropAllLocks dropAllLocks;
491 if (JSValueRef value = getProperty(toRef(exec), thisRef, propertyNameRef, toRef(exec->exceptionSlot())))
492 return toJS(value);
493 }
494
495 return throwError(exec, ReferenceError, "hasProperty callback returned true for a property that doesn't exist.");
496}
497
498} // namespace KJS
Note: See TracBrowser for help on using the repository browser.