Skip to content

Commit 68692bd

Browse files
committed
Extend direct method call to 20 arguments.
1 parent 3f86d8d commit 68692bd

File tree

2 files changed

+69
-13
lines changed

2 files changed

+69
-13
lines changed

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

Lines changed: 68 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -413,24 +413,80 @@ int PyTruffle_Debug(void *arg) {
413413
return 0;
414414
}
415415

416-
PyObject *wrap_direct(PyCFunction fun, PyObject *module, ...) {
416+
typedef PyObject* (*f0)();
417+
typedef PyObject* (*f1)(PyObject*);
418+
typedef PyObject* (*f2)(PyObject*, PyObject*);
419+
typedef PyObject* (*f3)(PyObject*, PyObject*, PyObject*);
420+
typedef PyObject* (*f4)(PyObject*, PyObject*, PyObject*, PyObject*);
421+
typedef PyObject* (*f5)(PyObject*, PyObject*, PyObject*, PyObject*, PyObject*);
422+
typedef PyObject* (*f6)(PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*);
423+
typedef PyObject* (*f7)(PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*);
424+
typedef PyObject* (*f8)(PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*);
425+
typedef PyObject* (*f9)(PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*);
426+
typedef PyObject* (*f10)(PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*);
427+
typedef PyObject* (*f11)(PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*);
428+
typedef PyObject* (*f12)(PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*);
429+
typedef PyObject* (*f13)(PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*);
430+
typedef PyObject* (*f14)(PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*);
431+
typedef PyObject* (*f15)(PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*);
432+
typedef PyObject* (*f16)(PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*);
433+
typedef PyObject* (*f17)(PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*);
434+
typedef PyObject* (*f18)(PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*);
435+
typedef PyObject* (*f19)(PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*);
436+
typedef PyObject* (*f20)(PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*);
437+
438+
#define _PICK_FUN_CAST(DUMMY, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, NAME, ...) NAME
439+
#define _CALL_ARITY(FUN, ...) ( (_PICK_FUN_CAST(NULL, ##__VA_ARGS__, f20, f19, f18, f17, f16, f15, f14, f13, f12, f11, f10, f9, f8, f7, f6, f5, f4, f3, f2, f1, f0))(FUN))(__VA_ARGS__)
440+
#define ARG(__n) _explicit_cast((PyObject*)polyglot_get_arg((__n)))
441+
442+
PyObject *wrap_direct(PyCFunction fun, ...) {
417443
PyObject *res = NULL;
418-
switch(polyglot_get_arg_count()) {
444+
switch(polyglot_get_arg_count()-1) {
419445
case 0:
420-
res = fun(Py_None, Py_None);
421-
break;
446+
return _CALL_ARITY(fun);
447+
case 1:
448+
return _CALL_ARITY(fun, ARG(1));
422449
case 2:
423-
res = fun(module, Py_None);
424-
break;
450+
return _CALL_ARITY(fun, ARG(1), ARG(2));
425451
case 3:
426-
res = fun(module, _explicit_cast((PyObject*)polyglot_get_arg(2)));
427-
break;
452+
return _CALL_ARITY(fun, ARG(1), ARG(2), ARG(3));
428453
case 4:
429-
res = ((PyCFunctionWithKeywords)fun)(module, _explicit_cast((PyObject*)polyglot_get_arg(1)), _explicit_cast((PyObject*)polyglot_get_arg(3)));
430-
break;
454+
return _CALL_ARITY(fun, ARG(1), ARG(2), ARG(3), ARG(4));
455+
case 5:
456+
return _CALL_ARITY(fun, ARG(1), ARG(2), ARG(3), ARG(4), ARG(5));
457+
case 6:
458+
return _CALL_ARITY(fun, ARG(1), ARG(2), ARG(3), ARG(4), ARG(5), ARG(6));
459+
case 7:
460+
return _CALL_ARITY(fun, ARG(1), ARG(2), ARG(3), ARG(4), ARG(5), ARG(6), ARG(7));
461+
case 8:
462+
return _CALL_ARITY(fun, ARG(1), ARG(2), ARG(3), ARG(4), ARG(5), ARG(6), ARG(7), ARG(8));
463+
case 9:
464+
return _CALL_ARITY(fun, ARG(1), ARG(2), ARG(3), ARG(4), ARG(5), ARG(6), ARG(7), ARG(8), ARG(9));
465+
case 10:
466+
return _CALL_ARITY(fun, ARG(1), ARG(2), ARG(3), ARG(4), ARG(5), ARG(6), ARG(7), ARG(8), ARG(9), ARG(10));
467+
case 11:
468+
return _CALL_ARITY(fun, ARG(1), ARG(2), ARG(3), ARG(4), ARG(5), ARG(6), ARG(7), ARG(8), ARG(9), ARG(10), ARG(11));
469+
case 12:
470+
return _CALL_ARITY(fun, ARG(1), ARG(2), ARG(3), ARG(4), ARG(5), ARG(6), ARG(7), ARG(8), ARG(9), ARG(10), ARG(11), ARG(12));
471+
case 13:
472+
return _CALL_ARITY(fun, ARG(1), ARG(2), ARG(3), ARG(4), ARG(5), ARG(6), ARG(7), ARG(8), ARG(9), ARG(10), ARG(11), ARG(12), ARG(13));
473+
case 14:
474+
return _CALL_ARITY(fun, ARG(1), ARG(2), ARG(3), ARG(4), ARG(5), ARG(6), ARG(7), ARG(8), ARG(9), ARG(10), ARG(11), ARG(12), ARG(13), ARG(14));
475+
case 15:
476+
return _CALL_ARITY(fun, ARG(1), ARG(2), ARG(3), ARG(4), ARG(5), ARG(6), ARG(7), ARG(8), ARG(9), ARG(10), ARG(11), ARG(12), ARG(13), ARG(14), ARG(15));
477+
case 16:
478+
return _CALL_ARITY(fun, ARG(1), ARG(2), ARG(3), ARG(4), ARG(5), ARG(6), ARG(7), ARG(8), ARG(9), ARG(10), ARG(11), ARG(12), ARG(13), ARG(14), ARG(15), ARG(16));
479+
case 17:
480+
return _CALL_ARITY(fun, ARG(1), ARG(2), ARG(3), ARG(4), ARG(5), ARG(6), ARG(7), ARG(8), ARG(9), ARG(10), ARG(11), ARG(12), ARG(13), ARG(14), ARG(15), ARG(16), ARG(17));
481+
case 18:
482+
return _CALL_ARITY(fun, ARG(1), ARG(2), ARG(3), ARG(4), ARG(5), ARG(6), ARG(7), ARG(8), ARG(9), ARG(10), ARG(11), ARG(12), ARG(13), ARG(14), ARG(15), ARG(16), ARG(17), ARG(18));
483+
case 19:
484+
return _CALL_ARITY(fun, ARG(1), ARG(2), ARG(3), ARG(4), ARG(5), ARG(6), ARG(7), ARG(8), ARG(9), ARG(10), ARG(11), ARG(12), ARG(13), ARG(14), ARG(15), ARG(16), ARG(17), ARG(18), ARG(19));
485+
case 20:
486+
return _CALL_ARITY(fun, ARG(1), ARG(2), ARG(3), ARG(4), ARG(5), ARG(6), ARG(7), ARG(8), ARG(9), ARG(10), ARG(11), ARG(12), ARG(13), ARG(14), ARG(15), ARG(16), ARG(17), ARG(18), ARG(19), ARG(20));
431487
}
432-
PyTruffle_Debug(res);
433-
return res;
488+
_PyErr_BadInternalCall(__FILE__, __LINE__);
489+
return NULL;
434490
}
435491

436492
PyObject *wrap_varargs(PyCFunction fun, PyObject *module, PyObject *varargs) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ void initialize_exceptions();
9595
void initialize_hashes();
9696

9797
// prototype of C landing function
98-
PyObject *wrap_direct(PyCFunction fun, PyObject *module, ...);
98+
PyObject *wrap_direct(PyCFunction fun, ...);
9999
PyObject *wrap_varargs(PyCFunction fun, PyObject *module, PyObject *varargs);
100100
PyObject *wrap_keywords(PyCFunctionWithKeywords fun, PyObject *module, PyObject *varargs, PyObject *kwargs);
101101
PyObject *wrap_noargs(PyCFunction fun, PyObject *module, PyObject *pnone);

0 commit comments

Comments
 (0)