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 "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"
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)
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.");
94 float* data = static_cast<float*>(fastMalloc(len * sizeof(float)));
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()) {
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)
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.");
117 int* data = static_cast<int*>(fastMalloc(len * sizeof(int)));
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()) {
131 static v8::Handle<v8::Value> toV8Object(const WebGLGetInfo& args, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
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));
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);
177 return v8::Undefined(isolate);
181 static v8::Handle<v8::Value> toV8Object(WebGLExtension* extension, v8::Handle<v8::Object> contextObject, v8::Isolate* isolate)
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";
192 case EXTFragDepthName:
193 extensionObject = toV8(static_cast<EXTFragDepth*>(extension), contextObject, isolate);
194 referenceName = "extFragDepthName";
196 case EXTShaderTextureLODName:
197 extensionObject = toV8(static_cast<EXTShaderTextureLOD*>(extension), contextObject, isolate);
198 referenceName = "extShaderTextureLODName";
200 case EXTTextureFilterAnisotropicName:
201 extensionObject = toV8(static_cast<EXTTextureFilterAnisotropic*>(extension), contextObject, isolate);
202 referenceName = "extTextureFilterAnisotropicName";
204 case OESElementIndexUintName:
205 extensionObject = toV8(static_cast<OESElementIndexUint*>(extension), contextObject, isolate);
206 referenceName = "oesElementIndexUintName";
208 case OESStandardDerivativesName:
209 extensionObject = toV8(static_cast<OESStandardDerivatives*>(extension), contextObject, isolate);
210 referenceName = "oesStandardDerivativesName";
212 case OESTextureFloatName:
213 extensionObject = toV8(static_cast<OESTextureFloat*>(extension), contextObject, isolate);
214 referenceName = "oesTextureFloatName";
216 case OESTextureFloatLinearName:
217 extensionObject = toV8(static_cast<OESTextureFloatLinear*>(extension), contextObject, isolate);
218 referenceName = "oesTextureFloatLinearName";
220 case OESTextureHalfFloatName:
221 extensionObject = toV8(static_cast<OESTextureHalfFloat*>(extension), contextObject, isolate);
222 referenceName = "oesTextureHalfFloatName";
224 case OESTextureHalfFloatLinearName:
225 extensionObject = toV8(static_cast<OESTextureHalfFloatLinear*>(extension), contextObject, isolate);
226 referenceName = "oesTextureHalfFloatLinearName";
228 case OESVertexArrayObjectName:
229 extensionObject = toV8(static_cast<OESVertexArrayObject*>(extension), contextObject, isolate);
230 referenceName = "oesVertexArrayObjectName";
232 case WebGLCompressedTextureATCName:
233 extensionObject = toV8(static_cast<WebGLCompressedTextureATC*>(extension), contextObject, isolate);
234 referenceName = "webGLCompressedTextureATCName";
236 case WebGLCompressedTextureETC1Name:
237 extensionObject = toV8(static_cast<WebGLCompressedTextureETC1*>(extension), contextObject, isolate);
238 referenceName = "webGLCompressedTextureETC1Name";
240 case WebGLCompressedTexturePVRTCName:
241 extensionObject = toV8(static_cast<WebGLCompressedTexturePVRTC*>(extension), contextObject, isolate);
242 referenceName = "webGLCompressedTexturePVRTCName";
244 case WebGLCompressedTextureS3TCName:
245 extensionObject = toV8(static_cast<WebGLCompressedTextureS3TC*>(extension), contextObject, isolate);
246 referenceName = "webGLCompressedTextureS3TCName";
248 case WebGLDebugRendererInfoName:
249 extensionObject = toV8(static_cast<WebGLDebugRendererInfo*>(extension), contextObject, isolate);
250 referenceName = "webGLDebugRendererInfoName";
252 case WebGLDebugShadersName:
253 extensionObject = toV8(static_cast<WebGLDebugShaders*>(extension), contextObject, isolate);
254 referenceName = "webGLDebugShadersName";
256 case WebGLDepthTextureName:
257 extensionObject = toV8(static_cast<WebGLDepthTexture*>(extension), contextObject, isolate);
258 referenceName = "webGLDepthTextureName";
260 case WebGLDrawBuffersName:
261 extensionObject = toV8(static_cast<WebGLDrawBuffers*>(extension), contextObject, isolate);
262 referenceName = "webGLDrawBuffersName";
264 case WebGLLoseContextName:
265 extensionObject = toV8(static_cast<WebGLLoseContext*>(extension), contextObject, isolate);
266 referenceName = "webGLLoseContextName";
268 case WebGLExtensionNameCount:
270 return v8::Undefined(isolate);
272 ASSERT(!extensionObject.IsEmpty());
273 V8HiddenValue::setHiddenValue(isolate, contextObject, v8AtomicString(isolate, referenceName), extensionObject);
274 return extensionObject;
278 kBuffer, kRenderbuffer, kTexture, kVertexAttrib
281 static void getObjectParameter(const v8::FunctionCallbackInfo<v8::Value>& info, ObjectType objectType, ExceptionState& exceptionState)
283 if (info.Length() != 2) {
284 exceptionState.throwTypeError(ExceptionMessages::notEnoughArguments(2, info.Length()));
285 exceptionState.throwIfNeeded();
289 WebGLRenderingContext* context = V8WebGLRenderingContext::toNative(info.Holder());
290 unsigned target = toInt32(info[0], exceptionState);
291 if (exceptionState.throwIfNeeded())
293 unsigned pname = toInt32(info[1], exceptionState);
294 if (exceptionState.throwIfNeeded())
297 switch (objectType) {
299 args = context->getBufferParameter(target, pname);
302 args = context->getRenderbufferParameter(target, pname);
305 args = context->getTexParameter(target, pname);
309 args = context->getVertexAttrib(target, pname);
315 v8SetReturnValue(info, toV8Object(args, info.Holder(), info.GetIsolate()));
318 static WebGLUniformLocation* toWebGLUniformLocation(v8::Handle<v8::Value> value, v8::Isolate* isolate)
320 return V8WebGLUniformLocation::toNativeWithTypeCheck(isolate, value);
323 enum WhichProgramCall {
324 kProgramParameter, kUniform
327 void V8WebGLRenderingContext::getAttachedShadersMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
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();
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();
343 WebGLProgram* program = V8WebGLProgram::toNativeWithTypeCheck(info.GetIsolate(), info[programArgumentIndex]);
344 Vector<RefPtr<WebGLShader> > shaders;
345 bool succeed = context->getAttachedShaders(program, shaders);
347 v8SetReturnValueNull(info);
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);
356 void V8WebGLRenderingContext::getBufferParameterMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
358 ExceptionState exceptionState(ExceptionState::ExecutionContext, "getBufferParameter", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
359 getObjectParameter(info, kBuffer, exceptionState);
362 void V8WebGLRenderingContext::getExtensionMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
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();
371 TOSTRING_VOID(V8StringResource<>, name, info[0]);
372 RefPtr<WebGLExtension> extension(impl->getExtension(name));
373 v8SetReturnValue(info, toV8Object(extension.get(), info.Holder(), info.GetIsolate()));
376 void V8WebGLRenderingContext::getFramebufferAttachmentParameterMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
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();
385 WebGLRenderingContext* context = V8WebGLRenderingContext::toNative(info.Holder());
386 unsigned target = toInt32(info[0]);
387 unsigned attachment = toInt32(info[1], exceptionState);
388 if (exceptionState.throwIfNeeded())
390 unsigned pname = toInt32(info[2], exceptionState);
391 if (exceptionState.throwIfNeeded())
393 WebGLGetInfo args = context->getFramebufferAttachmentParameter(target, attachment, pname);
394 v8SetReturnValue(info, toV8Object(args, info.Holder(), info.GetIsolate()));
397 void V8WebGLRenderingContext::getParameterMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
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();
406 WebGLRenderingContext* context = V8WebGLRenderingContext::toNative(info.Holder());
407 unsigned pname = toInt32(info[0], exceptionState);
408 if (exceptionState.throwIfNeeded())
410 WebGLGetInfo args = context->getParameter(pname);
411 v8SetReturnValue(info, toV8Object(args, info.Holder(), info.GetIsolate()));
414 void V8WebGLRenderingContext::getProgramParameterMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
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();
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();
430 WebGLProgram* program = V8WebGLProgram::toNativeWithTypeCheck(info.GetIsolate(), info[programArgumentIndex]);
431 unsigned pname = toInt32(info[1], exceptionState);
432 if (exceptionState.throwIfNeeded())
434 WebGLGetInfo args = context->getProgramParameter(program, pname);
435 v8SetReturnValue(info, toV8Object(args, info.Holder(), info.GetIsolate()));
438 void V8WebGLRenderingContext::getRenderbufferParameterMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
440 ExceptionState exceptionState(ExceptionState::ExecutionContext, "getRenderbufferParameter", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
441 getObjectParameter(info, kRenderbuffer, exceptionState);
444 void V8WebGLRenderingContext::getShaderParameterMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
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();
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();
460 WebGLShader* shader = V8WebGLShader::toNativeWithTypeCheck(info.GetIsolate(), info[shaderArgumentIndex]);
461 unsigned pname = toInt32(info[1], exceptionState);
462 if (exceptionState.throwIfNeeded())
464 WebGLGetInfo args = context->getShaderParameter(shader, pname);
465 v8SetReturnValue(info, toV8Object(args, info.Holder(), info.GetIsolate()));
468 void V8WebGLRenderingContext::getSupportedExtensionsMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
470 WebGLRenderingContext* impl = V8WebGLRenderingContext::toNative(info.Holder());
471 if (impl->isContextLost()) {
472 v8SetReturnValueNull(info);
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);
483 void V8WebGLRenderingContext::getTexParameterMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
485 ExceptionState exceptionState(ExceptionState::ExecutionContext, "getTexParameter", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
486 getObjectParameter(info, kTexture, exceptionState);
489 void V8WebGLRenderingContext::getUniformMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
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();
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();
505 WebGLProgram* program = V8WebGLProgram::toNativeWithTypeCheck(info.GetIsolate(), info[programArgumentIndex]);
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();
513 const int uniformLocationArgumentIndex = 1;
514 WebGLUniformLocation* location = toWebGLUniformLocation(info[uniformLocationArgumentIndex], info.GetIsolate());
516 WebGLGetInfo args = context->getUniform(program, location);
517 v8SetReturnValue(info, toV8Object(args, info.Holder(), info.GetIsolate()));
520 void V8WebGLRenderingContext::getVertexAttribMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
522 ExceptionState exceptionState(ExceptionState::ExecutionContext, "getVertexAttrib", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
523 getObjectParameter(info, kVertexAttrib, exceptionState);
526 enum FunctionToCall {
527 kUniform1v, kUniform2v, kUniform3v, kUniform4v,
528 kVertexAttrib1v, kVertexAttrib2v, kVertexAttrib3v, kVertexAttrib4v
531 bool isFunctionToCallForAttribute(FunctionToCall functionToCall)
533 switch (functionToCall) {
534 case kVertexAttrib1v:
535 case kVertexAttrib2v:
536 case kVertexAttrib3v:
537 case kVertexAttrib4v:
545 static void vertexAttribAndUniformHelperf(const v8::FunctionCallbackInfo<v8::Value>& info, FunctionToCall functionToCall, ExceptionState& exceptionState)
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);
565 if (info.Length() != 2) {
566 exceptionState.throwTypeError(ExceptionMessages::notEnoughArguments(2, info.Length()));
567 exceptionState.throwIfNeeded();
572 WebGLUniformLocation* location = 0;
574 if (isFunctionToCallForAttribute(functionToCall)) {
575 index = toInt32(info[0], exceptionState);
576 if (exceptionState.throwIfNeeded())
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();
585 location = toWebGLUniformLocation(info[uniformLocationArgumentIndex], info.GetIsolate());
588 WebGLRenderingContext* context = V8WebGLRenderingContext::toNative(info.Holder());
590 const int indexArrayArgument = 1;
591 if (V8Float32Array::hasInstance(info[indexArrayArgument], info.GetIsolate())) {
592 Float32Array* array = V8Float32Array::toNative(info[indexArrayArgument]->ToObject());
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;
608 if (info[indexArrayArgument].IsEmpty() || !info[indexArrayArgument]->IsArray()) {
609 exceptionState.throwTypeError(ExceptionMessages::argumentNullOrIncorrectType(indexArrayArgument + 1, "Array"));
610 exceptionState.throwIfNeeded();
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())
619 // FIXME: consider different / better exception type.
620 exceptionState.throwDOMException(SyntaxError, "Failed to convert array argument");
621 exceptionState.throwIfNeeded();
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;
638 static void uniformHelperi(const v8::FunctionCallbackInfo<v8::Value>& info, FunctionToCall functionToCall, ExceptionState& exceptionState)
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);
650 if (info.Length() != 2) {
651 exceptionState.throwTypeError(ExceptionMessages::notEnoughArguments(2, info.Length()));
652 exceptionState.throwIfNeeded();
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();
663 WebGLUniformLocation* location = toWebGLUniformLocation(info[uniformLocationArgumentIndex], info.GetIsolate());
665 const int indexArrayArgumentIndex = 1;
666 if (V8Int32Array::hasInstance(info[indexArrayArgumentIndex], info.GetIsolate())) {
667 Int32Array* array = V8Int32Array::toNative(info[indexArrayArgumentIndex]->ToObject());
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;
679 if (info[indexArrayArgumentIndex].IsEmpty() || !info[indexArrayArgumentIndex]->IsArray()) {
680 exceptionState.throwTypeError(ExceptionMessages::argumentNullOrIncorrectType(indexArrayArgumentIndex + 1, "Array"));
681 exceptionState.throwIfNeeded();
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())
690 // FIXME: consider different / better exception type.
691 exceptionState.throwDOMException(SyntaxError, "Failed to convert array argument");
692 exceptionState.throwIfNeeded();
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;
705 void V8WebGLRenderingContext::uniform1fvMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
707 ExceptionState exceptionState(ExceptionState::ExecutionContext, "uniform1fv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
708 vertexAttribAndUniformHelperf(info, kUniform1v, exceptionState);
711 void V8WebGLRenderingContext::uniform1ivMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
713 ExceptionState exceptionState(ExceptionState::ExecutionContext, "uniform1iv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
714 uniformHelperi(info, kUniform1v, exceptionState);
717 void V8WebGLRenderingContext::uniform2fvMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
719 ExceptionState exceptionState(ExceptionState::ExecutionContext, "uniform2fv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
720 vertexAttribAndUniformHelperf(info, kUniform2v, exceptionState);
723 void V8WebGLRenderingContext::uniform2ivMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
725 ExceptionState exceptionState(ExceptionState::ExecutionContext, "uniform2iv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
726 uniformHelperi(info, kUniform2v, exceptionState);
729 void V8WebGLRenderingContext::uniform3fvMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
731 ExceptionState exceptionState(ExceptionState::ExecutionContext, "uniform3fv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
732 vertexAttribAndUniformHelperf(info, kUniform3v, exceptionState);
735 void V8WebGLRenderingContext::uniform3ivMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
737 ExceptionState exceptionState(ExceptionState::ExecutionContext, "uniform3iv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
738 uniformHelperi(info, kUniform3v, exceptionState);
741 void V8WebGLRenderingContext::uniform4fvMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
743 ExceptionState exceptionState(ExceptionState::ExecutionContext, "uniform4fv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
744 vertexAttribAndUniformHelperf(info, kUniform4v, exceptionState);
747 void V8WebGLRenderingContext::uniform4ivMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
749 ExceptionState exceptionState(ExceptionState::ExecutionContext, "uniform4iv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
750 uniformHelperi(info, kUniform4v, exceptionState);
753 static void uniformMatrixHelper(const v8::FunctionCallbackInfo<v8::Value>& info, int matrixSize, ExceptionState& exceptionState)
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);
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();
770 WebGLRenderingContext* context = V8WebGLRenderingContext::toNative(info.Holder());
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();
778 WebGLUniformLocation* location = toWebGLUniformLocation(info[uniformLocationArgumentIndex], info.GetIsolate());
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());
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;
794 if (info[arrayArgumentIndex].IsEmpty() || !info[arrayArgumentIndex]->IsArray()) {
795 exceptionState.throwTypeError(ExceptionMessages::argumentNullOrIncorrectType(arrayArgumentIndex + 1, "Array"));
796 exceptionState.throwIfNeeded();
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())
805 // FIXME: consider different / better exception type.
806 exceptionState.throwDOMException(SyntaxError, "failed to convert Array value");
807 exceptionState.throwIfNeeded();
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;
819 void V8WebGLRenderingContext::uniformMatrix2fvMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
821 ExceptionState exceptionState(ExceptionState::ExecutionContext, "uniformMatrix2fv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
822 uniformMatrixHelper(info, 2, exceptionState);
825 void V8WebGLRenderingContext::uniformMatrix3fvMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
827 ExceptionState exceptionState(ExceptionState::ExecutionContext, "uniformMatrix3fv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
828 uniformMatrixHelper(info, 3, exceptionState);
831 void V8WebGLRenderingContext::uniformMatrix4fvMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
833 ExceptionState exceptionState(ExceptionState::ExecutionContext, "uniformMatrix4fv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
834 uniformMatrixHelper(info, 4, exceptionState);
837 void V8WebGLRenderingContext::vertexAttrib1fvMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
839 ExceptionState exceptionState(ExceptionState::ExecutionContext, "vertexAttrib1fv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
840 vertexAttribAndUniformHelperf(info, kVertexAttrib1v, exceptionState);
843 void V8WebGLRenderingContext::vertexAttrib2fvMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
845 ExceptionState exceptionState(ExceptionState::ExecutionContext, "vertexAttrib2fv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
846 vertexAttribAndUniformHelperf(info, kVertexAttrib2v, exceptionState);
849 void V8WebGLRenderingContext::vertexAttrib3fvMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
851 ExceptionState exceptionState(ExceptionState::ExecutionContext, "vertexAttrib3fv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
852 vertexAttribAndUniformHelperf(info, kVertexAttrib3v, exceptionState);
855 void V8WebGLRenderingContext::vertexAttrib4fvMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
857 ExceptionState exceptionState(ExceptionState::ExecutionContext, "vertexAttrib4fv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
858 vertexAttribAndUniformHelperf(info, kVertexAttrib4v, exceptionState);
861 } // namespace WebCore