fix(regex): dynamic regex.test()/exec() dispatch on untyped receiver (#1731) - #1734
Merged
Conversation
…1731) hono's RegExpRouter does `buildWildcardRegExp(k).test(path)` — a method call on the (untyped) result of a function call. Codegen's Expr::RegExpTest fast path only fires for statically-typed receivers, so this fell through to js_native_call_method, which had no regex-receiver arm and threw `TypeError: test is not a function` — breaking every Hono `app.use('*', …)` wildcard middleware (logger included). Adds a regex-receiver arm dispatching test/exec (arg coerced to string; exec returns null on no match), with the body in regex::dispatch_regex_receiver_method. Verified: the full #1655 acceptance (incl. `app.use('*', logger())`) now serves all routes end-to-end.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1731.
Root cause
hono's
RegExpRoutermatches wildcard middleware withbuildWildcardRegExp(k).test(path)— a method call on the untyped result of a function call. Codegen'sExpr::RegExpTest/Expr::RegExpExecfast path only fires when the receiver's static type is provably aRegExp(const re = /…/; re.test(x)). The dynamic-receiver form fell through tojs_native_call_method, which had arms forstring.match/search/replace(regex)(regex as argument) but none for a regex receiver withtest/exec— so it hit the catch-all and threwTypeError: test is not a function.That broke every Hono
app.use('*', …)wildcard middleware — includinglogger(), which was the last line blocking the #1655 acceptance program.Fix
A regex-receiver arm in
js_native_call_method: whenmethod ∈ {test, exec}and the receiver is a live regex (is_regex_pointer), dispatch tojs_regexp_test/js_regexp_exec(argument coerced to a string;execreturnsnullon no match, per spec). The body lives inregex::dispatch_regex_receiver_method; it returnsNonefor non-regex receivers so the generic dispatch continues unchanged.Testing
app.use('*', async (c,next)=>{await next()})+app.get('/')now returnsstatus 200.hono@4.10+@hono/perry-server,app.use('*', logger())included) now serves all routes end-to-end throughserve()+ livecurl:GET /→<html>…<h1>hello</h1>…</html>GET /api/echo?k=v&a=b→{"q":{"k":"v","a":"b"}}POST /api/echo→{"body":{"x":1}}logger()logs--> GET / 200 0ms(with color)cargo test -p perry-runtime: 560 passed, 0 failed.cargo fmt --all --checkclean; file-size gate clean.Note (out of scope)
typeof re.teston a regex value still reportsundefined(the method-as-value form via the property-get path) — hono doesn't rely on it. Left for a separate follow-up if needed.