Skip to content

Commit

Permalink
[GR-21700] [GR-21972] Add metaobject to TCK; Improve value asserts in…
Browse files Browse the repository at this point in the history
… TCK and some fixes.

PullRequest: graal/5642
  • Loading branch information
chumer committed Mar 31, 2020
2 parents 6ff944a + d09ed1b commit 97876cd
Show file tree
Hide file tree
Showing 40 changed files with 787 additions and 590 deletions.
4 changes: 2 additions & 2 deletions compiler/mx.compiler/suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -1849,7 +1849,7 @@
"org.graalvm.compiler.truffle.runtime",
"org.graalvm.compiler.core.test",
"truffle:TRUFFLE_SL_TEST",
"truffle:TRUFFLE_INSTRUMENT_TEST",
"truffle:TRUFFLE_TEST",
],
"annotationProcessors" : [
"GRAAL_PROCESSOR",
Expand Down Expand Up @@ -2118,7 +2118,7 @@
"JVMCI_HOTSPOT",
"GRAAL",
"truffle:TRUFFLE_SL_TEST",
"truffle:TRUFFLE_INSTRUMENT_TEST",
"truffle:TRUFFLE_TEST",
],
"exclude" : [
"mx:JUNIT",
Expand Down
1 change: 1 addition & 0 deletions sdk/src/org.graalvm.polyglot.tck/snapshot.sigtest
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ fld public final static org.graalvm.polyglot.tck.TypeDescriptor EXECUTABLE_ANY
fld public final static org.graalvm.polyglot.tck.TypeDescriptor HOST_OBJECT
fld public final static org.graalvm.polyglot.tck.TypeDescriptor INSTANTIABLE
fld public final static org.graalvm.polyglot.tck.TypeDescriptor INSTANTIABLE_ANY
fld public final static org.graalvm.polyglot.tck.TypeDescriptor META_OBJECT
fld public final static org.graalvm.polyglot.tck.TypeDescriptor NATIVE_POINTER
fld public final static org.graalvm.polyglot.tck.TypeDescriptor NULL
fld public final static org.graalvm.polyglot.tck.TypeDescriptor NUMBER
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,31 +125,38 @@ public final class TypeDescriptor {
/**
* Type descriptor for date.
*
* @since 20.0.0 beta 2
* @since 20.0
*/
public static final TypeDescriptor DATE = new TypeDescriptor(new PrimitiveImpl(PrimitiveKind.DATE));

/**
* Type descriptor for time.
*
* @since 20.0.0 beta 2
* @since 20.0
*/
public static final TypeDescriptor TIME = new TypeDescriptor(new PrimitiveImpl(PrimitiveKind.TIME));

/**
* Type descriptor for time zone.
*
* @since 20.0.0 beta 2
* @since 20.0
*/
public static final TypeDescriptor TIME_ZONE = new TypeDescriptor(new PrimitiveImpl(PrimitiveKind.TIME_ZONE));

/**
* Type descriptor for duration.
*
* @since 20.0.0 beta 2
* @since 20.0
*/
public static final TypeDescriptor DURATION = new TypeDescriptor(new PrimitiveImpl(PrimitiveKind.DURATION));

/**
* Type descriptor for metaobjects.
*
* @since 20.0
*/
public static final TypeDescriptor META_OBJECT = new TypeDescriptor(new PrimitiveImpl(PrimitiveKind.META_OBJECT));

/**
* Type descriptor for duration.
*
Expand Down Expand Up @@ -226,10 +233,11 @@ public final class TypeDescriptor {
*/
public static final TypeDescriptor ANY = new TypeDescriptor(new UnionImpl(new HashSet<>(Arrays.asList(
NOTYPE.impl, NULL.impl, BOOLEAN.impl, NUMBER.impl, STRING.impl, HOST_OBJECT.impl, NATIVE_POINTER.impl, OBJECT.impl, ARRAY.impl, EXECUTABLE_ANY.impl, INSTANTIABLE_ANY.impl,
DATE.impl, TIME.impl, TIME_ZONE.impl, DURATION.impl, EXCEPTION.impl))));
DATE.impl, TIME.impl, TIME_ZONE.impl, DURATION.impl, META_OBJECT.impl, EXCEPTION.impl))));

private static final TypeDescriptor[] PREDEFINED_TYPES = new TypeDescriptor[]{
NOTYPE, NULL, BOOLEAN, NUMBER, STRING, HOST_OBJECT, DATE, TIME, TIME_ZONE, DURATION, EXCEPTION, NATIVE_POINTER, OBJECT, ARRAY, EXECUTABLE, EXECUTABLE_ANY, INSTANTIABLE,
NOTYPE, NULL, BOOLEAN, NUMBER, STRING, HOST_OBJECT, DATE, TIME, TIME_ZONE, DURATION, META_OBJECT, EXCEPTION, NATIVE_POINTER, OBJECT, ARRAY, EXECUTABLE, EXECUTABLE_ANY,
INSTANTIABLE,
INSTANTIABLE_ANY, ANY
};

Expand Down Expand Up @@ -633,6 +641,9 @@ public static TypeDescriptor forValue(final Value value) {
if (value.isDuration()) {
descs.add(DURATION);
}
if (value.isMetaObject()) {
descs.add(META_OBJECT);
}
if (value.isException()) {
descs.add(EXCEPTION);
}
Expand Down Expand Up @@ -697,6 +708,7 @@ private enum PrimitiveKind {
TIME("time"),
TIME_ZONE("timeZone"),
DURATION("duration"),
META_OBJECT("metaObject"),
OBJECT("object"),
EXCEPTION("exception");

Expand Down
2 changes: 2 additions & 0 deletions sdk/src/org.graalvm.polyglot/snapshot.sigtest
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ meth public boolean asBoolean()
meth public boolean canExecute()
meth public boolean canInstantiate()
meth public boolean canInvokeMember(java.lang.String)
meth public boolean equals(java.lang.Object)
meth public boolean fitsInByte()
meth public boolean fitsInDouble()
meth public boolean fitsInFloat()
Expand Down Expand Up @@ -468,6 +469,7 @@ meth public byte asByte()
meth public double asDouble()
meth public float asFloat()
meth public int asInt()
meth public int hashCode()
meth public java.lang.RuntimeException throwException()
meth public java.lang.String asString()
meth public java.lang.String getMetaQualifiedName()
Expand Down
23 changes: 23 additions & 0 deletions sdk/src/org.graalvm.polyglot/src/org/graalvm/polyglot/Value.java
Original file line number Diff line number Diff line change
Expand Up @@ -1326,6 +1326,29 @@ public Context getContext() {
return impl.getContext();
}

/**
* {@inheritDoc}
*
* @since 20.1
*/
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Value)) {
return false;
}
return impl.equalsImpl(receiver, ((Value) obj).receiver);
}

/**
* {@inheritDoc}
*
* @since 20.1
*/
@Override
public int hashCode() {
return impl.hashCodeImpl(receiver);
}

