feat(m4-12): P2.5 generic this coercion — String.prototype methods#65
Merged
feat(m4-12): P2.5 generic this coercion — String.prototype methods#65
Conversation
Replace this_string_id (String-only) with coerce_this_string: RequireObjectCoercible(this) + ToString(this). All 23 String.prototype methods now correctly handle non-string this values: - null/undefined → TypeError - StringWrapper → unwrap to StringId - other primitives → ToString coercion String.prototype.indexOf.call(42, '2') now returns 1 (was error). 1460→1464 tests (+4: generic this coercion cases). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This PR updates the VM’s String.prototype native implementations to use spec-aligned “generic this” coercion (RequireObjectCoercible + ToString) so non-string this values behave correctly (throw on null/undefined; coerce other primitives; unwrap string wrappers).
Changes:
- Replaced the old
this_string_idhelper withcoerce_this_string(ctx, this) -> Result<StringId, VmError>. - Updated multiple
String.prototypenative methods (core + complement set) to use the new coercion helper. - Added regression tests covering non-string
thisbehavior (number/boolean coercion; null/undefined throw).
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| crates/script/elidex-js/src/vm/natives_string.rs | Introduces coerce_this_string and switches multiple String.prototype natives to use it. |
| crates/script/elidex-js/src/vm/natives_string_ext.rs | Switches String.prototype “complement” natives to use coerce_this_string. |
| crates/script/elidex-js/src/vm/tests/tests_string_complement.rs | Adds tests for generic this coercion behavior on String.prototype methods. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Replace
this_string_id(accepted onlyJsValue::String) withcoerce_this_string(RequireObjectCoercible + ToString) for all 23 String.prototype methods. Non-stringthisvalues now work correctly:null/undefined→ TypeErrorExample:
String.prototype.indexOf.call(42, '2')→1Tests: 1460 → 1464 (+4)
Test plan
mise run ci— full passcargo clippy -- -D warnings— clean🤖 Generated with Claude Code