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 #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*) | |
51 { | |
52 return TypedArrayTraits<TypedArray>::IsInstance(value); | |
53 } | |
54 | |
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; | |
60 | |
61 static v8::Handle<v8::Object> wrap(TypedArray* impl, v8::Handle<v8::Object>
creationContext, v8::Isolate* isolate) | |
62 { | |
63 ASSERT(impl); | |
64 ASSERT(!DOMDataStore::containsWrapper<Binding>(impl, isolate)); | |
65 return V8TypedArray<TypedArray>::createWrapper(impl, creationContext, is
olate); | |
66 } | |
67 | |
68 static v8::Handle<v8::Value> toV8(TypedArray* impl, v8::Handle<v8::Object> c
reationContext, v8::Isolate* isolate) | |
69 { | |
70 if (UNLIKELY(!impl)) | |
71 return v8::Null(isolate); | |
72 v8::Handle<v8::Value> wrapper = DOMDataStore::getWrapper<Binding>(impl,
isolate); | |
73 if (!wrapper.IsEmpty()) | |
74 return wrapper; | |
75 return wrap(impl, creationContext, isolate); | |
76 } | |
77 | |
78 template<typename CallbackInfo> | |
79 static void v8SetReturnValue(const CallbackInfo& info, TypedArray* impl) | |
80 { | |
81 if (UNLIKELY(!impl)) { | |
82 v8SetReturnValueNull(info); | |
83 return; | |
84 } | |
85 if (DOMDataStore::setReturnValueFromWrapper<Binding>(info.GetReturnValue
(), impl)) | |
86 return; | |
87 v8::Handle<v8::Object> wrapper = wrap(impl, info.Holder(), info.GetIsola
te()); | |
88 info.GetReturnValue().Set(wrapper); | |
89 } | |
90 | |
91 template<typename CallbackInfo> | |
92 static void v8SetReturnValueForMainWorld(const CallbackInfo& info, TypedArra
y* impl) | |
93 { | |
94 ASSERT(DOMWrapperWorld::current(info.GetIsolate()).isMainWorld()); | |
95 if (UNLIKELY(!impl)) { | |
96 v8SetReturnValueNull(info); | |
97 return; | |
98 } | |
99 if (DOMDataStore::setReturnValueFromWrapperForMainWorld<Binding>(info.Ge
tReturnValue(), impl)) | |
100 return; | |
101 v8::Handle<v8::Value> wrapper = wrap(impl, info.Holder(), info.GetIsolat
e()); | |
102 info.GetReturnValue().Set(wrapper); | |
103 } | |
104 | |
105 template<class CallbackInfo, class Wrappable> | |
106 static void v8SetReturnValueFast(const CallbackInfo& info, TypedArray* impl,
Wrappable* wrappable) | |
107 { | |
108 if (UNLIKELY(!impl)) { | |
109 v8SetReturnValueNull(info); | |
110 return; | |
111 } | |
112 if (DOMDataStore::setReturnValueFromWrapperFast<Binding>(info.GetReturnV
alue(), impl, info.Holder(), wrappable)) | |
113 return; | |
114 v8::Handle<v8::Object> wrapper = wrap(impl, info.Holder(), info.GetIsola
te()); | |
115 info.GetReturnValue().Set(wrapper); | |
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 const WrapperTypeInfo* info() { return &V8TypedArray<TypedArray>::wra
pperTypeInfo; } | |
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::containsWrapper<Binding>(impl.get(), isolate)); | |
142 | |
143 RefPtr<ArrayBuffer> buffer = impl->buffer(); | |
144 v8::Local<v8::Value> v8Buffer = WebCore::toV8(buffer.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, &wrapperTypeInfo, wr
apper, isolate, 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(), &wra
pperTypeInfo, object, v8::Isolate::GetCurrent(), WrapperConfiguration::Independe
nt); | |
167 | |
168 typedarrayPtr = object->GetAlignedPointerFromInternalField(v8DOMWrapperObjec
tIndex); | |
169 ASSERT(typedarrayPtr); | |
170 return reinterpret_cast<TypedArray*>(typedarrayPtr); | |
171 } | |
172 | |
173 template <typename TypedArray> | |
174 TypedArray* V8TypedArray<TypedArray>::toNativeWithTypeCheck(v8::Isolate* isolate
, v8::Handle<v8::Value> value) | |
175 { | |
176 return V8TypedArray<TypedArray>::hasInstance(value, isolate) ? V8TypedArray<
TypedArray>::toNative(v8::Handle<v8::Object>::Cast(value)) : 0; | |
177 } | |
178 | |
179 template <typename TypedArray> | |
180 const WrapperTypeInfo V8TypedArray<TypedArray>::wrapperTypeInfo = { | |
181 gin::kEmbedderBlink, | |
182 0, V8TypedArray<TypedArray>::derefObject, | |
183 0, 0, 0, 0, 0, WrapperTypeObjectPrototype, RefCountedObject | |
184 }; | |
185 | |
186 template <typename TypedArray> | |
187 void V8TypedArray<TypedArray>::derefObject(void* object) | |
188 { | |
189 static_cast<TypedArray*>(object)->deref(); | |
190 } | |
191 | |
192 | |
193 } // namespace WebCode | |
194 | |
195 #endif // V8TypedArrayCustom_h | |
OLD | NEW |