|
82 | 82 | import com.oracle.graal.python.builtins.objects.cext.CExtNodesFactory.MayRaiseTernaryNodeGen;
|
83 | 83 | import com.oracle.graal.python.builtins.objects.cext.CExtNodesFactory.MayRaiseUnaryNodeGen;
|
84 | 84 | import com.oracle.graal.python.builtins.objects.cext.HandleCache;
|
| 85 | +import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PrimitiveNativeWrapper; |
85 | 86 | import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PySequenceArrayWrapper;
|
86 | 87 | import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PythonClassInitNativeWrapper;
|
87 | 88 | import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PythonClassNativeWrapper;
|
@@ -773,6 +774,17 @@ abstract static class NativeBuiltin extends PythonBuiltinNode {
|
773 | 774 | @Child private Node isBoxedNode;
|
774 | 775 | @Child private Node unboxNode;
|
775 | 776 | @Child private GetByteArrayNode getByteArrayNode;
|
| 777 | + @Child private ReadAttributeFromObjectNode readNativeNull; |
| 778 | + |
| 779 | + protected Object getNativeNull(Object module) { |
| 780 | + if (readNativeNull == null) { |
| 781 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 782 | + readNativeNull = insert(ReadAttributeFromObjectNode.create()); |
| 783 | + } |
| 784 | + Object wrapper = readNativeNull.execute(module, NATIVE_NULL); |
| 785 | + assert wrapper instanceof PythonNativeNull; |
| 786 | + return wrapper; |
| 787 | + } |
776 | 788 |
|
777 | 789 | protected void transformToNative(PException p) {
|
778 | 790 | NativeBuiltin.transformToNative(getContext(), p);
|
@@ -1928,8 +1940,8 @@ abstract static class AsDouble extends PythonBuiltinNode {
|
1928 | 1940 | abstract static class PyTruffle_Register_NULL extends PythonUnaryBuiltinNode {
|
1929 | 1941 | @Specialization
|
1930 | 1942 | Object doIt(Object object,
|
1931 |
| - @Cached("create()") ReadAttributeFromObjectNode writeAttrNode) { |
1932 |
| - Object wrapper = writeAttrNode.execute(getCore().lookupBuiltinModule("python_cext"), NATIVE_NULL); |
| 1943 | + @Cached("create()") ReadAttributeFromObjectNode readAttrNode) { |
| 1944 | + Object wrapper = readAttrNode.execute(getCore().lookupBuiltinModule("python_cext"), NATIVE_NULL); |
1933 | 1945 | if (wrapper instanceof PythonNativeNull) {
|
1934 | 1946 | ((PythonNativeNull) wrapper).setPtr(object);
|
1935 | 1947 | }
|
@@ -2137,4 +2149,92 @@ PBytes doGeneric(PythonNativeObject object) {
|
2137 | 2149 | throw raise(TypeError, "invalid pointer: %s", object.object);
|
2138 | 2150 | }
|
2139 | 2151 | }
|
| 2152 | + |
| 2153 | + @Builtin(name = "PyFloat_AsDouble", fixedNumOfPositionalArgs = 1) |
| 2154 | + @GenerateNodeFactory |
| 2155 | + abstract static class PyFloat_AsDouble extends NativeBuiltin { |
| 2156 | + |
| 2157 | + @Child private CExtNodes.AsPythonObjectNode asPythonObjectNode; |
| 2158 | + @Child private CExtNodes.AsDouble asDoubleNode; |
| 2159 | + |
| 2160 | + @Specialization(guards = "!object.isDouble()") |
| 2161 | + double doLongNativeWrapper(PrimitiveNativeWrapper object) { |
| 2162 | + return object.getLong(); |
| 2163 | + } |
| 2164 | + |
| 2165 | + @Specialization(guards = "object.isDouble()") |
| 2166 | + double doDoubleNativeWrapper(PrimitiveNativeWrapper object) { |
| 2167 | + return object.getDouble(); |
| 2168 | + } |
| 2169 | + |
| 2170 | + @Specialization(rewriteOn = PException.class) |
| 2171 | + double doGeneric(Object object) { |
| 2172 | + if (asPythonObjectNode == null) { |
| 2173 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 2174 | + asPythonObjectNode = insert(CExtNodes.AsPythonObjectNode.create()); |
| 2175 | + } |
| 2176 | + if (asDoubleNode == null) { |
| 2177 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 2178 | + asDoubleNode = insert(CExtNodes.AsDouble.create()); |
| 2179 | + } |
| 2180 | + return asDoubleNode.execute(asPythonObjectNode.execute(object)); |
| 2181 | + } |
| 2182 | + |
| 2183 | + @Specialization(replaces = "doGeneric") |
| 2184 | + double doGenericErr(Object object) { |
| 2185 | + try { |
| 2186 | + return doGeneric(object); |
| 2187 | + } catch (PException e) { |
| 2188 | + transformToNative(e); |
| 2189 | + return -1.0; |
| 2190 | + } |
| 2191 | + } |
| 2192 | + } |
| 2193 | + |
| 2194 | + @Builtin(name = "PyNumber_Float", fixedNumOfPositionalArgs = 2, declaresExplicitSelf = true) |
| 2195 | + @GenerateNodeFactory |
| 2196 | + abstract static class PyNumber_Float extends NativeBuiltin { |
| 2197 | + |
| 2198 | + @Child private CExtNodes.AsPythonObjectNode asPythonObjectNode; |
| 2199 | + @Child private CExtNodes.ToSulongNode toSulongNode; |
| 2200 | + @Child private BuiltinConstructors.FloatNode floatNode; |
| 2201 | + |
| 2202 | + @Specialization(guards = "object.isDouble()") |
| 2203 | + Object doDoubleNativeWrapper(@SuppressWarnings("unused") Object module, PrimitiveNativeWrapper object) { |
| 2204 | + return object; |
| 2205 | + } |
| 2206 | + |
| 2207 | + @Specialization(guards = "!object.isDouble()") |
| 2208 | + Object doLongNativeWrapper(@SuppressWarnings("unused") Object module, PrimitiveNativeWrapper object, |
| 2209 | + @Cached("create()") CExtNodes.ToSulongNode primitiveToSulongNode) { |
| 2210 | + return primitiveToSulongNode.execute((double) object.getLong()); |
| 2211 | + } |
| 2212 | + |
| 2213 | + @Specialization(rewriteOn = PException.class) |
| 2214 | + Object doGeneric(@SuppressWarnings("unused") Object module, Object object) { |
| 2215 | + if (asPythonObjectNode == null) { |
| 2216 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 2217 | + asPythonObjectNode = insert(CExtNodes.AsPythonObjectNode.create()); |
| 2218 | + } |
| 2219 | + if (floatNode == null) { |
| 2220 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 2221 | + floatNode = insert(BuiltinConstructorsFactory.FloatNodeFactory.create(null)); |
| 2222 | + } |
| 2223 | + if (toSulongNode == null) { |
| 2224 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 2225 | + toSulongNode = insert(CExtNodes.ToSulongNode.create()); |
| 2226 | + } |
| 2227 | + return toSulongNode.execute(floatNode.executeWith(PythonBuiltinClassType.PFloat, asPythonObjectNode.execute(object))); |
| 2228 | + } |
| 2229 | + |
| 2230 | + @Specialization(replaces = "doGeneric") |
| 2231 | + Object doGenericErr(Object module, Object object) { |
| 2232 | + try { |
| 2233 | + return doGeneric(module, object); |
| 2234 | + } catch (PException e) { |
| 2235 | + transformToNative(e); |
| 2236 | + return getNativeNull(module); |
| 2237 | + } |
| 2238 | + } |
| 2239 | + } |
2140 | 2240 | }
|
0 commit comments