1 // -*- mode: c++; c-basic-offset: 4 -*-
3 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
28 #include "JSClassRef.h"
31 #include "JSCallbackObject.h"
32 #include "JSObjectRef.h"
33 #include <kjs/JSGlobalObject.h>
34 #include <kjs/identifier.h>
35 #include <kjs/object_object.h>
39 const JSClassDefinition kJSClassDefinitionEmpty = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
41 OpaqueJSClass::OpaqueJSClass(const JSClassDefinition* definition, OpaqueJSClass* protoClass)
42 : className(UString::Rep::createFromUTF8(definition->className))
43 , parentClass(definition->parentClass)
47 , initialize(definition->initialize)
48 , finalize(definition->finalize)
49 , hasProperty(definition->hasProperty)
50 , getProperty(definition->getProperty)
51 , setProperty(definition->setProperty)
52 , deleteProperty(definition->deleteProperty)
53 , getPropertyNames(definition->getPropertyNames)
54 , callAsFunction(definition->callAsFunction)
55 , callAsConstructor(definition->callAsConstructor)
56 , hasInstance(definition->hasInstance)
57 , convertToType(definition->convertToType)
60 if (const JSStaticValue* staticValue = definition->staticValues) {
61 staticValues = new StaticValuesTable();
62 while (staticValue->name) {
63 staticValues->add(Identifier(UString::Rep::createFromUTF8(staticValue->name).get()).ustring().rep(),
64 new StaticValueEntry(staticValue->getProperty, staticValue->setProperty, staticValue->attributes));
69 if (const JSStaticFunction* staticFunction = definition->staticFunctions) {
70 staticFunctions = new StaticFunctionsTable();
71 while (staticFunction->name) {
72 staticFunctions->add(Identifier(UString::Rep::createFromUTF8(staticFunction->name).get()).ustring().rep(),
73 new StaticFunctionEntry(staticFunction->callAsFunction, staticFunction->attributes));
79 prototypeClass = JSClassRetain(protoClass);
82 OpaqueJSClass::~OpaqueJSClass()
85 deleteAllValues(*staticValues);
89 if (staticFunctions) {
90 deleteAllValues(*staticFunctions);
91 delete staticFunctions;
95 JSClassRelease(prototypeClass);
98 PassRefPtr<OpaqueJSClass> OpaqueJSClass::createNoAutomaticPrototype(const JSClassDefinition* definition)
100 return adoptRef(new OpaqueJSClass(definition, 0));
103 void clearReferenceToPrototype(JSObjectRef prototype)
105 OpaqueJSClass* jsClass = static_cast<OpaqueJSClass*>(JSObjectGetPrivate(prototype));
107 jsClass->cachedPrototype = 0;
110 PassRefPtr<OpaqueJSClass> OpaqueJSClass::create(const JSClassDefinition* definition)
112 if (const JSStaticFunction* staticFunctions = definition->staticFunctions) {
113 // copy functions into a prototype class
114 JSClassDefinition protoDefinition = kJSClassDefinitionEmpty;
115 protoDefinition.staticFunctions = staticFunctions;
116 protoDefinition.finalize = clearReferenceToPrototype;
118 // We are supposed to use JSClassRetain/Release but since we know that we currently have
119 // the only reference to this class object we cheat and use a RefPtr instead.
120 RefPtr<OpaqueJSClass> protoClass = adoptRef(new OpaqueJSClass(&protoDefinition, 0));
122 // remove functions from the original class
123 JSClassDefinition objectDefinition = *definition;
124 objectDefinition.staticFunctions = 0;
126 return adoptRef(new OpaqueJSClass(&objectDefinition, protoClass.get()));
129 return adoptRef(new OpaqueJSClass(definition, 0));
133 // Doc here in case we make this public. (Hopefully we won't.)
135 @abstract Returns the prototype that will be used when constructing an object with a given class.
136 @param ctx The execution context to use.
137 @param jsClass A JSClass whose prototype you want to get.
138 @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.
140 JSObject* OpaqueJSClass::prototype(JSContextRef ctx)
142 /* Class (C++) and prototype (JS) inheritance are parallel, so:
144 * ParentClass | ParentClassPrototype
147 * DerivedClass | DerivedClassPrototype
153 ExecState* exec = toJS(ctx);
155 if (!cachedPrototype) {
156 // Recursive, but should be good enough for our purposes
157 JSObject* parentPrototype = 0;
159 parentPrototype = parentClass->prototype(ctx); // can be null
160 if (!parentPrototype)
161 parentPrototype = exec->dynamicGlobalObject()->objectPrototype();
162 cachedPrototype = new JSCallbackObject<JSObject>(exec, prototypeClass, parentPrototype, this); // set ourself as the object's private data, so it can clear our reference on destruction
164 return cachedPrototype;