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);
}
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);
}
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"));
}
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);
}
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();
}
Aggregations