Title: [124929] trunk/Source/WebCore
Revision
124929
Author
[email protected]
Date
2012-08-07 15:53:23 -0700 (Tue, 07 Aug 2012)

Log Message

[V8] Factor out V8Binding classes that manage V8 value's cache
https://bugs.webkit.org/show_bug.cgi?id=93343

Reviewed by Adam Barth.

We are factoring out V8Binding methods to separate files.
This patch moves V8Binding classes that manage V8 value's cache
to V8ValueCache.{h,cpp}.

No tests. No change in behavior.

* UseV8.cmake:
* WebCore.gypi:
* bindings/v8/V8Binding.cpp:
* bindings/v8/V8Binding.h:
(WebCore):
* bindings/v8/V8ValueCache.cpp: Added.
(WebCore):
(WebCore::makeExternalString):
(WebCore::cachedStringCallback):
(WebCore::StringCache::remove):
(WebCore::StringCache::v8ExternalStringSlow):
(WebCore::IntegerCache::createSmallIntegers):
(WebCore::IntegerCache::~IntegerCache):
* bindings/v8/V8ValueCache.h: Added.
(WebCore):
(StringCache):
(WebCore::StringCache::StringCache):
(WebCore::StringCache::v8ExternalString):
(WebCore::StringCache::clearOnGC):
(WebCoreStringResource):
(WebCore::WebCoreStringResource::WebCoreStringResource):
(WebCore::WebCoreStringResource::~WebCoreStringResource):
(WebCore::WebCoreStringResource::data):
(WebCore::WebCoreStringResource::length):
(WebCore::WebCoreStringResource::webcoreString):
(WebCore::WebCoreStringResource::atomicString):
(WebCore::WebCoreStringResource::visitStrings): This method is used by inspector
and thus is not performance-critical. To avoid circular #include dependency,
I moved the implementation to cpp.
(WebCore::WebCoreStringResource::toStringResource):
(IntegerCache):
(WebCore::IntegerCache::IntegerCache):
(WebCore::IntegerCache::v8Integer):
(WebCore::IntegerCache::v8UnsignedInteger):

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (124928 => 124929)


--- trunk/Source/WebCore/ChangeLog	2012-08-07 22:39:26 UTC (rev 124928)
+++ trunk/Source/WebCore/ChangeLog	2012-08-07 22:53:23 UTC (rev 124929)
@@ -1,3 +1,51 @@
+2012-08-07  Kentaro Hara  <[email protected]>
+
+        [V8] Factor out V8Binding classes that manage V8 value's cache
+        https://bugs.webkit.org/show_bug.cgi?id=93343
+
+        Reviewed by Adam Barth.
+
+        We are factoring out V8Binding methods to separate files.
+        This patch moves V8Binding classes that manage V8 value's cache
+        to V8ValueCache.{h,cpp}.
+
+        No tests. No change in behavior.
+
+        * UseV8.cmake:
+        * WebCore.gypi:
+        * bindings/v8/V8Binding.cpp:
+        * bindings/v8/V8Binding.h:
+        (WebCore):
+        * bindings/v8/V8ValueCache.cpp: Added.
+        (WebCore):
+        (WebCore::makeExternalString):
+        (WebCore::cachedStringCallback):
+        (WebCore::StringCache::remove):
+        (WebCore::StringCache::v8ExternalStringSlow):
+        (WebCore::IntegerCache::createSmallIntegers):
+        (WebCore::IntegerCache::~IntegerCache):
+        * bindings/v8/V8ValueCache.h: Added.
+        (WebCore):
+        (StringCache):
+        (WebCore::StringCache::StringCache):
+        (WebCore::StringCache::v8ExternalString):
+        (WebCore::StringCache::clearOnGC):
+        (WebCoreStringResource):
+        (WebCore::WebCoreStringResource::WebCoreStringResource):
+        (WebCore::WebCoreStringResource::~WebCoreStringResource):
+        (WebCore::WebCoreStringResource::data):
+        (WebCore::WebCoreStringResource::length):
+        (WebCore::WebCoreStringResource::webcoreString):
+        (WebCore::WebCoreStringResource::atomicString):
+        (WebCore::WebCoreStringResource::visitStrings): This method is used by inspector
+        and thus is not performance-critical. To avoid circular #include dependency,
+        I moved the implementation to cpp.
+        (WebCore::WebCoreStringResource::toStringResource):
+        (IntegerCache):
+        (WebCore::IntegerCache::IntegerCache):
+        (WebCore::IntegerCache::v8Integer):
+        (WebCore::IntegerCache::v8UnsignedInteger):
+
 2012-08-07  James Robinson  <[email protected]>
 
         [chromium] Switch PlatformLayer typedef to Platform API type for PLATFORM(CHROMIUM)

