|
81 | 81 | import com.oracle.graal.python.builtins.modules.SysModuleBuiltins;
|
82 | 82 | import com.oracle.graal.python.builtins.objects.PNone;
|
83 | 83 | import com.oracle.graal.python.builtins.objects.PNotImplemented;
|
| 84 | +import com.oracle.graal.python.builtins.objects.cext.PythonAbstractNativeObject; |
| 85 | +import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.PCallCapiFunction; |
| 86 | +import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.ToSulongNode; |
| 87 | +import com.oracle.graal.python.builtins.objects.cext.capi.NativeCAPISymbol; |
84 | 88 | import com.oracle.graal.python.builtins.objects.common.FormatNodeBase;
|
85 | 89 | import com.oracle.graal.python.builtins.objects.complex.ComplexBuiltinsClinicProviders.FormatNodeClinicProviderGen;
|
86 | 90 | import com.oracle.graal.python.builtins.objects.floats.FloatBuiltins;
|
|
102 | 106 | import com.oracle.graal.python.runtime.formatting.InternalFormat;
|
103 | 107 | import com.oracle.graal.python.runtime.formatting.InternalFormat.Spec;
|
104 | 108 | import com.oracle.graal.python.runtime.object.PythonObjectFactory;
|
| 109 | +import com.oracle.truffle.api.CompilerDirectives; |
105 | 110 | import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
|
106 | 111 | import com.oracle.truffle.api.dsl.Bind;
|
107 | 112 | import com.oracle.truffle.api.dsl.Cached;
|
|
112 | 117 | import com.oracle.truffle.api.dsl.Specialization;
|
113 | 118 | import com.oracle.truffle.api.dsl.TypeSystemReference;
|
114 | 119 | import com.oracle.truffle.api.frame.VirtualFrame;
|
| 120 | +import com.oracle.truffle.api.interop.InteropLibrary; |
| 121 | +import com.oracle.truffle.api.interop.UnsupportedMessageException; |
115 | 122 | import com.oracle.truffle.api.nodes.Node;
|
116 | 123 | import com.oracle.truffle.api.profiles.InlinedConditionProfile;
|
117 | 124 | import com.oracle.truffle.api.strings.TruffleString;
|
@@ -798,12 +805,43 @@ static double get(PComplex self) {
|
798 | 805 | @Builtin(name = J___HASH__, minNumOfPositionalArgs = 1)
|
799 | 806 | abstract static class HashNode extends PythonUnaryBuiltinNode {
|
800 | 807 | @Specialization
|
801 |
| - static long hash(PComplex self) { |
| 808 | + static long doPComplex(PComplex self) { |
| 809 | + return complexHash(self.getReal(), self.getImag()); |
| 810 | + } |
| 811 | + |
| 812 | + @Specialization |
| 813 | + static long doNative(PythonAbstractNativeObject self, |
| 814 | + @Cached ToSulongNode toSulongNode, |
| 815 | + @Cached PCallCapiFunction callGetRealNode, |
| 816 | + @Cached PCallCapiFunction callGetImagNode) { |
| 817 | + Object ptr = toSulongNode.execute(self); |
| 818 | + Object realObj = callGetRealNode.call(NativeCAPISymbol.FUN_GET_PY_COMPLEX_CVAL_REAL, ptr); |
| 819 | + Object imagObj = callGetImagNode.call(NativeCAPISymbol.FUN_GET_PY_COMPLEX_CVAL_IMAG, ptr); |
| 820 | + return complexHash(expectDouble(realObj), expectDouble(imagObj)); |
| 821 | + } |
| 822 | + |
| 823 | + private static long complexHash(double real, double imag) { |
802 | 824 | // just like CPython
|
803 |
| - long realHash = PyObjectHashNode.hash(self.getReal()); |
804 |
| - long imagHash = PyObjectHashNode.hash(self.getImag()); |
| 825 | + long realHash = PyObjectHashNode.hash(real); |
| 826 | + long imagHash = PyObjectHashNode.hash(imag); |
805 | 827 | return realHash + SysModuleBuiltins.HASH_IMAG * imagHash;
|
806 | 828 | }
|
| 829 | + |
| 830 | + private static double expectDouble(Object val) { |
| 831 | + if (val instanceof Double) { |
| 832 | + return (double) val; |
| 833 | + } |
| 834 | + InteropLibrary lib = InteropLibrary.getUncached(val); |
| 835 | + if (lib.fitsInDouble(val)) { |
| 836 | + try { |
| 837 | + return lib.asDouble(val); |
| 838 | + } catch (UnsupportedMessageException e) { |
| 839 | + // fall through |
| 840 | + } |
| 841 | + } |
| 842 | + throw CompilerDirectives.shouldNotReachHere(); |
| 843 | + } |
| 844 | + |
807 | 845 | }
|
808 | 846 |
|
809 | 847 | @GenerateNodeFactory
|
|
0 commit comments