Skip to content

Commit

Permalink
[Truffle] Introduce SourceSectionUtils.fileLine() to replace SourceSe…
Browse files Browse the repository at this point in the history
…ction.getShortDescription().

* Not a good replacement for builtins as in many cases
  we also want the method name, which is only available from the RootNode.
  • Loading branch information
eregon committed Nov 21, 2016
1 parent a4727a2 commit d3e4066
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 17 deletions.
Expand Up @@ -37,6 +37,7 @@
import org.jruby.truffle.language.control.ReturnException;
import org.jruby.truffle.language.methods.UnsupportedOperationBehavior;
import org.jruby.truffle.platform.UnsafeGroup;
import org.jruby.truffle.util.SourceSectionUtils;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
Expand Down Expand Up @@ -69,7 +70,7 @@ private static DynamicObject createFiber(RubyContext context, DynamicObject thre

public static void initialize(final RubyContext context, final DynamicObject fiber, final DynamicObject block, final Node currentNode) {
final SourceSection sourceSection = Layouts.PROC.getSharedMethodInfo(block).getSourceSection();
final String name = String.format("Ruby Fiber@%s:%d", sourceSection.getSource().getName(), sourceSection.getStartLine());
final String name = "Ruby Fiber@" + SourceSectionUtils.fileLine(sourceSection);
final Thread thread = new Thread(() -> handleFiberExceptions(context, fiber, block, currentNode));
thread.setName(name);
thread.start();
Expand Down
Expand Up @@ -41,9 +41,6 @@ public DynamicObject absolutePath(DynamicObject threadBacktraceLocation) {

final SourceSection sourceSection = activation.getCallNode().getEncapsulatingSourceSection();
final Source source = sourceSection.getSource();
if (source == null) {
return coreStrings().UNKNOWN.createInstance();
}

// Get absolute path
final String path = source.getPath();
Expand Down Expand Up @@ -71,9 +68,6 @@ public DynamicObject path(DynamicObject threadBacktraceLocation) {

final SourceSection sourceSection = activation.getCallNode().getEncapsulatingSourceSection();
final Source source = sourceSection.getSource();
if (source == null) {
return coreStrings().UNKNOWN.createInstance();
}

// Get file path except for the main script
final String path = source.getName();
Expand Down Expand Up @@ -135,10 +129,6 @@ public DynamicObject toS(DynamicObject threadBacktraceLocation) {

final SourceSection sourceSection = callNode.getEncapsulatingSourceSection();

if (sourceSection.getSource() == null) {
return createString(StringOperations.encodeRope(String.format("%s:%d", sourceSection.getSource().getName(), sourceSection.getStartLine()), UTF8Encoding.INSTANCE));
}

return createString(RopeOperations.format(getContext(),
sourceSection.getSource().getName(),
":",
Expand Down
Expand Up @@ -30,6 +30,7 @@
import org.jruby.truffle.language.control.ReturnException;
import org.jruby.truffle.language.control.ThreadExitException;
import org.jruby.truffle.language.objects.shared.SharedObjects;
import org.jruby.truffle.util.SourceSectionUtils;

import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -98,7 +99,7 @@ public static void initialize(final DynamicObject thread, RubyContext context, N
}

final SourceSection sourceSection = Layouts.PROC.getSharedMethodInfo(block).getSourceSection();
final String info = String.format("%s:%d", sourceSection.getSource().getName(), sourceSection.getStartLine());
final String info = SourceSectionUtils.fileLine(sourceSection);
initialize(thread, context, currentNode, info, () -> {
final Object value = ProcOperations.rootCall(block, arguments);
Layouts.THREAD.setValue(thread, value);
Expand Down
Expand Up @@ -21,6 +21,7 @@
import org.jruby.truffle.language.RubyRootNode;
import org.jruby.truffle.language.control.RaiseException;
import org.jruby.truffle.language.loader.SourceLoader;
import org.jruby.truffle.util.SourceSectionUtils;
import org.jruby.truffle.util.StringUtils;

import java.io.PrintWriter;
Expand Down Expand Up @@ -263,7 +264,7 @@ private String formatForeign(Node callNode) {
final SourceSection sourceSection = callNode.getEncapsulatingSourceSection();

if (sourceSection != null) {
final String shortDescription = String.format("%s:%d", sourceSection.getSource().getName(), sourceSection.getStartLine());
final String shortDescription = SourceSectionUtils.fileLine(sourceSection);


builder.append(shortDescription);
Expand Down
Expand Up @@ -21,6 +21,7 @@
import org.jruby.truffle.core.cast.BooleanCastNode;
import org.jruby.truffle.core.cast.BooleanCastNodeGen;
import org.jruby.truffle.language.RubyNode;
import org.jruby.truffle.util.SourceSectionUtils;

public final class WhileNode extends RubyNode {

Expand Down Expand Up @@ -67,8 +68,8 @@ public WhileRepeatingBaseNode(RubyContext context, RubyNode condition, RubyNode
@Override
public String toString() {
SourceSection sourceSection = getEncapsulatingSourceSection();
if (sourceSection != null && sourceSection.getSource() != null) {
return String.format("while loop at %s:%d", sourceSection.getSource().getName(), sourceSection.getStartLine());
if (sourceSection != null && sourceSection.isAvailable()) {
return "while loop at " + SourceSectionUtils.fileLine(sourceSection);
} else {
return "while loop";
}
Expand Down
Expand Up @@ -14,6 +14,7 @@
import org.jruby.runtime.ArgumentDescriptor;
import org.jruby.truffle.Layouts;
import org.jruby.truffle.language.LexicalScope;
import org.jruby.truffle.util.SourceSectionUtils;

/**
* {@link InternalMethod} objects are copied as properties such as visibility are changed.
Expand Down Expand Up @@ -143,7 +144,7 @@ public String getDescriptiveNameAndSource() {
if (sourceSection == null || !sourceSection.isAvailable()) {
return getDescriptiveName();
} else {
return String.format("%s %s:%d", getDescriptiveName(), sourceSection.getSource().getName(), sourceSection.getStartLine());
return getDescriptiveName() + " " + SourceSectionUtils.fileLine(sourceSection);
}
}

Expand Down
Expand Up @@ -13,10 +13,13 @@
import com.oracle.truffle.api.frame.MaterializedFrame;
import com.oracle.truffle.api.object.DynamicObject;
import com.oracle.truffle.api.object.Shape;
import com.oracle.truffle.api.source.SourceSection;

import org.jruby.truffle.Layouts;
import org.jruby.truffle.RubyContext;
import org.jruby.truffle.language.Options;
import org.jruby.truffle.language.objects.ObjectGraph;
import org.jruby.truffle.util.SourceSectionUtils;

import java.util.ArrayDeque;
import java.util.Deque;
Expand Down Expand Up @@ -54,7 +57,8 @@ public static void shareDeclarationFrame(DynamicObject block) {
final Deque<DynamicObject> stack = new ArrayDeque<>();

if (Options.SHARED_OBJECTS_DEBUG) {
System.err.println("Sharing decl frame of " + Layouts.PROC.getSharedMethodInfo(block).getSourceSection());
final SourceSection sourceSection = Layouts.PROC.getSharedMethodInfo(block).getSourceSection();
System.err.println("Sharing decl frame of " + SourceSectionUtils.fileLine(sourceSection));
}

final MaterializedFrame declarationFrame = Layouts.PROC.getDeclarationFrame(block);
Expand Down
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2016 Oracle and/or its affiliates. All rights reserved. This
* code is released under a tri EPL/GPL/LGPL license. You can use it,
* redistribute it and/or modify it under the terms of the:
*
* Eclipse Public License version 1.0
* GNU General Public License version 2
* GNU Lesser General Public License version 2.1
*/
package org.jruby.truffle.util;

import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.source.Source;
import com.oracle.truffle.api.source.SourceSection;

public abstract class SourceSectionUtils {

@TruffleBoundary
public static String fileLine(SourceSection section) {
Source source = section.getSource();
if (section.isAvailable()) {
return String.format("%s:%d", source.getName(), section.getStartLine());
} else {
return source.getName();
}
}

}

0 comments on commit d3e4066

Please sign in to comment.