-
-
Notifications
You must be signed in to change notification settings - Fork 132
fix(hir): keep upgrade-callback wsId tagged ("ws","Client") across its own param binding #5534
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 1 commit into
PerryTS:main
from
machineloop:fix/ws-upgrade-param-tag
Jun 22, 2026
+156
−5
Merged
Changes from all commits
Commits
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
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
71 changes: 71 additions & 0 deletions
71
crates/perry-hir/tests/ws_upgrade_param_native_dispatch.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,71 @@ | ||
| // Regression: the `wsId` parameter of an HTTP `'upgrade'` handler | ||
| // (`server.on('upgrade', (req, wsId, head) => …)`) must keep the | ||
| // `("ws", "Client")` native-instance tag the upgrade pre-scan assigns it, so | ||
| // `wsId.send(...)` / `wsId.on(...)` inside the handler lower to Client-class | ||
| // `Expr::NativeMethodCall { module: "ws", … }`. | ||
| // | ||
| // The arrow's own `wsId` param binding calls `shadow_native_instance_if_present` | ||
| // to tombstone any STALE native tag leaked from an outer scope — but here the | ||
| // tag is the FRESH, intended one the pre-scan just registered. Without the | ||
| // one-shot protection (`protect_native_param` / `prescan_protected_native_params`) | ||
| // that binding wrongly erased it, and the method calls fell back to generic | ||
| // dynamic dispatch (the post-upgrade "dead channel": `.send` / `.on` no longer | ||
| // routed to the ws Client shims). | ||
| // | ||
| // The handler arrow lowers to an inline `Expr::Closure { body, .. }` nested as | ||
| // the second argument of the `http.on` call (closures here are NOT lifted into | ||
| // `module.functions`). perry-hir's public walker (`walk_expr_children`) skips | ||
| // closure bodies and there is no public statement walker, so we assert over the | ||
| // module's `Debug` rendering instead: only `Expr::NativeMethodCall` carries a | ||
| // `module: "<name>"` field, so each `module: "ws"` occurrence is a surviving | ||
| // Client-class dispatch. Without the fix the calls degrade to `Expr::Call` and | ||
| // no `module: "ws"` appears. | ||
|
|
||
| use perry_diagnostics::SourceCache; | ||
| use perry_hir::{clear_current_module_source, fix_local_native_instances, lower_module}; | ||
| use perry_parser::parse_typescript_with_cache; | ||
|
|
||
| fn lower(src: &str) -> perry_hir::Module { | ||
| let mut cache = SourceCache::new(); | ||
| let parsed = | ||
| parse_typescript_with_cache(src, "/tmp/ws_upgrade_param.ts", &mut cache).expect("parse"); | ||
| let mut module = | ||
| lower_module(&parsed.module, "test", "/tmp/ws_upgrade_param.ts").expect("lower"); | ||
| clear_current_module_source(); | ||
| fix_local_native_instances(&mut module); | ||
| module | ||
| } | ||
|
|
||
| #[test] | ||
| fn upgrade_handler_wsid_keeps_client_class_dispatch_across_its_own_binding() { | ||
| let module = lower( | ||
| r#" | ||
| import { createServer } from "node:http"; | ||
|
|
||
| const server = createServer((req: any, res: any) => { | ||
| res.end("ok"); | ||
| }); | ||
|
|
||
| server.on("upgrade", (req: any, wsId: any, _head: any) => { | ||
| wsId.send("perry-hello"); | ||
| wsId.on("message", (_msg: any) => {}); | ||
| }); | ||
| "#, | ||
| ); | ||
|
|
||
| let dump = format!("{module:#?}"); | ||
| let ws_dispatches = dump.matches("module: \"ws\"").count(); | ||
|
|
||
| // Both `wsId.send` and `wsId.on` must survive as ws Client NativeMethodCalls. | ||
| assert!( | ||
| ws_dispatches >= 2, | ||
| "expected wsId.send + wsId.on to lower as ws Client-class NativeMethodCalls, \ | ||
| but found {ws_dispatches} `module: \"ws\"` dispatch(es). Lowered HIR:\n{dump}" | ||
| ); | ||
| // `method: "send"` is unique to the ws Client call (the only other native | ||
| // call here is `http.on`), so its presence pins the discriminating case. | ||
| assert!( | ||
| dump.contains("method: \"send\""), | ||
| "wsId.send must dispatch as a ws NativeMethodCall. Lowered HIR:\n{dump}" | ||
| ); | ||
| } |
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.