Skip to content

Commit 2b0edbe

Browse files
committed
Use polyglot casts to convert boxed primitives to C primitives.
1 parent ff00ffe commit 2b0edbe

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

graalpython/com.oracle.graal.python.cext/src/capi.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,10 @@ const char* PyTruffle_StringToCstr(void* o, int32_t strLen) {
364364
return str;
365365
}
366366

367-
const char* PyTruffle_CstrToString(const char* o) {
367+
void* PyTruffle_CstrToString(void* o) {
368+
if (polyglot_fits_in_i64(o)) {
369+
return polyglot_from_string((const char*)polyglot_as_i64(o), SRC_CS);
370+
}
368371
return polyglot_from_string(o, SRC_CS);
369372
}
370373

graalpython/com.oracle.graal.python.cext/src/modsupport.c

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,8 @@ typedef struct _build_stack {
533533
PyObject* _Py_BuildValue_SizeT(const char *format, ...) {
534534
# define ARG polyglot_get_arg(value_idx)
535535
# define APPEND_VALUE(list, value) PyList_Append(list, value); value_idx++
536+
# define AS_I64(__arg__) (polyglot_fits_in_i64((__arg__)) ? polyglot_as_i64((__arg__)) : ((int64_t)(__arg__)))
537+
# define AS_DOUBLE(__arg__) (polyglot_fits_in_double((__arg__)) ? polyglot_as_double((__arg__)) : ((double)(unsigned long long)(__arg__)))
536538

537539
PyObject* (*converter)(void*) = NULL;
538540
char argchar[2] = {'\0'};
@@ -551,7 +553,7 @@ PyObject* _Py_BuildValue_SizeT(const char *format, ...) {
551553
case 'z':
552554
case 'U':
553555
if (format[format_idx + 1] == '#') {
554-
int size = (int)polyglot_get_arg(value_idx + 1);
556+
int size = (int) AS_I64(polyglot_get_arg(value_idx + 1));
555557
if (ARG == NULL) {
556558
APPEND_VALUE(list, Py_None);
557559
} else {
@@ -569,7 +571,7 @@ PyObject* _Py_BuildValue_SizeT(const char *format, ...) {
569571
break;
570572
case 'y':
571573
if (format[format_idx + 1] == '#') {
572-
int size = (int)polyglot_get_arg(value_idx + 1);
574+
int size = (int) AS_I64(polyglot_get_arg(value_idx + 1));
573575
if (ARG == NULL) {
574576
APPEND_VALUE(list, Py_None);
575577
} else {
@@ -591,39 +593,39 @@ PyObject* _Py_BuildValue_SizeT(const char *format, ...) {
591593
case 'i':
592594
case 'b':
593595
case 'h':
594-
APPEND_VALUE(list, PyLong_FromLong((int)ARG));
596+
APPEND_VALUE(list, PyLong_FromLong((int)AS_I64(ARG)));
595597
break;
596598
case 'l':
597-
APPEND_VALUE(list, PyLong_FromLong((long)ARG));
599+
APPEND_VALUE(list, PyLong_FromLong(AS_I64(ARG)));
598600
break;
599601
case 'B':
600602
case 'H':
601603
case 'I':
602-
APPEND_VALUE(list, PyLong_FromUnsignedLong((unsigned int)ARG));
604+
APPEND_VALUE(list, PyLong_FromUnsignedLong((unsigned int)AS_I64(ARG)));
603605
break;
604606
case 'k':
605-
APPEND_VALUE(list, PyLong_FromUnsignedLong((unsigned long)ARG));
607+
APPEND_VALUE(list, PyLong_FromUnsignedLong((unsigned long)AS_I64(ARG)));
606608
break;
607609
case 'L':
608-
APPEND_VALUE(list, PyLong_FromLongLong((long long)ARG));
610+
APPEND_VALUE(list, PyLong_FromLongLong((long long)AS_I64(ARG)));
609611
break;
610612
case 'K':
611-
APPEND_VALUE(list, PyLong_FromLongLong((unsigned long long)ARG));
613+
APPEND_VALUE(list, PyLong_FromLongLong((unsigned long long)AS_I64(ARG)));
612614
break;
613615
case 'n':
614-
APPEND_VALUE(list, PyLong_FromSsize_t((Py_ssize_t)ARG));
616+
APPEND_VALUE(list, PyLong_FromSsize_t((Py_ssize_t)AS_I64(ARG)));
615617
break;
616618
case 'c':
617-
argchar[0] = (char)ARG;
619+
argchar[0] = (char)AS_I64(ARG);
618620
APPEND_VALUE(list, PyBytes_FromStringAndSize(argchar, 1));
619621
break;
620622
case 'C':
621-
argchar[0] = (char)ARG;
623+
argchar[0] = (char)AS_I64(ARG);
622624
APPEND_VALUE(list, polyglot_from_string(argchar, "ascii"));
623625
break;
624626
case 'd':
625627
case 'f':
626-
APPEND_VALUE(list, PyFloat_FromDouble((double)(unsigned long long)ARG));
628+
APPEND_VALUE(list, PyFloat_FromDouble((double)AS_DOUBLE(ARG)));
627629
break;
628630
case 'D':
629631
fprintf(stderr, "error: unsupported format 'D'\n");

0 commit comments

Comments
 (0)