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 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 V8DOMWrapper_h
32 #define V8DOMWrapper_h
34 #include "bindings/v8/DOMDataStore.h"
36 #include "wtf/PassRefPtr.h"
37 #include "wtf/text/AtomicString.h"
41 struct WrapperTypeInfo;
46 // Checks if a v8 value can be a DOM wrapper
47 static bool maybeDOMWrapper(v8::Handle<v8::Value>);
50 static v8::Local<v8::Object> createWrapper(v8::Handle<v8::Object> creationContext, const WrapperTypeInfo*, void*, v8::Isolate*);
52 template<typename V8T, typename T>
53 static inline v8::Handle<v8::Object> associateObjectWithWrapper(PassRefPtr<T>, const WrapperTypeInfo*, v8::Handle<v8::Object>, v8::Isolate*, WrapperConfiguration::Lifetime);
54 static inline void setNativeInfo(v8::Handle<v8::Object>, const WrapperTypeInfo*, void*);
55 static inline void clearNativeInfo(v8::Handle<v8::Object>, const WrapperTypeInfo*);
57 static bool isDOMWrapper(v8::Handle<v8::Value>);
58 static bool isWrapperOfType(v8::Handle<v8::Value>, const WrapperTypeInfo*);
61 inline void V8DOMWrapper::setNativeInfo(v8::Handle<v8::Object> wrapper, const WrapperTypeInfo* type, void* object)
63 ASSERT(wrapper->InternalFieldCount() >= 2);
66 wrapper->SetAlignedPointerInInternalField(v8DOMWrapperObjectIndex, object);
67 wrapper->SetAlignedPointerInInternalField(v8DOMWrapperTypeIndex, const_cast<WrapperTypeInfo*>(type));
70 inline void V8DOMWrapper::clearNativeInfo(v8::Handle<v8::Object> wrapper, const WrapperTypeInfo* type)
72 ASSERT(wrapper->InternalFieldCount() >= 2);
74 wrapper->SetAlignedPointerInInternalField(v8DOMWrapperTypeIndex, const_cast<WrapperTypeInfo*>(type));
75 wrapper->SetAlignedPointerInInternalField(v8DOMWrapperObjectIndex, 0);
78 template<typename V8T, typename T>
79 inline v8::Handle<v8::Object> V8DOMWrapper::associateObjectWithWrapper(PassRefPtr<T> object, const WrapperTypeInfo* type, v8::Handle<v8::Object> wrapper, v8::Isolate* isolate, WrapperConfiguration::Lifetime lifetime)
81 setNativeInfo(wrapper, type, V8T::toInternalPointer(object.get()));
82 ASSERT(maybeDOMWrapper(wrapper));
83 WrapperConfiguration configuration = buildWrapperConfiguration(object.get(), lifetime);
84 DOMDataStore::setWrapper<V8T>(object.leakRef(), wrapper, isolate, configuration);
88 class V8WrapperInstantiationScope {
90 V8WrapperInstantiationScope(v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
91 : m_didEnterContext(false)
92 , m_context(isolate->GetCurrentContext())
94 // FIXME: Remove all empty creationContexts from caller sites.
95 // If a creationContext is empty, we will end up creating a new object
96 // in the context currently entered. This is wrong.
97 if (creationContext.IsEmpty())
99 v8::Handle<v8::Context> contextForWrapper = creationContext->CreationContext();
100 // For performance, we enter the context only if the currently running context
101 // is different from the context that we are about to enter.
102 if (contextForWrapper == m_context)
104 m_context = v8::Local<v8::Context>::New(isolate, contextForWrapper);
105 m_didEnterContext = true;
109 ~V8WrapperInstantiationScope()
111 if (!m_didEnterContext)
116 v8::Handle<v8::Context> context() const { return m_context; }
119 bool m_didEnterContext;
120 v8::Handle<v8::Context> m_context;
125 #endif // V8DOMWrapper_h