Skip to content

Commit 0f7e1a8

Browse files
fangerercosminbasca
authored andcommitted
Support custom result value conversion for external function wrappers.
1 parent 25463d8 commit 0f7e1a8

File tree

4 files changed

+326
-247
lines changed

4 files changed

+326
-247
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/ExternalFunctionNodes.java

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
import com.oracle.graal.python.builtins.modules.ExternalFunctionNodesFactory.MaterializePrimitiveNodeGen;
4747
import com.oracle.graal.python.builtins.modules.ExternalFunctionNodesFactory.ReleaseNativeWrapperNodeGen;
4848
import com.oracle.graal.python.builtins.modules.PythonCextBuiltins.CheckFunctionResultNode;
49+
import com.oracle.graal.python.builtins.modules.PythonCextBuiltins.PExternalFunctionWrapper;
50+
import com.oracle.graal.python.builtins.modules.PythonCextBuiltinsFactory.DefaultCheckFunctionResultNodeGen;
4951
import com.oracle.graal.python.builtins.objects.PNone;
5052
import com.oracle.graal.python.builtins.objects.cext.CApiGuards;
5153
import com.oracle.graal.python.builtins.objects.cext.CExtNodes;
@@ -192,7 +194,7 @@ public static MethDirectRoot create(PythonLanguage lang, String name, Object cal
192194
*/
193195
static final class ExternalFunctionInvokeNode extends PNodeWithContext implements IndirectCallNode {
194196
@Child private CExtNodes.ConvertArgsToSulongNode toSulongNode;
195-
@Child private CheckFunctionResultNode checkResultNode = CheckFunctionResultNode.create();
197+
@Child private CheckFunctionResultNode checkResultNode;
196198
@Child private PForeignToPTypeNode fromForeign = PForeignToPTypeNode.create();
197199
@Child private ToJavaStealingNode asPythonObjectNode = ToJavaStealingNodeGen.create();
198200
@Child private InteropLibrary lib;
@@ -220,12 +222,17 @@ public Node copy() {
220222
return node;
221223
}
222224

225+
@TruffleBoundary
223226
ExternalFunctionInvokeNode() {
224227
this.toSulongNode = CExtNodes.AllToSulongNode.create();
225228
}
226229

227-
ExternalFunctionInvokeNode(ConvertArgsToSulongNode convertArgsNode) {
230+
@TruffleBoundary
231+
ExternalFunctionInvokeNode(PExternalFunctionWrapper provider) {
232+
ConvertArgsToSulongNode convertArgsNode = provider.createConvertArgsToSulongNode();
228233
this.toSulongNode = convertArgsNode != null ? convertArgsNode : CExtNodes.AllToSulongNode.create();
234+
CheckFunctionResultNode checkFunctionResultNode = provider.getCheckFunctionResultNode();
235+
this.checkResultNode = checkFunctionResultNode != null ? checkFunctionResultNode : DefaultCheckFunctionResultNodeGen.create();
229236
}
230237

231238
public Object execute(VirtualFrame frame, String name, Object callable, Object[] frameArgs, int argsOffset) {
@@ -283,8 +290,8 @@ public static ExternalFunctionInvokeNode create() {
283290
return new ExternalFunctionInvokeNode();
284291
}
285292

286-
public static ExternalFunctionInvokeNode create(ConvertArgsToSulongNode convertArgsNode) {
287-
return new ExternalFunctionInvokeNode(convertArgsNode);
293+
public static ExternalFunctionInvokeNode create(PExternalFunctionWrapper provider) {
294+
return new ExternalFunctionInvokeNode(provider);
288295
}
289296
}
290297

@@ -369,12 +376,12 @@ abstract static class MethodDescriptorRoot extends PRootNode {
369376
}
370377

371378
@TruffleBoundary
372-
MethodDescriptorRoot(PythonLanguage language, String name, Object callable, ConvertArgsToSulongNode convertArgsToSulongNode) {
379+
MethodDescriptorRoot(PythonLanguage language, String name, Object callable, PExternalFunctionWrapper provider) {
373380
super(language);
374381
this.name = name;
375382
this.callable = callable;
376-
if (convertArgsToSulongNode != null) {
377-
this.externalInvokeNode = ExternalFunctionInvokeNode.create(convertArgsToSulongNode);
383+
if (provider != null) {
384+
this.externalInvokeNode = ExternalFunctionInvokeNode.create(provider);
378385
} else {
379386
this.invokeNode = CallVarargsMethodNode.create();
380387
}
@@ -476,8 +483,8 @@ public MethKeywordsRoot(PythonLanguage language, String name, Object callable) {
476483
super(language, name, callable);
477484
}
478485

479-
public MethKeywordsRoot(PythonLanguage language, String name, Object callable, ConvertArgsToSulongNode convertArgsToSulongNode) {
480-
super(language, name, callable, convertArgsToSulongNode);
486+
public MethKeywordsRoot(PythonLanguage language, String name, Object callable, PExternalFunctionWrapper provider) {
487+
super(language, name, callable, provider);
481488
this.factory = PythonObjectFactory.create();
482489
this.readVarargsNode = ReadVarArgsNode.create(1, true);
483490
this.readKwargsNode = ReadVarKeywordsNode.create(new String[0]);
@@ -523,8 +530,8 @@ public MethVarargsRoot(PythonLanguage language, String name, Object callable) {
523530
super(language, name, callable);
524531
}
525532

526-
public MethVarargsRoot(PythonLanguage language, String name, Object callable, ConvertArgsToSulongNode convertArgsToSulongNode) {
527-
super(language, name, callable, convertArgsToSulongNode);
533+
public MethVarargsRoot(PythonLanguage language, String name, Object callable, PExternalFunctionWrapper provider) {
534+
super(language, name, callable, provider);
528535
this.factory = PythonObjectFactory.create();
529536
this.readVarargsNode = ReadVarArgsNode.create(1, true);
530537
this.createArgsTupleNode = CreateArgsTupleNodeGen.create();
@@ -564,8 +571,8 @@ public MethNoargsRoot(PythonLanguage language, String name, Object callable) {
564571
super(language, name, callable);
565572
}
566573

567-
public MethNoargsRoot(PythonLanguage language, String name, Object callable, ConvertArgsToSulongNode convertArgsToSulongNode) {
568-
super(language, name, callable, convertArgsToSulongNode);
574+
public MethNoargsRoot(PythonLanguage language, String name, Object callable, PExternalFunctionWrapper provider) {
575+
super(language, name, callable, provider);
569576
}
570577

571578
@Override
@@ -588,8 +595,8 @@ public MethORoot(PythonLanguage language, String name, Object callable) {
588595
super(language, name, callable);
589596
}
590597

591-
public MethORoot(PythonLanguage language, String name, Object callable, ConvertArgsToSulongNode convertArgsToSulongNode) {
592-
super(language, name, callable, convertArgsToSulongNode);
598+
public MethORoot(PythonLanguage language, String name, Object callable, PExternalFunctionWrapper provider) {
599+
super(language, name, callable, provider);
593600
this.readArgNode = ReadIndexedArgumentNode.create(1);
594601
}
595602

@@ -616,8 +623,8 @@ public MethFastcallWithKeywordsRoot(PythonLanguage language, String name, Object
616623
super(language, name, callable);
617624
}
618625

619-
public MethFastcallWithKeywordsRoot(PythonLanguage language, String name, Object callable, ConvertArgsToSulongNode convertArgsToSulongNode) {
620-
super(language, name, callable, convertArgsToSulongNode);
626+
public MethFastcallWithKeywordsRoot(PythonLanguage language, String name, Object callable, PExternalFunctionWrapper provider) {
627+
super(language, name, callable, provider);
621628
this.factory = PythonObjectFactory.create();
622629
this.readVarargsNode = ReadVarArgsNode.create(1, true);
623630
this.readKwargsNode = ReadVarKeywordsNode.create(new String[0]);
@@ -653,8 +660,8 @@ public MethFastcallRoot(PythonLanguage language, String name, Object callable) {
653660
super(language, name, callable);
654661
}
655662

656-
public MethFastcallRoot(PythonLanguage language, String name, Object callable, ConvertArgsToSulongNode convertArgsToSulongNode) {
657-
super(language, name, callable, convertArgsToSulongNode);
663+
public MethFastcallRoot(PythonLanguage language, String name, Object callable, PExternalFunctionWrapper provider) {
664+
super(language, name, callable, provider);
658665
this.factory = PythonObjectFactory.create();
659666
this.readVarargsNode = ReadVarArgsNode.create(1, true);
660667
}
@@ -684,8 +691,8 @@ static class AllocFuncRootNode extends MethodDescriptorRoot {
684691
super(language, name, callable);
685692
}
686693

687-
AllocFuncRootNode(PythonLanguage language, String name, Object callable, ConvertArgsToSulongNode convertArgsToSulongNode) {
688-
super(language, name, callable, convertArgsToSulongNode);
694+
AllocFuncRootNode(PythonLanguage language, String name, Object callable, PExternalFunctionWrapper provider) {
695+
super(language, name, callable, provider);
689696
this.readArgNode = ReadIndexedArgumentNode.create(1);
690697
this.asSsizeTNode = ConvertPIntToPrimitiveNodeGen.create();
691698
}
@@ -720,8 +727,8 @@ static final class GetAttrFuncRootNode extends MethodDescriptorRoot {
720727
super(language, name, callable);
721728
}
722729

723-
GetAttrFuncRootNode(PythonLanguage language, String name, Object callable, ConvertArgsToSulongNode convertArgsToSulongNode) {
724-
super(language, name, callable, convertArgsToSulongNode);
730+
GetAttrFuncRootNode(PythonLanguage language, String name, Object callable, PExternalFunctionWrapper provider) {
731+
super(language, name, callable, provider);
725732
this.readArgNode = ReadIndexedArgumentNode.create(1);
726733
this.asCharPointerNode = CExtNodes.AsCharPointerNode.create();
727734
}
@@ -754,8 +761,8 @@ static final class SetAttrFuncRootNode extends MethodDescriptorRoot {
754761
super(language, name, callable);
755762
}
756763

757-
SetAttrFuncRootNode(PythonLanguage language, String name, Object callable, ConvertArgsToSulongNode convertArgsToSulongNode) {
758-
super(language, name, callable, convertArgsToSulongNode);
764+
SetAttrFuncRootNode(PythonLanguage language, String name, Object callable, PExternalFunctionWrapper provider) {
765+
super(language, name, callable, provider);
759766
this.readArg1Node = ReadIndexedArgumentNode.create(1);
760767
this.readArg2Node = ReadIndexedArgumentNode.create(2);
761768
this.asCharPointerNode = CExtNodes.AsCharPointerNode.create();
@@ -790,8 +797,8 @@ static final class RichCmpFuncRootNode extends MethodDescriptorRoot {
790797
super(language, name, callable);
791798
}
792799

793-
RichCmpFuncRootNode(PythonLanguage language, String name, Object callable, ConvertArgsToSulongNode convertArgsToSulongNode) {
794-
super(language, name, callable, convertArgsToSulongNode);
800+
RichCmpFuncRootNode(PythonLanguage language, String name, Object callable, PExternalFunctionWrapper provider) {
801+
super(language, name, callable, provider);
795802
this.readArg1Node = ReadIndexedArgumentNode.create(1);
796803
this.readArg2Node = ReadIndexedArgumentNode.create(2);
797804
this.asSsizeTNode = ConvertPIntToPrimitiveNodeGen.create();
@@ -829,8 +836,8 @@ static final class SSizeObjArgProcRootNode extends MethodDescriptorRoot {
829836
super(language, name, callable);
830837
}
831838

832-
SSizeObjArgProcRootNode(PythonLanguage language, String name, Object callable, ConvertArgsToSulongNode convertArgsToSulongNode) {
833-
super(language, name, callable, convertArgsToSulongNode);
839+
SSizeObjArgProcRootNode(PythonLanguage language, String name, Object callable, PExternalFunctionWrapper provider) {
840+
super(language, name, callable, provider);
834841
this.readArg1Node = ReadIndexedArgumentNode.create(1);
835842
this.readArg2Node = ReadIndexedArgumentNode.create(2);
836843
this.asSsizeTNode = ConvertPIntToPrimitiveNodeGen.create();
@@ -869,8 +876,8 @@ static final class MethReverseRootNode extends MethodDescriptorRoot {
869876
this.readArg1Node = ReadIndexedArgumentNode.create(1);
870877
}
871878

872-
MethReverseRootNode(PythonLanguage language, String name, Object callable, ConvertArgsToSulongNode convertArgsToSulongNode) {
873-
super(language, name, callable, convertArgsToSulongNode);
879+
MethReverseRootNode(PythonLanguage language, String name, Object callable, PExternalFunctionWrapper provider) {
880+
super(language, name, callable, provider);
874881
this.readArg0Node = ReadIndexedArgumentNode.create(0);
875882
this.readArg1Node = ReadIndexedArgumentNode.create(1);
876883
}
@@ -910,8 +917,8 @@ static class MethPowRootNode extends MethodDescriptorRoot {
910917
this.profile = null;
911918
}
912919

913-
MethPowRootNode(PythonLanguage language, String name, Object callable, ConvertArgsToSulongNode convertArgsToSulongNode) {
914-
super(language, name, callable, convertArgsToSulongNode);
920+
MethPowRootNode(PythonLanguage language, String name, Object callable, PExternalFunctionWrapper provider) {
921+
super(language, name, callable, provider);
915922
this.readVarargsNode = ReadVarArgsNode.create(1, true);
916923
this.profile = ConditionProfile.createBinaryProfile();
917924
}
@@ -944,8 +951,8 @@ static final class MethRPowRootNode extends MethPowRootNode {
944951
super(language, name, callable);
945952
}
946953

947-
MethRPowRootNode(PythonLanguage language, String name, Object callable, ConvertArgsToSulongNode convertArgsToSulongNode) {
948-
super(language, name, callable, convertArgsToSulongNode);
954+
MethRPowRootNode(PythonLanguage language, String name, Object callable, PExternalFunctionWrapper provider) {
955+
super(language, name, callable, provider);
949956
}
950957

951958
@Override
@@ -969,8 +976,8 @@ static final class MethRichcmpOpRootNode extends MethodDescriptorRoot {
969976
this.op = op;
970977
}
971978

972-
MethRichcmpOpRootNode(PythonLanguage language, String name, Object callable, ConvertArgsToSulongNode convertArgsToSulongNode, int op) {
973-
super(language, name, callable, convertArgsToSulongNode);
979+
MethRichcmpOpRootNode(PythonLanguage language, String name, Object callable, PExternalFunctionWrapper provider, int op) {
980+
super(language, name, callable, provider);
974981
this.readArgNode = ReadIndexedArgumentNode.create(1);
975982
this.op = op;
976983
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/ImpModuleBuiltins.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import com.oracle.graal.python.builtins.CoreFunctions;
5757
import com.oracle.graal.python.builtins.PythonBuiltins;
5858
import com.oracle.graal.python.builtins.modules.PythonCextBuiltins.CheckFunctionResultNode;
59+
import com.oracle.graal.python.builtins.modules.PythonCextBuiltinsFactory.DefaultCheckFunctionResultNodeGen;
5960
import com.oracle.graal.python.builtins.objects.PNone;
6061
import com.oracle.graal.python.builtins.objects.PythonAbstractObjectFactory.PInteropGetAttributeNodeGen;
6162
import com.oracle.graal.python.builtins.objects.bytes.PBytes;
@@ -419,7 +420,7 @@ private SetItemNode getSetItemNode() {
419420
private CheckFunctionResultNode getCheckResultNode() {
420421
if (checkResultNode == null) {
421422
CompilerDirectives.transferToInterpreterAndInvalidate();
422-
checkResultNode = insert(CheckFunctionResultNode.create());
423+
checkResultNode = insert(DefaultCheckFunctionResultNodeGen.create());
423424
}
424425
return checkResultNode;
425426
}

0 commit comments

Comments
 (0)