Skip to content

fix(runtime,codegen): expose the Web Streams globals; resolve instanceof against a built-in held in a variable#6335

Merged
proggeramlug merged 3 commits into
PerryTS:mainfrom
proggeramlug:fix/webstream-globals-and-instanceof
Jul 13, 2026
Merged

fix(runtime,codegen): expose the Web Streams globals; resolve instanceof against a built-in held in a variable#6335
proggeramlug merged 3 commits into
PerryTS:mainfrom
proggeramlug:fix/webstream-globals-and-instanceof

Conversation

@proggeramlug

@proggeramlug proggeramlug commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Two gaps that both surface the moment a bundler minifies.

1. The Web Streams globals were missing

ReadableStream / WritableStream / TransformStream were absent from globalThis. Perry implements all three — codegen lowers new ReadableStream(…) and x instanceof ReadableStream, and each has a class id — but the NAMES were never registered, so a bare ReadableStream identifier resolved to nothing:

typeof ReadableStream        // perry: "undefined"   node: "function"
"ReadableStream" in globalThis // perry: false       node: true

Libraries feature-detect exactly that (typeof ReadableStream !== "undefined" ? … : …) when deciding how to consume a fetch() body, so they silently took the wrong branch.

2. instanceof failed for a built-in held in a variable

js_instanceof_dynamic returned false for every built-in reached through a variable rather than its bare name:

x instanceof Response        // true
const R = Response;
x instanceof R               // perry: false        node: true

Minifiers alias constructors as a matter of course, so in a bundle this was broadly broken. The dynamic path only understood user class objects; a built-in constructor is a ClosureHeader thunk, which it did not map back to a class id. It now recovers the id from the thunk's recorded name (ReadableStream / WritableStream / TransformStream / Response / Request / Headers / Blob) and defers to js_instanceof.

Validation

node-vs-perry differential — typeof and in globalThis for the three stream constructors, instanceof against both the bare name and an alias, and Response/Headers/Blob through aliases — byte-identical to Node, where 9 of the 10 assertions previously disagreed.

perry-runtime 1264 passed / 0 failed; perry-codegen 159 passed / 0 failed.

Summary by CodeRabbit

  • New Features

    • Added global support for Web Streams constructors: ReadableStream, WritableStream, and TransformStream.
    • These constructors are now available on globalThis for feature detection.
  • Bug Fixes

    • Improved instanceof behavior so checks work consistently when built-in constructors (including Web Streams, Request, Response, Headers, and Blob) are assigned to variables before use.

…eof against a built-in held in a variable

Two gaps that both surface the moment a bundler minifies.

1. `ReadableStream` / `WritableStream` / `TransformStream` were missing from
   `globalThis`. Perry implements all three — codegen lowers `new ReadableStream(…)`
   and `x instanceof ReadableStream`, and each has a class id — but the NAMES were
   never registered, so a bare `ReadableStream` identifier resolved to nothing:
   `typeof ReadableStream === "undefined"` and `"ReadableStream" in globalThis` was
   false, where Node exposes all three as functions. Libraries feature-detect exactly
   that (`typeof ReadableStream !== "undefined" ? … : …`) when deciding how to consume
   a `fetch()` body, so they silently took the wrong branch.

2. `js_instanceof_dynamic` returned `false` for every built-in reached through a
   *variable* rather than its bare name. `x instanceof Response` worked; the
   equivalent after `const R = Response` did not — and minifiers alias constructors
   as a matter of course, so in a bundle this was broadly broken. The dynamic path
   only understood user class objects; a built-in constructor is a `ClosureHeader`
   thunk, which it did not map back to a class id. It now recovers the id from the
   thunk's recorded name (ReadableStream / WritableStream / TransformStream /
   Response / Request / Headers / Blob) and defers to `js_instanceof`.

Validation: node-vs-perry differential — `typeof` and `in globalThis` for the three
stream constructors, `instanceof` against both the bare name and an alias, and
`Response`/`Headers`/`Blob` through aliases — byte-identical to Node, where 9 of the
10 assertions previously disagreed. perry-runtime 1264 passed / 0 failed;
perry-codegen 159 passed / 0 failed.
@coderabbitai

coderabbitai Bot commented Jul 13, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 48485fd3-2da2-4824-98f7-a905784aef7a

📥 Commits

Reviewing files that changed from the base of the PR and between e377222 and eca3843.

📒 Files selected for processing (1)
  • crates/perry-runtime/src/object/instanceof.rs
🚧 Files skipped from review as they are similar to previous changes (1)
  • crates/perry-runtime/src/object/instanceof.rs

📝 Walkthrough

Walkthrough

Web Streams constructors are added to Perry’s globalThis builtin name and constructor tables. Dynamic instanceof resolution now recognizes closure-backed builtin constructors, maps supported names to runtime class IDs, and delegates matching to the existing js_instanceof implementation.

Changes

Web Streams builtin support

Layer / File(s) Summary
Register Web Streams constructors
crates/perry-codegen/src/expr/helpers.rs, crates/perry-runtime/src/object/global_this_tables.rs
Adds ReadableStream, WritableStream, and TransformStream to codegen globalThis builtin recognition and runtime constructor registration.
Resolve builtin constructor instanceof
crates/perry-runtime/src/object/instanceof.rs
Validates closure-backed builtin constructors against globalThis identities, maps supported names to reserved class IDs, and uses those IDs in dynamic instanceof evaluation.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

  • PerryTS/perry#5874: Extends instanceof resolution for global and builtin constructor representations.
  • PerryTS/perry#5942: Adds builtin constructor handling to dynamic instanceof resolution.
  • PerryTS/perry#6205: Modifies constructor handling in the js_instanceof_dynamic path.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise and accurately summarizes the two main fixes in the PR.
Description check ✅ Passed The description covers the summary, changes, and validation, though it does not follow the template exactly.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@crates/perry-runtime/src/object/instanceof.rs`:
- Line 110: Update the pointer extraction in the instanceof logic to use the
canonical js_nanbox_get_pointer helper instead of JSValue::as_pointer when
decoding the NaN-boxed type_ref value, preserving the existing ClosureHeader
pointer handling.
- Around line 119-120: Update the string conversion flow around
str_bytes_from_jsvalue to check ptr.is_null() before calling
slice::from_raw_parts, returning None for a null pointer (including the
zero-length case); preserve the existing UTF-8 validation for non-null pointers.
- Around line 117-129: Update the constructor-to-class-ID logic around the
closure name lookup to resolve the constructor by identity against the current
globalThis builtins, rather than trusting the closure’s `.name` value. Only map
`ReadableStream`, `WritableStream`, `TransformStream`, `Response`, `Request`,
`Headers`, and `Blob` when the constructor is the corresponding native builtin;
otherwise return None, preserving the existing class-ID mappings.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 1f58df21-8a09-42c1-97ea-cd7ca040f57e

📥 Commits

Reviewing files that changed from the base of the PR and between 2c32585 and e377222.

📒 Files selected for processing (3)
  • crates/perry-codegen/src/expr/helpers.rs
  • crates/perry-runtime/src/object/global_this_tables.rs
  • crates/perry-runtime/src/object/instanceof.rs

Comment thread crates/perry-runtime/src/object/instanceof.rs Outdated
Comment thread crates/perry-runtime/src/object/instanceof.rs
Comment thread crates/perry-runtime/src/object/instanceof.rs Outdated
Ralph Küpper added 2 commits July 13, 2026 08:47
…t the globalThis builtin

CodeRabbit findings on the builtin-ctor brand map: use js_nanbox_get_pointer
for the pointer payload; refuse a null string pointer before from_raw_parts;
and require pointer identity with the constructor installed on globalThis so
a user function merely NAMED Response/ReadableStream/... cannot satisfy the
native brand check.
@proggeramlug proggeramlug merged commit 4d858ae into PerryTS:main Jul 13, 2026
23 of 24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant