]> git.saurik.com Git - apple/javascriptcore.git/blame - API/JSClassRef.cpp
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / API / JSClassRef.cpp
CommitLineData
b37bf2e1
A
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 *
81345200 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
b37bf2e1
A
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
81345200 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
b37bf2e1
A
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"
93a37866
A
30#include "Identifier.h"
31#include "InitializeThreading.h"
b37bf2e1 32#include "JSCallbackObject.h"
93a37866 33#include "JSGlobalObject.h"
b37bf2e1 34#include "JSObjectRef.h"
93a37866 35#include "ObjectPrototype.h"
81345200 36#include "JSCInlines.h"
4e4e5a6f
A
37#include <wtf/text/StringHash.h>
38#include <wtf/unicode/UTF8.h>
b37bf2e1 39
f9bf01c6 40using namespace std;
9dae56ea 41using namespace JSC;
4e4e5a6f 42using namespace WTF::Unicode;
b37bf2e1
A
43
44const JSClassDefinition kJSClassDefinitionEmpty = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
45
46OpaqueJSClass::OpaqueJSClass(const JSClassDefinition* definition, OpaqueJSClass* protoClass)
9dae56ea 47 : parentClass(definition->parentClass)
b37bf2e1 48 , prototypeClass(0)
b37bf2e1
A
49 , initialize(definition->initialize)
50 , finalize(definition->finalize)
51 , hasProperty(definition->hasProperty)
52 , getProperty(definition->getProperty)
53 , setProperty(definition->setProperty)
54 , deleteProperty(definition->deleteProperty)
55 , getPropertyNames(definition->getPropertyNames)
56 , callAsFunction(definition->callAsFunction)
57 , callAsConstructor(definition->callAsConstructor)
58 , hasInstance(definition->hasInstance)
59 , convertToType(definition->convertToType)
93a37866 60 , m_className(String::fromUTF8(definition->className))
b37bf2e1 61{
9dae56ea
A
62 initializeThreading();
63
b37bf2e1 64 if (const JSStaticValue* staticValue = definition->staticValues) {
ed1e77d3 65 m_staticValues = std::make_unique<OpaqueJSClassStaticValuesTable>();
b37bf2e1 66 while (staticValue->name) {
93a37866 67 String valueName = String::fromUTF8(staticValue->name);
6fe7ccc8 68 if (!valueName.isNull())
81345200 69 m_staticValues->set(valueName.impl(), std::make_unique<StaticValueEntry>(staticValue->getProperty, staticValue->setProperty, staticValue->attributes, valueName));
b37bf2e1
A
70 ++staticValue;
71 }
72 }
9dae56ea 73
b37bf2e1 74 if (const JSStaticFunction* staticFunction = definition->staticFunctions) {
ed1e77d3 75 m_staticFunctions = std::make_unique<OpaqueJSClassStaticFunctionsTable>();
b37bf2e1 76 while (staticFunction->name) {
93a37866 77 String functionName = String::fromUTF8(staticFunction->name);
6fe7ccc8 78 if (!functionName.isNull())
81345200 79 m_staticFunctions->set(functionName.impl(), std::make_unique<StaticFunctionEntry>(staticFunction->callAsFunction, staticFunction->attributes));
b37bf2e1
A
80 ++staticFunction;
81 }
82 }
83
84 if (protoClass)
85 prototypeClass = JSClassRetain(protoClass);
86}
87
88OpaqueJSClass::~OpaqueJSClass()
89{
4e4e5a6f 90 // The empty string is shared across threads & is an identifier, in all other cases we should have done a deep copy in className(), below.
81345200 91 ASSERT(!m_className.length() || !m_className.impl()->isAtomic());
9dae56ea 92
6fe7ccc8 93#ifndef NDEBUG
9dae56ea
A
94 if (m_staticValues) {
95 OpaqueJSClassStaticValuesTable::const_iterator end = m_staticValues->end();
6fe7ccc8 96 for (OpaqueJSClassStaticValuesTable::const_iterator it = m_staticValues->begin(); it != end; ++it)
81345200 97 ASSERT(!it->key->isAtomic());
b37bf2e1
A
98 }
99
9dae56ea
A
100 if (m_staticFunctions) {
101 OpaqueJSClassStaticFunctionsTable::const_iterator end = m_staticFunctions->end();
6fe7ccc8 102 for (OpaqueJSClassStaticFunctionsTable::const_iterator it = m_staticFunctions->begin(); it != end; ++it)
81345200 103 ASSERT(!it->key->isAtomic());
b37bf2e1 104 }
6fe7ccc8 105#endif
b37bf2e1
A
106
107 if (prototypeClass)
108 JSClassRelease(prototypeClass);
109}
110
ed1e77d3 111Ref<OpaqueJSClass> OpaqueJSClass::createNoAutomaticPrototype(const JSClassDefinition* definition)
b37bf2e1 112{
ed1e77d3 113 return adoptRef(*new OpaqueJSClass(definition, 0));
b37bf2e1
A
114}
115
ed1e77d3 116Ref<OpaqueJSClass> OpaqueJSClass::create(const JSClassDefinition* clientDefinition)
b37bf2e1 117{
f9bf01c6 118 JSClassDefinition definition = *clientDefinition; // Avoid modifying client copy.
9dae56ea 119
f9bf01c6 120 JSClassDefinition protoDefinition = kJSClassDefinitionEmpty;
14957cd0 121 protoDefinition.finalize = 0;
f9bf01c6
A
122 swap(definition.staticFunctions, protoDefinition.staticFunctions); // Move static functions to the prototype.
123
124 // We are supposed to use JSClassRetain/Release but since we know that we currently have
125 // the only reference to this class object we cheat and use a RefPtr instead.
126 RefPtr<OpaqueJSClass> protoClass = adoptRef(new OpaqueJSClass(&protoDefinition, 0));
ed1e77d3 127 return adoptRef(*new OpaqueJSClass(&definition, protoClass.get()));
9dae56ea
A
128}
129
93a37866 130OpaqueJSClassContextData::OpaqueJSClassContextData(JSC::VM&, OpaqueJSClass* jsClass)
9dae56ea 131 : m_class(jsClass)
9dae56ea
A
132{
133 if (jsClass->m_staticValues) {
81345200 134 staticValues = std::make_unique<OpaqueJSClassStaticValuesTable>();
9dae56ea
A
135 OpaqueJSClassStaticValuesTable::const_iterator end = jsClass->m_staticValues->end();
136 for (OpaqueJSClassStaticValuesTable::const_iterator it = jsClass->m_staticValues->begin(); it != end; ++it) {
81345200
A
137 ASSERT(!it->key->isAtomic());
138 String valueName = it->key->isolatedCopy();
139 staticValues->add(valueName.impl(), std::make_unique<StaticValueEntry>(it->value->getProperty, it->value->setProperty, it->value->attributes, valueName));
9dae56ea 140 }
6fe7ccc8 141 }
9dae56ea
A
142
143 if (jsClass->m_staticFunctions) {
81345200 144 staticFunctions = std::make_unique<OpaqueJSClassStaticFunctionsTable>();
9dae56ea
A
145 OpaqueJSClassStaticFunctionsTable::const_iterator end = jsClass->m_staticFunctions->end();
146 for (OpaqueJSClassStaticFunctionsTable::const_iterator it = jsClass->m_staticFunctions->begin(); it != end; ++it) {
81345200
A
147 ASSERT(!it->key->isAtomic());
148 staticFunctions->add(it->key->isolatedCopy(), std::make_unique<StaticFunctionEntry>(it->value->callAsFunction, it->value->attributes));
9dae56ea 149 }
9dae56ea
A
150 }
151}
152
153OpaqueJSClassContextData& OpaqueJSClass::contextData(ExecState* exec)
154{
81345200 155 std::unique_ptr<OpaqueJSClassContextData>& contextData = exec->lexicalGlobalObject()->opaqueJSClassData().add(this, nullptr).iterator->value;
9dae56ea 156 if (!contextData)
81345200 157 contextData = std::make_unique<OpaqueJSClassContextData>(exec->vm(), this);
9dae56ea
A
158 return *contextData;
159}
160
93a37866 161String OpaqueJSClass::className()
9dae56ea 162{
81345200 163 // Make a deep copy, so that the caller has no chance to put the original into AtomicStringTable.
93a37866 164 return m_className.isolatedCopy();
9dae56ea
A
165}
166
167OpaqueJSClassStaticValuesTable* OpaqueJSClass::staticValues(JSC::ExecState* exec)
168{
6fe7ccc8 169 return contextData(exec).staticValues.get();
9dae56ea
A
170}
171
172OpaqueJSClassStaticFunctionsTable* OpaqueJSClass::staticFunctions(JSC::ExecState* exec)
173{
6fe7ccc8 174 return contextData(exec).staticFunctions.get();
b37bf2e1
A
175}
176
177/*!
178// Doc here in case we make this public. (Hopefully we won't.)
179@function
180 @abstract Returns the prototype that will be used when constructing an object with a given class.
181 @param ctx The execution context to use.
182 @param jsClass A JSClass whose prototype you want to get.
183 @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.
184*/
9dae56ea 185JSObject* OpaqueJSClass::prototype(ExecState* exec)
b37bf2e1
A
186{
187 /* Class (C++) and prototype (JS) inheritance are parallel, so:
188 * (C++) | (JS)
189 * ParentClass | ParentClassPrototype
190 * ^ | ^
191 * | | |
192 * DerivedClass | DerivedClassPrototype
193 */
6fe7ccc8 194
b37bf2e1
A
195 if (!prototypeClass)
196 return 0;
9dae56ea
A
197
198 OpaqueJSClassContextData& jsClassData = contextData(exec);
199
93a37866
A
200 if (JSObject* prototype = jsClassData.cachedPrototype.get())
201 return prototype;
202
203 // Recursive, but should be good enough for our purposes
204 JSObject* prototype = JSCallbackObject<JSDestructibleObject>::create(exec, exec->lexicalGlobalObject(), exec->lexicalGlobalObject()->callbackObjectStructure(), prototypeClass, &jsClassData); // set jsClassData as the object's private data, so it can clear our reference on destruction
205 if (parentClass) {
206 if (JSObject* parentPrototype = parentClass->prototype(exec))
207 prototype->setPrototype(exec->vm(), parentPrototype);
b37bf2e1 208 }
93a37866 209
81345200 210 jsClassData.cachedPrototype = Weak<JSObject>(prototype);
93a37866 211 return prototype;
b37bf2e1 212}