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 // FIXME: <rdar://problem/4949018>
43 : className(definition
->className
)
44 , parentClass(definition
->parentClass
)
48 , initialize(definition
->initialize
)
49 , finalize(definition
->finalize
)
50 , hasProperty(definition
->hasProperty
)
51 , getProperty(definition
->getProperty
)
52 , setProperty(definition
->setProperty
)
53 , deleteProperty(definition
->deleteProperty
)
54 , getPropertyNames(definition
->getPropertyNames
)
55 , callAsFunction(definition
->callAsFunction
)
56 , callAsConstructor(definition
->callAsConstructor
)
57 , hasInstance(definition
->hasInstance
)
58 , convertToType(definition
->convertToType
)
61 if (const JSStaticValue
* staticValue
= definition
->staticValues
) {
62 staticValues
= new StaticValuesTable();
63 while (staticValue
->name
) {
64 // FIXME: <rdar://problem/4949018>
65 staticValues
->add(Identifier(staticValue
->name
).ustring().rep(),
66 new StaticValueEntry(staticValue
->getProperty
, staticValue
->setProperty
, staticValue
->attributes
));
71 if (const JSStaticFunction
* staticFunction
= definition
->staticFunctions
) {
72 staticFunctions
= new StaticFunctionsTable();
73 while (staticFunction
->name
) {
74 // FIXME: <rdar://problem/4949018>
75 staticFunctions
->add(Identifier(staticFunction
->name
).ustring().rep(),
76 new StaticFunctionEntry(staticFunction
->callAsFunction
, staticFunction
->attributes
));
82 prototypeClass
= JSClassRetain(protoClass
);
85 OpaqueJSClass::~OpaqueJSClass()
88 deleteAllValues(*staticValues
);
92 if (staticFunctions
) {
93 deleteAllValues(*staticFunctions
);
94 delete staticFunctions
;
98 JSClassRelease(prototypeClass
);
101 JSClassRef
OpaqueJSClass::createNoAutomaticPrototype(const JSClassDefinition
* definition
)
103 return new OpaqueJSClass(definition
, 0);
106 void clearReferenceToPrototype(JSObjectRef prototype
)
108 OpaqueJSClass
* jsClass
= static_cast<OpaqueJSClass
*>(JSObjectGetPrivate(prototype
));
110 jsClass
->cachedPrototype
= 0;
113 JSClassRef
OpaqueJSClass::create(const JSClassDefinition
* definition
)
115 if (const JSStaticFunction
* staticFunctions
= definition
->staticFunctions
) {
116 // copy functions into a prototype class
117 JSClassDefinition protoDefinition
= kJSClassDefinitionEmpty
;
118 protoDefinition
.staticFunctions
= staticFunctions
;
119 protoDefinition
.finalize
= clearReferenceToPrototype
;
120 OpaqueJSClass
* protoClass
= new OpaqueJSClass(&protoDefinition
, 0);
122 // remove functions from the original class
123 JSClassDefinition objectDefinition
= *definition
;
124 objectDefinition
.staticFunctions
= 0;
125 return new OpaqueJSClass(&objectDefinition
, protoClass
);
128 return new OpaqueJSClass(definition
, 0);
132 // Doc here in case we make this public. (Hopefully we won't.)
134 @abstract Returns the prototype that will be used when constructing an object with a given class.
135 @param ctx The execution context to use.
136 @param jsClass A JSClass whose prototype you want to get.
137 @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.
139 JSObject
* OpaqueJSClass::prototype(JSContextRef ctx
)
141 /* Class (C++) and prototype (JS) inheritance are parallel, so:
143 * ParentClass | ParentClassPrototype
146 * DerivedClass | DerivedClassPrototype
152 ExecState
* exec
= toJS(ctx
);
154 if (!cachedPrototype
) {
155 // Recursive, but should be good enough for our purposes
156 JSObject
* parentPrototype
= 0;
158 parentPrototype
= parentClass
->prototype(ctx
); // can be null
159 if (!parentPrototype
)
160 parentPrototype
= exec
->dynamicGlobalObject()->objectPrototype();
161 cachedPrototype
= new JSCallbackObject
<JSObject
>(exec
, prototypeClass
, parentPrototype
, this); // set ourself as the object's private data, so it can clear our reference on destruction
163 return cachedPrototype
;