Modified: trunk/Source/WebCore/UseV8.cmake (124928 => 124929)


--- trunk/Source/WebCore/UseV8.cmake	2012-08-07 22:39:26 UTC (rev 124928)
+++ trunk/Source/WebCore/UseV8.cmake	2012-08-07 22:53:23 UTC (rev 124929)
@@ -62,6 +62,7 @@
     bindings/v8/V8Proxy.cpp
     bindings/v8/V8RecursionScope.cpp
     bindings/v8/V8Utilities.cpp
+    bindings/v8/V8ValueCache.cpp
     bindings/v8/V8WindowErrorHandler.cpp
     bindings/v8/V8WorkerContextErrorHandler.cpp
     bindings/v8/V8WorkerContextEventListener.cpp

Modified: trunk/Source/WebCore/WebCore.gypi (124928 => 124929)


--- trunk/Source/WebCore/WebCore.gypi	2012-08-07 22:39:26 UTC (rev 124928)
+++ trunk/Source/WebCore/WebCore.gypi	2012-08-07 22:53:23 UTC (rev 124929)
@@ -2282,6 +2282,8 @@
             'bindings/v8/V8RecursionScope.h',
             'bindings/v8/V8Utilities.cpp',
             'bindings/v8/V8Utilities.h',
+            'bindings/v8/V8ValueCache.cpp',
+            'bindings/v8/V8ValueCache.h',
             'bindings/v8/V8WindowErrorHandler.cpp',
             'bindings/v8/V8WindowErrorHandler.h',
             'bindings/v8/V8WorkerContextErrorHandler.cpp',

Modified: trunk/Source/WebCore/bindings/v8/V8Binding.cpp (124928 => 124929)


--- trunk/Source/WebCore/bindings/v8/V8Binding.cpp	2012-08-07 22:39:26 UTC (rev 124928)
+++ trunk/Source/WebCore/bindings/v8/V8Binding.cpp	2012-08-07 22:53:23 UTC (rev 124929)
@@ -103,92 +103,6 @@
         info.addInstrumentedMember(m_domDataList[i]);
 }
 
