-
-
Notifications
You must be signed in to change notification settings - Fork 158
fix(codegen): #6065 — user charAt/charCodeAt/codePointAt class method mis-lowered to the String builtin
#6066
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
proggeramlug
merged 2 commits into
PerryTS:main
from
proggeramlug:fix/charAt-string-method-dispatch
Jul 6, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
208 changes: 208 additions & 0 deletions
208
crates/perry/tests/issue_6065_class_method_shadows_string_char_access.rs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,208 @@ | ||
| //! Regression test for #6065: a user class method whose name shadows a | ||
| //! `String.prototype` char-access method (`charAt` / `charCodeAt` / | ||
| //! `codePointAt`) must call the USER method, not the String builtin. | ||
| //! | ||
| //! The static string-method fast path was guarded by an arity heuristic | ||
| //! (`string_only_method_arity_ok`) so that a user method sharing a String | ||
| //! builtin name (e.g. joi's `internals.trim(value, schema)`) falls through to | ||
| //! runtime dispatch when the arg count can't match the builtin. But the | ||
| //! char-access methods ignore surplus args per spec, so that gate returns | ||
| //! `true` for ANY arg count — a user `charAt(n)` on a class instance was | ||
| //! therefore lowered to `String.prototype.charAt` with the receiver coerced to | ||
| //! `"[object Object]"`, so `this.charAt(0)` returned `"["`, `this.charAt(1)` | ||
| //! returned `"o"`, and so on. | ||
| //! | ||
| //! The `yaml` package's `Lexer.charAt(n) { return this.buffer[this.pos + n]; }` | ||
| //! is exactly this shape: with it mis-dispatched the tokenizer reads garbage | ||
| //! and its `*lex` state machine (`while (next) next = yield* this.parseNext(next)`) | ||
| //! never advances `pos`, spinning forever — hanging YAML parsing (and any large | ||
| //! esbuild-bundled CLI app that parses YAML at module-init time) at 100% CPU. | ||
| //! | ||
| //! Fix: don't take the static String path when the receiver's statically-known | ||
| //! class defines its own method, getter, or instance field of that name. | ||
| //! | ||
| //! Expected outputs are byte-for-byte what `node --experimental-strip-types` | ||
| //! prints. The generator/lexer cases are timeout-bounded so a regression FAILS | ||
| //! (spins) instead of hanging the test process. | ||
|
|
||
| use std::path::PathBuf; | ||
| use std::process::Command; | ||
| use std::time::{Duration, Instant}; | ||
|
|
||
| fn perry_bin() -> PathBuf { | ||
| PathBuf::from(env!("CARGO_BIN_EXE_perry")) | ||
| } | ||
|
|
||
| fn compile_and_run(dir: &std::path::Path, source: &str) -> String { | ||
| let entry = dir.join("main.ts"); | ||
| let output = dir.join("main_bin"); | ||
| std::fs::write(&entry, source).expect("write entry"); | ||
|
|
||
| let compile = Command::new(perry_bin()) | ||
| .current_dir(dir) | ||
| .arg("compile") | ||
| .arg(&entry) | ||
| .arg("-o") | ||
| .arg(&output) | ||
| .arg("--no-cache") | ||
| .output() | ||
| .expect("run perry compile"); | ||
| assert!( | ||
| compile.status.success(), | ||
| "perry compile failed\nstdout:\n{}\nstderr:\n{}", | ||
| String::from_utf8_lossy(&compile.stdout), | ||
| String::from_utf8_lossy(&compile.stderr) | ||
| ); | ||
|
|
||
| let mut child = Command::new(&output) | ||
| .current_dir(dir) | ||
| .stdout(std::process::Stdio::piped()) | ||
| .stderr(std::process::Stdio::piped()) | ||
| .spawn() | ||
| .expect("spawn compiled binary"); | ||
|
|
||
| let timeout = Duration::from_secs(30); | ||
| let start = Instant::now(); | ||
| loop { | ||
| match child.try_wait().expect("try_wait") { | ||
| Some(status) => { | ||
| let out = child.wait_with_output().expect("wait_with_output"); | ||
| assert!( | ||
| status.success(), | ||
| "compiled binary failed (exit {:?})\nstdout:\n{}\nstderr:\n{}", | ||
| status.code(), | ||
| String::from_utf8_lossy(&out.stdout), | ||
| String::from_utf8_lossy(&out.stderr) | ||
| ); | ||
| return String::from_utf8_lossy(&out.stdout).into_owned(); | ||
| } | ||
| None => { | ||
| if start.elapsed() > timeout { | ||
| let _ = child.kill(); | ||
| let _ = child.wait(); | ||
| panic!( | ||
| "compiled binary did not finish within {:?} — a user \ | ||
| `charAt`/`charCodeAt`/`codePointAt` method was mis-lowered \ | ||
| to the String builtin (regression)", | ||
| timeout | ||
| ); | ||
| } | ||
| std::thread::sleep(Duration::from_millis(50)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// A plain class method named `charAt` must call the user's method — not | ||
| /// `String.prototype.charAt` on a `"[object Object]"`-coerced receiver. | ||
| #[test] | ||
| fn plain_method_named_char_at_is_user_method() { | ||
| let dir = tempfile::tempdir().expect("tempdir"); | ||
| let stdout = compile_and_run( | ||
| dir.path(), | ||
| r#" | ||
| class Buf { | ||
| buffer = "hello"; | ||
| pos = 0; | ||
| charAt(n: number) { return this.buffer[this.pos + n]; } | ||
| } | ||
| const b = new Buf(); | ||
| console.log(b.charAt(0) + b.charAt(1) + b.charAt(4)); | ||
| "#, | ||
| ); | ||
| assert_eq!(stdout, "heo\n"); | ||
| } | ||
|
|
||
| /// A field holding a function value (`charAt = (n) => …`) shadows the builtin | ||
| /// the same way a method does — the receiver-class guard must treat instance | ||
| /// fields as defining the name, not just methods/getters. | ||
| #[test] | ||
| fn arrow_function_field_named_char_at_is_user_function() { | ||
| let dir = tempfile::tempdir().expect("tempdir"); | ||
| let stdout = compile_and_run( | ||
| dir.path(), | ||
| r#" | ||
| class Buf { | ||
| buffer = "hello"; | ||
| pos = 0; | ||
| charAt = (n: number) => this.buffer[this.pos + n]; | ||
| } | ||
| const b = new Buf(); | ||
| console.log(b.charAt(0) + b.charAt(1) + b.charAt(4)); | ||
| "#, | ||
| ); | ||
| assert_eq!(stdout, "heo\n"); | ||
| } | ||
|
|
||
| /// `charCodeAt` and `codePointAt` are affected by the same arity-gate no-op. | ||
| #[test] | ||
| fn plain_methods_named_char_code_at_and_code_point_at_are_user_methods() { | ||
| let dir = tempfile::tempdir().expect("tempdir"); | ||
| let stdout = compile_and_run( | ||
| dir.path(), | ||
| r#" | ||
| class C { | ||
| data = [10, 20, 30]; | ||
| charCodeAt(i: number) { return this.data[i] + 1; } | ||
| codePointAt(i: number) { return this.data[i] * 2; } | ||
| } | ||
| const c = new C(); | ||
| console.log(c.charCodeAt(0) + "," + c.codePointAt(2)); | ||
| "#, | ||
| ); | ||
| assert_eq!(stdout, "11,60\n"); | ||
| } | ||
|
|
||
| /// A genuine string receiver must still get `String.prototype.charAt` — the | ||
| /// fix must not break real string char access. | ||
| #[test] | ||
| fn real_string_char_at_still_works() { | ||
| let dir = tempfile::tempdir().expect("tempdir"); | ||
| let stdout = compile_and_run( | ||
| dir.path(), | ||
| r#" | ||
| const s = "world"; | ||
| console.log(s.charAt(0) + s.charAt(4) + "|" + s.charCodeAt(1)); | ||
| "#, | ||
| ); | ||
| assert_eq!(stdout, "wd|111\n"); | ||
| } | ||
|
|
||
| /// The exact hang shape: a generator class method using its own `charAt` in a | ||
| /// yielding `switch` loop that only terminates when `charAt` returns the real | ||
| /// chars. Mirrors the `yaml` `Lexer` indicator scan. | ||
| #[test] | ||
| fn generator_using_own_char_at_in_switch_loop_terminates() { | ||
| let dir = tempfile::tempdir().expect("tempdir"); | ||
| let stdout = compile_and_run( | ||
| dir.path(), | ||
| r#" | ||
| class Lexer { | ||
| buffer = ""; | ||
| pos = 0; | ||
| charAt(n: number) { return this.buffer[this.pos + n]; } | ||
| *lex(): Generator<string, number, unknown> { | ||
| let count = 0; | ||
| loop: while (true) { | ||
| switch (this.charAt(0)) { | ||
| case 'a': | ||
| yield 'A'; this.pos++; count++; continue loop; | ||
| case ':': | ||
| yield 'C'; this.pos++; count++; continue loop; | ||
| } | ||
| break loop; | ||
| } | ||
| return count; | ||
| } | ||
| } | ||
| const lx = new Lexer(); | ||
| lx.buffer = "aa:aZ"; | ||
| const out: string[] = []; | ||
| const g = lx.lex(); | ||
| let r = g.next(); | ||
| while (!r.done) { out.push(r.value as string); r = g.next(); } | ||
| console.log(out.join(",") + "|count=" + r.value + "|pos=" + lx.pos); | ||
| "#, | ||
| ); | ||
| assert_eq!(stdout, "A,A,C,A|count=4|pos=4\n"); | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.