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 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 V8DOMWrapper_h
     32 #define V8DOMWrapper_h
     33 
     34 #include "bindings/v8/DOMDataStore.h"
     35 #include <v8.h>
     36 #include "wtf/PassRefPtr.h"
     37 #include "wtf/text/AtomicString.h"
     38 
     39 namespace WebCore {
     40 
     41 struct WrapperTypeInfo;
     42 
     43     class V8DOMWrapper {
     44     public:
     45 #ifndef NDEBUG
     46         // Checks if a v8 value can be a DOM wrapper
     47         static bool maybeDOMWrapper(v8::Handle<v8::Value>);
     48 #endif
     49 
     50         static v8::Local<v8::Object> createWrapper(v8::Handle<v8::Object> creationContext, WrapperTypeInfo*, void*, v8::Isolate*);
     51 
     52         template<typename V8T, typename T>
     53         static inline v8::Handle<v8::Object> associateObjectWithWrapper(PassRefPtr<T>, WrapperTypeInfo*, v8::Handle<v8::Object>, v8::Isolate*, WrapperConfiguration::Lifetime);
     54         static inline void setNativeInfo(v8::Handle<v8::Object>, WrapperTypeInfo*, void*);
     55         static inline void clearNativeInfo(v8::Handle<v8::Object>, WrapperTypeInfo*);
     56 
     57         static bool isDOMWrapper(v8::Handle<v8::Value>);
     58         static bool isWrapperOfType(v8::Handle<v8::Value>, WrapperTypeInfo*);
     59     };
     60 
     61     inline void V8DOMWrapper::setNativeInfo(v8::Handle<v8::Object> wrapper, WrapperTypeInfo* type, void* object)
     62     {
     63         ASSERT(wrapper->InternalFieldCount() >= 2);
     64         ASSERT(object);
     65         ASSERT(type);
     66         wrapper->SetAlignedPointerInInternalField(v8DOMWrapperObjectIndex, object);
     67         wrapper->SetAlignedPointerInInternalField(v8DOMWrapperTypeIndex, type);
     68     }
     69 
     70     inline void V8DOMWrapper::clearNativeInfo(v8::Handle<v8::Object> wrapper, WrapperTypeInfo* type)
     71     {
     72         ASSERT(wrapper->InternalFieldCount() >= 2);
     73         ASSERT(type);
     74         wrapper->SetAlignedPointerInInternalField(v8DOMWrapperTypeIndex, type);
     75         wrapper->SetAlignedPointerInInternalField(v8DOMWrapperObjectIndex, 0);
     76     }
     77 
     78     template<typename V8T, typename T>
     79     inline v8::Handle<v8::Object> V8DOMWrapper::associateObjectWithWrapper(PassRefPtr<T> object, WrapperTypeInfo* type, v8::Handle<v8::Object> wrapper, v8::Isolate* isolate, WrapperConfiguration::Lifetime lifetime)
     80     {
     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);
     85         return wrapper;
     86     }
     87 
     88 }
     89 
     90 #endif // V8DOMWrapper_h
     91