Search in sources :

Example 51 with InteropLibrary

use of com.oracle.truffle.api.interop.InteropLibrary in project graal by oracle.

the class InteropAssertionsTest method testReadHashValueOrDefault.

@Test
public void testReadHashValueOrDefault() throws UnsupportedMessageException {
    // we need no multi threaded context.
    setupEnv(Context.create());
    HashTest hashTest = new HashTest();
    InteropLibrary hashLib = createLibrary(InteropLibrary.class, hashTest);
    hashTest.writeHashEntry(1, -1);
    assertEquals(-1, hashLib.readHashValueOrDefault(hashTest, 1, 0));
    assertEquals(-2, hashLib.readHashValueOrDefault(hashTest, 2, -2));
    assertFails(() -> hashLib.readHashValueOrDefault(hashTest, null, 0), NullPointerException.class);
    assertFails(() -> hashLib.readHashValueOrDefault(hashTest, 1, null), NullPointerException.class);
    hashTest.hasHashEntries = false;
    assertFails(() -> hashLib.readHashValueOrDefault(hashTest, 1, 0), AssertionError.class);
    hashTest.hasHashEntries = true;
    hashTest.data.put(1, new Object());
    assertFails(() -> hashLib.readHashValueOrDefault(hashTest, 1, 0), AssertionError.class);
    hashTest.hasHashEntries = true;
    hashTest.data = null;
    assertFails(() -> hashLib.readHashValue(hashTest, 1), UnsupportedMessageException.class);
}
Also used : InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Test(org.junit.Test)

Example 52 with InteropLibrary

use of com.oracle.truffle.api.interop.InteropLibrary in project graal by oracle.

the class InteropAssertionsTest method testGetMetaObject.

@Test
public void testGetMetaObject() throws UnsupportedMessageException {
    GetMetaObjectTest v = new GetMetaObjectTest();
    InteropLibrary l = createLibrary(InteropLibrary.class, v);
    MetaObjectTest testMeta = new MetaObjectTest();
    testMeta.isMetaObject = true;
    testMeta.isMetaInstance = (o) -> o == v;
    testMeta.getMetaQualifiedName = () -> "testQualifiedName";
    testMeta.getMetaSimpleName = () -> "testSimpleName";
    v.hasMetaObject = false;
    v.getMetaObject = null;
    assertFalse(l.hasMetaObject(v));
    assertFails(() -> l.getMetaObject(v), UnsupportedMessageException.class);
    v.hasMetaObject = true;
    v.getMetaObject = () -> testMeta;
    assertTrue(l.hasMetaObject(v));
    assertSame(testMeta, l.getMetaObject(v));
    v.hasMetaObject = true;
    v.getMetaObject = null;
    assertFails(() -> l.hasMetaObject(v), AssertionError.class);
    assertFails(() -> l.getMetaObject(v), AssertionError.class);
    v.hasMetaObject = true;
    v.getMetaObject = () -> null;
    assertFails(() -> l.hasMetaObject(v), AssertionError.class);
    assertFails(() -> l.getMetaObject(v), AssertionError.class);
    v.hasMetaObject = false;
    v.getMetaObject = () -> testMeta;
    assertFails(() -> l.hasMetaObject(v), AssertionError.class);
    assertFails(() -> l.getMetaObject(v), AssertionError.class);
    v.hasMetaObject = true;
    v.getMetaObject = () -> testMeta;
    testMeta.getMetaQualifiedName = null;
    assertFails(() -> l.hasMetaObject(v), AssertionError.class);
    assertFails(() -> l.getMetaObject(v), AssertionError.class);
    v.hasMetaObject = true;
    v.getMetaObject = () -> testMeta;
    testMeta.getMetaQualifiedName = () -> "testQualifiedName";
    testMeta.getMetaSimpleName = null;
    assertFails(() -> l.hasMetaObject(v), AssertionError.class);
    assertFails(() -> l.getMetaObject(v), AssertionError.class);
    Wrapper wrapper = new Wrapper(v);
    InteropLibrary wrapperLibrary = createLibrary(InteropLibrary.class, wrapper);
    v.hasMetaObject = true;
    v.getMetaObject = () -> testMeta;
    testMeta.isMetaObject = true;
    testMeta.isMetaInstance = (o) -> o == v;
    testMeta.getMetaQualifiedName = () -> "testQualifiedName";
    testMeta.getMetaSimpleName = () -> "testSimpleName";
    assertTrue(wrapperLibrary.hasMetaObject(wrapper));
    assertSame(testMeta, wrapperLibrary.getMetaObject(wrapper));
}
Also used : InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) Test(org.junit.Test)

