|
61 | 61 | import com.oracle.graal.python.builtins.modules.BuiltinFunctions.BinNode;
|
62 | 62 | import com.oracle.graal.python.builtins.modules.BuiltinFunctions.DivModNode;
|
63 | 63 | import com.oracle.graal.python.builtins.modules.BuiltinFunctions.HexNode;
|
| 64 | +import com.oracle.graal.python.builtins.modules.BuiltinFunctions.NextNode; |
64 | 65 | import com.oracle.graal.python.builtins.modules.BuiltinFunctions.OctNode;
|
65 | 66 | import com.oracle.graal.python.builtins.modules.cext.PythonCextBuiltins.NativeBuiltin;
|
66 | 67 | import com.oracle.graal.python.builtins.objects.PNone;
|
|
71 | 72 | import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.ToNewRefNode;
|
72 | 73 | import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.TransformExceptionToNativeNode;
|
73 | 74 | import com.oracle.graal.python.builtins.objects.cext.capi.DynamicObjectNativeWrapper.PrimitiveNativeWrapper;
|
| 75 | +import com.oracle.graal.python.builtins.objects.dict.DictBuiltins.ItemsNode; |
| 76 | +import com.oracle.graal.python.builtins.objects.dict.DictBuiltins.KeysNode; |
| 77 | +import com.oracle.graal.python.builtins.objects.dict.DictBuiltins.ValuesNode; |
| 78 | +import com.oracle.graal.python.builtins.objects.dict.PDict; |
| 79 | +import com.oracle.graal.python.builtins.objects.list.PList; |
74 | 80 | import com.oracle.graal.python.builtins.objects.tuple.PTuple;
|
75 | 81 | import com.oracle.graal.python.lib.PyNumberFloatNode;
|
76 | 82 | import com.oracle.graal.python.lib.PyObjectDelItem;
|
| 83 | +import com.oracle.graal.python.lib.PyObjectGetAttr; |
77 | 84 | import com.oracle.graal.python.lib.PyObjectLookupAttr;
|
78 | 85 | import com.oracle.graal.python.lib.PySequenceCheckNode;
|
79 | 86 | import com.oracle.graal.python.nodes.ErrorMessages;
|
| 87 | +import static com.oracle.graal.python.nodes.SpecialMethodNames.ITEMS; |
| 88 | +import static com.oracle.graal.python.nodes.SpecialMethodNames.KEYS; |
| 89 | +import static com.oracle.graal.python.nodes.SpecialMethodNames.VALUES; |
80 | 90 | import com.oracle.graal.python.nodes.builtins.ListNodes.ConstructListNode;
|
81 | 91 | import com.oracle.graal.python.nodes.call.CallNode;
|
82 | 92 | import com.oracle.graal.python.nodes.expression.BinaryArithmetic;
|
|
97 | 107 | import com.oracle.graal.python.runtime.exception.PException;
|
98 | 108 | import com.oracle.truffle.api.CompilerDirectives;
|
99 | 109 | import com.oracle.truffle.api.dsl.Cached;
|
| 110 | +import com.oracle.truffle.api.dsl.Cached.Shared; |
100 | 111 | import com.oracle.truffle.api.dsl.Fallback;
|
101 | 112 | import com.oracle.truffle.api.dsl.GenerateNodeFactory;
|
102 | 113 | import com.oracle.truffle.api.dsl.NodeFactory;
|
@@ -973,5 +984,164 @@ Object doManaged(VirtualFrame frame, Object listWrapper, Object position,
|
973 | 984 | }
|
974 | 985 | }
|
975 | 986 | }
|
| 987 | + |
| 988 | + /////// PyObject /////// |
| 989 | + |
| 990 | + @Builtin(name = "PyObject_GetItem", minNumOfPositionalArgs = 2) |
| 991 | + @GenerateNodeFactory |
| 992 | + abstract static class PyObjectGetItem extends PythonBinaryBuiltinNode { |
| 993 | + @Specialization |
| 994 | + Object doManaged(VirtualFrame frame, Object listWrapper, Object key, |
| 995 | + @Cached com.oracle.graal.python.lib.PyObjectGetItem getItem, |
| 996 | + @Cached AsPythonObjectNode listWrapperAsPythonObjectNode, |
| 997 | + @Cached AsPythonObjectNode keyAsPythonObjectNode, |
| 998 | + @Cached ToNewRefNode toNewRefNode, |
| 999 | + @Cached GetNativeNullNode getNativeNullNode, |
| 1000 | + @Cached TransformExceptionToNativeNode transformExceptionToNativeNode) { |
| 1001 | + try { |
| 1002 | + Object delegate = listWrapperAsPythonObjectNode.execute(listWrapper); |
| 1003 | + Object item = getItem.execute(frame, delegate, keyAsPythonObjectNode.execute(key)); |
| 1004 | + return toNewRefNode.execute(item); |
| 1005 | + } catch (PException e) { |
| 1006 | + transformExceptionToNativeNode.execute(frame, e); |
| 1007 | + return toNewRefNode.execute(getNativeNullNode.execute()); |
| 1008 | + } |
| 1009 | + } |
| 1010 | + } |
| 1011 | + |
| 1012 | + /////// PyMapping /////// |
| 1013 | + |
| 1014 | + @Builtin(name = "PyMapping_Keys", minNumOfPositionalArgs = 1) |
| 1015 | + @GenerateNodeFactory |
| 1016 | + public abstract static class PyMappingKeysNode extends PythonUnaryBuiltinNode { |
| 1017 | + @Specialization |
| 1018 | + public Object keys(VirtualFrame frame, PDict obj, |
| 1019 | + @Cached KeysNode keysNode, |
| 1020 | + @Cached ConstructListNode listNode, |
| 1021 | + @Cached TransformExceptionToNativeNode transformExceptionToNativeNode, |
| 1022 | + @Shared("nativeNull") @Cached GetNativeNullNode getNativeNullNode) { |
| 1023 | + try { |
| 1024 | + return listNode.execute(frame, keysNode.execute(frame, obj)); |
| 1025 | + } catch (PException e) { |
| 1026 | + transformExceptionToNativeNode.execute(e); |
| 1027 | + return getNativeNullNode.execute(); |
| 1028 | + } |
| 1029 | + } |
| 1030 | + |
| 1031 | + @Specialization(guards = "!isDict(obj)") |
| 1032 | + public Object keys(VirtualFrame frame, Object obj, |
| 1033 | + @Cached PyObjectGetAttr getAttrNode, |
| 1034 | + @Cached CallNode callNode, |
| 1035 | + @Cached ConstructListNode listNode, |
| 1036 | + @Cached TransformExceptionToNativeNode transformExceptionToNativeNode, |
| 1037 | + @Shared("nativeNull") @Cached GetNativeNullNode getNativeNullNode) { |
| 1038 | + try { |
| 1039 | + return getKeys(frame, obj, getAttrNode, callNode, listNode); |
| 1040 | + } catch (PException e) { |
| 1041 | + transformExceptionToNativeNode.execute(e); |
| 1042 | + return getNativeNullNode.execute(); |
| 1043 | + } |
| 1044 | + } |
| 1045 | + |
| 1046 | + } |
976 | 1047 |
|
| 1048 | + private static PList getKeys(VirtualFrame frame, Object obj, PyObjectGetAttr getAttrNode, CallNode callNode, ConstructListNode listNode) { |
| 1049 | + Object attr = getAttrNode.execute(frame, obj, KEYS); |
| 1050 | + return listNode.execute(frame, callNode.execute(frame, attr)); |
| 1051 | + } |
| 1052 | + |
| 1053 | + @Builtin(name = "PyMapping_Items", minNumOfPositionalArgs = 1) |
| 1054 | + @GenerateNodeFactory |
| 1055 | + public abstract static class PyMappingItemsNode extends PythonUnaryBuiltinNode { |
| 1056 | + @Specialization |
| 1057 | + public Object items(VirtualFrame frame, PDict obj, |
| 1058 | + @Cached ItemsNode itemsNode, |
| 1059 | + @Cached ConstructListNode listNode, |
| 1060 | + @Cached TransformExceptionToNativeNode transformExceptionToNativeNode, |
| 1061 | + @Shared("nativeNull") @Cached GetNativeNullNode getNativeNullNode) { |
| 1062 | + try { |
| 1063 | + return listNode.execute(frame, itemsNode.execute(frame, obj)); |
| 1064 | + } catch (PException e) { |
| 1065 | + transformExceptionToNativeNode.execute(e); |
| 1066 | + return getNativeNullNode.execute(); |
| 1067 | + } |
| 1068 | + } |
| 1069 | + |
| 1070 | + @Specialization(guards = "!isDict(obj)") |
| 1071 | + public Object items(VirtualFrame frame, Object obj, |
| 1072 | + @Cached PyObjectGetAttr getAttrNode, |
| 1073 | + @Cached CallNode callNode, |
| 1074 | + @Cached ConstructListNode listNode, |
| 1075 | + @Cached TransformExceptionToNativeNode transformExceptionToNativeNode, |
| 1076 | + @Shared("nativeNull") @Cached GetNativeNullNode getNativeNullNode) { |
| 1077 | + try { |
| 1078 | + Object attr = getAttrNode.execute(frame, obj, ITEMS); |
| 1079 | + return listNode.execute(frame, callNode.execute(frame, attr)); |
| 1080 | + } catch (PException e) { |
| 1081 | + transformExceptionToNativeNode.execute(e); |
| 1082 | + return getNativeNullNode.execute(); |
| 1083 | + } |
| 1084 | + } |
| 1085 | + } |
| 1086 | + |
| 1087 | + @Builtin(name = "PyMapping_Values", minNumOfPositionalArgs = 1) |
| 1088 | + @GenerateNodeFactory |
| 1089 | + public abstract static class PyMappingValuesNode extends PythonUnaryBuiltinNode { |
| 1090 | + @Specialization |
| 1091 | + public Object values(VirtualFrame frame, PDict obj, |
| 1092 | + @Cached ConstructListNode listNode, |
| 1093 | + @Cached ValuesNode valuesNode, |
| 1094 | + @Cached TransformExceptionToNativeNode transformExceptionToNativeNode, |
| 1095 | + @Shared("nativeNull") @Cached GetNativeNullNode getNativeNullNode) { |
| 1096 | + try { |
| 1097 | + return listNode.execute(frame, valuesNode.execute(frame, obj)); |
| 1098 | + } catch (PException e) { |
| 1099 | + transformExceptionToNativeNode.execute(e); |
| 1100 | + return getNativeNullNode.execute(); |
| 1101 | + } |
| 1102 | + } |
| 1103 | + |
| 1104 | + @Specialization(guards = "!isDict(obj)") |
| 1105 | + public Object values(VirtualFrame frame, Object obj, |
| 1106 | + @Cached PyObjectGetAttr getAttrNode, |
| 1107 | + @Cached CallNode callNode, |
| 1108 | + @Cached ConstructListNode listNode, |
| 1109 | + @Cached TransformExceptionToNativeNode transformExceptionToNativeNode, |
| 1110 | + @Shared("nativeNull") @Cached GetNativeNullNode getNativeNullNode) { |
| 1111 | + try { |
| 1112 | + Object attr = getAttrNode.execute(frame, obj, VALUES); |
| 1113 | + return listNode.execute(frame, callNode.execute(frame, attr)); |
| 1114 | + } catch (PException e) { |
| 1115 | + transformExceptionToNativeNode.execute(e); |
| 1116 | + return getNativeNullNode.execute(); |
| 1117 | + } |
| 1118 | + } |
| 1119 | + } |
| 1120 | + |
| 1121 | + /////// PyIter /////// |
| 1122 | + |
| 1123 | +//@may_raise |
| 1124 | +//def PyIter_Next(itObj): |
| 1125 | +// try: |
| 1126 | +// return next(itObj) |
| 1127 | +// except StopIteration: |
| 1128 | +// PyErr_Restore(None, None, None) |
| 1129 | +// return native_null |
| 1130 | + |
| 1131 | + @Builtin(name = "PyIter_Next", minNumOfPositionalArgs = 1) |
| 1132 | + @GenerateNodeFactory |
| 1133 | + abstract static class PyIterNextCheck extends PythonUnaryBuiltinNode { |
| 1134 | + @Specialization |
| 1135 | + static Object check(VirtualFrame frame, Object object, |
| 1136 | + @Cached NextNode nextNode, |
| 1137 | + @Cached TransformExceptionToNativeNode transformExceptionToNativeNode, |
| 1138 | + @Cached GetNativeNullNode getNativeNullNode) { |
| 1139 | + try { |
| 1140 | + return nextNode.execute(frame, object, PNone.NO_VALUE); |
| 1141 | + } catch (PException e) { |
| 1142 | + transformExceptionToNativeNode.execute(e); |
| 1143 | + return getNativeNullNode.execute(); |
| 1144 | + } |
| 1145 | + } |
| 1146 | + } |
977 | 1147 | }
|
0 commit comments