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