Skip to content

fix(runtime): dynamic new P(executor) for the Promise constructor value#6211

Open
proggeramlug wants to merge 1 commit into
mainfrom
fix/dynamic-builtin-te-td-promise
Open

fix(runtime): dynamic new P(executor) for the Promise constructor value#6211
proggeramlug wants to merge 1 commit into
mainfrom
fix/dynamic-builtin-te-td-promise

Conversation

@proggeramlug

@proggeramlug proggeramlug commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Problem

new P(executor) where P holds the global Promise constructor value
const P = Promise; or a polyfill/bundler alias — threw
Constructor Promise requires 'new'.

The dynamic-construct path (js_new_function_construct) identifies global
builtin constructor values and routes them to their native factories, but the
match had no "Promise" arm, so the callee fell through to the closure
fallback, which called promise_constructor_call_thunk as a plain function —
and that thunk (correctly) rejects being invoked without construct semantics.

const P = Promise;
new P((resolve) => resolve(42));   // threw; works in Node

Fix

Add the missing "Promise" arm to the identified-builtin construct match in
class_registry/construct.rs, routing to js_promise_new_with_executor exactly
like the static new Promise(executor) lowering. A non-callable executor still
throws the spec TypeError (validation lives in the factory).

Validation

New gap test test_gap_dynamic_builtin_construct_dispatch.ts, byte-for-byte
against node --experimental-strip-types:

Found while unblocking Next.js App Router SSR (#5989): react-server-dom
constructs builtins through module-namespace values rather than literals, so
every missing dynamic-construct arm surfaces there.

Summary by CodeRabbit

  • New Features

    • Improved support for creating built-in objects from constructor values at runtime.
    • Added broader coverage for dynamically obtained constructors, including promises, maps, sets, and text encoding/decoding.
  • Bug Fixes

    • Fixed new Promise(...) when the constructor is passed around as a value before instantiation.
    • Improved behavior for text encoding and decoding methods, including partial buffer handling and multi-byte data.

…alue

`new P(executor)` where P holds the global Promise constructor VALUE (a
polyfill or bundler alias) threw "Constructor Promise requires 'new'": the
dynamic-construct builtin match had no "Promise" arm, so the callee fell
through to the closure fallback, which called promise_constructor_call_thunk
as a plain function.

Add the missing arm, routing to js_promise_new_with_executor exactly like the
static literal lowering (non-callable executors still throw the spec
TypeError).

Adds test_gap_dynamic_builtin_construct_dispatch.ts, byte-for-byte against
node: Promise-value construction (resolve + reject paths), dynamic
TextEncoder/TextDecoder method dispatch through require("util") constructor
values (regression coverage for #6162), and Map/Set constructor-value guards.
@coderabbitai

coderabbitai Bot commented Jul 9, 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: 817bb142-1dea-42e5-8189-7887131e1491

📥 Commits

Reviewing files that changed from the base of the PR and between d9500be and 70d8c10.

📒 Files selected for processing (2)
  • crates/perry-runtime/src/object/class_registry/construct.rs
  • test-files/test_gap_dynamic_builtin_construct_dispatch.ts

📝 Walkthrough

Walkthrough

Adds a Promise match arm to the value-based global-builtin constructor dispatch in js_new_function_construct, extracting the executor argument and invoking js_promise_new_with_executor. Adds a test file covering dynamic construction and method dispatch for TextEncoder, TextDecoder, Promise, Map, and Set.

Changes

Dynamic Promise Construction

Layer / File(s) Summary
Promise constructor dispatch arm
crates/perry-runtime/src/object/class_registry/construct.rs
Adds a Promise match arm that extracts the executor argument, converts it to an executor pointer if pointer-tagged (else null), calls js_promise_new_with_executor, and returns the resulting promise as a NaN-boxed pointer.
Dynamic construction/dispatch test coverage
test-files/test_gap_dynamic_builtin_construct_dispatch.ts
New test file validating value-retrieved constructors (TextEncoder, TextDecoder, Promise, Map, Set) with new and subsequent dynamic method dispatch, including encodeInto/encode/decode behavior and Promise resolve/reject paths.

Estimated code review effort: 2 (Simple) | ~10 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Test as Test Script
  participant Dispatch as js_new_function_construct
  participant Promise as js_promise_new_with_executor

  Test->>Dispatch: new Promise(executor)
  Dispatch->>Dispatch: extract executor argument
  Dispatch->>Dispatch: check pointer-tagged closure/function
  Dispatch->>Promise: js_promise_new_with_executor(executorPtr)
  Promise-->>Dispatch: constructed promise
  Dispatch-->>Test: NaN-boxed promise pointer
Loading

Possibly related PRs

  • PerryTS/perry#5421: Both PRs extend the same value-based new dispatch in js_new_function_construct by adding global-constructor match arms.
  • PerryTS/perry#5677: Both PRs modify the same constructor-dispatch logic in js_new_function_construct in construct.rs.
  • PerryTS/perry#5821: Both PRs touch Promise constructor plumbing in the runtime.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The PR explains the problem, fix, and validation, but it does not follow the required template sections like Summary, Changes, Related issue, and Test plan. Reformat the description to match the template and add the missing Summary, Changes, Related issue, Test plan, and Checklist sections.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main runtime fix for dynamic Promise construction via a constructor value.
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 docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/dynamic-builtin-te-td-promise

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.

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