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>
36 #include <wtf/text/StringHash.h>
37 #include <wtf/unicode/UTF8.h>
41 using namespace WTF::Unicode
;
43 const JSClassDefinition kJSClassDefinitionEmpty
= { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
45 static inline UString
tryCreateStringFromUTF8(const char* string
)
50 size_t length
= strlen(string
);
51 Vector
<UChar
, 1024> buffer(length
);
52 UChar
* p
= buffer
.data();
53 if (conversionOK
!= convertUTF8ToUTF16(&string
, string
+ length
, &p
, p
+ length
))
56 return UString(buffer
.data(), p
- buffer
.data());
59 OpaqueJSClass::OpaqueJSClass(const JSClassDefinition
* definition
, OpaqueJSClass
* protoClass
)
60 : parentClass(definition
->parentClass
)
62 , initialize(definition
->initialize
)
63 , finalize(definition
->finalize
)
64 , hasProperty(definition
->hasProperty
)
65 , getProperty(definition
->getProperty
)
66 , setProperty(definition
->setProperty
)
67 , deleteProperty(definition
->deleteProperty
)
68 , getPropertyNames(definition
->getPropertyNames
)
69 , callAsFunction(definition
->callAsFunction
)
70 , callAsConstructor(definition
->callAsConstructor
)
71 , hasInstance(definition
->hasInstance
)
72 , convertToType(definition
->convertToType
)
73 , m_className(tryCreateStringFromUTF8(definition
->className
))
75 initializeThreading();
77 if (const JSStaticValue
* staticValue
= definition
->staticValues
) {
78 m_staticValues
= adoptPtr(new OpaqueJSClassStaticValuesTable
);
79 while (staticValue
->name
) {
80 UString valueName
= tryCreateStringFromUTF8(staticValue
->name
);
81 if (!valueName
.isNull())
82 m_staticValues
->set(valueName
.impl(), adoptPtr(new StaticValueEntry(staticValue
->getProperty
, staticValue
->setProperty
, staticValue
->attributes
)));
87 if (const JSStaticFunction
* staticFunction
= definition
->staticFunctions
) {
88 m_staticFunctions
= adoptPtr(new OpaqueJSClassStaticFunctionsTable
);
89 while (staticFunction
->name
) {
90 UString functionName
= tryCreateStringFromUTF8(staticFunction
->name
);
91 if (!functionName
.isNull())
92 m_staticFunctions
->set(functionName
.impl(), adoptPtr(new StaticFunctionEntry(staticFunction
->callAsFunction
, staticFunction
->attributes
)));
98 prototypeClass
= JSClassRetain(protoClass
);
101 OpaqueJSClass::~OpaqueJSClass()
103 // The empty string is shared across threads & is an identifier, in all other cases we should have done a deep copy in className(), below.
104 ASSERT(!m_className
.length() || !m_className
.impl()->isIdentifier());
107 if (m_staticValues
) {
108 OpaqueJSClassStaticValuesTable::const_iterator end
= m_staticValues
->end();
109 for (OpaqueJSClassStaticValuesTable::const_iterator it
= m_staticValues
->begin(); it
!= end
; ++it
)
110 ASSERT(!it
->first
->isIdentifier());
113 if (m_staticFunctions
) {
114 OpaqueJSClassStaticFunctionsTable::const_iterator end
= m_staticFunctions
->end();
115 for (OpaqueJSClassStaticFunctionsTable::const_iterator it
= m_staticFunctions
->begin(); it
!= end
; ++it
)
116 ASSERT(!it
->first
->isIdentifier());
121 JSClassRelease(prototypeClass
);
124 PassRefPtr
<OpaqueJSClass
> OpaqueJSClass::createNoAutomaticPrototype(const JSClassDefinition
* definition
)
126 return adoptRef(new OpaqueJSClass(definition
, 0));
129 PassRefPtr
<OpaqueJSClass
> OpaqueJSClass::create(const JSClassDefinition
* clientDefinition
)
131 JSClassDefinition definition
= *clientDefinition
; // Avoid modifying client copy.
133 JSClassDefinition protoDefinition
= kJSClassDefinitionEmpty
;
134 protoDefinition
.finalize
= 0;
135 swap(definition
.staticFunctions
, protoDefinition
.staticFunctions
); // Move static functions to the prototype.
137 // We are supposed to use JSClassRetain/Release but since we know that we currently have
138 // the only reference to this class object we cheat and use a RefPtr instead.
139 RefPtr
<OpaqueJSClass
> protoClass
= adoptRef(new OpaqueJSClass(&protoDefinition
, 0));
140 return adoptRef(new OpaqueJSClass(&definition
, protoClass
.get()));
143 OpaqueJSClassContextData::OpaqueJSClassContextData(JSC::JSGlobalData
&, OpaqueJSClass
* jsClass
)
146 if (jsClass
->m_staticValues
) {
147 staticValues
= adoptPtr(new OpaqueJSClassStaticValuesTable
);
148 OpaqueJSClassStaticValuesTable::const_iterator end
= jsClass
->m_staticValues
->end();
149 for (OpaqueJSClassStaticValuesTable::const_iterator it
= jsClass
->m_staticValues
->begin(); it
!= end
; ++it
) {
150 ASSERT(!it
->first
->isIdentifier());
151 staticValues
->add(StringImpl::create(it
->first
->characters(), it
->first
->length()), adoptPtr(new StaticValueEntry(it
->second
->getProperty
, it
->second
->setProperty
, it
->second
->attributes
)));
155 if (jsClass
->m_staticFunctions
) {
156 staticFunctions
= adoptPtr(new OpaqueJSClassStaticFunctionsTable
);
157 OpaqueJSClassStaticFunctionsTable::const_iterator end
= jsClass
->m_staticFunctions
->end();
158 for (OpaqueJSClassStaticFunctionsTable::const_iterator it
= jsClass
->m_staticFunctions
->begin(); it
!= end
; ++it
) {
159 ASSERT(!it
->first
->isIdentifier());
160 staticFunctions
->add(StringImpl::create(it
->first
->characters(), it
->first
->length()), adoptPtr(new StaticFunctionEntry(it
->second
->callAsFunction
, it
->second
->attributes
)));
165 OpaqueJSClassContextData
& OpaqueJSClass::contextData(ExecState
* exec
)
167 OwnPtr
<OpaqueJSClassContextData
>& contextData
= exec
->globalData().opaqueJSClassData
.add(this, nullptr).iterator
->second
;
169 contextData
= adoptPtr(new OpaqueJSClassContextData(exec
->globalData(), this));
173 UString
OpaqueJSClass::className()
175 // Make a deep copy, so that the caller has no chance to put the original into IdentifierTable.
176 return UString(m_className
.characters(), m_className
.length());
179 OpaqueJSClassStaticValuesTable
* OpaqueJSClass::staticValues(JSC::ExecState
* exec
)
181 return contextData(exec
).staticValues
.get();
184 OpaqueJSClassStaticFunctionsTable
* OpaqueJSClass::staticFunctions(JSC::ExecState
* exec
)
186 return contextData(exec
).staticFunctions
.get();
190 // Doc here in case we make this public. (Hopefully we won't.)
192 @abstract Returns the prototype that will be used when constructing an object with a given class.
193 @param ctx The execution context to use.
194 @param jsClass A JSClass whose prototype you want to get.
195 @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.
197 JSObject
* OpaqueJSClass::prototype(ExecState
* exec
)
199 /* Class (C++) and prototype (JS) inheritance are parallel, so:
201 * ParentClass | ParentClassPrototype
204 * DerivedClass | DerivedClassPrototype
210 OpaqueJSClassContextData
& jsClassData
= contextData(exec
);
212 if (!jsClassData
.cachedPrototype
) {
213 // Recursive, but should be good enough for our purposes
214 jsClassData
.cachedPrototype
= PassWeak
<JSObject
>(JSCallbackObject
<JSNonFinalObject
>::create(exec
, exec
->lexicalGlobalObject(), exec
->lexicalGlobalObject()->callbackObjectStructure(), prototypeClass
, &jsClassData
), 0); // set jsClassData as the object's private data, so it can clear our reference on destruction
216 if (JSObject
* prototype
= parentClass
->prototype(exec
))
217 jsClassData
.cachedPrototype
->setPrototype(exec
->globalData(), prototype
);
220 return jsClassData
.cachedPrototype
.get();