Skip to content

Commit e8070f3

Browse files
committed
Refactor FromNativeSubclassNode; use it in PyFloatAsDoubleNode
1 parent bef234d commit e8070f3

File tree

5 files changed

+202
-121
lines changed

5 files changed

+202
-121
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/CExtNodes.java

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@
159159
import com.oracle.graal.python.nodes.classes.IsSubtypeNode;
160160
import com.oracle.graal.python.nodes.frame.GetCurrentFrameRef;
161161
import com.oracle.graal.python.nodes.object.GetClassNode;
162+
import com.oracle.graal.python.nodes.object.InlinedGetClassNode;
163+
import com.oracle.graal.python.nodes.object.InlinedGetClassNode.GetPythonObjectClassNode;
162164
import com.oracle.graal.python.nodes.object.IsForeignObjectNode;
163165
import com.oracle.graal.python.nodes.truffle.PythonTypes;
164166
import com.oracle.graal.python.nodes.util.CannotCastException;
@@ -179,6 +181,7 @@
179181
import com.oracle.truffle.api.CompilerDirectives;
180182
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
181183
import com.oracle.truffle.api.TruffleLogger;
184+
import com.oracle.truffle.api.dsl.Bind;
182185
import com.oracle.truffle.api.dsl.Cached;
183186
import com.oracle.truffle.api.dsl.Cached.Exclusive;
184187
import com.oracle.truffle.api.dsl.Cached.Shared;
@@ -214,14 +217,14 @@ public abstract class CExtNodes {
214217
private static final String J_SUBTYPE_NEW = "_subtype_new";
215218

216219
@GenerateUncached
217-
abstract static class ImportCAPISymbolNode extends PNodeWithContext {
220+
public abstract static class ImportCAPISymbolNode extends PNodeWithContext {
218221

219222
public abstract Object execute(NativeCAPISymbol symbol);
220223

221224
@Specialization
222-
Object doGeneric(NativeCAPISymbol name,
225+
static Object doGeneric(NativeCAPISymbol name,
223226
@Cached ImportCExtSymbolNode importCExtSymbolNode) {
224-
return importCExtSymbolNode.execute(PythonContext.get(this).getCApiContext(), name);
227+
return importCExtSymbolNode.execute(PythonContext.get(importCExtSymbolNode).getCApiContext(), name);
225228
}
226229
}
227230

@@ -339,29 +342,47 @@ public static StringSubtypeNew create() {
339342
// -----------------------------------------------------------------------------------------------------------------
340343
public abstract static class FromNativeSubclassNode extends Node {
341344

342-
public abstract Double execute(VirtualFrame frame, PythonNativeObject object);
345+
public abstract Double execute(VirtualFrame frame, PythonAbstractNativeObject object);
343346

344347
@Specialization
345-
@SuppressWarnings("unchecked")
346-
public Double doDouble(VirtualFrame frame, PythonNativeObject object,
347-
@Exclusive @Cached GetClassNode getClass,
348-
@Exclusive @Cached IsSubtypeNode isSubtype,
348+
static Double doDouble(VirtualFrame frame, PythonAbstractNativeObject object,
349+
@Bind("this") Node inliningTarget,
350+
@Cached GetPythonObjectClassNode getClass,
351+
@Cached IsSubtypeNode isSubtype,
349352
@Exclusive @Cached ToSulongNode toSulongNode,
350353
@CachedLibrary(limit = "1") InteropLibrary interopLibrary,
351354
@Exclusive @Cached ImportCAPISymbolNode importCAPISymbolNode) {
352-
if (isFloatSubtype(frame, object, getClass, isSubtype)) {
353-
try {
354-
return (Double) interopLibrary.execute(importCAPISymbolNode.execute(FUN_PY_FLOAT_AS_DOUBLE), toSulongNode.execute(object));
355-
} catch (UnsupportedMessageException | UnsupportedTypeException | ArityException e) {
356-
CompilerDirectives.transferToInterpreterAndInvalidate();
357-
throw new IllegalStateException("C object conversion function failed", e);
358-
}
355+
if (isFloatSubtype(frame, inliningTarget, object, getClass, isSubtype)) {
356+
return readObFval(object, toSulongNode, interopLibrary, importCAPISymbolNode);
359357
}
360358
return null;
361359
}
362360

363-
public boolean isFloatSubtype(VirtualFrame frame, PythonNativeObject object, GetClassNode getClass, IsSubtypeNode isSubtype) {
364-
return isSubtype.execute(frame, getClass.execute(object), PythonContext.get(this).lookupType(PythonBuiltinClassType.PFloat));
361+
public static Double readObFval(PythonAbstractNativeObject object, ToSulongNode toSulongNode, InteropLibrary interopLibrary, ImportCAPISymbolNode importCAPISymbolNode) {
362+
Object res;
363+
try {
364+
res = interopLibrary.execute(importCAPISymbolNode.execute(FUN_PY_FLOAT_AS_DOUBLE), toSulongNode.execute(object));
365+
} catch (UnsupportedMessageException | UnsupportedTypeException | ArityException e) {
366+
throw CompilerDirectives.shouldNotReachHere(e);
367+
}
368+
369+
if (res instanceof Double) {
370+
return (Double) res;
371+
}
372+
/*
373+
* In case we want to be very correct, we would need to use
374+
* InteropLibrary.fitsInDouble/asDouble here.
375+
*/
376+
CompilerDirectives.transferToInterpreterAndInvalidate();
377+
throw CompilerDirectives.shouldNotReachHere(String.format("%s cannot be interpreted as Java double", object));
378+
}
379+
380+
public static boolean isFloatSubtype(VirtualFrame frame, Node inliningTarget, Object object, InlinedGetClassNode getClass, IsSubtypeNode isSubtype) {
381+
return isSubtype.execute(frame, getClass.execute(inliningTarget, object), PythonBuiltinClassType.PFloat);
382+
}
383+
384+
public static boolean isFloatSubtype(VirtualFrame frame, Node inliningTarget, PythonAbstractNativeObject object, GetPythonObjectClassNode getClass, IsSubtypeNode isSubtype) {
385+
return isSubtype.execute(frame, getClass.execute(inliningTarget, object), PythonBuiltinClassType.PFloat);
365386
}
366387

367388
@NeverDefault

0 commit comments

Comments
 (0)