Search in sources :

Example 71 with Value

use of org.graalvm.polyglot.Value in project graal by oracle.

the class PolyglotNativeAPI method polyglot_execute.

@CEntryPoint(name = "polyglot_execute")
public static PolyglotStatus polyglot_execute(IsolateThread isolate_thread, PolyglotValuePointer value_handle, PolyglotValuePointerPointer args, int args_size, PolyglotValuePointerPointer return_value) {
    return withHandledErrors(() -> {
        Value function = fetchHandle(value_handle);
        Object[] jArgs = new Object[args_size];
        for (int i = 0; i < args_size; i++) {
            PolyglotValuePointer handle = args.read(i);
            jArgs[i] = fetchHandle(handle);
        }
        Value result = function.execute(jArgs);
        return_value.write(createHandle(result));
    });
}
Also used : Value(org.graalvm.polyglot.Value) ProxyObject(org.graalvm.polyglot.proxy.ProxyObject) PolyglotValuePointer(org.graalvm.polyglot.nativeapi.PolyglotNativeAPITypes.PolyglotValuePointer) CEntryPoint(org.graalvm.nativeimage.c.function.CEntryPoint) CEntryPoint(org.graalvm.nativeimage.c.function.CEntryPoint)

Example 72 with Value

use of org.graalvm.polyglot.Value in project graal by oracle.

the class PolyglotNativeAPI method polyglot_value_as_int8.

@CEntryPoint(name = "polyglot_value_as_int8")
public static PolyglotStatus polyglot_value_as_int8(IsolateThread isolate_thread, PolyglotValuePointer value, CCharPointer result) {
    return withHandledErrors(() -> {
        Value valueObject = fetchHandle(value);
        result.write(valueObject.asByte());
    });
}
Also used : Value(org.graalvm.polyglot.Value) CEntryPoint(org.graalvm.nativeimage.c.function.CEntryPoint)

Example 73 with Value

use of org.graalvm.polyglot.Value in project graal by oracle.

the class PolyglotNativeAPI method polyglot_create_function.

@CEntryPoint(name = "polyglot_create_function")
public static PolyglotStatus polyglot_create_function(IsolateThread isolate_thread, PolyglotContextPointer polyglot_context, PolyglotCallbackPointer callback, VoidPointer data, PolyglotValuePointerPointer value) {
    return withHandledErrors(() -> {
        Context c = ObjectHandles.getGlobal().get(polyglot_context);
        ProxyExecutable executable = (Value... arguments) -> {
            ObjectHandle[] handleArgs = new ObjectHandle[arguments.length];
            for (int i = 0; i < arguments.length; i++) {
                handleArgs[i] = createHandle(arguments[i]);
            }
            PolyglotCallbackInfo cbInfo = (PolyglotCallbackInfo) createHandle(new PolyglotCallbackInfoInternal(handleArgs, data));
            try {
                PolyglotValuePointer result = callback.invoke(CEntryPointContext.getCurrentIsolateThread(), cbInfo);
                CallbackException ce = exceptionsTL.get();
                if (ce != null) {
                    exceptionsTL.remove();
                    throw ce;
                } else {
                    return PolyglotNativeAPI.fetchHandle(result);
                }
            } finally {
                PolyglotCallbackInfoInternal info = fetchHandle(cbInfo);
                for (ObjectHandle arg : info.arguments) {
                    freeHandle(arg);
                }
                freeHandle(cbInfo);
            }
        };
        value.write(createHandle(c.asValue(executable)));
    });
}
Also used : CEntryPointContext(org.graalvm.nativeimage.c.function.CEntryPointContext) Context(org.graalvm.polyglot.Context) PolyglotCallbackInfo(org.graalvm.polyglot.nativeapi.PolyglotNativeAPITypes.PolyglotCallbackInfo) ProxyExecutable(org.graalvm.polyglot.proxy.ProxyExecutable) ObjectHandle(org.graalvm.nativeimage.ObjectHandle) Value(org.graalvm.polyglot.Value) PolyglotValuePointer(org.graalvm.polyglot.nativeapi.PolyglotNativeAPITypes.PolyglotValuePointer) CEntryPoint(org.graalvm.nativeimage.c.function.CEntryPoint)

