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