<!--
Copyright (c) 2019 The Khronos Group Inc.
Use of this source code is governed by an MIT-style license that can be
found in the LICENSE.txt file.
-->

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="../../resources/js-test-style.css"/>
<script src="../../js/js-test-pre.js"></script>
<script src="../../js/webgl-test-utils.js"></script>
<script src="../../js/test-eval.js"></script>
</head>
<body>
<div id="description"></div>
<div id="console"></div>

<script>
"use strict";
var wtu = WebGLTestUtils;
description("Test that conversion of boolean arguments of WebGL functions follows EcmaScript 9.2. ToBoolean");
debug("");
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.");
debug("");
var gl = wtu.create3DContext();
var program = wtu.loadStandardProgram(gl);
var shader = wtu.loadStandardVertexShader(gl);
var shouldGenerateGLError = wtu.shouldGenerateGLError;

assertMsg(program != null, "Program Compiled");
assertMsg(shader != null, "Shader Compiled");

var uloc = gl.getUniformLocation(program, "u_modelViewProjMatrix");
var aloc = gl.getAttribLocation(program, "a_vertex");

gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer());

wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from initialization.");
assertMsg(uloc, "Uniform not found");
assertMsg(aloc >= 0, "Attribute not found");

var boolArg = { valueOf: function() { throw "Converting an Object to a Boolean should just give 'true' without further evaluation"; } }

function shouldNotThrowWithBoolArgs(code) {
  try {
    TestEval(code);
  } catch(e) {
    testFailed(code + " threw exception: " + e);
    return;
  }
  testPassed(code + " converted its boolean arguments correctly");
}

shouldNotThrowWithBoolArgs(
  "gl.colorMask(boolArg, boolArg, boolArg, boolArg)"
);

shouldNotThrowWithBoolArgs(
  "gl.depthMask(boolArg)"
);

shouldNotThrowWithBoolArgs(
  "gl.sampleCoverage(1, boolArg)"
);

function zeroArray(length) {
  var a = new Array(length);
  for (var i = 0; i < length; i++)
    a[i] = 0;
  return a;
}

function zeroFloat32Array(length) {
  var a = new Float32Array(length);
  for (var i = 0; i < length; i++)
    a[i] = 0;
  return a;
}

shouldNotThrowWithBoolArgs(
  "gl.uniformMatrix2fv(uloc, boolArg, zeroFloat32Array(4))"
);

shouldNotThrowWithBoolArgs(
  "gl.uniformMatrix2fv(uloc, boolArg, zeroArray(4))"
);

shouldNotThrowWithBoolArgs(
  "gl.uniformMatrix3fv(uloc, boolArg, zeroFloat32Array(9))"
);

shouldNotThrowWithBoolArgs(
  "gl.uniformMatrix3fv(uloc, boolArg, zeroArray(9))"
);

shouldNotThrowWithBoolArgs(
  "gl.uniformMatrix4fv(uloc, boolArg, zeroFloat32Array(16))"
);

shouldNotThrowWithBoolArgs(
  "gl.uniformMatrix4fv(uloc, boolArg, zeroArray(16))"
);

shouldNotThrowWithBoolArgs(
  "gl.vertexAttribPointer(aloc, 4, gl.FLOAT, boolArg, 4, 0)"
);

var successfullyParsed = true;
</script>

<script src="../../js/js-test-post.js"></script>
</body>
</html>
