bug 1366287 - Part 5: Implement BigInt.prototype.toLocaleString. r=jwalden
☠☠ backed out by f0cb25b7bdee ☠ ☠
authorRobin Templeton <robin@igalia.com>
Fri, 11 May 2018 19:47:17 -0700
changeset 794519 68fae6784ebe20c33da7ba554d32fb0a24db8ed7
parent 794518 a18120245eb7f5724a8083e5500ed8baa7df2b95
child 794520 971159738904104edcbf6ffb41dca1c281fc28ee
push id109697
push userbmo:sledru@mozilla.com
push dateSat, 12 May 2018 10:04:34 +0000
reviewersjwalden
bugs1366287
milestone62.0a1
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).
js/src/builtin/BigInt.cpp
js/src/builtin/BigInt.h
--- a/js/src/builtin/BigInt.cpp
+++ b/js/src/builtin/BigInt.cpp
@@ -149,16 +149,42 @@ BigIntObject::toString_impl(JSContext* c
 
 bool
 BigIntObject::toString(JSContext* cx, unsigned argc, Value* vp)
 {
     CallArgs args = CallArgsFromVp(argc, 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,
     nullptr,
     nullptr,
     BigIntObject::methods,
     BigIntObject::properties
 };
@@ -175,10 +201,11 @@ const JSPropertySpec BigIntObject::prope
     // BigInt proposal section 5.3.5
     JS_STRING_SYM_PS(toStringTag, "BigInt", JSPROP_READONLY),
     JS_PS_END
 };
 
 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
 };
--- a/js/src/builtin/BigInt.h
+++ b/js/src/builtin/BigInt.h
@@ -27,16 +27,18 @@ class BigIntObject : public NativeObject
 
     static JSObject* create(JSContext* cx, JS::Handle<JS::BigInt*> bi);
 
     // Methods defined on BigInt.prototype.
     static bool valueOf_impl(JSContext* cx, const CallArgs& args);
     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;
 
   private:
     static const JSPropertySpec properties[];
     static const JSFunctionSpec methods[];
 };