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

Last change on this file since 37868 was 37868, checked in by [email protected], 17 years ago

Suggested and rubber-stamped by Geoff Garen.

Fix a crash when opening Font Picker.

The change also hopefully fixes this bug, which I could never reproduce:
https://bugs.webkit.org/show_bug.cgi?id=20241
<rdar://problem/6290576> Safari crashes at JSValueUnprotect() when fontpicker view close

  • API/JSContextRef.cpp: (JSContextGetGlobalObject): Use lexical global object instead of dynamic one.
  • Property svn:eol-style set to native
File size: 4.2 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
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
37using namespace JSC;
38
39JSContextGroupRef JSContextGroupCreate()
40{
41 return toRef(JSGlobalData::create().releaseRef());
42}
43
44JSContextGroupRef JSContextGroupRetain(JSContextGroupRef group)
45{
46 toJS(group)->ref();
47 return group;
48}
49
50void JSContextGroupRelease(JSContextGroupRef group)
51{
52 toJS(group)->deref();
53}
54
55JSGlobalContextRef JSGlobalContextCreate(JSClassRef globalObjectClass)
56{
57 JSLock lock(true);
58 return JSGlobalContextCreateInGroup(toRef(&JSGlobalData::sharedInstance()), globalObjectClass);
59}
60
61JSGlobalContextRef JSGlobalContextCreateInGroup(JSContextGroupRef group, JSClassRef globalObjectClass)
62{
63 initializeThreading();
64
65 JSLock lock(true);
66
67 RefPtr<JSGlobalData> globalData = group ? PassRefPtr<JSGlobalData>(toJS(group)) : JSGlobalData::create();
68
69 if (!globalObjectClass) {
70 JSGlobalObject* globalObject = new (globalData.get()) JSGlobalObject;
71 return JSGlobalContextRetain(toGlobalRef(globalObject->globalExec()));
72 }
73
74 JSGlobalObject* globalObject = new (globalData.get()) JSCallbackObject<JSGlobalObject>(globalObjectClass);
75 ExecState* exec = globalObject->globalExec();
76 JSValue* prototype = globalObjectClass->prototype(exec);
77 if (!prototype)
78 prototype = jsNull();
79 globalObject->resetPrototype(prototype);
80 return JSGlobalContextRetain(toGlobalRef(exec));
81}
82
83JSGlobalContextRef JSGlobalContextRetain(JSGlobalContextRef ctx)
84{
85 ExecState* exec = toJS(ctx);
86 JSLock lock(exec);
87
88 JSGlobalData& globalData = exec->globalData();
89
90 globalData.heap.registerThread();
91
92 gcProtect(exec->dynamicGlobalObject());
93 globalData.ref();
94 return ctx;
95}
96
97void JSGlobalContextRelease(JSGlobalContextRef ctx)
98{
99 ExecState* exec = toJS(ctx);
100 JSLock lock(exec);
101
102 gcUnprotect(exec->dynamicGlobalObject());
103
104 JSGlobalData& globalData = exec->globalData();
105 if (globalData.refCount() == 2) { // One reference is held by JSGlobalObject, another added by JSGlobalContextRetain().
106 // The last reference was released, this is our last chance to collect.
107 ASSERT(!globalData.heap.protectedObjectCount());
108 ASSERT(!globalData.heap.isBusy());
109 globalData.heap.destroy();
110 } else
111 globalData.heap.collect();
112
113 globalData.deref();
114}
115
116JSObjectRef JSContextGetGlobalObject(JSContextRef ctx)
117{
118 ExecState* exec = toJS(ctx);
119 exec->globalData().heap.registerThread();
120 JSLock lock(exec);
121
122 // It is necessary to call toThisObject to get the wrapper object when used with WebCore.
123 return toRef(exec->lexicalGlobalObject()->toThisObject(exec));
124}
125
126JSContextGroupRef JSContextGetGroup(JSContextRef ctx)
127{
128 ExecState* exec = toJS(ctx);
129 return toRef(&exec->globalData());
130}
Note: See TracBrowser for help on using the repository browser.