OLD | NEW |
(Empty) | |
| 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 |
| 39 #include "wtf/ArrayBuffer.h" |
| 40 |
| 41 #include <v8.h> |
| 42 |
| 43 namespace WebCore { |
| 44 |
| 45 template<typename T> |
| 46 class TypedArrayTraits |
| 47 { }; |
| 48 |
| 49 template<typename TypedArray> |
| 50 class V8TypedArray { |
| 51 public: |
| 52 static bool HasInstance(v8::Handle<v8::Value> value, v8::Isolate*, WrapperWo
rldType) |
| 53 { |
| 54 return TypedArrayTraits<TypedArray>::IsInstance(value); |
| 55 } |
| 56 |
| 57 static bool HasInstanceInAnyWorld(v8::Handle<v8::Value> value, v8::Isolate*) |
| 58 { |
| 59 return TypedArrayTraits<TypedArray>::IsInstance(value); |
| 60 } |
| 61 |
| 62 static TypedArray* toNative(v8::Handle<v8::Object>); |
| 63 static void derefObject(void*); |
| 64 static WrapperTypeInfo info; |
| 65 static const int internalFieldCount = v8DefaultWrapperInternalFieldCount; |
| 66 |
| 67 static v8::Handle<v8::Object> wrap(TypedArray* impl, v8::Handle<v8::Object>
creationContext, v8::Isolate* isolate) |
| 68 { |
| 69 ASSERT(impl); |
| 70 ASSERT((DOMDataStore::template getWrapper<Binding>(impl, isolate).IsEmpt
y())); |
| 71 return V8TypedArray<TypedArray>::createWrapper(impl, creationContext, is
olate); |
| 72 } |
| 73 |
| 74 static v8::Handle<v8::Value> toV8(TypedArray* impl, v8::Handle<v8::Object> c
reationContext, v8::Isolate* isolate) |
| 75 { |
| 76 if (UNLIKELY(!impl)) |
| 77 return v8NullWithCheck(isolate); |
| 78 v8::Handle<v8::Value> wrapper = DOMDataStore::getWrapper<Binding>(impl,
isolate); |
| 79 if (!wrapper.IsEmpty()) |
| 80 return wrapper; |
| 81 return wrap(impl, creationContext, isolate); |
| 82 } |
| 83 |
| 84 static v8::Handle<v8::Value> toV8ForMainWorld(TypedArray* impl, v8::Handle<v
8::Object> creationContext, v8::Isolate* isolate) |
| 85 { |
| 86 ASSERT(worldType(isolate) == MainWorld); |
| 87 if (UNLIKELY(!impl)) |
| 88 return v8NullWithCheck(isolate); |
| 89 v8::Handle<v8::Value> wrapper = DOMDataStore::getWrapperForMainWorld<Bin
ding>(impl); |
| 90 if (!wrapper.IsEmpty()) |
| 91 return wrapper; |
| 92 return wrap(impl, creationContext, isolate); |
| 93 } |
| 94 |
| 95 template<class HolderContainer, class Wrappable> |
| 96 static v8::Handle<v8::Value> toV8Fast(TypedArray* impl, const HolderContaine
r& container, Wrappable* wrappable) |
| 97 { |
| 98 if (UNLIKELY(!impl)) |
| 99 return v8::Null(container.GetIsolate()); |
| 100 v8::Handle<v8::Object> wrapper = DOMDataStore::getWrapperFast<Binding>(i
mpl, container, wrappable); |
| 101 if (!wrapper.IsEmpty()) |
| 102 return wrapper; |
| 103 return wrap(impl, container.Holder(), container.GetIsolate()); |
| 104 } |
| 105 |
| 106 template<class HolderContainer, class Wrappable> |
| 107 static v8::Handle<v8::Value> toV8FastForMainWorld(TypedArray* impl, const Ho
lderContainer& container, Wrappable* wrappable) |
| 108 { |
| 109 ASSERT(worldType(container.GetIsolate()) == MainWorld); |
| 110 if (UNLIKELY(!impl)) |
| 111 return v8::Null(container.GetIsolate()); |
| 112 v8::Handle<v8::Object> wrapper = DOMDataStore::getWrapperForMainWorld<Bi
nding>(impl); |
| 113 if (!wrapper.IsEmpty()) |
| 114 return wrapper; |
| 115 return wrap(impl, container.Holder(), container.GetIsolate()); |
| 116 } |
| 117 |
| 118 static inline void* toInternalPointer(TypedArray* impl) |
| 119 { |
| 120 return impl; |
| 121 } |
| 122 private: |
| 123 typedef TypedArrayTraits<TypedArray> Traits; |
| 124 typedef typename Traits::V8Type V8Type; |
| 125 typedef V8TypedArray<TypedArray> Binding; |
| 126 |
| 127 static v8::Handle<v8::Object> createWrapper(PassRefPtr<TypedArray>, v8::Hand
le<v8::Object> creationContext, v8::Isolate*); |
| 128 }; |
| 129 |
| 130 template<typename TypedArray> |
| 131 class TypedArrayWrapperTraits { |
| 132 public: |
| 133 static WrapperTypeInfo* info() { return &V8TypedArray<TypedArray>::info; } |
| 134 }; |
| 135 |
| 136 |
| 137 template <typename TypedArray> |
| 138 v8::Handle<v8::Object> V8TypedArray<TypedArray>::createWrapper(PassRefPtr<TypedA
rray> impl, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate) |
| 139 { |
| 140 ASSERT(impl.get()); |
| 141 ASSERT(DOMDataStore::getWrapper<Binding>(impl.get(), isolate).IsEmpty()); |
| 142 |
| 143 RefPtr<ArrayBuffer> buffer = impl->buffer(); |
| 144 v8::Local<v8::Value> v8Buffer = v8::Local<v8::Value>::New(WebCore::toV8(buff
er.get(), creationContext, isolate)); |
| 145 |
| 146 ASSERT(v8Buffer->IsArrayBuffer()); |
| 147 |
| 148 v8::Local<v8::Object> wrapper = V8Type::New(v8Buffer.As<v8::ArrayBuffer>(),
impl->byteOffset(), Traits::length(impl.get())); |
| 149 |
| 150 V8DOMWrapper::associateObjectWithWrapper<Binding>(impl, &info, wrapper, isol
ate, WrapperConfiguration::Independent); |
| 151 return wrapper; |
| 152 } |
| 153 |
| 154 template <typename TypedArray> |
| 155 TypedArray* V8TypedArray<TypedArray>::toNative(v8::Handle<v8::Object> object) |
| 156 { |
| 157 ASSERT(Traits::IsInstance(object)); |
| 158 void* typedarrayPtr = object->GetAlignedPointerFromInternalField(v8DOMWrappe
rObjectIndex); |
| 159 if (typedarrayPtr) |
| 160 return reinterpret_cast<TypedArray*>(typedarrayPtr); |
| 161 |
| 162 v8::Handle<V8Type> view = object.As<V8Type>(); |
| 163 RefPtr<ArrayBuffer> arrayBuffer = V8ArrayBuffer::toNative(view->Buffer()); |
| 164 RefPtr<TypedArray> typedArray = TypedArray::create(arrayBuffer, view->ByteOf
fset(), Traits::length(view)); |
| 165 ASSERT(typedArray.get()); |
| 166 V8DOMWrapper::associateObjectWithWrapper<Binding>(typedArray.release(), &inf
o, object, v8::Isolate::GetCurrent(), WrapperConfiguration::Independent); |
| 167 |
| 168 typedarrayPtr = object->GetAlignedPointerFromInternalField(v8DOMWrapperObjec
tIndex); |
| 169 ASSERT(typedarrayPtr); |
| 170 return reinterpret_cast<TypedArray*>(typedarrayPtr); |
| 171 } |
| 172 |
| 173 |
| 174 template <typename TypedArray> |
| 175 WrapperTypeInfo V8TypedArray<TypedArray>::info = { |
| 176 0, V8TypedArray<TypedArray>::derefObject, |
| 177 0, 0, 0, 0, 0, WrapperTypeObjectPrototype |
| 178 }; |
| 179 |
| 180 template <typename TypedArray> |
| 181 void V8TypedArray<TypedArray>::derefObject(void* object) |
| 182 { |
| 183 static_cast<TypedArray*>(object)->deref(); |
| 184 } |
| 185 |
| 186 |
| 187 } // namespace WebCode |
| 188 |
| 189 #endif // V8TypedArrayCustom_h |
OLD | NEW |