Example 53 with InteropLibrary

use of com.oracle.truffle.api.interop.InteropLibrary in project graal by oracle.

the class InteropAssertionsTest method testIsSame.

@Test
public void testIsSame() {
    IdentityTest v0 = new IdentityTest();
    IdentityTest v1 = new IdentityTest();
    InteropLibrary l0 = createLibrary(InteropLibrary.class, v0);
    InteropLibrary l1 = createLibrary(InteropLibrary.class, v1);
    // correct usage
    v0.isSameOrUndefined = (r, o) -> TriState.valueOf(o == v0);
    v0.identityHashCode = (r) -> System.identityHashCode(r);
    v1.isSameOrUndefined = (r, o) -> TriState.valueOf(o == v1);
    v1.identityHashCode = (r) -> System.identityHashCode(r);
    assertTrue(l0.isIdentical(v0, v0, l0));
    assertTrue(l1.isIdentical(v1, v1, l1));
    assertFalse(l0.isIdentical(v0, v1, l1));
    // missing identity hash code
    v0.isSameOrUndefined = (r, o) -> TriState.valueOf(o == v0);
    v0.identityHashCode = null;
    assertFails(() -> l0.isIdentical(v0, v1, l1), AssertionError.class);
    // symmetry violated
    v0.isSameOrUndefined = (r, o) -> {
        if (o == v1) {
            return TriState.TRUE;
        }
        return TriState.UNDEFINED;
    };
    v0.identityHashCode = (r) -> System.identityHashCode(r);
    assertFails(() -> l0.isIdentical(v0, v1, l1), AssertionError.class);
    // reflexivity violated
    v0.isSameOrUndefined = (r, o) -> {
        if (o == v0) {
            return TriState.FALSE;
        }
        return TriState.UNDEFINED;
    };
    v0.identityHashCode = (r) -> System.identityHashCode(r);
    assertFails(() -> l0.isIdentical(v0, v0, l0), AssertionError.class);
    // invalid identity hash code
    v0.isSameOrUndefined = (r, o) -> TriState.valueOf(o == v0 || o == v1);
    v0.identityHashCode = (r) -> 42;
    v1.isSameOrUndefined = (r, o) -> TriState.valueOf(o == v0 || o == v1);
    v1.identityHashCode = (r) -> 43;
    assertFails(() -> l0.isIdentical(v0, v1, l1), AssertionError.class);
    // fix invalid identity hash code
    v1.identityHashCode = (r) -> 42;
    assertTrue(l0.isIdentical(v0, v1, l1));
}
Also used : InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) Test(org.junit.Test)

Example 54 with InteropLibrary

use of com.oracle.truffle.api.interop.InteropLibrary in project graal by oracle.

the class InteropAssertionsTest method testSourceLocation.

