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/ScriptGCEvent.h"
31 #include "bindings/v8/ScriptProfiler.h"
32 #include "bindings/v8/V8Binding.h"
33 #include "bindings/v8/V8ObjectConstructor.h"
34 #include "bindings/v8/V8ScriptRunner.h"
38 V8PerIsolateData::V8PerIsolateData(v8::Isolate* isolate)
40 , m_stringCache(adoptPtr(new StringCache()))
41 , m_workerDomDataStore(0)
42 , m_constructorMode(ConstructorMode::CreateNewObject)
45 , m_internalScriptRecursionLevel(0)
47 , m_gcEventData(adoptPtr(new GCEventData()))
48 , m_shouldCollectGarbageSoon(false)
52 V8PerIsolateData::~V8PerIsolateData()
56 V8PerIsolateData* V8PerIsolateData::create(v8::Isolate* isolate)
59 ASSERT(!isolate->GetData(gin::kEmbedderBlink));
60 V8PerIsolateData* data = new V8PerIsolateData(isolate);
61 isolate->SetData(gin::kEmbedderBlink, data);
65 void V8PerIsolateData::ensureInitialized(v8::Isolate* isolate)
68 if (!isolate->GetData(gin::kEmbedderBlink))
72 v8::Persistent<v8::Value>& V8PerIsolateData::ensureLiveRoot()
74 if (m_liveRoot.isEmpty())
75 m_liveRoot.set(m_isolate, v8::Null(m_isolate));
76 return m_liveRoot.getUnsafe();
79 void V8PerIsolateData::dispose(v8::Isolate* isolate)
81 void* data = isolate->GetData(gin::kEmbedderBlink);
82 delete static_cast<V8PerIsolateData*>(data);
83 isolate->SetData(gin::kEmbedderBlink, 0);
86 v8::Handle<v8::FunctionTemplate> V8PerIsolateData::toStringTemplate()
88 if (m_toStringTemplate.isEmpty())
89 m_toStringTemplate.set(m_isolate, v8::FunctionTemplate::New(m_isolate, constructorOfToString));
90 return m_toStringTemplate.newLocal(m_isolate);
93 v8::Handle<v8::FunctionTemplate> V8PerIsolateData::privateTemplate(WrapperWorldType currentWorldType, void* privatePointer, v8::FunctionCallback callback, v8::Handle<v8::Value> data, v8::Handle<v8::Signature> signature, int length)
95 TemplateMap& templates = templateMap(currentWorldType);
96 TemplateMap::iterator result = templates.find(privatePointer);
97 if (result != templates.end())
98 return result->value.newLocal(m_isolate);
99 v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(m_isolate, callback, data, signature, length);
100 templates.add(privatePointer, UnsafePersistent<v8::FunctionTemplate>(m_isolate, templ));
104 v8::Handle<v8::FunctionTemplate> V8PerIsolateData::privateTemplateIfExists(WrapperWorldType currentWorldType, void* privatePointer)
106 TemplateMap& templates = templateMap(currentWorldType);
107 TemplateMap::iterator result = templates.find(privatePointer);
108 if (result != templates.end())
109 return result->value.newLocal(m_isolate);
110 return v8::Local<v8::FunctionTemplate>();
113 void V8PerIsolateData::setPrivateTemplate(WrapperWorldType currentWorldType, void* privatePointer, v8::Handle<v8::FunctionTemplate> templ)
115 templateMap(currentWorldType).add(privatePointer, UnsafePersistent<v8::FunctionTemplate>(m_isolate, templ));
118 v8::Local<v8::Context> V8PerIsolateData::ensureRegexContext()
120 if (m_regexContext.isEmpty()) {
121 v8::HandleScope handleScope(m_isolate);
122 m_regexContext.set(m_isolate, v8::Context::New(m_isolate));
124 return m_regexContext.newLocal(m_isolate);
127 bool V8PerIsolateData::hasInstanceInMainWorld(const WrapperTypeInfo* info, v8::Handle<v8::Value> value)
129 return hasInstance(info, value, m_templatesForMainWorld);
132 bool V8PerIsolateData::hasInstanceInNonMainWorld(const WrapperTypeInfo* info, v8::Handle<v8::Value> value)
134 return hasInstance(info, value, m_templatesForNonMainWorld);
137 bool V8PerIsolateData::hasInstance(const WrapperTypeInfo* info, v8::Handle<v8::Value> value, TemplateMap& templates)
139 TemplateMap::iterator result = templates.find(info);
140 if (result == templates.end())
142 v8::HandleScope handleScope(m_isolate);
143 return result->value.newLocal(m_isolate)->HasInstance(value);
146 void V8PerIsolateData::constructorOfToString(const v8::FunctionCallbackInfo<v8::Value>& info)
148 // The DOM constructors' toString functions grab the current toString
149 // for Functions by taking the toString function of itself and then
150 // calling it with the constructor as its receiver. This means that
151 // changes to the Function prototype chain or toString function are
152 // reflected when printing DOM constructors. The only wart is that
153 // changes to a DOM constructor's toString's toString will cause the
154 // toString of the DOM constructor itself to change. This is extremely
155 // obscure and unlikely to be a problem.
156 v8::Handle<v8::Value> value = info.Callee()->Get(v8AtomicString(info.GetIsolate(), "toString"));
157 if (!value->IsFunction()) {
158 v8SetReturnValue(info, v8::String::Empty(info.GetIsolate()));
161 v8SetReturnValue(info, V8ScriptRunner::callInternalFunction(v8::Handle<v8::Function>::Cast(value), info.This(), 0, 0, v8::Isolate::GetCurrent()));
164 } // namespace WebCore