Skip to content

Commit cbcbfe9

Browse files
committed
Implement context function ctx_Tuple_FromArray
1 parent 0cf3003 commit cbcbfe9

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/hpy/GraalHPyContext.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
import com.oracle.graal.python.builtins.objects.cext.hpy.GraalHPyContextFunctions.GraalHPyRichcompare;
9797
import com.oracle.graal.python.builtins.objects.cext.hpy.GraalHPyContextFunctions.GraalHPySetAttr;
9898
import com.oracle.graal.python.builtins.objects.cext.hpy.GraalHPyContextFunctions.GraalHPySetItem;
99+
import com.oracle.graal.python.builtins.objects.cext.hpy.GraalHPyContextFunctions.GraalHPyTupleFromArray;
99100
import com.oracle.graal.python.builtins.objects.cext.hpy.GraalHPyContextFunctions.GraalHPyTypeFromSpec;
100101
import com.oracle.graal.python.builtins.objects.cext.hpy.GraalHPyContextFunctions.GraalHPyTypeGenericNew;
101102
import com.oracle.graal.python.builtins.objects.cext.hpy.GraalHPyContextFunctions.GraalHPyUnicodeAsUTF8String;
@@ -249,7 +250,8 @@ enum HPyContextMembers {
249250
CTX_RICHCOMPAREBOOL("ctx_RichCompareBool"),
250251
CTX_HASH("ctx_Hash"),
251252
CTX_NUMBER_CHECK("ctx_Number_Check"),
252-
CTX_LENGTH("ctx_Length");
253+
CTX_LENGTH("ctx_Length"),
254+
CTX_TUPLE_FROMARRAY("ctx_Tuple_FromArray");
253255

254256
private final String name;
255257

@@ -539,6 +541,7 @@ private static Object[] createMembers(PythonContext context) {
539541
members[HPyContextMembers.CTX_HASH.ordinal()] = new GraalHPyCallBuiltinFunction(BuiltinNames.HASH, 1, GraalHPyConversionNodeSupplier.TO_INT64);
540542
members[HPyContextMembers.CTX_NUMBER_CHECK.ordinal()] = new GraalHPyIsNumber();
541543
members[HPyContextMembers.CTX_LENGTH.ordinal()] = new GraalHPyCallBuiltinFunction(BuiltinNames.LEN, 1, GraalHPyConversionNodeSupplier.TO_INT64);
544+
members[HPyContextMembers.CTX_TUPLE_FROMARRAY.ordinal()] = new GraalHPyTupleFromArray();
542545
return members;
543546
}
544547

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/hpy/GraalHPyContextFunctions.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@
153153
import com.oracle.truffle.api.interop.ArityException;
154154
import com.oracle.truffle.api.interop.InteropException;
155155
import com.oracle.truffle.api.interop.InteropLibrary;
156+
import com.oracle.truffle.api.interop.InvalidArrayIndexException;
156157
import com.oracle.truffle.api.interop.TruffleObject;
157158
import com.oracle.truffle.api.interop.UnsupportedMessageException;
158159
import com.oracle.truffle.api.interop.UnsupportedTypeException;
@@ -1612,4 +1613,58 @@ Object execute(Object[] arguments,
16121613
}
16131614
}
16141615
}
1616+
1617+
@ExportLibrary(InteropLibrary.class)
1618+
public static final class GraalHPyTupleFromArray extends GraalHPyContextFunction {
1619+
1620+
@ExportMessage
1621+
Object execute(Object[] arguments,
1622+
@Cached HPyAsContextNode asContextNode,
1623+
@Cached CastToJavaIntExactNode castToJavaIntExactNode,
1624+
@CachedLibrary(limit = "3") InteropLibrary lib,
1625+
@Cached PCallHPyFunction callHelperNode,
1626+
@Cached HPyAsPythonObjectNode asPythonObjectNode,
1627+
@Cached PRaiseNode raiseNode,
1628+
@Cached PythonObjectFactory factory,
1629+
@Cached HPyAsHandleNode asHandleNode,
1630+
@Cached HPyTransformExceptionToNativeNode transformExceptionToNativeNode) throws ArityException, UnsupportedTypeException {
1631+
if (arguments.length != 3) {
1632+
CompilerDirectives.transferToInterpreterAndInvalidate();
1633+
throw ArityException.create(3, arguments.length);
1634+
}
1635+
GraalHPyContext nativeContext = asContextNode.execute(arguments[0]);
1636+
Object arrayPtr = arguments[1];
1637+
int n;
1638+
try {
1639+
n = castToJavaIntExactNode.execute(arguments[2]);
1640+
} catch (CannotCastException e) {
1641+
CompilerDirectives.transferToInterpreterAndInvalidate();
1642+
throw UnsupportedTypeException.create(arguments, "third argument must fit into int");
1643+
}
1644+
1645+
Object typedArrayPtr = callHelperNode.call(nativeContext, GraalHPyNativeSymbols.GRAAL_HPY_FROM_HPY_ARRAY, arrayPtr, n);
1646+
if (!lib.hasArrayElements(typedArrayPtr)) {
1647+
throw CompilerDirectives.shouldNotReachHere("returned pointer object must have array type");
1648+
}
1649+
1650+
try {
1651+
Object[] elements = new Object[n];
1652+
try {
1653+
for (int i = 0; i < elements.length; i++) {
1654+
elements[i] = asPythonObjectNode.execute(nativeContext, lib.readArrayElement(typedArrayPtr, i));
1655+
}
1656+
} catch (UnsupportedMessageException e) {
1657+
throw CompilerDirectives.shouldNotReachHere(e);
1658+
} catch (InvalidArrayIndexException e) {
1659+
CompilerDirectives.transferToInterpreterAndInvalidate();
1660+
throw raiseNode.raise(SystemError, "Cannot access index %d although array should have size %d ", e.getInvalidIndex(), n);
1661+
}
1662+
1663+
return asHandleNode.execute(nativeContext, factory.createTuple(elements));
1664+
} catch (PException e) {
1665+
transformExceptionToNativeNode.execute(nativeContext, e);
1666+
return nativeContext.getNullHandle();
1667+
}
1668+
}
1669+
}
16151670
}

0 commit comments

Comments
 (0)