use of com.oracle.truffle.api.source.Source in project graal by oracle.
the class InstrumentationHandler method lazyInitializeSourcesExecutedList.
/**
* Initializes sourcesExecuted and sourcesExecutedList by populating them from executedRoots.
*/
private void lazyInitializeSourcesExecutedList() {
assert Thread.holdsLock(sourcesExecuted);
if (sourcesExecutedListRef.get() == null) {
// build the sourcesExecutedList, we need it now
Collection<Source> sourcesExecutedList = new WeakAsyncList<>(16);
sourcesExecutedListRef.set(sourcesExecutedList);
for (RootNode root : executedRoots) {
int rootBits = RootNodeBits.get(root);
if (RootNodeBits.isNoSourceSection(rootBits)) {
continue;
} else {
SourceSection sourceSection = root.getSourceSection();
if (RootNodeBits.isSameSource(rootBits) && sourceSection != null) {
Source source = sourceSection.getSource();
if (!sourcesExecuted.containsKey(source)) {
sourcesExecuted.put(source, null);
sourcesExecutedList.add(source);
}
} else {
if (sourceSection != null) {
findSourcesExecutedVisitor.adoptSource(sourceSection.getSource());
}
visitRoot(root, root, findSourcesExecutedVisitor, false);
for (Source source : findSourcesExecutedVisitor.rootSources) {
if (!sourcesExecuted.containsKey(source)) {
sourcesExecuted.put(source, null);
sourcesExecutedList.add(source);
}
}
findSourcesExecutedVisitor.rootSources.clear();
}
}
}
}
}
use of com.oracle.truffle.api.source.Source in project graal by oracle.
the class SLLanguage method parse.
@Override
protected CallTarget parse(ParsingRequest request) throws Exception {
Source source = request.getSource();
Map<String, RootCallTarget> functions;
/*
* Parse the provided source. At this point, we do not have a SLContext yet. Registration of
* the functions with the SLContext happens lazily in SLEvalRootNode.
*/
if (request.getArgumentNames().isEmpty()) {
functions = Parser.parseSL(this, source);
} else {
StringBuilder sb = new StringBuilder();
sb.append("function main(");
String sep = "";
for (String argumentName : request.getArgumentNames()) {
sb.append(sep);
sb.append(argumentName);
sep = ",";
}
sb.append(") { return ");
sb.append(request.getSource().getCharacters());
sb.append(";}");
Source decoratedSource = Source.newBuilder(sb.toString()).mimeType(request.getSource().getMimeType()).name(request.getSource().getName()).build();
functions = Parser.parseSL(this, decoratedSource);
}
RootCallTarget main = functions.get("main");
RootNode evalMain;
if (main != null) {
/*
* We have a main function, so "evaluating" the parsed source means invoking that main
* function. However, we need to lazily register functions into the SLContext first, so
* we cannot use the original SLRootNode for the main function. Instead, we create a new
* SLEvalRootNode that does everything we need.
*/
evalMain = new SLEvalRootNode(this, main, functions);
} else {
/*
* Even without a main function, "evaluating" the parsed source needs to register the
* functions into the SLContext.
*/
evalMain = new SLEvalRootNode(this, null, functions);
}
return Truffle.getRuntime().createCallTarget(evalMain);
}
use of com.oracle.truffle.api.source.Source in project graal by oracle.
the class SLDefineFunctionBuiltin method defineFunction.
@TruffleBoundary
@Specialization
public String defineFunction(String code) {
// @formatter:off
Source source = Source.newBuilder(code).name("[defineFunction]").mimeType(SLLanguage.MIME_TYPE).build();
// @formatter:on
/* The same parsing code as for parsing the initial source. */
getContext().getFunctionRegistry().register(source);
return code;
}
use of com.oracle.truffle.api.source.Source in project graal by oracle.
the class SLStatementNode method getSourceSection.
/*
* The creation of source section can be implemented lazily by looking up the root node source
* and then creating the source section object using the indices stored in the node. This avoids
* the eager creation of source section objects during parsing and creates them only when they
* are needed. Alternatively, if the language uses source sections to implement language
* semantics, then it might be more efficient to eagerly create source sections and store it in
* the AST.
*
* For more details see {@link InstrumentableNode}.
*/
@Override
@TruffleBoundary
public final SourceSection getSourceSection() {
if (sourceCharIndex == NO_SOURCE) {
// AST node without source
return null;
}
RootNode rootNode = getRootNode();
if (rootNode == null) {
// not yet adopted yet
return null;
}
SourceSection rootSourceSection = rootNode.getSourceSection();
if (rootSourceSection == null) {
return null;
}
Source source = rootSourceSection.getSource();
if (sourceCharIndex == UNAVAILABLE_SOURCE) {
return source.createUnavailableSection();
} else {
return source.createSection(sourceCharIndex, sourceLength);
}
}
use of com.oracle.truffle.api.source.Source in project sieve by jtulach.
the class Main method main.
public static void main(String[] args) throws Exception {
System.err.println("Setting up PolyglotEngine");
PolyglotEngine vm = PolyglotEngine.newBuilder().build();
vm.eval(Source.fromText("", "warmup.R").withMimeType("text/x-r"));
URL url = Main.class.getProtectionDomain().getCodeSource().getLocation();
File local = new File(url.toURI());
for (; ; ) {
File sieveInR = new File(local, "sieve.R");
if (sieveInR.exists()) {
break;
}
local = local.getParentFile();
}
System.err.println("engine is ready");
Source r = Source.fromFileName(new File(local, "sieve.R").getPath());
vm.eval(r);
}
Aggregations