boolean-argument-conversion.html (3166B)
1 <!-- 2 Copyright (c) 2019 The Khronos Group Inc. 3 Use of this source code is governed by an MIT-style license that can be 4 found in the LICENSE.txt file. 5 --> 6 7 <!DOCTYPE html> 8 <html> 9 <head> 10 <meta charset="utf-8"> 11 <link rel="stylesheet" href="../../resources/js-test-style.css"/> 12 <script src="../../js/js-test-pre.js"></script> 13 <script src="../../js/webgl-test-utils.js"></script> 14 <script src="../../js/test-eval.js"></script> 15 </head> 16 <body> 17 <div id="description"></div> 18 <div id="console"></div> 19 20 <script> 21 "use strict"; 22 var wtu = WebGLTestUtils; 23 description("Test that conversion of boolean arguments of WebGL functions follows EcmaScript 9.2. ToBoolean"); 24 debug(""); 25 debug("When an object is converted to a boolean, it should always evaluate as true. Any valueOf() method should not even get called. See Mozilla bug 727590 where Gecko incorrectly converted such an argument to a Number instead of a Boolean, giving the wrong behavior. See 9.2 and 9.3 in the EcmaScript specification."); 26 debug(""); 27 var gl = wtu.create3DContext(); 28 var program = wtu.loadStandardProgram(gl); 29 var shader = wtu.loadStandardVertexShader(gl); 30 var shouldGenerateGLError = wtu.shouldGenerateGLError; 31 32 assertMsg(program != null, "Program Compiled"); 33 assertMsg(shader != null, "Shader Compiled"); 34 35 var uloc = gl.getUniformLocation(program, "u_modelViewProjMatrix"); 36 var aloc = gl.getAttribLocation(program, "a_vertex"); 37 38 gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer()); 39 40 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from initialization."); 41 assertMsg(uloc, "Uniform not found"); 42 assertMsg(aloc >= 0, "Attribute not found"); 43 44 var boolArg = { valueOf: function() { throw "Converting an Object to a Boolean should just give 'true' without further evaluation"; } } 45 46 function shouldNotThrowWithBoolArgs(code) { 47 try { 48 TestEval(code); 49 } catch(e) { 50 testFailed(code + " threw exception: " + e); 51 return; 52 } 53 testPassed(code + " converted its boolean arguments correctly"); 54 } 55 56 shouldNotThrowWithBoolArgs( 57 "gl.colorMask(boolArg, boolArg, boolArg, boolArg)" 58 ); 59 60 shouldNotThrowWithBoolArgs( 61 "gl.depthMask(boolArg)" 62 ); 63 64 shouldNotThrowWithBoolArgs( 65 "gl.sampleCoverage(1, boolArg)" 66 ); 67 68 function zeroArray(length) { 69 var a = new Array(length); 70 for (var i = 0; i < length; i++) 71 a[i] = 0; 72 return a; 73 } 74 75 function zeroFloat32Array(length) { 76 var a = new Float32Array(length); 77 for (var i = 0; i < length; i++) 78 a[i] = 0; 79 return a; 80 } 81 82 shouldNotThrowWithBoolArgs( 83 "gl.uniformMatrix2fv(uloc, boolArg, zeroFloat32Array(4))" 84 ); 85 86 shouldNotThrowWithBoolArgs( 87 "gl.uniformMatrix2fv(uloc, boolArg, zeroArray(4))" 88 ); 89 90 shouldNotThrowWithBoolArgs( 91 "gl.uniformMatrix3fv(uloc, boolArg, zeroFloat32Array(9))" 92 ); 93 94 shouldNotThrowWithBoolArgs( 95 "gl.uniformMatrix3fv(uloc, boolArg, zeroArray(9))" 96 ); 97 98 shouldNotThrowWithBoolArgs( 99 "gl.uniformMatrix4fv(uloc, boolArg, zeroFloat32Array(16))" 100 ); 101 102 shouldNotThrowWithBoolArgs( 103 "gl.uniformMatrix4fv(uloc, boolArg, zeroArray(16))" 104 ); 105 106 shouldNotThrowWithBoolArgs( 107 "gl.vertexAttribPointer(aloc, 4, gl.FLOAT, boolArg, 4, 0)" 108 ); 109 110 var successfullyParsed = true; 111 </script> 112 113 <script src="../../js/js-test-post.js"></script> 114 </body> 115 </html>