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.
26 #ifndef V8PerIsolateData_h
27 #define V8PerIsolateData_h
29 #include "bindings/v8/ScopedPersistent.h"
30 #include "bindings/v8/UnsafePersistent.h"
31 #include "bindings/v8/WrapperTypeInfo.h"
32 #include "gin/public/gin_embedders.h"
34 #include "wtf/Forward.h"
35 #include "wtf/HashMap.h"
36 #include "wtf/OwnPtr.h"
37 #include "wtf/Vector.h"
44 struct WrapperTypeInfo;
46 class ExternalStringVisitor;
48 typedef WTF::Vector<DOMDataStore*> DOMDataStoreList;
50 class V8PerIsolateData {
52 static void ensureInitialized(v8::Isolate*);
53 static V8PerIsolateData* current()
55 return from(v8::Isolate::GetCurrent());
57 static V8PerIsolateData* from(v8::Isolate* isolate)
60 ASSERT(isolate->GetData(gin::kEmbedderBlink));
61 return static_cast<V8PerIsolateData*>(isolate->GetData(gin::kEmbedderBlink));
63 static void dispose(v8::Isolate*);
64 static v8::Isolate* mainThreadIsolate();
66 bool isMainThread() { return m_isMainThread; };
68 typedef HashMap<const void*, UnsafePersistent<v8::FunctionTemplate> > TemplateMap;
70 TemplateMap& templateMap(WrapperWorldType worldType)
72 if (worldType == MainWorld)
73 return m_templatesForMainWorld;
74 return m_templatesForNonMainWorld;
77 v8::Handle<v8::FunctionTemplate> toStringTemplate();
79 StringCache* stringCache() { return m_stringCache.get(); }
81 v8::Persistent<v8::Value>& ensureLiveRoot();
83 DOMDataStoreList& allStores() { return m_domDataStoreList; }
85 void registerDOMDataStore(DOMDataStore* domDataStore)
87 ASSERT(m_domDataStoreList.find(domDataStore) == kNotFound);
88 m_domDataStoreList.append(domDataStore);
91 void unregisterDOMDataStore(DOMDataStore* domDataStore)
93 ASSERT(m_domDataStoreList.find(domDataStore) != kNotFound);
94 m_domDataStoreList.remove(m_domDataStoreList.find(domDataStore));
97 int recursionLevel() const { return m_recursionLevel; }
98 int incrementRecursionLevel() { return ++m_recursionLevel; }
99 int decrementRecursionLevel() { return --m_recursionLevel; }
101 bool performingMicrotaskCheckpoint() const { return m_performingMicrotaskCheckpoint; }
102 void setPerformingMicrotaskCheckpoint(bool performingMicrotaskCheckpoint) { m_performingMicrotaskCheckpoint = performingMicrotaskCheckpoint; }
105 int internalScriptRecursionLevel() const { return m_internalScriptRecursionLevel; }
106 int incrementInternalScriptRecursionLevel() { return ++m_internalScriptRecursionLevel; }
107 int decrementInternalScriptRecursionLevel() { return --m_internalScriptRecursionLevel; }
110 GCEventData* gcEventData() { return m_gcEventData.get(); }
112 // Gives the system a hint that we should request garbage collection
113 // upon the next close or navigation event, because some expensive
114 // objects have been allocated that we want to take every opportunity
116 void setShouldCollectGarbageSoon() { m_shouldCollectGarbageSoon = true; }
117 void clearShouldCollectGarbageSoon() { m_shouldCollectGarbageSoon = false; }
118 bool shouldCollectGarbageSoon() const { return m_shouldCollectGarbageSoon; }
120 v8::Handle<v8::FunctionTemplate> privateTemplate(WrapperWorldType, void* privatePointer, v8::FunctionCallback = 0, v8::Handle<v8::Value> data = v8::Handle<v8::Value>(), v8::Handle<v8::Signature> = v8::Handle<v8::Signature>(), int length = 0);
121 v8::Handle<v8::FunctionTemplate> privateTemplateIfExists(WrapperWorldType, void* privatePointer);
122 void setPrivateTemplate(WrapperWorldType, void* privatePointer, v8::Handle<v8::FunctionTemplate>);
124 bool hasInstanceInMainWorld(const WrapperTypeInfo*, v8::Handle<v8::Value>);
125 bool hasInstanceInNonMainWorld(const WrapperTypeInfo*, v8::Handle<v8::Value>);
127 v8::Local<v8::Context> ensureRegexContext();
129 const char* previousSamplingState() const { return m_previousSamplingState; }
130 void setPreviousSamplingState(const char* name) { m_previousSamplingState = name; }
133 explicit V8PerIsolateData(v8::Isolate*);
135 bool hasInstance(const WrapperTypeInfo*, v8::Handle<v8::Value>, TemplateMap&);
136 static void constructorOfToString(const v8::FunctionCallbackInfo<v8::Value>&);
138 v8::Isolate* m_isolate;
139 bool m_isMainThread; // Caches the result of isMainThread() for performance.
140 TemplateMap m_templatesForMainWorld;
141 TemplateMap m_templatesForNonMainWorld;
142 ScopedPersistent<v8::FunctionTemplate> m_toStringTemplate;
143 OwnPtr<StringCache> m_stringCache;
145 Vector<DOMDataStore*> m_domDataStoreList;
147 ScopedPersistent<v8::Value> m_liveRoot;
148 ScopedPersistent<v8::Context> m_regexContext;
150 const char* m_previousSamplingState;
152 bool m_constructorMode;
153 friend class ConstructorMode;
155 int m_recursionLevel;
158 int m_internalScriptRecursionLevel;
160 OwnPtr<GCEventData> m_gcEventData;
161 bool m_shouldCollectGarbageSoon;
162 bool m_performingMicrotaskCheckpoint;
165 } // namespace WebCore
167 #endif // V8PerIsolateData_h