2 * Copyright (C) 2012 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
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #ifndef V8ArrayBufferCustom_h
27 #define V8ArrayBufferCustom_h
29 #include "bindings/core/v8/V8Binding.h"
30 #include "bindings/core/v8/V8DOMWrapper.h"
31 #include "bindings/core/v8/WrapperTypeInfo.h"
32 #include "wtf/ArrayBuffer.h"
37 class V8ArrayBufferDeallocationObserver FINAL: public WTF::ArrayBufferDeallocationObserver {
39 virtual void arrayBufferDeallocated(unsigned sizeInBytes) OVERRIDE
41 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(-static_cast<int>(sizeInBytes));
43 static V8ArrayBufferDeallocationObserver* instanceTemplate();
46 virtual void blinkAllocatedMemory(unsigned sizeInBytes) OVERRIDE
48 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(static_cast<int>(sizeInBytes));
54 static bool hasInstance(v8::Handle<v8::Value>, v8::Isolate*);
55 static ArrayBuffer* toImpl(v8::Handle<v8::Object>);
56 static ArrayBuffer* toImplWithTypeCheck(v8::Isolate*, v8::Handle<v8::Value>);
57 static void refObject(ScriptWrappableBase* internalPointer);
58 static void derefObject(ScriptWrappableBase* internalPointer);
59 static WrapperPersistentNode* createPersistentHandle(ScriptWrappableBase* internalPointer);
60 static const WrapperTypeInfo wrapperTypeInfo;
61 static const int internalFieldCount = v8DefaultWrapperInternalFieldCount;
63 static inline ScriptWrappableBase* toScriptWrappableBase(ArrayBuffer* impl)
65 return reinterpret_cast<ScriptWrappableBase*>(impl);
68 static inline ArrayBuffer* toImpl(ScriptWrappableBase* internalPointer)
70 return reinterpret_cast<ArrayBuffer*>(internalPointer);
74 friend v8::Handle<v8::Object> wrap(ArrayBuffer*, v8::Handle<v8::Object> creationContext, v8::Isolate*);
75 static v8::Handle<v8::Object> createWrapper(PassRefPtr<ArrayBuffer>, v8::Handle<v8::Object> creationContext, v8::Isolate*);
78 inline v8::Handle<v8::Object> wrap(ArrayBuffer* impl, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
81 ASSERT(!DOMDataStore::containsWrapper<V8ArrayBuffer>(impl, isolate));
82 return V8ArrayBuffer::createWrapper(impl, creationContext, isolate);
85 inline v8::Handle<v8::Value> toV8(ArrayBuffer* impl, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
88 return v8::Null(isolate);
89 v8::Handle<v8::Value> wrapper = DOMDataStore::getWrapper<V8ArrayBuffer>(impl, isolate);
90 if (!wrapper.IsEmpty())
92 return wrap(impl, creationContext, isolate);
95 template<class CallbackInfo>
96 inline void v8SetReturnValue(const CallbackInfo& info, ArrayBuffer* impl)
98 if (UNLIKELY(!impl)) {
99 v8SetReturnValueNull(info);
102 if (DOMDataStore::setReturnValueFromWrapper<V8ArrayBuffer>(info.GetReturnValue(), impl))
104 v8::Handle<v8::Object> wrapper = wrap(impl, info.Holder(), info.GetIsolate());
105 v8SetReturnValue(info, wrapper);
108 template<class CallbackInfo>
109 inline void v8SetReturnValueForMainWorld(const CallbackInfo& info, ArrayBuffer* impl)
111 ASSERT(DOMWrapperWorld::current(info.GetIsolate()).isMainWorld());
112 if (UNLIKELY(!impl)) {
113 v8SetReturnValueNull(info);
116 if (DOMDataStore::setReturnValueFromWrapperForMainWorld<V8ArrayBuffer>(info.GetReturnValue(), impl))
118 v8::Handle<v8::Value> wrapper = wrap(impl, info.Holder(), info.GetIsolate());
119 v8SetReturnValue(info, wrapper);
122 template<class CallbackInfo, class Wrappable>
123 inline void v8SetReturnValueFast(const CallbackInfo& info, ArrayBuffer* impl, Wrappable* wrappable)
125 if (UNLIKELY(!impl)) {
126 v8SetReturnValueNull(info);
129 if (DOMDataStore::setReturnValueFromWrapperFast<V8ArrayBuffer>(info.GetReturnValue(), impl, info.Holder(), wrappable))
131 v8::Handle<v8::Object> wrapper = wrap(impl, info.Holder(), info.GetIsolate());
132 v8SetReturnValue(info, wrapper);
135 inline v8::Handle<v8::Value> toV8(PassRefPtr< ArrayBuffer > impl, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
137 return toV8(impl.get(), creationContext, isolate);
140 template<class CallbackInfo>
141 inline void v8SetReturnValue(const CallbackInfo& info, PassRefPtr< ArrayBuffer > impl)
143 v8SetReturnValue(info, impl.get());
146 template<class CallbackInfo>
147 inline void v8SetReturnValueForMainWorld(const CallbackInfo& info, PassRefPtr< ArrayBuffer > impl)
149 v8SetReturnValueForMainWorld(info, impl.get());
152 template<class CallbackInfo, class Wrappable>
153 inline void v8SetReturnValueFast(const CallbackInfo& info, PassRefPtr< ArrayBuffer > impl, Wrappable* wrappable)
155 v8SetReturnValueFast(info, impl.get(), wrappable);
160 #endif // V8ArrayBufferCustom_h