Bug 766796. Make IDL conversions to 64-bit ints treat NaN and Infinity as 0 instead of whatever the compiler decides to do in that undefined-behavior case. r=peterv

This commit is contained in:
Boris Zbarsky 2012-06-25 23:37:47 -04:00
parent 6ced091cef
commit ae835cd014
3 changed files with 53 additions and 2 deletions

View File

@ -58,6 +58,7 @@ $(CPPSRCS): ../%Binding.cpp: $(bindinggen_dependencies) \
_TEST_FILES = \
test_enums.html \
test_integers.html \
test_interfaceToString.html \
test_lookupGetter.html \
test_InstanceOf.html \

View File

@ -0,0 +1,39 @@
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<p id="display"></p>
<div id="content" style="display: none">
<canvas id="c" width="1" height="1"></canvas>
</div>
<pre id="test">
<script type="application/javascript">
function testInt64NonFinite(arg) {
// We can use a WebGLRenderingContext to test conversion to 64-bit signed
// ints edge cases.
var gl = $("c").getContext("experimental-webgl");
is(gl.getError(), 0, "Should not start in an error state");
var b = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, b);
var a = new Float32Array(1);
gl.bufferData(gl.ARRAY_BUFFER, a, gl.STATIC_DRAW);
gl.bufferSubData(gl.ARRAY_BUFFER, arg, a);
is(gl.getError(), 0, "Should have treated non-finite double as 0");
}
testInt64NonFinite(NaN);
testInt64NonFinite(Infinity);
testInt64NonFinite(-Infinity);
</script>
</pre>
</body>
</html>

View File

@ -22,6 +22,7 @@
#include "nsStringGlue.h"
#include "nsTArray.h"
#include "mozilla/dom/DOMJSClass.h"
#include "nsMathUtils.h"
class nsIPrincipal;
class nsIXPConnectWrappedJS;
@ -284,7 +285,12 @@ ValueToInt64(JSContext *cx, JS::Value v, int64_t *result)
double doubleval;
if (!JS_ValueToNumber(cx, v, &doubleval))
return false;
*result = static_cast<int64_t>(doubleval);
// Be careful with non-finite doubles
if (NS_finite(doubleval))
// XXXbz this isn't quite right either; need to do the mod thing
*result = static_cast<int64_t>(doubleval);
else
*result = 0;
}
return true;
}
@ -304,7 +310,12 @@ ValueToUint64(JSContext *cx, JS::Value v, uint64_t *result)
double doubleval;
if (!JS_ValueToNumber(cx, v, &doubleval))
return false;
*result = static_cast<uint64_t>(doubleval);
// Be careful with non-finite doubles
if (NS_finite(doubleval))
// XXXbz this isn't quite right either; need to do the mod thing
*result = static_cast<uint64_t>(doubleval);
else
*result = 0;
}
return true;
}