@@ -136,6 +136,125 @@ test.list <- atime::atime_test_list(
136136 " NAMESPACE" ,
137137 sprintf(' useDynLib\\ ("?%s"?' , Package_regex ),
138138 paste0(' useDynLib(' , new.Package_ ))
139+ pkg_find_replace(
140+ file.path(" src" , " Makevars.*in" ),
141+ " @PKG_CFLAGS@" , " @PKG_CFLAGS@ -DSTRING_PTR_RO=STRING_PTR_RO" )
142+ backports = c(
143+ " src/data.table.h" = '
144+ #include <Rversion.h>
145+ #if R_VERSION >= R_Version(4, 6, 0)
146+ // backports.c
147+ void SETLENGTH(SEXP x, R_xlen_t n);
148+ R_xlen_t TRUELENGTH(SEXP x);
149+ void SET_TRUELENGTH(SEXP x, R_xlen_t n);
150+ void SET_GROWABLE_BIT(SEXP);
151+ int LEVELS(SEXP);
152+ int NAMED(SEXP);
153+ #define REFCNT(x) NAMED(x)
154+ SEXP ATTRIB(SEXP);
155+ void SET_ATTRIB(SEXP, SEXP);
156+ int OBJECT(SEXP);
157+ void SET_OBJECT(SEXP, int);
158+ #define isFrame(x) isDataFrame(x)
159+ #define GetOption(x, none) GetOption1(x)
160+ #undef findVar // Rf_ mapping remains
161+ #define findVar(sym, env) R_getVar(sym, env, FALSE)
162+ #define STRING_PTR(x) ((SEXP *)STRING_PTR_RO(x))
163+ int IS_S4_OBJECT(SEXP);
164+ void SET_S4_OBJECT(SEXP);
165+ void UNSET_S4_OBJECT(SEXP);
166+ void SET_TYPEOF(SEXP, int);
167+ #define VECTOR_ELT(x, i) VECTOR_ELT_(x, i)
168+ SEXP VECTOR_ELT_(SEXP, R_xlen_t);
169+ #define VECTOR_PTR(x) ((SEXP*)DATAPTR_RO(x))
170+ #define DATAPTR(x) ((void*)DATAPTR_RO(x))
171+ #endif
172+ ' ,
173+ " src/backports.c" = '
174+ #include "data.table.h"
175+ #if R_VERSION >= R_Version(4, 6, 0)
176+ #define NAMED_BITS 16
177+ struct sxpinfo_struct {
178+ SEXPTYPE type : TYPE_BITS; // in Rinternals.h
179+ unsigned int scalar: 1;
180+ unsigned int obj : 1;
181+ unsigned int alt : 1;
182+ unsigned int gp : 16;
183+ unsigned int mark : 1;
184+ unsigned int debug : 1;
185+ unsigned int trace : 1;
186+ unsigned int spare : 1;
187+ unsigned int gcgen : 1;
188+ unsigned int gccls : 3;
189+ unsigned int named : NAMED_BITS;
190+ unsigned int extra : 32 - NAMED_BITS;
191+ };
192+
193+ struct vecsxp_struct {
194+ R_xlen_t length;
195+ R_xlen_t truelength;
196+ };
197+
198+ typedef struct VECTOR_SEXPREC {
199+ struct sxpinfo_struct sxpinfo;
200+ SEXP attrib;
201+ SEXP gengc_next_node, gengc_prev_node;
202+ struct vecsxp_struct vecsxp;
203+ } *VECSEXP;
204+
205+ void SETLENGTH(SEXP x, R_xlen_t n) {
206+ ((VECSEXP)x)->vecsxp.length = n;
207+ }
208+ R_xlen_t TRUELENGTH(SEXP x) {
209+ return ((VECSEXP)x)->vecsxp.truelength;
210+ }
211+ void SET_TRUELENGTH(SEXP x, R_xlen_t n) {
212+ ((VECSEXP)x)->vecsxp.truelength = n;
213+ }
214+ void SET_GROWABLE_BIT(SEXP x) {
215+ ((VECSEXP)x)->sxpinfo.gp |= 0x20;
216+ }
217+ int LEVELS(SEXP x) {
218+ return ((VECSEXP)x)->sxpinfo.gp;
219+ }
220+ int NAMED(SEXP x) {
221+ return ((VECSEXP)x)->sxpinfo.named;
222+ }
223+ int OBJECT(SEXP x) {
224+ return ((VECSEXP)x)->sxpinfo.obj;
225+ }
226+ void SET_OBJECT(SEXP x, int o) {
227+ ((VECSEXP)x)->sxpinfo.obj = o;
228+ }
229+ SEXP ATTRIB(SEXP x) {
230+ return ((VECSEXP)x)->attrib;
231+ }
232+ void SET_ATTRIB(SEXP x, SEXP att) {
233+ ((VECSEXP)x)->attrib = att;
234+ }
235+ #define S4_OBJECT (1<<4)
236+ int IS_S4_OBJECT(SEXP x) {
237+ return ((VECSEXP)x)->sxpinfo.gp & S4_OBJECT;
238+ }
239+ void SET_S4_OBJECT(SEXP x) {
240+ ((VECSEXP)x)->sxpinfo.gp |= S4_OBJECT;
241+ }
242+ void UNSET_S4_OBJECT(SEXP x) {
243+ ((VECSEXP)x)->sxpinfo.gp &= ~S4_OBJECT;
244+ }
245+ void SET_TYPEOF(SEXP x, int type) {
246+ ((VECSEXP)x)->sxpinfo.type = type;
247+ }
248+ SEXP VECTOR_ELT_(SEXP x, R_xlen_t i) {
249+ return ALTREP(x) ? (VECTOR_ELT)(x, i) : ((SEXP*)DATAPTR_RO(x))[i];
250+ }
251+ #endif
252+ ' )
253+ for (n in names(backports )) {
254+ f = file(file.path(new.pkg.path , n ), " a" )
255+ writeLines(backports [[n ]], f )
256+ close(f )
257+ }
139258 },
140259
141260 # Constant overhead improvement https://github.com/Rdatatable/data.table/pull/6925
0 commit comments