Bug 755889 - Inline NonstandardToInt32 into JS_ValueToInt32; r=luke

This commit is contained in:
Ms2ger 2012-05-18 10:29:38 +02:00
parent 96a13d013d
commit e4025a0ffd
3 changed files with 20 additions and 37 deletions

View File

@ -593,7 +593,26 @@ JS_ValueToInt32(JSContext *cx, jsval v, int32_t *ip)
CHECK_REQUEST(cx);
assertSameCompartment(cx, v);
return NonstandardToInt32(cx, v, (int32_t *)ip);
if (v.isInt32()) {
*ip = v.toInt32();
return true;
}
double d;
if (v.isDouble()) {
d = v.toDouble();
} else if (!ToNumberSlow(cx, v, &d)) {
return false;
}
if (MOZ_DOUBLE_IS_NaN(d) || d <= -2147483649.0 || 2147483648.0 <= d) {
js_ReportValueError(cx, JSMSG_CANT_CONVERT,
JSDVG_SEARCH_STACK, v, NULL);
return false;
}
*ip = (int32_t) floor(d + 0.5); /* Round to nearest */
return true;
}
JS_PUBLIC_API(JSBool)

View File

@ -1308,26 +1308,6 @@ ToUint32Slow(JSContext *cx, const Value &v, uint32_t *out)
return true;
}
bool
NonstandardToInt32Slow(JSContext *cx, const Value &v, int32_t *out)
{
JS_ASSERT(!v.isInt32());
double d;
if (v.isDouble()) {
d = v.toDouble();
} else if (!ToNumberSlow(cx, v, &d)) {
return false;
}
if (MOZ_DOUBLE_IS_NaN(d) || d <= -2147483649.0 || 2147483648.0 <= d) {
js_ReportValueError(cx, JSMSG_CANT_CONVERT,
JSDVG_SEARCH_STACK, v, NULL);
return false;
}
*out = (int32_t) floor(d + 0.5); /* Round to nearest */
return true;
}
bool
ValueToUint16Slow(JSContext *cx, const Value &v, uint16_t *out)
{

View File

@ -186,22 +186,6 @@ ToUint32(JSContext *cx, const js::Value &v, uint32_t *out)
return ToUint32Slow(cx, v, out);
}
/*
* Convert a value to a number, then to an int32_t if it fits by rounding to
* nearest. Return converted value in *out on success, !ok on failure. As a
* side effect, *vp will be mutated to match *out.
*/
JS_ALWAYS_INLINE bool
NonstandardToInt32(JSContext *cx, const js::Value &v, int32_t *out)
{
if (v.isInt32()) {
*out = v.toInt32();
return true;
}
extern bool NonstandardToInt32Slow(JSContext *cx, const js::Value &v, int32_t *ip);
return NonstandardToInt32Slow(cx, v, out);
}
/*
* Convert a value to a number, then to a uint16_t according to the ECMA rules
* for ToUint16. Return converted value on success, !ok on failure. v must be a