Example 74 with Value

use of org.graalvm.polyglot.Value in project graal by oracle.

the class ToJavaNode method convertImpl.

private Object convertImpl(Object value, Class<?> targetType, Type genericType, Object languageContext) {
    Object convertedValue;
    if (isAssignableFromTrufflePrimitiveType(targetType)) {
        convertedValue = primitive.toPrimitive(value, targetType);
        if (convertedValue != null) {
            return convertedValue;
        } else if (targetType == char.class || targetType == Character.class) {
            Integer safeChar = primitive.toInteger(value);
            if (safeChar != null) {
                int v = safeChar;
                if (v >= 0 && v < 65536) {
                    return (char) v;
                }
            }
        }
    }
    if (targetType == Value.class && languageContext != null) {
        convertedValue = value instanceof Value ? value : JavaInterop.toHostValue(value, languageContext);
    } else if (value instanceof TruffleObject) {
        convertedValue = asJavaObject((TruffleObject) value, targetType, genericType, languageContext);
    } else if (targetType.isAssignableFrom(value.getClass())) {
        convertedValue = value;
    } else {
        CompilerDirectives.transferToInterpreter();
        String reason;
        if (isAssignableFromTrufflePrimitiveType(targetType)) {
            reason = "Invalid or lossy primitive coercion.";
        } else {
            reason = "Unsupported target type.";
        }
        throw JavaInteropErrors.cannotConvert(languageContext, value, targetType, reason);
    }
    return convertedValue;
}
Also used : Value(org.graalvm.polyglot.Value) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) TruffleObject(com.oracle.truffle.api.interop.TruffleObject)

Example 75 with Value

use of org.graalvm.polyglot.Value in project graal by oracle.

the class SLInstrumentTest method testChangeArgumentsOnReenter.

/**
 * Test that we can alter function arguments on reenter.
 */
@Test
public void testChangeArgumentsOnReenter() throws Exception {
    String code = "function main() {\n" + "  y = fce(0, 10000);\n" + "  return y;\n" + "}\n" + "function fce(x, z) {\n" + "  y = 2 * x;\n" + "  if (y < z) {\n" + "    print(\"A bad error.\");\n" + "    return 0 - 1;\n" + "  } else {\n" + "    return y;\n" + "  }\n" + "}\n";
    final Source source = Source.newBuilder("sl", code, "testing").build();
    Context context = Context.create();
    IncreaseArgOnErrorInstrument incOnError = context.getEngine().getInstruments().get("testIncreaseArgumentOnError").lookup(IncreaseArgOnErrorInstrument.class);
    incOnError.attachOn("A bad error");
    Value ret = context.eval(source);
    assertEquals(10000, ret.asInt());
}
Also used : Context(org.graalvm.polyglot.Context) EventContext(com.oracle.truffle.api.instrumentation.EventContext) Value(org.graalvm.polyglot.Value) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Aggregations

Value (org.graalvm.polyglot.Value)277 Test (org.junit.Test)203 ValueAssert.assertValue (com.oracle.truffle.api.test.polyglot.ValueAssert.assertValue)65 Context (org.graalvm.polyglot.Context)58 BoxedTestValue (com.oracle.truffle.llvm.test.interop.values.BoxedTestValue)43 PolyglotException (org.graalvm.polyglot.PolyglotException)42 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)34 ProxyObject (org.graalvm.polyglot.proxy.ProxyObject)34 Source (org.graalvm.polyglot.Source)31 ArrayList (java.util.ArrayList)30 CEntryPoint (org.graalvm.nativeimage.c.function.CEntryPoint)23 ProxyExecutable (org.graalvm.polyglot.proxy.ProxyExecutable)18 HashMap (java.util.HashMap)14 TruffleContext (com.oracle.truffle.api.TruffleContext)11 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)11 NullValue (com.oracle.truffle.llvm.test.interop.values.NullValue)11 UnsupportedTypeException (com.oracle.truffle.api.interop.UnsupportedTypeException)10 DebugValue (com.oracle.truffle.api.debug.DebugValue)9 LanguageContext (com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext)9 List (java.util.List)9