]> git.saurik.com Git - apple/javascriptcore.git/blame - API/JSContextRef.cpp
JavaScriptCore-1097.3.3.tar.gz
[apple/javascriptcore.git] / API / JSContextRef.cpp
CommitLineData
b37bf2e1
A
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"
f9bf01c6 28#include "JSContextRefPrivate.h"
b37bf2e1
A
29
30#include "APICast.h"
9dae56ea 31#include "InitializeThreading.h"
14957cd0
A
32#include <interpreter/CallFrame.h>
33#include <interpreter/Interpreter.h>
b37bf2e1
A
34#include "JSCallbackObject.h"
35#include "JSClassRef.h"
36#include "JSGlobalObject.h"
9dae56ea 37#include "JSObject.h"
14957cd0 38#include "UStringBuilder.h"
4e4e5a6f 39#include <wtf/text/StringHash.h>
b37bf2e1 40
f9bf01c6 41#if OS(DARWIN)
9dae56ea
A
42#include <mach-o/dyld.h>
43
44static const int32_t webkitFirstVersionWithConcurrentGlobalContexts = 0x2100500; // 528.5.0
45#endif
46
47using namespace JSC;
48
6fe7ccc8
A
49// From the API's perspective, a context group remains alive iff
50// (a) it has been JSContextGroupRetained
51// OR
52// (b) one of its contexts has been JSContextRetained
53
9dae56ea
A
54JSContextGroupRef JSContextGroupCreate()
55{
56 initializeThreading();
14957cd0 57 return toRef(JSGlobalData::createContextGroup(ThreadStackTypeSmall).leakRef());
9dae56ea
A
58}
59
60JSContextGroupRef JSContextGroupRetain(JSContextGroupRef group)
61{
62 toJS(group)->ref();
63 return group;
64}
65
66void JSContextGroupRelease(JSContextGroupRef group)
67{
68 toJS(group)->deref();
69}
b37bf2e1 70
6fe7ccc8
A
71// From the API's perspective, a global context remains alive iff it has been JSGlobalContextRetained.
72
b37bf2e1
A
73JSGlobalContextRef JSGlobalContextCreate(JSClassRef globalObjectClass)
74{
9dae56ea 75 initializeThreading();
6fe7ccc8 76
f9bf01c6 77#if OS(DARWIN)
6fe7ccc8
A
78 // If the application was linked before JSGlobalContextCreate was changed to use a unique JSGlobalData,
79 // we use a shared one for backwards compatibility.
9dae56ea 80 if (NSVersionOfLinkTimeLibrary("JavaScriptCore") <= webkitFirstVersionWithConcurrentGlobalContexts) {
9dae56ea
A
81 return JSGlobalContextCreateInGroup(toRef(&JSGlobalData::sharedInstance()), globalObjectClass);
82 }
f9bf01c6 83#endif // OS(DARWIN)
9dae56ea
A
84
85 return JSGlobalContextCreateInGroup(0, globalObjectClass);
86}
87
88JSGlobalContextRef JSGlobalContextCreateInGroup(JSContextGroupRef group, JSClassRef globalObjectClass)
89{
90 initializeThreading();
91
4e4e5a6f 92 RefPtr<JSGlobalData> globalData = group ? PassRefPtr<JSGlobalData>(toJS(group)) : JSGlobalData::createContextGroup(ThreadStackTypeSmall);
9dae56ea 93
f9bf01c6 94 APIEntryShim entryShim(globalData.get(), false);
9dae56ea 95 globalData->makeUsableFromMultipleThreads();
b37bf2e1
A
96
97 if (!globalObjectClass) {
6fe7ccc8 98 JSGlobalObject* globalObject = JSGlobalObject::create(*globalData, JSGlobalObject::createStructure(*globalData, jsNull()));
b37bf2e1
A
99 return JSGlobalContextRetain(toGlobalRef(globalObject->globalExec()));
100 }
101
6fe7ccc8 102 JSGlobalObject* globalObject = JSCallbackObject<JSGlobalObject>::create(*globalData, globalObjectClass, JSCallbackObject<JSGlobalObject>::createStructure(*globalData, 0, jsNull()));
9dae56ea 103 ExecState* exec = globalObject->globalExec();
ba379fdc 104 JSValue prototype = globalObjectClass->prototype(exec);
b37bf2e1
A
105 if (!prototype)
106 prototype = jsNull();
14957cd0 107 globalObject->resetPrototype(*globalData, prototype);
9dae56ea 108 return JSGlobalContextRetain(toGlobalRef(exec));
b37bf2e1
A
109}
110
111JSGlobalContextRef JSGlobalContextRetain(JSGlobalContextRef ctx)
112{
b37bf2e1 113 ExecState* exec = toJS(ctx);
f9bf01c6 114 APIEntryShim entryShim(exec);
9dae56ea
A
115
116 JSGlobalData& globalData = exec->globalData();
b37bf2e1 117 gcProtect(exec->dynamicGlobalObject());
9dae56ea 118 globalData.ref();
b37bf2e1
A
119 return ctx;
120}
121
122void JSGlobalContextRelease(JSGlobalContextRef ctx)
123{
6fe7ccc8 124 IdentifierTable* savedIdentifierTable;
b37bf2e1 125 ExecState* exec = toJS(ctx);
6fe7ccc8
A
126 {
127 JSLockHolder lock(exec);
9dae56ea 128
6fe7ccc8
A
129 JSGlobalData& globalData = exec->globalData();
130 savedIdentifierTable = wtfThreadData().setCurrentIdentifierTable(globalData.identifierTable);
9dae56ea 131
6fe7ccc8
A
132 bool protectCountIsZero = Heap::heap(exec->dynamicGlobalObject())->unprotect(exec->dynamicGlobalObject());
133 if (protectCountIsZero)
134 globalData.heap.reportAbandonedObjectGraph();
135 globalData.deref();
136 }
f9bf01c6 137
4e4e5a6f 138 wtfThreadData().setCurrentIdentifierTable(savedIdentifierTable);
b37bf2e1
A
139}
140
141JSObjectRef JSContextGetGlobalObject(JSContextRef ctx)
142{
143 ExecState* exec = toJS(ctx);
f9bf01c6 144 APIEntryShim entryShim(exec);
9dae56ea
A
145
146 // It is necessary to call toThisObject to get the wrapper object when used with WebCore.
6fe7ccc8 147 return toRef(exec->lexicalGlobalObject()->methodTable()->toThisObject(exec->lexicalGlobalObject(), exec));
9dae56ea
A
148}
149
150JSContextGroupRef JSContextGetGroup(JSContextRef ctx)
151{
152 ExecState* exec = toJS(ctx);
153 return toRef(&exec->globalData());
b37bf2e1 154}
f9bf01c6
A
155
156JSGlobalContextRef JSContextGetGlobalContext(JSContextRef ctx)
157{
158 ExecState* exec = toJS(ctx);
159 APIEntryShim entryShim(exec);
160
161 return toGlobalRef(exec->lexicalGlobalObject()->globalExec());
162}
14957cd0
A
163
164JSStringRef JSContextCreateBacktrace(JSContextRef ctx, unsigned maxStackSize)
165{
166 ExecState* exec = toJS(ctx);
6fe7ccc8 167 JSLockHolder lock(exec);
14957cd0
A
168
169 unsigned count = 0;
170 UStringBuilder builder;
171 CallFrame* callFrame = exec;
172 UString functionName;
173 if (exec->callee()) {
174 if (asObject(exec->callee())->inherits(&InternalFunction::s_info)) {
175 functionName = asInternalFunction(exec->callee())->name(exec);
176 builder.append("#0 ");
177 builder.append(functionName);
178 builder.append("() ");
179 count++;
180 }
181 }
182 while (true) {
183 ASSERT(callFrame);
184 int signedLineNumber;
185 intptr_t sourceID;
186 UString urlString;
187 JSValue function;
188
189 UString levelStr = UString::number(count);
190
191 exec->interpreter()->retrieveLastCaller(callFrame, signedLineNumber, sourceID, urlString, function);
192
193 if (function)
6fe7ccc8 194 functionName = jsCast<JSFunction*>(function)->name(exec);
14957cd0
A
195 else {
196 // Caller is unknown, but if frame is empty we should still add the frame, because
197 // something called us, and gave us arguments.
198 if (count)
199 break;
200 }
201 unsigned lineNumber = signedLineNumber >= 0 ? signedLineNumber : 0;
202 if (!builder.isEmpty())
203 builder.append("\n");
204 builder.append("#");
205 builder.append(levelStr);
206 builder.append(" ");
207 builder.append(functionName);
208 builder.append("() at ");
209 builder.append(urlString);
210 builder.append(":");
211 builder.append(UString::number(lineNumber));
212 if (!function || ++count == maxStackSize)
213 break;
214 callFrame = callFrame->callerFrame();
215 }
216 return OpaqueJSString::create(builder.toUString()).leakRef();
217}
218
219