Home | History | Annotate | Download | only in v8
      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 #ifndef V8PerIsolateData_h
     27 #define V8PerIsolateData_h
     28 
     29 #include "bindings/v8/ScopedPersistent.h"
     30 #include "bindings/v8/UnsafePersistent.h"
     31 #include "bindings/v8/WrapperTypeInfo.h"
     32 #include <v8.h>
     33 #include "wtf/Forward.h"
     34 #include "wtf/HashMap.h"
     35 #include "wtf/OwnPtr.h"
     36 #include "wtf/Vector.h"
     37 
     38 namespace WebCore {
     39 
     40 class DOMDataStore;
     41 class GCEventData;
     42 class StringCache;
     43 class V8HiddenPropertyName;
     44 struct WrapperTypeInfo;
     45 
     46 class ExternalStringVisitor;
     47 
     48 typedef WTF::Vector<DOMDataStore*> DOMDataList;
     49 
     50 class V8PerIsolateData {
     51 public:
     52     static V8PerIsolateData* create(v8::Isolate*);
     53     static void ensureInitialized(v8::Isolate*);
     54     static V8PerIsolateData* current()
     55     {
     56         return from(v8::Isolate::GetCurrent());
     57     }
     58     static V8PerIsolateData* from(v8::Isolate* isolate)
     59     {
     60         ASSERT(isolate);
     61         ASSERT(isolate->GetData());
     62         return static_cast<V8PerIsolateData*>(isolate->GetData());
     63     }
     64     static void dispose(v8::Isolate*);
     65 
     66     typedef HashMap<void*, UnsafePersistent<v8::FunctionTemplate> > TemplateMap;
     67 
     68     TemplateMap& rawTemplateMap(WrapperWorldType worldType)
     69     {
     70         if (worldType == MainWorld)
     71             return m_rawTemplatesForMainWorld;
     72         return m_rawTemplatesForNonMainWorld;
     73     }
     74 
     75     TemplateMap& templateMap(WrapperWorldType worldType)
     76     {
     77         if (worldType == MainWorld)
     78             return m_templatesForMainWorld;
     79         return m_templatesForNonMainWorld;
     80     }
     81 
     82     v8::Handle<v8::FunctionTemplate> toStringTemplate();
     83     v8::Handle<v8::FunctionTemplate> lazyEventListenerToStringTemplate()
     84     {
     85         return v8::Local<v8::FunctionTemplate>::New(m_isolate, m_lazyEventListenerToStringTemplate);
     86     }
     87 
     88     StringCache* stringCache() { return m_stringCache.get(); }
     89 
     90     v8::Persistent<v8::Value>& ensureLiveRoot();
     91 
     92     DOMDataList& allStores() { return m_domDataList; }
     93 
     94     V8HiddenPropertyName* hiddenPropertyName() { return m_hiddenPropertyName.get(); }
     95 
     96     void registerDOMDataStore(DOMDataStore* domDataStore)
     97     {
     98         ASSERT(m_domDataList.find(domDataStore) == notFound);
     99         m_domDataList.append(domDataStore);
    100     }
    101 
    102     void unregisterDOMDataStore(DOMDataStore* domDataStore)
    103     {
    104         ASSERT(m_domDataList.find(domDataStore) != notFound);
    105         m_domDataList.remove(m_domDataList.find(domDataStore));
    106     }
    107 
    108     // DOMDataStore is owned outside V8PerIsolateData.
    109     DOMDataStore* workerDOMDataStore() { return m_workerDomDataStore; }
    110     void setWorkerDOMDataStore(DOMDataStore* store) { m_workerDomDataStore = store; }
    111 
    112     int recursionLevel() const { return m_recursionLevel; }
    113     int incrementRecursionLevel() { return ++m_recursionLevel; }
    114     int decrementRecursionLevel() { return --m_recursionLevel; }
    115 
    116 #ifndef NDEBUG
    117     int internalScriptRecursionLevel() const { return m_internalScriptRecursionLevel; }
    118     int incrementInternalScriptRecursionLevel() { return ++m_internalScriptRecursionLevel; }
    119     int decrementInternalScriptRecursionLevel() { return --m_internalScriptRecursionLevel; }
    120 #endif
    121 
    122     GCEventData* gcEventData() { return m_gcEventData.get(); }
    123 
    124     // Gives the system a hint that we should request garbage collection
    125     // upon the next close or navigation event, because some expensive
    126     // objects have been allocated that we want to take every opportunity
    127     // to collect.
    128     void setShouldCollectGarbageSoon() { m_shouldCollectGarbageSoon = true; }
    129     void clearShouldCollectGarbageSoon() { m_shouldCollectGarbageSoon = false; }
    130     bool shouldCollectGarbageSoon() const { return m_shouldCollectGarbageSoon; }
    131 
    132     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);
    133     v8::Handle<v8::FunctionTemplate> privateTemplateIfExists(WrapperWorldType, void* privatePointer);
    134     void setPrivateTemplate(WrapperWorldType, void* privatePointer, v8::Handle<v8::FunctionTemplate>);
    135 
    136     v8::Handle<v8::FunctionTemplate> rawTemplate(WrapperTypeInfo*, WrapperWorldType);
    137 
    138     bool hasInstance(WrapperTypeInfo*, v8::Handle<v8::Value>, WrapperWorldType);
    139 
    140     v8::Local<v8::Context> ensureRegexContext();
    141 
    142     const char* previousSamplingState() const { return m_previousSamplingState; }
    143     void setPreviousSamplingState(const char* name) { m_previousSamplingState = name; }
    144 
    145 private:
    146     explicit V8PerIsolateData(v8::Isolate*);
    147     ~V8PerIsolateData();
    148     static void constructorOfToString(const v8::FunctionCallbackInfo<v8::Value>&);
    149 
    150     v8::Isolate* m_isolate;
    151     TemplateMap m_rawTemplatesForMainWorld;
    152     TemplateMap m_rawTemplatesForNonMainWorld;
    153     TemplateMap m_templatesForMainWorld;
    154     TemplateMap m_templatesForNonMainWorld;
    155     ScopedPersistent<v8::FunctionTemplate> m_toStringTemplate;
    156     v8::Persistent<v8::FunctionTemplate> m_lazyEventListenerToStringTemplate;
    157     OwnPtr<StringCache> m_stringCache;
    158 
    159     Vector<DOMDataStore*> m_domDataList;
    160     DOMDataStore* m_workerDomDataStore;
    161 
    162     OwnPtr<V8HiddenPropertyName> m_hiddenPropertyName;
    163     ScopedPersistent<v8::Value> m_liveRoot;
    164     ScopedPersistent<v8::Context> m_regexContext;
    165 
    166     const char* m_previousSamplingState;
    167 
    168     bool m_constructorMode;
    169     friend class ConstructorMode;
    170 
    171     int m_recursionLevel;
    172 
    173 #ifndef NDEBUG
    174     int m_internalScriptRecursionLevel;
    175 #endif
    176     OwnPtr<GCEventData> m_gcEventData;
    177     bool m_shouldCollectGarbageSoon;
    178 };
    179 
    180 } // namespace WebCore
    181 
    182 #endif // V8PerIsolateData_h
    183