source: webkit/trunk/JavaScriptCore/API/JSClassRef.cpp@ 64320

Last change on this file since 64320 was 59941, checked in by [email protected], 15 years ago

2010-05-21 Oliver Hunt <[email protected]>

Reviewed by Geoffrey Garen.

All callable objects should have a global object reference
https://bugs.webkit.org/show_bug.cgi?id=39495

All objects that may ever return a value other CallTypeNone
or ConstructTypeNone now get a global object in their constructor
and store that in their first anonymous slot. We add a new type
JSObjectWithGlobalObject to allow us to share this logic as much
as possible, however some objects have specific inheritance
requirements so we can't just use it universally.

To enforce this requirement JSValue::getCallData and getConstructData
make use of a new "isValidCallee" function to assert that any object
that returns a value other than CallType/ConstructTypeNone has a
global object in anonymous slot 0.

In order to ensure that static function slots are converted into
function objects with the correct global object, all prototype objects
and other classes with static function slots also gain a global object
reference. Happily this fixes the long standing issue where host
function objects get a prototype from the lexical global object of the
first function that calls them, instead of the global object that they
are defined on.

  • API/JSCallbackConstructor.cpp: (JSC::JSCallbackConstructor::JSCallbackConstructor):
  • API/JSCallbackConstructor.h:
  • API/JSCallbackFunction.cpp: (JSC::JSCallbackFunction::JSCallbackFunction):
  • API/JSCallbackFunction.h:
  • API/JSCallbackObject.cpp: (JSC::):
  • API/JSCallbackObject.h:
  • API/JSCallbackObjectFunctions.h: (JSC::::JSCallbackObject): (JSC::::staticFunctionGetter):
  • API/JSClassRef.cpp: (OpaqueJSClass::prototype):
  • API/JSContextRef.cpp:
  • API/JSObjectRef.cpp: (JSObjectMake): (JSObjectMakeFunctionWithCallback): (JSObjectMakeConstructor): (JSObjectGetPrivate): (JSObjectSetPrivate): (JSObjectGetPrivateProperty): (JSObjectSetPrivateProperty): (JSObjectDeletePrivateProperty):
  • API/JSValueRef.cpp: (JSValueIsObjectOfClass):
  • API/JSWeakObjectMapRefPrivate.cpp:
  • CMakeLists.txt:
  • GNUmakefile.am:
  • JavaScriptCore.exp:
  • JavaScriptCore.gypi:
  • JavaScriptCore.vcproj/JavaScriptCore/JavaScriptCore.vcproj:
  • JavaScriptCore.xcodeproj/project.pbxproj:
  • interpreter/Interpreter.cpp: (JSC::Interpreter::privateExecute):
  • jit/JITStubs.cpp: (JSC::DEFINE_STUB_FUNCTION):
  • jsc.cpp: (GlobalObject::GlobalObject):
  • runtime/ArrayConstructor.cpp: (JSC::ArrayConstructor::ArrayConstructor):
  • runtime/ArrayConstructor.h:
  • runtime/ArrayPrototype.cpp: (JSC::ArrayPrototype::ArrayPrototype):
  • runtime/ArrayPrototype.h: (JSC::ArrayPrototype::createStructure):
  • runtime/BooleanConstructor.cpp: (JSC::BooleanConstructor::BooleanConstructor):
  • runtime/BooleanConstructor.h:
  • runtime/BooleanPrototype.cpp: (JSC::BooleanPrototype::BooleanPrototype):
  • runtime/BooleanPrototype.h:
  • runtime/DateConstructor.cpp: (JSC::DateConstructor::DateConstructor):
  • runtime/DateConstructor.h:
  • runtime/DatePrototype.cpp: (JSC::DatePrototype::DatePrototype):
  • runtime/DatePrototype.h:
  • runtime/ErrorConstructor.cpp: (JSC::ErrorConstructor::ErrorConstructor):
  • runtime/ErrorConstructor.h:
  • runtime/ErrorPrototype.cpp: (JSC::ErrorPrototype::ErrorPrototype):
  • runtime/ErrorPrototype.h:
  • runtime/FunctionConstructor.cpp: (JSC::FunctionConstructor::FunctionConstructor):
  • runtime/FunctionConstructor.h:
  • runtime/FunctionPrototype.cpp: (JSC::FunctionPrototype::FunctionPrototype): (JSC::FunctionPrototype::addFunctionProperties):
  • runtime/FunctionPrototype.h:
  • runtime/GlobalEvalFunction.cpp: (JSC::GlobalEvalFunction::GlobalEvalFunction):
  • runtime/GlobalEvalFunction.h:
  • runtime/InternalFunction.cpp: (JSC::InternalFunction::InternalFunction):
  • runtime/InternalFunction.h:
  • runtime/JSCell.h: (JSC::JSValue::getCallData): (JSC::JSValue::getConstructData):
  • runtime/JSFunction.cpp: (JSC::JSFunction::JSFunction):
  • runtime/JSFunction.h:
  • runtime/JSGlobalObject.cpp: (JSC::JSGlobalObject::reset):
  • runtime/JSGlobalObject.h: (JSC::JSGlobalObject::JSGlobalObject):
  • runtime/JSONObject.cpp: (JSC::JSONObject::JSONObject):
  • runtime/JSONObject.h:
  • runtime/JSObject.h:
  • runtime/JSObjectWithGlobalObject.cpp: Added. (JSC::JSObjectWithGlobalObject::JSObjectWithGlobalObject):
  • runtime/JSObjectWithGlobalObject.h: Added. (JSC::JSObjectWithGlobalObject::createStructure): (JSC::JSObjectWithGlobalObject::JSObjectWithGlobalObject):
  • runtime/JSValue.cpp: (JSC::JSValue::isValidCallee):
  • runtime/JSValue.h:
  • runtime/Lookup.cpp: (JSC::setUpStaticFunctionSlot):
  • runtime/MathObject.cpp: (JSC::MathObject::MathObject):
  • runtime/MathObject.h:
  • runtime/NativeErrorConstructor.cpp: (JSC::NativeErrorConstructor::NativeErrorConstructor):
  • runtime/NativeErrorConstructor.h:
  • runtime/NativeErrorPrototype.cpp: (JSC::NativeErrorPrototype::NativeErrorPrototype):
  • runtime/NativeErrorPrototype.h:
  • runtime/NumberConstructor.cpp: (JSC::NumberConstructor::NumberConstructor):
  • runtime/NumberConstructor.h:
  • runtime/NumberPrototype.cpp: (JSC::NumberPrototype::NumberPrototype):
  • runtime/NumberPrototype.h:
  • runtime/ObjectConstructor.cpp: (JSC::ObjectConstructor::ObjectConstructor):
  • runtime/ObjectConstructor.h:
  • runtime/ObjectPrototype.cpp: (JSC::ObjectPrototype::ObjectPrototype):
  • runtime/ObjectPrototype.h:
  • runtime/PrototypeFunction.cpp: (JSC::PrototypeFunction::PrototypeFunction):
  • runtime/PrototypeFunction.h:
  • runtime/RegExpConstructor.cpp: (JSC::RegExpConstructor::RegExpConstructor): (JSC::constructRegExp):
  • runtime/RegExpConstructor.h:
  • runtime/RegExpObject.cpp: (JSC::RegExpObject::RegExpObject):
  • runtime/RegExpObject.h:
  • runtime/RegExpPrototype.cpp: (JSC::RegExpPrototype::RegExpPrototype):
  • runtime/RegExpPrototype.h:
  • runtime/StringConstructor.cpp: (JSC::StringConstructor::StringConstructor):
  • runtime/StringConstructor.h:
  • runtime/StringPrototype.cpp: (JSC::StringPrototype::StringPrototype):
  • runtime/StringPrototype.h:

2010-05-21 Oliver Hunt <[email protected]>

Reviewed by Geoffrey Garen.

All callable objects should have a global object reference
https://bugs.webkit.org/show_bug.cgi?id=39495

Update expected results as we now give all function objects
get their prototypes from the correct global object.

  • fast/dom/prototype-inheritance-expected.txt:

2010-05-21 Oliver Hunt <[email protected]>

Reviewed by Geoffrey Garen.

All callable objects should have a global object reference
https://bugs.webkit.org/show_bug.cgi?id=39495

Update the bindings generator to give prototype objects a
global object. Update all the manually written JSObject
subclasses to pass a global object.

  • ForwardingHeaders/runtime/JSObjectWithGlobalObject.h: Added.
  • WebCore.PluginHostProcess.exp:
  • bindings/js/JSDOMBinding.cpp: (WebCore::objectToStringFunctionGetter):
  • bindings/js/JSDOMWindowCustom.cpp: (WebCore::nonCachingStaticFunctionGetter):
  • bindings/js/JSDOMWindowShell.cpp: (WebCore::JSDOMWindowShell::setWindow):
  • bindings/js/JSHistoryCustom.cpp: (WebCore::nonCachingStaticBackFunctionGetter): (WebCore::nonCachingStaticForwardFunctionGetter): (WebCore::nonCachingStaticGoFunctionGetter):
  • bindings/js/JSLocationCustom.cpp: (WebCore::nonCachingStaticReplaceFunctionGetter): (WebCore::nonCachingStaticReloadFunctionGetter): (WebCore::nonCachingStaticAssignFunctionGetter):
  • bindings/js/WorkerScriptController.cpp: (WebCore::WorkerScriptController::initScript):
  • bindings/scripts/CodeGeneratorJS.pm:
  • bridge/c/CRuntimeObject.cpp: (JSC::Bindings::CRuntimeObject::CRuntimeObject):
  • bridge/c/CRuntimeObject.h:
  • bridge/c/c_instance.cpp: (JSC::Bindings::CInstance::newRuntimeObject): (JSC::Bindings::CRuntimeMethod::CRuntimeMethod): (JSC::Bindings::CInstance::getMethod):
  • bridge/jni/jsc/JavaInstanceJSC.cpp: (JavaInstance::newRuntimeObject): (JavaRuntimeMethod::JavaRuntimeMethod): (JavaInstance::getMethod):
  • bridge/jni/jsc/JavaRuntimeObject.cpp: (JSC::Bindings::JavaRuntimeObject::JavaRuntimeObject):
  • bridge/jni/jsc/JavaRuntimeObject.h:
  • bridge/jsc/BridgeJSC.cpp: (JSC::Bindings::Instance::newRuntimeObject):
  • bridge/objc/ObjCRuntimeObject.h:
  • bridge/objc/ObjCRuntimeObject.mm: (JSC::Bindings::ObjCRuntimeObject::ObjCRuntimeObject):
  • bridge/objc/objc_class.mm: (JSC::Bindings::ObjcClass::fallbackObject):
  • bridge/objc/objc_instance.mm: (ObjcInstance::newRuntimeObject): (ObjCRuntimeMethod::ObjCRuntimeMethod): (ObjcInstance::getMethod):
  • bridge/objc/objc_runtime.h:
  • bridge/objc/objc_runtime.mm: (JSC::Bindings::ObjcFallbackObjectImp::ObjcFallbackObjectImp):
  • bridge/runtime_method.cpp: (JSC::RuntimeMethod::RuntimeMethod):
  • bridge/runtime_method.h:
  • bridge/runtime_object.cpp: (JSC::Bindings::RuntimeObject::RuntimeObject):
  • bridge/runtime_object.h:

2010-05-21 Oliver Hunt <[email protected]>

Reviewed by Geoffrey Garen.

All callable objects should have a global object reference
https://bugs.webkit.org/show_bug.cgi?id=39495

Update the plugin proxy to handle the need for global object.

  • Plugins/Hosted/ProxyInstance.mm: (WebKit::ProxyInstance::newRuntimeObject): (WebKit::ProxyRuntimeMethod::ProxyRuntimeMethod): (WebKit::ProxyInstance::getMethod):
  • Plugins/Hosted/ProxyRuntimeObject.h:
  • Plugins/Hosted/ProxyRuntimeObject.mm: (WebKit::ProxyRuntimeObject::ProxyRuntimeObject):
  • Property svn:eol-style set to native
File size: 10.8 KB
Line 
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 *
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.
24 */
25
26#include "config.h"
27#include "JSClassRef.h"
28
29#include "APICast.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>
38
39using namespace std;
40using namespace JSC;
41using namespace WTF::Unicode;
42
43const JSClassDefinition kJSClassDefinitionEmpty = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
44
45static inline UString tryCreateStringFromUTF8(const char* string)
46{
47 if (!string)
48 return UString::null();
49
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))
54 return UString::null();
55
56 return UString(buffer.data(), p - buffer.data());
57}
58
59OpaqueJSClass::OpaqueJSClass(const JSClassDefinition* definition, OpaqueJSClass* protoClass)
60 : parentClass(definition->parentClass)
61 , prototypeClass(0)
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))
74 , m_staticValues(0)
75 , m_staticFunctions(0)
76{
77 initializeThreading();
78
79 if (const JSStaticValue* staticValue = definition->staticValues) {
80 m_staticValues = new OpaqueJSClassStaticValuesTable();
81 while (staticValue->name) {
82 UString valueName = tryCreateStringFromUTF8(staticValue->name);
83 if (!valueName.isNull()) {
84 // Use a local variable here to sidestep an RVCT compiler bug.
85 StaticValueEntry* entry = new StaticValueEntry(staticValue->getProperty, staticValue->setProperty, staticValue->attributes);
86 UStringImpl* impl = valueName.rep();
87 impl->ref();
88 m_staticValues->add(impl, entry);
89 }
90 ++staticValue;
91 }
92 }
93
94 if (const JSStaticFunction* staticFunction = definition->staticFunctions) {
95 m_staticFunctions = new OpaqueJSClassStaticFunctionsTable();
96 while (staticFunction->name) {
97 UString functionName = tryCreateStringFromUTF8(staticFunction->name);
98 if (!functionName.isNull()) {
99 // Use a local variable here to sidestep an RVCT compiler bug.
100 StaticFunctionEntry* entry = new StaticFunctionEntry(staticFunction->callAsFunction, staticFunction->attributes);
101 UStringImpl* impl = functionName.rep();
102 impl->ref();
103 m_staticFunctions->add(impl, entry);
104 }
105 ++staticFunction;
106 }
107 }
108
109 if (protoClass)
110 prototypeClass = JSClassRetain(protoClass);
111}
112
113OpaqueJSClass::~OpaqueJSClass()
114{
115 // The empty string is shared across threads & is an identifier, in all other cases we should have done a deep copy in className(), below.
116 ASSERT(!m_className.size() || !m_className.rep()->isIdentifier());
117
118 if (m_staticValues) {
119 OpaqueJSClassStaticValuesTable::const_iterator end = m_staticValues->end();
120 for (OpaqueJSClassStaticValuesTable::const_iterator it = m_staticValues->begin(); it != end; ++it) {
121 ASSERT(!it->first->isIdentifier());
122 delete it->second;
123 }
124 delete m_staticValues;
125 }
126
127 if (m_staticFunctions) {
128 OpaqueJSClassStaticFunctionsTable::const_iterator end = m_staticFunctions->end();
129 for (OpaqueJSClassStaticFunctionsTable::const_iterator it = m_staticFunctions->begin(); it != end; ++it) {
130 ASSERT(!it->first->isIdentifier());
131 delete it->second;
132 }
133 delete m_staticFunctions;
134 }
135
136 if (prototypeClass)
137 JSClassRelease(prototypeClass);
138}
139
140PassRefPtr<OpaqueJSClass> OpaqueJSClass::createNoAutomaticPrototype(const JSClassDefinition* definition)
141{
142 return adoptRef(new OpaqueJSClass(definition, 0));
143}
144
145static void clearReferenceToPrototype(JSObjectRef prototype)
146{
147 OpaqueJSClassContextData* jsClassData = static_cast<OpaqueJSClassContextData*>(JSObjectGetPrivate(prototype));
148 ASSERT(jsClassData);
149 jsClassData->cachedPrototype.clear(toJS(prototype));
150}
151
152PassRefPtr<OpaqueJSClass> OpaqueJSClass::create(const JSClassDefinition* clientDefinition)
153{
154 JSClassDefinition definition = *clientDefinition; // Avoid modifying client copy.
155
156 JSClassDefinition protoDefinition = kJSClassDefinitionEmpty;
157 protoDefinition.finalize = clearReferenceToPrototype;
158 swap(definition.staticFunctions, protoDefinition.staticFunctions); // Move static functions to the prototype.
159
160 // We are supposed to use JSClassRetain/Release but since we know that we currently have
161 // the only reference to this class object we cheat and use a RefPtr instead.
162 RefPtr<OpaqueJSClass> protoClass = adoptRef(new OpaqueJSClass(&protoDefinition, 0));
163 return adoptRef(new OpaqueJSClass(&definition, protoClass.get()));
164}
165
166OpaqueJSClassContextData::OpaqueJSClassContextData(OpaqueJSClass* jsClass)
167 : m_class(jsClass)
168{
169 if (jsClass->m_staticValues) {
170 staticValues = new OpaqueJSClassStaticValuesTable;
171 OpaqueJSClassStaticValuesTable::const_iterator end = jsClass->m_staticValues->end();
172 for (OpaqueJSClassStaticValuesTable::const_iterator it = jsClass->m_staticValues->begin(); it != end; ++it) {
173 ASSERT(!it->first->isIdentifier());
174 // Use a local variable here to sidestep an RVCT compiler bug.
175 StaticValueEntry* entry = new StaticValueEntry(it->second->getProperty, it->second->setProperty, it->second->attributes);
176 staticValues->add(UString::Rep::create(it->first->characters(), it->first->length()), entry);
177 }
178 } else
179 staticValues = 0;
180
181 if (jsClass->m_staticFunctions) {
182 staticFunctions = new OpaqueJSClassStaticFunctionsTable;
183 OpaqueJSClassStaticFunctionsTable::const_iterator end = jsClass->m_staticFunctions->end();
184 for (OpaqueJSClassStaticFunctionsTable::const_iterator it = jsClass->m_staticFunctions->begin(); it != end; ++it) {
185 ASSERT(!it->first->isIdentifier());
186 // Use a local variable here to sidestep an RVCT compiler bug.
187 StaticFunctionEntry* entry = new StaticFunctionEntry(it->second->callAsFunction, it->second->attributes);
188 staticFunctions->add(UString::Rep::create(it->first->characters(), it->first->length()), entry);
189 }
190
191 } else
192 staticFunctions = 0;
193}
194
195OpaqueJSClassContextData::~OpaqueJSClassContextData()
196{
197 if (staticValues) {
198 deleteAllValues(*staticValues);
199 delete staticValues;
200 }
201
202 if (staticFunctions) {
203 deleteAllValues(*staticFunctions);
204 delete staticFunctions;
205 }
206}
207
208OpaqueJSClassContextData& OpaqueJSClass::contextData(ExecState* exec)
209{
210 OpaqueJSClassContextData*& contextData = exec->globalData().opaqueJSClassData.add(this, 0).first->second;
211 if (!contextData)
212 contextData = new OpaqueJSClassContextData(this);
213 return *contextData;
214}
215
216UString OpaqueJSClass::className()
217{
218 // Make a deep copy, so that the caller has no chance to put the original into IdentifierTable.
219 return UString(m_className.data(), m_className.size());
220}
221
222OpaqueJSClassStaticValuesTable* OpaqueJSClass::staticValues(JSC::ExecState* exec)
223{
224 OpaqueJSClassContextData& jsClassData = contextData(exec);
225 return jsClassData.staticValues;
226}
227
228OpaqueJSClassStaticFunctionsTable* OpaqueJSClass::staticFunctions(JSC::ExecState* exec)
229{
230 OpaqueJSClassContextData& jsClassData = contextData(exec);
231 return jsClassData.staticFunctions;
232}
233
234/*!
235// Doc here in case we make this public. (Hopefully we won't.)
236@function
237 @abstract Returns the prototype that will be used when constructing an object with a given class.
238 @param ctx The execution context to use.
239 @param jsClass A JSClass whose prototype you want to get.
240 @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.
241*/
242JSObject* OpaqueJSClass::prototype(ExecState* exec)
243{
244 /* Class (C++) and prototype (JS) inheritance are parallel, so:
245 * (C++) | (JS)
246 * ParentClass | ParentClassPrototype
247 * ^ | ^
248 * | | |
249 * DerivedClass | DerivedClassPrototype
250 */
251
252 if (!prototypeClass)
253 return 0;
254
255 OpaqueJSClassContextData& jsClassData = contextData(exec);
256
257 if (!jsClassData.cachedPrototype) {
258 // Recursive, but should be good enough for our purposes
259 jsClassData.cachedPrototype = new (exec) JSCallbackObject<JSObjectWithGlobalObject>(exec, exec->lexicalGlobalObject(), exec->lexicalGlobalObject()->callbackObjectStructure(), prototypeClass, &jsClassData); // set jsClassData as the object's private data, so it can clear our reference on destruction
260 if (parentClass) {
261 if (JSObject* prototype = parentClass->prototype(exec))
262 jsClassData.cachedPrototype->setPrototype(prototype);
263 }
264 }
265 return jsClassData.cachedPrototype.get();
266}
Note: See TracBrowser for help on using the repository browser.