Skip to content

Commit 1dce7ad

Browse files
committed
[GR-2925] Add test interface to Truffle.
2 parents a38fd02 + 70b3f55 commit 1dce7ad

File tree

6 files changed

+597
-2
lines changed

6 files changed

+597
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ This changelog summarizes major changes between Truffle versions relevant to lan
2525
* Added [TruffleInstrument.Registration.services()](http://graalvm.github.io/truffle/javadoc/com/oracle/truffle/api/instrumentation/TruffleInstrument.Registration#services) to support declarative registration of services
2626
* Deprecated internal class DSLOptions. Will be removed in the next release.
2727
* Deprecated [Shape.getData()](http://graalvm.github.io/truffle/javadoc/com/oracle/truffle/api/object/Shape.html) and [ObjectType.createShapeData(Shape)](http://graalvm.github.io/truffle/javadoc/com/oracle/truffle/api/object/ObjectType.html) without replacement.
28+
* Added [TruffleRunner](http://graalvm.github.io/truffle/javadoc/com/oracle/truffle/tck/TruffleRunner.html) JUnit runner for unit testing Truffle compilation.
2829

2930
## Version 0.24
3031
1-Mar-2017

truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/impl/DefaultTruffleRuntime.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -60,6 +60,19 @@ public final class DefaultTruffleRuntime implements TruffleRuntime {
6060
private final ThreadLocal<DefaultFrameInstance> stackTraces = new ThreadLocal<>();
6161
private final DefaultTVMCI tvmci = new DefaultTVMCI();
6262

63+
private final TVMCI.Test<CallTarget> testTvmci = new TVMCI.Test<CallTarget>() {
64+
65+
@Override
66+
public CallTarget createTestCallTarget(String testName, RootNode testNode) {
67+
return createCallTarget(testNode);
68+
}
69+
70+
@Override
71+
public void finishWarmup(CallTarget callTarget) {
72+
// do nothing if we have no compiler
73+
}
74+
};
75+
6376
public DefaultTruffleRuntime() {
6477
}
6578

@@ -183,6 +196,10 @@ void popFrame() {
183196
}
184197

185198
public <T> T getCapability(Class<T> capability) {
199+
if (capability == TVMCI.Test.class) {
200+
return capability.cast(testTvmci);
201+
}
202+
186203
final Iterator<T> it = ServiceLoader.load(capability).iterator();
187204
try {
188205
return it.hasNext() ? it.next() : null;

truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/impl/TVMCI.java

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -24,6 +24,7 @@
2424
*/
2525
package com.oracle.truffle.api.impl;
2626

27+
import com.oracle.truffle.api.CallTarget;
2728
import com.oracle.truffle.api.TruffleLanguage;
2829
import com.oracle.truffle.api.frame.FrameDescriptor;
2930
import com.oracle.truffle.api.impl.Accessor.InstrumentSupport;
@@ -37,6 +38,37 @@
3738
* @since 0.12
3839
*/
3940
public abstract class TVMCI {
41+
42+
/**
43+
* An interface between the Truffle test runner and hosting virtual machine.
44+
*
45+
* @param <T> the {@link CallTarget} subclass of the hosting virtual machine
46+
*
47+
* @since 0.25
48+
*/
49+
public abstract static class Test<T extends CallTarget> {
50+
51+
/**
52+
* Create a call target for the purpose of running a unit test.
53+
*
54+
* @param testName the name of the unit test
55+
* @param testNode the root node containing the test code
56+
* @return a call target
57+
*
58+
* @since 0.25
59+
*/
60+
protected abstract T createTestCallTarget(String testName, RootNode testNode);
61+
62+
/**
63+
* Notify the VM that the warmup is finished, and it should now compile the test code.
64+
*
65+
* @param callTarget a call target that was created with {@link #createTestCallTarget}
66+
*
67+
* @since 0.25
68+
*/
69+
protected abstract void finishWarmup(T callTarget);
70+
}
71+
4072
/**
4173
* Only useful for virtual machine implementors.
4274
*
@@ -132,4 +164,31 @@ protected boolean isCloneUninitializedSupported(RootNode root) {
132164
protected RootNode cloneUninitialized(RootNode root) {
133165
return Accessor.nodesAccess().cloneUninitialized(root);
134166
}
167+
168+
/**
169+
* Accessor for {@link TVMCI#Test} class.
170+
*
171+
* @param <T>
172+
*
173+
* @since 0.25
174+
*/
175+
public static class TestAccessor<T extends CallTarget> {
176+
177+
private final TVMCI.Test<T> testTvmci;
178+
179+
protected TestAccessor(TVMCI.Test<T> testTvmci) {
180+
if (!this.getClass().getPackage().getName().equals("com.oracle.truffle.tck")) {
181+
throw new IllegalStateException();
182+
}
183+
this.testTvmci = testTvmci;
184+
}
185+
186+
protected final T createTestCallTarget(String testName, RootNode testNode) {
187+
return testTvmci.createTestCallTarget(testName, testNode);
188+
}
189+
190+
protected final void finishWarmup(T callTarget) {
191+
testTvmci.finishWarmup(callTarget);
192+
}
193+
}
135194
}

0 commit comments

Comments
 (0)