@Test
public void testSourceLocation() throws UnsupportedMessageException {
    SourceSectionTest v = new SourceSectionTest();
    InteropLibrary l = createLibrary(InteropLibrary.class, v);
    SourceSection section = Source.newBuilder(ProxyLanguage.ID, "", "").build().createUnavailableSection();
    assertFails(() -> l.getSourceLocation(null), NullPointerException.class);
    v.hasSourceSection = false;
    v.sourceSection = null;
    assertFalse(l.hasSourceLocation(v));
    assertFails(() -> l.getSourceLocation(v), UnsupportedMessageException.class);
    v.hasSourceSection = true;
    v.sourceSection = () -> section;
    assertTrue(l.hasSourceLocation(v));
    assertSame(section, l.getSourceLocation(v));
    v.hasSourceSection = true;
    v.sourceSection = null;
    assertFails(() -> l.hasSourceLocation(v), AssertionError.class);
    assertFails(() -> l.getSourceLocation(v), AssertionError.class);
    v.hasSourceSection = true;
    v.sourceSection = () -> null;
    assertFails(() -> l.hasSourceLocation(v), AssertionError.class);
    assertFails(() -> l.getSourceLocation(v), AssertionError.class);
    v.hasSourceSection = false;
    v.sourceSection = () -> section;
    assertFails(() -> l.hasSourceLocation(v), AssertionError.class);
    assertFails(() -> l.getSourceLocation(v), AssertionError.class);
}
Also used : InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) SourceSection(com.oracle.truffle.api.source.SourceSection) Test(org.junit.Test)

Example 55 with InteropLibrary

use of com.oracle.truffle.api.interop.InteropLibrary in project graal by oracle.

the class InteropAssertionsTest method getExceptionCause.

@Test
public void getExceptionCause() throws UnsupportedMessageException {
    ExceptionTest exceptionTest = new ExceptionTest();
    InteropLibrary exceptionLib = createLibrary(InteropLibrary.class, exceptionTest);
    exceptionTest.hasExceptionCause = true;
    ExceptionTest cause = new ExceptionTest();
    exceptionTest.getExceptionCause = () -> cause;
    assertEquals(cause, exceptionLib.getExceptionCause(exceptionTest));
    exceptionTest.hasExceptionCause = false;
    assertFails(() -> exceptionLib.getExceptionCause(exceptionTest), AssertionError.class);
    exceptionTest.hasExceptionCause = true;
    exceptionTest.getExceptionCause = null;
    assertFails(() -> exceptionLib.getExceptionCause(exceptionTest), AssertionError.class);
    exceptionTest.getExceptionCause = () -> null;
    assertFails(() -> exceptionLib.getExceptionCause(exceptionTest), NullPointerException.class);
    TruffleObject empty = new TruffleObject() {
    };
    exceptionTest.getExceptionCause = () -> empty;
    assertFails(() -> exceptionLib.getExceptionCause(exceptionTest), AssertionError.class);
    exceptionTest.getExceptionCause = () -> new RuntimeException();
    assertFails(() -> exceptionLib.getExceptionCause(exceptionTest), AssertionError.class);
    InteropLibrary objectLib = createLibrary(InteropLibrary.class, empty);
    assertFails(() -> objectLib.getExceptionCause(empty), UnsupportedMessageException.class);
}
Also used : InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Test(org.junit.Test)

Aggregations

InteropLibrary (com.oracle.truffle.api.interop.InteropLibrary)158 Test (org.junit.Test)76 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)60 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)51 UnknownIdentifierException (com.oracle.truffle.api.interop.UnknownIdentifierException)18 UnsupportedTypeException (com.oracle.truffle.api.interop.UnsupportedTypeException)18 ArityException (com.oracle.truffle.api.interop.ArityException)16 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)15 WasmInstance (org.graalvm.wasm.WasmInstance)15 WebAssembly (org.graalvm.wasm.api.WebAssembly)15 VirtualFrame (com.oracle.truffle.api.frame.VirtualFrame)12 RootNode (com.oracle.truffle.api.nodes.RootNode)12 InteropException (com.oracle.truffle.api.interop.InteropException)11 InvalidArrayIndexException (com.oracle.truffle.api.interop.InvalidArrayIndexException)11 Dictionary (org.graalvm.wasm.api.Dictionary)8 SourceSection (com.oracle.truffle.api.source.SourceSection)6 CallTarget (com.oracle.truffle.api.CallTarget)5 Node (com.oracle.truffle.api.nodes.Node)5 ProxyObject (org.graalvm.polyglot.proxy.ProxyObject)5 WasmJsApiException (org.graalvm.wasm.exception.WasmJsApiException)5