2 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "JSClassRef.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>
40 const JSClassDefinition kJSClassDefinitionEmpty
= { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
42 OpaqueJSClass::OpaqueJSClass(const JSClassDefinition
* definition
, OpaqueJSClass
* protoClass
)
43 : parentClass(definition
->parentClass
)
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())
58 , m_staticFunctions(0)
60 initializeThreading();
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(UString::createFromUTF8(staticValue
->name
).rep()->ref(), entry
);
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(UString::createFromUTF8(staticFunction
->name
).rep()->ref(), entry
);
83 prototypeClass
= JSClassRetain(protoClass
);
86 OpaqueJSClass::~OpaqueJSClass()
88 ASSERT(!m_className
.rep()->isIdentifier());
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());
96 delete m_staticValues
;
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());
105 delete m_staticFunctions
;
109 JSClassRelease(prototypeClass
);
112 PassRefPtr
<OpaqueJSClass
> OpaqueJSClass::createNoAutomaticPrototype(const JSClassDefinition
* definition
)
114 return adoptRef(new OpaqueJSClass(definition
, 0));
117 static void clearReferenceToPrototype(JSObjectRef prototype
)
119 OpaqueJSClassContextData
* jsClassData
= static_cast<OpaqueJSClassContextData
*>(JSObjectGetPrivate(prototype
));
121 jsClassData
->cachedPrototype
.clear(toJS(prototype
));
124 PassRefPtr
<OpaqueJSClass
> OpaqueJSClass::create(const JSClassDefinition
* clientDefinition
)
126 JSClassDefinition definition
= *clientDefinition
; // Avoid modifying client copy.
128 JSClassDefinition protoDefinition
= kJSClassDefinitionEmpty
;
129 protoDefinition
.finalize
= clearReferenceToPrototype
;
130 swap(definition
.staticFunctions
, protoDefinition
.staticFunctions
); // Move static functions to the prototype.
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(new OpaqueJSClass(&protoDefinition
, 0));
135 return adoptRef(new OpaqueJSClass(&definition
, protoClass
.get()));
138 OpaqueJSClassContextData::OpaqueJSClassContextData(OpaqueJSClass
* jsClass
)
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(UString::Rep::create(it
->first
->data(), it
->first
->size()), entry
);
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(UString::Rep::create(it
->first
->data(), it
->first
->size()), entry
);
170 OpaqueJSClassContextData::~OpaqueJSClassContextData()
173 deleteAllValues(*staticValues
);
177 if (staticFunctions
) {
178 deleteAllValues(*staticFunctions
);
179 delete staticFunctions
;
183 OpaqueJSClassContextData
& OpaqueJSClass::contextData(ExecState
* exec
)
185 OpaqueJSClassContextData
*& contextData
= exec
->globalData().opaqueJSClassData
.add(this, 0).first
->second
;
187 contextData
= new OpaqueJSClassContextData(this);
191 UString
OpaqueJSClass::className()
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());
197 OpaqueJSClassStaticValuesTable
* OpaqueJSClass::staticValues(JSC::ExecState
* exec
)
199 OpaqueJSClassContextData
& jsClassData
= contextData(exec
);
200 return jsClassData
.staticValues
;
203 OpaqueJSClassStaticFunctionsTable
* OpaqueJSClass::staticFunctions(JSC::ExecState
* exec
)
205 OpaqueJSClassContextData
& jsClassData
= contextData(exec
);
206 return jsClassData
.staticFunctions
;
210 // Doc here in case we make this public. (Hopefully we won't.)
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.
217 JSObject
* OpaqueJSClass::prototype(ExecState
* exec
)
219 /* Class (C++) and prototype (JS) inheritance are parallel, so:
221 * ParentClass | ParentClassPrototype
224 * DerivedClass | DerivedClassPrototype
230 OpaqueJSClassContextData
& jsClassData
= contextData(exec
);
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
236 if (JSObject
* prototype
= parentClass
->prototype(exec
))
237 jsClassData
.cachedPrototype
->setPrototype(prototype
);
240 return jsClassData
.cachedPrototype
.get();