1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 03:27:45 +00:00

LibJS: Add spec comments to Value::to_bigint()

This commit is contained in:
Linus Groh 2022-12-09 23:59:51 +00:00
parent b97cdfc36c
commit 0d4b798932

View file

@ -705,22 +705,35 @@ static Optional<BigInt*> string_to_bigint(VM& vm, StringView string);
// 7.1.13 ToBigInt ( argument ), https://tc39.es/ecma262/#sec-tobigint // 7.1.13 ToBigInt ( argument ), https://tc39.es/ecma262/#sec-tobigint
ThrowCompletionOr<BigInt*> Value::to_bigint(VM& vm) const ThrowCompletionOr<BigInt*> Value::to_bigint(VM& vm) const
{ {
// 1. Let prim be ? ToPrimitive(argument, number).
auto primitive = TRY(to_primitive(vm, PreferredType::Number)); auto primitive = TRY(to_primitive(vm, PreferredType::Number));
VERIFY(!primitive.is_empty()); // 2. Return the value that prim corresponds to in Table 12.
if (primitive.is_number())
// Number
if (primitive.is_number()) {
// Throw a TypeError exception.
return vm.throw_completion<TypeError>(ErrorType::Convert, "number", "BigInt"); return vm.throw_completion<TypeError>(ErrorType::Convert, "number", "BigInt");
}
switch (primitive.m_value.tag) { switch (primitive.m_value.tag) {
// Undefined
case UNDEFINED_TAG: case UNDEFINED_TAG:
// Throw a TypeError exception.
return vm.throw_completion<TypeError>(ErrorType::Convert, "undefined", "BigInt"); return vm.throw_completion<TypeError>(ErrorType::Convert, "undefined", "BigInt");
// Null
case NULL_TAG: case NULL_TAG:
// Throw a TypeError exception.
return vm.throw_completion<TypeError>(ErrorType::Convert, "null", "BigInt"); return vm.throw_completion<TypeError>(ErrorType::Convert, "null", "BigInt");
// Boolean
case BOOLEAN_TAG: { case BOOLEAN_TAG: {
// Return 1n if prim is true and 0n if prim is false.
auto value = primitive.as_bool() ? 1 : 0; auto value = primitive.as_bool() ? 1 : 0;
return BigInt::create(vm, Crypto::SignedBigInteger { value }).ptr(); return BigInt::create(vm, Crypto::SignedBigInteger { value }).ptr();
} }
// BigInt
case BIGINT_TAG: case BIGINT_TAG:
// Return prim.
return &primitive.as_bigint(); return &primitive.as_bigint();
case STRING_TAG: { case STRING_TAG: {
// 1. Let n be ! StringToBigInt(prim). // 1. Let n be ! StringToBigInt(prim).
@ -733,7 +746,9 @@ ThrowCompletionOr<BigInt*> Value::to_bigint(VM& vm) const
// 3. Return n. // 3. Return n.
return bigint.release_value(); return bigint.release_value();
} }
// Symbol
case SYMBOL_TAG: case SYMBOL_TAG:
// Throw a TypeError exception.
return vm.throw_completion<TypeError>(ErrorType::Convert, "symbol", "BigInt"); return vm.throw_completion<TypeError>(ErrorType::Convert, "symbol", "BigInt");
default: default:
VERIFY_NOT_REACHED(); VERIFY_NOT_REACHED();