Search in sources :

Example 81 with TruffleObject

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

the class TruffleTCK method testPrimitiveReturnTypeDouble.

/**
 * @since 0.8 or earlier
 */
@Test
public void testPrimitiveReturnTypeDouble() throws Exception {
    PolyglotEngine.Value apply = findGlobalSymbol(applyNumbers());
    double value = RANDOM.nextInt(1000) + RANDOM.nextDouble();
    TruffleObject fn = JavaInterop.asTruffleFunction(ObjectBinaryOperation.class, new ConstantFunction(value));
    Number n = apply.execute(fn).as(Number.class);
    assertDouble("The same value returned (" + value + " + 10): ", value + 10, n.doubleValue());
}
Also used : TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Test(org.junit.Test)

Example 82 with TruffleObject

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

the class TruffleTCK method unwrapTruffleObject.

private static Object unwrapTruffleObject(Object obj) {
    try {
        if (obj instanceof TruffleObject) {
            Class<?> eto = Class.forName("com.oracle.truffle.api.vm.EngineTruffleObject");
            if (eto.isInstance(obj)) {
                final Field field = eto.getDeclaredField("delegate");
                field.setAccessible(true);
                return field.get(obj);
            }
        }
        return obj;
    } catch (Exception ex) {
        throw new IllegalStateException(ex);
    }
}
Also used : Field(java.lang.reflect.Field) TruffleObject(com.oracle.truffle.api.interop.TruffleObject)

Example 83 with TruffleObject

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

the class TruffleTCK method testIsExecutable.

/**
 * @since 0.16
 */
@Test
public void testIsExecutable() throws Exception {
    String id = functionAddNumbers();
    if (id == null) {
        return;
    }
    PolyglotEngine.Value apply = findGlobalSymbol(id);
    TruffleObject truffleObject = (TruffleObject) apply.execute().get();
    assertIsObjectOfLanguage(truffleObject);
    MessageInterface object = JavaInterop.asJavaObject(MessageInterface.class, truffleObject);
    Assert.assertEquals(true, object.isExecutable());
}
Also used : TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Test(org.junit.Test)

Example 84 with TruffleObject

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

the class TruffleTCK method testObjectWithKeyInfoAttributes.

/**
 * @since 0.26
 */
@Test
public void testObjectWithKeyInfoAttributes() throws Exception {
    String id = objectWithKeyInfoAttributes();
    if (id == null) {
        return;
    }
    PolyglotEngine.Value apply = findGlobalSymbol(id);
    TruffleObject obj = (TruffleObject) apply.execute().get();
    assertIsObjectOfLanguage(obj);
    KeyInfoInterface object = JavaInterop.asJavaObject(KeyInfoInterface.class, obj);
    int numKeys = KeyInfo.NONE;
    int keyInfo = object.unknown();
    assertFalse("An unknown property", KeyInfo.isExisting(keyInfo));
    int ro = object.ro();
    if (KeyInfo.isExisting(ro)) {
        assertTrue(KeyInfo.isReadable(ro));
        assertFalse(KeyInfo.isWritable(ro));
        assertFalse(KeyInfo.isInternal(ro));
        numKeys++;
    }
    int wo = object.wo();
    if (KeyInfo.isExisting(wo)) {
        assertFalse(KeyInfo.isReadable(wo));
        assertTrue(KeyInfo.isWritable(wo));
        assertFalse(KeyInfo.isInternal(wo));
        numKeys++;
    }
    int rw = object.rw();
    if (KeyInfo.isExisting(rw)) {
        assertTrue(KeyInfo.isReadable(rw));
        assertTrue(KeyInfo.isWritable(rw));
        assertFalse(KeyInfo.isInternal(rw));
        numKeys++;
    }
    int rm = object.rm();
    if (KeyInfo.isExisting(rm)) {
        assertTrue(KeyInfo.isRemovable(rm));
        numKeys++;
    }
    int invocable = object.invocable();
    if (KeyInfo.isExisting(invocable)) {
        assertTrue(KeyInfo.isInvocable(invocable));
        assertFalse(KeyInfo.isInternal(invocable));
        numKeys++;
    }
    int intern = object.intern();
    if (KeyInfo.isExisting(intern)) {
        assertTrue(KeyInfo.isInternal(intern));
    }
    Map map = JavaInterop.asJavaObject(Map.class, obj);
    assertEquals(map.toString(), numKeys, map.size());
    if (KeyInfo.isExisting(ro)) {
        assertTrue(map.containsKey("ro"));
    }
    if (KeyInfo.isExisting(wo)) {
        assertTrue(map.containsKey("wo"));
    }
    if (KeyInfo.isExisting(rw)) {
        assertTrue(map.containsKey("rw"));
    }
    if (KeyInfo.isExisting(rm)) {
        assertTrue(map.containsKey("rm"));
    }
    if (KeyInfo.isExisting(invocable)) {
        assertTrue(map.containsKey("invocable"));
    }
    assertFalse(map.containsKey("intern"));
    map = JavaInterop.getMapView(map, true);
    if (KeyInfo.isExisting(intern)) {
        assertEquals(numKeys + 1, map.size());
        assertTrue(map.containsKey("intern"));
    } else {
        assertEquals(numKeys, map.size());
    }
}
Also used : HashMap(java.util.HashMap) Map(java.util.Map) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Test(org.junit.Test)

