4040 */
4141package com .oracle .truffle .nfi .test ;
4242
43- import static org .hamcrest .CoreMatchers .instanceOf ;
44- import static org .hamcrest .CoreMatchers .is ;
45-
4643import java .lang .reflect .Array ;
4744import java .util .ArrayList ;
4845import java .util .Collection ;
4946
47+ import static org .hamcrest .CoreMatchers .instanceOf ;
48+ import static org .hamcrest .CoreMatchers .is ;
5049import org .junit .Assert ;
5150import org .junit .Test ;
5251import org .junit .runner .RunWith ;
5958import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
6059import com .oracle .truffle .api .frame .VirtualFrame ;
6160import com .oracle .truffle .api .interop .InteropException ;
62- import com .oracle .truffle .api .interop . InteropLibrary ;
61+ import com .oracle .truffle .nfi . api .SignatureLibrary ;
6362import com .oracle .truffle .nfi .backend .spi .types .NativeSimpleType ;
6463import com .oracle .truffle .tck .TruffleRunner ;
6564import com .oracle .truffle .tck .TruffleRunner .Inject ;
@@ -93,29 +92,36 @@ public static Collection<Object[]> data() {
9392
9493 public class CreateAndSumArray extends NFITestRootNode {
9594
96- private final Class <?> finalJavaType ;
97-
9895 private final Object store ;
96+ private final Object storeSignature ;
97+
9998 private final Object sum ;
99+ private final Object sumSignature ;
100100
101- @ Child InteropLibrary storeInterop ;
102- @ Child InteropLibrary sumInterop ;
101+ @ Child SignatureLibrary storeSignatureLibrary ;
102+ @ Child SignatureLibrary sumSignatureLibrary ;
103103
104104 public CreateAndSumArray () {
105- this . finalJavaType = javaType ;
106- this .store = lookupAndBind ( "store_" + nativeType , String . format ( "([%s], uint32, %s) : void" , nativeType , nativeType ) );
107- this .sum = lookupAndBind ( "sum_" + nativeType , String .format ("([%s], uint32) : %s " , nativeType , nativeType ));
105+ try {
106+ this .store = UNCACHED_INTEROP . readMember ( testLibrary , "store_" + nativeType );
107+ this .storeSignature = parseSignature ( String .format ("([%s], uint32, %s ) : void " , nativeType , nativeType ));
108108
109- this .storeInterop = getInterop (this .store );
110- this .sumInterop = getInterop (this .sum );
109+ this .sum = UNCACHED_INTEROP .readMember (testLibrary , "sum_" + nativeType );
110+ this .sumSignature = parseSignature (String .format ("([%s], uint32) : %s" , nativeType , nativeType ));
111+
112+ this .storeSignatureLibrary = SignatureLibrary .getFactory ().create (this .storeSignature );
113+ this .sumSignatureLibrary = SignatureLibrary .getFactory ().create (this .sumSignature );
114+ } catch (InteropException ex ) {
115+ throw new AssertionError (ex );
116+ }
111117 }
112118
113119 @ TruffleBoundary
114120 private void verifyArray (Object array ) {
115121 int length = Array .getLength (array );
116122 for (int i = 0 ; i < length ; i ++) {
117123 Object elem = Array .get (array , i );
118- Assert .assertThat ("array element" , elem , is (instanceOf (finalJavaType )));
124+ Assert .assertThat ("array element" , elem , is (instanceOf (javaType )));
119125 long actual = 0 ;
120126 long expected = i + 1 ;
121127 if (elem instanceof Number ) {
@@ -135,29 +141,40 @@ private void verifyArray(Object array) {
135141
136142 @ Override
137143 public Object executeTest (VirtualFrame frame ) {
138- int arrayLength = (Integer ) frame .getArguments ()[0 ];
139-
140- Object array = Array .newInstance (finalJavaType , arrayLength );
141- Object wrappedArray = runWithPolyglot .getTruffleTestEnv ().asGuestValue (array );
144+ Object array = frame .getArguments ()[0 ];
145+ Object wrappedArray = frame .getArguments ()[1 ];
146+ int arrayLength = Array .getLength (array );
142147
143148 try {
144149 for (int i = 0 ; i < arrayLength ; i ++) {
145- storeInterop . execute ( store , wrappedArray , i , i + 1 );
150+ storeSignatureLibrary . call ( storeSignature , store , wrappedArray , i , i + 1 );
146151 }
147152 verifyArray (array );
148- return sumInterop . execute ( sum , wrappedArray , arrayLength );
153+ return sumSignatureLibrary . call ( sumSignature , sum , wrappedArray , arrayLength );
149154 } catch (InteropException ex ) {
150155 CompilerDirectives .transferToInterpreter ();
151156 throw new AssertionError (ex );
152157 }
153158 }
154159 }
155160
156- @ Test
157- public void testSumArray (@ Inject (CreateAndSumArray .class ) CallTarget callTarget ) {
158- int arrayLength = 5 ;
159- Object ret = callTarget .call (arrayLength );
161+ private static void testSumArray (CallTarget callTarget , Object array , Object wrappedArray ) {
162+ int arrayLength = Array .getLength (array );
163+ Object ret = callTarget .call (array , wrappedArray );
160164 Assert .assertThat ("return value" , ret , is (instanceOf (Number .class )));
161165 Assert .assertEquals ("return value" , arrayLength * (arrayLength + 1 ) / 2 , ((Number ) ret ).intValue ());
162166 }
167+
168+ @ Test
169+ public void testSumArrayDirect (@ Inject (CreateAndSumArray .class ) CallTarget callTarget ) {
170+ Object array = Array .newInstance (javaType , 5 );
171+ testSumArray (callTarget , array , array );
172+ }
173+
174+ @ Test
175+ public void testSumArrayWrapped (@ Inject (CreateAndSumArray .class ) CallTarget callTarget ) {
176+ Object array = Array .newInstance (javaType , 5 );
177+ Object wrappedArray = runWithPolyglot .getTruffleTestEnv ().asGuestValue (array );
178+ testSumArray (callTarget , array , wrappedArray );
179+ }
163180}
0 commit comments