Java Code Examples for org.graalvm.polyglot.Value#as()

The following examples show how to use org.graalvm.polyglot.Value#as() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VmBridge.java    From swim with Apache License 2.0 5 votes vote down vote up
@Override
public Object guestToHost(Object guestValue) {
  Object hostValue;
  if (guestValue instanceof Value) {
    final Value value = (Value) guestValue;
    if (value.isProxyObject()) {
      hostValue = value.asProxyObject();
    } else if (value.isHostObject()) {
      hostValue = value.asHostObject();
    } else if (value.isString()) {
      hostValue = value.asString();
    } else if (value.isNumber()) {
      hostValue = value.as(Number.class);
    } else if (value.isBoolean()) {
      hostValue = value.asBoolean();
    } else if (value.isNull()) {
      hostValue = null;
    } else {
      hostValue = guestValue;
    }
  } else {
    hostValue = guestValue;
  }
  if (hostValue instanceof VmHostProxy<?>) {
    hostValue = ((VmHostProxy<?>) hostValue).unwrap();
  }
  return hostValue;
}
 
Example 2
Source File: HashemJavaInteropTest.java    From mr-hashemi with Universal Permissive License v1.0 5 votes vote down vote up
@Test
public void asFunction() throws Exception {
    String scriptText = "bebin test() {\n" + "    bechap(\"Called!\");\n" + "}\n";
    context.eval("hashemi", scriptText);
    Value main = lookup("test");
    Runnable runnable = main.as(Runnable.class);
    runnable.run();

    assertEquals("Called!\n", toUnixString(os));
}
 
Example 3
Source File: HashemJavaInteropTest.java    From mr-hashemi with Universal Permissive License v1.0 5 votes vote down vote up
@Test
public void asFunctionWithArg() throws Exception {
    String scriptText = "bebin values(a, b) {\n" + //
                    "  bechap(\"Called with \" + a + \" and \" + b);\n" + //
                    "}\n"; //
    context.eval("hashemi", scriptText);
    Value fn = lookup("values");
    PassInValues valuesIn = fn.as(PassInValues.class);
    valuesIn.call("OK", "Fine");

    assertEquals("Called with OK and Fine\n", toUnixString(os));
}
 
Example 4
Source File: HashemJavaInteropTest.java    From mr-hashemi with Universal Permissive License v1.0 5 votes vote down vote up
@Test
public void asFunctionWithArr() throws Exception {
    String scriptText = "bebin values(a, b) {\n" + //
                    "  bechap(\"Called with \" + a[0] + a[1] + \" and \" + b);\n" + //
                    "}\n"; //
    context.eval("hashemi", scriptText);
    Value fn = lookup("values");
    PassInArray valuesIn = fn.as(PassInArray.class);
    valuesIn.call(new Object[]{"OK", "Fine"});
    assertEquals("Called with OKFine and POOCH\n", toUnixString(os));
}
 
Example 5
Source File: HashemJavaInteropTest.java    From mr-hashemi with Universal Permissive License v1.0 5 votes vote down vote up
@Test
public void asFunctionWithVarArgs() throws Exception {
    String scriptText = "bebin values(a, b) {\n" + //
                    "  bechap(\"Called with \" + a + \" and \" + b);\n" + //
                    "}\n"; //
    context.eval("hashemi", scriptText);
    Value fn = lookup("values");
    PassInVarArg valuesIn = fn.as(PassInVarArg.class);

    valuesIn.call("OK", "Fine");
    assertEquals("Called with OK and Fine\n", toUnixString(os));
}
 
Example 6
Source File: HashemJavaInteropTest.java    From mr-hashemi with Universal Permissive License v1.0 5 votes vote down vote up
@Test
public void asFunctionWithArgVarArgs() throws Exception {
    String scriptText = "bebin values(a, b, c) {\n" + //
                    "  bechap(\"Called with \" + a + \" and \" + b + c);\n" + //
                    "}\n"; //
    context.eval("hashemi", scriptText);
    Value fn = lookup("values");
    PassInArgAndVarArg valuesIn = fn.as(PassInArgAndVarArg.class);

    valuesIn.call("OK", "Fine", "Well");
    assertEquals("Called with OK and FineWell\n", toUnixString(os));
}
 
Example 7
Source File: JalangiTest.java    From nodeprof.js with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithTemplate() throws IOException {
    Context context = Context.create("js");

    // TODO this walks to the NodeProf root to find emptyTemplate.js, is that how it's done?
    File dir = new File(new File(".").getAbsolutePath());
    do {
        dir = dir.getParentFile();
    } while (dir != null && dir.list() != null && !Arrays.asList(dir.list()).contains("mx.nodeprof"));
    assertNotNull(dir);
    assertTrue(dir.isDirectory());

    String templatePath = dir + "/src/ch.usi.inf.nodeprof/js/analysis/trivial/emptyTemplate.js";

    // minimal harness
    context.eval("js", "J$ = {};");

    // evaluate analysis template
    context.eval("js", Source.readFully(new FileReader(templatePath)));

    // retrieve properties (ie. the callbacks) defined in analysis object
    Value v = context.eval("js", "Object.getOwnPropertyNames(J$.analysis)");
    assertTrue(v.hasArrayElements());

    // test callbacks from template
    @SuppressWarnings("unchecked")
    List<String> callbacks = v.as(List.class);
    for (String cb : callbacks) {
        if (JalangiAnalysis.unimplementedCallbacks.contains(cb) || JalangiAnalysis.ignoredCallbacks.contains(cb)) {
            // nothing to test
            continue;
        }
        // for all other callbacks, check if they map to a tag
        assertNotNull("not in callback map: " + cb, JalangiAnalysis.callbackMap.get(cb));
        assertTrue("does not map to any tag: " + cb, JalangiAnalysis.callbackMap.get(cb).size() > 0);
    }
}
 
Example 8
Source File: GraalEngine.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private Object unbox(Value result) {
    if (result.isNull()) {
        return null;
    }
    if (result.isNumber()) {
        return result.as(Number.class);
    }
    if (result.isString()) {
        return result.as(String.class);
    }
    return result;
}
 
Example 9
Source File: PolyglotValuesConverter.java    From crate with Apache License 2.0 5 votes vote down vote up
static Object toCrateObject(Value value, DataType<?> type) {
    if (value == null) {
        return null;
    }
    switch (type.id()) {
        case ArrayType.ID:
            ArrayList<Object> items = new ArrayList<>((int) value.getArraySize());
            for (int idx = 0; idx < value.getArraySize(); idx++) {
                var item = toCrateObject(value.getArrayElement(idx), ((ArrayType) type).innerType());
                items.add(idx, item);
            }
            return type.value(items);
        case ObjectType.ID:
            return type.value(value.as(MAP_TYPE_LITERAL));
        case GeoPointType.ID:
            if (value.hasArrayElements()) {
                return type.value(toCrateObject(value, DataTypes.DOUBLE_ARRAY));
            } else {
                return type.value(value.asString());
            }
        case GeoShapeType.ID:
            if (value.isString()) {
                return type.value(value.asString());
            } else {
                return type.value(value.as(MAP_TYPE_LITERAL));
            }
        default:
            final Object polyglotValue;
            if (value.isNumber()) {
                polyglotValue = value.as(NUMBER_TYPE_LITERAL);
            } else if (value.isString()) {
                polyglotValue = value.asString();
            } else if (value.isBoolean()) {
                polyglotValue = value.asBoolean();
            } else {
                polyglotValue = value.asString();
            }
            return type.value(polyglotValue);
    }
}