Example 85 with TruffleObject

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

the class TruffleTCK method testRootNodeName.

/**
 * @since 0.15
 */
@Test
public void testRootNodeName() throws Exception {
    final int[] haltCount = new int[1];
    final String name = applyNumbers();
    final String[] actualName = new String[1];
    final PolyglotEngine engine = prepareVM(PolyglotEngine.newBuilder());
    final PolyglotEngine.Value apply = engine.findGlobalSymbol(name);
    final int value = RANDOM.nextInt(100);
    final TruffleObject fn = JavaInterop.asTruffleFunction(ObjectBinaryOperation.class, new ConstantFunction(value));
    try (DebuggerSession session = Debugger.find(engine).startSession(new SuspendedCallback() {

        public void onSuspend(SuspendedEvent ev) {
            actualName[0] = ev.getTopStackFrame().getName();
            haltCount[0] = haltCount[0] + 1;
        }
    })) {
        session.suspendNextExecution();
        apply.execute(fn).as(Number.class);
    }
    assertEquals(1, haltCount[0]);
    assertEquals(name, actualName[0]);
}
Also used : DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) SuspendedEvent(com.oracle.truffle.api.debug.SuspendedEvent) SuspendedCallback(com.oracle.truffle.api.debug.SuspendedCallback) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Test(org.junit.Test)

Aggregations

TruffleObject (com.oracle.truffle.api.interop.TruffleObject)201 Test (org.junit.Test)135 ValueHostInteropTest (com.oracle.truffle.api.test.polyglot.ValueHostInteropTest)34 InteropException (com.oracle.truffle.api.interop.InteropException)18 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)17 Specialization (com.oracle.truffle.api.dsl.Specialization)16 UnknownIdentifierException (com.oracle.truffle.api.interop.UnknownIdentifierException)14 LLVMTruffleObject (com.oracle.truffle.llvm.runtime.LLVMTruffleObject)12 ArrayTruffleObject (com.oracle.truffle.api.test.polyglot.ValueHostInteropTest.ArrayTruffleObject)10 Node (com.oracle.truffle.api.nodes.Node)9 UnsupportedTypeException (com.oracle.truffle.api.interop.UnsupportedTypeException)8 CallTarget (com.oracle.truffle.api.CallTarget)7 StackPointer (com.oracle.truffle.llvm.runtime.memory.LLVMStack.StackPointer)7 TestCallback (com.oracle.truffle.nfi.test.interop.TestCallback)7 LinkedHashMap (java.util.LinkedHashMap)7 Source (com.oracle.truffle.api.source.Source)6 Method (java.lang.reflect.Method)6 HashMap (java.util.HashMap)6 Map (java.util.Map)6 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)5