1/*
2 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "JSClassRef.h"
28
29#include "APICast.h"
30#include "JSCallbackObject.h"
31#include "JSObjectRef.h"
32#include <runtime/InitializeThreading.h>
33#include <runtime/JSGlobalObject.h>
34#include <runtime/ObjectPrototype.h>
35#include <runtime/Identifier.h>
36
37using namespace std;
38using namespace JSC;
39
40const JSClassDefinition kJSClassDefinitionEmpty = { .version: 0, .attributes: 0, .className: 0, .parentClass: 0, .staticValues: 0, .staticFunctions: 0, .initialize: 0, .finalize: 0, .hasProperty: 0, .getProperty: 0, .setProperty: 0, .deleteProperty: 0, .getPropertyNames: 0, .callAsFunction: 0, .callAsConstructor: 0, .hasInstance: 0, .convertToType: 0 };
41
42OpaqueJSClass::OpaqueJSClass(const JSClassDefinition* definition, OpaqueJSClass* protoClass)
43 : parentClass(definition->parentClass)
44 , prototypeClass(0)
45 , initialize(definition->initialize)
46 , finalize(definition->finalize)
47 , hasProperty(definition->hasProperty)
48 , getProperty(definition->getProperty)
49 , setProperty(definition->setProperty)
50 , deleteProperty(definition->deleteProperty)
51 , getPropertyNames(definition->getPropertyNames)
52 , callAsFunction(definition->callAsFunction)
53 , callAsConstructor(definition->callAsConstructor)
54 , hasInstance(definition->hasInstance)
55 , convertToType(definition->convertToType)
56 , m_className(UString::createFromUTF8(definition->className).rep()->ref())
57 , m_staticValues(0)
58 , m_staticFunctions(0)
59{
60 initializeThreading();
61
62 if (const JSStaticValue* staticValue = definition->staticValues) {
63 m_staticValues = new OpaqueJSClassStaticValuesTable();
64 while (staticValue->name) {
65 // Use a local variable here to sidestep an RVCT compiler bug.
66 StaticValueEntry* entry = new StaticValueEntry(staticValue->getProperty, staticValue->setProperty, staticValue->attributes);
67 m_staticValues->add(key: UString::createFromUTF8(staticValue->name).rep()->ref(), mapped: entry);
68 ++staticValue;
69 }
70 }
71
72 if (const JSStaticFunction* staticFunction = definition->staticFunctions) {
73 m_staticFunctions = new OpaqueJSClassStaticFunctionsTable();
74 while (staticFunction->name) {
75 // Use a local variable here to sidestep an RVCT compiler bug.
76 StaticFunctionEntry* entry = new StaticFunctionEntry(staticFunction->callAsFunction, staticFunction->attributes);
77 m_staticFunctions->add(key: UString::createFromUTF8(staticFunction->name).rep()->ref(), mapped: entry);
78 ++staticFunction;
79 }
80 }
81
82 if (protoClass)
83 prototypeClass = JSClassRetain(jsClass: protoClass);
84}
85
86OpaqueJSClass::~OpaqueJSClass()
87{
88 ASSERT(!m_className.rep()->isIdentifier());
89
90 if (m_staticValues) {
91 OpaqueJSClassStaticValuesTable::const_iterator end = m_staticValues->end();
92 for (OpaqueJSClassStaticValuesTable::const_iterator it = m_staticValues->begin(); it != end; ++it) {
93 ASSERT(!it->first->isIdentifier());
94 delete it->second;
95 }
96 delete m_staticValues;
97 }
98
99 if (m_staticFunctions) {
100 OpaqueJSClassStaticFunctionsTable::const_iterator end = m_staticFunctions->end();
101 for (OpaqueJSClassStaticFunctionsTable::const_iterator it = m_staticFunctions->begin(); it != end; ++it) {
102 ASSERT(!it->first->isIdentifier());
103 delete it->second;
104 }
105 delete m_staticFunctions;
106 }
107
108 if (prototypeClass)
109 JSClassRelease(jsClass: prototypeClass);
110}
111
112PassRefPtr<OpaqueJSClass> OpaqueJSClass::createNoAutomaticPrototype(const JSClassDefinition* definition)
113{
114 return adoptRef(p: new OpaqueJSClass(definition, 0));
115}
116
117static void clearReferenceToPrototype(JSObjectRef prototype)
118{
119 OpaqueJSClassContextData* jsClassData = static_cast<OpaqueJSClassContextData*>(JSObjectGetPrivate(object: prototype));
120 ASSERT(jsClassData);
121 jsClassData->cachedPrototype = 0;
122}
123
124PassRefPtr<OpaqueJSClass> OpaqueJSClass::create(const JSClassDefinition* clientDefinition)
125{
126 JSClassDefinition definition = *clientDefinition; // Avoid modifying client copy.
127
128 JSClassDefinition protoDefinition = kJSClassDefinitionEmpty;
129 protoDefinition.finalize = clearReferenceToPrototype;
130 swap(a&: definition.staticFunctions, b&: protoDefinition.staticFunctions); // Move static functions to the prototype.
131
132 // We are supposed to use JSClassRetain/Release but since we know that we currently have
133 // the only reference to this class object we cheat and use a RefPtr instead.
134 RefPtr<OpaqueJSClass> protoClass = adoptRef(p: new OpaqueJSClass(&protoDefinition, 0));
135 return adoptRef(p: new OpaqueJSClass(&definition, protoClass.get()));
136}
137
138OpaqueJSClassContextData::OpaqueJSClassContextData(OpaqueJSClass* jsClass)
139 : m_class(jsClass)
140{
141 if (jsClass->m_staticValues) {
142 staticValues = new OpaqueJSClassStaticValuesTable;
143 OpaqueJSClassStaticValuesTable::const_iterator end = jsClass->m_staticValues->end();
144 for (OpaqueJSClassStaticValuesTable::const_iterator it = jsClass->m_staticValues->begin(); it != end; ++it) {
145 ASSERT(!it->first->isIdentifier());
146 // Use a local variable here to sidestep an RVCT compiler bug.
147 StaticValueEntry* entry = new StaticValueEntry(it->second->getProperty, it->second->setProperty, it->second->attributes);
148 staticValues->add(key: UString::Rep::create(buffer: it->first->data(), length: it->first->size()), mapped: entry);
149
150 }
151
152 } else
153 staticValues = 0;
154
155
156 if (jsClass->m_staticFunctions) {
157 staticFunctions = new OpaqueJSClassStaticFunctionsTable;
158 OpaqueJSClassStaticFunctionsTable::const_iterator end = jsClass->m_staticFunctions->end();
159 for (OpaqueJSClassStaticFunctionsTable::const_iterator it = jsClass->m_staticFunctions->begin(); it != end; ++it) {
160 ASSERT(!it->first->isIdentifier());
161 // Use a local variable here to sidestep an RVCT compiler bug.
162 StaticFunctionEntry* entry = new StaticFunctionEntry(it->second->callAsFunction, it->second->attributes);
163 staticFunctions->add(key: UString::Rep::create(buffer: it->first->data(), length: it->first->size()), mapped: entry);
164 }
165
166 } else
167 staticFunctions = 0;
168}
169
170OpaqueJSClassContextData::~OpaqueJSClassContextData()
171{
172 if (staticValues) {
173 deleteAllValues(collection: *staticValues);
174 delete staticValues;
175 }
176
177 if (staticFunctions) {
178 deleteAllValues(collection: *staticFunctions);
179 delete staticFunctions;
180 }
181}
182
183OpaqueJSClassContextData& OpaqueJSClass::contextData(ExecState* exec)
184{
185 OpaqueJSClassContextData*& contextData = exec->globalData().opaqueJSClassData.add(key: this, mapped: 0).first->second;
186 if (!contextData)
187 contextData = new OpaqueJSClassContextData(this);
188 return *contextData;
189}
190
191UString OpaqueJSClass::className()
192{
193 // Make a deep copy, so that the caller has no chance to put the original into IdentifierTable.
194 return UString(m_className.data(), m_className.size());
195}
196
197OpaqueJSClassStaticValuesTable* OpaqueJSClass::staticValues(JSC::ExecState* exec)
198{
199 OpaqueJSClassContextData& jsClassData = contextData(exec);
200 return jsClassData.staticValues;
201}
202
203OpaqueJSClassStaticFunctionsTable* OpaqueJSClass::staticFunctions(JSC::ExecState* exec)
204{
205 OpaqueJSClassContextData& jsClassData = contextData(exec);
206 return jsClassData.staticFunctions;
207}
208
209/*!
210// Doc here in case we make this public. (Hopefully we won't.)
211@function
212 @abstract Returns the prototype that will be used when constructing an object with a given class.
213 @param ctx The execution context to use.
214 @param jsClass A JSClass whose prototype you want to get.
215 @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.
216*/
217JSObject* OpaqueJSClass::prototype(ExecState* exec)
218{
219 /* Class (C++) and prototype (JS) inheritance are parallel, so:
220 * (C++) | (JS)
221 * ParentClass | ParentClassPrototype
222 * ^ | ^
223 * | | |
224 * DerivedClass | DerivedClassPrototype
225 */
226
227 if (!prototypeClass)
228 return 0;
229
230 OpaqueJSClassContextData& jsClassData = contextData(exec);
231
232 if (!jsClassData.cachedPrototype) {
233 // Recursive, but should be good enough for our purposes
234 jsClassData.cachedPrototype = new (exec) JSCallbackObject<JSObject>(exec, exec->lexicalGlobalObject()->callbackObjectStructure(), prototypeClass, &jsClassData); // set jsClassData as the object's private data, so it can clear our reference on destruction
235 if (parentClass) {
236 if (JSObject* prototype = parentClass->prototype(exec))
237 jsClassData.cachedPrototype->setPrototype(prototype);
238 }
239 }
240 return jsClassData.cachedPrototype.get();
241}
242

source code of qtscript/src/3rdparty/javascriptcore/JavaScriptCore/API/JSClassRef.cpp