2 * Copyright (C) 2013 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 are
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
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.
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.
31 #ifndef UnsafePersistent_h
32 #define UnsafePersistent_h
34 #include "bindings/v8/WrapperTypeInfo.h"
40 template<class KeyType> class DOMWrapperMap;
42 // An unsafe way to pass Persistent handles around. Do not use unless you know
43 // what you're doing. UnsafePersistent is only safe to use when we know that the
44 // memory pointed by the it is not going away: 1) When GC cannot happen while
45 // the UnsafePersistent is alive or 2) when there is a strong Persistent keeping
46 // the memory alive while the UnsafePersistent is alive.
47 template<typename T> class UnsafePersistent {
49 UnsafePersistent() : m_value(0) { }
50 explicit UnsafePersistent(T* value) : m_value(value) { }
51 explicit UnsafePersistent(v8::Persistent<T>& handle)
53 m_value = handle.ClearAndLeak();
56 UnsafePersistent(v8::Isolate* isolate, v8::Handle<T>& handle)
58 v8::Persistent<T> persistent(isolate, handle);
59 m_value = persistent.ClearAndLeak();
67 template<typename V8T, typename U>
68 inline bool setReturnValueWithSecurityCheck(v8::ReturnValue<v8::Value> returnValue, U* object)
70 // Security: always guard against malicious tampering.
71 RELEASE_ASSERT_WITH_SECURITY_IMPLICATION(isEmpty() || value()->GetAlignedPointerFromInternalField(v8DOMWrapperObjectIndex) == V8T::toInternalPointer(object));
72 returnValue.Set(*persistent());
76 inline bool setReturnValue(v8::ReturnValue<v8::Value> returnValue)
78 returnValue.Set(*persistent());
82 // This is incredibly unsafe: the handle is valid only when this
83 // UnsafePersistent is alive and valid (see class level comment).
84 v8::Persistent<T>* persistent()
86 v8::Persistent<T>* handle = reinterpret_cast<v8::Persistent<T>*>(&m_value);
90 void setReferenceFrom(const v8::Persistent<v8::Object>& parent, v8::Isolate* isolate)
92 isolate->SetReference(parent, *persistent());
97 persistent()->Reset();
106 v8::Local<T> newLocal(v8::Isolate* isolate)
108 return v8::Local<T>::New(isolate, *persistent());
120 } // namespace WebCore
122 #endif // UnsafePersistent_h