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 V8TypedArrayCustom_h
32 #define V8TypedArrayCustom_h
34 #include "bindings/v8/V8Binding.h"
35 #include "bindings/v8/V8DOMWrapper.h"
36 #include "bindings/v8/WrapperTypeInfo.h"
37 #include "bindings/v8/custom/V8ArrayBufferCustom.h"
38 #include "wtf/ArrayBuffer.h"
44 class TypedArrayTraits
47 template<typename TypedArray>
50 static bool hasInstance(v8::Handle<v8::Value> value, v8::Isolate*)
52 return TypedArrayTraits<TypedArray>::IsInstance(value);
55 static TypedArray* toNative(v8::Handle<v8::Object>);
56 static TypedArray* toNativeWithTypeCheck(v8::Isolate*, v8::Handle<v8::Value>);
57 static void derefObject(void*);
58 static const WrapperTypeInfo wrapperTypeInfo;
59 static const int internalFieldCount = v8DefaultWrapperInternalFieldCount;
61 static v8::Handle<v8::Object> wrap(TypedArray* impl, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
64 ASSERT(!DOMDataStore::containsWrapper<Binding>(impl, isolate));
65 return V8TypedArray<TypedArray>::createWrapper(impl, creationContext, isolate);
68 static v8::Handle<v8::Value> toV8(TypedArray* impl, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
71 return v8::Null(isolate);
72 v8::Handle<v8::Value> wrapper = DOMDataStore::getWrapper<Binding>(impl, isolate);
73 if (!wrapper.IsEmpty())
75 return wrap(impl, creationContext, isolate);
78 template<typename CallbackInfo>
79 static void v8SetReturnValue(const CallbackInfo& info, TypedArray* impl)
81 if (UNLIKELY(!impl)) {
82 v8SetReturnValueNull(info);
85 if (DOMDataStore::setReturnValueFromWrapper<Binding>(info.GetReturnValue(), impl))
87 v8::Handle<v8::Object> wrapper = wrap(impl, info.Holder(), info.GetIsolate());
88 info.GetReturnValue().Set(wrapper);
91 template<typename CallbackInfo>
92 static void v8SetReturnValueForMainWorld(const CallbackInfo& info, TypedArray* impl)
94 ASSERT(DOMWrapperWorld::current(info.GetIsolate()).isMainWorld());
95 if (UNLIKELY(!impl)) {
96 v8SetReturnValueNull(info);
99 if (DOMDataStore::setReturnValueFromWrapperForMainWorld<Binding>(info.GetReturnValue(), impl))
101 v8::Handle<v8::Value> wrapper = wrap(impl, info.Holder(), info.GetIsolate());
102 info.GetReturnValue().Set(wrapper);
105 template<class CallbackInfo, class Wrappable>
106 static void v8SetReturnValueFast(const CallbackInfo& info, TypedArray* impl, Wrappable* wrappable)
108 if (UNLIKELY(!impl)) {
109 v8SetReturnValueNull(info);
112 if (DOMDataStore::setReturnValueFromWrapperFast<Binding>(info.GetReturnValue(), impl, info.Holder(), wrappable))
114 v8::Handle<v8::Object> wrapper = wrap(impl, info.Holder(), info.GetIsolate());
115 info.GetReturnValue().Set(wrapper);
118 static inline void* toInternalPointer(TypedArray* impl)
123 typedef TypedArrayTraits<TypedArray> Traits;
124 typedef typename Traits::V8Type V8Type;
125 typedef V8TypedArray<TypedArray> Binding;
127 static v8::Handle<v8::Object> createWrapper(PassRefPtr<TypedArray>, v8::Handle<v8::Object> creationContext, v8::Isolate*);
130 template<typename TypedArray>
131 class TypedArrayWrapperTraits {
133 static const WrapperTypeInfo* info() { return &V8TypedArray<TypedArray>::wrapperTypeInfo; }
137 template <typename TypedArray>
138 v8::Handle<v8::Object> V8TypedArray<TypedArray>::createWrapper(PassRefPtr<TypedArray> impl, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
141 ASSERT(!DOMDataStore::containsWrapper<Binding>(impl.get(), isolate));
143 RefPtr<ArrayBuffer> buffer = impl->buffer();
144 v8::Local<v8::Value> v8Buffer = WebCore::toV8(buffer.get(), creationContext, isolate);
146 ASSERT(v8Buffer->IsArrayBuffer());
148 v8::Local<v8::Object> wrapper = V8Type::New(v8Buffer.As<v8::ArrayBuffer>(), impl->byteOffset(), Traits::length(impl.get()));
150 V8DOMWrapper::associateObjectWithWrapper<Binding>(impl, &wrapperTypeInfo, wrapper, isolate, WrapperConfiguration::Independent);
154 template <typename TypedArray>
155 TypedArray* V8TypedArray<TypedArray>::toNative(v8::Handle<v8::Object> object)
157 ASSERT(Traits::IsInstance(object));
158 void* typedarrayPtr = object->GetAlignedPointerFromInternalField(v8DOMWrapperObjectIndex);
160 return reinterpret_cast<TypedArray*>(typedarrayPtr);
162 v8::Handle<V8Type> view = object.As<V8Type>();
163 RefPtr<ArrayBuffer> arrayBuffer = V8ArrayBuffer::toNative(view->Buffer());
164 RefPtr<TypedArray> typedArray = TypedArray::create(arrayBuffer, view->ByteOffset(), Traits::length(view));
165 ASSERT(typedArray.get());
166 V8DOMWrapper::associateObjectWithWrapper<Binding>(typedArray.release(), &wrapperTypeInfo, object, v8::Isolate::GetCurrent(), WrapperConfiguration::Independent);
168 typedarrayPtr = object->GetAlignedPointerFromInternalField(v8DOMWrapperObjectIndex);
169 ASSERT(typedarrayPtr);
170 return reinterpret_cast<TypedArray*>(typedarrayPtr);
173 template <typename TypedArray>
174 TypedArray* V8TypedArray<TypedArray>::toNativeWithTypeCheck(v8::Isolate* isolate, v8::Handle<v8::Value> value)
176 return V8TypedArray<TypedArray>::hasInstance(value, isolate) ? V8TypedArray<TypedArray>::toNative(v8::Handle<v8::Object>::Cast(value)) : 0;
179 template <typename TypedArray>
180 const WrapperTypeInfo V8TypedArray<TypedArray>::wrapperTypeInfo = {
182 0, V8TypedArray<TypedArray>::derefObject,
183 0, 0, 0, 0, 0, WrapperTypeObjectPrototype, RefCountedObject
186 template <typename TypedArray>
187 void V8TypedArray<TypedArray>::derefObject(void* object)
189 static_cast<TypedArray*>(object)->deref();
193 } // namespace WebCode
195 #endif // V8TypedArrayCustom_h