Skip to content

feat(runtime): implement regex d flag (hasIndices) - #4930

Merged
proggeramlug merged 2 commits into
PerryTS:mainfrom
nglmercer:feat/regex-d-flag-hasIndices
Jun 10, 2026
Merged

proggeramlug merged 2 commits into
PerryTS:mainfrom
nglmercer:feat/regex-d-flag-hasIndices

Conversation

@nglmercer

Copy link
Copy Markdown
Contributor

Summary

Adds support for the ES2022 RegExp d flag (hasIndices), which attaches an indices property to match results containing start/end character positions for each capture group.

Changes

crates/perry-runtime/src/regex.rs (+260 lines)

  • set_exec_array_indices() — attaches indices to match-result arrays for standard regex::Regex matches
  • set_exec_array_indices_fancy() — same for fancy_regex::Regex captures (lookbehind/backreference patterns)
  • Converts byte offsets to character indices (matching existing .index behavior)
  • Handles unmatched capture groups as undefined
  • Supports named capture groups via .groups property on the indices array

Tests

  • test-files/test_regex_d_flag.ts — 18 comprehensive test cases covering basic indices, capture groups, named groups, unmatched groups, hasIndices getter, match(), global match, empty matches, zero-width assertions, lookbehind, backreferences, complex URL pattern, own property check, aliasing, and RegExp constructor
  • tests/test_regex_d_flag.sh — basic shell regression test
  • tests/test_regex_d_flag_comprehensive.sh — 5 shell test scenarios

Test Results

All 18 TypeScript test cases pass. Both shell test suites pass.

nglmercer and others added 2 commits June 10, 2026 10:23
Add support for the ES2022 RegExp `d` flag which attaches an `indices`
property to match results containing start/end character positions for
each capture group.

Implementation:
- Add set_exec_array_indices() for standard regex::Regex matches
- Add set_exec_array_indices_fancy() for fancy_regex matches
- Convert byte offsets to character indices
- Handle unmatched groups as undefined
- Support named capture groups via .groups property

Tests:
- test-files/test_regex_d_flag.ts: 18 comprehensive test cases
- tests/test_regex_d_flag.sh: basic shell test
- tests/test_regex_d_flag_comprehensive.sh: 5 shell test scenarios
Non-global String.prototype.match delegates to RegExpExec, so its result
must carry the same `indices` array as RegExp.prototype.exec under the `d`
flag. js_string_match built its own result array and never called
set_exec_array_indices, so `str.match(/re/d).indices` was undefined.

Wire set_exec_array_indices (standard) and set_exec_array_indices_fancy
(lookbehind/backref fallback) into both non-global branches of
js_string_match. Also correct the test file's hand-written expected-output
comments for tests 4/11/14 to match Node (exec output was already correct;
the comments were wrong).
@proggeramlug
proggeramlug merged commit dfed847 into PerryTS:main Jun 10, 2026
11 of 13 checks passed
@proggeramlug

Copy link
Copy Markdown
Contributor

Thanks @nglmercer great addition!

proggeramlug pushed a commit that referenced this pull request Jun 10, 2026
…ine CI cap

#4930 (regex d flag) pushed regex.rs to 2260 lines, tripping the lint
job's file-size gate on every PR merged after it. Extract the match-
result array decoration group (index/input/groups/indices builders for
both the regex-crate fast path and the fancy_regex fallback, plus the
char/byte index converters) into regex/exec_array.rs — same recipe as
the date.rs split (#4925). No behavior change; d-flag output verified
byte-identical with Node post-split.
proggeramlug added a commit that referenced this pull request Jun 10, 2026
…nstructable via new (#4904) (#4936)

* fix(http): make Agent/ClientRequest/IncomingMessage/ServerResponse constructable via new (#4904)

Node exposes http.Agent, http.ClientRequest, http.IncomingMessage, and
http.ServerResponse as constructable classes; under Perry, new-ing them
threw 'TypeError: <X> is not a constructor' through every value-aliasing
path (const { Agent } = require('http'), require('_http_agent').Agent,
const CR = http.ClientRequest, new http.IncomingMessage(), ...).

Mechanism (mirrors the existing OutgoingMessage route end-to-end):

- runtime/native_module: export the four classes (plus the previously
  missing http.get / http.request twins of the https entries) as bound
  callable values with Node .length arities.
- runtime/class_registry: extend the http construct arm so
  js_new_function_construct forwards (module, class, args) through
  JS_NATIVE_HTTP_DISPATCH; forward the real module name so https.Agent
  constructs with the https protocol default.
- stdlib/dispatch: constructor arms — Agent -> js_http_agent_new,
  ClientRequest -> new js_http_client_request_standalone_new,
  IncomingMessage/ServerResponse -> new standalone factories in
  perry-ext-http-server; plus get/request value-call arms.
- HIR: member-form new http.{ClientRequest,IncomingMessage,
  ServerResponse}() joins the OutgoingMessage NewDynamic route;
  bare-ident forms (destructured imports) added for all four classes;
  the three handle-backed classes are skip-listed from typed
  native-instance registration so instances dispatch dynamically.
- cjs_wrap: require('_http_agent') (and the other _http_* internal
  modules) binds its hoisted import to the public 'http' surface.

Instance surface (perry-ext-http-server):
- IncomingMessage: standalone constructor storing the socket argument;
  socket/connection get/set aliasing (Node's connection accessor writes
  this.socket); _addHeaderLine with Node's matchKnownFields semantics
  (first-wins singles, ', '/'; ' joins, set-cookie array).
- ServerResponse: standalone constructor (req.method captured; HEAD
  suppresses the body), assignSocket/detachSocket with
  ERR_HTTP_SOCKET_ASSIGNED on double assignment, write(chunk, cb)
  callback queueing, end() flushing head+body through the assigned
  socket's JS write method (one corked write + the zero-length finish
  chunk), write/end callbacks invoked in order.
- Agent: dynamic property reads (maxSockets, freeSockets, protocol, ...)
  and writes (tunables + createConnection/createSocket monkeypatching)
  through handle dispatch in both perry-stdlib and perry-ext-http.

Also fixes a latent SIGSEGV: json/stringify's is_closure_value probed
CLOSURE_MAGIC at offset 12 of POINTER_TAG payloads without the handle-
band guard (same #2154 bug class as the sibling probe in the file), so
JSON.stringify of { agent, lookup: () => {} } dereferenced unmapped low
memory. Route through addr_class::is_handle_band first.

Node corpus (test/parallel, pinned v22): 7 of the 13 tests in #4904 now
pass outright (client-defaults, agent-timeout-option,
client-timeout-option-with-agent, incoming-message-connection-setter,
incoming-message-destroy, outgoing-message-write-callback,
server-response-standalone); the other 6 construct and run to their
assertions, failing on deeper Agent pool emulation (sockets/freeSockets
per-key arrays, real createConnection sockets) and a pre-existing
deepStrictEqual divergence, tracked separately.

Closes #4904. Part of #2132.

* chore: cargo fmt — regex.rs long call from #4930 landed unformatted

* chore(runtime): split regex.rs exec-array decoration under the 2000-line CI cap

#4930 (regex d flag) pushed regex.rs to 2260 lines, tripping the lint
job's file-size gate on every PR merged after it. Extract the match-
result array decoration group (index/input/groups/indices builders for
both the regex-crate fast path and the fancy_regex fallback, plus the
char/byte index converters) into regex/exec_array.rs — same recipe as
the date.rs split (#4925). No behavior change; d-flag output verified
byte-identical with Node post-split.

---------

Co-authored-by: Ralph Küpper <ralph@skelpo.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants