Search in sources :

Example 1 with SourceSection

use of org.graalvm.polyglot.SourceSection 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);
}
Also used : RootNode(com.oracle.truffle.api.nodes.RootNode) Node(com.oracle.truffle.api.nodes.Node) RootNode(com.oracle.truffle.api.nodes.RootNode) Source(org.graalvm.polyglot.Source) LanguageInfo(com.oracle.truffle.api.nodes.LanguageInfo) SourceSection(org.graalvm.polyglot.SourceSection)

Example 2 with SourceSection

use of org.graalvm.polyglot.SourceSection in project graal by oracle.

the class PolyglotExceptionFrame method createHost.

static PolyglotExceptionFrame createHost(PolyglotExceptionImpl exception, StackTraceElement hostStack) {
    PolyglotLanguage language = exception.context.getEngine().hostLanguage;
    // source section for the host language is currently null
    // we should potentially in the future create a source section for the host language
    // however it was not clear how a host language source section would consist of.
    SourceSection location = null;
    String rootname = hostStack.getClassName() + "." + hostStack.getMethodName();
    return new PolyglotExceptionFrame(exception, language, location, rootname, true, hostStack);
}
Also used : SourceSection(org.graalvm.polyglot.SourceSection)

Example 3 with SourceSection

use of org.graalvm.polyglot.SourceSection in project graal by oracle.

the class SourceSectionListenerTest method testLoadSourceSectionImpl.

private void testLoadSourceSectionImpl(int runTimes) throws IOException {
    Instrument instrument = engine.getInstruments().get("testLoadSourceSection1");
    SourceSection[] sourceSections1 = sections("STATEMENT(EXPRESSION, EXPRESSION)", "STATEMENT(EXPRESSION, EXPRESSION)", "EXPRESSION");
    final SourceSectionFilter statementFilter = SourceSectionFilter.newBuilder().tagIs(StandardTags.StatementTag.class).build();
    final SourceSectionFilter exprFilter = SourceSectionFilter.newBuilder().tagIs(InstrumentationTestLanguage.EXPRESSION).build();
    Source source1 = sourceSections1[0].getSource();
    for (int i = 0; i < runTimes; i++) {
        run(source1);
    }
    assureEnabled(instrument);
    TestLoadSourceSection1 impl = instrument.lookup(TestLoadSourceSection1.class);
    assertSections(impl.query(SourceSectionFilter.ANY), sourceSections1);
    assertSections(impl.query(statementFilter), sourceSections1[0]);
    assertSections(impl.query(exprFilter), sourceSections1[2], sourceSections1[3]);
    SourceSection[] sourceSections2 = sections("STATEMENT(EXPRESSION)", "STATEMENT(EXPRESSION)", "EXPRESSION");
    Source source2 = sourceSections2[0].getSource();
    for (int i = 0; i < runTimes; i++) {
        run(source2);
    }
    assertEvents(impl.allEvents, merge(sourceSections1, sourceSections2));
    assertEvents(impl.onlyNewEvents, sourceSections2);
    assertEvents(impl.onlyStatements, sourceSections1[0], sourceSections2[0]);
    assertEvents(impl.onlyExpressions, sourceSections1[2], sourceSections1[3], sourceSections2[2]);
    assertSections(impl.query(SourceSectionFilter.ANY), merge(sourceSections1, sourceSections2));
    assertSections(impl.query(statementFilter), sourceSections1[0], sourceSections2[0]);
    assertSections(impl.query(exprFilter), sourceSections1[2], sourceSections1[3], sourceSections2[2]);
    // disables the instrument
    engine.close();
    engine = null;
    engine = getEngine();
    SourceSection[] sourceSections3 = sections("STATEMENT(EXPRESSION, EXPRESSION, EXPRESSION)", "STATEMENT(EXPRESSION, EXPRESSION, EXPRESSION)", "EXPRESSION");
    Source source3 = sourceSections3[0].getSource();
    for (int i = 0; i < runTimes; i++) {
        run(source3);
    }
    assertEvents(impl.allEvents, merge(sourceSections1, sourceSections2));
    assertEvents(impl.onlyNewEvents, sourceSections2);
    assertEvents(impl.onlyStatements, sourceSections1[0], sourceSections2[0]);
    assertEvents(impl.onlyExpressions, sourceSections1[2], sourceSections1[3], sourceSections2[2]);
    assertSections(impl.query(SourceSectionFilter.ANY), merge(sourceSections1, sourceSections2));
    assertSections(impl.query(statementFilter), sourceSections1[0], sourceSections2[0]);
    assertEvents(impl.onlyExpressions, sourceSections1[2], sourceSections1[3], sourceSections2[2]);
    instrument = engine.getInstruments().get("testLoadSourceSection1");
    assureEnabled(instrument);
    // new instrument needs update
    impl = instrument.lookup(TestLoadSourceSection1.class);
    assertEvents(impl.onlyNewEvents);
    assertEvents(impl.allEvents, sourceSections3);
    assertEvents(impl.onlyStatements, sourceSections3[0]);
    assertEvents(impl.onlyExpressions, sourceSections3[2], sourceSections3[3], sourceSections3[4]);
}
Also used : Instrument(org.graalvm.polyglot.Instrument) TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) SourceSection(org.graalvm.polyglot.SourceSection) SourceSectionFilter(com.oracle.truffle.api.instrumentation.SourceSectionFilter) Source(org.graalvm.polyglot.Source)

