Upstream version 5.34.98.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / v8 / V8PerIsolateData.cpp
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/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"
35
36 namespace WebCore {
37
38 V8PerIsolateData::V8PerIsolateData(v8::Isolate* isolate)
39     : m_isolate(isolate)
40     , m_stringCache(adoptPtr(new StringCache()))
41     , m_workerDomDataStore(0)
42     , m_constructorMode(ConstructorMode::CreateNewObject)
43     , m_recursionLevel(0)
44 #ifndef NDEBUG
45     , m_internalScriptRecursionLevel(0)
46 #endif
47     , m_gcEventData(adoptPtr(new GCEventData()))
48     , m_shouldCollectGarbageSoon(false)
49 {
50 }
51
52 V8PerIsolateData::~V8PerIsolateData()
53 {
54 }
55
56 V8PerIsolateData* V8PerIsolateData::create(v8::Isolate* isolate)
57 {
58     ASSERT(isolate);
59     ASSERT(!isolate->GetData(gin::kEmbedderBlink));
60     V8PerIsolateData* data = new V8PerIsolateData(isolate);
61     isolate->SetData(gin::kEmbedderBlink, data);
62     return data;
63 }
64
65 void V8PerIsolateData::ensureInitialized(v8::Isolate* isolate)
66 {
67     ASSERT(isolate);
68     if (!isolate->GetData(gin::kEmbedderBlink))
69         create(isolate);
70 }
71
72 v8::Persistent<v8::Value>& V8PerIsolateData::ensureLiveRoot()
73 {
74     if (m_liveRoot.isEmpty())
75         m_liveRoot.set(m_isolate, v8::Null(m_isolate));
76     return m_liveRoot.getUnsafe();
77 }
78
79 void V8PerIsolateData::dispose(v8::Isolate* isolate)
80 {
81     void* data = isolate->GetData(gin::kEmbedderBlink);
82     delete static_cast<V8PerIsolateData*>(data);
83     isolate->SetData(gin::kEmbedderBlink, 0);
84 }
85
86 v8::Handle<v8::FunctionTemplate> V8PerIsolateData::toStringTemplate()
87 {
88     if (m_toStringTemplate.isEmpty())
89         m_toStringTemplate.set(m_isolate, v8::FunctionTemplate::New(m_isolate, constructorOfToString));
90     return m_toStringTemplate.newLocal(m_isolate);
91 }
92
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)
94 {
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));
101     return templ;
102 }
103
104 v8::Handle<v8::FunctionTemplate> V8PerIsolateData::privateTemplateIfExists(WrapperWorldType currentWorldType, void* privatePointer)
105 {
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>();
111 }
112
113 void V8PerIsolateData::setPrivateTemplate(WrapperWorldType currentWorldType, void* privatePointer, v8::Handle<v8::FunctionTemplate> templ)
114 {
115     templateMap(currentWorldType).add(privatePointer, UnsafePersistent<v8::FunctionTemplate>(m_isolate, templ));
116 }
117
118 v8::Local<v8::Context> V8PerIsolateData::ensureRegexContext()
119 {
120     if (m_regexContext.isEmpty()) {
121         v8::HandleScope handleScope(m_isolate);
122         m_regexContext.set(m_isolate, v8::Context::New(m_isolate));
123     }
124     return m_regexContext.newLocal(m_isolate);
125 }
126
127 bool V8PerIsolateData::hasInstanceInMainWorld(const WrapperTypeInfo* info, v8::Handle<v8::Value> value)
128 {
129     return hasInstance(info, value, m_templatesForMainWorld);
130 }
131
132 bool V8PerIsolateData::hasInstanceInNonMainWorld(const WrapperTypeInfo* info, v8::Handle<v8::Value> value)
133 {
134     return hasInstance(info, value, m_templatesForNonMainWorld);
135 }
136
137 bool V8PerIsolateData::hasInstance(const WrapperTypeInfo* info, v8::Handle<v8::Value> value, TemplateMap& templates)
138 {
139     TemplateMap::iterator result = templates.find(info);
140     if (result == templates.end())
141         return false;
142     v8::HandleScope handleScope(m_isolate);
143     return result->value.newLocal(m_isolate)->HasInstance(value);
144 }
145
146 void V8PerIsolateData::constructorOfToString(const v8::FunctionCallbackInfo<v8::Value>& info)
147 {
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()));
159         return;
160     }
161     v8SetReturnValue(info, V8ScriptRunner::callInternalFunction(v8::Handle<v8::Function>::Cast(value), info.This(), 0, 0, v8::Isolate::GetCurrent()));
162 }
163
164 } // namespace WebCore