Source/WebCore/ChangeLog

 12012-08-30 Elliott Sprehn <esprehn@chromium.org>
 2
 3 Add new V8DependentRetained that allows keeping a v8::Object alive as long as another v8::Object is alive
 4 https://bugs.webkit.org/show_bug.cgi?id=95519
 5
 6 Reviewed by NOBODY (OOPS!).
 7
 8 Add new V8DependentRetained that allows keeping a v8::Object alive as long as another
 9 v8::Object is alive. This is useful for keeping callbacks attached to wrappers without
 10 keeping strong references to v8::Objects in the C++ side which can result in leaks
 11 when cycles are created.
 12
 13 No new tests needed, this will be used to fix MutationObservers which will have tests.
 14
 15 * WebCore.gypi:
 16 * bindings/v8/V8DependentRetained.h: Added.
 17 (WebCore):
 18 (V8DependentRetained):
 19 (WebCore::V8DependentRetained::V8DependentRetained):
 20 (WebCore::V8DependentRetained::~V8DependentRetained):
 21 (WebCore::V8DependentRetained::get): Gets the v8::Object value.
 22 (WebCore::V8DependentRetained::isEmpty): Checks if the value is still alive.
 23 (WebCore::V8DependentRetained::createPropertyName):
 24 (WebCore::V8DependentRetained::weakCallback):
 25 (WebCore::V8DependentRetained::release):
 26 * bindings/v8/V8HiddenPropertyName.cpp:
 27 (WebCore::V8HiddenPropertyName::hiddenReferenceName): Modified to allow creating hidden String or Symbol names.
 28 * bindings/v8/V8HiddenPropertyName.h:
 29 (V8HiddenPropertyName):
 30
1312012-08-24 Robert Hogan <robert@webkit.org>
232
333 CSS 2.1 failure: margin-collapse-clear-012 fails

Source/WebCore/WebCore.gypi

22662266 'bindings/v8/V8Callback.h',
22672267 'bindings/v8/V8Collection.cpp',
22682268 'bindings/v8/V8Collection.h',
 2269 'bindings/v8/V8DependentRetained.h',
22692270 'bindings/v8/V8DOMConfiguration.cpp',
22702271 'bindings/v8/V8DOMConfiguration.h',
22712272 'bindings/v8/V8DOMMap.cpp',

Source/WebCore/bindings/v8/V8DependentRetained.h

 1/*
 2 * Copyright (C) 2012 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 are
 6 * met:
 7 *
 8 * * Redistributions of source code must retain the above copyright
 9 * notice, this list of conditions and the following disclaimer.
 10 * * Redistributions in binary form must reproduce the above
 11 * copyright notice, this list of conditions and the following disclaimer
 12 * in the documentation and/or other materials provided with the
 13 * distribution.
 14 * * Neither the name of Google Inc. nor the names of its
 15 * contributors may be used to endorse or promote products derived from
 16 * this software without specific prior written permission.
 17 *
 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 29 */
 30
 31#ifndef V8DependentRetained_h
 32#define V8DependentRetained_h
 33
 34#include "ScopedPersistent.h"
 35#include "V8HiddenPropertyName.h"
 36#include <v8.h>
 37#include <wtf/Forward.h>
 38#include <wtf/MainThread.h>
 39#include <wtf/dtoa.h>
 40
 41namespace WebCore {
 42
 43#define V8_DEPENDENT_RETAINED "V8DependentRetained"
 44
 45class V8DependentRetained {
 46public:
 47 V8DependentRetained(v8::Handle<v8::Object> owner, v8::Handle<v8::Object> value)
 48 : m_owner(owner)
 49 , m_propertyName(createPropertyName())
 50 {
 51 ASSERT(!owner.IsEmpty());
 52 ASSERT(!value.IsEmpty());
 53 owner->SetHiddenValue(m_propertyName.get(), value);
 54 m_owner.get().MakeWeak(this, &V8DependentRetained::weakCallback);
 55 }
 56
 57 ~V8DependentRetained()
 58 {
 59 release();
 60 }
 61
 62 v8::Local<v8::Object> get() const
 63 {
 64 return m_owner->GetHiddenValue(m_propertyName.get()).As<v8::Object>();
 65 }
 66
 67 bool isEmpty() const
 68 {
 69 return m_owner.isEmpty() || get().IsEmpty();
 70 }
 71
 72private:
 73 static v8::Persistent<v8::String> createPropertyName()
 74 {
 75 ASSERT(isMainThread());
 76 static double nextId = 0;
 77 v8::HandleScope scope;
 78 NumberToStringBuffer buffer;
 79 Vector<char, 32> name;
 80 const char* id = numberToString(nextId++, buffer);
 81 name.append(V8_DEPENDENT_RETAINED, sizeof(V8_DEPENDENT_RETAINED) - 1);
 82 name.append(id, strlen(id));
 83 return v8::Persistent<v8::String>::New(V8HiddenPropertyName::hiddenReferenceName(name.data(), NewString));
 84 }
 85
 86 static void weakCallback(v8::Persistent<v8::Value> object, void* parameter)
 87 {
 88 V8DependentRetained* value = static_cast<V8DependentRetained*>(parameter);
 89 value->release();
 90 }
 91
 92 void release()
 93 {
 94 if (!m_owner.isEmpty())
 95 m_owner->DeleteHiddenValue(m_propertyName.get());
 96 m_owner.clear();
 97 }
 98
 99 ScopedPersistent<v8::Object> m_owner;
 100 ScopedPersistent<v8::String> m_propertyName;
 101};
 102
 103} // namespace WebCore
 104
 105#endif // V8DependentRetained_h

