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 "bindings/core/v8/V8WebGLRenderingContext.h"
34 #include "bindings/core/v8/ExceptionMessages.h"
35 #include "bindings/core/v8/V8ANGLEInstancedArrays.h"
36 #include "bindings/core/v8/V8Binding.h"
37 #include "bindings/core/v8/V8EXTBlendMinMax.h"
38 #include "bindings/core/v8/V8EXTFragDepth.h"
39 #include "bindings/core/v8/V8EXTShaderTextureLOD.h"
40 #include "bindings/core/v8/V8EXTTextureFilterAnisotropic.h"
41 #include "bindings/core/v8/V8HTMLCanvasElement.h"
42 #include "bindings/core/v8/V8HTMLImageElement.h"
43 #include "bindings/core/v8/V8HTMLVideoElement.h"
44 #include "bindings/core/v8/V8HiddenValue.h"
45 #include "bindings/core/v8/V8ImageData.h"
46 #include "bindings/core/v8/V8OESElementIndexUint.h"
47 #include "bindings/core/v8/V8OESStandardDerivatives.h"
48 #include "bindings/core/v8/V8OESTextureFloat.h"
49 #include "bindings/core/v8/V8OESTextureFloatLinear.h"
50 #include "bindings/core/v8/V8OESTextureHalfFloat.h"
51 #include "bindings/core/v8/V8OESTextureHalfFloatLinear.h"
52 #include "bindings/core/v8/V8OESVertexArrayObject.h"
53 #include "bindings/core/v8/V8WebGLBuffer.h"
54 #include "bindings/core/v8/V8WebGLCompressedTextureATC.h"
55 #include "bindings/core/v8/V8WebGLCompressedTextureETC1.h"
56 #include "bindings/core/v8/V8WebGLCompressedTexturePVRTC.h"
57 #include "bindings/core/v8/V8WebGLCompressedTextureS3TC.h"
58 #include "bindings/core/v8/V8WebGLDebugRendererInfo.h"
59 #include "bindings/core/v8/V8WebGLDebugShaders.h"
60 #include "bindings/core/v8/V8WebGLDepthTexture.h"
61 #include "bindings/core/v8/V8WebGLDrawBuffers.h"
62 #include "bindings/core/v8/V8WebGLFramebuffer.h"
63 #include "bindings/core/v8/V8WebGLLoseContext.h"
64 #include "bindings/core/v8/V8WebGLProgram.h"
65 #include "bindings/core/v8/V8WebGLRenderbuffer.h"
66 #include "bindings/core/v8/V8WebGLShader.h"
67 #include "bindings/core/v8/V8WebGLTexture.h"
68 #include "bindings/core/v8/V8WebGLUniformLocation.h"
69 #include "bindings/core/v8/V8WebGLVertexArrayObjectOES.h"
70 #include "bindings/core/v8/custom/V8ArrayBufferViewCustom.h"
71 #include "bindings/core/v8/custom/V8Float32ArrayCustom.h"
72 #include "bindings/core/v8/custom/V8Int16ArrayCustom.h"
73 #include "bindings/core/v8/custom/V8Int32ArrayCustom.h"
74 #include "bindings/core/v8/custom/V8Int8ArrayCustom.h"
75 #include "bindings/core/v8/custom/V8Uint16ArrayCustom.h"
76 #include "bindings/core/v8/custom/V8Uint32ArrayCustom.h"
77 #include "bindings/core/v8/custom/V8Uint8ArrayCustom.h"
78 #include "core/dom/ExceptionCode.h"
79 #include "core/html/canvas/WebGLRenderingContext.h"
80 #include "platform/NotImplemented.h"
81 #include "wtf/FastMalloc.h"
86 // Allocates new storage via fastMalloc.
87 // Returns 0 if array failed to convert for any reason.
88 static float* jsArrayToFloatArray(v8::Handle<v8::Array> array, uint32_t len, ExceptionState& exceptionState)
90 // Convert the data element-by-element.
91 if (len > std::numeric_limits<uint32_t>::max() / sizeof(float)) {
92 exceptionState.throwTypeError("Array length exceeds supported limit.");
95 float* data = static_cast<float*>(fastMalloc(len * sizeof(float)));
97 for (uint32_t i = 0; i < len; i++) {
98 v8::Local<v8::Value> val = array->Get(i);
99 float value = toFloat(val, exceptionState);
100 if (exceptionState.hadException()) {
109 // Allocates new storage via fastMalloc.
110 // Returns 0 if array failed to convert for any reason.
111 static int* jsArrayToIntArray(v8::Handle<v8::Array> array, uint32_t len, ExceptionState& exceptionState)
113 // Convert the data element-by-element.
114 if (len > std::numeric_limits<uint32_t>::max() / sizeof(int)) {
115 exceptionState.throwTypeError("Array length exceeds supported limit.");
118 int* data = static_cast<int*>(fastMalloc(len * sizeof(int)));
120 for (uint32_t i = 0; i < len; i++) {
121 v8::Local<v8::Value> val = array->Get(i);
122 int ival = toInt32(val, exceptionState);
123 if (exceptionState.hadException()) {
132 static v8::Handle<v8::Value> toV8Object(const WebGLGetInfo& args, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
134 switch (args.getType()) {
135 case WebGLGetInfo::kTypeBool:
136 return v8Boolean(args.getBool(), isolate);
137 case WebGLGetInfo::kTypeBoolArray: {
138 const Vector<bool>& value = args.getBoolArray();
139 v8::Local<v8::Array> array = v8::Array::New(isolate, value.size());
140 for (size_t ii = 0; ii < value.size(); ++ii)
141 array->Set(v8::Integer::New(isolate, ii), v8Boolean(value[ii], isolate));
144 case WebGLGetInfo::kTypeFloat:
145 return v8::Number::New(isolate, args.getFloat());
146 case WebGLGetInfo::kTypeInt:
147 return v8::Integer::New(isolate, args.getInt());
148 case WebGLGetInfo::kTypeNull:
149 return v8::Null(isolate);
150 case WebGLGetInfo::kTypeString:
151 return v8String(isolate, args.getString());
152 case WebGLGetInfo::kTypeUnsignedInt:
153 return v8::Integer::NewFromUnsigned(isolate, args.getUnsignedInt());
154 case WebGLGetInfo::kTypeWebGLBuffer:
155 return toV8(args.getWebGLBuffer(), creationContext, isolate);
156 case WebGLGetInfo::kTypeWebGLFloatArray:
157 return toV8(args.getWebGLFloatArray(), creationContext, isolate);
158 case WebGLGetInfo::kTypeWebGLFramebuffer:
159 return toV8(args.getWebGLFramebuffer(), creationContext, isolate);
160 case WebGLGetInfo::kTypeWebGLIntArray:
161 return toV8(args.getWebGLIntArray(), creationContext, isolate);
162 // FIXME: implement WebGLObjectArray
163 // case WebGLGetInfo::kTypeWebGLObjectArray:
164 case WebGLGetInfo::kTypeWebGLProgram:
165 return toV8(args.getWebGLProgram(), creationContext, isolate);
166 case WebGLGetInfo::kTypeWebGLRenderbuffer:
167 return toV8(args.getWebGLRenderbuffer(), creationContext, isolate);
168 case WebGLGetInfo::kTypeWebGLTexture:
169 return toV8(args.getWebGLTexture(), creationContext, isolate);
170 case WebGLGetInfo::kTypeWebGLUnsignedByteArray:
171 return toV8(args.getWebGLUnsignedByteArray(), creationContext, isolate);
172 case WebGLGetInfo::kTypeWebGLUnsignedIntArray:
173 return toV8(args.getWebGLUnsignedIntArray(), creationContext, isolate);
174 case WebGLGetInfo::kTypeWebGLVertexArrayObjectOES:
175 return toV8(args.getWebGLVertexArrayObjectOES(), creationContext, isolate);
178 return v8::Undefined(isolate);
182 static v8::Handle<v8::Value> toV8Object(WebGLExtension* extension, v8::Handle<v8::Object> contextObject, v8::Isolate* isolate)
185 return v8::Null(isolate);
186 v8::Handle<v8::Value> extensionObject;
187 const char* referenceName = 0;
188 switch (extension->name()) {
189 case ANGLEInstancedArraysName:
190 extensionObject = toV8(static_cast<ANGLEInstancedArrays*>(extension), contextObject, isolate);
191 referenceName = "angleInstancedArraysName";
193 case EXTBlendMinMaxName:
194 extensionObject = toV8(static_cast<EXTBlendMinMax*>(extension), contextObject, isolate);
195 referenceName = "extBlendMinMaxName";
197 case EXTFragDepthName:
198 extensionObject = toV8(static_cast<EXTFragDepth*>(extension), contextObject, isolate);
199 referenceName = "extFragDepthName";
201 case EXTShaderTextureLODName:
202 extensionObject = toV8(static_cast<EXTShaderTextureLOD*>(extension), contextObject, isolate);
203 referenceName = "extShaderTextureLODName";
205 case EXTTextureFilterAnisotropicName:
206 extensionObject = toV8(static_cast<EXTTextureFilterAnisotropic*>(extension), contextObject, isolate);
207 referenceName = "extTextureFilterAnisotropicName";
209 case OESElementIndexUintName:
210 extensionObject = toV8(static_cast<OESElementIndexUint*>(extension), contextObject, isolate);
211 referenceName = "oesElementIndexUintName";
213 case OESStandardDerivativesName:
214 extensionObject = toV8(static_cast<OESStandardDerivatives*>(extension), contextObject, isolate);
215 referenceName = "oesStandardDerivativesName";
217 case OESTextureFloatName:
218 extensionObject = toV8(static_cast<OESTextureFloat*>(extension), contextObject, isolate);
219 referenceName = "oesTextureFloatName";
221 case OESTextureFloatLinearName:
222 extensionObject = toV8(static_cast<OESTextureFloatLinear*>(extension), contextObject, isolate);
223 referenceName = "oesTextureFloatLinearName";
225 case OESTextureHalfFloatName:
226 extensionObject = toV8(static_cast<OESTextureHalfFloat*>(extension), contextObject, isolate);
227 referenceName = "oesTextureHalfFloatName";
229 case OESTextureHalfFloatLinearName:
230 extensionObject = toV8(static_cast<OESTextureHalfFloatLinear*>(extension), contextObject, isolate);
231 referenceName = "oesTextureHalfFloatLinearName";
233 case OESVertexArrayObjectName:
234 extensionObject = toV8(static_cast<OESVertexArrayObject*>(extension), contextObject, isolate);
235 referenceName = "oesVertexArrayObjectName";
237 case WebGLCompressedTextureATCName:
238 extensionObject = toV8(static_cast<WebGLCompressedTextureATC*>(extension), contextObject, isolate);
239 referenceName = "webGLCompressedTextureATCName";
241 case WebGLCompressedTextureETC1Name:
242 extensionObject = toV8(static_cast<WebGLCompressedTextureETC1*>(extension), contextObject, isolate);
243 referenceName = "webGLCompressedTextureETC1Name";
245 case WebGLCompressedTexturePVRTCName:
246 extensionObject = toV8(static_cast<WebGLCompressedTexturePVRTC*>(extension), contextObject, isolate);
247 referenceName = "webGLCompressedTexturePVRTCName";
249 case WebGLCompressedTextureS3TCName:
250 extensionObject = toV8(static_cast<WebGLCompressedTextureS3TC*>(extension), contextObject, isolate);
251 referenceName = "webGLCompressedTextureS3TCName";
253 case WebGLDebugRendererInfoName:
254 extensionObject = toV8(static_cast<WebGLDebugRendererInfo*>(extension), contextObject, isolate);
255 referenceName = "webGLDebugRendererInfoName";
257 case WebGLDebugShadersName:
258 extensionObject = toV8(static_cast<WebGLDebugShaders*>(extension), contextObject, isolate);
259 referenceName = "webGLDebugShadersName";
261 case WebGLDepthTextureName:
262 extensionObject = toV8(static_cast<WebGLDepthTexture*>(extension), contextObject, isolate);
263 referenceName = "webGLDepthTextureName";
265 case WebGLDrawBuffersName:
266 extensionObject = toV8(static_cast<WebGLDrawBuffers*>(extension), contextObject, isolate);
267 referenceName = "webGLDrawBuffersName";
269 case WebGLLoseContextName:
270 extensionObject = toV8(static_cast<WebGLLoseContext*>(extension), contextObject, isolate);
271 referenceName = "webGLLoseContextName";
273 case WebGLExtensionNameCount:
275 return v8::Undefined(isolate);
277 ASSERT(!extensionObject.IsEmpty());
278 V8HiddenValue::setHiddenValue(isolate, contextObject, v8AtomicString(isolate, referenceName), extensionObject);
279 return extensionObject;
283 kBuffer, kRenderbuffer, kTexture, kVertexAttrib
286 static void getObjectParameter(const v8::FunctionCallbackInfo<v8::Value>& info, ObjectType objectType, ExceptionState& exceptionState)
288 if (info.Length() != 2) {
289 exceptionState.throwTypeError(ExceptionMessages::notEnoughArguments(2, info.Length()));
290 exceptionState.throwIfNeeded();
294 WebGLRenderingContext* context = V8WebGLRenderingContext::toNative(info.Holder());
299 V8RethrowTryCatchScope rethrow(block);
300 TONATIVE_VOID_EXCEPTIONSTATE_INTERNAL(target, toUInt32(info[0], exceptionState), exceptionState);
301 TONATIVE_VOID_EXCEPTIONSTATE_INTERNAL(pname, toUInt32(info[1], exceptionState), exceptionState);
304 switch (objectType) {
306 args = context->getBufferParameter(target, pname);
309 args = context->getRenderbufferParameter(target, pname);
312 args = context->getTexParameter(target, pname);
316 args = context->getVertexAttrib(target, pname);
322 v8SetReturnValue(info, toV8Object(args, info.Holder(), info.GetIsolate()));
325 static WebGLUniformLocation* toWebGLUniformLocation(v8::Handle<v8::Value> value, v8::Isolate* isolate)
327 return V8WebGLUniformLocation::toNativeWithTypeCheck(isolate, value);
330 void V8WebGLRenderingContext::getBufferParameterMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
332 ExceptionState exceptionState(ExceptionState::ExecutionContext, "getBufferParameter", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
333 getObjectParameter(info, kBuffer, exceptionState);
336 void V8WebGLRenderingContext::getExtensionMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
338 ExceptionState exceptionState(ExceptionState::ExecutionContext, "getExtension", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
339 WebGLRenderingContext* impl = V8WebGLRenderingContext::toNative(info.Holder());
340 if (info.Length() < 1) {
341 exceptionState.throwTypeError(ExceptionMessages::notEnoughArguments(1, info.Length()));
342 exceptionState.throwIfNeeded();
345 TOSTRING_VOID(V8StringResource<>, name, info[0]);
346 RefPtrWillBeRawPtr<WebGLExtension> extension(impl->getExtension(name));
347 v8SetReturnValue(info, toV8Object(extension.get(), info.Holder(), info.GetIsolate()));
350 void V8WebGLRenderingContext::getFramebufferAttachmentParameterMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
352 ExceptionState exceptionState(ExceptionState::ExecutionContext, "getFramebufferAttachmentParameter", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
353 if (info.Length() != 3) {
354 exceptionState.throwTypeError(ExceptionMessages::notEnoughArguments(3, info.Length()));
355 exceptionState.throwIfNeeded();
359 WebGLRenderingContext* context = V8WebGLRenderingContext::toNative(info.Holder());
365 V8RethrowTryCatchScope rethrow(block);
366 TONATIVE_VOID_EXCEPTIONSTATE_INTERNAL(target, toUInt32(info[0], exceptionState), exceptionState);
367 TONATIVE_VOID_EXCEPTIONSTATE_INTERNAL(attachment, toUInt32(info[1], exceptionState), exceptionState);
368 TONATIVE_VOID_EXCEPTIONSTATE_INTERNAL(pname, toUInt32(info[2], exceptionState), exceptionState);
370 WebGLGetInfo args = context->getFramebufferAttachmentParameter(target, attachment, pname);
371 v8SetReturnValue(info, toV8Object(args, info.Holder(), info.GetIsolate()));
374 void V8WebGLRenderingContext::getParameterMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
376 ExceptionState exceptionState(ExceptionState::ExecutionContext, "getParameter", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
377 if (info.Length() != 1) {
378 exceptionState.throwTypeError(ExceptionMessages::notEnoughArguments(1, info.Length()));
379 exceptionState.throwIfNeeded();
383 WebGLRenderingContext* context = V8WebGLRenderingContext::toNative(info.Holder());
387 V8RethrowTryCatchScope rethrow(block);
388 TONATIVE_VOID_EXCEPTIONSTATE_INTERNAL(pname, toUInt32(info[0], exceptionState), exceptionState);
390 WebGLGetInfo args = context->getParameter(pname);
391 v8SetReturnValue(info, toV8Object(args, info.Holder(), info.GetIsolate()));
394 void V8WebGLRenderingContext::getProgramParameterMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
396 ExceptionState exceptionState(ExceptionState::ExecutionContext, "getProgramParameter", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
397 if (info.Length() != 2) {
398 exceptionState.throwTypeError(ExceptionMessages::notEnoughArguments(2, info.Length()));
399 exceptionState.throwIfNeeded();
403 WebGLRenderingContext* context = V8WebGLRenderingContext::toNative(info.Holder());
404 WebGLProgram* program;
408 V8RethrowTryCatchScope rethrow(block);
409 if (info.Length() > 0 && !isUndefinedOrNull(info[0]) && !V8WebGLProgram::hasInstance(info[0], info.GetIsolate())) {
410 exceptionState.throwTypeError("parameter 1 is not of type 'WebGLProgram'.");
411 exceptionState.throwIfNeeded();
414 TONATIVE_VOID_INTERNAL(program, V8WebGLProgram::toNativeWithTypeCheck(info.GetIsolate(), info[0]));
415 TONATIVE_VOID_EXCEPTIONSTATE_INTERNAL(pname, toUInt32(info[1], exceptionState), exceptionState);
417 WebGLGetInfo args = context->getProgramParameter(program, pname);
418 v8SetReturnValue(info, toV8Object(args, info.Holder(), info.GetIsolate()));
421 void V8WebGLRenderingContext::getRenderbufferParameterMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
423 ExceptionState exceptionState(ExceptionState::ExecutionContext, "getRenderbufferParameter", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
424 getObjectParameter(info, kRenderbuffer, exceptionState);
427 void V8WebGLRenderingContext::getShaderParameterMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
429 ExceptionState exceptionState(ExceptionState::ExecutionContext, "getShaderParameter", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
430 if (info.Length() != 2) {
431 exceptionState.throwTypeError(ExceptionMessages::notEnoughArguments(2, info.Length()));
432 exceptionState.throwIfNeeded();
436 WebGLRenderingContext* context = V8WebGLRenderingContext::toNative(info.Holder());
441 V8RethrowTryCatchScope rethrow(block);
442 if (info.Length() > 0 && !isUndefinedOrNull(info[0]) && !V8WebGLShader::hasInstance(info[0], info.GetIsolate())) {
443 exceptionState.throwTypeError("parameter 1 is not of type 'WebGLShader'.");
444 exceptionState.throwIfNeeded();
447 TONATIVE_VOID_INTERNAL(shader, V8WebGLShader::toNativeWithTypeCheck(info.GetIsolate(), info[0]));
448 TONATIVE_VOID_EXCEPTIONSTATE_INTERNAL(pname, toUInt32(info[1], exceptionState), exceptionState);
450 WebGLGetInfo args = context->getShaderParameter(shader, pname);
451 v8SetReturnValue(info, toV8Object(args, info.Holder(), info.GetIsolate()));
454 void V8WebGLRenderingContext::getTexParameterMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
456 ExceptionState exceptionState(ExceptionState::ExecutionContext, "getTexParameter", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
457 getObjectParameter(info, kTexture, exceptionState);
460 void V8WebGLRenderingContext::getUniformMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
462 ExceptionState exceptionState(ExceptionState::ExecutionContext, "getUniform", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
463 if (info.Length() != 2) {
464 exceptionState.throwTypeError(ExceptionMessages::notEnoughArguments(2, info.Length()));
465 exceptionState.throwIfNeeded();
469 WebGLRenderingContext* context = V8WebGLRenderingContext::toNative(info.Holder());
470 WebGLProgram* program;
471 WebGLUniformLocation* location;
474 V8RethrowTryCatchScope rethrow(block);
475 if (info.Length() > 0 && !isUndefinedOrNull(info[0]) && !V8WebGLProgram::hasInstance(info[0], info.GetIsolate())) {
476 V8ThrowException::throwTypeError(ExceptionMessages::failedToExecute("getUniform", "WebGLRenderingContext", "parameter 1 is not of type 'WebGLProgram'."), info.GetIsolate());
479 TONATIVE_VOID_INTERNAL(program, V8WebGLProgram::toNativeWithTypeCheck(info.GetIsolate(), info[0]));
480 if (info.Length() > 1 && !isUndefinedOrNull(info[1]) && !V8WebGLUniformLocation::hasInstance(info[1], info.GetIsolate())) {
481 V8ThrowException::throwTypeError(ExceptionMessages::failedToExecute("getUniform", "WebGLRenderingContext", "parameter 2 is not of type 'WebGLUniformLocation'."), info.GetIsolate());
484 TONATIVE_VOID_INTERNAL(location, V8WebGLUniformLocation::toNativeWithTypeCheck(info.GetIsolate(), info[1]));
486 WebGLGetInfo args = context->getUniform(program, location);
487 v8SetReturnValue(info, toV8Object(args, info.Holder(), info.GetIsolate()));
490 void V8WebGLRenderingContext::getVertexAttribMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
492 ExceptionState exceptionState(ExceptionState::ExecutionContext, "getVertexAttrib", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
493 getObjectParameter(info, kVertexAttrib, exceptionState);
496 enum FunctionToCall {
497 kUniform1v, kUniform2v, kUniform3v, kUniform4v,
498 kVertexAttrib1v, kVertexAttrib2v, kVertexAttrib3v, kVertexAttrib4v
501 bool isFunctionToCallForAttribute(FunctionToCall functionToCall)
503 switch (functionToCall) {
504 case kVertexAttrib1v:
505 case kVertexAttrib2v:
506 case kVertexAttrib3v:
507 case kVertexAttrib4v:
515 static void vertexAttribAndUniformHelperf(const v8::FunctionCallbackInfo<v8::Value>& info, FunctionToCall functionToCall, ExceptionState& exceptionState)
518 // * glUniform1fv(WebGLUniformLocation location, Array data);
519 // * glUniform1fv(WebGLUniformLocation location, Float32Array data);
520 // * glUniform2fv(WebGLUniformLocation location, Array data);
521 // * glUniform2fv(WebGLUniformLocation location, Float32Array data);
522 // * glUniform3fv(WebGLUniformLocation location, Array data);
523 // * glUniform3fv(WebGLUniformLocation location, Float32Array data);
524 // * glUniform4fv(WebGLUniformLocation location, Array data);
525 // * glUniform4fv(WebGLUniformLocation location, Float32Array data);
526 // * glVertexAttrib1fv(GLint index, Array data);
527 // * glVertexAttrib1fv(GLint index, Float32Array data);
528 // * glVertexAttrib2fv(GLint index, Array data);
529 // * glVertexAttrib2fv(GLint index, Float32Array data);
530 // * glVertexAttrib3fv(GLint index, Array data);
531 // * glVertexAttrib3fv(GLint index, Float32Array data);
532 // * glVertexAttrib4fv(GLint index, Array data);
533 // * glVertexAttrib4fv(GLint index, Float32Array data);
535 if (info.Length() != 2) {
536 exceptionState.throwTypeError(ExceptionMessages::notEnoughArguments(2, info.Length()));
537 exceptionState.throwIfNeeded();
542 WebGLUniformLocation* location = 0;
544 if (isFunctionToCallForAttribute(functionToCall)) {
545 index = toInt32(info[0], exceptionState);
546 if (exceptionState.throwIfNeeded())
549 const int uniformLocationArgumentIndex = 0;
550 if (info.Length() > 0 && !isUndefinedOrNull(info[uniformLocationArgumentIndex]) && !V8WebGLUniformLocation::hasInstance(info[uniformLocationArgumentIndex], info.GetIsolate())) {
551 exceptionState.throwTypeError(ExceptionMessages::argumentNullOrIncorrectType(uniformLocationArgumentIndex + 1, "WebGLUniformLocation"));
552 exceptionState.throwIfNeeded();
555 location = toWebGLUniformLocation(info[uniformLocationArgumentIndex], info.GetIsolate());
558 WebGLRenderingContext* context = V8WebGLRenderingContext::toNative(info.Holder());
560 const int indexArrayArgument = 1;
561 if (V8Float32Array::hasInstance(info[indexArrayArgument], info.GetIsolate())) {
562 Float32Array* array = V8Float32Array::toNative(info[indexArrayArgument]->ToObject());
564 switch (functionToCall) {
565 case kUniform1v: context->uniform1fv(location, array); break;
566 case kUniform2v: context->uniform2fv(location, array); break;
567 case kUniform3v: context->uniform3fv(location, array); break;
568 case kUniform4v: context->uniform4fv(location, array); break;
569 case kVertexAttrib1v: context->vertexAttrib1fv(index, array); break;
570 case kVertexAttrib2v: context->vertexAttrib2fv(index, array); break;
571 case kVertexAttrib3v: context->vertexAttrib3fv(index, array); break;
572 case kVertexAttrib4v: context->vertexAttrib4fv(index, array); break;
573 default: ASSERT_NOT_REACHED(); break;
578 if (info[indexArrayArgument].IsEmpty() || !info[indexArrayArgument]->IsArray()) {
579 exceptionState.throwTypeError(ExceptionMessages::argumentNullOrIncorrectType(indexArrayArgument + 1, "Array"));
580 exceptionState.throwIfNeeded();
583 v8::Handle<v8::Array> array = v8::Local<v8::Array>::Cast(info[1]);
584 uint32_t len = array->Length();
585 float* data = jsArrayToFloatArray(array, len, exceptionState);
586 if (exceptionState.throwIfNeeded())
589 // FIXME: consider different / better exception type.
590 exceptionState.throwDOMException(SyntaxError, "Failed to convert array argument");
591 exceptionState.throwIfNeeded();
594 switch (functionToCall) {
595 case kUniform1v: context->uniform1fv(location, data, len); break;
596 case kUniform2v: context->uniform2fv(location, data, len); break;
597 case kUniform3v: context->uniform3fv(location, data, len); break;
598 case kUniform4v: context->uniform4fv(location, data, len); break;
599 case kVertexAttrib1v: context->vertexAttrib1fv(index, data, len); break;
600 case kVertexAttrib2v: context->vertexAttrib2fv(index, data, len); break;
601 case kVertexAttrib3v: context->vertexAttrib3fv(index, data, len); break;
602 case kVertexAttrib4v: context->vertexAttrib4fv(index, data, len); break;
603 default: ASSERT_NOT_REACHED(); break;
608 static void uniformHelperi(const v8::FunctionCallbackInfo<v8::Value>& info, FunctionToCall functionToCall, ExceptionState& exceptionState)
611 // * glUniform1iv(GLUniformLocation location, Array data);
612 // * glUniform1iv(GLUniformLocation location, Int32Array data);
613 // * glUniform2iv(GLUniformLocation location, Array data);
614 // * glUniform2iv(GLUniformLocation location, Int32Array data);
615 // * glUniform3iv(GLUniformLocation location, Array data);
616 // * glUniform3iv(GLUniformLocation location, Int32Array data);
617 // * glUniform4iv(GLUniformLocation location, Array data);
618 // * glUniform4iv(GLUniformLocation location, Int32Array data);
620 if (info.Length() != 2) {
621 exceptionState.throwTypeError(ExceptionMessages::notEnoughArguments(2, info.Length()));
622 exceptionState.throwIfNeeded();
626 const int uniformLocationArgumentIndex = 0;
627 WebGLRenderingContext* context = V8WebGLRenderingContext::toNative(info.Holder());
628 if (info.Length() > 0 && !isUndefinedOrNull(info[uniformLocationArgumentIndex]) && !V8WebGLUniformLocation::hasInstance(info[uniformLocationArgumentIndex], info.GetIsolate())) {
629 exceptionState.throwTypeError(ExceptionMessages::argumentNullOrIncorrectType(uniformLocationArgumentIndex + 1, "WebGLUniformLocation"));
630 exceptionState.throwIfNeeded();
633 WebGLUniformLocation* location = toWebGLUniformLocation(info[uniformLocationArgumentIndex], info.GetIsolate());
635 const int indexArrayArgumentIndex = 1;
636 if (V8Int32Array::hasInstance(info[indexArrayArgumentIndex], info.GetIsolate())) {
637 Int32Array* array = V8Int32Array::toNative(info[indexArrayArgumentIndex]->ToObject());
639 switch (functionToCall) {
640 case kUniform1v: context->uniform1iv(location, array); break;
641 case kUniform2v: context->uniform2iv(location, array); break;
642 case kUniform3v: context->uniform3iv(location, array); break;
643 case kUniform4v: context->uniform4iv(location, array); break;
644 default: ASSERT_NOT_REACHED(); break;
649 if (info[indexArrayArgumentIndex].IsEmpty() || !info[indexArrayArgumentIndex]->IsArray()) {
650 exceptionState.throwTypeError(ExceptionMessages::argumentNullOrIncorrectType(indexArrayArgumentIndex + 1, "Array"));
651 exceptionState.throwIfNeeded();
654 v8::Handle<v8::Array> array = v8::Local<v8::Array>::Cast(info[indexArrayArgumentIndex]);
655 uint32_t len = array->Length();
656 int* data = jsArrayToIntArray(array, len, exceptionState);
657 if (exceptionState.throwIfNeeded())
660 // FIXME: consider different / better exception type.
661 exceptionState.throwDOMException(SyntaxError, "Failed to convert array argument");
662 exceptionState.throwIfNeeded();
665 switch (functionToCall) {
666 case kUniform1v: context->uniform1iv(location, data, len); break;
667 case kUniform2v: context->uniform2iv(location, data, len); break;
668 case kUniform3v: context->uniform3iv(location, data, len); break;
669 case kUniform4v: context->uniform4iv(location, data, len); break;
670 default: ASSERT_NOT_REACHED(); break;
675 void V8WebGLRenderingContext::uniform1fvMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
677 ExceptionState exceptionState(ExceptionState::ExecutionContext, "uniform1fv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
678 vertexAttribAndUniformHelperf(info, kUniform1v, exceptionState);
681 void V8WebGLRenderingContext::uniform1ivMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
683 ExceptionState exceptionState(ExceptionState::ExecutionContext, "uniform1iv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
684 uniformHelperi(info, kUniform1v, exceptionState);
687 void V8WebGLRenderingContext::uniform2fvMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
689 ExceptionState exceptionState(ExceptionState::ExecutionContext, "uniform2fv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
690 vertexAttribAndUniformHelperf(info, kUniform2v, exceptionState);
693 void V8WebGLRenderingContext::uniform2ivMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
695 ExceptionState exceptionState(ExceptionState::ExecutionContext, "uniform2iv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
696 uniformHelperi(info, kUniform2v, exceptionState);
699 void V8WebGLRenderingContext::uniform3fvMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
701 ExceptionState exceptionState(ExceptionState::ExecutionContext, "uniform3fv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
702 vertexAttribAndUniformHelperf(info, kUniform3v, exceptionState);
705 void V8WebGLRenderingContext::uniform3ivMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
707 ExceptionState exceptionState(ExceptionState::ExecutionContext, "uniform3iv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
708 uniformHelperi(info, kUniform3v, exceptionState);
711 void V8WebGLRenderingContext::uniform4fvMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
713 ExceptionState exceptionState(ExceptionState::ExecutionContext, "uniform4fv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
714 vertexAttribAndUniformHelperf(info, kUniform4v, exceptionState);
717 void V8WebGLRenderingContext::uniform4ivMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
719 ExceptionState exceptionState(ExceptionState::ExecutionContext, "uniform4iv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
720 uniformHelperi(info, kUniform4v, exceptionState);
723 static void uniformMatrixHelper(const v8::FunctionCallbackInfo<v8::Value>& info, int matrixSize, ExceptionState& exceptionState)
726 // * glUniformMatrix2fv(GLint location, GLboolean transpose, Array data);
727 // * glUniformMatrix2fv(GLint location, GLboolean transpose, Float32Array data);
728 // * glUniformMatrix3fv(GLint location, GLboolean transpose, Array data);
729 // * glUniformMatrix3fv(GLint location, GLboolean transpose, Float32Array data);
730 // * glUniformMatrix4fv(GLint location, GLboolean transpose, Array data);
731 // * glUniformMatrix4fv(GLint location, GLboolean transpose, Float32Array data);
733 // FIXME: need to change to accept Float32Array as well.
734 if (info.Length() != 3) {
735 exceptionState.throwTypeError(ExceptionMessages::notEnoughArguments(3, info.Length()));
736 exceptionState.throwIfNeeded();
740 WebGLRenderingContext* context = V8WebGLRenderingContext::toNative(info.Holder());
742 const int uniformLocationArgumentIndex = 0;
743 if (info.Length() > 0 && !isUndefinedOrNull(info[uniformLocationArgumentIndex]) && !V8WebGLUniformLocation::hasInstance(info[uniformLocationArgumentIndex], info.GetIsolate())) {
744 exceptionState.throwTypeError(ExceptionMessages::argumentNullOrIncorrectType(uniformLocationArgumentIndex + 1, "WebGLUniformLocation"));
745 exceptionState.throwIfNeeded();
748 WebGLUniformLocation* location = toWebGLUniformLocation(info[uniformLocationArgumentIndex], info.GetIsolate());
750 bool transpose = info[1]->BooleanValue();
751 const int arrayArgumentIndex = 2;
752 if (V8Float32Array::hasInstance(info[arrayArgumentIndex], info.GetIsolate())) {
753 Float32Array* array = V8Float32Array::toNative(info[arrayArgumentIndex]->ToObject());
755 switch (matrixSize) {
756 case 2: context->uniformMatrix2fv(location, transpose, array); break;
757 case 3: context->uniformMatrix3fv(location, transpose, array); break;
758 case 4: context->uniformMatrix4fv(location, transpose, array); break;
759 default: ASSERT_NOT_REACHED(); break;
764 if (info[arrayArgumentIndex].IsEmpty() || !info[arrayArgumentIndex]->IsArray()) {
765 exceptionState.throwTypeError(ExceptionMessages::argumentNullOrIncorrectType(arrayArgumentIndex + 1, "Array"));
766 exceptionState.throwIfNeeded();
769 v8::Handle<v8::Array> array = v8::Local<v8::Array>::Cast(info[2]);
770 uint32_t len = array->Length();
771 float* data = jsArrayToFloatArray(array, len, exceptionState);
772 if (exceptionState.throwIfNeeded())
775 // FIXME: consider different / better exception type.
776 exceptionState.throwDOMException(SyntaxError, "failed to convert Array value");
777 exceptionState.throwIfNeeded();
780 switch (matrixSize) {
781 case 2: context->uniformMatrix2fv(location, transpose, data, len); break;
782 case 3: context->uniformMatrix3fv(location, transpose, data, len); break;
783 case 4: context->uniformMatrix4fv(location, transpose, data, len); break;
784 default: ASSERT_NOT_REACHED(); break;
789 void V8WebGLRenderingContext::uniformMatrix2fvMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
791 ExceptionState exceptionState(ExceptionState::ExecutionContext, "uniformMatrix2fv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
792 uniformMatrixHelper(info, 2, exceptionState);
795 void V8WebGLRenderingContext::uniformMatrix3fvMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
797 ExceptionState exceptionState(ExceptionState::ExecutionContext, "uniformMatrix3fv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
798 uniformMatrixHelper(info, 3, exceptionState);
801 void V8WebGLRenderingContext::uniformMatrix4fvMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
803 ExceptionState exceptionState(ExceptionState::ExecutionContext, "uniformMatrix4fv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
804 uniformMatrixHelper(info, 4, exceptionState);
807 void V8WebGLRenderingContext::vertexAttrib1fvMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
809 ExceptionState exceptionState(ExceptionState::ExecutionContext, "vertexAttrib1fv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
810 vertexAttribAndUniformHelperf(info, kVertexAttrib1v, exceptionState);
813 void V8WebGLRenderingContext::vertexAttrib2fvMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
815 ExceptionState exceptionState(ExceptionState::ExecutionContext, "vertexAttrib2fv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
816 vertexAttribAndUniformHelperf(info, kVertexAttrib2v, exceptionState);
819 void V8WebGLRenderingContext::vertexAttrib3fvMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
821 ExceptionState exceptionState(ExceptionState::ExecutionContext, "vertexAttrib3fv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
822 vertexAttribAndUniformHelperf(info, kVertexAttrib3v, exceptionState);
825 void V8WebGLRenderingContext::vertexAttrib4fvMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
827 ExceptionState exceptionState(ExceptionState::ExecutionContext, "vertexAttrib4fv", "WebGLRenderingContext", info.Holder(), info.GetIsolate());
828 vertexAttribAndUniformHelperf(info, kVertexAttrib4v, exceptionState);