Search in sources :

Example 91 with Value

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

the class SLJavaInteropTest method sumPairInMapOfArray.

@Test
public void sumPairInMapOfArray() {
    String scriptText = // 
    "function values(sum, arr) {\n" + // 
    "  sum.sumMapArray(arr);\n" + // 
    "}\n";
    context.eval("sl", scriptText);
    Value fn = lookup("values");
    Sum javaSum = new Sum();
    TwoPairsImpl groups = new TwoPairsImpl(new PairImpl[] { new PairImpl("one", 1) }, new PairImpl[] { new PairImpl("two", 2), new PairImpl("three", 3) });
    fn.execute(javaSum, groups);
    assertEquals(6, javaSum.sum);
}
Also used : Value(org.graalvm.polyglot.Value) Test(org.junit.Test)

Example 92 with Value

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

the class SLJavaInteropTest method sumMapInArrayOfArray.

@Test
public void sumMapInArrayOfArray() {
    String scriptText = // 
    "function values(sum, arr) {\n" + // 
    "  sum.sumArrayMap(arr);\n" + // 
    "}\n";
    context.eval("sl", scriptText);
    Value fn = lookup("values");
    Sum javaSum = new Sum();
    PairImpl[][] arr = { new PairImpl[] { new PairImpl("one", 1) }, new PairImpl[] { new PairImpl("two", 2), new PairImpl("three", 3) } };
    fn.execute(javaSum, arr);
    assertEquals(6, javaSum.sum);
}
Also used : Value(org.graalvm.polyglot.Value) Test(org.junit.Test)

Example 93 with Value

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

the class SLJavaInteropTest method asFunctionWithArr.

@Test
public void asFunctionWithArr() throws Exception {
    String scriptText = // 
    "function values(a, b) {\n" + // 
    "  println(\"Called with \" + a[0] + a[1] + \" and \" + b);\n" + // 
    "}\n";
    context.eval("sl", scriptText);
    Value fn = lookup("values");
    PassInArray valuesIn = fn.as(PassInArray.class);
    valuesIn.call(new Object[] { "OK", "Fine" });
    assertEquals("Called with OKFine and null\n", os.toString("UTF-8"));
}
Also used : Value(org.graalvm.polyglot.Value) Test(org.junit.Test)

Example 94 with Value

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

the class SLJavaInteropTest method accessJavaMap.

@Test
public void accessJavaMap() {
    String scriptText = "function write(map, key, value) {\n" + "  map.put(key, value);\n" + "}\n" + "function read(map, key) {\n" + "  return map.get(key);\n" + "}\n";
    context.eval("sl", scriptText);
    Value read = lookup("read");
    Value write = lookup("write");
    Map<Object, Object> map = new HashMap<>();
    map.put("a", 42);
    Object b = read.execute(map, "a").as(Object.class);
    assertNumber(42L, b);
    write.execute(map, "a", 33);
    Object c = read.execute(map, "a").as(Object.class);
    assertNumber(33L, c);
}
Also used : HashMap(java.util.HashMap) Value(org.graalvm.polyglot.Value) Test(org.junit.Test)

Example 95 with Value

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

the class SLTCKLanguageProvider method createStatement.

private static Snippet createStatement(final Context context, final String id, final String functionName, final String expression, final TypeDescriptor returnType, TypeDescriptor... paramTypes) {
    final Object[] formalParams = new String[paramTypes.length + 1];
    formalParams[0] = "r";
    for (int i = 1; i < formalParams.length; i++) {
        formalParams[i] = "p" + i;
    }
    final String formattedExpression = MessageFormat.format(expression, formalParams);
    final Value fnc = eval(context, String.format(PATTERN_STATEMENTS[paramTypes.length], functionName, formattedExpression), functionName);
    return Snippet.newBuilder(id, fnc, returnType).parameterTypes(paramTypes).build();
}
Also used : Value(org.graalvm.polyglot.Value)

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