Example 4 with SourceSection

use of org.graalvm.polyglot.SourceSection in project graal by oracle.

the class SourceSectionListenerTest method sections.

private SourceSection[] sections(String code, String... match) throws IOException {
    Source source = Source.newBuilder(InstrumentationTestLanguage.ID, code, "sourceSectionTest").build();
    List<SourceSection> sections = new ArrayList<>();
    sections.add(createSection(source, 0, code.length()));
    for (String matchExpression : match) {
        int index = -1;
        while ((index = code.indexOf(matchExpression, index + 1)) != -1) {
            sections.add(createSection(source, index, matchExpression.length()));
        }
    }
    return sections.toArray(new SourceSection[0]);
}
Also used : ArrayList(java.util.ArrayList) SourceSection(org.graalvm.polyglot.SourceSection) Source(org.graalvm.polyglot.Source)

Example 5 with SourceSection

use of org.graalvm.polyglot.SourceSection in project graal by oracle.

the class SourceSectionListenerTest method assertEvents.

private static void assertEvents(List<LoadSourceSectionEvent> actualEvents, SourceSection... expectedSourceSections) {
    Assert.assertEquals(expectedSourceSections.length, actualEvents.size());
    for (int i = 0; i < expectedSourceSections.length; i++) {
        LoadSourceSectionEvent actualEvent = actualEvents.get(i);
        SourceSection expectedSourceSection = expectedSourceSections[i];
        Assert.assertEquals("index " + i, expectedSourceSection, actualEvent.getSourceSection());
        Assert.assertSame("index " + i, actualEvent.getNode().getSourceSection(), actualEvent.getSourceSection());
    }
}
Also used : LoadSourceSectionEvent(com.oracle.truffle.api.instrumentation.LoadSourceSectionEvent) SourceSection(org.graalvm.polyglot.SourceSection)

Aggregations

SourceSection (org.graalvm.polyglot.SourceSection)5 Source (org.graalvm.polyglot.Source)3 LoadSourceSectionEvent (com.oracle.truffle.api.instrumentation.LoadSourceSectionEvent)1 SourceSectionFilter (com.oracle.truffle.api.instrumentation.SourceSectionFilter)1 TruffleInstrument (com.oracle.truffle.api.instrumentation.TruffleInstrument)1 LanguageInfo (com.oracle.truffle.api.nodes.LanguageInfo)1 Node (com.oracle.truffle.api.nodes.Node)1 RootNode (com.oracle.truffle.api.nodes.RootNode)1 ArrayList (java.util.ArrayList)1 Instrument (org.graalvm.polyglot.Instrument)1