Bug 1233111 - Add a new ToUint8() function. r=efaust

This is needed for casting values into a SIMD.Uint8x16 lane.

--HG--
extra : rebase_source : 361feb40a7216cacf9e39c4e264b925cb06da540
This commit is contained in:
Jakob Stoklund Olesen 2015-12-23 09:52:39 -08:00
parent 84f9789075
commit 2d7330a315
2 changed files with 43 additions and 0 deletions

View File

@ -36,6 +36,10 @@ ToNumberSlow(JSContext* cx, JS::Value v, double* dp);
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);
@ -215,6 +219,19 @@ ToInt8(JSContext *cx, JS::HandleValue v, int8_t *out)
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.
@ -525,6 +542,13 @@ 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)

View File

@ -1573,6 +1573,25 @@ js::ToInt8Slow(JSContext *cx, const HandleValue v, int8_t *out)
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.