use of org.graalvm.polyglot.Source in project graal by oracle.
the class SLTestRunner method run.
private static void run(Context context, Path path, PrintWriter out) throws IOException {
try {
/* Parse the SL source file. */
Source source = Source.newBuilder(SLLanguage.ID, path.toFile()).interactive(true).build();
/* Call the main entry point, without any arguments. */
context.eval(source);
} catch (PolyglotException ex) {
if (!ex.isInternalError()) {
out.println(ex.getMessage());
} else {
throw ex;
}
}
}
use of org.graalvm.polyglot.Source in project graal by oracle.
the class PolyglotExceptionFrame method createGuest.
static PolyglotExceptionFrame createGuest(PolyglotExceptionImpl exception, TruffleStackTraceElement frame, boolean first) {
if (frame == null) {
return null;
}
RootNode targetRoot = frame.getTarget().getRootNode();
if (targetRoot.isInternal()) {
return null;
}
LanguageInfo info = targetRoot.getLanguageInfo();
if (info == null) {
return null;
}
PolyglotEngineImpl engine = exception.context.getEngine();
PolyglotLanguage language = engine.idToLanguage.get(info.getId());
String rootName = targetRoot.getName();
SourceSection location;
Node callNode = frame.getLocation();
if (callNode != null) {
com.oracle.truffle.api.source.SourceSection section = callNode.getEncapsulatingSourceSection();
if (section != null) {
Source source = engine.getAPIAccess().newSource(language.getId(), section.getSource());
location = engine.getAPIAccess().newSourceSection(source, section);
} else {
location = null;
}
} else {
location = first ? exception.getSourceLocation() : null;
}
return new PolyglotExceptionFrame(exception, language, location, rootName, false, null);
}
use of org.graalvm.polyglot.Source in project graal by oracle.
the class PolyglotExceptionFrame method formatSource.
private static String formatSource(SourceSection sourceSection) {
if (sourceSection == null) {
return "Unknown";
}
Source source = sourceSection.getSource();
if (source == null) {
// safety check. likely not necsssary
return "Unknown";
}
StringBuilder b = new StringBuilder();
String path = source.getPath();
if (path == null) {
b.append(source.getName());
} else {
Path pathAbsolute = Paths.get(path);
Path pathBase = new File("").getAbsoluteFile().toPath();
try {
Path pathRelative = pathBase.relativize(pathAbsolute);
b.append(pathRelative.toFile());
} catch (IllegalArgumentException e) {
b.append(source.getName());
}
}
b.append(":").append(formatIndices(sourceSection, true));
return b.toString();
}
use of org.graalvm.polyglot.Source in project graal by oracle.
the class PolyglotEngineWithJavaScript method createJavaScriptFactoryForJavaClass.
public void createJavaScriptFactoryForJavaClass() {
Source src = Source.newBuilder("js", "" + "(function(Moment) {\n" + " return function(h, m, s) {\n" + " return new Moment(h, m, s);\n" + " };\n" + "})\n", "ConstructMoment.js").buildLiteral();
// Evaluate the JavaScript function definition
final Value jsFunction = context.eval(src);
// Create a JavaScript factory for the provided Java class
final Value jsFactory = jsFunction.execute(Moment.class);
// Convert the JavaScript factory to a Java foreign function
MomentFactory momentFactory = jsFactory.as(MomentFactory.class);
final Moment javaMoment = momentFactory.create(6, 30, 10);
assertEquals("Hours", 6, javaMoment.hours);
assertEquals("Minutes", 30, javaMoment.minutes);
assertEquals("Seconds", 10, javaMoment.seconds);
}
use of org.graalvm.polyglot.Source in project graal by oracle.
the class PolyglotEngineWithJavaScript method accessFieldsOfJavaObject.
public void accessFieldsOfJavaObject() {
Source src = Source.newBuilder("js", "" + "(function(t) {\n" + " return 3600 * t.hours + 60 * t.minutes + t.seconds;\n" + "})\n", "MomentToSeconds.js").buildLiteral();
final Moment javaMoment = new Moment(6, 30, 10);
// Evaluate the JavaScript function definition
Value jsFunction = context.eval(src);
// Execute the JavaScript function, passing a Java object argument
Value jsSeconds = jsFunction.execute(javaMoment);
// Convert foreign object result to desired Java type
int seconds = jsSeconds.as(Number.class).intValue();
assertEquals(3600 * 6 + 30 * 60 + 10, seconds);
}
Aggregations