Improve TypeError diagnostics in WeakMap, WeakSet and Object builtins and update tests#4863
Conversation
… and update tests Signed-off-by: mrhapile <allinonegaming3456@gmail.com>
There was a problem hiding this comment.
Pull request overview
This PR improves TypeError diagnostics for receiver/argument validation across the WeakMap, WeakSet, and Object builtins, and updates unit tests to assert the new, more explicit error messages.
Changes:
- Update
WeakMap/WeakSetprototype methods to emit clearer “expected 'this' …” TypeErrors when called with an invalid receiver. - Improve
Objectbuiltin TypeError messages (e.g.,Object.create,Object.setPrototypeOf, legacy__defineGetter__/__defineSetter__, etc.). - Update unit tests to match the revised diagnostics.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| core/engine/src/builtins/weak_set/mod.rs | Updates WeakSet prototype receiver TypeError messages to be more explicit. |
| core/engine/src/builtins/weak_map/mod.rs | Updates WeakMap prototype receiver TypeError messages to be more explicit. |
| core/engine/src/builtins/weak_map/tests.rs | Adjusts WeakMap tests to assert the new receiver TypeError strings. |
| core/engine/src/builtins/object/mod.rs | Refines TypeError messages for several Object builtins (create/setPrototypeOf/defineProperty/defineProperties/legacy accessors). |
| core/engine/src/builtins/object/tests.rs | Updates Object.create error-message assertions to match the new wording. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
| Err(JsNativeError::typ() | ||
| .with_message("Object.defineProperty called on non-object") | ||
| .with_message("Object.defineProperty: expected 'this' to be an Object") | ||
| .into()) |
There was a problem hiding this comment.
The TypeError message here refers to "'this'" being an Object, but this check is validating the first argument (args[0]) is an object. This makes the diagnostic misleading (the call receiver is ignored for this builtin). Consider updating the message to refer to the object/target parameter (and ideally include the received type) instead of 'this'.
| } else { | ||
| Err(JsNativeError::typ() | ||
| .with_message("Expected an object") | ||
| .with_message("Object.defineProperties: expected 'this' to be an Object") |
There was a problem hiding this comment.
Same issue as Object.defineProperty: the error message says it expected 'this' to be an Object, but the code is rejecting non-object first arguments. Updating the message to reference the first parameter (and possibly the received type) would make the diagnostics accurate.
| .with_message("Object.defineProperties: expected 'this' to be an Object") | |
| .with_message("Object.defineProperties: expected first argument to be an object") |
Test262 conformance changes
Tested main commit: |
|
Have you thought refactoring to use the |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #4863 +/- ##
===========================================
+ Coverage 47.24% 58.37% +11.12%
===========================================
Files 476 559 +83
Lines 46892 61409 +14517
===========================================
+ Hits 22154 35846 +13692
- Misses 24738 25563 +825 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Signed-off-by: mrhapile <allinonegaming3456@gmail.com>
I’ve refactored the error construction to use the |
|
@hansl Could you please review this? I’d like to make any necessary changes based on your feedback. |
jedel1043
left a comment
There was a problem hiding this comment.
Yep, the error messages look much better!
Improve TypeError diagnostics in WeakMap, WeakSet and Object builtins and update tests
This Pull Request fixes/closes #.
It changes the following:
WeakMap,WeakSet, andObjectbuiltins to provide clearer diagnostics."called with non-object value"and"Expected an object"with explicit messages indicating the expected receiver type.<Builtin>.<method>: expected 'this' to be a <Type> objectBefore
Some errors used generic messages that did not clearly indicate which builtin method failed or what type was expected.
Examples:
These messages lacked clear context about the failing method or the expected
thisvalue.After
Error messages now explicitly describe the expected receiver type and the builtin method where the error occurred.
Examples:
Files Modified
Engine changes
core/engine/src/builtins/weak_map/mod.rscore/engine/src/builtins/weak_set/mod.rscore/engine/src/builtins/object/mod.rsThese files were updated to replace generic
TypeErrormessages with more descriptive diagnostics.Test updates
To keep the test suite consistent with the improved diagnostics, expected error strings were updated in:
core/engine/src/builtins/weak_map/tests.rscore/engine/src/builtins/object/tests.rsNo behavioral changes were introduced; only error message diagnostics were improved and tests updated accordingly.