Search in sources :

Example 96 with TruffleObject

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

the class LLVMTruffleHandleToManaged method doIntrinsic.

@Specialization
protected LLVMTruffleObject doIntrinsic(Object rawHandle, @Cached("getContextReference()") ContextReference<LLVMContext> context, @Cached("createToNativeWithTarget()") LLVMToNativeNode forceAddressNode) {
    LLVMAddress handle = forceAddressNode.executeWithTarget(rawHandle);
    TruffleObject object = context.get().getManagedObjectForHandle(handle);
    return new LLVMTruffleObject(object);
}
Also used : LLVMAddress(com.oracle.truffle.llvm.runtime.LLVMAddress) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) LLVMTruffleObject(com.oracle.truffle.llvm.runtime.LLVMTruffleObject) LLVMTruffleObject(com.oracle.truffle.llvm.runtime.LLVMTruffleObject) Specialization(com.oracle.truffle.api.dsl.Specialization)

Example 97 with TruffleObject

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

the class LLVMTruffleReadNBytes method doIntrinsic.

@Specialization
protected Object doIntrinsic(LLVMAddress value, int n, @Cached("getLLVMMemory()") LLVMMemory memory, @Cached("getContextReference()") ContextReference<LLVMContext> ctxRef) {
    int count = n < 0 ? 0 : n;
    byte[] bytes = new byte[count];
    long ptr = value.getVal();
    for (int i = 0; i < count; i++) {
        bytes[i] = memory.getI8(ptr);
        ptr += Byte.BYTES;
    }
    TruffleObject ret = (TruffleObject) ctxRef.get().getEnv().asGuestValue(bytes);
    return new LLVMTruffleObject(LLVMTypedForeignObject.createUnknown(ret));
}
Also used : TruffleObject(com.oracle.truffle.api.interop.TruffleObject) LLVMTruffleObject(com.oracle.truffle.llvm.runtime.LLVMTruffleObject) LLVMTruffleObject(com.oracle.truffle.llvm.runtime.LLVMTruffleObject) Specialization(com.oracle.truffle.api.dsl.Specialization)

Example 98 with TruffleObject

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

the class LLVMTruffleReadNString method interop.

@Specialization
protected Object interop(LLVMTruffleObject objectWithOffset, int n, @Cached("createForeignReadNode()") Node foreignRead, @Cached("createToByteNode()") ForeignToLLVM toLLVM) {
    long offset = objectWithOffset.getOffset();
    TruffleObject object = objectWithOffset.getObject();
    char[] chars = new char[n];
    for (int i = 0; i < n; i++) {
        Object rawValue;
        try {
            rawValue = ForeignAccess.sendRead(foreignRead, object, offset + i);
        } catch (UnknownIdentifierException | UnsupportedMessageException e) {
            CompilerDirectives.transferToInterpreter();
            throw new IllegalStateException(e);
        }
        byte byteValue = (byte) toLLVM.executeWithTarget(rawValue);
        chars[i] = (char) Byte.toUnsignedInt(byteValue);
    }
    return new String(chars);
}
Also used : UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) UnknownIdentifierException(com.oracle.truffle.api.interop.UnknownIdentifierException) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) LLVMTruffleObject(com.oracle.truffle.llvm.runtime.LLVMTruffleObject) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) LLVMTruffleObject(com.oracle.truffle.llvm.runtime.LLVMTruffleObject) Specialization(com.oracle.truffle.api.dsl.Specialization)

Example 99 with TruffleObject

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

the class LLVMPolyglotExport method doExport.

@Specialization
protected Object doExport(Object name, Object value, @Cached("getContextReference()") ContextReference<LLVMContext> ctxRef) {
    String symbolName = readString.executeWithTarget(name);
    LLVMContext ctx = ctxRef.get();
    Object escaped = escape.executeWithTarget(value);
    try {
        ForeignAccess.sendWrite(write, (TruffleObject) ctx.getEnv().getPolyglotBindings(), symbolName, escaped);
    } catch (InteropException ex) {
        throw ex.raise();
    }
    return null;
}
Also used : LLVMContext(com.oracle.truffle.llvm.runtime.LLVMContext) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) InteropException(com.oracle.truffle.api.interop.InteropException) Specialization(com.oracle.truffle.api.dsl.Specialization)

Example 100 with TruffleObject

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

the class LLVMTruffleExecute method doExecute.

@ExplodeLoop
private Object doExecute(VirtualFrame frame, TruffleObject value, LLVMContext context, LLVMGetStackNode getStack) {
    Object[] evaluatedArgs = new Object[args.length];
    for (int i = 0; i < args.length; i++) {
        evaluatedArgs[i] = prepareValuesForEscape[i].executeWithTarget(args[i].executeGeneric(frame));
    }
    try {
        LLVMStack stack = getStack.executeWithTarget(getThreadingStack(context), Thread.currentThread());
        Object rawValue;
        try (StackPointer save = stack.newFrame()) {
            rawValue = ForeignAccess.sendExecute(foreignExecute, value, evaluatedArgs);
        }
        return toLLVM.executeWithTarget(rawValue);
    } catch (UnsupportedMessageException | UnsupportedTypeException | ArityException e) {
        CompilerDirectives.transferToInterpreter();
        throw new IllegalStateException(e);
    }
}
Also used : UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) UnsupportedTypeException(com.oracle.truffle.api.interop.UnsupportedTypeException) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) LLVMTruffleObject(com.oracle.truffle.llvm.runtime.LLVMTruffleObject) StackPointer(com.oracle.truffle.llvm.runtime.memory.LLVMStack.StackPointer) ArityException(com.oracle.truffle.api.interop.ArityException) LLVMStack(com.oracle.truffle.llvm.runtime.memory.LLVMStack) ExplodeLoop(com.oracle.truffle.api.nodes.ExplodeLoop)

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