LibRegex+LibJS: Include all named capture groups in source order

Previously, named capture groups in RegExp results did not always follow
their source order, and unmatched groups were omitted. According to the
spec, all named capture groups must appear in the result object in the
order they are defined, even if they did not participate in the match.
This commit makes sure we follow this requirement.
This commit is contained in:
aplefull
2025-07-24 12:24:56 +02:00
committed by Ali Mohammad Pur
parent c4eef822de
commit 25a47ceb1b
Notes: github-actions[bot] 2025-10-16 14:39:13 +00:00
4 changed files with 154 additions and 41 deletions

View File

@@ -187,6 +187,8 @@ Parser::Result Parser::parse(Optional<AllOptions> regex_options)
else
set_error(Error::InvalidPattern);
auto capture_groups = m_parser_state.named_capture_groups.keys();
dbgln_if(REGEX_DEBUG, "[PARSER] Produced bytecode with {} entries (opcodes + arguments)", m_parser_state.bytecode.size());
return {
move(m_parser_state.bytecode),
@@ -195,7 +197,7 @@ Parser::Result Parser::parse(Optional<AllOptions> regex_options)
move(m_parser_state.match_length_minimum),
move(m_parser_state.error),
move(m_parser_state.error_token),
m_parser_state.named_capture_groups.keys(),
move(capture_groups),
m_parser_state.regex_options,
};
}
@@ -2704,7 +2706,8 @@ bool ECMA262Parser::parse_capture_group(ByteCode& stack, size_t& match_length_mi
return false;
}
m_parser_state.named_capture_groups.ensure(name).append({ group_index, m_current_alternative_id });
auto& group_vector = m_parser_state.named_capture_groups.ensure(name);
group_vector.append({ group_index, m_current_alternative_id });
ByteCode capture_group_bytecode;
size_t length = 0;