mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 23:07:35 +00:00
LibJS: Allow BigInts as destructuring property names
These are simply treated as their numerical value which means that above 2^32 - 1 they are strings.
This commit is contained in:
parent
e663504df1
commit
fce2b33758
3 changed files with 37 additions and 1 deletions
|
@ -2762,7 +2762,7 @@ RefPtr<BindingPattern> Parser::parse_binding_pattern(Parser::AllowDuplicates all
|
||||||
name = static_ptr_cast<Identifier>(expression);
|
name = static_ptr_cast<Identifier>(expression);
|
||||||
else
|
else
|
||||||
syntax_error("Invalid destructuring assignment target", expression_position);
|
syntax_error("Invalid destructuring assignment target", expression_position);
|
||||||
} else if (match_identifier_name() || match(TokenType::StringLiteral) || match(TokenType::NumericLiteral)) {
|
} else if (match_identifier_name() || match(TokenType::StringLiteral) || match(TokenType::NumericLiteral) || match(TokenType::BigIntLiteral)) {
|
||||||
if (match(TokenType::StringLiteral) || match(TokenType::NumericLiteral))
|
if (match(TokenType::StringLiteral) || match(TokenType::NumericLiteral))
|
||||||
needs_alias = true;
|
needs_alias = true;
|
||||||
|
|
||||||
|
@ -2773,6 +2773,12 @@ RefPtr<BindingPattern> Parser::parse_binding_pattern(Parser::AllowDuplicates all
|
||||||
name = create_ast_node<Identifier>(
|
name = create_ast_node<Identifier>(
|
||||||
{ m_state.current_token.filename(), rule_start.position(), position() },
|
{ m_state.current_token.filename(), rule_start.position(), position() },
|
||||||
string_literal->value());
|
string_literal->value());
|
||||||
|
} else if (match(TokenType::BigIntLiteral)) {
|
||||||
|
auto string_value = consume().flystring_value();
|
||||||
|
VERIFY(string_value.ends_with("n"sv));
|
||||||
|
name = create_ast_node<Identifier>(
|
||||||
|
{ m_state.current_token.filename(), rule_start.position(), position() },
|
||||||
|
FlyString(string_value.view().substring_view(0, string_value.length() - 1)));
|
||||||
} else {
|
} else {
|
||||||
name = create_ast_node<Identifier>(
|
name = create_ast_node<Identifier>(
|
||||||
{ m_state.current_token.filename(), rule_start.position(), position() },
|
{ m_state.current_token.filename(), rule_start.position(), position() },
|
||||||
|
|
|
@ -28,3 +28,26 @@ test("numeric properties", () => {
|
||||||
"4294967296", // >= 2^32 - 1
|
"4294967296", // >= 2^32 - 1
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("big int properties", () => {
|
||||||
|
const o = {
|
||||||
|
[-1n]: "foo",
|
||||||
|
0n: "foo",
|
||||||
|
1n: "foo",
|
||||||
|
[12345678901n]: "foo",
|
||||||
|
[4294967294n]: "foo",
|
||||||
|
[4294967295n]: "foo",
|
||||||
|
};
|
||||||
|
// Numeric properties come first in Object.getOwnPropertyNames()'s output,
|
||||||
|
// which means we can test what each is treated as internally.
|
||||||
|
expect(Object.getOwnPropertyNames(o)).toEqual([
|
||||||
|
// Numeric properties
|
||||||
|
"0",
|
||||||
|
"1",
|
||||||
|
"4294967294",
|
||||||
|
// Non-numeric properties
|
||||||
|
"-1",
|
||||||
|
"12345678901", // >= 2^32 - 1
|
||||||
|
"4294967295", // >= 2^32 - 1
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
|
@ -223,4 +223,11 @@ describe("evaluating", () => {
|
||||||
expect(x).toBe("foo");
|
expect(x).toBe("foo");
|
||||||
expect(a).toBe(o.a);
|
expect(a).toBe(o.a);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("can use big int values as number-like properties", () => {
|
||||||
|
let o = { "99999999999999999": 1 };
|
||||||
|
let { 123n: a = "foo", 99999999999999999n: b = "bar" } = o;
|
||||||
|
expect(a).toBe("foo");
|
||||||
|
expect(b).toBe(1);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue