From d3e406664491edc927b333794b5894ebb7e1cdfd Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Mon, 21 Nov 2016 10:57:01 +0100 Subject: [PATCH] [Truffle] Introduce SourceSectionUtils.fileLine() to replace SourceSection.getShortDescription(). * Not a good replacement for builtins as in many cases we also want the method name, which is only available from the RootNode. --- .../jruby/truffle/core/fiber/FiberNodes.java | 3 +- .../thread/ThreadBacktraceLocationNodes.java | 10 ------- .../truffle/core/thread/ThreadManager.java | 3 +- .../backtrace/BacktraceFormatter.java | 3 +- .../truffle/language/control/WhileNode.java | 5 ++-- .../language/methods/SharedMethodInfo.java | 3 +- .../objects/shared/SharedObjects.java | 6 +++- .../truffle/util/SourceSectionUtils.java | 28 +++++++++++++++++++ 8 files changed, 44 insertions(+), 17 deletions(-) create mode 100644 truffle/src/main/java/org/jruby/truffle/util/SourceSectionUtils.java diff --git a/truffle/src/main/java/org/jruby/truffle/core/fiber/FiberNodes.java b/truffle/src/main/java/org/jruby/truffle/core/fiber/FiberNodes.java index f3f023cd597..cb591e299d5 100644 --- a/truffle/src/main/java/org/jruby/truffle/core/fiber/FiberNodes.java +++ b/truffle/src/main/java/org/jruby/truffle/core/fiber/FiberNodes.java @@ -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; @@ -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(); diff --git a/truffle/src/main/java/org/jruby/truffle/core/thread/ThreadBacktraceLocationNodes.java b/truffle/src/main/java/org/jruby/truffle/core/thread/ThreadBacktraceLocationNodes.java index 38482fefeeb..65b58ddf400 100644 --- a/truffle/src/main/java/org/jruby/truffle/core/thread/ThreadBacktraceLocationNodes.java +++ b/truffle/src/main/java/org/jruby/truffle/core/thread/ThreadBacktraceLocationNodes.java @@ -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(); @@ -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(); @@ -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(), ":", diff --git a/truffle/src/main/java/org/jruby/truffle/core/thread/ThreadManager.java b/truffle/src/main/java/org/jruby/truffle/core/thread/ThreadManager.java index 48db40fef8e..3ec2b1c26fe 100644 --- a/truffle/src/main/java/org/jruby/truffle/core/thread/ThreadManager.java +++ b/truffle/src/main/java/org/jruby/truffle/core/thread/ThreadManager.java @@ -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; @@ -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); diff --git a/truffle/src/main/java/org/jruby/truffle/language/backtrace/BacktraceFormatter.java b/truffle/src/main/java/org/jruby/truffle/language/backtrace/BacktraceFormatter.java index 6f203ad4d78..a225c177165 100644 --- a/truffle/src/main/java/org/jruby/truffle/language/backtrace/BacktraceFormatter.java +++ b/truffle/src/main/java/org/jruby/truffle/language/backtrace/BacktraceFormatter.java @@ -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; @@ -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); diff --git a/truffle/src/main/java/org/jruby/truffle/language/control/WhileNode.java b/truffle/src/main/java/org/jruby/truffle/language/control/WhileNode.java index 3fac7e8de54..36708128705 100644 --- a/truffle/src/main/java/org/jruby/truffle/language/control/WhileNode.java +++ b/truffle/src/main/java/org/jruby/truffle/language/control/WhileNode.java @@ -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 { @@ -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"; } diff --git a/truffle/src/main/java/org/jruby/truffle/language/methods/SharedMethodInfo.java b/truffle/src/main/java/org/jruby/truffle/language/methods/SharedMethodInfo.java index 9c850c865bc..0135eb100de 100644 --- a/truffle/src/main/java/org/jruby/truffle/language/methods/SharedMethodInfo.java +++ b/truffle/src/main/java/org/jruby/truffle/language/methods/SharedMethodInfo.java @@ -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. @@ -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); } } diff --git a/truffle/src/main/java/org/jruby/truffle/language/objects/shared/SharedObjects.java b/truffle/src/main/java/org/jruby/truffle/language/objects/shared/SharedObjects.java index 7abef716db0..189926b2566 100644 --- a/truffle/src/main/java/org/jruby/truffle/language/objects/shared/SharedObjects.java +++ b/truffle/src/main/java/org/jruby/truffle/language/objects/shared/SharedObjects.java @@ -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; @@ -54,7 +57,8 @@ public static void shareDeclarationFrame(DynamicObject block) { final Deque 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); diff --git a/truffle/src/main/java/org/jruby/truffle/util/SourceSectionUtils.java b/truffle/src/main/java/org/jruby/truffle/util/SourceSectionUtils.java new file mode 100644 index 00000000000..7024e5b1a68 --- /dev/null +++ b/truffle/src/main/java/org/jruby/truffle/util/SourceSectionUtils.java @@ -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(); + } + } + +}