2 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "JSContextRef.h"
28 #include "JSContextRefPrivate.h"
31 #include "InitializeThreading.h"
32 #include <interpreter/CallFrame.h>
33 #include <interpreter/Interpreter.h>
34 #include "JSCallbackObject.h"
35 #include "JSClassRef.h"
36 #include "JSGlobalObject.h"
38 #include "UStringBuilder.h"
39 #include <wtf/text/StringHash.h>
43 #include <mach-o/dyld.h>
45 static const int32_t webkitFirstVersionWithConcurrentGlobalContexts
= 0x2100500; // 528.5.0
50 JSContextGroupRef
JSContextGroupCreate()
52 initializeThreading();
53 return toRef(JSGlobalData::createContextGroup(ThreadStackTypeSmall
).leakRef());
56 JSContextGroupRef
JSContextGroupRetain(JSContextGroupRef group
)
62 void JSContextGroupRelease(JSContextGroupRef group
)
67 JSGlobalContextRef
JSGlobalContextCreate(JSClassRef globalObjectClass
)
69 initializeThreading();
71 // When running on Tiger or Leopard, or if the application was linked before JSGlobalContextCreate was changed
72 // to use a unique JSGlobalData, we use a shared one for compatibility.
73 #ifndef BUILDING_ON_LEOPARD
74 if (NSVersionOfLinkTimeLibrary("JavaScriptCore") <= webkitFirstVersionWithConcurrentGlobalContexts
) {
78 JSLock
lock(LockForReal
);
79 return JSGlobalContextCreateInGroup(toRef(&JSGlobalData::sharedInstance()), globalObjectClass
);
83 return JSGlobalContextCreateInGroup(0, globalObjectClass
);
86 JSGlobalContextRef
JSGlobalContextCreateInGroup(JSContextGroupRef group
, JSClassRef globalObjectClass
)
88 initializeThreading();
90 JSLock
lock(LockForReal
);
91 RefPtr
<JSGlobalData
> globalData
= group
? PassRefPtr
<JSGlobalData
>(toJS(group
)) : JSGlobalData::createContextGroup(ThreadStackTypeSmall
);
93 APIEntryShim
entryShim(globalData
.get(), false);
95 #if ENABLE(JSC_MULTIPLE_THREADS)
96 globalData
->makeUsableFromMultipleThreads();
99 if (!globalObjectClass
) {
100 JSGlobalObject
* globalObject
= new (globalData
.get()) JSGlobalObject(*globalData
, JSGlobalObject::createStructure(*globalData
, jsNull()));
101 return JSGlobalContextRetain(toGlobalRef(globalObject
->globalExec()));
104 JSGlobalObject
* globalObject
= new (globalData
.get()) JSCallbackObject
<JSGlobalObject
>(*globalData
, globalObjectClass
, JSCallbackObject
<JSGlobalObject
>::createStructure(*globalData
, jsNull()));
105 ExecState
* exec
= globalObject
->globalExec();
106 JSValue prototype
= globalObjectClass
->prototype(exec
);
108 prototype
= jsNull();
109 globalObject
->resetPrototype(*globalData
, prototype
);
110 return JSGlobalContextRetain(toGlobalRef(exec
));
113 JSGlobalContextRef
JSGlobalContextRetain(JSGlobalContextRef ctx
)
115 ExecState
* exec
= toJS(ctx
);
116 APIEntryShim
entryShim(exec
);
118 JSGlobalData
& globalData
= exec
->globalData();
119 gcProtect(exec
->dynamicGlobalObject());
124 void JSGlobalContextRelease(JSGlobalContextRef ctx
)
126 ExecState
* exec
= toJS(ctx
);
129 JSGlobalData
& globalData
= exec
->globalData();
130 JSGlobalObject
* dgo
= exec
->dynamicGlobalObject();
131 IdentifierTable
* savedIdentifierTable
= wtfThreadData().setCurrentIdentifierTable(globalData
.identifierTable
);
133 // One reference is held by JSGlobalObject, another added by JSGlobalContextRetain().
134 bool releasingContextGroup
= globalData
.refCount() == 2;
135 bool releasingGlobalObject
= Heap::heap(dgo
)->unprotect(dgo
);
136 // If this is the last reference to a global data, it should also
137 // be the only remaining reference to the global object too!
138 ASSERT(!releasingContextGroup
|| releasingGlobalObject
);
140 // An API 'JSGlobalContextRef' retains two things - a global object and a
141 // global data (or context group, in API terminology).
142 // * If this is the last reference to any contexts in the given context group,
143 // call destroy on the heap (the global data is being freed).
144 // * If this was the last reference to the global object, then unprotecting
145 // it may release a lot of GC memory - tickle the activity callback to
146 // garbage collect soon.
147 // * If there are more references remaining the the global object, then do nothing
148 // (specifically that is more protects, which we assume come from other JSGlobalContextRefs).
149 if (releasingContextGroup
) {
150 globalData
.clearBuiltinStructures();
151 globalData
.heap
.destroy();
152 } else if (releasingGlobalObject
) {
153 globalData
.heap
.activityCallback()->synchronize();
154 (*globalData
.heap
.activityCallback())();
159 wtfThreadData().setCurrentIdentifierTable(savedIdentifierTable
);
162 JSObjectRef
JSContextGetGlobalObject(JSContextRef ctx
)
164 ExecState
* exec
= toJS(ctx
);
165 APIEntryShim
entryShim(exec
);
167 // It is necessary to call toThisObject to get the wrapper object when used with WebCore.
168 return toRef(exec
->lexicalGlobalObject()->toThisObject(exec
));
171 JSContextGroupRef
JSContextGetGroup(JSContextRef ctx
)
173 ExecState
* exec
= toJS(ctx
);
174 return toRef(&exec
->globalData());
177 JSGlobalContextRef
JSContextGetGlobalContext(JSContextRef ctx
)
179 ExecState
* exec
= toJS(ctx
);
180 APIEntryShim
entryShim(exec
);
182 return toGlobalRef(exec
->lexicalGlobalObject()->globalExec());
185 JSStringRef
JSContextCreateBacktrace(JSContextRef ctx
, unsigned maxStackSize
)
187 ExecState
* exec
= toJS(ctx
);
191 UStringBuilder builder
;
192 CallFrame
* callFrame
= exec
;
193 UString functionName
;
194 if (exec
->callee()) {
195 if (asObject(exec
->callee())->inherits(&InternalFunction::s_info
)) {
196 functionName
= asInternalFunction(exec
->callee())->name(exec
);
197 builder
.append("#0 ");
198 builder
.append(functionName
);
199 builder
.append("() ");
205 int signedLineNumber
;
210 UString levelStr
= UString::number(count
);
212 exec
->interpreter()->retrieveLastCaller(callFrame
, signedLineNumber
, sourceID
, urlString
, function
);
215 functionName
= asFunction(function
)->name(exec
);
217 // Caller is unknown, but if frame is empty we should still add the frame, because
218 // something called us, and gave us arguments.
222 unsigned lineNumber
= signedLineNumber
>= 0 ? signedLineNumber
: 0;
223 if (!builder
.isEmpty())
224 builder
.append("\n");
226 builder
.append(levelStr
);
228 builder
.append(functionName
);
229 builder
.append("() at ");
230 builder
.append(urlString
);
232 builder
.append(UString::number(lineNumber
));
233 if (!function
|| ++count
== maxStackSize
)
235 callFrame
= callFrame
->callerFrame();
237 return OpaqueJSString::create(builder
.toUString()).leakRef();