Source/WebCore/bindings/v8/V8HiddenPropertyName.cpp

@@v8::Handle<v8::String> V8HiddenPropertyName::name() \
5555
5656V8_HIDDEN_PROPERTIES(V8_DEFINE_HIDDEN_PROPERTY);
5757
58 v8::Handle<v8::String> V8HiddenPropertyName::hiddenReferenceName(const char* name)
 58v8::Handle<v8::String> V8HiddenPropertyName::hiddenReferenceName(const char* name, V8HiddenPropertyCreationType type)
5959{
 60 ASSERT(name && strlen(name));
6061 Vector<char, 64> prefixedName;
6162 prefixedName.append(V8_HIDDEN_PROPERTY_PREFIX, sizeof(V8_HIDDEN_PROPERTY_PREFIX) - 1);
62  ASSERT(name && strlen(name));
6363 prefixedName.append(name, strlen(name));
64  return v8::String::NewSymbol(prefixedName.data(), static_cast<int>(prefixedName.size()));
 64 switch (type) {
 65 case NewSymbol:
 66 return v8::String::NewSymbol(prefixedName.data(), static_cast<int>(prefixedName.size()));
 67 case NewString:
 68 return v8::String::New(prefixedName.data(), static_cast<int>(prefixedName.size()));
 69 }
 70 ASSERT_NOT_REACHED();
 71 return v8::Handle<v8::String>();
6572}
6673
6774v8::Persistent<v8::String> V8HiddenPropertyName::createString(const char* key)

Source/WebCore/bindings/v8/V8HiddenPropertyName.h

@@namespace WebCore {
5050 V(textTracks) \
5151 V(toStringString)
5252
 53 enum V8HiddenPropertyCreationType { NewSymbol, NewString };
 54
5355 class V8HiddenPropertyName {
5456 public:
5557 V8HiddenPropertyName() { }

@@namespace WebCore {
5759 V8_HIDDEN_PROPERTIES(V8_DECLARE_PROPERTY);
5860#undef V8_DECLARE_PROPERTY
5961
60  static v8::Handle<v8::String> hiddenReferenceName(const char* name);
 62 static v8::Handle<v8::String> hiddenReferenceName(const char* name, V8HiddenPropertyCreationType type = NewSymbol);
6163
6264 private:
6365 static v8::Persistent<v8::String> createString(const char* key);