/**
* Converts a Java host value to a polyglot value. Returns a value for any host or guest value.
* If there is a context available use {@link Context#asValue(Object)} for efficiency instead.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,10 @@ public boolean isMetaObject(Object receiver) {
public abstract String getMetaSimpleName(Object receiver);

public abstract boolean isMetaInstance(Object receiver, Object instance);

public abstract boolean equalsImpl(Object receiver, Object obj);

public abstract int hashCodeImpl(Object receiver);
}

public abstract Class<?> loadLanguageClass(String className);
Expand Down
12 changes: 6 additions & 6 deletions tools/mx.tools/suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
"sourceDirs" : ["src"],
"dependencies" : [
"com.oracle.truffle.tools.agentscript",
"truffle:TRUFFLE_INSTRUMENT_TEST",
"truffle:TRUFFLE_TEST",
"mx:JUNIT"
],
"annotationProcessors" : ["truffle:TRUFFLE_DSL_PROCESSOR"],
Expand Down Expand Up @@ -115,7 +115,7 @@
"sourceDirs" : ["src"],
"dependencies" : [
"com.oracle.truffle.tools.profiler",
"truffle:TRUFFLE_INSTRUMENT_TEST",
"truffle:TRUFFLE_TEST",
"mx:JUNIT"
],
"annotationProcessors" : ["truffle:TRUFFLE_DSL_PROCESSOR"],
Expand Down Expand Up @@ -144,7 +144,7 @@
"sourceDirs" : ["src"],
"dependencies" : [
"com.oracle.truffle.tools.coverage",
"truffle:TRUFFLE_INSTRUMENT_TEST",
"truffle:TRUFFLE_TEST",
"mx:JUNIT"
],
"annotationProcessors" : ["truffle:TRUFFLE_DSL_PROCESSOR"],
Expand Down Expand Up @@ -300,7 +300,7 @@
"com.oracle.truffle.tools.agentscript.test",
],
"distDependencies" : [
"truffle:TRUFFLE_INSTRUMENT_TEST",
"truffle:TRUFFLE_TEST",
"AGENTSCRIPT",
],
"description" : "Tests for the script driven tracing and instrumentation Agent.",
Expand Down Expand Up @@ -335,7 +335,7 @@
"com.oracle.truffle.tools.profiler.test",
],
"distDependencies" : [
"truffle:TRUFFLE_INSTRUMENT_TEST",
"truffle:TRUFFLE_TEST",
"TRUFFLE_PROFILER",
],
"description" : "Tests for the truffle profiler.",
Expand Down Expand Up @@ -370,7 +370,7 @@
"com.oracle.truffle.tools.coverage.test",
],
"distDependencies" : [
"truffle:TRUFFLE_INSTRUMENT_TEST",
"truffle:TRUFFLE_TEST",
"TRUFFLE_COVERAGE",
],
"description" : "Tests for the truffle coverage tool.",
Expand Down
26 changes: 5 additions & 21 deletions truffle/mx.truffle/suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@
"subDir" : "src",
"sourceDirs" : ["src"],
"dependencies" : [
"TRUFFLE_TCK_TESTS",
"TRUFFLE_API",
"TRUFFLE_SL",
"mx:JUNIT",
Expand All @@ -194,8 +195,8 @@
"subDir" : "src",
"sourceDirs" : ["src"],
"dependencies" : [
"com.oracle.truffle.api.instrumentation.test",
"TRUFFLE_API",
"TRUFFLE_INSTRUMENT_TEST",
"mx:JMH_1_21",
],
"requiresConcealed" : {
Expand Down Expand Up @@ -1025,25 +1026,6 @@
"maven" : False
},

"TRUFFLE_INSTRUMENT_TEST" : {
"subDir" : "src",
"javaCompliance" : "8+",
"dependencies" : [
"com.oracle.truffle.api.instrumentation.test",
],
"exclude" : ["mx:HAMCREST", "mx:JUNIT", "mx:JMH_1_21"],
"distDependencies" : [
"TRUFFLE_API",
"TRUFFLE_SL",
"TRUFFLE_TCK",
"sdk:POLYGLOT_TCK",
"TRUFFLE_DSL_PROCESSOR",
],
"description" : "Instrumentation tests including InstrumentationTestLanguage.",
"allowsJavadocWarnings": True,
"maven" : False,
},

"TRUFFLE_TEST" : {
"subDir" : "src",
"javaCompliance" : "8+",
Expand All @@ -1061,9 +1043,11 @@
"exclude" : ["mx:HAMCREST", "mx:JUNIT", "mx:JMH_1_21"],
"distDependencies" : [
"TRUFFLE_API",
"TRUFFLE_SL",
"TRUFFLE_TCK_COMMON",
"TRUFFLE_TCK_TESTS",
"TRUFFLE_NFI",
"TRUFFLE_DSL_PROCESSOR",
"TRUFFLE_INSTRUMENT_TEST",
"TRUFFLE_TEST_NATIVE",
"TRUFFLE_TCK",
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,10 @@ final class ReflectionLibraryDefault {
static class Send {

@Specialization(guards = {"message == cachedMessage", "cachedLibrary.accepts(receiver)"}, limit = "LIMIT")
static Object doSendCached(Object receiver, Message message, Object[] args,
static Object doSendCached(Object receiver, @SuppressWarnings("unused") Message message, Object[] args,
@Cached("message") Message cachedMessage,
@Cached("createLibrary(message, receiver)") Library cachedLibrary) throws Exception {
return message.getFactory().genericDispatch(cachedLibrary, receiver, cachedMessage, args, 0);
return cachedMessage.getFactory().genericDispatch(cachedLibrary, receiver, cachedMessage, args, 0);
}

static Library createLibrary(Message message, Object receiver) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Args = -H:Features=com.oracle.truffle.api.test.polyglot.RegisterTestClassesForReflectionFeature \
-H:ReflectionConfigurationResources=com/oracle/truffle/api/test/polyglot/reflection.json \
-H:DynamicProxyConfigurationResources=com/oracle/truffle/api/test/polyglot/proxys.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import static org.junit.Assert.fail;

import java.util.concurrent.Callable;
import java.util.function.Consumer;
import java.util.function.Supplier;

import org.graalvm.polyglot.Context;
Expand Down Expand Up @@ -201,6 +202,32 @@ public static void assertFails(Callable<?> callable, Class<? extends Throwable>
fail("expected " + exceptionType.getName() + " but no exception was thrown");
}

public static <T extends Throwable> void assertFails(Runnable run, Class<T> exceptionType, Consumer<T> verifier) {
try {
run.run();
} catch (Throwable t) {
if (!exceptionType.isInstance(t)) {
throw new AssertionError("expected instanceof " + exceptionType.getName() + " was " + t.getClass().getName(), t);
}
verifier.accept(exceptionType.cast(t));
return;
}
fail("expected " + exceptionType.getName() + " but no exception was thrown");
}

public static <T extends Throwable> void assertFails(Callable<?> callable, Class<T> exceptionType, Consumer<T> verifier) {
try {
callable.call();
} catch (Throwable t) {
if (!exceptionType.isInstance(t)) {
throw new AssertionError("expected instanceof " + exceptionType.getName() + " was " + t.getClass().getName(), t);
}
verifier.accept(exceptionType.cast(t));
return;
}
fail("expected " + exceptionType.getName() + " but no exception was thrown");
}

private static class TestRootNode extends RootNode {

@Child private Node node;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,16 @@
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand All @@ -65,7 +69,11 @@
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Engine;
import org.graalvm.polyglot.HostAccess;
import org.graalvm.polyglot.PolyglotAccess;
import org.graalvm.polyglot.Value;
import org.graalvm.polyglot.proxy.ProxyArray;
import org.graalvm.polyglot.proxy.ProxyExecutable;
import org.graalvm.polyglot.proxy.ProxyObject;
import org.junit.BeforeClass;
import org.junit.Test;

Expand All @@ -87,15 +95,8 @@
import com.oracle.truffle.api.test.GCUtils;
import com.oracle.truffle.api.test.option.OptionProcessorTest.OptionTestLang1;
import com.oracle.truffle.api.test.polyglot.ContextAPITestLanguage.LanguageContext;
import com.oracle.truffle.api.test.polyglot.ValueAssert.Trait;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.util.HashSet;
import java.util.Set;
import org.graalvm.polyglot.PolyglotAccess;
import org.graalvm.polyglot.proxy.ProxyArray;
import org.graalvm.polyglot.proxy.ProxyExecutable;
import org.graalvm.polyglot.proxy.ProxyObject;
import com.oracle.truffle.tck.tests.ValueAssert;
import com.oracle.truffle.tck.tests.ValueAssert.Trait;

public class ContextAPITest {
private static HostAccess CONFIG;
Expand Down Expand Up @@ -248,15 +249,15 @@ public void testExperimentalOption() {

@Test
public void testExperimentalOptionException() {
ValueAssert.assertFails(() -> Context.newBuilder().option("optiontestlang1.StringOption2", "Hello").build(), IllegalArgumentException.class, e -> {
AbstractPolyglotTest.assertFails(() -> Context.newBuilder().option("optiontestlang1.StringOption2", "Hello").build(), IllegalArgumentException.class, e -> {
assertEquals("Option 'optiontestlang1.StringOption2' is experimental and must be enabled with allowExperimentalOptions(). Do not use experimental options in production environments.",
e.getMessage());
});
}

@Test
public void testImageBuildTimeOptionAtRuntime() {
ValueAssert.assertFails(() -> Context.newBuilder().option("image-build-time.DisablePrivileges", "createProcess").build(), IllegalArgumentException.class, e -> {
AbstractPolyglotTest.assertFails(() -> Context.newBuilder().option("image-build-time.DisablePrivileges", "createProcess").build(), IllegalArgumentException.class, e -> {
assertEquals("Image build-time option 'image-build-time.DisablePrivileges' cannot be set at runtime", e.getMessage());
});
}
Expand Down

0 comments on commit 97876cd

Please sign in to comment.