Search in sources :

Example 46 with InteropLibrary

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

the class InteropDefaultsTest method testIterableDefaults.

@Test
public void testIterableDefaults() throws UnsupportedMessageException {
    Object empty = new TruffleObject() {
    };
    InteropLibrary emptyLib = createLibrary(InteropLibrary.class, empty);
    assertFalse(emptyLib.hasIterator(empty));
    assertFails(() -> emptyLib.getIterator(empty), UnsupportedMessageException.class);
    Array array = new Array(1, 2, 3);
    InteropLibrary arrayLib = createLibrary(InteropLibrary.class, array);
    assertTrue(arrayLib.hasIterator(array));
    arrayLib.getIterator(array);
}
Also used : InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Test(org.junit.Test)

Example 47 with InteropLibrary

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

the class InteropDefaultsTest method testIteratorDefaults.

@Test
public void testIteratorDefaults() throws UnsupportedMessageException, StopIterationException {
    Object empty = new TruffleObject() {
    };
    InteropLibrary emptyLib = createLibrary(InteropLibrary.class, empty);
    assertFalse(emptyLib.isIterator(empty));
    assertFails(() -> emptyLib.hasIteratorNextElement(empty), UnsupportedMessageException.class);
    assertFails(() -> emptyLib.getIteratorNextElement(empty), UnsupportedMessageException.class);
    Array array = new Array(1, 2, 3);
    InteropLibrary arrayLib = createLibrary(InteropLibrary.class, array);
    assertFalse(arrayLib.isIterator(array));
    assertFails(() -> arrayLib.hasIteratorNextElement(array), UnsupportedMessageException.class);
    assertFails(() -> arrayLib.getIteratorNextElement(array), UnsupportedMessageException.class);
    Object iterator = arrayLib.getIterator(array);
    InteropLibrary iteratorLib = createLibrary(InteropLibrary.class, iterator);
    assertTrue(iteratorLib.isIterator(iterator));
    assertTrue(iteratorLib.hasIteratorNextElement(iterator));
    iteratorLib.getIteratorNextElement(iterator);
}
Also used : InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Test(org.junit.Test)

Example 48 with InteropLibrary

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

the class InteropDefaultsTest method assertNumber.

private void assertNumber(Object v, boolean supportsByte, boolean supportsShort, boolean supportsInt, boolean supportsLong, boolean supportsFloat, boolean supportsDouble) throws InteropException {
    Object expectedValue = v;
    InteropLibrary l = createLibrary(InteropLibrary.class, v);
    assertTrue(l.isNumber(v));
    assertEquals(supportsByte, l.fitsInByte(v));
    assertEquals(supportsShort, l.fitsInShort(v));
    assertEquals(supportsInt, l.fitsInInt(v));
    assertEquals(supportsLong, l.fitsInLong(v));
    assertEquals(supportsFloat, l.fitsInFloat(v));
    assertEquals(supportsDouble, l.fitsInDouble(v));
    if (supportsByte) {
        assertEquals(((Number) expectedValue).byteValue(), l.asByte(v));
    } else {
        assertUnsupported(() -> l.asByte(v));
    }
    if (supportsShort) {
        assertEquals(((Number) expectedValue).shortValue(), l.asShort(v));
    } else {
        assertUnsupported(() -> l.asShort(v));
    }
    if (supportsInt) {
        assertEquals(((Number) expectedValue).intValue(), l.asInt(v));
    } else {
        assertUnsupported(() -> l.asInt(v));
    }
    if (supportsLong) {
        assertEquals(((Number) expectedValue).longValue(), l.asLong(v));
    } else {
        assertUnsupported(() -> l.asLong(v));
    }
    if (supportsFloat) {
        assertEquals(((Number) expectedValue).floatValue(), l.asFloat(v), 0);
    } else {
        assertUnsupported(() -> l.asFloat(v));
    }
    if (supportsDouble) {
        assertEquals(((Number) expectedValue).doubleValue(), l.asDouble(v), 0);
    } else {
        assertUnsupported(() -> l.asDouble(v));
    }
    assertEquals(v.toString(), l.toDisplayString(v));
    assertNoBoolean(v);
    assertNotNull(v);
    assertNoObject(v);
    assertNoArray(v);
    assertNoBuffer(v);
    assertNoString(v);
    // assert number
    assertNoNative(v);
    assertNotExecutable(v);
    assertNotInstantiable(v);
    assertNoMetaObject(v);
    assertHasNoMetaObject(v);
    assertNoDate(v);
    assertNoTime(v);
    assertNoTimeZone(v);
    assertNoDuration(v);
    assertNoSourceLocation(v);
    assertNoLanguage(v);
    assertNoIdentity(v);
}
Also used : InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) TruffleObject(com.oracle.truffle.api.interop.TruffleObject)

