|
| 1 | +/* |
| 2 | + * Copyright (c) 2015 Oracle and/or its affiliates. All rights reserved. This |
| 3 | + * code is released under a tri EPL/GPL/LGPL license. You can use it, |
| 4 | + * redistribute it and/or modify it under the terms of the: |
| 5 | + * |
| 6 | + * Eclipse Public License version 1.0 |
| 7 | + * GNU General Public License version 2 |
| 8 | + * GNU Lesser General Public License version 2.1 |
| 9 | + */ |
| 10 | +package org.jruby.truffle.nodes.core; |
| 11 | + |
| 12 | +import com.oracle.truffle.api.CompilerDirectives; |
| 13 | +import com.oracle.truffle.api.dsl.Specialization; |
| 14 | +import com.oracle.truffle.api.nodes.Node; |
| 15 | +import com.oracle.truffle.api.object.*; |
| 16 | +import com.oracle.truffle.api.source.NullSourceSection; |
| 17 | +import com.oracle.truffle.api.source.SourceSection; |
| 18 | +import org.jruby.truffle.nodes.objects.Allocator; |
| 19 | +import org.jruby.truffle.runtime.RubyContext; |
| 20 | +import org.jruby.truffle.runtime.backtrace.Activation; |
| 21 | +import org.jruby.truffle.runtime.core.RubyBasicObject; |
| 22 | +import org.jruby.truffle.runtime.core.RubyClass; |
| 23 | +import org.jruby.truffle.runtime.core.RubyString; |
| 24 | + |
| 25 | +import java.util.EnumSet; |
| 26 | +import java.util.concurrent.locks.ReentrantLock; |
| 27 | + |
| 28 | +@CoreClass(name = "Thread::Backtrace::Location") |
| 29 | +public class ThreadBacktraceLocationNodes { |
| 30 | + |
| 31 | + private static final HiddenKey ACTIVATION_IDENTIFIER = new HiddenKey("activation"); |
| 32 | + private static final Property ACTIVATION_PROPERTY; |
| 33 | + |
| 34 | + static { |
| 35 | + Shape.Allocator allocator = RubyBasicObject.LAYOUT.createAllocator(); |
| 36 | + ACTIVATION_PROPERTY = Property.create(ACTIVATION_IDENTIFIER, allocator.locationForType(ReentrantLock.class, EnumSet.of(LocationModifier.NonNull)), 0); |
| 37 | + } |
| 38 | + |
| 39 | + public static Allocator createThreadBacktraceLocationAllocator(Shape emptyShape) { |
| 40 | + final Shape shape = emptyShape.addProperty(ACTIVATION_PROPERTY); |
| 41 | + final DynamicObjectFactory factory = shape.createFactory(); |
| 42 | + |
| 43 | + return new Allocator() { |
| 44 | + @Override |
| 45 | + public RubyBasicObject allocate(RubyContext context, RubyClass rubyClass, Node currentNode) { |
| 46 | + return new RubyBasicObject(rubyClass, factory.newInstance(new ReentrantLock())); |
| 47 | + } |
| 48 | + }; |
| 49 | + } |
| 50 | + |
| 51 | + public static void setActivation(RubyBasicObject threadBacktraceLocation, Activation activation) { |
| 52 | + assert threadBacktraceLocation.getDynamicObject().getShape().hasProperty(ACTIVATION_IDENTIFIER); |
| 53 | + |
| 54 | + try { |
| 55 | + ACTIVATION_PROPERTY.set(threadBacktraceLocation.getDynamicObject(), activation, threadBacktraceLocation.getDynamicObject().getShape()); |
| 56 | + } catch (IncompatibleLocationException | FinalLocationException e) { |
| 57 | + throw new UnsupportedOperationException(); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + protected static Activation getActivation(RubyBasicObject threadBacktraceLocation) { |
| 62 | + assert threadBacktraceLocation.getDynamicObject().getShape().hasProperty(ACTIVATION_IDENTIFIER); |
| 63 | + return (Activation) ACTIVATION_PROPERTY.get(threadBacktraceLocation.getDynamicObject(), true); |
| 64 | + } |
| 65 | + |
| 66 | + @CoreMethod(names = "absolute_path") |
| 67 | + public abstract static class AbsolutePathNode extends UnaryCoreMethodNode { |
| 68 | + |
| 69 | + public AbsolutePathNode(RubyContext context, SourceSection sourceSection) { |
| 70 | + super(context, sourceSection); |
| 71 | + } |
| 72 | + |
| 73 | + @CompilerDirectives.TruffleBoundary |
| 74 | + @Specialization |
| 75 | + public RubyString absolutePath(RubyBasicObject threadBacktraceLocation) { |
| 76 | + final Activation activation = getActivation(threadBacktraceLocation); |
| 77 | + |
| 78 | + final SourceSection sourceSection = activation.getCallNode().getEncapsulatingSourceSection(); |
| 79 | + |
| 80 | + if (sourceSection instanceof NullSourceSection) { |
| 81 | + return getContext().makeString(sourceSection.getShortDescription()); |
| 82 | + } |
| 83 | + |
| 84 | + // TODO CS 30-Apr-15: not absolute - not sure how to solve that |
| 85 | + |
| 86 | + return getContext().makeString(sourceSection.getSource().getPath()); |
| 87 | + } |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | + @CoreMethod(names = {"to_s", "inspect"}) |
| 92 | + public abstract static class ToSNode extends UnaryCoreMethodNode { |
| 93 | + |
| 94 | + public ToSNode(RubyContext context, SourceSection sourceSection) { |
| 95 | + super(context, sourceSection); |
| 96 | + } |
| 97 | + |
| 98 | + @CompilerDirectives.TruffleBoundary |
| 99 | + @Specialization |
| 100 | + public RubyString toS(RubyBasicObject threadBacktraceLocation) { |
| 101 | + final Activation activation = getActivation(threadBacktraceLocation); |
| 102 | + |
| 103 | + final SourceSection sourceSection = activation.getCallNode().getEncapsulatingSourceSection(); |
| 104 | + |
| 105 | + if (sourceSection instanceof NullSourceSection) { |
| 106 | + return getContext().makeString(sourceSection.getShortDescription()); |
| 107 | + } |
| 108 | + |
| 109 | + return getContext().makeString(String.format("%s:%d:in `%s'", |
| 110 | + sourceSection.getSource().getShortName(), |
| 111 | + sourceSection.getStartLine(), |
| 112 | + sourceSection.getIdentifier())); |
| 113 | + } |
| 114 | + |
| 115 | + } |
| 116 | + |
| 117 | +} |
0 commit comments