Skip to content

feat: add typed batch matching entry points - #7

Merged
ronag merged 3 commits into
mainfrom
feat/typed-test-many-overloads
Jul 24, 2026
Merged

feat: add typed batch matching entry points#7
ronag merged 3 commits into
mainfrom
feat/typed-test-many-overloads

Conversation

@ronag

@ronag ronag commented Jul 24, 2026

Copy link
Copy Markdown
Member

Adds the requested overloads to both RE2 and RE2Set, sync and async:

testManySlice(inputs: readonly Slice[], options?: TestManyOptions)
testManyString(inputs: readonly string[], options?: TestManyOptions)
testManyBuffer(inputs: readonly Buffer[], options?: TestManyOptions)
testManySliceAsync(inputs: readonly Slice[], options?: TestManyAsyncOptions)
testManyStringAsync(inputs: readonly string[], options?: TestManyAsyncOptions)
testManyBufferAsync(inputs: readonly Buffer[], options?: TestManyAsyncOptions)

Slice is a new exported type: { buffer: BinaryView, byteOffset?: number, byteLength?: number }, with the byte-range clamping of test()byteOffset defaults to 0, byteLength to the bytes remaining after the truncated offset.

Native

Each batch call now passes an input kind, so one entry point reads one input shape:

  • view (existing testMany*) — Buffer, TypedArray, or DataView, unchanged.
  • buffernapi_is_buffer plus napi_get_buffer_info, no view fallbacks.
  • slicebuffer, byteOffset, and byteLength are read once, natively, then clamped like test(). Because the properties are validated natively, invalid slice contents reject the Promise of the async variants instead of throwing.
  • string — UTF-8 bytes are appended straight into the batch, so no per-input Buffer.from() allocation. Unpaired surrogates encode as U+FFFD, matching Buffer.from(input).

GetTexts() and GetTextBatch() are replaced by one GetBatchInputs() over a new BatchInputs (borrowed views or an owned copied batch), which AsyncBatch now derives from instead of duplicating the storage choice. String bytes cannot be borrowed, so they are always copied and unsafe has no effect on the string variants. Omitting the kind keeps the binary-view behaviour of earlier releases, so direct native callers are unaffected.

Tests

  • test.js: string batches (multi-byte UTF-8, embedded NUL, unpaired surrogate, empty), Buffer batches (subarrays, empty, SharedArrayBuffer-backed, rejection of other views), slice batches (defaults, negative/fractional/NaN/Infinity/oversized ranges, agreement with test() on integral ranges, single property read), safe-async snapshotting for all three kinds, plus batch-size and error paths. Mirrored for RE2Set.
  • test/native-boundary.test.js: input-kind validation, the omitted-kind default, and the new header in the npm tarball manifest.
  • type-tests/index.ts: typed usage of all six methods on both classes and @ts-expect-error cases for mismatched inputs.

npm test passes (62 passing, 3 platform skips). Coverage of lib/re2.js and lib/re2-set.js is 100% line/branch/function; lib/binary.js is 98.4%, with the only uncovered lines being the pre-existing Too many patterns guard.

🤖 Generated with Claude Code

Add testManySlice(), testManyString(), and testManyBuffer(), plus their
asynchronous counterparts, to RE2 and RE2Set. Batch inputs now carry a
native input kind so each entry point validates and reads one input shape:
Buffers directly, slice objects as clamped byte ranges of a view, and
strings as their UTF-8 bytes.

Native batch reading is unified behind BatchInputs, which either borrows
the supplied views or owns a copied batch. String bytes only exist in the
engine's representation, so they are always copied and `unsafe` has no
effect on the string variants.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
The mutation fixture is the only test that distinguishes a real borrow
from a silent copy, so extend it to the new entry points: unsafe slice
and Buffer batches observe mutation, their safe counterparts do not, and
unsafe string batches stay unaffected because their bytes are copied.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds typed, input-specific batch matching entry points to the Node.js RE2 wrapper and native addon, enabling separate batch APIs for strings, Buffers, and byte-range “slice” objects (sync + async) while keeping the existing binary-view batch behavior intact.

Changes:

  • Added testMany{String,Buffer,Slice} and testMany{String,Buffer,Slice}Async on both RE2 and RE2Set, plus exported Slice type.
  • Updated native batch plumbing to accept an explicit “input kind” and to parse/validate each input shape natively (including slice clamping semantics and string UTF-8 encoding).
  • Expanded JS/TS and native-boundary tests to cover the new entry points, kind validation, and async snapshot/unsafe semantics.

Reviewed changes

Copilot reviewed 17 out of 17 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
type-tests/index.ts Adds TS type coverage for the six new methods on RE2/RE2Set and negative type cases.
test/native-boundary.test.js Verifies native “kind” parameter validation and tarball manifest expectations.
test.js Adds extensive runtime tests for string/Buffer/slice batch variants (sync + async) on both classes.
src/re2.ts Exposes the new typed batch entry points on RE2 and passes explicit input kind to native calls.
src/re2-set.ts Exposes the new typed batch entry points on RE2Set and passes explicit input kind to native calls.
src/index.ts Exports the new Slice type from the public entry point.
src/binding.ts Broadens binding typings to accept multiple batch input shapes and an optional kind.
src/binary.ts Introduces Slice, input-kind constants, and new per-kind input normalizers.
README.md Documents the new batch entry points and slice/string semantics.
native/regex-binding.cc Extends regex batch bindings to accept/validate InputKind and unified batch input handling.
native/set-binding.cc Extends set batch bindings similarly to regex batch bindings.
native/napi-utils.h Replaces legacy text-batch getters with InputKind + GetBatchInputs declarations.
native/napi-utils.cc Implements InputKind validation and unified batch input extraction for view/buffer/string/slice.
native/batch-inputs.h Adds shared InputKind enum and BatchInputs container used across native batch paths.
native/async-batch.h Refactors async batch representation to derive from BatchInputs and carry borrowed references.
native/async-batch.cc Updates async batch preparation to use GetBatchInputs and kind-aware snapshot/borrow behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread native/napi-utils.cc
Comment thread README.md Outdated
Name the methods the Buffer/TypedArray/DataView constraint applies to and
point at the string and slice entry points, and state that the borrowing
and snapshot rules extend to the slice and Buffer variants. Also spell out
why the string byte guard rejects on equality.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 17 out of 17 changed files in this pull request and generated no new comments.

@ronag
ronag merged commit aa2f0cd into main Jul 24, 2026
1 check passed
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