Skip to content

jsc: implement CoerceTo for JSValue and drop the two get_optional_value shims - #40311

Closed
robobun wants to merge 1 commit into
mainfrom
farm/82e10165/jsvalue-get-optional-dedupe
Closed

robobun wants to merge 1 commit into
mainfrom
farm/82e10165/jsvalue-get-optional-dedupe

Conversation

@robobun

@robobun robobun commented Aug 24, 2026

Copy link
Copy Markdown
Collaborator

Problem

  • Bun.Terminal option parsing (src/runtime/api/bun/Terminal.rs) and the bake config parser (src/runtime/bake/bake_body.rs) each carry a private get_optional_value shim. Both read a property and map undefined or null to None. Their bodies are identical. mordant reports this as reimplemented_helper for Terminal.rs, accepted by the baseline line "reimplemented_helper:src/runtime/api/bun/Terminal.rs" = 1.
  • bun_jsc already has this read as JSValue::get_optional::<T>. It only lacked a CoerceTo impl that hands the value back unchanged, so the two crates could not call it for a plain JSValue. The Terminal shim also duplicated get_optional::<i32> as get_optional_i32.

Fix

  • Add impl CoerceTo for JSValue in src/jsc/JSValue.rs. coerce_from returns the value. get_optional::<JSValue> is then the same read the shims did: get, then None for undefined or null.
  • Delete the JSValueTerminalExt trait in Terminal.rs and the get_optional_value function in bake_body.rs. The 13 call sites use get_optional::<JSValue> or get_optional::<i32>.
  • Behavior is unchanged. JSValue::get_optional has the same two steps as the shims: get returns None for a missing property and for undefined, then is_undefined_or_null filters null, then coerce. For i32 the Terminal shim called the same coerce::<i32>.
  • Verified: cargo dylint --all -p bun_runtime is clean against the edited baseline. bun bd test test/js/bun/terminal/terminal.test.ts and bun bd test test/bake/dev/bundle.test.ts pass.

Background

  • JSValue::get(global, name) returns Ok(None) when the property is missing or undefined, Ok(Some(v)) otherwise, and Err when the getter throws.
  • CoerceTo is the dispatch trait behind JSValue::coerce::<T>(). i32 and i64 implement it with ToNumber style conversions. get_optional::<T> composes get, the null filter, and coerce::<T>.
  • mordant is the dylint lint pack CI runs through bun run rust:mordant. mordant-baseline.toml records, per lint and file, how many findings are accepted. reimplemented_helper flags two functions in one crate with the same signature and body.

…ue shims

Terminal.rs and bake_body.rs each had a private get_optional_value that
reads a property and maps undefined or null to None. JSValue::get_optional
already does that read; it only lacked a CoerceTo impl for a plain
JSValue. Add the identity impl and call get_optional::<JSValue> and
get_optional::<i32> at the 13 sites.

Remove the reimplemented_helper baseline line for Terminal.rs.
@coderabbitai

coderabbitai Bot commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: d69535d5-2e3b-429d-a42f-1755fd397411

📥 Commits

Reviewing files that changed from the base of the PR and between f2fe7d3 and 81ee4b8.

📒 Files selected for processing (4)
  • mordant-baseline.toml
  • src/jsc/JSValue.rs
  • src/runtime/api/bun/Terminal.rs
  • src/runtime/bake/bake_body.rs
💤 Files with no reviewable changes (1)
  • mordant-baseline.toml

Included review availability: Your plan provides up to 5 included reviews per hour; 0 remain after this review.


Walkthrough

Changes

Typed optional access

Layer / File(s) Summary
JSValue identity coercion
src/jsc/JSValue.rs
JSValue implements CoerceTo and returns itself without coercion.
Runtime option parsing migration
src/runtime/api/bun/Terminal.rs, src/runtime/bake/bake_body.rs, mordant-baseline.toml
Terminal and bake option parsing use typed get_optional calls. Local shims and the related baseline entry are removed.

Suggested reviewers: jarred-sumner, dylan-conway

Merge Risk: ⚪ Minimal · up to 81ee4

This localized change centralizes optional JavaScript value handling and removes duplicate parsing helpers without a reported behavior change; no actionable merge-blocking risk remains after normal checks and review.

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Description check ✅ Passed The description explains the problem, fix, behavior, background, and verification steps, although it uses different headings from the template.
Title check ✅ Passed The title clearly identifies the JSValue change and the removal of both get_optional_value shims.
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.

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

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM — mechanical dedup that routes two local get_optional_value shims through the existing JSValue::get_optional::<T> by adding an identity CoerceTo for JSValue.

What was reviewed

  • Traced get_optional::<T> (JSValue.rs:1528) and get (JSValue.rs:1109): same two-step filter (missing/undefined → None, then null → None) as the deleted shims, so get_optional::<JSValue> is byte-for-byte equivalent.
  • get_optional_i32 → get_optional::<i32>: both end in coerce::<i32>, identical semantics.
  • Checked all 13 rewritten call sites in Terminal.rs and bake_body.rs — argument order and property names unchanged; baseline entry removal matches the deleted duplicate.
Extended reasoning...

Overview

This PR deduplicates two private helper functions. Terminal.rs had a JSValueTerminalExt trait with get_optional_value / get_optional_i32, and bake_body.rs had a free get_optional_value function. Both implemented the same read: get(global, name)?, filter undefined/null to None, otherwise return the value (or coerce to i32). The shared JSValue::get_optional::<T: CoerceTo> in bun_jsc already does exactly this composition but lacked a CoerceTo impl for JSValue itself. The PR adds a 3-line identity impl (coerce_from returns Ok(v)), rewires 13 call sites to get_optional::<JSValue> / get_optional::<i32>, deletes the shims, and removes the corresponding reimplemented_helper mordant baseline line.

Security risks

None. This is option-parsing plumbing with no change to what values are accepted, coerced, or rejected. No auth, crypto, path handling, or untrusted-input parsing behavior is touched.

Level of scrutiny

Low. I verified the equivalence claim directly against source: get (JSValue.rs:1109-1137) maps missing/ValueDeleted/undefined to None; get_optional (JSValue.rs:1528-1540) then filters is_undefined_or_null() and calls coerce::<T>. With the new identity CoerceTo for JSValue, get_optional::<JSValue> is precisely match get()? { Some(v) if !v.is_undefined_or_null() => Some(v), _ => None } — the deleted shim body. The i32 path already used the same coerce::<i32> the deleted get_optional_i32 called. The change is purely structural.

Other factors

The PR description states bun bd test test/js/bun/terminal/terminal.test.ts and bun bd test test/bake/dev/bundle.test.ts pass, and cargo dylint is clean against the edited baseline. The diff is small (~60 lines net removed), touches no lifecycle/refcount/GC code, and follows the repo's stated preference for extending shared helpers over per-crate shims. No prior review comments on the timeline.

@robobun

robobun commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator Author

Closing as stale: this PR conflicts with main, and its last commit is from 2026-08-24.

The branch stays, so the work is not lost. To pick it up again, rebase the branch on main and reopen this PR.

@robobun robobun closed this Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants