2 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "V8WebGLRenderingContext.h"
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"
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)
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.");
92 float* data = static_cast<float*>(fastMalloc(len * sizeof(float)));
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()) {
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)
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.");
115 int* data = static_cast<int*>(fastMalloc(len * sizeof(int)));
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()) {
129 static v8::Handle<v8::Value> toV8Object(const WebGLGetInfo& args, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
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));
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);
175 return v8::Undefined(isolate);
179 static v8::Handle<v8::Value> toV8Object(WebGLExtension* extension, v8::Handle<v8::Object> contextObject, v8::Isolate* isolate)
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";
190 case EXTFragDepthName:
191 extensionObject = toV8(static_cast<EXTFragDepth*>(extension), contextObject, isolate);
192 referenceName = "extFragDepthName";
194 case EXTTextureFilterAnisotropicName:
195 extensionObject = toV8(static_cast<EXTTextureFilterAnisotropic*>(extension), contextObject, isolate);
196 referenceName = "extTextureFilterAnisotropicName";
198 case OESElementIndexUintName:
199 extensionObject = toV8(static_cast<OESElementIndexUint*>(extension), contextObject, isolate);
200 referenceName = "oesElementIndexUintName";
202 case OESStandardDerivativesName:
203 extensionObject = toV8(static_cast<OESStandardDerivatives*>(extension), contextObject, isolate);
204 referenceName = "oesStandardDerivativesName";
206 case OESTextureFloatName:
207 extensionObject = toV8(static_cast<OESTextureFloat*>(extension), contextObject, isolate);
208 referenceName = "oesTextureFloatName";
210 case OESTextureFloatLinearName:
211 extensionObject = toV8(static_cast<OESTextureFloatLinear*>(extension), contextObject, isolate);
212 referenceName = "oesTextureFloatLinearName";
214 case OESTextureHalfFloatName:
215 extensionObject = toV8(static_cast<OESTextureHalfFloat*>(extension), contextObject, isolate);
216 referenceName = "oesTextureHalfFloatName";
218 case OESTextureHalfFloatLinearName:
219 extensionObject = toV8(static_cast<OESTextureHalfFloatLinear*>(extension), contextObject, isolate);
220 referenceName = "oesTextureHalfFloatLinearName";
222 case OESVertexArrayObjectName:
223 extensionObject = toV8(static_cast<OESVertexArrayObject*>(extension), contextObject, isolate);
224 referenceName = "oesVertexArrayObjectName";
226 case WebGLCompressedTextureATCName:
227 extensionObject = toV8(static_cast<WebGLCompressedTextureATC*>(extension), contextObject, isolate);
228 referenceName = "webGLCompressedTextureATCName";
230 case WebGLCompressedTexturePVRTCName:
231 extensionObject = toV8(static_cast<WebGLCompressedTexturePVRTC*>(extension), contextObject, isolate);
232 referenceName = "webGLCompressedTexturePVRTCName";
234 case WebGLCompressedTextureS3TCName:
235 extensionObject = toV8(static_cast<WebGLCompressedTextureS3TC*>(extension), contextObject, isolate);
236 referenceName = "webGLCompressedTextureS3TCName";
238 case WebGLDebugRendererInfoName:
239 extensionObject = toV8(static_cast<WebGLDebugRendererInfo*>(extension), contextObject, isolate);
240 referenceName = "webGLDebugRendererInfoName";
242 case WebGLDebugShadersName:
243 extensionObject = toV8(static_cast<WebGLDebugShaders*>(extension), contextObject, isolate);
244 referenceName = "webGLDebugShadersName";
246 case WebGLDepthTextureName:
247 extensionObject = toV8(static_cast<WebGLDepthTexture*>(extension), contextObject, isolate);
248 referenceName = "webGLDepthTextureName";
250 case WebGLDrawBuffersName:
251 extensionObject = toV8(static_cast<WebGLDrawBuffers*>(extension), contextObject, isolate);
252 referenceName = "webGLDrawBuffersName";
254 case WebGLLoseContextName:
255 extensionObject = toV8(static_cast<WebGLLoseContext*>(extension), contextObject, isolate);
256 referenceName = "webGLLoseContextName";
258 case WebGLExtensionNameCount:
260 return v8::Undefined(isolate);
262 ASSERT(!extensionObject.IsEmpty());
263 V8HiddenValue::setHiddenValue(isolate, contextObject, v8AtomicString(isolate, referenceName), extensionObject);
264 return extensionObject;
268 kBuffer, kRenderbuffer, kTexture, kVertexAttrib
271 static void getObjectParameter(const v8::FunctionCallbackInfo<v8::Value>& info, ObjectType objectType, ExceptionState& exceptionState)
273 if (info.Length() != 2) {
274 exceptionState.throwTypeError(ExceptionMessages::notEnoughArguments(2, info.Length()));
275 exceptionState.throwIfNeeded();
279 WebGLRenderingContext* context = V8WebGLRenderingContext::toNative(info.Holder());
280 unsigned target = toInt32(info[0], exceptionState);
281 if (exceptionState.throwIfNeeded())
283 unsigned pname = toInt32(info[1], exceptionState);
284 if (exceptionState.throwIfNeeded())
287 switch (objectType) {
289 args = context->getBufferParameter(target, pname);
292 args = context->getRenderbufferParameter(target, pname);
295 args = context->getTexParameter(target, pname);
299 args = context->getVertexAttrib(target, pname);
305 v8SetReturnValue(info, toV8Object(args, info.Holder(), info.GetIsolate()));
308 static WebGLUniformLocation* toWebGLUniformLocation(v8::Handle<v8::Value> value, v8::Isolate* isolate)
310 return V8WebGLUniformLocation::toNativeWithTypeCheck(isolate, value);
313 enum WhichProgramCall {
314 kProgramParameter, kUniform
317 void V8WebGLRenderingContext::getAttachedShadersMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
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();
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();
333 WebGLProgram* program = V8WebGLProgram::toNativeWithTypeCheck(info.GetIsolate(), info[programArgumentIndex]);
334 Vector<RefPtr<WebGLShader> > shaders;
335 bool succeed = context->getAttachedShaders(program, shaders);
337 v8SetReturnValueNull(info);
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);
346 void V8WebGLRenderingContext::getBufferParameterMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
348 ExceptionState exceptionState(ExceptionState::ExecutionContext, "getBufferParameter", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
349 getObjectParameter(info, kBuffer, exceptionState);
352 void V8WebGLRenderingContext::getExtensionMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
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();
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()));
366 void V8WebGLRenderingContext::getFramebufferAttachmentParameterMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
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();
375 WebGLRenderingContext* context = V8WebGLRenderingContext::toNative(info.Holder());
376 unsigned target = toInt32(info[0]);
377 unsigned attachment = toInt32(info[1], exceptionState);
378 if (exceptionState.throwIfNeeded())
380 unsigned pname = toInt32(info[2], exceptionState);
381 if (exceptionState.throwIfNeeded())
383 WebGLGetInfo args = context->getFramebufferAttachmentParameter(target, attachment, pname);
384 v8SetReturnValue(info, toV8Object(args, info.Holder(), info.GetIsolate()));
387 void V8WebGLRenderingContext::getParameterMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
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();
396 WebGLRenderingContext* context = V8WebGLRenderingContext::toNative(info.Holder());
397 unsigned pname = toInt32(info[0], exceptionState);
398 if (exceptionState.throwIfNeeded())
400 WebGLGetInfo args = context->getParameter(pname);
401 v8SetReturnValue(info, toV8Object(args, info.Holder(), info.GetIsolate()));
404 void V8WebGLRenderingContext::getProgramParameterMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
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();
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();
420 WebGLProgram* program = V8WebGLProgram::toNativeWithTypeCheck(info.GetIsolate(), info[programArgumentIndex]);
421 unsigned pname = toInt32(info[1], exceptionState);
422 if (exceptionState.throwIfNeeded())
424 WebGLGetInfo args = context->getProgramParameter(program, pname);
425 v8SetReturnValue(info, toV8Object(args, info.Holder(), info.GetIsolate()));
428 void V8WebGLRenderingContext::getRenderbufferParameterMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
430 ExceptionState exceptionState(ExceptionState::ExecutionContext, "getRenderbufferParameter", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
431 getObjectParameter(info, kRenderbuffer, exceptionState);
434 void V8WebGLRenderingContext::getShaderParameterMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
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();
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();
450 WebGLShader* shader = V8WebGLShader::toNativeWithTypeCheck(info.GetIsolate(), info[shaderArgumentIndex]);
451 unsigned pname = toInt32(info[1], exceptionState);
452 if (exceptionState.throwIfNeeded())
454 WebGLGetInfo args = context->getShaderParameter(shader, pname);
455 v8SetReturnValue(info, toV8Object(args, info.Holder(), info.GetIsolate()));
458 void V8WebGLRenderingContext::getSupportedExtensionsMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
460 WebGLRenderingContext* impl = V8WebGLRenderingContext::toNative(info.Holder());
461 if (impl->isContextLost()) {
462 v8SetReturnValueNull(info);
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);
473 void V8WebGLRenderingContext::getTexParameterMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
475 ExceptionState exceptionState(ExceptionState::ExecutionContext, "getTexParameter", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
476 getObjectParameter(info, kTexture, exceptionState);
479 void V8WebGLRenderingContext::getUniformMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
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();
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();
495 WebGLProgram* program = V8WebGLProgram::toNativeWithTypeCheck(info.GetIsolate(), info[programArgumentIndex]);
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();
503 const int uniformLocationArgumentIndex = 1;
504 WebGLUniformLocation* location = toWebGLUniformLocation(info[uniformLocationArgumentIndex], info.GetIsolate());
506 WebGLGetInfo args = context->getUniform(program, location);
507 v8SetReturnValue(info, toV8Object(args, info.Holder(), info.GetIsolate()));
510 void V8WebGLRenderingContext::getVertexAttribMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
512 ExceptionState exceptionState(ExceptionState::ExecutionContext, "getVertexAttrib", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
513 getObjectParameter(info, kVertexAttrib, exceptionState);
516 enum FunctionToCall {
517 kUniform1v, kUniform2v, kUniform3v, kUniform4v,
518 kVertexAttrib1v, kVertexAttrib2v, kVertexAttrib3v, kVertexAttrib4v
521 bool isFunctionToCallForAttribute(FunctionToCall functionToCall)
523 switch (functionToCall) {
524 case kVertexAttrib1v:
525 case kVertexAttrib2v:
526 case kVertexAttrib3v:
527 case kVertexAttrib4v:
535 static void vertexAttribAndUniformHelperf(const v8::FunctionCallbackInfo<v8::Value>& info, FunctionToCall functionToCall, ExceptionState& exceptionState)
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);
555 if (info.Length() != 2) {
556 exceptionState.throwTypeError(ExceptionMessages::notEnoughArguments(2, info.Length()));
557 exceptionState.throwIfNeeded();
562 WebGLUniformLocation* location = 0;
564 if (isFunctionToCallForAttribute(functionToCall)) {
565 index = toInt32(info[0], exceptionState);
566 if (exceptionState.throwIfNeeded())
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();
575 location = toWebGLUniformLocation(info[uniformLocationArgumentIndex], info.GetIsolate());
578 WebGLRenderingContext* context = V8WebGLRenderingContext::toNative(info.Holder());
580 const int indexArrayArgument = 1;
581 if (V8Float32Array::hasInstance(info[indexArrayArgument], info.GetIsolate())) {
582 Float32Array* array = V8Float32Array::toNative(info[indexArrayArgument]->ToObject());
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;
598 if (info[indexArrayArgument].IsEmpty() || !info[indexArrayArgument]->IsArray()) {
599 exceptionState.throwTypeError(ExceptionMessages::argumentNullOrIncorrectType(indexArrayArgument + 1, "Array"));
600 exceptionState.throwIfNeeded();
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())
609 // FIXME: consider different / better exception type.
610 exceptionState.throwDOMException(SyntaxError, "Failed to convert array argument");
611 exceptionState.throwIfNeeded();
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;
628 static void uniformHelperi(const v8::FunctionCallbackInfo<v8::Value>& info, FunctionToCall functionToCall, ExceptionState& exceptionState)
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);
640 if (info.Length() != 2) {
641 exceptionState.throwTypeError(ExceptionMessages::notEnoughArguments(2, info.Length()));
642 exceptionState.throwIfNeeded();
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();
653 WebGLUniformLocation* location = toWebGLUniformLocation(info[uniformLocationArgumentIndex], info.GetIsolate());
655 const int indexArrayArgumentIndex = 1;
656 if (V8Int32Array::hasInstance(info[indexArrayArgumentIndex], info.GetIsolate())) {
657 Int32Array* array = V8Int32Array::toNative(info[indexArrayArgumentIndex]->ToObject());
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;
669 if (info[indexArrayArgumentIndex].IsEmpty() || !info[indexArrayArgumentIndex]->IsArray()) {
670 exceptionState.throwTypeError(ExceptionMessages::argumentNullOrIncorrectType(indexArrayArgumentIndex + 1, "Array"));
671 exceptionState.throwIfNeeded();
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())
680 // FIXME: consider different / better exception type.
681 exceptionState.throwDOMException(SyntaxError, "Failed to convert array argument");
682 exceptionState.throwIfNeeded();
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;
695 void V8WebGLRenderingContext::uniform1fvMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
697 ExceptionState exceptionState(ExceptionState::ExecutionContext, "uniform1fv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
698 vertexAttribAndUniformHelperf(info, kUniform1v, exceptionState);
701 void V8WebGLRenderingContext::uniform1ivMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
703 ExceptionState exceptionState(ExceptionState::ExecutionContext, "uniform1iv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
704 uniformHelperi(info, kUniform1v, exceptionState);
707 void V8WebGLRenderingContext::uniform2fvMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
709 ExceptionState exceptionState(ExceptionState::ExecutionContext, "uniform2fv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
710 vertexAttribAndUniformHelperf(info, kUniform2v, exceptionState);
713 void V8WebGLRenderingContext::uniform2ivMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
715 ExceptionState exceptionState(ExceptionState::ExecutionContext, "uniform2iv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
716 uniformHelperi(info, kUniform2v, exceptionState);
719 void V8WebGLRenderingContext::uniform3fvMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
721 ExceptionState exceptionState(ExceptionState::ExecutionContext, "uniform3fv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
722 vertexAttribAndUniformHelperf(info, kUniform3v, exceptionState);
725 void V8WebGLRenderingContext::uniform3ivMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
727 ExceptionState exceptionState(ExceptionState::ExecutionContext, "uniform3iv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
728 uniformHelperi(info, kUniform3v, exceptionState);
731 void V8WebGLRenderingContext::uniform4fvMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
733 ExceptionState exceptionState(ExceptionState::ExecutionContext, "uniform4fv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
734 vertexAttribAndUniformHelperf(info, kUniform4v, exceptionState);
737 void V8WebGLRenderingContext::uniform4ivMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
739 ExceptionState exceptionState(ExceptionState::ExecutionContext, "uniform4iv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
740 uniformHelperi(info, kUniform4v, exceptionState);
743 static void uniformMatrixHelper(const v8::FunctionCallbackInfo<v8::Value>& info, int matrixSize, ExceptionState& exceptionState)
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);
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();
760 WebGLRenderingContext* context = V8WebGLRenderingContext::toNative(info.Holder());
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();
768 WebGLUniformLocation* location = toWebGLUniformLocation(info[uniformLocationArgumentIndex], info.GetIsolate());
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());
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;
784 if (info[arrayArgumentIndex].IsEmpty() || !info[arrayArgumentIndex]->IsArray()) {
785 exceptionState.throwTypeError(ExceptionMessages::argumentNullOrIncorrectType(arrayArgumentIndex + 1, "Array"));
786 exceptionState.throwIfNeeded();
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())
795 // FIXME: consider different / better exception type.
796 exceptionState.throwDOMException(SyntaxError, "failed to convert Array value");
797 exceptionState.throwIfNeeded();
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;
809 void V8WebGLRenderingContext::uniformMatrix2fvMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
811 ExceptionState exceptionState(ExceptionState::ExecutionContext, "uniformMatrix2fv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
812 uniformMatrixHelper(info, 2, exceptionState);
815 void V8WebGLRenderingContext::uniformMatrix3fvMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
817 ExceptionState exceptionState(ExceptionState::ExecutionContext, "uniformMatrix3fv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
818 uniformMatrixHelper(info, 3, exceptionState);
821 void V8WebGLRenderingContext::uniformMatrix4fvMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
823 ExceptionState exceptionState(ExceptionState::ExecutionContext, "uniformMatrix4fv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
824 uniformMatrixHelper(info, 4, exceptionState);
827 void V8WebGLRenderingContext::vertexAttrib1fvMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
829 ExceptionState exceptionState(ExceptionState::ExecutionContext, "vertexAttrib1fv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
830 vertexAttribAndUniformHelperf(info, kVertexAttrib1v, exceptionState);
833 void V8WebGLRenderingContext::vertexAttrib2fvMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
835 ExceptionState exceptionState(ExceptionState::ExecutionContext, "vertexAttrib2fv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
836 vertexAttribAndUniformHelperf(info, kVertexAttrib2v, exceptionState);
839 void V8WebGLRenderingContext::vertexAttrib3fvMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
841 ExceptionState exceptionState(ExceptionState::ExecutionContext, "vertexAttrib3fv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
842 vertexAttribAndUniformHelperf(info, kVertexAttrib3v, exceptionState);
845 void V8WebGLRenderingContext::vertexAttrib4fvMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
847 ExceptionState exceptionState(ExceptionState::ExecutionContext, "vertexAttrib4fv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
848 vertexAttribAndUniformHelperf(info, kVertexAttrib4v, exceptionState);
851 } // namespace WebCore