2 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "bindings/v8/V8PerIsolateData.h"
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 "public/platform/Platform.h"
39 #include "wtf/MainThread.h"
43 static V8PerIsolateData* mainThreadPerIsolateData = 0;
46 static void assertV8RecursionScope()
48 ASSERT(V8RecursionScope::properlyUsed(v8::Isolate::GetCurrent()));
52 V8PerIsolateData::V8PerIsolateData(v8::Isolate* isolate)
54 , m_isolateHolder(adoptPtr(new gin::IsolateHolder(m_isolate, v8ArrayBufferAllocator())))
55 , m_stringCache(adoptPtr(new StringCache(m_isolate)))
56 , m_hiddenValue(adoptPtr(new V8HiddenValue()))
57 , m_constructorMode(ConstructorMode::CreateNewObject)
60 , m_internalScriptRecursionLevel(0)
62 , m_gcEventData(adoptPtr(new GCEventData()))
63 , m_performingMicrotaskCheckpoint(false)
66 // currentThread will always be non-null in production, but can be null in Chromium unit tests.
67 if (blink::Platform::current()->currentThread())
68 isolate->AddCallCompletedCallback(&assertV8RecursionScope);
71 mainThreadPerIsolateData = this;
72 PageScriptDebugServer::setMainThreadIsolate(isolate);
76 V8PerIsolateData::~V8PerIsolateData()
78 if (m_blinkInJSScriptState)
79 m_blinkInJSScriptState->disposePerContextData();
81 mainThreadPerIsolateData = 0;
84 v8::Isolate* V8PerIsolateData::mainThreadIsolate()
86 ASSERT(isMainThread());
87 ASSERT(mainThreadPerIsolateData);
88 return mainThreadPerIsolateData->isolate();
91 void V8PerIsolateData::ensureInitialized(v8::Isolate* isolate)
94 if (!isolate->GetData(gin::kEmbedderBlink)) {
95 V8PerIsolateData* data = new V8PerIsolateData(isolate);
96 isolate->SetData(gin::kEmbedderBlink, data);
100 v8::Persistent<v8::Value>& V8PerIsolateData::ensureLiveRoot()
102 if (m_liveRoot.isEmpty())
103 m_liveRoot.set(m_isolate, v8::Null(m_isolate));
104 return m_liveRoot.getUnsafe();
107 void V8PerIsolateData::dispose(v8::Isolate* isolate)
110 if (blink::Platform::current()->currentThread())
111 isolate->RemoveCallCompletedCallback(&assertV8RecursionScope);
113 void* data = isolate->GetData(gin::kEmbedderBlink);
114 delete static_cast<V8PerIsolateData*>(data);
115 isolate->SetData(gin::kEmbedderBlink, 0);
118 V8PerIsolateData::DOMTemplateMap& V8PerIsolateData::currentDOMTemplateMap()
120 if (DOMWrapperWorld::current(m_isolate).isMainWorld())
121 return m_domTemplateMapForMainWorld;
122 return m_domTemplateMapForNonMainWorld;
125 v8::Handle<v8::FunctionTemplate> V8PerIsolateData::domTemplate(void* domTemplateKey, v8::FunctionCallback callback, v8::Handle<v8::Value> data, v8::Handle<v8::Signature> signature, int length)
127 DOMTemplateMap& domTemplateMap = currentDOMTemplateMap();
128 DOMTemplateMap::iterator result = domTemplateMap.find(domTemplateKey);
129 if (result != domTemplateMap.end())
130 return result->value.Get(m_isolate);
132 v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(m_isolate, callback, data, signature, length);
133 domTemplateMap.add(domTemplateKey, v8::Eternal<v8::FunctionTemplate>(m_isolate, templ));
137 v8::Handle<v8::FunctionTemplate> V8PerIsolateData::existingDOMTemplate(void* domTemplateKey)
139 DOMTemplateMap& domTemplateMap = currentDOMTemplateMap();
140 DOMTemplateMap::iterator result = domTemplateMap.find(domTemplateKey);
141 if (result != domTemplateMap.end())
142 return result->value.Get(m_isolate);
143 return v8::Local<v8::FunctionTemplate>();
146 void V8PerIsolateData::setDOMTemplate(void* domTemplateKey, v8::Handle<v8::FunctionTemplate> templ)
148 currentDOMTemplateMap().add(domTemplateKey, v8::Eternal<v8::FunctionTemplate>(m_isolate, v8::Local<v8::FunctionTemplate>(templ)));
151 v8::Local<v8::Context> V8PerIsolateData::ensureDomInJSContext()
153 if (!m_blinkInJSScriptState)
154 m_blinkInJSScriptState = ScriptState::create(v8::Context::New(m_isolate), DOMWrapperWorld::create());
155 return m_blinkInJSScriptState->context();
158 bool V8PerIsolateData::hasInstance(const WrapperTypeInfo* info, v8::Handle<v8::Value> value)
160 return hasInstance(info, value, m_domTemplateMapForMainWorld)
161 || hasInstance(info, value, m_domTemplateMapForNonMainWorld);
164 bool V8PerIsolateData::hasInstance(const WrapperTypeInfo* info, v8::Handle<v8::Value> value, DOMTemplateMap& domTemplateMap)
166 DOMTemplateMap::iterator result = domTemplateMap.find(info);
167 if (result == domTemplateMap.end())
169 v8::Handle<v8::FunctionTemplate> templ = result->value.Get(m_isolate);
170 return templ->HasInstance(value);
173 v8::Handle<v8::Object> V8PerIsolateData::findInstanceInPrototypeChain(const WrapperTypeInfo* info, v8::Handle<v8::Value> value)
175 v8::Handle<v8::Object> wrapper = findInstanceInPrototypeChain(info, value, m_domTemplateMapForMainWorld);
176 if (!wrapper.IsEmpty())
178 return findInstanceInPrototypeChain(info, value, m_domTemplateMapForNonMainWorld);
181 v8::Handle<v8::Object> V8PerIsolateData::findInstanceInPrototypeChain(const WrapperTypeInfo* info, v8::Handle<v8::Value> value, DOMTemplateMap& domTemplateMap)
183 if (value.IsEmpty() || !value->IsObject())
184 return v8::Handle<v8::Object>();
185 DOMTemplateMap::iterator result = domTemplateMap.find(info);
186 if (result == domTemplateMap.end())
187 return v8::Handle<v8::Object>();
188 v8::Handle<v8::FunctionTemplate> templ = result->value.Get(m_isolate);
189 return v8::Handle<v8::Object>::Cast(value)->FindInstanceInPrototypeChain(templ);
192 static void constructorOfToString(const v8::FunctionCallbackInfo<v8::Value>& info)
194 // The DOM constructors' toString functions grab the current toString
195 // for Functions by taking the toString function of itself and then
196 // calling it with the constructor as its receiver. This means that
197 // changes to the Function prototype chain or toString function are
198 // reflected when printing DOM constructors. The only wart is that
199 // changes to a DOM constructor's toString's toString will cause the
200 // toString of the DOM constructor itself to change. This is extremely
201 // obscure and unlikely to be a problem.
202 v8::Handle<v8::Value> value = info.Callee()->Get(v8AtomicString(info.GetIsolate(), "toString"));
203 if (!value->IsFunction()) {
204 v8SetReturnValue(info, v8::String::Empty(info.GetIsolate()));
207 v8SetReturnValue(info, V8ScriptRunner::callInternalFunction(v8::Handle<v8::Function>::Cast(value), info.This(), 0, 0, info.GetIsolate()));
210 v8::Handle<v8::FunctionTemplate> V8PerIsolateData::toStringTemplate()
212 if (m_toStringTemplate.isEmpty())
213 m_toStringTemplate.set(m_isolate, v8::FunctionTemplate::New(m_isolate, constructorOfToString));
214 return m_toStringTemplate.newLocal(m_isolate);
217 } // namespace WebCore