|
61 | 61 | import java.nio.charset.StandardCharsets;
|
62 | 62 | import java.util.WeakHashMap;
|
63 | 63 |
|
64 |
| -import com.oracle.truffle.api.interop.UnknownIdentifierException; |
65 | 64 | import org.graalvm.collections.Pair;
|
66 | 65 |
|
67 | 66 | import com.oracle.graal.python.PythonLanguage;
|
|
98 | 97 | import com.oracle.graal.python.builtins.objects.common.HashingStorage;
|
99 | 98 | import com.oracle.graal.python.builtins.objects.common.HashingStorageLibrary;
|
100 | 99 | import com.oracle.graal.python.builtins.objects.common.SequenceNodes;
|
| 100 | +import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes; |
| 101 | +import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes.NoGeneralizationNode; |
101 | 102 | import com.oracle.graal.python.builtins.objects.dict.PDict;
|
102 | 103 | import com.oracle.graal.python.builtins.objects.function.PArguments;
|
103 | 104 | import com.oracle.graal.python.builtins.objects.function.PBuiltinFunction;
|
|
140 | 141 | import com.oracle.graal.python.runtime.exception.PException;
|
141 | 142 | import com.oracle.graal.python.runtime.object.PythonObjectFactory;
|
142 | 143 | import com.oracle.graal.python.runtime.sequence.PSequence;
|
| 144 | +import com.oracle.graal.python.runtime.sequence.storage.ObjectSequenceStorage; |
143 | 145 | import com.oracle.graal.python.util.PythonUtils;
|
144 | 146 | import com.oracle.truffle.api.CompilerAsserts;
|
145 | 147 | import com.oracle.truffle.api.CompilerDirectives;
|
|
156 | 158 | import com.oracle.truffle.api.interop.InteropLibrary;
|
157 | 159 | import com.oracle.truffle.api.interop.InvalidArrayIndexException;
|
158 | 160 | import com.oracle.truffle.api.interop.TruffleObject;
|
| 161 | +import com.oracle.truffle.api.interop.UnknownIdentifierException; |
159 | 162 | import com.oracle.truffle.api.interop.UnsupportedMessageException;
|
160 | 163 | import com.oracle.truffle.api.interop.UnsupportedTypeException;
|
161 | 164 | import com.oracle.truffle.api.library.CachedLibrary;
|
@@ -230,15 +233,7 @@ Object execute(Object[] arguments,
|
230 | 233 | }
|
231 | 234 | GraalHPyContext hpyContext = asContextNode.execute(arguments[0]);
|
232 | 235 | GraalHPyHandle handle = ensureHandleNode.execute(hpyContext, arguments[1]);
|
233 |
| - if (handle.isNative()) { |
234 |
| - try { |
235 |
| - hpyContext.releaseHPyHandleForObject((int) handle.asPointer()); |
236 |
| - } catch (UnsupportedMessageException e) { |
237 |
| - CompilerDirectives.transferToInterpreterAndInvalidate(); |
238 |
| - throw new IllegalStateException("trying to release non-native handle that claims to be native"); |
239 |
| - } |
240 |
| - } |
241 |
| - // nothing to do if the handle never got 'toNative' |
| 236 | + handle.close(hpyContext); |
242 | 237 | return 0;
|
243 | 238 | }
|
244 | 239 | }
|
@@ -1675,4 +1670,165 @@ Object execute(Object[] arguments,
|
1675 | 1670 | }
|
1676 | 1671 | }
|
1677 | 1672 | }
|
| 1673 | + |
| 1674 | + @ExportLibrary(InteropLibrary.class) |
| 1675 | + public static final class GraalHPyBuilderNew extends GraalHPyContextFunction { |
| 1676 | + |
| 1677 | + @ExportMessage |
| 1678 | + Object execute(Object[] arguments, |
| 1679 | + @Cached HPyAsContextNode asContextNode, |
| 1680 | + @Cached CastToJavaIntExactNode castToJavaIntExactNode, |
| 1681 | + @Cached HPyAsHandleNode asHandleNode) throws ArityException { |
| 1682 | + if (arguments.length != 2) { |
| 1683 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 1684 | + throw ArityException.create(2, arguments.length); |
| 1685 | + } |
| 1686 | + GraalHPyContext nativeContext = asContextNode.execute(arguments[0]); |
| 1687 | + try { |
| 1688 | + int capacity = castToJavaIntExactNode.execute(arguments[1]); |
| 1689 | + if (capacity >= 0) { |
| 1690 | + Object[] data = new Object[capacity]; |
| 1691 | + for (int i = 0; i < data.length; i++) { |
| 1692 | + data[i] = PNone.NONE; |
| 1693 | + } |
| 1694 | + return asHandleNode.execute(nativeContext, new ObjectSequenceStorage(data)); |
| 1695 | + } |
| 1696 | + } catch (CannotCastException e) { |
| 1697 | + // fall through |
| 1698 | + } |
| 1699 | + return nativeContext.getNullHandle(); |
| 1700 | + } |
| 1701 | + } |
| 1702 | + |
| 1703 | + @ExportLibrary(InteropLibrary.class) |
| 1704 | + public static final class GraalHPyBuilderSet extends GraalHPyContextFunction { |
| 1705 | + |
| 1706 | + @ExportMessage |
| 1707 | + Object execute(Object[] arguments, |
| 1708 | + @Cached HPyAsContextNode asContextNode, |
| 1709 | + @Cached HPyAsPythonObjectNode asPythonObjectNode, |
| 1710 | + @Cached CastToJavaIntExactNode castToJavaIntExactNode, |
| 1711 | + @Cached SequenceStorageNodes.SetItemDynamicNode setItemNode, |
| 1712 | + @Cached TransformExceptionToNativeNode transformExceptionToNativeNode) throws ArityException, UnsupportedTypeException { |
| 1713 | + if (arguments.length != 4) { |
| 1714 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 1715 | + throw ArityException.create(4, arguments.length); |
| 1716 | + } |
| 1717 | + GraalHPyContext nativeContext = asContextNode.execute(arguments[0]); |
| 1718 | + Object builder = asPythonObjectNode.execute(nativeContext, arguments[1]); |
| 1719 | + if (!isValid(builder)) { |
| 1720 | + // that's really unexpected since the C signature should enforce a valid builder but |
| 1721 | + // someone could have messed it up |
| 1722 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 1723 | + throw UnsupportedTypeException.create(arguments, "invalid builder object"); |
| 1724 | + } |
| 1725 | + ObjectSequenceStorage storage = (ObjectSequenceStorage) builder; |
| 1726 | + |
| 1727 | + try { |
| 1728 | + int idx = castToJavaIntExactNode.execute(arguments[2]); |
| 1729 | + Object value = asPythonObjectNode.execute(nativeContext, arguments[3]); |
| 1730 | + setItemNode.execute(NoGeneralizationNode.DEFAULT, storage, idx, value); |
| 1731 | + } catch (CannotCastException e) { |
| 1732 | + // fall through |
| 1733 | + } catch (PException e) { |
| 1734 | + transformExceptionToNativeNode.execute(e); |
| 1735 | + } |
| 1736 | + return 0; |
| 1737 | + } |
| 1738 | + |
| 1739 | + private boolean isValid(Object object) { |
| 1740 | + return object instanceof ObjectSequenceStorage; |
| 1741 | + } |
| 1742 | + |
| 1743 | + } |
| 1744 | + |
| 1745 | + @ExportLibrary(InteropLibrary.class) |
| 1746 | + public static final class GraalHPyBuilderBuild extends GraalHPyContextFunction { |
| 1747 | + |
| 1748 | + private final PythonBuiltinClassType type; |
| 1749 | + |
| 1750 | + public GraalHPyBuilderBuild(PythonBuiltinClassType type) { |
| 1751 | + assert type == PythonBuiltinClassType.PTuple || type == PythonBuiltinClassType.PList; |
| 1752 | + this.type = type; |
| 1753 | + } |
| 1754 | + |
| 1755 | + @ExportMessage |
| 1756 | + Object execute(Object[] arguments, |
| 1757 | + @Cached HPyAsContextNode asContextNode, |
| 1758 | + @Cached HPyAsPythonObjectNode asPythonObjectNode, |
| 1759 | + @Cached PythonObjectFactory factory, |
| 1760 | + @Cached HPyAsHandleNode asHandleNode) throws ArityException, UnsupportedTypeException { |
| 1761 | + if (arguments.length != 2) { |
| 1762 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 1763 | + throw ArityException.create(2, arguments.length); |
| 1764 | + } |
| 1765 | + GraalHPyContext nativeContext = asContextNode.execute(arguments[0]); |
| 1766 | + ObjectSequenceStorage builder = cast(asPythonObjectNode.execute(nativeContext, arguments[1])); |
| 1767 | + if (builder == null) { |
| 1768 | + // that's really unexpected since the C signature should enforce a valid builder but |
| 1769 | + // someone could have messed it up |
| 1770 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 1771 | + throw UnsupportedTypeException.create(arguments, "invalid builder object"); |
| 1772 | + } |
| 1773 | + |
| 1774 | + Object sequence; |
| 1775 | + switch (type) { |
| 1776 | + case PTuple: |
| 1777 | + sequence = factory.createTuple(builder); |
| 1778 | + break; |
| 1779 | + case PList: |
| 1780 | + sequence = factory.createList(builder); |
| 1781 | + break; |
| 1782 | + default: |
| 1783 | + throw CompilerDirectives.shouldNotReachHere(); |
| 1784 | + } |
| 1785 | + return asHandleNode.execute(sequence); |
| 1786 | + } |
| 1787 | + |
| 1788 | + private static ObjectSequenceStorage cast(Object object) { |
| 1789 | + if (object instanceof ObjectSequenceStorage) { |
| 1790 | + return (ObjectSequenceStorage) object; |
| 1791 | + } |
| 1792 | + return null; |
| 1793 | + } |
| 1794 | + |
| 1795 | + } |
| 1796 | + |
| 1797 | + @ExportLibrary(InteropLibrary.class) |
| 1798 | + public static final class GraalHPyBuilderCancel extends GraalHPyContextFunction { |
| 1799 | + |
| 1800 | + @ExportMessage |
| 1801 | + Object execute(Object[] arguments, |
| 1802 | + @Cached HPyAsContextNode asContextNode, |
| 1803 | + @Cached HPyEnsureHandleNode ensureHandleNode, |
| 1804 | + @Cached HPyAsPythonObjectNode asPythonObjectNode) throws ArityException, UnsupportedTypeException { |
| 1805 | + if (arguments.length != 2) { |
| 1806 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 1807 | + throw ArityException.create(2, arguments.length); |
| 1808 | + } |
| 1809 | + GraalHPyContext nativeContext = asContextNode.execute(arguments[0]); |
| 1810 | + GraalHPyHandle hpyHandle = ensureHandleNode.execute(nativeContext, arguments[1]); |
| 1811 | + hpyHandle.close(nativeContext); |
| 1812 | + |
| 1813 | + // be pedantic and also check what we are cancelling |
| 1814 | + ObjectSequenceStorage builder = cast(asPythonObjectNode.execute(nativeContext, hpyHandle)); |
| 1815 | + if (builder == null) { |
| 1816 | + // that's really unexpected since the C signature should enforce a valid builder but |
| 1817 | + // someone could have messed it up |
| 1818 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 1819 | + throw UnsupportedTypeException.create(arguments, "invalid builder object"); |
| 1820 | + } |
| 1821 | + |
| 1822 | + return 0; |
| 1823 | + } |
| 1824 | + |
| 1825 | + private static ObjectSequenceStorage cast(Object object) { |
| 1826 | + if (object instanceof ObjectSequenceStorage) { |
| 1827 | + return (ObjectSequenceStorage) object; |
| 1828 | + } |
| 1829 | + return null; |
| 1830 | + } |
| 1831 | + |
| 1832 | + } |
| 1833 | + |
1678 | 1834 | }
|
0 commit comments