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
29 #include "APICast.h"
30 #include "InitializeThreading.h"
31 #include "JSCallbackObject.h"
32 #include "JSClassRef.h"
33 #include "JSGlobalObject.h"
34 #include "JSObject.h"
35 #include <wtf/Platform.h>
36
37 #if PLATFORM(DARWIN)
38 #include <mach-o/dyld.h>
39
40 static const int32_t webkitFirstVersionWithConcurrentGlobalContexts = 0x2100500; // 528.5.0
41 #endif
42
43 using namespace JSC;
44
JSContextGroupCreate()45 JSContextGroupRef JSContextGroupCreate()
46 {
47 initializeThreading();
48 return toRef(JSGlobalData::create().releaseRef());
49 }
50
JSContextGroupRetain(JSContextGroupRef group)51 JSContextGroupRef JSContextGroupRetain(JSContextGroupRef group)
52 {
53 toJS(group)->ref();
54 return group;
55 }
56
JSContextGroupRelease(JSContextGroupRef group)57 void JSContextGroupRelease(JSContextGroupRef group)
58 {
59 toJS(group)->deref();
60 }
61
JSGlobalContextCreate(JSClassRef globalObjectClass)62 JSGlobalContextRef JSGlobalContextCreate(JSClassRef globalObjectClass)
63 {
64 initializeThreading();
65 #if PLATFORM(DARWIN)
66 // When running on Tiger or Leopard, or if the application was linked before JSGlobalContextCreate was changed
67 // to use a unique JSGlobalData, we use a shared one for compatibility.
68 #if !defined(BUILDING_ON_TIGER) && !defined(BUILDING_ON_LEOPARD)
69 if (NSVersionOfLinkTimeLibrary("JavaScriptCore") <= webkitFirstVersionWithConcurrentGlobalContexts) {
70 #else
71 {
72 #endif
73 JSLock lock(LockForReal);
74 return JSGlobalContextCreateInGroup(toRef(&JSGlobalData::sharedInstance()), globalObjectClass);
75 }
76 #endif // PLATFORM(DARWIN)
77
78 return JSGlobalContextCreateInGroup(0, globalObjectClass);
79 }
80
81 JSGlobalContextRef JSGlobalContextCreateInGroup(JSContextGroupRef group, JSClassRef globalObjectClass)
82 {
83 initializeThreading();
84
85 JSLock lock(LockForReal);
86
87 RefPtr<JSGlobalData> globalData = group ? PassRefPtr<JSGlobalData>(toJS(group)) : JSGlobalData::create();
88
89 #if ENABLE(JSC_MULTIPLE_THREADS)
90 globalData->makeUsableFromMultipleThreads();
91 #endif
92
93 if (!globalObjectClass) {
94 JSGlobalObject* globalObject = new (globalData.get()) JSGlobalObject;
95 return JSGlobalContextRetain(toGlobalRef(globalObject->globalExec()));
96 }
97
98 JSGlobalObject* globalObject = new (globalData.get()) JSCallbackObject<JSGlobalObject>(globalObjectClass);
99 ExecState* exec = globalObject->globalExec();
100 JSValue prototype = globalObjectClass->prototype(exec);
101 if (!prototype)
102 prototype = jsNull();
103 globalObject->resetPrototype(prototype);
104 return JSGlobalContextRetain(toGlobalRef(exec));
105 }
106
107 JSGlobalContextRef JSGlobalContextRetain(JSGlobalContextRef ctx)
108 {
109 ExecState* exec = toJS(ctx);
110 JSLock lock(exec);
111
112 JSGlobalData& globalData = exec->globalData();
113
114 globalData.heap.registerThread();
115
116 gcProtect(exec->dynamicGlobalObject());
117 globalData.ref();
118 return ctx;
119 }
120
121 void JSGlobalContextRelease(JSGlobalContextRef ctx)
122 {
123 ExecState* exec = toJS(ctx);
124 JSLock lock(exec);
125
126 gcUnprotect(exec->dynamicGlobalObject());
127
128 JSGlobalData& globalData = exec->globalData();
129 if (globalData.refCount() == 2) { // One reference is held by JSGlobalObject, another added by JSGlobalContextRetain().
130 // The last reference was released, this is our last chance to collect.
131 ASSERT(!globalData.heap.protectedObjectCount());
132 ASSERT(!globalData.heap.isBusy());
133 globalData.heap.destroy();
134 } else
135 globalData.heap.collect();
136
137 globalData.deref();
138 }
139
140 JSObjectRef JSContextGetGlobalObject(JSContextRef ctx)
141 {
142 ExecState* exec = toJS(ctx);
143 exec->globalData().heap.registerThread();
144 JSLock lock(exec);
145
146 // It is necessary to call toThisObject to get the wrapper object when used with WebCore.
147 return toRef(exec->lexicalGlobalObject()->toThisObject(exec));
148 }
149
150 JSContextGroupRef JSContextGetGroup(JSContextRef ctx)
151 {
152 ExecState* exec = toJS(ctx);
153 return toRef(&exec->globalData());
154 }
155