use of com.oracle.truffle.api.source.Source in project graal by oracle.
the class TruffleProfiler method getCoverage.
private Params getCoverage(Collection<CPUTracer.Payload> payloads) {
JSONObject json = new JSONObject();
Map<Source, Map<String, Collection<CPUTracer.Payload>>> sourceToRoots = new HashMap<>();
payloads.forEach(payload -> {
Map<String, Collection<CPUTracer.Payload>> rootsToPayloads = sourceToRoots.computeIfAbsent(payload.getSourceSection().getSource(), s -> new LinkedHashMap<>());
Collection<CPUTracer.Payload> pls = rootsToPayloads.computeIfAbsent(payload.getRootName(), t -> new LinkedList<>());
pls.add(payload);
});
JSONArray result = new JSONArray();
sourceToRoots.entrySet().stream().map(sourceEntry -> {
List<FunctionCoverage> functions = new ArrayList<>();
sourceEntry.getValue().entrySet().forEach(rootEntry -> {
boolean isBlockCoverage = false;
List<CoverageRange> ranges = new ArrayList<>();
for (CPUTracer.Payload payload : rootEntry.getValue()) {
isBlockCoverage |= payload.getTags().contains(StandardTags.StatementTag.class);
ranges.add(new CoverageRange(payload.getSourceSection().getCharIndex(), payload.getSourceSection().getCharEndIndex(), payload.getCount()));
}
functions.add(new FunctionCoverage(rootEntry.getKey(), isBlockCoverage, ranges.toArray(new CoverageRange[ranges.size()])));
});
int scriptId = slh.getScriptId(sourceEntry.getKey());
Script script = scriptId < 0 ? null : slh.getScript(scriptId);
return new ScriptCoverage(script != null ? script.getId() : 0, script != null ? script.getUrl() : "", functions.toArray(new FunctionCoverage[functions.size()]));
}).forEachOrdered(scriptCoverage -> {
result.put(scriptCoverage.toJSON());
});
json.put("result", result);
return new Params(json);
}
use of com.oracle.truffle.api.source.Source in project graal by oracle.
the class TruffleRuntime method compileScript.
@Override
public Params compileScript(String expression, String sourceURL, boolean persistScript, long executionContextId) throws CommandProcessException {
if (expression == null) {
throw new CommandProcessException("An expression required.");
}
JSONObject ret = new JSONObject();
ScriptsHandler sh = context.getScriptsHandler();
try {
if (sh != null) {
Source source = createSource(expression, sourceURL);
boolean parsed = false;
String[] exceptionText = new String[1];
if (context.getSuspendedInfo() != null) {
try {
parsed = context.executeInSuspendThread(new SuspendThreadExecutable<Boolean>() {
@Override
public Boolean executeCommand() throws CommandProcessException {
try {
context.getEnv().parse(source);
return true;
} catch (Exception ex) {
// Didn't manage to parse this
exceptionText[0] = ex.getLocalizedMessage();
return false;
}
}
});
} catch (GuestLanguageException ex) {
fillExceptionDetails(ret, ex);
} catch (NoSuspendedThreadException ex) {
exceptionText[0] = ex.getLocalizedMessage();
}
} else {
// Parse on the current thread will fail most likely due to a lack of context
parsed = false;
exceptionText[0] = "<Not suspended>";
}
if (parsed && persistScript) {
int id = sh.assureLoaded(source);
ret.put("scriptId", Integer.toString(id));
}
if (exceptionText[0] != null) {
JSONObject exceptionDetails = new JSONObject();
exceptionDetails.put("text", exceptionText[0]);
ret.put("exceptionDetails", exceptionDetails);
}
}
} finally {
context.releaseScriptsHandler();
}
return new Params(ret);
}
use of com.oracle.truffle.api.source.Source in project graal by oracle.
the class LoadNFILibraryTest method eval.
private static TruffleObject eval(String format, Object... args) {
Source source = Source.newBuilder(String.format(format, args)).name("LoadLibraryTest").mimeType("application/x-native").build();
CallTarget target = runWithPolyglot.getTruffleTestEnv().parse(source);
return (TruffleObject) target.call();
}
use of com.oracle.truffle.api.source.Source in project graal by oracle.
the class Context method initializeContext.
@Override
protected void initializeContext(Context context) throws Exception {
super.initializeContext(context);
Source code = context.initSource;
if (code != null) {
SourceSection outer = code.createSection(0, code.getLength());
BaseNode node = parse(code);
RootCallTarget rct = Truffle.getRuntime().createCallTarget(new InstrumentationTestRootNode(this, "", outer, node));
rct.call();
if (context.runInitAfterExec) {
context.afterTarget = rct;
}
}
}
use of com.oracle.truffle.api.source.Source in project graal by oracle.
the class Context method parse.
@Override
protected ExecutableNode parse(InlineParsingRequest request) throws Exception {
Source code = request.getSource();
Node location = request.getLocation();
if (location == null) {
throw new IllegalArgumentException("Location must not be null.");
}
BaseNode node;
try {
node = parse(code);
} catch (LanguageError e) {
throw new IOException(e);
}
return new InlineExecutableNode(this, node);
}
Aggregations