Example 49 with InteropLibrary

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

the class IsSameWrapperTest method test.

@Test
public void test() throws UnsupportedMessageException {
    Object[] values = new Object[] { true, false, new IdentityBoolean(true), new IdentityBoolean(true), new IdentityBoolean(false), new IdentityBoolean(false) };
    int actualTrueCount = 0;
    int actualFalseCount = 0;
    for (int i = 0; i < values.length; i++) {
        for (int j = 0; j < values.length; j++) {
            Object left = values[i];
            Object right = values[j];
            InteropLibrary leftLib = createLibrary(InteropLibrary.class, left);
            InteropLibrary rightLib = createLibrary(InteropLibrary.class, right);
            boolean expectedResult = true;
            if (!leftLib.hasIdentity(left) || !rightLib.hasIdentity(right)) {
                expectedResult = false;
            } else {
                expectedResult = leftLib.asBoolean(left) == rightLib.asBoolean(right);
            }
            if (expectedResult) {
                actualTrueCount++;
            } else {
                actualFalseCount++;
            }
            Object leftWrapper = new IdentityBooleanWrapper(left);
            Object rightWrapper = new IdentityBooleanWrapper(right);
            InteropLibrary leftWrapperLib = createLibrary(InteropLibrary.class, leftWrapper);
            InteropLibrary rightWrapperLib = createLibrary(InteropLibrary.class, rightWrapper);
            assertEquals(expectedResult, leftLib.isIdentical(left, right, rightLib));
            assertEquals(expectedResult, rightLib.isIdentical(right, left, leftLib));
            assertEquals(expectedResult, leftWrapperLib.isIdentical(leftWrapper, right, rightLib));
            assertEquals(expectedResult, leftWrapperLib.isIdentical(leftWrapper, rightWrapper, rightWrapperLib));
            assertEquals(expectedResult, leftLib.isIdentical(left, rightWrapper, rightWrapperLib));
            if (leftLib.hasIdentity(left)) {
                assertEquals(leftLib.asBoolean(left) ? 1 : 0, leftLib.identityHashCode(left));
                assertEquals(leftLib.asBoolean(left) ? 1 : 0, leftWrapperLib.identityHashCode(leftWrapper));
            } else {
                assertFails(() -> leftLib.identityHashCode(left), UnsupportedMessageException.class);
                assertFails(() -> leftWrapperLib.identityHashCode(leftWrapper), UnsupportedMessageException.class);
            }
            if (leftLib.hasIdentity(left) && rightLib.hasIdentity(right)) {
                if (expectedResult) {
                    assertEquals(leftLib.identityHashCode(left), rightLib.identityHashCode(right));
                    assertEquals(leftLib.identityHashCode(left), rightWrapperLib.identityHashCode(rightWrapper));
                    assertEquals(leftWrapperLib.identityHashCode(leftWrapper), rightLib.identityHashCode(right));
                    assertEquals(leftWrapperLib.identityHashCode(leftWrapper), rightWrapperLib.identityHashCode(rightWrapper));
                } else {
                    assertNotEquals(leftLib.identityHashCode(left), rightLib.identityHashCode(right));
                    assertNotEquals(leftLib.identityHashCode(left), rightWrapperLib.identityHashCode(rightWrapper));
                    assertNotEquals(leftWrapperLib.identityHashCode(leftWrapper), rightLib.identityHashCode(right));
                    assertNotEquals(leftWrapperLib.identityHashCode(leftWrapper), rightWrapperLib.identityHashCode(rightWrapper));
                }
            }
        }
    }
    assertEquals(8, actualTrueCount);
    assertEquals(28, actualFalseCount);
}
Also used : InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Test(org.junit.Test)

Example 50 with InteropLibrary

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

the class InteropAssertionsTest method getIterator.

@Test
public void getIterator() throws UnsupportedMessageException {
    IterableTest iterableTest = new IterableTest(new IteratorTest(1));
    InteropLibrary iterableLib = createLibrary(InteropLibrary.class, iterableTest);
    assertEquals(iterableTest.iterator.get(), iterableLib.getIterator(iterableTest));
    iterableTest.hasIterator = false;
    assertFails(() -> iterableLib.getIterator(iterableTest), AssertionError.class);
    iterableTest.hasIterator = true;
    iterableTest.iterator = null;
    assertFails(() -> iterableLib.getIterator(iterableTest), AssertionError.class);
    iterableTest.iterator = () -> null;
    assertFails(() -> iterableLib.getIterator(iterableTest), AssertionError.class);
    TruffleObject empty = new TruffleObject() {
    };
    iterableTest.iterator = () -> empty;
    assertFails(() -> iterableLib.getIterator(iterableTest), AssertionError.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