package com.oracle.truffle.llvm.tests.interop;
import java.util.HashMap;
import java.util.Map;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.interop.InteropException;
import com.oracle.truffle.api.nodes.RootNode;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import com.oracle.truffle.api.CallTarget;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.llvm.tests.interop.values.StructObject;
import com.oracle.truffle.tck.TruffleRunner;
import com.oracle.truffle.tck.TruffleRunner.Inject;
@RunWith(TruffleRunner.class)
public class SlowPathCallTest extends InteropTestBase {
private static Object[] function;
private static Object testLibrary;
@BeforeClass
public static void loadTestBitcode() {
testLibrary = loadTestBitcodeInternal("interopSlowpathCall.c");
}
public class TestSlow extends RootNode {
public TestSlow() {
super(null);
InteropLibrary interop = InteropLibrary.getFactory().getUncached(testLibrary);
try {
function = new Object[]{
interop.readMember(testLibrary, "get_a"),
interop.readMember(testLibrary, "get_b"),
interop.readMember(testLibrary, "get_c"),
interop.readMember(testLibrary, "get_d"),
interop.readMember(testLibrary, "get_e")
};
} catch (InteropException ex) {
throw new AssertionError(ex);
}
}
@Override
public Object execute(VirtualFrame frame) {
InteropLibrary interop = InteropLibrary.getFactory().getUncached();
try {
Object a = interop.execute(function[0], frame.getArguments());
Object b = interop.execute(function[1], frame.getArguments());
Object c = interop.execute(function[2], frame.getArguments());
Object d = interop.execute(function[3], frame.getArguments());
Object e = interop.execute(function[4], frame.getArguments());
assertResult(a, b, c, d, e);
} catch (InteropException ex) {
throw new AssertionError(ex);
}
return null;
}
@CompilerDirectives.TruffleBoundary
private void assertResult(Object a, Object b, Object c, Object d, Object e) {
Map<String, Object> expected = makeObjectSlowpathCall();
Assert.assertEquals(expected.get("a"), a);
Assert.assertEquals(expected.get("b"), b);
Assert.assertEquals(expected.get("c"), c);
Assert.assertEquals(expected.get("d"), d);
Assert.assertEquals(expected.get("e"), e);
}
}
@Test
public void testReadFromDerefHandle(@Inject(TestSlow.class) CallTarget accessDerefHandle) {
Map<String, Object> members = makeObjectSlowpathCall();
accessDerefHandle.call(new StructObject(members));
}
private static Map<String, Object> makeObjectSlowpathCall() {
HashMap<String, Object> values = new HashMap<>();
values.put("a", 2);
values.put("b", 3);
values.put("c", 5);
values.put("d", 7);
values.put("e", 11);
return values;
}
}