b5d3bb20d4e62e60ff754ee068e620d1b21373ad
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / v8 / custom / V8WebGLRenderingContextCustom.cpp
1 /*
2  * Copyright (C) 2009 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 #include "config.h"
32 #include "V8WebGLRenderingContext.h"
33
34 #include "V8ANGLEInstancedArrays.h"
35 #include "V8EXTFragDepth.h"
36 #include "V8EXTTextureFilterAnisotropic.h"
37 #include "V8HTMLCanvasElement.h"
38 #include "V8HTMLImageElement.h"
39 #include "V8HTMLVideoElement.h"
40 #include "V8ImageData.h"
41 #include "V8OESElementIndexUint.h"
42 #include "V8OESStandardDerivatives.h"
43 #include "V8OESTextureFloat.h"
44 #include "V8OESTextureFloatLinear.h"
45 #include "V8OESTextureHalfFloat.h"
46 #include "V8OESTextureHalfFloatLinear.h"
47 #include "V8OESVertexArrayObject.h"
48 #include "V8WebGLBuffer.h"
49 #include "V8WebGLCompressedTextureATC.h"
50 #include "V8WebGLCompressedTexturePVRTC.h"
51 #include "V8WebGLCompressedTextureS3TC.h"
52 #include "V8WebGLDebugRendererInfo.h"
53 #include "V8WebGLDebugShaders.h"
54 #include "V8WebGLDepthTexture.h"
55 #include "V8WebGLDrawBuffers.h"
56 #include "V8WebGLFramebuffer.h"
57 #include "V8WebGLLoseContext.h"
58 #include "V8WebGLProgram.h"
59 #include "V8WebGLRenderbuffer.h"
60 #include "V8WebGLShader.h"
61 #include "V8WebGLTexture.h"
62 #include "V8WebGLUniformLocation.h"
63 #include "V8WebGLVertexArrayObjectOES.h"
64 #include "bindings/v8/ExceptionMessages.h"
65 #include "bindings/v8/V8Binding.h"
66 #include "bindings/v8/V8HiddenValue.h"
67 #include "bindings/v8/custom/V8ArrayBufferViewCustom.h"
68 #include "bindings/v8/custom/V8Float32ArrayCustom.h"
69 #include "bindings/v8/custom/V8Int16ArrayCustom.h"
70 #include "bindings/v8/custom/V8Int32ArrayCustom.h"
71 #include "bindings/v8/custom/V8Int8ArrayCustom.h"
72 #include "bindings/v8/custom/V8Uint16ArrayCustom.h"
73 #include "bindings/v8/custom/V8Uint32ArrayCustom.h"
74 #include "bindings/v8/custom/V8Uint8ArrayCustom.h"
75 #include "core/dom/ExceptionCode.h"
76 #include "core/html/canvas/WebGLRenderingContext.h"
77 #include "platform/NotImplemented.h"
78 #include "wtf/FastMalloc.h"
79 #include <limits>
80
81 namespace WebCore {
82
83 // Allocates new storage via fastMalloc.
84 // Returns 0 if array failed to convert for any reason.
85 static float* jsArrayToFloatArray(v8::Handle<v8::Array> array, uint32_t len, ExceptionState& exceptionState)
86 {
87     // Convert the data element-by-element.
88     if (len > std::numeric_limits<uint32_t>::max() / sizeof(float)) {
89         exceptionState.throwTypeError("Array length exceeds supported limit.");
90         return 0;
91     }
92     float* data = static_cast<float*>(fastMalloc(len * sizeof(float)));
93
94     for (uint32_t i = 0; i < len; i++) {
95         v8::Local<v8::Value> val = array->Get(i);
96         float value = toFloat(val, exceptionState);
97         if (exceptionState.hadException()) {
98             fastFree(data);
99             return 0;
100         }
101         data[i] = value;
102     }
103     return data;
104 }
105
106 // Allocates new storage via fastMalloc.
107 // Returns 0 if array failed to convert for any reason.
108 static int* jsArrayToIntArray(v8::Handle<v8::Array> array, uint32_t len, ExceptionState& exceptionState)
109 {
110     // Convert the data element-by-element.
111     if (len > std::numeric_limits<uint32_t>::max() / sizeof(int)) {
112         exceptionState.throwTypeError("Array length exceeds supported limit.");
113         return 0;
114     }
115     int* data = static_cast<int*>(fastMalloc(len * sizeof(int)));
116
117     for (uint32_t i = 0; i < len; i++) {
118         v8::Local<v8::Value> val = array->Get(i);
119         int ival = toInt32(val, exceptionState);
120         if (exceptionState.hadException()) {
121             fastFree(data);
122             return 0;
123         }
124         data[i] = ival;
125     }
126     return data;
127 }
128
129 static v8::Handle<v8::Value> toV8Object(const WebGLGetInfo& args, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
130 {
131     switch (args.getType()) {
132     case WebGLGetInfo::kTypeBool:
133         return v8Boolean(args.getBool(), isolate);
134     case WebGLGetInfo::kTypeBoolArray: {
135         const Vector<bool>& value = args.getBoolArray();
136         v8::Local<v8::Array> array = v8::Array::New(isolate, value.size());
137         for (size_t ii = 0; ii < value.size(); ++ii)
138             array->Set(v8::Integer::New(isolate, ii), v8Boolean(value[ii], isolate));
139         return array;
140     }
141     case WebGLGetInfo::kTypeFloat:
142         return v8::Number::New(isolate, args.getFloat());
143     case WebGLGetInfo::kTypeInt:
144         return v8::Integer::New(isolate, args.getInt());
145     case WebGLGetInfo::kTypeNull:
146         return v8::Null(isolate);
147     case WebGLGetInfo::kTypeString:
148         return v8String(isolate, args.getString());
149     case WebGLGetInfo::kTypeUnsignedInt:
150         return v8::Integer::NewFromUnsigned(isolate, args.getUnsignedInt());
151     case WebGLGetInfo::kTypeWebGLBuffer:
152         return toV8(args.getWebGLBuffer(), creationContext, isolate);
153     case WebGLGetInfo::kTypeWebGLFloatArray:
154         return toV8(args.getWebGLFloatArray(), creationContext, isolate);
155     case WebGLGetInfo::kTypeWebGLFramebuffer:
156         return toV8(args.getWebGLFramebuffer(), creationContext, isolate);
157     case WebGLGetInfo::kTypeWebGLIntArray:
158         return toV8(args.getWebGLIntArray(), creationContext, isolate);
159     // FIXME: implement WebGLObjectArray
160     // case WebGLGetInfo::kTypeWebGLObjectArray:
161     case WebGLGetInfo::kTypeWebGLProgram:
162         return toV8(args.getWebGLProgram(), creationContext, isolate);
163     case WebGLGetInfo::kTypeWebGLRenderbuffer:
164         return toV8(args.getWebGLRenderbuffer(), creationContext, isolate);
165     case WebGLGetInfo::kTypeWebGLTexture:
166         return toV8(args.getWebGLTexture(), creationContext, isolate);
167     case WebGLGetInfo::kTypeWebGLUnsignedByteArray:
168         return toV8(args.getWebGLUnsignedByteArray(), creationContext, isolate);
169     case WebGLGetInfo::kTypeWebGLUnsignedIntArray:
170         return toV8(args.getWebGLUnsignedIntArray(), creationContext, isolate);
171     case WebGLGetInfo::kTypeWebGLVertexArrayObjectOES:
172         return toV8(args.getWebGLVertexArrayObjectOES(), creationContext, isolate);
173     default:
174         notImplemented();
175         return v8::Undefined(isolate);
176     }
177 }
178
179 static v8::Handle<v8::Value> toV8Object(WebGLExtension* extension, v8::Handle<v8::Object> contextObject, v8::Isolate* isolate)
180 {
181     if (!extension)
182         return v8::Null(isolate);
183     v8::Handle<v8::Value> extensionObject;
184     const char* referenceName = 0;
185     switch (extension->name()) {
186     case ANGLEInstancedArraysName:
187         extensionObject = toV8(static_cast<ANGLEInstancedArrays*>(extension), contextObject, isolate);
188         referenceName = "angleInstancedArraysName";
189         break;
190     case EXTFragDepthName:
191         extensionObject = toV8(static_cast<EXTFragDepth*>(extension), contextObject, isolate);
192         referenceName = "extFragDepthName";
193         break;
194     case EXTTextureFilterAnisotropicName:
195         extensionObject = toV8(static_cast<EXTTextureFilterAnisotropic*>(extension), contextObject, isolate);
196         referenceName = "extTextureFilterAnisotropicName";
197         break;
198     case OESElementIndexUintName:
199         extensionObject = toV8(static_cast<OESElementIndexUint*>(extension), contextObject, isolate);
200         referenceName = "oesElementIndexUintName";
201         break;
202     case OESStandardDerivativesName:
203         extensionObject = toV8(static_cast<OESStandardDerivatives*>(extension), contextObject, isolate);
204         referenceName = "oesStandardDerivativesName";
205         break;
206     case OESTextureFloatName:
207         extensionObject = toV8(static_cast<OESTextureFloat*>(extension), contextObject, isolate);
208         referenceName = "oesTextureFloatName";
209         break;
210     case OESTextureFloatLinearName:
211         extensionObject = toV8(static_cast<OESTextureFloatLinear*>(extension), contextObject, isolate);
212         referenceName = "oesTextureFloatLinearName";
213         break;
214     case OESTextureHalfFloatName:
215         extensionObject = toV8(static_cast<OESTextureHalfFloat*>(extension), contextObject, isolate);
216         referenceName = "oesTextureHalfFloatName";
217         break;
218     case OESTextureHalfFloatLinearName:
219         extensionObject = toV8(static_cast<OESTextureHalfFloatLinear*>(extension), contextObject, isolate);
220         referenceName = "oesTextureHalfFloatLinearName";
221         break;
222     case OESVertexArrayObjectName:
223         extensionObject = toV8(static_cast<OESVertexArrayObject*>(extension), contextObject, isolate);
224         referenceName = "oesVertexArrayObjectName";
225         break;
226     case WebGLCompressedTextureATCName:
227         extensionObject = toV8(static_cast<WebGLCompressedTextureATC*>(extension), contextObject, isolate);
228         referenceName = "webGLCompressedTextureATCName";
229         break;
230     case WebGLCompressedTexturePVRTCName:
231         extensionObject = toV8(static_cast<WebGLCompressedTexturePVRTC*>(extension), contextObject, isolate);
232         referenceName = "webGLCompressedTexturePVRTCName";
233         break;
234     case WebGLCompressedTextureS3TCName:
235         extensionObject = toV8(static_cast<WebGLCompressedTextureS3TC*>(extension), contextObject, isolate);
236         referenceName = "webGLCompressedTextureS3TCName";
237         break;
238     case WebGLDebugRendererInfoName:
239         extensionObject = toV8(static_cast<WebGLDebugRendererInfo*>(extension), contextObject, isolate);
240         referenceName = "webGLDebugRendererInfoName";
241         break;
242     case WebGLDebugShadersName:
243         extensionObject = toV8(static_cast<WebGLDebugShaders*>(extension), contextObject, isolate);
244         referenceName = "webGLDebugShadersName";
245         break;
246     case WebGLDepthTextureName:
247         extensionObject = toV8(static_cast<WebGLDepthTexture*>(extension), contextObject, isolate);
248         referenceName = "webGLDepthTextureName";
249         break;
250     case WebGLDrawBuffersName:
251         extensionObject = toV8(static_cast<WebGLDrawBuffers*>(extension), contextObject, isolate);
252         referenceName = "webGLDrawBuffersName";
253         break;
254     case WebGLLoseContextName:
255         extensionObject = toV8(static_cast<WebGLLoseContext*>(extension), contextObject, isolate);
256         referenceName = "webGLLoseContextName";
257         break;
258     case WebGLExtensionNameCount:
259         notImplemented();
260         return v8::Undefined(isolate);
261     }
262     ASSERT(!extensionObject.IsEmpty());
263     V8HiddenValue::setHiddenValue(isolate, contextObject, v8AtomicString(isolate, referenceName), extensionObject);
264     return extensionObject;
265 }
266
267 enum ObjectType {
268     kBuffer, kRenderbuffer, kTexture, kVertexAttrib
269 };
270
271 static void getObjectParameter(const v8::FunctionCallbackInfo<v8::Value>& info, ObjectType objectType, ExceptionState& exceptionState)
272 {
273     if (info.Length() != 2) {
274         exceptionState.throwTypeError(ExceptionMessages::notEnoughArguments(2, info.Length()));
275         exceptionState.throwIfNeeded();
276         return;
277     }
278
279     WebGLRenderingContext* context = V8WebGLRenderingContext::toNative(info.Holder());
280     unsigned target = toInt32(info[0], exceptionState);
281     if (exceptionState.throwIfNeeded())
282         return;
283     unsigned pname = toInt32(info[1], exceptionState);
284     if (exceptionState.throwIfNeeded())
285         return;
286     WebGLGetInfo args;
287     switch (objectType) {
288     case kBuffer:
289         args = context->getBufferParameter(target, pname);
290         break;
291     case kRenderbuffer:
292         args = context->getRenderbufferParameter(target, pname);
293         break;
294     case kTexture:
295         args = context->getTexParameter(target, pname);
296         break;
297     case kVertexAttrib:
298         // target => index
299         args = context->getVertexAttrib(target, pname);
300         break;
301     default:
302         notImplemented();
303         break;
304     }
305     v8SetReturnValue(info, toV8Object(args, info.Holder(), info.GetIsolate()));
306 }
307
308 static WebGLUniformLocation* toWebGLUniformLocation(v8::Handle<v8::Value> value, v8::Isolate* isolate)
309 {
310     return V8WebGLUniformLocation::toNativeWithTypeCheck(isolate, value);
311 }
312
313 enum WhichProgramCall {
314     kProgramParameter, kUniform
315 };
316
317 void V8WebGLRenderingContext::getAttachedShadersMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
318 {
319     ExceptionState exceptionState(ExceptionState::ExecutionContext, "getAttachedShaders", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
320     if (info.Length() < 1) {
321         exceptionState.throwTypeError(ExceptionMessages::notEnoughArguments(1, info.Length()));
322         exceptionState.throwIfNeeded();
323         return;
324     }
325
326     const int programArgumentIndex = 0;
327     WebGLRenderingContext* context = V8WebGLRenderingContext::toNative(info.Holder());
328     if (info.Length() > 0 && !isUndefinedOrNull(info[programArgumentIndex]) && !V8WebGLProgram::hasInstance(info[programArgumentIndex], info.GetIsolate())) {
329         exceptionState.throwTypeError(ExceptionMessages::argumentNullOrIncorrectType(programArgumentIndex + 1, "WebGLProgram"));
330         exceptionState.throwIfNeeded();
331         return;
332     }
333     WebGLProgram* program = V8WebGLProgram::toNativeWithTypeCheck(info.GetIsolate(), info[programArgumentIndex]);
334     Vector<RefPtr<WebGLShader> > shaders;
335     bool succeed = context->getAttachedShaders(program, shaders);
336     if (!succeed) {
337         v8SetReturnValueNull(info);
338         return;
339     }
340     v8::Local<v8::Array> array = v8::Array::New(info.GetIsolate(), shaders.size());
341     for (size_t ii = 0; ii < shaders.size(); ++ii)
342         array->Set(v8::Integer::New(info.GetIsolate(), ii), toV8(shaders[ii].get(), info.Holder(), info.GetIsolate()));
343     v8SetReturnValue(info, array);
344 }
345
346 void V8WebGLRenderingContext::getBufferParameterMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
347 {
348     ExceptionState exceptionState(ExceptionState::ExecutionContext, "getBufferParameter", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
349     getObjectParameter(info, kBuffer, exceptionState);
350 }
351
352 void V8WebGLRenderingContext::getExtensionMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
353 {
354     ExceptionState exceptionState(ExceptionState::ExecutionContext, "getExtension", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
355     WebGLRenderingContext* impl = V8WebGLRenderingContext::toNative(info.Holder());
356     if (info.Length() < 1) {
357         exceptionState.throwTypeError(ExceptionMessages::notEnoughArguments(1, info.Length()));
358         exceptionState.throwIfNeeded();
359         return;
360     }
361     V8TRYCATCH_FOR_V8STRINGRESOURCE_VOID(V8StringResource<>, name, info[0]);
362     RefPtr<WebGLExtension> extension(impl->getExtension(name));
363     v8SetReturnValue(info, toV8Object(extension.get(), info.Holder(), info.GetIsolate()));
364 }
365
366 void V8WebGLRenderingContext::getFramebufferAttachmentParameterMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
367 {
368     ExceptionState exceptionState(ExceptionState::ExecutionContext, "getFramebufferAttachmentParameter", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
369     if (info.Length() != 3) {
370         exceptionState.throwTypeError(ExceptionMessages::notEnoughArguments(3, info.Length()));
371         exceptionState.throwIfNeeded();
372         return;
373     }
374
375     WebGLRenderingContext* context = V8WebGLRenderingContext::toNative(info.Holder());
376     unsigned target = toInt32(info[0]);
377     unsigned attachment = toInt32(info[1], exceptionState);
378     if (exceptionState.throwIfNeeded())
379         return;
380     unsigned pname = toInt32(info[2], exceptionState);
381     if (exceptionState.throwIfNeeded())
382         return;
383     WebGLGetInfo args = context->getFramebufferAttachmentParameter(target, attachment, pname);
384     v8SetReturnValue(info, toV8Object(args, info.Holder(), info.GetIsolate()));
385 }
386
387 void V8WebGLRenderingContext::getParameterMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
388 {
389     ExceptionState exceptionState(ExceptionState::ExecutionContext, "getParameter", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
390     if (info.Length() != 1) {
391         exceptionState.throwTypeError(ExceptionMessages::notEnoughArguments(1, info.Length()));
392         exceptionState.throwIfNeeded();
393         return;
394     }
395
396     WebGLRenderingContext* context = V8WebGLRenderingContext::toNative(info.Holder());
397     unsigned pname = toInt32(info[0], exceptionState);
398     if (exceptionState.throwIfNeeded())
399         return;
400     WebGLGetInfo args = context->getParameter(pname);
401     v8SetReturnValue(info, toV8Object(args, info.Holder(), info.GetIsolate()));
402 }
403
404 void V8WebGLRenderingContext::getProgramParameterMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
405 {
406     ExceptionState exceptionState(ExceptionState::ExecutionContext, "getProgramParameter", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
407     if (info.Length() != 2) {
408         exceptionState.throwTypeError(ExceptionMessages::notEnoughArguments(2, info.Length()));
409         exceptionState.throwIfNeeded();
410         return;
411     }
412
413     const int programArgumentIndex = 0;
414     WebGLRenderingContext* context = V8WebGLRenderingContext::toNative(info.Holder());
415     if (info.Length() > 0 && !isUndefinedOrNull(info[programArgumentIndex]) && !V8WebGLProgram::hasInstance(info[programArgumentIndex], info.GetIsolate())) {
416         exceptionState.throwTypeError(ExceptionMessages::argumentNullOrIncorrectType(programArgumentIndex + 1, "WebGLProgram"));
417         exceptionState.throwIfNeeded();
418         return;
419     }
420     WebGLProgram* program = V8WebGLProgram::toNativeWithTypeCheck(info.GetIsolate(), info[programArgumentIndex]);
421     unsigned pname = toInt32(info[1], exceptionState);
422     if (exceptionState.throwIfNeeded())
423         return;
424     WebGLGetInfo args = context->getProgramParameter(program, pname);
425     v8SetReturnValue(info, toV8Object(args, info.Holder(), info.GetIsolate()));
426 }
427
428 void V8WebGLRenderingContext::getRenderbufferParameterMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
429 {
430     ExceptionState exceptionState(ExceptionState::ExecutionContext, "getRenderbufferParameter", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
431     getObjectParameter(info, kRenderbuffer, exceptionState);
432 }
433
434 void V8WebGLRenderingContext::getShaderParameterMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
435 {
436     ExceptionState exceptionState(ExceptionState::ExecutionContext, "getShaderParameter", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
437     if (info.Length() != 2) {
438         exceptionState.throwTypeError(ExceptionMessages::notEnoughArguments(2, info.Length()));
439         exceptionState.throwIfNeeded();
440         return;
441     }
442
443     const int shaderArgumentIndex = 0;
444     WebGLRenderingContext* context = V8WebGLRenderingContext::toNative(info.Holder());
445     if (info.Length() > 0 && !isUndefinedOrNull(info[shaderArgumentIndex]) && !V8WebGLShader::hasInstance(info[shaderArgumentIndex], info.GetIsolate())) {
446         exceptionState.throwTypeError(ExceptionMessages::argumentNullOrIncorrectType(shaderArgumentIndex + 1, "WebGLShader"));
447         exceptionState.throwIfNeeded();
448         return;
449     }
450     WebGLShader* shader = V8WebGLShader::toNativeWithTypeCheck(info.GetIsolate(), info[shaderArgumentIndex]);
451     unsigned pname = toInt32(info[1], exceptionState);
452     if (exceptionState.throwIfNeeded())
453         return;
454     WebGLGetInfo args = context->getShaderParameter(shader, pname);
455     v8SetReturnValue(info, toV8Object(args, info.Holder(), info.GetIsolate()));
456 }
457
458 void V8WebGLRenderingContext::getSupportedExtensionsMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
459 {
460     WebGLRenderingContext* impl = V8WebGLRenderingContext::toNative(info.Holder());
461     if (impl->isContextLost()) {
462         v8SetReturnValueNull(info);
463         return;
464     }
465
466     Vector<String> value = impl->getSupportedExtensions();
467     v8::Local<v8::Array> array = v8::Array::New(info.GetIsolate(), value.size());
468     for (size_t ii = 0; ii < value.size(); ++ii)
469         array->Set(v8::Integer::New(info.GetIsolate(), ii), v8String(info.GetIsolate(), value[ii]));
470     v8SetReturnValue(info, array);
471 }
472
473 void V8WebGLRenderingContext::getTexParameterMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
474 {
475     ExceptionState exceptionState(ExceptionState::ExecutionContext, "getTexParameter", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
476     getObjectParameter(info, kTexture, exceptionState);
477 }
478
479 void V8WebGLRenderingContext::getUniformMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
480 {
481     ExceptionState exceptionState(ExceptionState::ExecutionContext, "getUniform", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
482     if (info.Length() != 2) {
483         exceptionState.throwTypeError(ExceptionMessages::notEnoughArguments(2, info.Length()));
484         exceptionState.throwIfNeeded();
485         return;
486     }
487
488     const int programArgumentIndex = 0;
489     WebGLRenderingContext* context = V8WebGLRenderingContext::toNative(info.Holder());
490     if (info.Length() > 0 && !isUndefinedOrNull(info[programArgumentIndex]) && !V8WebGLProgram::hasInstance(info[programArgumentIndex], info.GetIsolate())) {
491         exceptionState.throwTypeError(ExceptionMessages::argumentNullOrIncorrectType(programArgumentIndex + 1, "WebGLProgram"));
492         exceptionState.throwIfNeeded();
493         return;
494     }
495     WebGLProgram* program = V8WebGLProgram::toNativeWithTypeCheck(info.GetIsolate(), info[programArgumentIndex]);
496
497     const int uniformArgumentIndex = 1;
498     if (info.Length() > 1 && !isUndefinedOrNull(info[uniformArgumentIndex]) && !V8WebGLUniformLocation::hasInstance(info[uniformArgumentIndex], info.GetIsolate())) {
499         exceptionState.throwTypeError(ExceptionMessages::argumentNullOrIncorrectType(uniformArgumentIndex + 1, "WebGLUniformLocation"));
500         exceptionState.throwIfNeeded();
501         return;
502     }
503     const int uniformLocationArgumentIndex = 1;
504     WebGLUniformLocation* location = toWebGLUniformLocation(info[uniformLocationArgumentIndex], info.GetIsolate());
505
506     WebGLGetInfo args = context->getUniform(program, location);
507     v8SetReturnValue(info, toV8Object(args, info.Holder(), info.GetIsolate()));
508 }
509
510 void V8WebGLRenderingContext::getVertexAttribMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
511 {
512     ExceptionState exceptionState(ExceptionState::ExecutionContext, "getVertexAttrib", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
513     getObjectParameter(info, kVertexAttrib, exceptionState);
514 }
515
516 enum FunctionToCall {
517     kUniform1v, kUniform2v, kUniform3v, kUniform4v,
518     kVertexAttrib1v, kVertexAttrib2v, kVertexAttrib3v, kVertexAttrib4v
519 };
520
521 bool isFunctionToCallForAttribute(FunctionToCall functionToCall)
522 {
523     switch (functionToCall) {
524     case kVertexAttrib1v:
525     case kVertexAttrib2v:
526     case kVertexAttrib3v:
527     case kVertexAttrib4v:
528         return true;
529     default:
530         break;
531     }
532     return false;
533 }
534
535 static void vertexAttribAndUniformHelperf(const v8::FunctionCallbackInfo<v8::Value>& info, FunctionToCall functionToCall, ExceptionState& exceptionState)
536 {
537     // Forms:
538     // * glUniform1fv(WebGLUniformLocation location, Array data);
539     // * glUniform1fv(WebGLUniformLocation location, Float32Array data);
540     // * glUniform2fv(WebGLUniformLocation location, Array data);
541     // * glUniform2fv(WebGLUniformLocation location, Float32Array data);
542     // * glUniform3fv(WebGLUniformLocation location, Array data);
543     // * glUniform3fv(WebGLUniformLocation location, Float32Array data);
544     // * glUniform4fv(WebGLUniformLocation location, Array data);
545     // * glUniform4fv(WebGLUniformLocation location, Float32Array data);
546     // * glVertexAttrib1fv(GLint index, Array data);
547     // * glVertexAttrib1fv(GLint index, Float32Array data);
548     // * glVertexAttrib2fv(GLint index, Array data);
549     // * glVertexAttrib2fv(GLint index, Float32Array data);
550     // * glVertexAttrib3fv(GLint index, Array data);
551     // * glVertexAttrib3fv(GLint index, Float32Array data);
552     // * glVertexAttrib4fv(GLint index, Array data);
553     // * glVertexAttrib4fv(GLint index, Float32Array data);
554
555     if (info.Length() != 2) {
556         exceptionState.throwTypeError(ExceptionMessages::notEnoughArguments(2, info.Length()));
557         exceptionState.throwIfNeeded();
558         return;
559     }
560
561     int index = -1;
562     WebGLUniformLocation* location = 0;
563
564     if (isFunctionToCallForAttribute(functionToCall)) {
565         index = toInt32(info[0], exceptionState);
566         if (exceptionState.throwIfNeeded())
567             return;
568     } else {
569         const int uniformLocationArgumentIndex = 0;
570         if (info.Length() > 0 && !isUndefinedOrNull(info[uniformLocationArgumentIndex]) && !V8WebGLUniformLocation::hasInstance(info[uniformLocationArgumentIndex], info.GetIsolate())) {
571             exceptionState.throwTypeError(ExceptionMessages::argumentNullOrIncorrectType(uniformLocationArgumentIndex + 1, "WebGLUniformLocation"));
572             exceptionState.throwIfNeeded();
573             return;
574         }
575         location = toWebGLUniformLocation(info[uniformLocationArgumentIndex], info.GetIsolate());
576     }
577
578     WebGLRenderingContext* context = V8WebGLRenderingContext::toNative(info.Holder());
579
580     const int indexArrayArgument = 1;
581     if (V8Float32Array::hasInstance(info[indexArrayArgument], info.GetIsolate())) {
582         Float32Array* array = V8Float32Array::toNative(info[indexArrayArgument]->ToObject());
583         ASSERT(array);
584         switch (functionToCall) {
585         case kUniform1v: context->uniform1fv(location, array); break;
586         case kUniform2v: context->uniform2fv(location, array); break;
587         case kUniform3v: context->uniform3fv(location, array); break;
588         case kUniform4v: context->uniform4fv(location, array); break;
589         case kVertexAttrib1v: context->vertexAttrib1fv(index, array); break;
590         case kVertexAttrib2v: context->vertexAttrib2fv(index, array); break;
591         case kVertexAttrib3v: context->vertexAttrib3fv(index, array); break;
592         case kVertexAttrib4v: context->vertexAttrib4fv(index, array); break;
593         default: ASSERT_NOT_REACHED(); break;
594         }
595         return;
596     }
597
598     if (info[indexArrayArgument].IsEmpty() || !info[indexArrayArgument]->IsArray()) {
599         exceptionState.throwTypeError(ExceptionMessages::argumentNullOrIncorrectType(indexArrayArgument + 1, "Array"));
600         exceptionState.throwIfNeeded();
601         return;
602     }
603     v8::Handle<v8::Array> array = v8::Local<v8::Array>::Cast(info[1]);
604     uint32_t len = array->Length();
605     float* data = jsArrayToFloatArray(array, len, exceptionState);
606     if (exceptionState.throwIfNeeded())
607         return;
608     if (!data) {
609         // FIXME: consider different / better exception type.
610         exceptionState.throwDOMException(SyntaxError, "Failed to convert array argument");
611         exceptionState.throwIfNeeded();
612         return;
613     }
614     switch (functionToCall) {
615     case kUniform1v: context->uniform1fv(location, data, len); break;
616     case kUniform2v: context->uniform2fv(location, data, len); break;
617     case kUniform3v: context->uniform3fv(location, data, len); break;
618     case kUniform4v: context->uniform4fv(location, data, len); break;
619     case kVertexAttrib1v: context->vertexAttrib1fv(index, data, len); break;
620     case kVertexAttrib2v: context->vertexAttrib2fv(index, data, len); break;
621     case kVertexAttrib3v: context->vertexAttrib3fv(index, data, len); break;
622     case kVertexAttrib4v: context->vertexAttrib4fv(index, data, len); break;
623     default: ASSERT_NOT_REACHED(); break;
624     }
625     fastFree(data);
626 }
627
628 static void uniformHelperi(const v8::FunctionCallbackInfo<v8::Value>& info, FunctionToCall functionToCall, ExceptionState& exceptionState)
629 {
630     // Forms:
631     // * glUniform1iv(GLUniformLocation location, Array data);
632     // * glUniform1iv(GLUniformLocation location, Int32Array data);
633     // * glUniform2iv(GLUniformLocation location, Array data);
634     // * glUniform2iv(GLUniformLocation location, Int32Array data);
635     // * glUniform3iv(GLUniformLocation location, Array data);
636     // * glUniform3iv(GLUniformLocation location, Int32Array data);
637     // * glUniform4iv(GLUniformLocation location, Array data);
638     // * glUniform4iv(GLUniformLocation location, Int32Array data);
639
640     if (info.Length() != 2) {
641         exceptionState.throwTypeError(ExceptionMessages::notEnoughArguments(2, info.Length()));
642         exceptionState.throwIfNeeded();
643         return;
644     }
645
646     const int uniformLocationArgumentIndex = 0;
647     WebGLRenderingContext* context = V8WebGLRenderingContext::toNative(info.Holder());
648     if (info.Length() > 0 && !isUndefinedOrNull(info[uniformLocationArgumentIndex]) && !V8WebGLUniformLocation::hasInstance(info[uniformLocationArgumentIndex], info.GetIsolate())) {
649         exceptionState.throwTypeError(ExceptionMessages::argumentNullOrIncorrectType(uniformLocationArgumentIndex + 1, "WebGLUniformLocation"));
650         exceptionState.throwIfNeeded();
651         return;
652     }
653     WebGLUniformLocation* location = toWebGLUniformLocation(info[uniformLocationArgumentIndex], info.GetIsolate());
654
655     const int indexArrayArgumentIndex = 1;
656     if (V8Int32Array::hasInstance(info[indexArrayArgumentIndex], info.GetIsolate())) {
657         Int32Array* array = V8Int32Array::toNative(info[indexArrayArgumentIndex]->ToObject());
658         ASSERT(array);
659         switch (functionToCall) {
660         case kUniform1v: context->uniform1iv(location, array); break;
661         case kUniform2v: context->uniform2iv(location, array); break;
662         case kUniform3v: context->uniform3iv(location, array); break;
663         case kUniform4v: context->uniform4iv(location, array); break;
664         default: ASSERT_NOT_REACHED(); break;
665         }
666         return;
667     }
668
669     if (info[indexArrayArgumentIndex].IsEmpty() || !info[indexArrayArgumentIndex]->IsArray()) {
670         exceptionState.throwTypeError(ExceptionMessages::argumentNullOrIncorrectType(indexArrayArgumentIndex + 1, "Array"));
671         exceptionState.throwIfNeeded();
672         return;
673     }
674     v8::Handle<v8::Array> array = v8::Local<v8::Array>::Cast(info[indexArrayArgumentIndex]);
675     uint32_t len = array->Length();
676     int* data = jsArrayToIntArray(array, len, exceptionState);
677     if (exceptionState.throwIfNeeded())
678         return;
679     if (!data) {
680         // FIXME: consider different / better exception type.
681         exceptionState.throwDOMException(SyntaxError, "Failed to convert array argument");
682         exceptionState.throwIfNeeded();
683         return;
684     }
685     switch (functionToCall) {
686     case kUniform1v: context->uniform1iv(location, data, len); break;
687     case kUniform2v: context->uniform2iv(location, data, len); break;
688     case kUniform3v: context->uniform3iv(location, data, len); break;
689     case kUniform4v: context->uniform4iv(location, data, len); break;
690     default: ASSERT_NOT_REACHED(); break;
691     }
692     fastFree(data);
693 }
694
695 void V8WebGLRenderingContext::uniform1fvMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
696 {
697     ExceptionState exceptionState(ExceptionState::ExecutionContext, "uniform1fv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
698     vertexAttribAndUniformHelperf(info, kUniform1v, exceptionState);
699 }
700
701 void V8WebGLRenderingContext::uniform1ivMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
702 {
703     ExceptionState exceptionState(ExceptionState::ExecutionContext, "uniform1iv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
704     uniformHelperi(info, kUniform1v, exceptionState);
705 }
706
707 void V8WebGLRenderingContext::uniform2fvMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
708 {
709     ExceptionState exceptionState(ExceptionState::ExecutionContext, "uniform2fv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
710     vertexAttribAndUniformHelperf(info, kUniform2v, exceptionState);
711 }
712
713 void V8WebGLRenderingContext::uniform2ivMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
714 {
715     ExceptionState exceptionState(ExceptionState::ExecutionContext, "uniform2iv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
716     uniformHelperi(info, kUniform2v, exceptionState);
717 }
718
719 void V8WebGLRenderingContext::uniform3fvMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
720 {
721     ExceptionState exceptionState(ExceptionState::ExecutionContext, "uniform3fv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
722     vertexAttribAndUniformHelperf(info, kUniform3v, exceptionState);
723 }
724
725 void V8WebGLRenderingContext::uniform3ivMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
726 {
727     ExceptionState exceptionState(ExceptionState::ExecutionContext, "uniform3iv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
728     uniformHelperi(info, kUniform3v, exceptionState);
729 }
730
731 void V8WebGLRenderingContext::uniform4fvMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
732 {
733     ExceptionState exceptionState(ExceptionState::ExecutionContext, "uniform4fv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
734     vertexAttribAndUniformHelperf(info, kUniform4v, exceptionState);
735 }
736
737 void V8WebGLRenderingContext::uniform4ivMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
738 {
739     ExceptionState exceptionState(ExceptionState::ExecutionContext, "uniform4iv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
740     uniformHelperi(info, kUniform4v, exceptionState);
741 }
742
743 static void uniformMatrixHelper(const v8::FunctionCallbackInfo<v8::Value>& info, int matrixSize, ExceptionState& exceptionState)
744 {
745     // Forms:
746     // * glUniformMatrix2fv(GLint location, GLboolean transpose, Array data);
747     // * glUniformMatrix2fv(GLint location, GLboolean transpose, Float32Array data);
748     // * glUniformMatrix3fv(GLint location, GLboolean transpose, Array data);
749     // * glUniformMatrix3fv(GLint location, GLboolean transpose, Float32Array data);
750     // * glUniformMatrix4fv(GLint location, GLboolean transpose, Array data);
751     // * glUniformMatrix4fv(GLint location, GLboolean transpose, Float32Array data);
752     //
753     // FIXME: need to change to accept Float32Array as well.
754     if (info.Length() != 3) {
755         exceptionState.throwTypeError(ExceptionMessages::notEnoughArguments(3, info.Length()));
756         exceptionState.throwIfNeeded();
757         return;
758     }
759
760     WebGLRenderingContext* context = V8WebGLRenderingContext::toNative(info.Holder());
761
762     const int uniformLocationArgumentIndex = 0;
763     if (info.Length() > 0 && !isUndefinedOrNull(info[uniformLocationArgumentIndex]) && !V8WebGLUniformLocation::hasInstance(info[uniformLocationArgumentIndex], info.GetIsolate())) {
764         exceptionState.throwTypeError(ExceptionMessages::argumentNullOrIncorrectType(uniformLocationArgumentIndex + 1, "WebGLUniformLocation"));
765         exceptionState.throwIfNeeded();
766         return;
767     }
768     WebGLUniformLocation* location = toWebGLUniformLocation(info[uniformLocationArgumentIndex], info.GetIsolate());
769
770     bool transpose = info[1]->BooleanValue();
771     const int arrayArgumentIndex = 2;
772     if (V8Float32Array::hasInstance(info[arrayArgumentIndex], info.GetIsolate())) {
773         Float32Array* array = V8Float32Array::toNative(info[arrayArgumentIndex]->ToObject());
774         ASSERT(array);
775         switch (matrixSize) {
776         case 2: context->uniformMatrix2fv(location, transpose, array); break;
777         case 3: context->uniformMatrix3fv(location, transpose, array); break;
778         case 4: context->uniformMatrix4fv(location, transpose, array); break;
779         default: ASSERT_NOT_REACHED(); break;
780         }
781         return;
782     }
783
784     if (info[arrayArgumentIndex].IsEmpty() || !info[arrayArgumentIndex]->IsArray()) {
785         exceptionState.throwTypeError(ExceptionMessages::argumentNullOrIncorrectType(arrayArgumentIndex + 1, "Array"));
786         exceptionState.throwIfNeeded();
787         return;
788     }
789     v8::Handle<v8::Array> array = v8::Local<v8::Array>::Cast(info[2]);
790     uint32_t len = array->Length();
791     float* data = jsArrayToFloatArray(array, len, exceptionState);
792     if (exceptionState.throwIfNeeded())
793         return;
794     if (!data) {
795         // FIXME: consider different / better exception type.
796         exceptionState.throwDOMException(SyntaxError, "failed to convert Array value");
797         exceptionState.throwIfNeeded();
798         return;
799     }
800     switch (matrixSize) {
801     case 2: context->uniformMatrix2fv(location, transpose, data, len); break;
802     case 3: context->uniformMatrix3fv(location, transpose, data, len); break;
803     case 4: context->uniformMatrix4fv(location, transpose, data, len); break;
804     default: ASSERT_NOT_REACHED(); break;
805     }
806     fastFree(data);
807 }
808
809 void V8WebGLRenderingContext::uniformMatrix2fvMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
810 {
811     ExceptionState exceptionState(ExceptionState::ExecutionContext, "uniformMatrix2fv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
812     uniformMatrixHelper(info, 2, exceptionState);
813 }
814
815 void V8WebGLRenderingContext::uniformMatrix3fvMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
816 {
817     ExceptionState exceptionState(ExceptionState::ExecutionContext, "uniformMatrix3fv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
818     uniformMatrixHelper(info, 3, exceptionState);
819 }
820
821 void V8WebGLRenderingContext::uniformMatrix4fvMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
822 {
823     ExceptionState exceptionState(ExceptionState::ExecutionContext, "uniformMatrix4fv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
824     uniformMatrixHelper(info, 4, exceptionState);
825 }
826
827 void V8WebGLRenderingContext::vertexAttrib1fvMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
828 {
829     ExceptionState exceptionState(ExceptionState::ExecutionContext, "vertexAttrib1fv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
830     vertexAttribAndUniformHelperf(info, kVertexAttrib1v, exceptionState);
831 }
832
833 void V8WebGLRenderingContext::vertexAttrib2fvMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
834 {
835     ExceptionState exceptionState(ExceptionState::ExecutionContext, "vertexAttrib2fv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
836     vertexAttribAndUniformHelperf(info, kVertexAttrib2v, exceptionState);
837 }
838
839 void V8WebGLRenderingContext::vertexAttrib3fvMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
840 {
841     ExceptionState exceptionState(ExceptionState::ExecutionContext, "vertexAttrib3fv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
842     vertexAttribAndUniformHelperf(info, kVertexAttrib3v, exceptionState);
843 }
844
845 void V8WebGLRenderingContext::vertexAttrib4fvMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
846 {
847     ExceptionState exceptionState(ExceptionState::ExecutionContext, "vertexAttrib4fv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
848     vertexAttribAndUniformHelperf(info, kVertexAttrib4v, exceptionState);
849 }
850
851 } // namespace WebCore