49
49
import com .oracle .graal .python .builtins .modules .PythonCextBuiltins .PExternalFunctionWrapper ;
50
50
import com .oracle .graal .python .builtins .modules .PythonCextBuiltinsFactory .DefaultCheckFunctionResultNodeGen ;
51
51
import com .oracle .graal .python .builtins .objects .PNone ;
52
- import com .oracle .graal .python .builtins .objects .cell .CellBuiltins ;
53
- import com .oracle .graal .python .builtins .objects .cell .CellBuiltins .GetRefNode ;
54
- import com .oracle .graal .python .builtins .objects .cell .PCell ;
55
52
import com .oracle .graal .python .builtins .objects .cext .capi .CApiGuards ;
56
53
import com .oracle .graal .python .builtins .objects .cext .capi .CExtNodes ;
57
54
import com .oracle .graal .python .builtins .objects .cext .capi .CExtNodes .ConvertArgsToSulongNode ;
117
114
118
115
public abstract class ExternalFunctionNodes {
119
116
120
- /**
121
- * The index of the cell that contains the target (e.g. the pointer of native getter/setter
122
- * function).
123
- */
124
- static final int CELL_INDEX_TARGET = 0 ;
125
-
126
- public static PCell [] createPythonClosure (Object target , PythonObjectFactory factory , Assumption effectivelyFinal ) {
127
- PCell targetCell = factory .createCell (effectivelyFinal );
128
- targetCell .setRef (target );
129
- return new PCell []{targetCell };
130
- }
117
+ public static final String KW_CALLABLE = "$callable" ;
118
+ private static final String [] KEYWORDS_HIDDEN_CALLABLE = new String []{KW_CALLABLE };
131
119
132
120
public static final class MethDirectRoot extends PRootNode {
133
- private static final Signature SIGNATURE = Signature .createVarArgsAndKwArgsOnly ();
121
+
122
+ private static final Signature SIGNATURE = new Signature (-1 , true , 0 , false , null , KEYWORDS_HIDDEN_CALLABLE );
134
123
135
124
@ Child private ExternalFunctionInvokeNode invokeNode ;
136
125
@ Child private CalleeContext calleeContext = CalleeContext .create ();
137
- @ Child private CellBuiltins . GetRefNode readTargetCellNode ;
126
+ @ Child private ReadIndexedArgumentNode readCallableNode = ReadIndexedArgumentNode . create ( 0 ) ;
138
127
139
128
private final String name ;
140
129
@@ -148,9 +137,7 @@ private MethDirectRoot(PythonLanguage lang, String name) {
148
137
public Object execute (VirtualFrame frame ) {
149
138
calleeContext .enter (frame );
150
139
try {
151
- PCell [] frameClosure = PArguments .getClosure (frame );
152
- assert frameClosure .length == 1 : "invalid closure for MethDirectRoot" ;
153
- Object callable = ensureReadTargetCellNode ().execute (frameClosure [CELL_INDEX_TARGET ]);
140
+ Object callable = readCallableNode .execute (frame );
154
141
return ensureInvokeNode ().execute (frame , name , callable , PArguments .getVariableArguments (frame ), 0 );
155
142
} finally {
156
143
calleeContext .exit (frame , this );
@@ -191,14 +178,6 @@ private ExternalFunctionInvokeNode ensureInvokeNode() {
191
178
return invokeNode ;
192
179
}
193
180
194
- private GetRefNode ensureReadTargetCellNode () {
195
- if (readTargetCellNode == null ) {
196
- CompilerDirectives .transferToInterpreterAndInvalidate ();
197
- readTargetCellNode = insert (GetRefNode .create ());
198
- }
199
- return readTargetCellNode ;
200
- }
201
-
202
181
@ TruffleBoundary
203
182
public static MethDirectRoot create (PythonLanguage lang , String name ) {
204
183
return new MethDirectRoot (lang , name );
@@ -388,8 +367,8 @@ abstract static class MethodDescriptorRoot extends PRootNode {
388
367
@ Child private CalleeContext calleeContext = CalleeContext .create ();
389
368
@ Child private CallVarargsMethodNode invokeNode ;
390
369
@ Child private ExternalFunctionInvokeNode externalInvokeNode ;
391
- @ Child private CellBuiltins .GetRefNode readTargetCellNode ;
392
370
@ Child ReadIndexedArgumentNode readSelfNode = ReadIndexedArgumentNode .create (0 );
371
+ @ Child private ReadIndexedArgumentNode readCallableNode ;
393
372
394
373
private final String name ;
395
374
@@ -412,10 +391,7 @@ abstract static class MethodDescriptorRoot extends PRootNode {
412
391
public Object execute (VirtualFrame frame ) {
413
392
calleeContext .enter (frame );
414
393
try {
415
- PCell [] frameClosure = PArguments .getClosure (frame );
416
- assert frameClosure .length == 1 : "invalid closure for MethDirectRoot" ;
417
- Object callable = ensureReadTargetCellNode ().execute (frameClosure [CELL_INDEX_TARGET ]);
418
-
394
+ Object callable = ensureReadCallableNode ().execute (frame );
419
395
if (externalInvokeNode != null ) {
420
396
Object [] cArguments = prepareCArguments (frame );
421
397
try {
@@ -443,7 +419,8 @@ protected Object[] preparePArguments(VirtualFrame frame) {
443
419
Object [] variableArguments = PArguments .getVariableArguments (frame );
444
420
445
421
int variableArgumentsLength = variableArguments != null ? variableArguments .length : 0 ;
446
- int userArgumentLength = PArguments .getUserArgumentLength (frame );
422
+ // we need to subtract 1 due to the hidden default param that carries the callable
423
+ int userArgumentLength = PArguments .getUserArgumentLength (frame ) - 1 ;
447
424
int argumentsLength = userArgumentLength + variableArgumentsLength ;
448
425
Object [] arguments = new Object [argumentsLength ];
449
426
@@ -469,12 +446,14 @@ static Object[] copyPArguments(VirtualFrame frame, int newUserArgumentLength) {
469
446
return objects ;
470
447
}
471
448
472
- private GetRefNode ensureReadTargetCellNode () {
473
- if (readTargetCellNode == null ) {
449
+ private ReadIndexedArgumentNode ensureReadCallableNode () {
450
+ if (readCallableNode == null ) {
474
451
CompilerDirectives .transferToInterpreterAndInvalidate ();
475
- readTargetCellNode = insert (GetRefNode .create ());
452
+ // we insert a hidden argument at the end of the positional arguments
453
+ int hiddenArg = getSignature ().getParameterIds ().length ;
454
+ readCallableNode = insert (ReadIndexedArgumentNode .create (hiddenArg ));
476
455
}
477
- return readTargetCellNode ;
456
+ return readCallableNode ;
478
457
}
479
458
480
459
@ Override
@@ -505,7 +484,7 @@ public boolean isPythonInternal() {
505
484
}
506
485
507
486
public static final class MethKeywordsRoot extends MethodDescriptorRoot {
508
- private static final Signature SIGNATURE = new Signature (-1 , true , 1 , false , new String []{"self" }, PythonUtils . EMPTY_STRING_ARRAY );
487
+ private static final Signature SIGNATURE = new Signature (-1 , true , 1 , false , new String []{"self" }, KEYWORDS_HIDDEN_CALLABLE );
509
488
@ Child private PythonObjectFactory factory ;
510
489
@ Child private ReadVarArgsNode readVarargsNode ;
511
490
@ Child private ReadVarKeywordsNode readKwargsNode ;
@@ -553,7 +532,7 @@ private ReleaseNativeWrapperNode ensureReleaseNativeWrapperNode() {
553
532
}
554
533
555
534
public static final class MethVarargsRoot extends MethodDescriptorRoot {
556
- private static final Signature SIGNATURE = new Signature (-1 , false , 1 , false , new String []{"self" }, PythonUtils . EMPTY_STRING_ARRAY );
535
+ private static final Signature SIGNATURE = new Signature (-1 , false , 1 , false , new String []{"self" }, KEYWORDS_HIDDEN_CALLABLE );
557
536
@ Child private PythonObjectFactory factory ;
558
537
@ Child private ReadVarArgsNode readVarargsNode ;
559
538
@ Child private CreateArgsTupleNode createArgsTupleNode ;
@@ -598,7 +577,7 @@ private ReleaseNativeWrapperNode ensureReleaseNativeWrapperNode() {
598
577
}
599
578
600
579
public static final class MethNoargsRoot extends MethodDescriptorRoot {
601
- private static final Signature SIGNATURE = new Signature (-1 , false , -1 , false , new String []{"self" }, PythonUtils . EMPTY_STRING_ARRAY );
580
+ private static final Signature SIGNATURE = new Signature (-1 , false , -1 , false , new String []{"self" }, KEYWORDS_HIDDEN_CALLABLE );
602
581
603
582
public MethNoargsRoot (PythonLanguage language , String name ) {
604
583
super (language , name );
@@ -621,7 +600,7 @@ public Signature getSignature() {
621
600
}
622
601
623
602
public static final class MethORoot extends MethodDescriptorRoot {
624
- private static final Signature SIGNATURE = new Signature (-1 , false , -1 , false , new String []{"self" , "arg" }, PythonUtils . EMPTY_STRING_ARRAY );
603
+ private static final Signature SIGNATURE = new Signature (-1 , false , -1 , false , new String []{"self" , "arg" }, KEYWORDS_HIDDEN_CALLABLE );
625
604
@ Child private ReadIndexedArgumentNode readArgNode ;
626
605
627
606
public MethORoot (PythonLanguage language , String name ) {
@@ -647,7 +626,7 @@ public Signature getSignature() {
647
626
}
648
627
649
628
public static final class MethFastcallWithKeywordsRoot extends MethodDescriptorRoot {
650
- private static final Signature SIGNATURE = new Signature (-1 , true , 1 , false , new String []{"self" }, PythonUtils . EMPTY_STRING_ARRAY );
629
+ private static final Signature SIGNATURE = new Signature (-1 , true , 1 , false , new String []{"self" }, KEYWORDS_HIDDEN_CALLABLE );
651
630
@ Child private PythonObjectFactory factory ;
652
631
@ Child private ReadVarArgsNode readVarargsNode ;
653
632
@ Child private ReadVarKeywordsNode readKwargsNode ;
@@ -685,7 +664,7 @@ public Signature getSignature() {
685
664
}
686
665
687
666
public static final class MethFastcallRoot extends MethodDescriptorRoot {
688
- private static final Signature SIGNATURE = new Signature (-1 , false , 1 , false , new String []{"self" }, PythonUtils . EMPTY_STRING_ARRAY );
667
+ private static final Signature SIGNATURE = new Signature (-1 , false , 1 , false , new String []{"self" }, KEYWORDS_HIDDEN_CALLABLE );
689
668
@ Child private PythonObjectFactory factory ;
690
669
@ Child private ReadVarArgsNode readVarargsNode ;
691
670
@@ -716,7 +695,7 @@ public Signature getSignature() {
716
695
* Wrapper root node for C function type {@code allocfunc} and {@code ssizeargfunc}.
717
696
*/
718
697
static class AllocFuncRootNode extends MethodDescriptorRoot {
719
- private static final Signature SIGNATURE = new Signature (false , -1 , false , new String []{"self" , "nitems" }, PythonUtils . EMPTY_STRING_ARRAY );
698
+ private static final Signature SIGNATURE = new Signature (false , -1 , false , new String []{"self" , "nitems" }, KEYWORDS_HIDDEN_CALLABLE );
720
699
@ Child private ReadIndexedArgumentNode readArgNode ;
721
700
@ Child private ConvertPIntToPrimitiveNode asSsizeTNode ;
722
701
@@ -752,7 +731,7 @@ public Signature getSignature() {
752
731
* Wrapper root node for a get attribute function (C type {@code getattrfunc}).
753
732
*/
754
733
static final class GetAttrFuncRootNode extends MethodDescriptorRoot {
755
- private static final Signature SIGNATURE = new Signature (false , -1 , false , new String []{"self" , "key" }, PythonUtils . EMPTY_STRING_ARRAY );
734
+ private static final Signature SIGNATURE = new Signature (false , -1 , false , new String []{"self" , "key" }, KEYWORDS_HIDDEN_CALLABLE );
756
735
@ Child private ReadIndexedArgumentNode readArgNode ;
757
736
@ Child private CExtNodes .AsCharPointerNode asCharPointerNode ;
758
737
@@ -785,7 +764,7 @@ public Signature getSignature() {
785
764
* Wrapper root node for a set attribute function (C type {@code setattrfunc}).
786
765
*/
787
766
static final class SetAttrFuncRootNode extends MethodDescriptorRoot {
788
- private static final Signature SIGNATURE = new Signature (false , -1 , false , new String []{"self" , "key" , "value" }, PythonUtils . EMPTY_STRING_ARRAY );
767
+ private static final Signature SIGNATURE = new Signature (false , -1 , false , new String []{"self" , "key" , "value" }, KEYWORDS_HIDDEN_CALLABLE );
789
768
@ Child private ReadIndexedArgumentNode readArg1Node ;
790
769
@ Child private ReadIndexedArgumentNode readArg2Node ;
791
770
@ Child private CExtNodes .AsCharPointerNode asCharPointerNode ;
@@ -821,7 +800,7 @@ public Signature getSignature() {
821
800
* Wrapper root node for a rich compare function (C type {@code richcmpfunc}).
822
801
*/
823
802
static final class RichCmpFuncRootNode extends MethodDescriptorRoot {
824
- private static final Signature SIGNATURE = new Signature (false , -1 , false , new String []{"self" , "other" , "op" }, PythonUtils . EMPTY_STRING_ARRAY );
803
+ private static final Signature SIGNATURE = new Signature (false , -1 , false , new String []{"self" , "other" , "op" }, KEYWORDS_HIDDEN_CALLABLE );
825
804
@ Child private ReadIndexedArgumentNode readArg1Node ;
826
805
@ Child private ReadIndexedArgumentNode readArg2Node ;
827
806
@ Child private ConvertPIntToPrimitiveNode asSsizeTNode ;
@@ -860,7 +839,7 @@ public Signature getSignature() {
860
839
* Wrapper root node for C function type {@code ssizeobjargproc}.
861
840
*/
862
841
static final class SSizeObjArgProcRootNode extends MethodDescriptorRoot {
863
- private static final Signature SIGNATURE = new Signature (false , -1 , false , new String []{"self" , "i" , "value" }, PythonUtils . EMPTY_STRING_ARRAY );
842
+ private static final Signature SIGNATURE = new Signature (false , -1 , false , new String []{"self" , "i" , "value" }, KEYWORDS_HIDDEN_CALLABLE );
864
843
@ Child private ReadIndexedArgumentNode readArg1Node ;
865
844
@ Child private ReadIndexedArgumentNode readArg2Node ;
866
845
@ Child private ConvertPIntToPrimitiveNode asSsizeTNode ;
@@ -899,7 +878,7 @@ public Signature getSignature() {
899
878
* Wrapper root node for reverse binary operations.
900
879
*/
901
880
static final class MethReverseRootNode extends MethodDescriptorRoot {
902
- private static final Signature SIGNATURE = new Signature (false , -1 , false , new String []{"self" , "obj" }, PythonUtils . EMPTY_STRING_ARRAY );
881
+ private static final Signature SIGNATURE = new Signature (false , -1 , false , new String []{"self" , "obj" }, KEYWORDS_HIDDEN_CALLABLE );
903
882
@ Child private ReadIndexedArgumentNode readArg0Node ;
904
883
@ Child private ReadIndexedArgumentNode readArg1Node ;
905
884
@@ -939,7 +918,7 @@ public Signature getSignature() {
939
918
* Wrapper root node for native power function (with an optional third argument).
940
919
*/
941
920
static class MethPowRootNode extends MethodDescriptorRoot {
942
- private static final Signature SIGNATURE = new Signature (false , 0 , false , new String []{"args" }, PythonUtils . EMPTY_STRING_ARRAY );
921
+ private static final Signature SIGNATURE = new Signature (false , 0 , false , new String []{"args" }, KEYWORDS_HIDDEN_CALLABLE );
943
922
944
923
@ Child private ReadVarArgsNode readVarargsNode ;
945
924
@@ -998,7 +977,7 @@ Object[] getArguments(Object arg0, Object arg1, Object arg2) {
998
977
* Wrapper root node for native power function (with an optional third argument).
999
978
*/
1000
979
static final class MethRichcmpOpRootNode extends MethodDescriptorRoot {
1001
- private static final Signature SIGNATURE = new Signature (false , -1 , false , new String []{"self" , "other" }, PythonUtils . EMPTY_STRING_ARRAY );
980
+ private static final Signature SIGNATURE = new Signature (false , -1 , false , new String []{"self" , "other" }, KEYWORDS_HIDDEN_CALLABLE );
1002
981
@ Child private ReadIndexedArgumentNode readArgNode ;
1003
982
1004
983
private final int op ;
0 commit comments