initial import
[vuplus_webkit] / Source / JavaScriptCore / API / JSContextRef.cpp
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 <interpreter/CallFrame.h>
33 #include <interpreter/Interpreter.h>
34 #include "JSCallbackObject.h"
35 #include "JSClassRef.h"
36 #include "JSGlobalObject.h"
37 #include "JSObject.h"
38 #include "UStringBuilder.h"
39 #include <wtf/text/StringHash.h>
40
41
42 #if OS(DARWIN)
43 #include <mach-o/dyld.h>
44
45 static const int32_t webkitFirstVersionWithConcurrentGlobalContexts = 0x2100500; // 528.5.0
46 #endif
47
48 using namespace JSC;
49
50 JSContextGroupRef JSContextGroupCreate()
51 {
52     initializeThreading();
53     return toRef(JSGlobalData::createContextGroup(ThreadStackTypeSmall).leakRef());
54 }
55
56 JSContextGroupRef JSContextGroupRetain(JSContextGroupRef group)
57 {
58     toJS(group)->ref();
59     return group;
60 }
61
62 void JSContextGroupRelease(JSContextGroupRef group)
63 {
64     toJS(group)->deref();
65 }
66
67 JSGlobalContextRef JSGlobalContextCreate(JSClassRef globalObjectClass)
68 {
69     initializeThreading();
70 #if OS(DARWIN)
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) {
75 #else
76     {
77 #endif
78         JSLock lock(LockForReal);
79         return JSGlobalContextCreateInGroup(toRef(&JSGlobalData::sharedInstance()), globalObjectClass);
80     }
81 #endif // OS(DARWIN)
82
83     return JSGlobalContextCreateInGroup(0, globalObjectClass);
84 }
85
86 JSGlobalContextRef JSGlobalContextCreateInGroup(JSContextGroupRef group, JSClassRef globalObjectClass)
87 {
88     initializeThreading();
89
90     JSLock lock(LockForReal);
91     RefPtr<JSGlobalData> globalData = group ? PassRefPtr<JSGlobalData>(toJS(group)) : JSGlobalData::createContextGroup(ThreadStackTypeSmall);
92
93     APIEntryShim entryShim(globalData.get(), false);
94
95 #if ENABLE(JSC_MULTIPLE_THREADS)
96     globalData->makeUsableFromMultipleThreads();
97 #endif
98
99     if (!globalObjectClass) {
100         JSGlobalObject* globalObject = JSGlobalObject::create(*globalData, JSGlobalObject::createStructure(*globalData, jsNull()));
101         return JSGlobalContextRetain(toGlobalRef(globalObject->globalExec()));
102     }
103
104     JSGlobalObject* globalObject = JSCallbackObject<JSGlobalObject>::create(*globalData, globalObjectClass, JSCallbackObject<JSGlobalObject>::createStructure(*globalData, 0, jsNull()));
105     ExecState* exec = globalObject->globalExec();
106     JSValue prototype = globalObjectClass->prototype(exec);
107     if (!prototype)
108         prototype = jsNull();
109     globalObject->resetPrototype(*globalData, prototype);
110     return JSGlobalContextRetain(toGlobalRef(exec));
111 }
112
113 JSGlobalContextRef JSGlobalContextRetain(JSGlobalContextRef ctx)
114 {
115     ExecState* exec = toJS(ctx);
116     APIEntryShim entryShim(exec);
117
118     JSGlobalData& globalData = exec->globalData();
119     gcProtect(exec->dynamicGlobalObject());
120     globalData.ref();
121     return ctx;
122 }
123
124 void JSGlobalContextRelease(JSGlobalContextRef ctx)
125 {
126     ExecState* exec = toJS(ctx);
127     JSLock lock(exec);
128
129     JSGlobalData& globalData = exec->globalData();
130     JSGlobalObject* dgo = exec->dynamicGlobalObject();
131     IdentifierTable* savedIdentifierTable = wtfThreadData().setCurrentIdentifierTable(globalData.identifierTable);
132
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);
139
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())();
155     }
156
157     globalData.deref();
158
159     wtfThreadData().setCurrentIdentifierTable(savedIdentifierTable);
160 }
161
162 JSObjectRef JSContextGetGlobalObject(JSContextRef ctx)
163 {
164     ExecState* exec = toJS(ctx);
165     APIEntryShim entryShim(exec);
166
167     // It is necessary to call toThisObject to get the wrapper object when used with WebCore.
168     return toRef(exec->lexicalGlobalObject()->toThisObject(exec));
169 }
170
171 JSContextGroupRef JSContextGetGroup(JSContextRef ctx)
172 {
173     ExecState* exec = toJS(ctx);
174     return toRef(&exec->globalData());
175 }
176
177 JSGlobalContextRef JSContextGetGlobalContext(JSContextRef ctx)
178 {
179     ExecState* exec = toJS(ctx);
180     APIEntryShim entryShim(exec);
181
182     return toGlobalRef(exec->lexicalGlobalObject()->globalExec());
183 }
184     
185 JSStringRef JSContextCreateBacktrace(JSContextRef ctx, unsigned maxStackSize)
186 {
187     ExecState* exec = toJS(ctx);
188     JSLock lock(exec);
189
190     unsigned count = 0;
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("() ");
200             count++;
201         }
202     }
203     while (true) {
204         ASSERT(callFrame);
205         int signedLineNumber;
206         intptr_t sourceID;
207         UString urlString;
208         JSValue function;
209         
210         UString levelStr = UString::number(count);
211         
212         exec->interpreter()->retrieveLastCaller(callFrame, signedLineNumber, sourceID, urlString, function);
213
214         if (function)
215             functionName = asFunction(function)->name(exec);
216         else {
217             // Caller is unknown, but if frame is empty we should still add the frame, because
218             // something called us, and gave us arguments.
219             if (count)
220                 break;
221         }
222         unsigned lineNumber = signedLineNumber >= 0 ? signedLineNumber : 0;
223         if (!builder.isEmpty())
224             builder.append("\n");
225         builder.append("#");
226         builder.append(levelStr);
227         builder.append(" ");
228         builder.append(functionName);
229         builder.append("() at ");
230         builder.append(urlString);
231         builder.append(":");
232         builder.append(UString::number(lineNumber));
233         if (!function || ++count == maxStackSize)
234             break;
235         callFrame = callFrame->callerFrame();
236     }
237     return OpaqueJSString::create(builder.toUString()).leakRef();
238 }
239
240