hologram icon indicating copy to clipboard operation
hologram copied to clipboard

Port of :binary.compile_pattern to JS

Open atavistock opened this issue 5 months ago • 3 comments

atavistock avatar Nov 26 '25 01:11 atavistock

@bartblast So this is waiting on the registry discussed in https://github.com/bartblast/hologram/pull/418#discussion_r2565135144

I can take a stab at this if you have too many things going on.

atavistock avatar Dec 05 '25 18:12 atavistock

Thanks for your patience, @atavistock! The blocker was generating new references - they have a very specific format, include node information, and must be unique across nodes. Since they’re created client-side in this case, it caused some complications. I think I’ve got a working solution now. I’ll merge it soon and let you know.

bartblast avatar Dec 09 '25 23:12 bartblast

@atavistock, I've created missing client-side infrastructure:

  • binary pattern registry: https://github.com/bartblast/hologram/pull/465/changes
  • ERTS reference support: https://github.com/bartblast/hologram/pull/463/changes

To create a new reference you run:

const ref = Erlang["make_ref/0"]()

To put your binary pattern to the registry you run:

ERTS.binaryPatternRegistry.put(ref, myPattern)

(myPattern can be anything you need it to be)

and to retrieve the pattern from registry by reference:

ERTS.binaryPatternRegistry.get(ref)

bartblast avatar Dec 18 '25 19:12 bartblast

@coderabbitai full review

bartblast avatar Jan 03 '26 00:01 bartblast

✅ Actions performed

Full review triggered.

coderabbitai[bot] avatar Jan 03 '26 00:01 coderabbitai[bot]

📝 Walkthrough

Walkthrough

Introduces pattern compilation infrastructure for binary pattern matching, adding Boyer-Moore and Aho-Corasick pattern matcher functions with runtime compilation support and a centralized pattern registry to store and retrieve compiled patterns.

Changes

Cohort / File(s) Summary
Pattern Compilation Infrastructure
assets/js/erlang/binary.mjs
Adds three new public functions: compile_pattern/1 dispatcher that delegates to single-pattern or multi-pattern matchers; _boyer_moore_pattern_matcher/1 for precomputing bad-shift tables; and _aho_corasick_pattern_matcher/1 for building trie-based automatons. Introduces centralized pattern registry (ERTS.binaryPatternRegistry) and returns type-tagged tuples ([bm, ref] or [ac, ref]). Enhances copy/2 error handling with granular argument error messages.
Elixir Consistency Tests
test/elixir/hologram/ex_js_consistency/erlang/binary_test.exs
Adds comprehensive test suite describe "compile_pattern/1" validating single-pattern Boyer-Moore compilation, multi-pattern Aho-Corasick compilation, single-element list handling, and ArgumentError assertions for invalid patterns (non-binaries, bitstrings, invalid list contents).
JavaScript Unit Tests
test/javascript/erlang/binary_test.mjs
Introduces test blocks for compile_pattern/1, _boyer_moore_pattern_matcher, and _aho_corasick_pattern_matcher, verifying correct pattern compilation, registry storage, bad-shift table computation, and trie structure building with appropriate error handling for invalid inputs.

Sequence Diagram

sequenceDiagram
    participant Client
    participant compile_pattern
    participant _boyer_moore_pattern_matcher
    participant _aho_corasick_pattern_matcher
    participant Registry as ERTS.binaryPatternRegistry

    Client->>compile_pattern: compile_pattern(pattern)
    
    alt Single Binary Pattern
        compile_pattern->>_boyer_moore_pattern_matcher: _boyer_moore_pattern_matcher(pattern)
        _boyer_moore_pattern_matcher->>_boyer_moore_pattern_matcher: Precompute bad-shift table
        _boyer_moore_pattern_matcher->>Registry: Store pattern with fresh ref
        Registry-->>_boyer_moore_pattern_matcher: ref
        _boyer_moore_pattern_matcher-->>compile_pattern: [bm, ref]
    else List with Single Pattern
        compile_pattern->>_boyer_moore_pattern_matcher: _boyer_moore_pattern_matcher(pattern)
        _boyer_moore_pattern_matcher->>Registry: Store pattern
        Registry-->>_boyer_moore_pattern_matcher: ref
        _boyer_moore_pattern_matcher-->>compile_pattern: [bm, ref]
    else List with Multiple Patterns
        compile_pattern->>_aho_corasick_pattern_matcher: _aho_corasick_pattern_matcher(patterns)
        _aho_corasick_pattern_matcher->>_aho_corasick_pattern_matcher: Build trie with failure links
        _aho_corasick_pattern_matcher->>Registry: Store automaton
        Registry-->>_aho_corasick_pattern_matcher: ref
        _aho_corasick_pattern_matcher-->>compile_pattern: [ac, ref]
    else Invalid Pattern
        compile_pattern-->>Client: ArgumentError
    end
    
    compile_pattern-->>Client: [type, ref]

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

🐰 A patterns' dance, so swift and grand, Boyer and Aho join hand in hand, Binaries compiled, stored with care, References whisper through the air, Binary matching blooms, beyond compare! 🌟

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately and concisely describes the main objective of the pull request: porting the Erlang/Elixir :binary.compile_pattern function to JavaScript, which aligns with all changes across the modified files.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
  • [ ] 📝 Generate docstrings

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot] avatar Jan 03 '26 00:01 coderabbitai[bot]