use of org.graalvm.polyglot.Context in project graal by oracle.
the class VirtualizedFileSystemTest method testList.
@Test
public void testList() {
final Context ctx = cfg.getContext();
final Path path = cfg.getPath();
final boolean canRead = cfg.canRead();
languageAction = (Env env) -> {
final Path folderExisting = path.resolve(FOLDER_EXISTING);
final TruffleFile file = cfg.needsURI() ? env.getTruffleFile(folderExisting.toUri()) : env.getTruffleFile(folderExisting.toString());
try {
final String expected = path.resolve(FOLDER_EXISTING).resolve(FILE_EXISTING).toString();
final Collection<? extends TruffleFile> children = file.list();
Assert.assertTrue(cfg.formatErrorMessage("Expected SecurityException"), canRead);
final Optional<String> expectedFile = children.stream().map(TruffleFile::getAbsoluteFile).map(TruffleFile::getPath).filter(expected::equals).findAny();
Assert.assertTrue(cfg.formatErrorMessage("Expected child"), expectedFile.isPresent());
} catch (SecurityException se) {
Assert.assertFalse(cfg.formatErrorMessage("Unexpected SecurityException"), canRead);
} catch (IOException ioe) {
throw new AssertionError(cfg.formatErrorMessage(ioe.getMessage()), ioe);
}
};
ctx.eval(LANGAUGE_ID, "");
}
use of org.graalvm.polyglot.Context in project graal by oracle.
the class VirtualizedFileSystemTest method isReadable.
@Test
public void isReadable() {
final Context ctx = cfg.getContext();
final Path path = cfg.getPath();
final boolean canRead = cfg.canRead();
languageAction = (Env env) -> {
final TruffleFile root = cfg.needsURI() ? env.getTruffleFile(path.toUri()) : env.getTruffleFile(path.toString());
try {
final TruffleFile file = root.resolve(FOLDER_EXISTING).resolve(FILE_EXISTING);
final boolean readable = file.isReadable();
Assert.assertTrue(cfg.formatErrorMessage("Expected SecurityException"), canRead);
Assert.assertTrue(cfg.formatErrorMessage("Is readable"), readable);
} catch (SecurityException se) {
Assert.assertFalse(cfg.formatErrorMessage("Unexpected SecurityException"), canRead);
}
};
ctx.eval(LANGAUGE_ID, "");
}
use of org.graalvm.polyglot.Context 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.Context in project graal by oracle.
the class PolyglotNativeAPI method polyglot_export_symbol.
@CEntryPoint(name = "polyglot_export_symbol")
public static PolyglotStatus polyglot_export_symbol(IsolateThread isolate_thread, PolyglotContextPointer context, CCharPointer symbol_name, PolyglotValuePointer value) {
return withHandledErrors(() -> {
Context jContext = ObjectHandles.getGlobal().get(context);
String symbolName = CTypeConversion.toJavaString(symbol_name);
jContext.getPolyglotBindings().putMember(symbolName, fetchHandle(value));
});
}
use of org.graalvm.polyglot.Context in project graal by oracle.
the class PolyglotNativeAPI method polyglot_create_int64.
@SuppressWarnings("UnnecessaryBoxing")
@CEntryPoint(name = "polyglot_create_int64")
public static PolyglotStatus polyglot_create_int64(IsolateThread isolate_thread, PolyglotContextPointer polyglot_context, long value, PolyglotValuePointerPointer result) {
return withHandledErrors(() -> {
Context ctx = ObjectHandles.getGlobal().get(polyglot_context);
result.write(createHandle(ctx.asValue(Long.valueOf(value))));
});
}
Aggregations