refactor: extract type-check boilerplate into a single require_internal_slot! macro#5233
refactor: extract type-check boilerplate into a single require_internal_slot! macro#5233Nakshatra480 wants to merge 3 commits intoboa-dev:mainfrom
require_internal_slot! macro#5233Conversation
4053092 to
4c42492
Compare
Test262 conformance changes
Tested main commit: |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #5233 +/- ##
===========================================
+ Coverage 47.24% 60.25% +13.01%
===========================================
Files 476 589 +113
Lines 46892 62702 +15810
===========================================
+ Hits 22154 37783 +15629
- Misses 24738 24919 +181 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
nekevss
left a comment
There was a problem hiding this comment.
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?
4c42492 to
56ac6f3
Compare
56ac6f3 to
4fee1fe
Compare
4fee1fe to
2fdc570
Compare
2fdc570 to
4c4edd1
Compare
…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)
4c4edd1 to
fb5096a
Compare
require_internal_slot! macro
require_internal_slot! macrorequire_internal_slot! macro
|
Hey @nekevss Thanks for the review! To answer your question: Yes, while originally working on Temporal, I realized that the 5-line this-type-check boilerplate is heavily duplicated across almost all built-ins. I have now expanded the scope of this PR to apply the macro across the entire engine, not just Temporal. Here is a summary of the updates in the latest push:
All CI tests, including |
Switch the require_internal_slot! macro to use JsObject::downcast instead of downcast_ref, returning JsObject<T> (owned handle) rather than Ref<T>. This prevents GcRefCell borrow panics by ensuring borrows are only held for the duration of specific read/write operations. Updated all call sites across 20 built-in modules to use .borrow().data() / .borrow_mut().data_mut() for inner field access: - Date (renamed macro binding to date_obj in setters to avoid shadowing) - Temporal (Duration, Instant, PlainDate, PlainDateTime, PlainTime, PlainMonthDay, PlainYearMonth, ZonedDateTime) - ArrayBuffer, SharedArrayBuffer, DataView, TypedArray - WeakRef, WeakMap, WeakSet - Intl (Collator, Segmenter, Segments)
|
Hey @jedel1043 I've pushed the requested changes using JsObject::downcast, and all builds and tests are now passing cleanly. The PR is ready for another look whenever you have time! |
Summary
This PR introduces a new
require_internal_slot!macro to replace the repetitive 5-line this-type-checking boilerplate found across the engine's built-in prototype methods.Previously, almost every prototype method (across Temporal, Date, TypedArrays, etc.) manually downcasted
thisand returned aTypeErrorif the internal slot was missing:This PR refactors those occurrences into a single, much cleaner macro call that correctly handles the necessary borrowing and lifetime scoping:
require_internal_slot!(date = this, Date, "Date");Scope of changes
While originally intended for just the Temporal built-ins, I realized this boilerplate is heavily duplicated across the entire engine. Therefore, I've broadened the scope and applied the macro to all affected built-ins, including:
Error Messages & Testing
As part of standardizing this pattern, all error messages across the engine for missing internal slots have been unified to use the generic format: TypeError: "the this object must be a {Type} object.".
All existing tests (specifically in Builtin::Date) that previously relied on custom error strings like "'this' is not a Date" have been updated to expect the new standardized message. CI tests now pass cleanly with these updates.