diff --git a/js/src/builtin/BigInt.cpp b/js/src/builtin/BigInt.cpp index 4aaa84bf1d..f6c244ce39 100644 --- a/js/src/builtin/BigInt.cpp +++ b/js/src/builtin/BigInt.cpp @@ -179,10 +179,66 @@ BigIntObject::toLocaleString(JSContext* cx, unsigned argc, Value* vp) return CallNonGenericMethod(cx, args); } +// BigInt proposal section 5.2.1. BigInt.asUintN ( bits, bigint ) +bool +BigIntObject::asUintN(JSContext* cx, unsigned argc, Value* vp) +{ + CallArgs args = CallArgsFromVp(argc, vp); + + // Step 1. + uint64_t bits; + if (!ToIndex(cx, args[0], &bits)) { + return false; + } + + // Step 2. + RootedBigInt bi(cx, ToBigInt(cx, args[1])); + if (!bi) { + return false; + } + + // Step 3. + BigInt* res = BigInt::asUintN(cx, bi, bits); + if (!res) { + return false; + } + + args.rval().setBigInt(res); + return true; +} + +// BigInt proposal section 5.2.2. BigInt.asIntN ( bits, bigint ) +bool +BigIntObject::asIntN(JSContext* cx, unsigned argc, Value* vp) +{ + CallArgs args = CallArgsFromVp(argc, vp); + + // Step 1. + uint64_t bits; + if (!ToIndex(cx, args[0], &bits)) { + return false; + } + + // Step 2. + RootedBigInt bi(cx, ToBigInt(cx, args[1])); + if (!bi) { + return false; + } + + // Step 3. + BigInt* res = BigInt::asIntN(cx, bi, bits); + if (!res) { + return false; + } + + args.rval().setBigInt(res); + return true; +} + const ClassSpec BigIntObject::classSpec_ = { GenericCreateConstructor, CreateBigIntPrototype, - nullptr, + BigIntObject::staticMethods, nullptr, BigIntObject::methods, BigIntObject::properties @@ -208,3 +264,9 @@ const JSFunctionSpec BigIntObject::methods[] = { JS_FN("toLocaleString", toLocaleString, 0, 0), JS_FS_END }; + +const JSFunctionSpec BigIntObject::staticMethods[] = { + JS_FN("asUintN", asUintN, 2, 0), + JS_FN("asIntN", asIntN, 2, 0), + JS_FS_END +}; diff --git a/js/src/builtin/BigInt.h b/js/src/builtin/BigInt.h index 13c1985b30..f1bf471ecb 100644 --- a/js/src/builtin/BigInt.h +++ b/js/src/builtin/BigInt.h @@ -33,12 +33,15 @@ class BigIntObject : public NativeObject 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); + static bool asUintN(JSContext* cx, unsigned argc, JS::Value* vp); + static bool asIntN(JSContext* cx, unsigned argc, JS::Value* vp); JS::BigInt* unbox() const; private: static const JSPropertySpec properties[]; static const JSFunctionSpec methods[]; + static const JSFunctionSpec staticMethods[]; }; extern JSObject*