bug 1366287 - Part 5: Implement BigInt.prototype.toLocaleString. r=jwalden

This will have its behavior defined by ECMA-402 in the future; for now,
it returns the same result as the toString method (which is permitted by
the BigInt spec).

--HG--
extra : rebase_source : 0474757caa464c2979e58038825610957e0b15f3
This commit is contained in:
Robin Templeton 2018-05-11 19:47:17 -07:00
parent 6168e9d4ab
commit 085cf5c912
2 changed files with 29 additions and 0 deletions

View File

@ -154,6 +154,32 @@ BigIntObject::toString(JSContext* cx, unsigned argc, Value* vp)
return CallNonGenericMethod<IsBigInt, toString_impl>(cx, args);
}
// BigInt proposal section 5.3.2. "This function is
// implementation-dependent, and it is permissible, but not encouraged,
// for it to return the same thing as toString."
bool
BigIntObject::toLocaleString_impl(JSContext* cx, const CallArgs& args)
{
HandleValue thisv = args.thisv();
MOZ_ASSERT(IsBigInt(thisv));
RootedBigInt bi(cx, thisv.isBigInt()
? thisv.toBigInt()
: thisv.toObject().as<BigIntObject>().unbox());
RootedString str(cx, BigInt::toString(cx, bi, 10));
if (!str)
return false;
args.rval().setString(str);
return true;
}
bool
BigIntObject::toLocaleString(JSContext* cx, unsigned argc, Value* vp)
{
CallArgs args = CallArgsFromVp(argc, vp);
return CallNonGenericMethod<IsBigInt, toLocaleString_impl>(cx, args);
}
const ClassSpec BigIntObject::classSpec_ = {
GenericCreateConstructor<BigIntConstructor, 1, gc::AllocKind::FUNCTION>,
CreateBigIntPrototype,
@ -180,5 +206,6 @@ const JSPropertySpec BigIntObject::properties[] = {
const JSFunctionSpec BigIntObject::methods[] = {
JS_FN("valueOf", valueOf, 0, 0),
JS_FN("toString", toString, 0, 0),
JS_FN("toLocaleString", toLocaleString, 0, 0),
JS_FS_END
};

View File

@ -32,6 +32,8 @@ class BigIntObject : public NativeObject
static bool valueOf(JSContext* cx, unsigned argc, JS::Value* vp);
static bool toString_impl(JSContext* cx, const CallArgs& args);
static bool toString(JSContext* cx, unsigned argc, JS::Value* vp);
static bool toLocaleString_impl(JSContext* cx, const CallArgs& args);
static bool toLocaleString(JSContext* cx, unsigned argc, JS::Value* vp);
JS::BigInt* unbox() const;