Bug 1233111 - Add a new ToUint8() function. r?efaust
This is needed for casting values into a SIMD.Uint8x16 lane.
--- a/js/public/Conversions.h
+++ b/js/public/Conversions.h
@@ -31,16 +31,20 @@ ToBooleanSlow(JS::HandleValue v);
/* DO NOT CALL THIS. Use JS::ToNumber. */
extern JS_PUBLIC_API(bool)
ToNumberSlow(JSContext* cx, JS::Value v, double* dp);
/* DO NOT CALL THIS. Use JS::ToInt8. */
extern JS_PUBLIC_API(bool)
ToInt8Slow(JSContext *cx, JS::HandleValue v, int8_t *out);
+/* DO NOT CALL THIS. Use JS::ToUint8. */
+extern JS_PUBLIC_API(bool)
+ToUint8Slow(JSContext *cx, JS::HandleValue v, uint8_t *out);
+
/* DO NOT CALL THIS. Use JS::ToInt16. */
extern JS_PUBLIC_API(bool)
ToInt16Slow(JSContext *cx, JS::HandleValue v, int16_t *out);
/* DO NOT CALL THIS. Use JS::ToInt32. */
extern JS_PUBLIC_API(bool)
ToInt32Slow(JSContext* cx, JS::HandleValue v, int32_t* out);
@@ -210,16 +214,29 @@ ToInt8(JSContext *cx, JS::HandleValue v,
if (v.isInt32()) {
*out = int8_t(v.toInt32());
return true;
}
return js::ToInt8Slow(cx, v, out);
}
+/* ES6 ECMA-262, 7.1.10 */
+MOZ_ALWAYS_INLINE bool
+ToUint8(JSContext *cx, JS::HandleValue v, uint8_t *out)
+{
+ detail::AssertArgumentsAreSane(cx, v);
+
+ if (v.isInt32()) {
+ *out = uint8_t(v.toInt32());
+ return true;
+ }
+ return js::ToUint8Slow(cx, v, out);
+}
+
/*
* Non-standard, with behavior similar to that of ToInt32, except in its
* producing an int64_t.
*/
MOZ_ALWAYS_INLINE bool
ToInt64(JSContext* cx, HandleValue v, int64_t* out)
{
detail::AssertArgumentsAreSane(cx, v);
@@ -520,16 +537,23 @@ ToUint32(double d)
/* WEBIDL 4.2.4 */
inline int8_t
ToInt8(double d)
{
return detail::ToIntWidth<int8_t>(d);
}
+/* ECMA-262 7.1.10 ToUInt8() specialized for doubles. */
+inline int8_t
+ToUint8(double d)
+{
+ return detail::ToUintWidth<uint8_t>(d);
+}
+
/* WEBIDL 4.2.6 */
inline int16_t
ToInt16(double d)
{
return detail::ToIntWidth<int16_t>(d);
}
/* WEBIDL 4.2.10 */
--- a/js/src/jsnum.cpp
+++ b/js/src/jsnum.cpp
@@ -1569,16 +1569,35 @@ js::ToInt8Slow(JSContext *cx, const Hand
if (!ToNumberSlow(cx, v, &d))
return false;
}
*out = ToInt8(d);
return true;
}
/*
+ * Convert a value to an uint8_t, according to the ToUInt8() function in ES6
+ * ECMA-262, 7.1.10. Return converted value in *out on success, false on failure.
+ */
+JS_PUBLIC_API(bool)
+js::ToUint8Slow(JSContext *cx, const HandleValue v, uint8_t *out)
+{
+ MOZ_ASSERT(!v.isInt32());
+ double d;
+ if (v.isDouble()) {
+ d = v.toDouble();
+ } else {
+ if (!ToNumberSlow(cx, v, &d))
+ return false;
+ }
+ *out = ToInt8(d);
+ return true;
+}
+
+/*
* Convert a value to an int16_t, according to the WebIDL rules for short
* conversion. Return converted value in *out on success, false on failure.
*/
JS_PUBLIC_API(bool)
js::ToInt16Slow(JSContext *cx, const HandleValue v, int16_t *out)
{
MOZ_ASSERT(!v.isInt32());
double d;