-
Notifications
You must be signed in to change notification settings - Fork 71
Use R C API compliant S4 utilities #2084
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Conversation
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
DavisVaughan
commented
Oct 23, 2025
Comment on lines
-274
to
297
|
|
||
| static inline SEXP r_mark_s4(SEXP x) { | ||
| SET_S4_OBJECT(x); | ||
| return(x); | ||
| static inline bool r_is_s4(SEXP x) { | ||
| return Rf_isS4(x); | ||
| } | ||
| static inline SEXP r_unmark_s4(SEXP x) { | ||
| UNSET_S4_OBJECT(x); | ||
| return(x); | ||
| static inline SEXP r_as_s4(SEXP x) { | ||
| // - Return value must be used, unlike `SET_S4_OBJECT()` | ||
| // - `Rf_asS4()` calls `shallow_duplicate(x)` if `MAYBE_SHARED(x)` | ||
| // - `flag = 1` goes through `SET_S4_OBJECT()` | ||
| // - `complete` is never utilized when `flag = 1` | ||
| const Rboolean flag = 1; | ||
| const int complete = 0; | ||
| return Rf_asS4(x, flag, complete); | ||
| } | ||
| static inline SEXP r_as_not_s4(SEXP x) { | ||
| // - Return value must be used, unlike `UNSET_S4_OBJECT()` | ||
| // - `Rf_asS4()` calls `shallow_duplicate(x)` if `MAYBE_SHARED(x)` | ||
| // - `flag = 0` goes through `UNSET_S4_OBJECT()` | ||
| // - `complete` is for S4 objects that wrap a "complete" S3 object by placing | ||
| // it in the `.Data` slot. If you set `complete = 1`, it will unwrap and | ||
| // return that, which we don't want. If `complete = 0`, no additional | ||
| // behavior will happen beyond the `UNSET_S4_OBJECT()` call. | ||
| const Rboolean flag = 0; | ||
| const int complete = 0; | ||
| return Rf_asS4(x, flag, complete); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is the family of 3 S4 utilities we need
The main points are:
- These are no longer "mark" functions. The return value actually does matter. I have updated the names and call sites accordingly.
Rf_asS4(), used for both setting and unsetting the S4 bit, will shallow clonexif it is shared. Where we use it invec_restore_default()we have freshly clonedxalready withvec_clone_referenced(x, ownership)so it should not look shared unless we providedVCTRS_OWNERSHIP_deepandxwas somehow shared already, even though we claimed to own it completely. There is nothing we can do about this since we don't have access toSET_S4_OBJECT()now. I don't know how common this would be.completeis not anything we need to worry about, we just set it to0and it is never used.
|
|
||
| if (is_s4) { | ||
| r_mark_s4(x); | ||
| x = r_as_s4(x); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not protecting because we are on the way out here
lionel-
approved these changes
Oct 24, 2025
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.
Part of #1933
Closes #1940
Uses
Rf_isS4()andRf_asS4()fromhttps://github.com/wch/r-source/blob/8d57e7515089387414c75df502f3f0c0e2c022e5/src/main/objects.c#L1838-L1878
Most important comments are pointed out below as code comments