Skip to content

refactor(temporal): extract this-type-check boilerplate into a macro#5233

Draft
Nakshatra480 wants to merge 1 commit intoboa-dev:mainfrom
Nakshatra480:refactor/temporal-this-type-check
Draft

refactor(temporal): extract this-type-check boilerplate into a macro#5233
Nakshatra480 wants to merge 1 commit intoboa-dev:mainfrom
Nakshatra480:refactor/temporal-this-type-check

Conversation

@Nakshatra480
Copy link
Contributor

@Nakshatra480 Nakshatra480 commented Mar 23, 2026

Summary

This PR removes the repetitive this-type-check boilerplate present in almost every Temporal prototype method.

Previously, each method manually called this.as_object(), downcasted with .as_ref().and_then(JsObject::downcast_ref::<Self>), and threw a TypeError on failure - 5 lines repeated 158 times across 8 files.

I've extracted this into a single this_temporal_object! macro in temporal/mod.rs. A helper function isn't viable here because this.as_object() returns an owned JsObject that the resulting Ref<T> borrows from - the owner must live in the caller's scope. The macro solves this by emitting the let bindings directly there.

Changes

  • Replaced 158 occurrences of the type-check boilerplate across 8 Temporal modules with the new macro
  • Unified all TypeError messages to "the this object must be a {Type} object." - a few types like Duration previously used inconsistent phrasing
  • Net delta: +183 / -1,106 lines

N downcast_ref::<Self> calls in static ::from() methods are deliberately left untouched - those check item, not this.

@github-actions github-actions bot added Waiting On Review Waiting on reviews from the maintainers C-Builtins PRs and Issues related to builtins/intrinsics and removed Waiting On Review Waiting on reviews from the maintainers labels Mar 23, 2026
@github-actions github-actions bot added this to the v1.0.0 milestone Mar 23, 2026
@Nakshatra480 Nakshatra480 force-pushed the refactor/temporal-this-type-check branch from 4053092 to 4c42492 Compare March 23, 2026 05:48
@github-actions github-actions bot added the Waiting On Review Waiting on reviews from the maintainers label Mar 23, 2026
@github-actions
Copy link

github-actions bot commented Mar 23, 2026

Test262 conformance changes

Test result main count PR count difference
Total 52,963 52,963 0
Passed 50,732 50,732 0
Ignored 1,426 1,426 0
Failed 805 805 0
Panics 0 0 0
Conformance 95.79% 95.79% 0.00%

Tested main commit: 2437b6704c4315f6ec8f0766bf229fba23e0e61e
Tested PR commit: 2fdc570ea4f0449516cd7d3c19712c52d772bb9f
Compare commits: 2437b67...2fdc570

@codecov
Copy link

codecov bot commented Mar 23, 2026

Codecov Report

❌ Patch coverage is 3.03030% with 160 lines in your changes missing coverage. Please review.
✅ Project coverage is 60.39%. Comparing base (6ddc2b4) to head (56ac6f3).
⚠️ Report is 918 commits behind head on main.

Files with missing lines Patch % Lines
.../engine/src/builtins/temporal/zoneddatetime/mod.rs 0.00% 47 Missing ⚠️
...ngine/src/builtins/temporal/plain_date_time/mod.rs 2.70% 36 Missing ⚠️
...ore/engine/src/builtins/temporal/plain_date/mod.rs 0.00% 15 Missing ⚠️
...ore/engine/src/builtins/temporal/plain_time/mod.rs 6.25% 15 Missing ⚠️
...gine/src/builtins/temporal/plain_year_month/mod.rs 0.00% 15 Missing ⚠️
core/engine/src/builtins/temporal/instant/mod.rs 0.00% 12 Missing ⚠️
core/engine/src/builtins/temporal/duration/mod.rs 23.07% 10 Missing ⚠️
...ngine/src/builtins/temporal/plain_month_day/mod.rs 0.00% 8 Missing ⚠️
core/engine/src/builtins/temporal/mod.rs 0.00% 2 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##             main    #5233       +/-   ##
===========================================
+ Coverage   47.24%   60.39%   +13.15%     
===========================================
  Files         476      589      +113     
  Lines       46892    62753    +15861     
===========================================
+ Hits        22154    37902    +15748     
- Misses      24738    24851      +113     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Member

@nekevss nekevss left a comment

Choose a reason for hiding this comment

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

I do sort of like the change for how succinct it is, and it's potential for making the code more readable.

One question: why change just the temporal objects and not the built-ins in general? Had you looked at other built-ins?

@Nakshatra480 Nakshatra480 force-pushed the refactor/temporal-this-type-check branch from 4c42492 to 56ac6f3 Compare March 25, 2026 09:38
@Nakshatra480 Nakshatra480 requested a review from nekevss March 25, 2026 10:50
@Nakshatra480 Nakshatra480 marked this pull request as draft March 25, 2026 14:57
@Nakshatra480 Nakshatra480 force-pushed the refactor/temporal-this-type-check branch from 56ac6f3 to 4fee1fe Compare March 25, 2026 15:02
@github-actions github-actions bot added the C-Intl Changes related to the `Intl` implementation label Mar 25, 2026
@Nakshatra480 Nakshatra480 marked this pull request as ready for review March 25, 2026 15:05
@Nakshatra480 Nakshatra480 requested a review from a team as a code owner March 25, 2026 15:05
@Nakshatra480 Nakshatra480 marked this pull request as draft March 25, 2026 15:16
…lot! macro

Replace the repeated as_object/downcast_ref/ok_or_else boilerplate with
a single require_internal_slot! macro defined in builtins/mod.rs.

The macro produces two let-bindings in the caller's scope: one for the
owned JsObject (to keep it alive) and one for the downcast Ref<T>.
Uses the js_error! macro internally for error construction.

Temporal modules (163 replacements):
- zoneddatetime, plain_date_time, plain_time, plain_year_month,
  plain_date, duration, instant, plain_month_day

Other builtins (32 replacements):
- date (9), dataview (5), typed_array (4), array_buffer (4),
  shared_array_buffer (3), weak_map (2), intl/segmenter (2),
  intl/collator (1), weak_set (1), weak_ref (1)
@Nakshatra480 Nakshatra480 force-pushed the refactor/temporal-this-type-check branch from 4fee1fe to 2fdc570 Compare March 25, 2026 16:37
@github-actions github-actions bot added the C-Tests Issues and PRs related to the tests. label Mar 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-Builtins PRs and Issues related to builtins/intrinsics C-Intl Changes related to the `Intl` implementation C-Tests Issues and PRs related to the tests. Waiting On Review Waiting on reviews from the maintainers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants