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

The following examples show how to use org.graalvm.polyglot.Value#asHostObject() . 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: GraalScriptRunner.java    From citeproc-java with Apache License 2.0 4 votes vote down vote up
/**
 * Recursively convert a JavaScript value to a Java object
 * @param v the value to convert
 * @return the object
 */
private static Object convert(Value v) {
    Object o = v;
    if (v.isNull()) {
        o = null;
    } else if (v.isBoolean()) {
        o = v.asBoolean();
    } else if (v.isDate()) {
        o = v.asDate();
    } else if (v.isDuration()) {
        o = v.asDuration();
    } else if (v.isHostObject()) {
        o = v.asHostObject();
    } else if (v.isInstant()) {
        o = v.asInstant();
    } else if (v.isNativePointer()) {
        o = v.asNativePointer();
    } else if (v.isNumber()) {
        if (v.fitsInInt()) {
            o = v.asInt();
        } else if (v.fitsInLong()) {
            o = v.asLong();
        } else if (v.fitsInDouble()) {
            o = v.asDouble();
        } else {
            throw new IllegalStateException("Unknown type of number");
        }
    } else if (v.isProxyObject()) {
        o = v.asProxyObject();
    } else if (v.isString()) {
        o = v.asString();
    } else if (v.isTime()) {
        o = v.asTime();
    } else if (v.isTimeZone()) {
        o = v.asTimeZone();
    } else if (v.hasArrayElements()) {
        o = convertArray(v);
    } else if (v.hasMembers()) {
        o = convertObject(v);
    }
    return o;
}