OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2009, 2011 Google Inc. All rights reserved. | 2 * Copyright (C) 2009, 2011 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 29 matching lines...) Expand all Loading... |
40 | 40 |
41 namespace WebCore { | 41 namespace WebCore { |
42 | 42 |
43 const char tooLargeSize[] = "Size is too large (or is negative)."; | 43 const char tooLargeSize[] = "Size is too large (or is negative)."; |
44 const char outOfRangeLengthAndOffset[] = "Index is out of range."; | 44 const char outOfRangeLengthAndOffset[] = "Index is out of range."; |
45 | 45 |
46 // Copy the elements from the source array to the typed destination array. | 46 // Copy the elements from the source array to the typed destination array. |
47 // Returns true if it succeeded, otherwise returns false. | 47 // Returns true if it succeeded, otherwise returns false. |
48 bool copyElements(v8::Handle<v8::Object> destArray, v8::Handle<v8::Object> srcAr
ray, uint32_t length, uint32_t offset, v8::Isolate*); | 48 bool copyElements(v8::Handle<v8::Object> destArray, v8::Handle<v8::Object> srcAr
ray, uint32_t length, uint32_t offset, v8::Isolate*); |
49 | 49 |
50 template<class ArrayClass> | 50 template<class JavaScriptWrapperArrayType, class ArrayClass> |
51 void wrapArrayBufferView(const v8::FunctionCallbackInfo<v8::Value>& args, Wrappe
rTypeInfo* type, ArrayClass array, v8::ExternalArrayType arrayType, bool hasInde
xer) | 51 void wrapArrayBufferView(const v8::FunctionCallbackInfo<v8::Value>& args, Wrappe
rTypeInfo* type, ArrayClass array, v8::ExternalArrayType arrayType, bool hasInde
xer) |
52 { | 52 { |
53 // Transform the holder into a wrapper object for the array. | 53 // Transform the holder into a wrapper object for the array. |
54 ASSERT(!hasIndexer || static_cast<int32_t>(array.get()->length()) >= 0); | 54 ASSERT(!hasIndexer || static_cast<int32_t>(array.get()->length()) >= 0); |
55 if (hasIndexer) | 55 if (hasIndexer) |
56 args.Holder()->SetIndexedPropertiesToExternalArrayData(array.get()->base
Address(), arrayType, array.get()->length()); | 56 args.Holder()->SetIndexedPropertiesToExternalArrayData(array.get()->base
Address(), arrayType, array.get()->length()); |
57 v8::Handle<v8::Object> wrapper = args.Holder(); | 57 v8::Handle<v8::Object> wrapper = args.Holder(); |
58 V8DOMWrapper::associateObjectWithWrapper(array.release(), type, wrapper, arg
s.GetIsolate(), WrapperConfiguration::Independent); | 58 V8DOMWrapper::associateObjectWithWrapper<JavaScriptWrapperArrayType>(array.r
elease(), type, wrapper, args.GetIsolate(), WrapperConfiguration::Independent); |
59 args.GetReturnValue().Set(wrapper); | 59 args.GetReturnValue().Set(wrapper); |
60 } | 60 } |
61 | 61 |
62 // Template function used by the ArrayBufferView*Constructor callbacks. | 62 // Template function used by the ArrayBufferView*Constructor callbacks. |
63 template<class ArrayClass, class ElementType> | 63 template<class ArrayClass, class ElementType, class JavaScriptWrapperArrayType> |
64 void constructWebGLArrayWithArrayBufferArgument(const v8::FunctionCallbackInfo<v
8::Value>& args, WrapperTypeInfo* type, v8::ExternalArrayType arrayType, bool ha
sIndexer) | 64 void constructWebGLArrayWithArrayBufferArgument(const v8::FunctionCallbackInfo<v
8::Value>& args, WrapperTypeInfo* type, v8::ExternalArrayType arrayType, bool ha
sIndexer) |
65 { | 65 { |
66 ArrayBuffer* buf = V8ArrayBuffer::toNative(args[0]->ToObject()); | 66 ArrayBuffer* buf = V8ArrayBuffer::toNative(args[0]->ToObject()); |
67 if (!buf) { | 67 if (!buf) { |
68 throwTypeError("Could not convert argument 0 to a ArrayBuffer", args.Get
Isolate()); | 68 throwTypeError("Could not convert argument 0 to a ArrayBuffer", args.Get
Isolate()); |
69 return; | 69 return; |
70 } | 70 } |
71 bool ok; | 71 bool ok; |
72 uint32_t offset = 0; | 72 uint32_t offset = 0; |
73 int argLen = args.Length(); | 73 int argLen = args.Length(); |
(...skipping 23 matching lines...) Expand all Loading... |
97 throwError(v8RangeError, tooLargeSize, args.GetIsolate()); | 97 throwError(v8RangeError, tooLargeSize, args.GetIsolate()); |
98 return; | 98 return; |
99 } | 99 } |
100 | 100 |
101 RefPtr<ArrayClass> array = ArrayClass::create(buf, offset, length); | 101 RefPtr<ArrayClass> array = ArrayClass::create(buf, offset, length); |
102 if (!array) { | 102 if (!array) { |
103 throwError(v8RangeError, tooLargeSize, args.GetIsolate()); | 103 throwError(v8RangeError, tooLargeSize, args.GetIsolate()); |
104 return; | 104 return; |
105 } | 105 } |
106 | 106 |
107 wrapArrayBufferView(args, type, array, arrayType, hasIndexer); | 107 wrapArrayBufferView<JavaScriptWrapperArrayType>(args, type, array, arrayType
, hasIndexer); |
108 } | 108 } |
109 | 109 |
110 // Template function used by the ArrayBufferView*Constructor callbacks. | 110 // Template function used by the ArrayBufferView*Constructor callbacks. |
111 template<class ArrayClass, class JavaScriptWrapperArrayType, class ElementType> | 111 template<class ArrayClass, class JavaScriptWrapperArrayType, class ElementType> |
112 void constructWebGLArray(const v8::FunctionCallbackInfo<v8::Value>& args, Wrappe
rTypeInfo* type, v8::ExternalArrayType arrayType) | 112 void constructWebGLArray(const v8::FunctionCallbackInfo<v8::Value>& args, Wrappe
rTypeInfo* type, v8::ExternalArrayType arrayType) |
113 { | 113 { |
114 if (!args.IsConstructCall()) { | 114 if (!args.IsConstructCall()) { |
115 throwTypeError("DOM object constructor cannot be called as a function.",
args.GetIsolate()); | 115 throwTypeError("DOM object constructor cannot be called as a function.",
args.GetIsolate()); |
116 return; | 116 return; |
117 } | 117 } |
(...skipping 10 matching lines...) Expand all Loading... |
128 // The V8DOMWrapper will set the internal pointer in the | 128 // The V8DOMWrapper will set the internal pointer in the |
129 // created object. Unfortunately it doesn't look like it's | 129 // created object. Unfortunately it doesn't look like it's |
130 // possible to distinguish between this case and that where | 130 // possible to distinguish between this case and that where |
131 // the user calls "new <Type>Array()" from JavaScript. We must | 131 // the user calls "new <Type>Array()" from JavaScript. We must |
132 // construct an empty view to avoid crashes when fetching the | 132 // construct an empty view to avoid crashes when fetching the |
133 // length. | 133 // length. |
134 RefPtr<ArrayClass> array = ArrayClass::create(0); | 134 RefPtr<ArrayClass> array = ArrayClass::create(0); |
135 // Do not call SetIndexedPropertiesToExternalArrayData on this | 135 // Do not call SetIndexedPropertiesToExternalArrayData on this |
136 // object. Not only is there no point from a performance | 136 // object. Not only is there no point from a performance |
137 // perspective, but doing so causes errors in the subset() case. | 137 // perspective, but doing so causes errors in the subset() case. |
138 wrapArrayBufferView(args, type, array, arrayType, false); | 138 wrapArrayBufferView<JavaScriptWrapperArrayType>(args, type, array, array
Type, false); |
139 return; | 139 return; |
140 } | 140 } |
141 | 141 |
142 // Supported constructors: | 142 // Supported constructors: |
143 // WebGL<T>Array(n) where n is an integer: | 143 // WebGL<T>Array(n) where n is an integer: |
144 // -- create an empty array of n elements | 144 // -- create an empty array of n elements |
145 // WebGL<T>Array(arr) where arr is an array: | 145 // WebGL<T>Array(arr) where arr is an array: |
146 // -- create a WebGL<T>Array containing the contents of "arr" | 146 // -- create a WebGL<T>Array containing the contents of "arr" |
147 // WebGL<T>Array(buf, offset, length) | 147 // WebGL<T>Array(buf, offset, length) |
148 // -- create a WebGL<T>Array pointing to the ArrayBuffer | 148 // -- create a WebGL<T>Array pointing to the ArrayBuffer |
149 // "buf", starting at the specified offset, for the given | 149 // "buf", starting at the specified offset, for the given |
150 // length | 150 // length |
151 | 151 |
152 if (args[0]->IsNull()) { | 152 if (args[0]->IsNull()) { |
153 // Invalid first argument | 153 // Invalid first argument |
154 throwTypeError(0, args.GetIsolate()); | 154 throwTypeError(0, args.GetIsolate()); |
155 return; | 155 return; |
156 } | 156 } |
157 | 157 |
158 // See whether the first argument is a ArrayBuffer. | 158 // See whether the first argument is a ArrayBuffer. |
159 if (V8ArrayBuffer::HasInstance(args[0], args.GetIsolate(), worldType(args.Ge
tIsolate()))) { | 159 if (V8ArrayBuffer::HasInstance(args[0], args.GetIsolate(), worldType(args.Ge
tIsolate()))) { |
160 constructWebGLArrayWithArrayBufferArgument<ArrayClass, ElementType>(args
, type, arrayType, true); | 160 constructWebGLArrayWithArrayBufferArgument<ArrayClass, ElementType, Java
ScriptWrapperArrayType>(args, type, arrayType, true); |
161 return; | 161 return; |
162 } | 162 } |
163 | 163 |
164 // See whether the first argument is the same type as impl. In that case, | 164 // See whether the first argument is the same type as impl. In that case, |
165 // we can simply memcpy data from source to impl. | 165 // we can simply memcpy data from source to impl. |
166 if (JavaScriptWrapperArrayType::HasInstance(args[0], args.GetIsolate(), worl
dType(args.GetIsolate()))) { | 166 if (JavaScriptWrapperArrayType::HasInstance(args[0], args.GetIsolate(), worl
dType(args.GetIsolate()))) { |
167 ArrayClass* source = JavaScriptWrapperArrayType::toNative(args[0]->ToObj
ect()); | 167 ArrayClass* source = JavaScriptWrapperArrayType::toNative(args[0]->ToObj
ect()); |
168 uint32_t length = source->length(); | 168 uint32_t length = source->length(); |
169 | 169 |
170 if (static_cast<int32_t>(length) < 0) { | 170 if (static_cast<int32_t>(length) < 0) { |
171 throwError(v8RangeError, tooLargeSize, args.GetIsolate()); | 171 throwError(v8RangeError, tooLargeSize, args.GetIsolate()); |
172 return; | 172 return; |
173 } | 173 } |
174 | 174 |
175 RefPtr<ArrayClass> array = ArrayClass::createUninitialized(length); | 175 RefPtr<ArrayClass> array = ArrayClass::createUninitialized(length); |
176 if (!array.get()) { | 176 if (!array.get()) { |
177 throwError(v8RangeError, tooLargeSize, args.GetIsolate()); | 177 throwError(v8RangeError, tooLargeSize, args.GetIsolate()); |
178 return; | 178 return; |
179 } | 179 } |
180 | 180 |
181 array->buffer()->setDeallocationObserver(V8ArrayBufferDeallocationObserv
er::instance()); | 181 array->buffer()->setDeallocationObserver(V8ArrayBufferDeallocationObserv
er::instance()); |
182 v8::V8::AdjustAmountOfExternalAllocatedMemory(array->byteLength()); | 182 v8::V8::AdjustAmountOfExternalAllocatedMemory(array->byteLength()); |
183 | 183 |
184 memcpy(array->baseAddress(), source->baseAddress(), length * sizeof(Elem
entType)); | 184 memcpy(array->baseAddress(), source->baseAddress(), length * sizeof(Elem
entType)); |
185 | 185 |
186 wrapArrayBufferView(args, type, array, arrayType, true); | 186 wrapArrayBufferView<JavaScriptWrapperArrayType>(args, type, array, array
Type, true); |
187 return; | 187 return; |
188 } | 188 } |
189 | 189 |
190 uint32_t len = 0; | 190 uint32_t len = 0; |
191 v8::Handle<v8::Object> srcArray; | 191 v8::Handle<v8::Object> srcArray; |
192 bool doInstantiation = false; | 192 bool doInstantiation = false; |
193 | 193 |
194 if (args[0]->IsObject()) { | 194 if (args[0]->IsObject()) { |
195 srcArray = args[0]->ToObject(); | 195 srcArray = args[0]->ToObject(); |
196 if (srcArray.IsEmpty()) { | 196 if (srcArray.IsEmpty()) { |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 if (val.IsEmpty()) { | 248 if (val.IsEmpty()) { |
249 // Exception thrown during fetch. | 249 // Exception thrown during fetch. |
250 return; | 250 return; |
251 } | 251 } |
252 array->set(i, val->NumberValue()); | 252 array->set(i, val->NumberValue()); |
253 } | 253 } |
254 } | 254 } |
255 } | 255 } |
256 | 256 |
257 v8::Handle<v8::Object> wrapper = args.Holder(); | 257 v8::Handle<v8::Object> wrapper = args.Holder(); |
258 V8DOMWrapper::associateObjectWithWrapper(array.release(), type, wrapper, arg
s.GetIsolate(), WrapperConfiguration::Independent); | 258 V8DOMWrapper::associateObjectWithWrapper<JavaScriptWrapperArrayType>(array.r
elease(), type, wrapper, args.GetIsolate(), WrapperConfiguration::Independent); |
259 args.GetReturnValue().Set(wrapper); | 259 args.GetReturnValue().Set(wrapper); |
260 } | 260 } |
261 | 261 |
262 template <class CPlusPlusArrayType, class JavaScriptWrapperArrayType> | 262 template <class CPlusPlusArrayType, class JavaScriptWrapperArrayType> |
263 void setWebGLArrayHelper(const v8::FunctionCallbackInfo<v8::Value>& args) | 263 void setWebGLArrayHelper(const v8::FunctionCallbackInfo<v8::Value>& args) |
264 { | 264 { |
265 if (args.Length() < 1) { | 265 if (args.Length() < 1) { |
266 throwNotEnoughArgumentsError(args.GetIsolate()); | 266 throwNotEnoughArgumentsError(args.GetIsolate()); |
267 return; | 267 return; |
268 } | 268 } |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
300 } | 300 } |
301 return; | 301 return; |
302 } | 302 } |
303 | 303 |
304 throwTypeError("Invalid argument", args.GetIsolate()); | 304 throwTypeError("Invalid argument", args.GetIsolate()); |
305 } | 305 } |
306 | 306 |
307 } | 307 } |
308 | 308 |
309 #endif // V8ArrayBufferViewCustom_h | 309 #endif // V8ArrayBufferViewCustom_h |
OLD | NEW |