-// WebCoreStringResource is a helper class for v8ExternalString. It is used
-// to manage the life-cycle of the underlying buffer of the external string.
-class WebCoreStringResource : public v8::String::ExternalStringResource {
-public:
-    explicit WebCoreStringResource(const String& string)
-        : m_plainString(string)
-    {
-#ifndef NDEBUG
-        m_threadId = WTF::currentThread();
-#endif
-        ASSERT(!string.isNull());
-        v8::V8::AdjustAmountOfExternalAllocatedMemory(2 * string.length());
-    }
-
-    explicit WebCoreStringResource(const AtomicString& string)
-        : m_plainString(string.string())
-        , m_atomicString(string)
-    {
-#ifndef NDEBUG
-        m_threadId = WTF::currentThread();
-#endif
-        ASSERT(!string.isNull());
-        v8::V8::AdjustAmountOfExternalAllocatedMemory(2 * string.length());
-    }
-
-    virtual ~WebCoreStringResource()
-    {
-#ifndef NDEBUG
-        ASSERT(m_threadId == WTF::currentThread());
-#endif
-        int reducedExternalMemory = -2 * m_plainString.length();
-        if (m_plainString.impl() != m_atomicString.impl() && !m_atomicString.isNull())
-            reducedExternalMemory *= 2;
-        v8::V8::AdjustAmountOfExternalAllocatedMemory(reducedExternalMemory);
-    }
-
-    virtual const uint16_t* data() const
-    {
-        return reinterpret_cast<const uint16_t*>(m_plainString.impl()->characters());
-    }
-
-    virtual size_t length() const { return m_plainString.impl()->length(); }
-
-    String webcoreString() { return m_plainString; }
-
-    AtomicString atomicString()
-    {
-#ifndef NDEBUG
-        ASSERT(m_threadId == WTF::currentThread());
-#endif
-        if (m_atomicString.isNull()) {
-            m_atomicString = AtomicString(m_plainString);
-            ASSERT(!m_atomicString.isNull());
-            if (m_plainString.impl() != m_atomicString.impl())
-                v8::V8::AdjustAmountOfExternalAllocatedMemory(2 * m_atomicString.length());
-        }
-        return m_atomicString;
-    }
-
-    void visitStrings(ExternalStringVisitor* visitor)
-    {
-        visitor->visitJSExternalString(m_plainString.impl());
-        if (m_plainString.impl() != m_atomicString.impl() && !m_atomicString.isNull())
-            visitor->visitJSExternalString(m_atomicString.impl());
-    }
-
-    static WebCoreStringResource* toStringResource(v8::Handle<v8::String> v8String)
-    {
-        return static_cast<WebCoreStringResource*>(v8String->GetExternalStringResource());
-    }
-
-private:
-    // A shallow copy of the string. Keeps the string buffer alive until the V8 engine garbage collects it.
-    String m_plainString;
-    // If this string is atomic or has been made atomic earlier the
-    // atomic string is held here. In the case where the string starts
-    // off non-atomic and becomes atomic later it is necessary to keep
-    // the original string alive because v8 may keep derived pointers
-    // into that string.
-    AtomicString m_atomicString;
-
-#ifndef NDEBUG
-    WTF::ThreadIdentifier m_threadId;
-#endif
-};
-
 #if ENABLE(INSPECTOR)
 void V8BindingPerIsolateData::visitExternalStrings(ExternalStringVisitor* visitor)
 {
@@ -446,88 +360,6 @@
     return String::number(value);
 }
 
