Search in sources :

Example 56 with Value

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

the class ValueHostInteropTest method arrayHasSize.

@Test
public void arrayHasSize() {
    Value arrObj = context.asValue(new String[] { "Hello", "World", "!" });
    assertTrue(arrObj.hasArrayElements());
    assertEquals("Three elements", 3L, arrObj.getArraySize());
    assertEquals("Hello", arrObj.getArrayElement(0).asString());
    assertEquals("World", arrObj.getArrayElement(1).asString());
    assertEquals("!", arrObj.getArrayElement(2).asString());
}
Also used : ValueAssert.assertValue(com.oracle.truffle.api.test.polyglot.ValueAssert.assertValue) Value(org.graalvm.polyglot.Value) Test(org.junit.Test)

Example 57 with Value

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

the class ValueHostInteropTest method arrayAsList.

@Test
public void arrayAsList() {
    String[] arr = new String[] { "Hello", "World", "!" };
    Value arrObj = context.asValue(arr);
    List<String> list = arrObj.as(LIST_STRING);
    assertEquals("Three elements", 3, list.size());
    assertEquals("Hello", list.get(0));
    assertEquals("World", list.get(1));
    assertEquals("!", list.get(2));
    list.set(1, "there");
    assertEquals("there", arr[1]);
}
Also used : ValueAssert.assertValue(com.oracle.truffle.api.test.polyglot.ValueAssert.assertValue) Value(org.graalvm.polyglot.Value) Test(org.junit.Test)

Example 58 with Value

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

the class PolyglotLauncher method runScripts.

private void runScripts(List<Script> scripts, Context.Builder contextBuilder, String[] programArgs) {
    Script mainScript = scripts.get(scripts.size() - 1);
    try (Context context = contextBuilder.arguments(mainScript.getLanguage(), programArgs).build()) {
        Engine engine = context.getEngine();
        checkLanguage(mainLanguage, engine);
        for (Script script : scripts) {
            checkLanguage(script.languageId, engine);
        }
        for (Script script : scripts) {
            try {
                Value result = context.eval(script.getSource());
                if (script.isPrintResult()) {
                    System.out.println(result);
                }
            } catch (PolyglotException e) {
                if (e.isExit()) {
                    throw exit(e.getExitStatus());
                } else if (e.isGuestException()) {
                    e.printStackTrace();
                    throw exit(1);
                } else {
                    throw abort(e);
                }
            } catch (IOException e) {
                throw abort(e);
            } catch (Throwable t) {
                throw abort(t);
            }
        }
    }
}
Also used : Context(org.graalvm.polyglot.Context) Value(org.graalvm.polyglot.Value) IOException(java.io.IOException) PolyglotException(org.graalvm.polyglot.PolyglotException) Engine(org.graalvm.polyglot.Engine)

Example 59 with Value

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

the class PolyglotNativeAPI method polyglot_value_as_int64.

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

Example 60 with Value

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

the class PolyglotNativeAPI method polyglot_value_as_float.

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

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