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

The following examples show how to use org.graalvm.polyglot.Value#asString() . 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: 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);
    }
}
 
Example 3
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;
}