fix(string): replace/replaceAll detect a RegExp searchValue at runtime (#4871)#5053
Merged
Merged
Conversation
#4871) Codegen's needle_is_regex detection covers RegExp literals and RegExp-typed locals only. A RegExp read from an object property (or destructured in a for...of) lowered as an opaque value and was ToString-coerced — str.replace(obj.regex, …) literally searched for "/foo/g" and silently returned the input unchanged (surfaced as a PII-redaction pass that did nothing in the native binary). Add js_string_replace_search_dyn / js_string_replace_all_search_dyn, which probe the registered-RegExp set before coercing and defer to the existing _dyn replacement-shape dispatchers; route every statically untyped searchValue through them. Statically known regex/string needles keep their existing direct paths.
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 #4871.
Root cause
In
lower_string_method.rs,needle_is_regexis a static test: RegExp literal or RegExp-typed local. A RegExp read from an object property (obj.regex) or destructured in afor...ofis neither, so the lowering fell into thejs_string_coerce(needle)arm — the RegExp was stringified to"/foo/g"and searched literally. No match → input returned unchanged, silently (the issue's PII-redaction table did zero replacements).Fix
js_string_replace_search_dyn/js_string_replace_all_search_dyn(regex/replace_fn.rs): probe the needle against the registered-RegExp set (handles NaN-boxed and raw-I64 module-slot pointers), route to the regex_dynpath on hit, ToString-coerce on miss — preserving spec coercion order (searchValue before replaceValue) and every replacement shape (string, $-templates, named groups, function replacer) via the existing_dynfamily.searchValuethrough them. Statically-known regex/string needles keep their direct fast paths.Validation
10-case e2e matches
node --experimental-strip-typesbyte-for-byte: all four issue-table forms,replaceAllfrom property, function replacer over a destructured pattern table, non-regex dynamic needles (string-prop, number) still coerce,$1backrefs and$<name>named groups through property regexes. Dynamic (any-typed) receivers verified separately.search_dyn_dispatches_runtime_regex_and_coerces_non_regex;replace(10) andregex(21) unit-test groups pass; string-semantics / key-order / spec-throws gap tests diff clean vs Node.Code-only PR — version bump + changelog left for merge time.