OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2009 Google 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 INC. AND ITS CONTRIBUTORS ``AS IS'' | |
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | |
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS | |
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | |
23 * THE POSSIBILITY OF SUCH DAMAGE. | |
24 */ | |
25 | |
26 #include "config.h" | |
27 #include "bindings/v8/V8PerIsolateData.h" | |
28 | |
29 #include "bindings/v8/DOMDataStore.h" | |
30 #include "bindings/v8/PageScriptDebugServer.h" | |
31 #include "bindings/v8/ScriptGCEvent.h" | |
32 #include "bindings/v8/ScriptProfiler.h" | |
33 #include "bindings/v8/V8Binding.h" | |
34 #include "bindings/v8/V8HiddenValue.h" | |
35 #include "bindings/v8/V8ObjectConstructor.h" | |
36 #include "bindings/v8/V8RecursionScope.h" | |
37 #include "bindings/v8/V8ScriptRunner.h" | |
38 #include "core/frame/UseCounter.h" | |
39 #include "public/platform/Platform.h" | |
40 #include "wtf/MainThread.h" | |
41 | |
42 namespace WebCore { | |
43 | |
44 static V8PerIsolateData* mainThreadPerIsolateData = 0; | |
45 | |
46 #ifndef NDEBUG | |
47 static void assertV8RecursionScope() | |
48 { | |
49 ASSERT(V8RecursionScope::properlyUsed(v8::Isolate::GetCurrent())); | |
50 } | |
51 #endif | |
52 | |
53 static void useCounterCallback(v8::Isolate* isolate, v8::Isolate::UseCounterFeat
ure feature) | |
54 { | |
55 switch (feature) { | |
56 case v8::Isolate::kUseAsm: | |
57 UseCounter::count(currentExecutionContext(isolate), UseCounter::UseAsm); | |
58 break; | |
59 default: | |
60 ASSERT_NOT_REACHED(); | |
61 } | |
62 } | |
63 | |
64 V8PerIsolateData::V8PerIsolateData(v8::Isolate* isolate) | |
65 : m_isolate(isolate) | |
66 , m_isolateHolder(adoptPtr(new gin::IsolateHolder(m_isolate, v8ArrayBufferAl
locator()))) | |
67 , m_stringCache(adoptPtr(new StringCache(m_isolate))) | |
68 , m_hiddenValue(adoptPtr(new V8HiddenValue())) | |
69 , m_constructorMode(ConstructorMode::CreateNewObject) | |
70 , m_recursionLevel(0) | |
71 #ifndef NDEBUG | |
72 , m_internalScriptRecursionLevel(0) | |
73 #endif | |
74 , m_gcEventData(adoptPtr(new GCEventData())) | |
75 , m_performingMicrotaskCheckpoint(false) | |
76 { | |
77 #ifndef NDEBUG | |
78 // currentThread will always be non-null in production, but can be null in C
hromium unit tests. | |
79 if (blink::Platform::current()->currentThread()) | |
80 isolate->AddCallCompletedCallback(&assertV8RecursionScope); | |
81 #endif | |
82 if (isMainThread()) { | |
83 mainThreadPerIsolateData = this; | |
84 PageScriptDebugServer::setMainThreadIsolate(isolate); | |
85 } | |
86 isolate->SetUseCounterCallback(&useCounterCallback); | |
87 } | |
88 | |
89 V8PerIsolateData::~V8PerIsolateData() | |
90 { | |
91 if (m_scriptRegexpScriptState) | |
92 m_scriptRegexpScriptState->disposePerContextData(); | |
93 if (isMainThread()) | |
94 mainThreadPerIsolateData = 0; | |
95 } | |
96 | |
97 v8::Isolate* V8PerIsolateData::mainThreadIsolate() | |
98 { | |
99 ASSERT(isMainThread()); | |
100 ASSERT(mainThreadPerIsolateData); | |
101 return mainThreadPerIsolateData->isolate(); | |
102 } | |
103 | |
104 void V8PerIsolateData::ensureInitialized(v8::Isolate* isolate) | |
105 { | |
106 ASSERT(isolate); | |
107 if (!isolate->GetData(gin::kEmbedderBlink)) { | |
108 V8PerIsolateData* data = new V8PerIsolateData(isolate); | |
109 isolate->SetData(gin::kEmbedderBlink, data); | |
110 } | |
111 } | |
112 | |
113 v8::Persistent<v8::Value>& V8PerIsolateData::ensureLiveRoot() | |
114 { | |
115 if (m_liveRoot.isEmpty()) | |
116 m_liveRoot.set(m_isolate, v8::Null(m_isolate)); | |
117 return m_liveRoot.getUnsafe(); | |
118 } | |
119 | |
120 void V8PerIsolateData::dispose(v8::Isolate* isolate) | |
121 { | |
122 #ifndef NDEBUG | |
123 if (blink::Platform::current()->currentThread()) | |
124 isolate->RemoveCallCompletedCallback(&assertV8RecursionScope); | |
125 #endif | |
126 void* data = isolate->GetData(gin::kEmbedderBlink); | |
127 delete static_cast<V8PerIsolateData*>(data); | |
128 isolate->SetData(gin::kEmbedderBlink, 0); | |
129 } | |
130 | |
131 V8PerIsolateData::DOMTemplateMap& V8PerIsolateData::currentDOMTemplateMap() | |
132 { | |
133 if (DOMWrapperWorld::current(m_isolate).isMainWorld()) | |
134 return m_domTemplateMapForMainWorld; | |
135 return m_domTemplateMapForNonMainWorld; | |
136 } | |
137 | |
138 v8::Handle<v8::FunctionTemplate> V8PerIsolateData::domTemplate(void* domTemplate
Key, v8::FunctionCallback callback, v8::Handle<v8::Value> data, v8::Handle<v8::S
ignature> signature, int length) | |
139 { | |
140 DOMTemplateMap& domTemplateMap = currentDOMTemplateMap(); | |
141 DOMTemplateMap::iterator result = domTemplateMap.find(domTemplateKey); | |
142 if (result != domTemplateMap.end()) | |
143 return result->value.Get(m_isolate); | |
144 | |
145 v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(m_isolate,
callback, data, signature, length); | |
146 domTemplateMap.add(domTemplateKey, v8::Eternal<v8::FunctionTemplate>(m_isola
te, templ)); | |
147 return templ; | |
148 } | |
149 | |
150 v8::Handle<v8::FunctionTemplate> V8PerIsolateData::existingDOMTemplate(void* dom
TemplateKey) | |
151 { | |
152 DOMTemplateMap& domTemplateMap = currentDOMTemplateMap(); | |
153 DOMTemplateMap::iterator result = domTemplateMap.find(domTemplateKey); | |
154 if (result != domTemplateMap.end()) | |
155 return result->value.Get(m_isolate); | |
156 return v8::Local<v8::FunctionTemplate>(); | |
157 } | |
158 | |
159 void V8PerIsolateData::setDOMTemplate(void* domTemplateKey, v8::Handle<v8::Funct
ionTemplate> templ) | |
160 { | |
161 currentDOMTemplateMap().add(domTemplateKey, v8::Eternal<v8::FunctionTemplate
>(m_isolate, v8::Local<v8::FunctionTemplate>(templ))); | |
162 } | |
163 | |
164 v8::Local<v8::Context> V8PerIsolateData::ensureScriptRegexpContext() | |
165 { | |
166 if (!m_scriptRegexpScriptState) { | |
167 v8::Local<v8::Context> context(v8::Context::New(m_isolate)); | |
168 m_scriptRegexpScriptState = ScriptState::create(context, DOMWrapperWorld
::create()); | |
169 } | |
170 return m_scriptRegexpScriptState->context(); | |
171 } | |
172 | |
173 bool V8PerIsolateData::hasInstance(const WrapperTypeInfo* info, v8::Handle<v8::V
alue> value) | |
174 { | |
175 return hasInstance(info, value, m_domTemplateMapForMainWorld) | |
176 || hasInstance(info, value, m_domTemplateMapForNonMainWorld); | |
177 } | |
178 | |
179 bool V8PerIsolateData::hasInstance(const WrapperTypeInfo* info, v8::Handle<v8::V
alue> value, DOMTemplateMap& domTemplateMap) | |
180 { | |
181 DOMTemplateMap::iterator result = domTemplateMap.find(info); | |
182 if (result == domTemplateMap.end()) | |
183 return false; | |
184 v8::Handle<v8::FunctionTemplate> templ = result->value.Get(m_isolate); | |
185 return templ->HasInstance(value); | |
186 } | |
187 | |
188 v8::Handle<v8::Object> V8PerIsolateData::findInstanceInPrototypeChain(const Wrap
perTypeInfo* info, v8::Handle<v8::Value> value) | |
189 { | |
190 v8::Handle<v8::Object> wrapper = findInstanceInPrototypeChain(info, value, m
_domTemplateMapForMainWorld); | |
191 if (!wrapper.IsEmpty()) | |
192 return wrapper; | |
193 return findInstanceInPrototypeChain(info, value, m_domTemplateMapForNonMainW
orld); | |
194 } | |
195 | |
196 v8::Handle<v8::Object> V8PerIsolateData::findInstanceInPrototypeChain(const Wrap
perTypeInfo* info, v8::Handle<v8::Value> value, DOMTemplateMap& domTemplateMap) | |
197 { | |
198 if (value.IsEmpty() || !value->IsObject()) | |
199 return v8::Handle<v8::Object>(); | |
200 DOMTemplateMap::iterator result = domTemplateMap.find(info); | |
201 if (result == domTemplateMap.end()) | |
202 return v8::Handle<v8::Object>(); | |
203 v8::Handle<v8::FunctionTemplate> templ = result->value.Get(m_isolate); | |
204 return v8::Handle<v8::Object>::Cast(value)->FindInstanceInPrototypeChain(tem
pl); | |
205 } | |
206 | |
207 static void constructorOfToString(const v8::FunctionCallbackInfo<v8::Value>& inf
o) | |
208 { | |
209 // The DOM constructors' toString functions grab the current toString | |
210 // for Functions by taking the toString function of itself and then | |
211 // calling it with the constructor as its receiver. This means that | |
212 // changes to the Function prototype chain or toString function are | |
213 // reflected when printing DOM constructors. The only wart is that | |
214 // changes to a DOM constructor's toString's toString will cause the | |
215 // toString of the DOM constructor itself to change. This is extremely | |
216 // obscure and unlikely to be a problem. | |
217 v8::Handle<v8::Value> value = info.Callee()->Get(v8AtomicString(info.GetIsol
ate(), "toString")); | |
218 if (!value->IsFunction()) { | |
219 v8SetReturnValue(info, v8::String::Empty(info.GetIsolate())); | |
220 return; | |
221 } | |
222 v8SetReturnValue(info, V8ScriptRunner::callInternalFunction(v8::Handle<v8::F
unction>::Cast(value), info.This(), 0, 0, info.GetIsolate())); | |
223 } | |
224 | |
225 v8::Handle<v8::FunctionTemplate> V8PerIsolateData::toStringTemplate() | |
226 { | |
227 if (m_toStringTemplate.isEmpty()) | |
228 m_toStringTemplate.set(m_isolate, v8::FunctionTemplate::New(m_isolate, c
onstructorOfToString)); | |
229 return m_toStringTemplate.newLocal(m_isolate); | |
230 } | |
231 | |
232 } // namespace WebCore | |
OLD | NEW |