source: webkit/trunk/JavaScriptCore/API/JSClassRef.cpp@ 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: 6.3 KB
Line 
1// -*- mode: c++; c-basic-offset: 4 -*-
2/*
3 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved.
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 <wtf/Platform.h>
28#include "APICast.h"
29#include "JSCallbackObject.h"
30#include "JSClassRef.h"
31#include "JSObjectRef.h"
32#include "identifier.h"
33
34using namespace KJS;
35
36const JSClassDefinition kJSClassDefinitionEmpty = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
37
38OpaqueJSClass::OpaqueJSClass(const JSClassDefinition* definition, OpaqueJSClass* protoClass)
39 : refCount(0)
40 // FIXME: <rdar://problem/4949018>
41 , className(definition->className)
42 , parentClass(definition->parentClass)
43 , prototypeClass(0)
44 , staticValues(0)
45 , staticFunctions(0)
46 , initialize(definition->initialize)
47 , finalize(definition->finalize)
48 , hasProperty(definition->hasProperty)
49 , getProperty(definition->getProperty)
50 , setProperty(definition->setProperty)
51 , deleteProperty(definition->deleteProperty)
52 , getPropertyNames(definition->getPropertyNames)
53 , callAsFunction(definition->callAsFunction)
54 , callAsConstructor(definition->callAsConstructor)
55 , hasInstance(definition->hasInstance)
56 , convertToType(definition->convertToType)
57 , cachedPrototype(0)
58{
59 if (const JSStaticValue* staticValue = definition->staticValues) {
60 staticValues = new StaticValuesTable();
61 while (staticValue->name) {
62 // FIXME: <rdar://problem/4949018>
63 staticValues->add(Identifier(staticValue->name).ustring().rep(),
64 new StaticValueEntry(staticValue->getProperty, staticValue->setProperty, staticValue->attributes));
65 ++staticValue;
66 }
67 }
68
69 if (const JSStaticFunction* staticFunction = definition->staticFunctions) {
70 staticFunctions = new StaticFunctionsTable();
71 while (staticFunction->name) {
72 // FIXME: <rdar://problem/4949018>
73 staticFunctions->add(Identifier(staticFunction->name).ustring().rep(),
74 new StaticFunctionEntry(staticFunction->callAsFunction, staticFunction->attributes));
75 ++staticFunction;
76 }
77 }
78
79 if (protoClass)
80 prototypeClass = JSClassRetain(protoClass);
81}
82
83OpaqueJSClass::~OpaqueJSClass()
84{
85 if (staticValues) {
86 deleteAllValues(*staticValues);
87 delete staticValues;
88 }
89
90 if (staticFunctions) {
91 deleteAllValues(*staticFunctions);
92 delete staticFunctions;
93 }
94
95 if (prototypeClass)
96 JSClassRelease(prototypeClass);
97}
98
99JSClassRef OpaqueJSClass::createNoAutomaticPrototype(const JSClassDefinition* definition)
100{
101 return new OpaqueJSClass(definition, 0);
102}
103
104void clearReferenceToPrototype(JSObjectRef prototype)
105{
106 OpaqueJSClass* jsClass = static_cast<OpaqueJSClass*>(JSObjectGetPrivate(prototype));
107 ASSERT(jsClass);
108 jsClass->cachedPrototype = 0;
109}
110
111JSClassRef OpaqueJSClass::create(const JSClassDefinition* definition)
112{
113 if (const JSStaticFunction* staticFunctions = definition->staticFunctions) {
114 // copy functions into a prototype class
115 JSClassDefinition protoDefinition = kJSClassDefinitionEmpty;
116 protoDefinition.staticFunctions = staticFunctions;
117 protoDefinition.finalize = clearReferenceToPrototype;
118 OpaqueJSClass* protoClass = new OpaqueJSClass(&protoDefinition, 0);
119
120 // remove functions from the original class
121 JSClassDefinition objectDefinition = *definition;
122 objectDefinition.staticFunctions = 0;
123 return new OpaqueJSClass(&objectDefinition, protoClass);
124 }
125
126 return new OpaqueJSClass(definition, 0);
127}
128
129/*!
130// Doc here in case we make this public. (Hopefully we won't.)
131@function
132 @abstract Returns the prototype that will be used when constructing an object with a given class.
133 @param ctx The execution context to use.
134 @param jsClass A JSClass whose prototype you want to get.
135 @result The JSObject prototype that was automatically generated for jsClass, or NULL if no prototype was automatically generated. This is the prototype that will be used when constructing an object using jsClass.
136*/
137JSObject* OpaqueJSClass::prototype(JSContextRef ctx)
138{
139 /* Class (C++) and prototype (JS) inheritance are parallel, so:
140 * (C++) | (JS)
141 * ParentClass | ParentClassPrototype
142 * ^ | ^
143 * | | |
144 * DerivedClass | DerivedClassPrototype
145 */
146
147 if (!prototypeClass)
148 return 0;
149
150 ExecState* exec = toJS(ctx);
151
152 if (!cachedPrototype) {
153 // Recursive, but should be good enough for our purposes
154 JSObject* parentPrototype = 0;
155 if (parentClass)
156 parentPrototype = parentClass->prototype(ctx); // can be null
157 if (!parentPrototype)
158 parentPrototype = exec->dynamicInterpreter()->builtinObjectPrototype();
159 cachedPrototype = new JSCallbackObject<JSObject>(exec, prototypeClass, parentPrototype, this); // set ourself as the object's private data, so it can clear our reference on destruction
160 }
161 return cachedPrototype;
162}
Note: See TracBrowser for help on using the repository browser.