use of org.graalvm.polyglot.Value in project graal by oracle.
the class ValueHostInteropTest method arrayHasSize.
@Test
public void arrayHasSize() {
Value arrObj = context.asValue(new String[] { "Hello", "World", "!" });
assertTrue(arrObj.hasArrayElements());
assertEquals("Three elements", 3L, arrObj.getArraySize());
assertEquals("Hello", arrObj.getArrayElement(0).asString());
assertEquals("World", arrObj.getArrayElement(1).asString());
assertEquals("!", arrObj.getArrayElement(2).asString());
}
use of org.graalvm.polyglot.Value in project graal by oracle.
the class ValueHostInteropTest method arrayAsList.
@Test
public void arrayAsList() {
String[] arr = new String[] { "Hello", "World", "!" };
Value arrObj = context.asValue(arr);
List<String> list = arrObj.as(LIST_STRING);
assertEquals("Three elements", 3, list.size());
assertEquals("Hello", list.get(0));
assertEquals("World", list.get(1));
assertEquals("!", list.get(2));
list.set(1, "there");
assertEquals("there", arr[1]);
}
use of org.graalvm.polyglot.Value in project graal by oracle.
the class PolyglotLauncher method runScripts.
private void runScripts(List<Script> scripts, Context.Builder contextBuilder, String[] programArgs) {
Script mainScript = scripts.get(scripts.size() - 1);
try (Context context = contextBuilder.arguments(mainScript.getLanguage(), programArgs).build()) {
Engine engine = context.getEngine();
checkLanguage(mainLanguage, engine);
for (Script script : scripts) {
checkLanguage(script.languageId, engine);
}
for (Script script : scripts) {
try {
Value result = context.eval(script.getSource());
if (script.isPrintResult()) {
System.out.println(result);
}
} catch (PolyglotException e) {
if (e.isExit()) {
throw exit(e.getExitStatus());
} else if (e.isGuestException()) {
e.printStackTrace();
throw exit(1);
} else {
throw abort(e);
}
} catch (IOException e) {
throw abort(e);
} catch (Throwable t) {
throw abort(t);
}
}
}
}
use of org.graalvm.polyglot.Value in project graal by oracle.
the class PolyglotNativeAPI method polyglot_value_as_int64.
@CEntryPoint(name = "polyglot_value_as_int64")
public static PolyglotStatus polyglot_value_as_int64(IsolateThread isolate_thread, PolyglotValuePointer value, CLongPointer result) {
return withHandledErrors(() -> {
Value valueObject = fetchHandle(value);
result.write(valueObject.asLong());
});
}
use of org.graalvm.polyglot.Value in project graal by oracle.
the class PolyglotNativeAPI method polyglot_value_as_float.
@CEntryPoint(name = "polyglot_value_as_float")
public static PolyglotStatus polyglot_value_as_float(IsolateThread isolate_thread, PolyglotValuePointer value, CFloatPointer result) {
return withHandledErrors(() -> {
Value dataObject = fetchHandle(value);
result.write(dataObject.asFloat());
});
}
Aggregations