source: webkit/trunk/JavaScriptCore/API/JSContextRef.cpp@ 60057

Last change on this file since 60057 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: 6.3 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 "JSContextRef.h"
28#include "JSContextRefPrivate.h"
29
30#include "APICast.h"
31#include "InitializeThreading.h"
32#include "JSCallbackObject.h"
33#include "JSClassRef.h"
34#include "JSGlobalObject.h"
35#include "JSObject.h"
36#include <wtf/text/StringHash.h>
37
38#if OS(DARWIN)
39#include <mach-o/dyld.h>
40
41static const int32_t webkitFirstVersionWithConcurrentGlobalContexts = 0x2100500; // 528.5.0
42#endif
43
44using namespace JSC;
45
46JSContextGroupRef JSContextGroupCreate()
47{
48 initializeThreading();
49 return toRef(JSGlobalData::createContextGroup(ThreadStackTypeSmall).releaseRef());
50}
51
52JSContextGroupRef JSContextGroupRetain(JSContextGroupRef group)
53{
54 toJS(group)->ref();
55 return group;
56}
57
58void JSContextGroupRelease(JSContextGroupRef group)
59{
60 toJS(group)->deref();
61}
62
63JSGlobalContextRef JSGlobalContextCreate(JSClassRef globalObjectClass)
64{
65 initializeThreading();
66#if OS(DARWIN)
67 // When running on Tiger or Leopard, or if the application was linked before JSGlobalContextCreate was changed
68 // to use a unique JSGlobalData, we use a shared one for compatibility.
69#if !defined(BUILDING_ON_TIGER) && !defined(BUILDING_ON_LEOPARD)
70 if (NSVersionOfLinkTimeLibrary("JavaScriptCore") <= webkitFirstVersionWithConcurrentGlobalContexts) {
71#else
72 {
73#endif
74 JSLock lock(LockForReal);
75 return JSGlobalContextCreateInGroup(toRef(&JSGlobalData::sharedInstance()), globalObjectClass);
76 }
77#endif // OS(DARWIN)
78
79 return JSGlobalContextCreateInGroup(0, globalObjectClass);
80}
81
82JSGlobalContextRef JSGlobalContextCreateInGroup(JSContextGroupRef group, JSClassRef globalObjectClass)
83{
84 initializeThreading();
85
86 JSLock lock(LockForReal);
87 RefPtr<JSGlobalData> globalData = group ? PassRefPtr<JSGlobalData>(toJS(group)) : JSGlobalData::createContextGroup(ThreadStackTypeSmall);
88
89 APIEntryShim entryShim(globalData.get(), false);
90
91#if ENABLE(JSC_MULTIPLE_THREADS)
92 globalData->makeUsableFromMultipleThreads();
93#endif
94
95 if (!globalObjectClass) {
96 JSGlobalObject* globalObject = new (globalData.get()) JSGlobalObject;
97 return JSGlobalContextRetain(toGlobalRef(globalObject->globalExec()));
98 }
99
100 JSGlobalObject* globalObject = new (globalData.get()) JSCallbackObject<JSGlobalObject>(globalObjectClass, JSCallbackObject<JSGlobalObject>::createStructure(jsNull()));
101 ExecState* exec = globalObject->globalExec();
102 JSValue prototype = globalObjectClass->prototype(exec);
103 if (!prototype)
104 prototype = jsNull();
105 globalObject->resetPrototype(prototype);
106 return JSGlobalContextRetain(toGlobalRef(exec));
107}
108
109JSGlobalContextRef JSGlobalContextRetain(JSGlobalContextRef ctx)
110{
111 ExecState* exec = toJS(ctx);
112 APIEntryShim entryShim(exec);
113
114 JSGlobalData& globalData = exec->globalData();
115 gcProtect(exec->dynamicGlobalObject());
116 globalData.ref();
117 return ctx;
118}
119
120void JSGlobalContextRelease(JSGlobalContextRef ctx)
121{
122 ExecState* exec = toJS(ctx);
123 JSLock lock(exec);
124
125 JSGlobalData& globalData = exec->globalData();
126 JSGlobalObject* dgo = exec->dynamicGlobalObject();
127 IdentifierTable* savedIdentifierTable = wtfThreadData().setCurrentIdentifierTable(globalData.identifierTable);
128
129 // One reference is held by JSGlobalObject, another added by JSGlobalContextRetain().
130 bool releasingContextGroup = globalData.refCount() == 2;
131 bool releasingGlobalObject = Heap::heap(dgo)->unprotect(dgo);
132 // If this is the last reference to a global data, it should also
133 // be the only remaining reference to the global object too!
134 ASSERT(!releasingContextGroup || releasingGlobalObject);
135
136 // An API 'JSGlobalContextRef' retains two things - a global object and a
137 // global data (or context group, in API terminology).
138 // * If this is the last reference to any contexts in the given context group,
139 // call destroy on the heap (the global data is being freed).
140 // * If this was the last reference to the global object, then unprotecting
141 // it may release a lot of GC memory - run the garbage collector now.
142 // * If there are more references remaining the the global object, then do nothing
143 // (specifically that is more protects, which we assume come from other JSGlobalContextRefs).
144 if (releasingContextGroup)
145 globalData.heap.destroy();
146 else if (releasingGlobalObject)
147 globalData.heap.collectAllGarbage();
148
149 globalData.deref();
150
151 wtfThreadData().setCurrentIdentifierTable(savedIdentifierTable);
152}
153
154JSObjectRef JSContextGetGlobalObject(JSContextRef ctx)
155{
156 ExecState* exec = toJS(ctx);
157 APIEntryShim entryShim(exec);
158
159 // It is necessary to call toThisObject to get the wrapper object when used with WebCore.
160 return toRef(exec->lexicalGlobalObject()->toThisObject(exec));
161}
162
163JSContextGroupRef JSContextGetGroup(JSContextRef ctx)
164{
165 ExecState* exec = toJS(ctx);
166 return toRef(&exec->globalData());
167}
168
169JSGlobalContextRef JSContextGetGlobalContext(JSContextRef ctx)
170{
171 ExecState* exec = toJS(ctx);
172 APIEntryShim entryShim(exec);
173
174 return toGlobalRef(exec->lexicalGlobalObject()->globalExec());
175}
Note: See TracBrowser for help on using the repository browser.