]> git.saurik.com Git - apple/javascriptcore.git/blob - API/JSClassRef.cpp
JavaScriptCore-466.1.tar.gz
[apple/javascriptcore.git] / API / JSClassRef.cpp
1 // -*- mode: c++; c-basic-offset: 4 -*-
2 /*
3 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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.
25 */
26
27 #include "config.h"
28 #include "JSClassRef.h"
29
30 #include "APICast.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>
36
37 using namespace KJS;
38
39 const JSClassDefinition kJSClassDefinitionEmpty = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
40
41 OpaqueJSClass::OpaqueJSClass(const JSClassDefinition* definition, OpaqueJSClass* protoClass)
42 // FIXME: <rdar://problem/4949018>
43 : className(definition->className)
44 , parentClass(definition->parentClass)
45 , prototypeClass(0)
46 , staticValues(0)
47 , staticFunctions(0)
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)
59 , cachedPrototype(0)
60 {
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));
67 ++staticValue;
68 }
69 }
70
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));
77 ++staticFunction;
78 }
79 }
80
81 if (protoClass)
82 prototypeClass = JSClassRetain(protoClass);
83 }
84
85 OpaqueJSClass::~OpaqueJSClass()
86 {
87 if (staticValues) {
88 deleteAllValues(*staticValues);
89 delete staticValues;
90 }
91
92 if (staticFunctions) {
93 deleteAllValues(*staticFunctions);
94 delete staticFunctions;
95 }
96
97 if (prototypeClass)
98 JSClassRelease(prototypeClass);
99 }
100
101 JSClassRef OpaqueJSClass::createNoAutomaticPrototype(const JSClassDefinition* definition)
102 {
103 return new OpaqueJSClass(definition, 0);
104 }
105
106 void clearReferenceToPrototype(JSObjectRef prototype)
107 {
108 OpaqueJSClass* jsClass = static_cast<OpaqueJSClass*>(JSObjectGetPrivate(prototype));
109 ASSERT(jsClass);
110 jsClass->cachedPrototype = 0;
111 }
112
113 JSClassRef OpaqueJSClass::create(const JSClassDefinition* definition)
114 {
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);
121
122 // remove functions from the original class
123 JSClassDefinition objectDefinition = *definition;
124 objectDefinition.staticFunctions = 0;
125 return new OpaqueJSClass(&objectDefinition, protoClass);
126 }
127
128 return new OpaqueJSClass(definition, 0);
129 }
130
131 /*!
132 // Doc here in case we make this public. (Hopefully we won't.)
133 @function
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.
138 */
139 JSObject* OpaqueJSClass::prototype(JSContextRef ctx)
140 {
141 /* Class (C++) and prototype (JS) inheritance are parallel, so:
142 * (C++) | (JS)
143 * ParentClass | ParentClassPrototype
144 * ^ | ^
145 * | | |
146 * DerivedClass | DerivedClassPrototype
147 */
148
149 if (!prototypeClass)
150 return 0;
151
152 ExecState* exec = toJS(ctx);
153
154 if (!cachedPrototype) {
155 // Recursive, but should be good enough for our purposes
156 JSObject* parentPrototype = 0;
157 if (parentClass)
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
162 }
163 return cachedPrototype;
164 }