Skip to content

Commit 4ed4e8d

Browse files
committed
use R_getAttribNames() in R 4.6.0+
1 parent c8f3318 commit 4ed4e8d

1 file changed

Lines changed: 17 additions & 3 deletions

File tree

src/jri/src/Rengine.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -410,19 +410,33 @@ JNIEXPORT jobjectArray JNICALL Java_org_rosuda_JRI_Rengine_rniGetAttrNames
410410
(JNIEnv *env, jobject this, jlong exp)
411411
{
412412
SEXP o = L2SEXP(exp);
413-
SEXP att = ATTRIB(o), ah = att;
414413
unsigned int ac = 0;
415414
jobjectArray sa;
415+
#if (R_VERSION >= R_Version(4,6,0))
416+
SEXP ans = R_getAttribNames(o);
417+
ac = (unsigned int) XLENGTH(o);
418+
if (!ac) return 0;
419+
#else
420+
SEXP att = ATTRIB(o), ah = att;
416421
if (att == R_NilValue) return 0;
417422
/* count the number of attributes */
418423
while (ah != R_NilValue) {
419424
ac++;
420425
ah = CDR(ah);
421426
}
427+
#endif
422428
/* allocate Java array */
423429
sa = (*env)->NewObjectArray(env, ac, (*env)->FindClass(env, "java/lang/String"), 0);
424430
if (!sa) return 0;
425431
ac = 0;
432+
#if (R_VERSION >= R_Version(4,6,0))
433+
R_xlen_t i = 0, n = XLENGTH(ans);
434+
while (i < n) {
435+
jobject s = (*env)->NewStringUTF(env, CHAR_UTF8(STRING_ELT(ans, i)));
436+
(*env)->SetObjectArrayElement(env, sa, (unsigned int)i, s);
437+
i++;
438+
}
439+
#else
426440
ah = att;
427441
/* iterate again and set create the strings */
428442
while (ah != R_NilValue) {
@@ -434,6 +448,7 @@ JNIEXPORT jobjectArray JNICALL Java_org_rosuda_JRI_Rengine_rniGetAttrNames
434448
ac++;
435449
ah = CDR(ah);
436450
}
451+
#endif
437452
return sa;
438453
}
439454

@@ -444,8 +459,7 @@ JNIEXPORT void JNICALL Java_org_rosuda_JRI_Rengine_rniSetAttr
444459
if (!an || an==R_NilValue || exp==0 || L2SEXP(exp)==R_NilValue) return;
445460

446461
setAttrib(L2SEXP(exp), an, (attr==0)?R_NilValue:L2SEXP(attr));
447-
448-
/* BTW: we don't need to adjust the object bit for "class", setAttrib does that already */
462+
/* BTW: we don't need to adjust the object bit for "class", setAttrib does that already */
449463

450464
/* this is not official API, but whoever uses this should know what he's doing
451465
it's ok for directly constructing attr lists, and that's what it should be used for

0 commit comments

Comments
 (0)