-static v8::Local<v8::String> makeExternalString(const String& string)
-{
-    WebCoreStringResource* stringResource = new WebCoreStringResource(string);
-    v8::Local<v8::String> newString = v8::String::NewExternal(stringResource);
-    if (newString.IsEmpty())
-        delete stringResource;
-
-    return newString;
-}
-
-static void cachedStringCallback(v8::Persistent<v8::Value> wrapper, void* parameter)
-{
-    StringImpl* stringImpl = static_cast<StringImpl*>(parameter);
-    V8BindingPerIsolateData::current()->stringCache()->remove(stringImpl);
-    wrapper.Dispose();
-    stringImpl->deref();
-}
-
-void StringCache::remove(StringImpl* stringImpl) 
-{
-    ASSERT(m_stringCache.contains(stringImpl));
-    m_stringCache.remove(stringImpl);
-    // Make sure that already disposed m_lastV8String is not used in
-    // StringCache::v8ExternalString().
-    if (m_lastStringImpl.get() == stringImpl)
-        m_lastStringImpl = 0;
-}
-
-v8::Local<v8::String> StringCache::v8ExternalStringSlow(StringImpl* stringImpl, v8::Isolate* isolate)
-{
-    if (!stringImpl->length())
-        return isolate ? v8::String::Empty(isolate) : v8::String::Empty();
-
-    v8::String* cachedV8String = m_stringCache.get(stringImpl);
-    if (cachedV8String) {
-        v8::Persistent<v8::String> handle(cachedV8String);
-        if (!handle.IsNearDeath() && !handle.IsEmpty()) {
-            m_lastStringImpl = stringImpl;
-            m_lastV8String = handle;
-            return v8::Local<v8::String>::New(handle);
-        }
-    }
-
-    v8::Local<v8::String> newString = makeExternalString(String(stringImpl));
-    if (newString.IsEmpty())
-        return newString;
-
-    v8::Persistent<v8::String> wrapper = v8::Persistent<v8::String>::New(newString);
-    if (wrapper.IsEmpty())
-        return newString;
-
-    stringImpl->ref();
-    wrapper.MarkIndependent();
-    wrapper.MakeWeak(stringImpl, cachedStringCallback);
-    m_stringCache.set(stringImpl, *wrapper);
-
-    m_lastStringImpl = stringImpl;
-    m_lastV8String = wrapper;
-
-    return newString;
-}
-
-void IntegerCache::createSmallIntegers()
-{
-    ASSERT(!m_initialized);
-    // We initialize m_smallIntegers not in a constructor but in v8Integer(),
-    // because Integer::New() requires a HandleScope. At the point where
-    // IntegerCache is constructed, a HandleScope might not exist.
-    for (int value = 0; value < numberOfCachedSmallIntegers; value++)
-        m_smallIntegers[value] = v8::Persistent<v8::Integer>::New(v8::Integer::New(value));
-    m_initialized = true;
-}
-
-IntegerCache::~IntegerCache()
-{
-    if (m_initialized) {
-        for (int value = 0; value < numberOfCachedSmallIntegers; value++)
-            m_smallIntegers[value].Dispose();
-        m_initialized = false;
-    }
-}
-
 v8::Persistent<v8::FunctionTemplate> createRawTemplate()
 {
     v8::HandleScope scope;

Modified: trunk/Source/WebCore/bindings/v8/V8Binding.h (124928 => 124929)


--- trunk/Source/WebCore/bindings/v8/V8Binding.h	2012-08-07 22:39:26 UTC (rev 124928)
+++ trunk/Source/WebCore/bindings/v8/V8Binding.h	2012-08-07 22:53:23 UTC (rev 124929)
@@ -41,6 +41,7 @@
 #include "V8GCController.h"
 #include "V8HiddenPropertyName.h"
 #include "V8Proxy.h"
+#include "V8ValueCache.h"
 #include <wtf/MathExtras.h>
 #include <wtf/Noncopyable.h>
 #include <wtf/text/AtomicString.h>
@@ -53,75 +54,6 @@
     class EventTarget;
     class ExternalStringVisitor;
     class MemoryObjectInfo;
-
-    class StringCache {
-    public:
-        StringCache() { }
-
-        v8::Local<v8::String> v8ExternalString(StringImpl* stringImpl, v8::Isolate* isolate)
-        {
-            if (m_lastStringImpl.get() == stringImpl) {
-                ASSERT(!m_lastV8String.IsNearDeath());
-                ASSERT(!m_lastV8String.IsEmpty());
-                return v8::Local<v8::String>::New(m_lastV8String);
-            }
-
-            return v8ExternalStringSlow(stringImpl, isolate);
-        }
-
-        void clearOnGC() 
-        {
-            m_lastStringImpl = 0;
-            m_lastV8String.Clear();
-        }
-
-        void remove(StringImpl*);
-
-        void reportMemoryUsage(MemoryObjectInfo*) const;
-
-    private:
-        v8::Local<v8::String> v8ExternalStringSlow(StringImpl*, v8::Isolate*);
-
-        HashMap<StringImpl*, v8::String*> m_stringCache;
-        v8::Persistent<v8::String> m_lastV8String;
-        // Note: RefPtr is a must as we cache by StringImpl* equality, not identity
-        // hence lastStringImpl might be not a key of the cache (in sense of identity)
-        // and hence it's not refed on addition.
-        RefPtr<StringImpl> m_lastStringImpl;
-    };
-
-    const int numberOfCachedSmallIntegers = 64;
-
-    class IntegerCache {
-    public:
-        IntegerCache() : m_initialized(false) { };
-        ~IntegerCache();
-
-        v8::Handle<v8::Integer> v8Integer(int value)
-        {
-            if (!m_initialized)
-                createSmallIntegers();
-            if (0 <= value && value < numberOfCachedSmallIntegers)
-                return m_smallIntegers[value];
-            return v8::Integer::New(value);
-        }
-
-        v8::Handle<v8::Integer> v8UnsignedInteger(unsigned value)
-        {
-            if (!m_initialized)
-                createSmallIntegers();
-            if (value < static_cast<unsigned>(numberOfCachedSmallIntegers))
-                return m_smallIntegers[value];
-            return v8::Integer::NewFromUnsigned(value);
-        }
-
-    private:
-        void createSmallIntegers();
-
-        v8::Persistent<v8::Integer> m_smallIntegers[numberOfCachedSmallIntegers];
-        bool m_initialized;
-    };
-
     class ScriptGCEventListener;
 
     class GCEventData {

Added: trunk/Source/WebCore/bindings/v8/V8ValueCache.cpp (0 => 124929)


--- trunk/Source/WebCore/bindings/v8/V8ValueCache.cpp	                        (rev 0)
+++ trunk/Source/WebCore/bindings/v8/V8ValueCache.cpp	2012-08-07 22:53:23 UTC (rev 124929)
@@ -0,0 +1,123 @@
+/*
+ * Copyright (C) 2009 Google Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "V8ValueCache.h"
+
+#include "BindingVisitors.h"
+#include "V8Binding.h"
+
+namespace WebCore {
+
+static v8::Local<v8::String> makeExternalString(const String& string)
+{
+    WebCoreStringResource* stringResource = new WebCoreStringResource(string);
+    v8::Local<v8::String> newString = v8::String::NewExternal(stringResource);
+    if (newString.IsEmpty())
+        delete stringResource;
+
+    return newString;
+}
+
+static void cachedStringCallback(v8::Persistent<v8::Value> wrapper, void* parameter)
+{
+    StringImpl* stringImpl = static_cast<StringImpl*>(parameter);
+    V8BindingPerIsolateData::current()->stringCache()->remove(stringImpl);
+    wrapper.Dispose();
+    stringImpl->deref();
+}
+
+void StringCache::remove(StringImpl* stringImpl) 
+{
+    ASSERT(m_stringCache.contains(stringImpl));
+    m_stringCache.remove(stringImpl);
+    // Make sure that already disposed m_lastV8String is not used in
+    // StringCache::v8ExternalString().
+    if (m_lastStringImpl.get() == stringImpl)
+        m_lastStringImpl = 0;
+}
+
+v8::Local<v8::String> StringCache::v8ExternalStringSlow(StringImpl* stringImpl, v8::Isolate* isolate)
+{
+    if (!stringImpl->length())
+        return isolate ? v8::String::Empty(isolate) : v8::String::Empty();
+
+    v8::String* cachedV8String = m_stringCache.get(stringImpl);
+    if (cachedV8String) {
+        v8::Persistent<v8::String> handle(cachedV8String);
+        if (!handle.IsNearDeath() && !handle.IsEmpty()) {
+            m_lastStringImpl = stringImpl;
+            m_lastV8String = handle;
+            return v8::Local<v8::String>::New(handle);
+        }
+    }
+
+    v8::Local<v8::String> newString = makeExternalString(String(stringImpl));
+    if (newString.IsEmpty())
+        return newString;
+
+    v8::Persistent<v8::String> wrapper = v8::Persistent<v8::String>::New(newString);
+    if (wrapper.IsEmpty())
+        return newString;
+
+    stringImpl->ref();
+    wrapper.MarkIndependent();
+    wrapper.MakeWeak(stringImpl, cachedStringCallback);
+    m_stringCache.set(stringImpl, *wrapper);
+
+    m_lastStringImpl = stringImpl;
+    m_lastV8String = wrapper;
+
+    return newString;
+}
+
+void WebCoreStringResource::visitStrings(ExternalStringVisitor* visitor)
+{
+    visitor->visitJSExternalString(m_plainString.impl());
+    if (m_plainString.impl() != m_atomicString.impl() && !m_atomicString.isNull())
+        visitor->visitJSExternalString(m_atomicString.impl());
+}
+
+void IntegerCache::createSmallIntegers()
+{
+    ASSERT(!m_initialized);
+    // We initialize m_smallIntegers not in a constructor but in v8Integer(),
+    // because Integer::New() requires a HandleScope. At the point where
+    // IntegerCache is constructed, a HandleScope might not exist.
+    for (int value = 0; value < numberOfCachedSmallIntegers; value++)
+        m_smallIntegers[value] = v8::Persistent<v8::Integer>::New(v8::Integer::New(value));
+    m_initialized = true;
+}
+
+IntegerCache::~IntegerCache()
+{
+    if (m_initialized) {
+        for (int value = 0; value < numberOfCachedSmallIntegers; value++)
+            m_smallIntegers[value].Dispose();
+        m_initialized = false;
+    }
+}
+
+} // namespace WebCore

Added: trunk/Source/WebCore/bindings/v8/V8ValueCache.h (0 => 124929)


--- trunk/Source/WebCore/bindings/v8/V8ValueCache.h	                        (rev 0)
+++ trunk/Source/WebCore/bindings/v8/V8ValueCache.h	2012-08-07 22:53:23 UTC (rev 124929)
@@ -0,0 +1,190 @@
+/*
+ * Copyright (C) 2009 Google Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef V8ValueCache_h
+#define V8ValueCache_h
+
+#include <v8.h>
+#include <wtf/HashMap.h>
+#include <wtf/RefPtr.h>
+#include <wtf/text/AtomicString.h>
+#include <wtf/text/WTFString.h>
+
+namespace WebCore {
+
+class ExternalStringVisitor;
+class MemoryObjectInfo;
+
+class StringCache {
+public:
+    StringCache() { }
+
+    v8::Local<v8::String> v8ExternalString(StringImpl* stringImpl, v8::Isolate* isolate)
+    {
+        if (m_lastStringImpl.get() == stringImpl) {
+            ASSERT(!m_lastV8String.IsNearDeath());
+            ASSERT(!m_lastV8String.IsEmpty());
+            return v8::Local<v8::String>::New(m_lastV8String);
+        }
+
+        return v8ExternalStringSlow(stringImpl, isolate);
+    }
+
+    void clearOnGC() 
+    {
+        m_lastStringImpl = 0;
+        m_lastV8String.Clear();
+    }
+
+    void remove(StringImpl*);
+    void reportMemoryUsage(MemoryObjectInfo*) const;
+
+private:
+    v8::Local<v8::String> v8ExternalStringSlow(StringImpl*, v8::Isolate*);
+
+    HashMap<StringImpl*, v8::String*> m_stringCache;
+    v8::Persistent<v8::String> m_lastV8String;
+    // Note: RefPtr is a must as we cache by StringImpl* equality, not identity
+    // hence lastStringImpl might be not a key of the cache (in sense of identity)
+    // and hence it's not refed on addition.
+    RefPtr<StringImpl> m_lastStringImpl;
+};
+
+const int numberOfCachedSmallIntegers = 64;
+
+// WebCoreStringResource is a helper class for v8ExternalString. It is used
+// to manage the life-cycle of the underlying buffer of the external string.
+class WebCoreStringResource : public v8::String::ExternalStringResource {
+public:
+    explicit WebCoreStringResource(const String& string)
+        : m_plainString(string)
+    {
+#ifndef NDEBUG
+        m_threadId = WTF::currentThread();
+#endif
+        ASSERT(!string.isNull());
+        v8::V8::AdjustAmountOfExternalAllocatedMemory(2 * string.length());
+    }
+
+    explicit WebCoreStringResource(const AtomicString& string)
+        : m_plainString(string.string())
+        , m_atomicString(string)
+    {
+#ifndef NDEBUG
+        m_threadId = WTF::currentThread();
+#endif
+        ASSERT(!string.isNull());
+        v8::V8::AdjustAmountOfExternalAllocatedMemory(2 * string.length());
+    }
+
+    virtual ~WebCoreStringResource()
+    {
+#ifndef NDEBUG
+        ASSERT(m_threadId == WTF::currentThread());
+#endif
+        int reducedExternalMemory = -2 * m_plainString.length();
+        if (m_plainString.impl() != m_atomicString.impl() && !m_atomicString.isNull())
+            reducedExternalMemory *= 2;
+        v8::V8::AdjustAmountOfExternalAllocatedMemory(reducedExternalMemory);
+    }
+
+    virtual const uint16_t* data() const
+    {
+        return reinterpret_cast<const uint16_t*>(m_plainString.impl()->characters());
+    }
+
+    virtual size_t length() const { return m_plainString.impl()->length(); }
+
+    String webcoreString() { return m_plainString; }
+
+    AtomicString atomicString()
+    {
+#ifndef NDEBUG
+        ASSERT(m_threadId == WTF::currentThread());
+#endif
+        if (m_atomicString.isNull()) {
+            m_atomicString = AtomicString(m_plainString);
+            ASSERT(!m_atomicString.isNull());
+            if (m_plainString.impl() != m_atomicString.impl())
+                v8::V8::AdjustAmountOfExternalAllocatedMemory(2 * m_atomicString.length());
+        }
+        return m_atomicString;
+    }
+
+    void visitStrings(ExternalStringVisitor*);
+
+    static WebCoreStringResource* toStringResource(v8::Handle<v8::String> v8String)
+    {
+        return static_cast<WebCoreStringResource*>(v8String->GetExternalStringResource());
+    }
+
+private:
+    // A shallow copy of the string. Keeps the string buffer alive until the V8 engine garbage collects it.
+    String m_plainString;
+    // If this string is atomic or has been made atomic earlier the
+    // atomic string is held here. In the case where the string starts
+    // off non-atomic and becomes atomic later it is necessary to keep
+    // the original string alive because v8 may keep derived pointers
+    // into that string.
+    AtomicString m_atomicString;
+
+#ifndef NDEBUG
+    WTF::ThreadIdentifier m_threadId;
+#endif
+};
+
+class IntegerCache {
+public:
+     IntegerCache() : m_initialized(false) { };
+    ~IntegerCache();
+
+    v8::Handle<v8::Integer> v8Integer(int value)
+    {
+        if (!m_initialized)
+            createSmallIntegers();
+        if (0 <= value && value < numberOfCachedSmallIntegers)
+            return m_smallIntegers[value];
+        return v8::Integer::New(value);
+    }
+
+    v8::Handle<v8::Integer> v8UnsignedInteger(unsigned value)
+    {
+        if (!m_initialized)
+            createSmallIntegers();
+        if (value < static_cast<unsigned>(numberOfCachedSmallIntegers))
+            return m_smallIntegers[value];
+        return v8::Integer::NewFromUnsigned(value);
+    }
+
+private:
+    void createSmallIntegers();
+
+    v8::Persistent<v8::Integer> m_smallIntegers[numberOfCachedSmallIntegers];
+    bool m_initialized;
+};
+    
+} // namespace WebCore
+
+#endif // V8ValueCache_h
_______________________________________________
webkit-changes mailing list
[email protected]
http://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to