1 /* 2 * Copyright (C) 2013 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 V8TypedArrayCustom_h 32 #define V8TypedArrayCustom_h 33 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" 39 #include <v8.h> 40 41 namespace WebCore { 42 43 template<typename T> 44 class TypedArrayTraits 45 { }; 46 47 template<typename TypedArray> 48 class V8TypedArray { 49 public: 50 static bool hasInstance(v8::Handle<v8::Value> value, v8::Isolate*, WrapperWorldType) 51 { 52 return TypedArrayTraits<TypedArray>::IsInstance(value); 53 } 54 55 static bool hasInstanceInAnyWorld(v8::Handle<v8::Value> value, v8::Isolate*) 56 { 57 return TypedArrayTraits<TypedArray>::IsInstance(value); 58 } 59 60 static TypedArray* toNative(v8::Handle<v8::Object>); 61 static void derefObject(void*); 62 static const WrapperTypeInfo wrapperTypeInfo; 63 static const int internalFieldCount = v8DefaultWrapperInternalFieldCount; 64 65 static v8::Handle<v8::Object> wrap(TypedArray* impl, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate) 66 { 67 ASSERT(impl); 68 ASSERT(!DOMDataStore::containsWrapper<Binding>(impl, isolate)); 69 return V8TypedArray<TypedArray>::createWrapper(impl, creationContext, isolate); 70 } 71 72 static v8::Handle<v8::Value> toV8(TypedArray* impl, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate) 73 { 74 if (UNLIKELY(!impl)) 75 return v8::Null(isolate); 76 v8::Handle<v8::Value> wrapper = DOMDataStore::getWrapper<Binding>(impl, isolate); 77 if (!wrapper.IsEmpty()) 78 return wrapper; 79 return wrap(impl, creationContext, isolate); 80 } 81 82 template<typename CallbackInfo> 83 static void v8SetReturnValue(const CallbackInfo& info, TypedArray* impl) 84 { 85 if (UNLIKELY(!impl)) { 86 v8SetReturnValueNull(info); 87 return; 88 } 89 if (DOMDataStore::setReturnValueFromWrapper<Binding>(info.GetReturnValue(), impl)) 90 return; 91 v8::Handle<v8::Object> wrapper = wrap(impl, info.Holder(), info.GetIsolate()); 92 info.GetReturnValue().Set(wrapper); 93 } 94 95 template<typename CallbackInfo> 96 static void v8SetReturnValueForMainWorld(const CallbackInfo& info, TypedArray* impl) 97 { 98 ASSERT(worldType(info.GetIsolate()) == MainWorld); 99 if (UNLIKELY(!impl)) { 100 v8SetReturnValueNull(info); 101 return; 102 } 103 if (DOMDataStore::setReturnValueFromWrapperForMainWorld<Binding>(info.GetReturnValue(), impl)) 104 return; 105 v8::Handle<v8::Value> wrapper = wrap(impl, info.Holder(), info.GetIsolate()); 106 info.GetReturnValue().Set(wrapper); 107 } 108 109 template<class CallbackInfo, class Wrappable> 110 static void v8SetReturnValueFast(const CallbackInfo& info, TypedArray* impl, Wrappable* wrappable) 111 { 112 if (UNLIKELY(!impl)) { 113 v8SetReturnValueNull(info); 114 return; 115 } 116 if (DOMDataStore::setReturnValueFromWrapperFast<Binding>(info.GetReturnValue(), impl, info.Holder(), wrappable)) 117 return; 118 v8::Handle<v8::Object> wrapper = wrap(impl, info.Holder(), info.GetIsolate()); 119 info.GetReturnValue().Set(wrapper); 120 } 121 122 static inline void* toInternalPointer(TypedArray* impl) 123 { 124 return impl; 125 } 126 private: 127 typedef TypedArrayTraits<TypedArray> Traits; 128 typedef typename Traits::V8Type V8Type; 129 typedef V8TypedArray<TypedArray> Binding; 130 131 static v8::Handle<v8::Object> createWrapper(PassRefPtr<TypedArray>, v8::Handle<v8::Object> creationContext, v8::Isolate*); 132 }; 133 134 template<typename TypedArray> 135 class TypedArrayWrapperTraits { 136 public: 137 static const WrapperTypeInfo* info() { return &V8TypedArray<TypedArray>::wrapperTypeInfo; } 138 }; 139 140 141 template <typename TypedArray> 142 v8::Handle<v8::Object> V8TypedArray<TypedArray>::createWrapper(PassRefPtr<TypedArray> impl, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate) 143 { 144 ASSERT(impl.get()); 145 ASSERT(!DOMDataStore::containsWrapper<Binding>(impl.get(), isolate)); 146 147 RefPtr<ArrayBuffer> buffer = impl->buffer(); 148 v8::Local<v8::Value> v8Buffer = WebCore::toV8(buffer.get(), creationContext, isolate); 149 150 ASSERT(v8Buffer->IsArrayBuffer()); 151 152 v8::Local<v8::Object> wrapper = V8Type::New(v8Buffer.As<v8::ArrayBuffer>(), impl->byteOffset(), Traits::length(impl.get())); 153 154 V8DOMWrapper::associateObjectWithWrapper<Binding>(impl, &wrapperTypeInfo, wrapper, isolate, WrapperConfiguration::Independent); 155 return wrapper; 156 } 157 158 template <typename TypedArray> 159 TypedArray* V8TypedArray<TypedArray>::toNative(v8::Handle<v8::Object> object) 160 { 161 ASSERT(Traits::IsInstance(object)); 162 void* typedarrayPtr = object->GetAlignedPointerFromInternalField(v8DOMWrapperObjectIndex); 163 if (typedarrayPtr) 164 return reinterpret_cast<TypedArray*>(typedarrayPtr); 165 166 v8::Handle<V8Type> view = object.As<V8Type>(); 167 RefPtr<ArrayBuffer> arrayBuffer = V8ArrayBuffer::toNative(view->Buffer()); 168 RefPtr<TypedArray> typedArray = TypedArray::create(arrayBuffer, view->ByteOffset(), Traits::length(view)); 169 ASSERT(typedArray.get()); 170 V8DOMWrapper::associateObjectWithWrapper<Binding>(typedArray.release(), &wrapperTypeInfo, object, v8::Isolate::GetCurrent(), WrapperConfiguration::Independent); 171 172 typedarrayPtr = object->GetAlignedPointerFromInternalField(v8DOMWrapperObjectIndex); 173 ASSERT(typedarrayPtr); 174 return reinterpret_cast<TypedArray*>(typedarrayPtr); 175 } 176 177 178 template <typename TypedArray> 179 const WrapperTypeInfo V8TypedArray<TypedArray>::wrapperTypeInfo = { 180 gin::kEmbedderBlink, 181 0, V8TypedArray<TypedArray>::derefObject, 182 0, 0, 0, 0, 0, WrapperTypeObjectPrototype 183 }; 184 185 template <typename TypedArray> 186 void V8TypedArray<TypedArray>::derefObject(void* object) 187 { 188 static_cast<TypedArray*>(object)->deref(); 189 } 190 191 192 } // namespace WebCode 193 194 #endif // V8TypedArrayCustom_h 195