134
134
import com .oracle .graal .python .runtime .object .PythonObjectFactory ;
135
135
import com .oracle .graal .python .runtime .sequence .PSequence ;
136
136
import com .oracle .graal .python .runtime .sequence .storage .ObjectSequenceStorage ;
137
+ import com .oracle .graal .python .util .OverflowException ;
137
138
import com .oracle .graal .python .util .PythonUtils ;
138
139
import com .oracle .truffle .api .CompilerAsserts ;
139
140
import com .oracle .truffle .api .CompilerDirectives ;
@@ -1598,11 +1599,11 @@ Object execute(Object[] arguments,
1598
1599
}
1599
1600
}
1600
1601
1601
- @ ExportLibrary (InteropLibrary .class )
1602
- public static final class GraalHPyBuilderNew extends GraalHPyContextFunction {
1602
+ abstract static class GraalHPyBuilderNewBase extends GraalHPyContextFunction {
1603
1603
1604
- @ ExportMessage
1605
- Object execute (Object [] arguments ,
1604
+ protected abstract Object createObject (int capacity );
1605
+
1606
+ protected final Object doExecute (Object [] arguments ,
1606
1607
@ Cached HPyAsContextNode asContextNode ,
1607
1608
@ Cached CastToJavaIntExactNode castToJavaIntExactNode ,
1608
1609
@ Cached HPyAsHandleNode asHandleNode ) throws ArityException {
@@ -1614,11 +1615,7 @@ Object execute(Object[] arguments,
1614
1615
try {
1615
1616
int capacity = castToJavaIntExactNode .execute (arguments [1 ]);
1616
1617
if (capacity >= 0 ) {
1617
- Object [] data = new Object [capacity ];
1618
- for (int i = 0 ; i < data .length ; i ++) {
1619
- data [i ] = PNone .NONE ;
1620
- }
1621
- return asHandleNode .execute (nativeContext , new ObjectSequenceStorage (data ));
1618
+ return asHandleNode .execute (nativeContext , createObject (capacity ));
1622
1619
}
1623
1620
} catch (CannotCastException e ) {
1624
1621
// fall through
@@ -1627,6 +1624,26 @@ Object execute(Object[] arguments,
1627
1624
}
1628
1625
}
1629
1626
1627
+ @ ExportLibrary (InteropLibrary .class )
1628
+ public static final class GraalHPyBuilderNew extends GraalHPyBuilderNewBase {
1629
+ @ ExportMessage
1630
+ Object execute (Object [] arguments ,
1631
+ @ Cached HPyAsContextNode asContextNode ,
1632
+ @ Cached CastToJavaIntExactNode castToJavaIntExactNode ,
1633
+ @ Cached HPyAsHandleNode asHandleNode ) throws ArityException {
1634
+ return doExecute (arguments , asContextNode , castToJavaIntExactNode , asHandleNode );
1635
+ }
1636
+
1637
+ @ Override
1638
+ protected Object createObject (int capacity ) {
1639
+ Object [] data = new Object [capacity ];
1640
+ for (int i = 0 ; i < data .length ; i ++) {
1641
+ data [i ] = PNone .NONE ;
1642
+ }
1643
+ return new ObjectSequenceStorage (data );
1644
+ }
1645
+ }
1646
+
1630
1647
@ ExportLibrary (InteropLibrary .class )
1631
1648
public static final class GraalHPyBuilderSet extends GraalHPyContextFunction {
1632
1649
@@ -1759,4 +1776,94 @@ private static ObjectSequenceStorage cast(Object object) {
1759
1776
1760
1777
}
1761
1778
1779
+ @ ExportLibrary (InteropLibrary .class )
1780
+ public static final class GraalHPyTrackerNew extends GraalHPyBuilderNewBase {
1781
+ @ ExportMessage
1782
+ Object execute (Object [] arguments ,
1783
+ @ Cached HPyAsContextNode asContextNode ,
1784
+ @ Cached CastToJavaIntExactNode castToJavaIntExactNode ,
1785
+ @ Cached HPyAsHandleNode asHandleNode ) throws ArityException {
1786
+ return doExecute (arguments , asContextNode , castToJavaIntExactNode , asHandleNode );
1787
+ }
1788
+
1789
+ @ Override
1790
+ protected Object createObject (int capacity ) {
1791
+ return new GraalHPyTracker (capacity );
1792
+ }
1793
+ }
1794
+
1795
+ @ ExportLibrary (InteropLibrary .class )
1796
+ public static final class GraalHPyTrackerAdd extends GraalHPyContextFunction {
1797
+ @ ExportMessage
1798
+ Object execute (Object [] arguments ,
1799
+ @ Cached HPyAsContextNode asContextNode ,
1800
+ @ Cached HPyAsPythonObjectNode asPythonObjectNode ,
1801
+ @ Cached HPyEnsureHandleNode ensureHandleNode ) throws ArityException , UnsupportedTypeException {
1802
+ if (arguments .length != 3 ) {
1803
+ CompilerDirectives .transferToInterpreterAndInvalidate ();
1804
+ throw ArityException .create (2 , arguments .length );
1805
+ }
1806
+ GraalHPyContext nativeContext = asContextNode .execute (arguments [0 ]);
1807
+ GraalHPyTracker builder = cast (asPythonObjectNode .execute (nativeContext , arguments [1 ]));
1808
+ if (builder == null ) {
1809
+ // that's really unexpected since the C signature should enforce a valid builder but
1810
+ // someone could have messed it up
1811
+ CompilerDirectives .transferToInterpreterAndInvalidate ();
1812
+ throw UnsupportedTypeException .create (arguments , "invalid builder object" );
1813
+ }
1814
+ try {
1815
+ builder .add (ensureHandleNode .execute (nativeContext , arguments [2 ]));
1816
+ } catch (OverflowException | OutOfMemoryError e ) {
1817
+ return -1 ;
1818
+ }
1819
+ return 0 ;
1820
+ }
1821
+
1822
+ private static GraalHPyTracker cast (Object object ) {
1823
+ if (object instanceof GraalHPyTracker ) {
1824
+ return (GraalHPyTracker ) object ;
1825
+ }
1826
+ return null ;
1827
+ }
1828
+ }
1829
+
1830
+ @ ExportLibrary (InteropLibrary .class )
1831
+ public static final class GraalHPyTrackerCleanup extends GraalHPyContextFunction {
1832
+ private final boolean removeAll ;
1833
+
1834
+ public GraalHPyTrackerCleanup (boolean removeAll ) {
1835
+ this .removeAll = removeAll ;
1836
+ }
1837
+
1838
+ @ ExportMessage
1839
+ Object execute (Object [] arguments ,
1840
+ @ Cached HPyAsContextNode asContextNode ,
1841
+ @ Cached HPyAsPythonObjectNode asPythonObjectNode ) throws ArityException , UnsupportedTypeException {
1842
+ if (arguments .length != 2 ) {
1843
+ CompilerDirectives .transferToInterpreterAndInvalidate ();
1844
+ throw ArityException .create (2 , arguments .length );
1845
+ }
1846
+ GraalHPyContext nativeContext = asContextNode .execute (arguments [0 ]);
1847
+ GraalHPyTracker builder = cast (asPythonObjectNode .execute (nativeContext , arguments [1 ]));
1848
+ if (builder == null ) {
1849
+ // that's really unexpected since the C signature should enforce a valid builder but
1850
+ // someone could have messed it up
1851
+ CompilerDirectives .transferToInterpreterAndInvalidate ();
1852
+ throw UnsupportedTypeException .create (arguments , "invalid builder object" );
1853
+ }
1854
+ if (removeAll ) {
1855
+ builder .removeAll ();
1856
+ } else {
1857
+ builder .free (nativeContext , ConditionProfile .getUncached ());
1858
+ }
1859
+ return 0 ;
1860
+ }
1861
+
1862
+ private static GraalHPyTracker cast (Object object ) {
1863
+ if (object instanceof GraalHPyTracker ) {
1864
+ return (GraalHPyTracker ) object ;
1865
+ }
1866
+ return null ;
1867
+ }
1868
+ }
1762
1869
}
0 commit comments