Issue #1240 - Part 5a - BigInt to Number conversion. https://bugzilla.mozilla.org/show_bug.cgi?id=1466893

This commit is contained in:
Brian Smith 2023-07-19 02:17:04 -05:00 committed by roytam1
commit 25ee810247
2 changed files with 49 additions and 5 deletions

View file

@ -524,15 +524,21 @@ Number(JSContext* cx, unsigned argc, Value* vp)
bool isConstructing = args.isConstructing();
if (args.length() > 0) {
if (!ToNumber(cx, args[0]))
// BigInt proposal section 6.2, steps 2a-c.
if (!ToNumeric(cx, args[0]))
return false;
args.rval().set(args[0]);
} else {
args.rval().setInt32(0);
if (args[0].isBigInt())
args[0].setNumber(BigInt::numberValue(args[0].toBigInt()));
}
if (!isConstructing)
if (!isConstructing) {
if (args.length() > 0) {
args.rval().set(args[0]);
} else {
args.rval().setInt32(0);
}
return true;
}
RootedObject newTarget(cx, &args.newTarget().toObject());
RootedObject proto(cx);
@ -1492,6 +1498,30 @@ js::ToInt8Slow(JSContext *cx, const HandleValue v, int8_t *out)
return true;
}
// BigInt proposal section 3.1.6
bool
js::ToNumericSlow(ExclusiveContext* cx, MutableHandleValue vp)
{
MOZ_ASSERT(!vp.isNumber());
MOZ_ASSERT(!vp.isBigInt());
// Step 1.
if (!vp.isPrimitive()) {
if (!cx->isJSContext())
return false;
if (!ToPrimitive(cx->asJSContext(), JSTYPE_NUMBER, vp))
return false;
}
// Step 2.
if (vp.isBigInt()) {
return true;
}
// Step 3.
return ToNumber(cx->asJSContext(), vp);
}
/*
* 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.