Class ThreadContext

java.lang.Object
org.jruby.runtime.ThreadContext

public final class ThreadContext extends Object
  • Field Details

    • LOG

      private static final Logger LOG
    • INITIAL_SIZE

      private static final int INITIAL_SIZE
      See Also:
    • INITIAL_FRAMES_SIZE

      private static final int INITIAL_FRAMES_SIZE
      See Also:
    • CALL_POLL_COUNT

      private static final int CALL_POLL_COUNT
      The number of calls after which to do a thread event poll
      See Also:
    • runtime

      public final Ruby runtime
    • nil

      public final IRubyObject nil
    • tru

      public final RubyBoolean tru
    • fals

      public final RubyBoolean fals
    • runtimeCache

      public final RuntimeCache runtimeCache
    • traceFuncHook

      private Ruby.CallTraceFuncHook traceFuncHook
    • isWithinTrace

      private boolean isWithinTrace
    • thread

      private RubyThread thread
    • NULL_FIBER_REF

      private static final WeakReference<ThreadFiber> NULL_FIBER_REF
    • fiber

      private WeakReference<ThreadFiber> fiber
    • rootFiber

      private ThreadFiber rootFiber
    • dateFormatter

      private RubyDateFormatter dateFormatter
    • frameStack

      private Frame[] frameStack
    • frameIndex

      private int frameIndex
    • backtrace

      private BacktraceElement[] backtrace
    • backtraceIndex

      private int backtraceIndex
    • scopeStack

      private DynamicScope[] scopeStack
    • scopeIndex

      private int scopeIndex
    • EMPTY_CATCHTARGET_STACK

      private static final CatchThrow[] EMPTY_CATCHTARGET_STACK
    • catchStack

      private CatchThrow[] catchStack
    • catchIndex

      private int catchIndex
    • isProfiling

      private boolean isProfiling
    • profileCollection

      private ProfileCollection profileCollection
    • eventHooksEnabled

      private boolean eventHooksEnabled
    • lastCallType

      CallType lastCallType
    • lastVisibility

      Visibility lastVisibility
    • lastExitStatus

      IRubyObject lastExitStatus
    • savedExcInLambda

      private Throwable savedExcInLambda
    • secureRandom

      @Deprecated public transient SecureRandom secureRandom
      Deprecated.
      This fields is no longer initialized, is null by default! Use getSecureRandom() instead.
    • tryPreferredPRNG

      private static boolean tryPreferredPRNG
    • trySHA1PRNG

      private static boolean trySHA1PRNG
    • privateConstantReference

      private RubyModule privateConstantReference
    • sites

      public final JavaSites sites
    • matchData

      private RubyMatchData matchData
    • encodingHolder

      private org.jcodings.Encoding[] encodingHolder
    • callNumber

      public int callNumber
    • WALKER

      public static final com.headius.backport9.stack.StackWalker WALKER
    • WALKER8

      public static final com.headius.backport9.stack.StackWalker WALKER8
    • currentMethodSerial

      private int currentMethodSerial
    • symToGuards

      private Map<String,Map<IRubyObject,IRubyObject>> symToGuards
    • recursiveSet

      private Set<RecursiveComparator.Pair> recursiveSet
    • exceptionRequiresBacktrace

      public boolean exceptionRequiresBacktrace
  • Constructor Details

    • ThreadContext

      private ThreadContext(Ruby runtime)
      Constructor for Context.
  • Method Details

    • newContext

      public static ThreadContext newContext(Ruby runtime)
    • getSecureRandom

      public SecureRandom getSecureRandom()
    • finalize

      protected void finalize() throws Throwable
      Overrides:
      finalize in class Object
      Throws:
      Throwable
    • getRuntime

      public final Ruby getRuntime()
      Retrieve the runtime associated with this context. Note that there's no reason to call this method rather than accessing the runtime field directly.
      Returns:
      the runtime associated with this context
      See Also:
    • getErrorInfo

      public IRubyObject getErrorInfo()
    • setErrorInfo

      public IRubyObject setErrorInfo(IRubyObject errorInfo)
    • getSavedExceptionInLambda

      public Throwable getSavedExceptionInLambda()
    • setSavedExceptionInLambda

      public void setSavedExceptionInLambda(Throwable e)
    • getLastCallType

      public CallType getLastCallType()
    • getLastVisibility

      public Visibility getLastVisibility()
    • setLastCallStatusAndVisibility

      public void setLastCallStatusAndVisibility(CallType callType, Visibility visibility)
    • getLastExitStatus

      public IRubyObject getLastExitStatus()
    • setLastExitStatus

      public void setLastExitStatus(IRubyObject lastExitStatus)
    • printScope

      public void printScope()
    • getCurrentScope

      public DynamicScope getCurrentScope()
    • getCurrentStaticScope

      public StaticScope getCurrentStaticScope()
    • expandFrameStack

      private void expandFrameStack()
    • fillNewFrameStack

      private Frame[] fillNewFrameStack(Frame[] newFrameStack, int newSize)
    • pushScope

      public void pushScope(DynamicScope scope)
    • pushNewScope

      public DynamicScope pushNewScope(StaticScope staticScope)
    • popScope

      public void popScope()
    • expandScopeStack

      private void expandScopeStack()
    • getThread

      public RubyThread getThread()
    • getFiberCurrentThread

      public RubyThread getFiberCurrentThread()
    • getRubyDateFormatter

      public RubyDateFormatter getRubyDateFormatter()
    • setThread

      public void setThread(RubyThread thread)
    • getFiber

      public ThreadFiber getFiber()
    • setFiber

      public void setFiber(ThreadFiber fiber)
    • useRecursionGuardsFrom

      public void useRecursionGuardsFrom(ThreadContext context)
      Fibers must use the same recursion guards as their parent thread.
    • setRootFiber

      public void setRootFiber(ThreadFiber rootFiber)
    • expandCatchStack

      private void expandCatchStack()
    • pushCatch

      @Deprecated public void pushCatch(RubyContinuation.Continuation catchTarget)
      Deprecated.
    • pushCatch

      public void pushCatch(CatchThrow catchTarget)
    • popCatch

      public void popCatch()
    • getActiveCatch

      public CatchThrow getActiveCatch(Object tag)
      Find the active Continuation for the given tag. Must be called with an interned string.
      Parameters:
      tag - The interned string to search for
      Returns:
      The continuation associated with this tag
    • pushFrame

      private Frame pushFrame(Frame frame)
    • pushEvalSimpleFrame

      public void pushEvalSimpleFrame(IRubyObject executeObject)
    • pushCallFrame

      private void pushCallFrame(RubyModule clazz, String name, IRubyObject self, Block block)
    • pushCallFrame

      private void pushCallFrame(RubyModule clazz, String name, IRubyObject self, Visibility visibility, Block block)
    • pushBackrefFrame

      private void pushBackrefFrame()
    • popBackrefFrame

      private void popBackrefFrame()
    • pushEvalFrame

      private void pushEvalFrame(IRubyObject self)
    • pushFrame

      public void pushFrame()
    • popFrame

      public void popFrame()
    • popFrameReal

      private void popFrameReal(Frame oldFrame)
    • getCurrentFrame

      public Frame getCurrentFrame()
    • getNextFrame

      public Frame getNextFrame()
    • getPreviousFrame

      public Frame getPreviousFrame()
    • clearBackRef

      public IRubyObject clearBackRef()
      Set the $~ (backref) "global" to nil.
      Returns:
      nil
    • updateBackref

      public IRubyObject updateBackref()
      Update the current frame's backref using the current thread-local match, or clear it if that match is null.
      Returns:
      The current match, or nil
    • setBackRef

      public IRubyObject setBackRef(RubyMatchData match)
      Set the $~ (backref) "global" to the given RubyMatchData value. The value will be marked as "in use" since it can now be seen across threads that share the current frame.
      Parameters:
      match - the value to set
      Returns:
      the value passed in
    • getBackRef

      public IRubyObject getBackRef()
      Get the value of the $~ (backref) "global".
      Returns:
      the value of $~
    • last_match

      public IRubyObject last_match()
      MRI: rb_reg_last_match
    • match_pre

      public IRubyObject match_pre()
      MRI: rb_reg_match_pre
    • match_post

      public IRubyObject match_post()
      MRI: rb_reg_match_post
    • match_last

      public IRubyObject match_last()
      MRI: rb_reg_match_last
    • setLastLine

      public IRubyObject setLastLine(IRubyObject last)
      Set the $_ (lastlne) "global" to the given value.
      Parameters:
      last - the value to set
      Returns:
      the value passed in
    • getLastLine

      public IRubyObject getLastLine()
      Get the value of the $_ (lastline) "global".
      Returns:
      the value of $_
    • expandBacktraceStack

      private static void expandBacktraceStack(ThreadContext context)
    • fillNewBacktrace

      private static BacktraceElement[] fillNewBacktrace(ThreadContext context, BacktraceElement[] newBacktrace, int newSize)
    • pushBacktrace

      public static void pushBacktrace(ThreadContext context, String method, String file, int line)
    • popBacktrace

      public static void popBacktrace(ThreadContext context)
    • hasAnyScopes

      public boolean hasAnyScopes()
    • scopeExistsOnCallStack

      public boolean scopeExistsOnCallStack(DynamicScope scope)
      Check if a scope is present on the call stack. This is the IR equivalent of isJumpTargetAlive
      Parameters:
      scope - the scope to look for
      Returns:
      true if it exists. otherwise false.
    • getFrameName

      public String getFrameName()
    • getFrameSelf

      public IRubyObject getFrameSelf()
    • getFrameKlazz

      public RubyModule getFrameKlazz()
    • getFrameBlock

      public Block getFrameBlock()
    • getFile

      public String getFile()
    • getLine

      public int getLine()
    • setLine

      public void setLine(int line)
    • setFileAndLine

      public void setFileAndLine(String file, int line)
    • getCurrentVisibility

      public Visibility getCurrentVisibility()
    • setCurrentVisibility

      public void setCurrentVisibility(Visibility visibility)
    • pollThreadEvents

      public void pollThreadEvents()
    • getCurrentTarget

      public int getCurrentTarget()
    • callThreadPoll

      public void callThreadPoll()
    • blockingThreadPoll

      public void blockingThreadPoll()
      Poll for thread events that should be fired before a blocking call. See vm_check_ints_blocking and RUBY_VM_CHECK_INTS_BLOCKING in CRuby.
    • callThreadPoll

      public static void callThreadPoll(ThreadContext context)
    • trace

      public void trace(RubyEvent event, String name, RubyModule implClass)
    • trace

      public void trace(RubyEvent event, String name, RubyModule implClass, String file, int line)
    • getConstant

      @Deprecated public IRubyObject getConstant(String internedName)
      Deprecated.
      Used by the evaluator and the compiler to look up a constant by name
    • renderCurrentBacktrace

      public void renderCurrentBacktrace(StringBuilder sb)
      Render the current backtrace as a string to the given StringBuilder. This will honor the currently-configured backtrace format and content.
      Parameters:
      sb - the StringBuilder to which to render the backtrace
    • createCallerBacktrace

      public IRubyObject createCallerBacktrace(int level, int length, Stream<com.headius.backport9.stack.StackWalker.StackFrame> stackStream)
      Create an Array with backtrace information for Kernel#caller
      Parameters:
      level -
      length -
      Returns:
      an Array with the backtrace
    • createCallerLocations

      public IRubyObject createCallerLocations(int level, Integer length, Stream<com.headius.backport9.stack.StackWalker.StackFrame> stackStream)
      Create an array containing Thread::Backtrace::Location objects for the requested caller trace level and length.
      Parameters:
      level - the level at which the trace should start
      length - the length of the trace
      Returns:
      an Array with the backtrace locations
    • getFullTrace

      private RubyStackTraceElement[] getFullTrace(Integer length, Stream<com.headius.backport9.stack.StackWalker.StackFrame> stackStream)
    • getPartialTrace

      private RubyStackTraceElement[] getPartialTrace(int level, Integer length, Stream<com.headius.backport9.stack.StackWalker.StackFrame> stackStream)
    • safeLength

      private static int safeLength(int level, Integer length, RubyStackTraceElement[] trace)
    • getSingleBacktrace

      public RubyStackTraceElement getSingleBacktrace(int level)
      Return a single RubyStackTraceElement representing the nearest Ruby stack trace element. Used for warnings and Kernel#__dir__.
      Returns:
      the nearest stack trace element
    • getSingleBacktrace

      public RubyStackTraceElement getSingleBacktrace()
      Same as calling getSingleBacktrace(0);
      See Also:
    • isEventHooksEnabled

      public boolean isEventHooksEnabled()
    • setEventHooksEnabled

      public void setEventHooksEnabled(boolean flag)
    • getBacktrace

      public Stream<BacktraceElement> getBacktrace()
      Create a snapshot Array with current backtrace information.
      Returns:
      the backtrace
    • getBacktrace

      public final Stream<BacktraceElement> getBacktrace(int level)
    • createRawBacktraceStringFromThrowable

      public static String createRawBacktraceStringFromThrowable(Throwable ex, boolean color)
    • pushFrameForBlock

      private Frame pushFrameForBlock(Binding binding)
    • preAdoptThread

      public void preAdoptThread()
    • preExtensionLoad

      public void preExtensionLoad(IRubyObject self)
    • preMethodFrameAndScope

      public void preMethodFrameAndScope(RubyModule clazz, String name, IRubyObject self, Block block, StaticScope staticScope)
    • preMethodFrameAndDummyScope

      public void preMethodFrameAndDummyScope(RubyModule clazz, String name, IRubyObject self, Block block, StaticScope staticScope)
    • preMethodNoFrameAndDummyScope

      public void preMethodNoFrameAndDummyScope(StaticScope staticScope)
    • postMethodFrameAndScope

      public void postMethodFrameAndScope()
    • preMethodFrameOnly

      public void preMethodFrameOnly(RubyModule clazz, String name, IRubyObject self, Block block)
    • preMethodFrameOnly

      public void preMethodFrameOnly(RubyModule clazz, String name, IRubyObject self, Visibility visiblity, Block block)
    • preMethodFrameOnly

      public void preMethodFrameOnly(RubyModule clazz, String name, IRubyObject self)
    • preBackrefMethod

      public void preBackrefMethod()
    • postMethodFrameOnly

      public void postMethodFrameOnly()
    • postBackrefMethod

      public void postBackrefMethod()
    • preMethodScopeOnly

      public void preMethodScopeOnly(StaticScope staticScope)
    • postMethodScopeOnly

      public void postMethodScopeOnly()
    • preMethodBacktraceAndScope

      public void preMethodBacktraceAndScope(String name, StaticScope staticScope)
    • postMethodBacktraceAndScope

      public void postMethodBacktraceAndScope()
    • preMethodBacktraceOnly

      public void preMethodBacktraceOnly(String name)
    • preMethodBacktraceDummyScope

      public void preMethodBacktraceDummyScope(String name, StaticScope staticScope)
    • postMethodBacktraceOnly

      public void postMethodBacktraceOnly()
    • postMethodBacktraceDummyScope

      public void postMethodBacktraceDummyScope()
    • prepareTopLevel

      public void prepareTopLevel(RubyClass objectClass, IRubyObject topSelf)
    • preNodeEval

      public void preNodeEval(IRubyObject self)
    • postNodeEval

      public void postNodeEval()
    • preExecuteUnder

      public void preExecuteUnder(IRubyObject executeUnderObj, RubyModule executeUnderClass, Block block)
    • postExecuteUnder

      public void postExecuteUnder()
    • preTrace

      public void preTrace()
    • postTrace

      public void postTrace()
    • preYieldSpecificBlock

      public Frame preYieldSpecificBlock(Binding binding, StaticScope scope)
    • preYieldNoScope

      public Frame preYieldNoScope(Binding binding)
    • preYieldNoScope

      public Frame preYieldNoScope(Block block)
    • preEvalScriptlet

      public void preEvalScriptlet(DynamicScope scope)
    • postEvalScriptlet

      public void postEvalScriptlet()
    • preEvalWithBinding

      public Frame preEvalWithBinding(Binding binding)
    • postEvalWithBinding

      public void postEvalWithBinding(Binding binding, Frame lastFrame)
    • postYield

      public void postYield(Binding binding, Frame lastFrame)
    • postYieldNoScope

      public void postYieldNoScope(Frame lastFrame)
    • preScopedBody

      public void preScopedBody(DynamicScope scope)
    • postScopedBody

      public void postScopedBody()
    • isWithinTrace

      public boolean isWithinTrace()
      Is this thread actively tracing at this moment.
      Returns:
      true if so
      See Also:
    • setWithinTrace

      public void setWithinTrace(boolean isWithinTrace)
      Set whether we are actively tracing or not on this thread.
      Parameters:
      isWithinTrace - true is so
      See Also:
    • currentBinding

      public Binding currentBinding()
      Return a binding representing the current call's state
      Returns:
      the current binding
    • currentBinding

      public Binding currentBinding(IRubyObject self)
      Return a binding representing the current call's state but with a specified self
      Parameters:
      self - the self object to use
      Returns:
      the current binding, using the specified self
    • currentBinding

      public Binding currentBinding(IRubyObject self, Visibility visibility)
      Return a binding representing the current call's state but with the specified visibility and self.
      Parameters:
      self - the self object to use
      visibility - the visibility to use
      Returns:
      the current binding using the specified self and visibility
    • currentBinding

      public Binding currentBinding(IRubyObject self, DynamicScope scope)
      Return a binding representing the current call's state but with the specified scope and self.
      Parameters:
      self - the self object to use
      scope - the scope to use
      Returns:
      the current binding using the specified self and scope
    • currentBinding

      public Binding currentBinding(IRubyObject self, Visibility visibility, DynamicScope scope)
      Return a binding representing the current call's state but with the specified visibility, scope, and self. For shared-scope binding consumers like for loops.
      Parameters:
      self - the self object to use
      visibility - the visibility to use
      scope - the scope to use
      Returns:
      the current binding using the specified self, scope, and visibility
    • getProfileCollection

      public ProfileCollection getProfileCollection()
      Get the profile collection for this thread (ThreadContext).
      Returns:
      the thread's profile collection
    • startProfiling

      public void startProfiling()
    • stopProfiling

      public void stopProfiling()
    • isProfiling

      public boolean isProfiling()
    • profileEnter

      public int profileEnter(int nextMethod)
    • profileEnter

      public int profileEnter(String name, DynamicMethod nextMethod)
    • profileExit

      public int profileExit(int nextMethod, long startTime)
    • getRecursiveSet

      public Set<RecursiveComparator.Pair> getRecursiveSet()
    • setRecursiveSet

      public void setRecursiveSet(Set<RecursiveComparator.Pair> recursiveSet)
    • setExceptionRequiresBacktrace

      public void setExceptionRequiresBacktrace(boolean exceptionRequiresBacktrace)
    • exceptionBacktraceOn

      public void exceptionBacktraceOn()
    • exceptionBacktraceOff

      public void exceptionBacktraceOff()
    • clearThreadTraceFunctions

      public IRubyObject clearThreadTraceFunctions()
    • addThreadTraceFunction

      public IRubyObject addThreadTraceFunction(IRubyObject trace_func, boolean useContextHook)
    • setThreadTraceFunction

      public IRubyObject setThreadTraceFunction(IRubyObject trace_func)
    • setPrivateConstantReference

      public void setPrivateConstantReference(RubyModule privateConstantReference)
    • getPrivateConstantReference

      public RubyModule getPrivateConstantReference()
    • safeRecurse

      public <T> IRubyObject safeRecurse(ThreadContext.RecursiveFunctionEx<T> func, T state, IRubyObject obj, String name, boolean outer)
    • safeRecurseOutermost

      private <T> IRubyObject safeRecurseOutermost(ThreadContext.RecursiveFunctionEx<T> func, T state, IRubyObject obj, Map<IRubyObject,IRubyObject> guards)
    • safeRecurseGetGuards

      private Map<IRubyObject,IRubyObject> safeRecurseGetGuards(String name)
    • safeRecurseInner

      private <T> IRubyObject safeRecurseInner(ThreadContext.RecursiveFunctionEx<T> func, T state, IRubyObject obj, Map<IRubyObject,IRubyObject> guards)
    • encodingHolder

      public org.jcodings.Encoding[] encodingHolder()
    • setLocalMatch

      public void setLocalMatch(RubyMatchData localMatch)
      Set the thread-local MatchData specific to this context. This is different from the frame backref since frames may be shared by several executing contexts at once (see jruby/jruby#4868).
      Parameters:
      localMatch - the new thread-local MatchData or null
    • clearLocalMatch

      public void clearLocalMatch()
      Set the thread-local MatchData specific to this context to null.
      See Also:
    • getLocalMatch

      public RubyMatchData getLocalMatch()
      Get the thread-local MatchData specific to this context. This is different from the frame backref since frames may be shared by several executing contexts at once (see jruby/jruby#4868).
      Returns:
      the current thread-local MatchData, or null if none
    • getLocalMatchOrNil

      public IRubyObject getLocalMatchOrNil()
      Get the thread-local MatchData specific to this context or nil if none.
      Returns:
      the current thread-local MatchData, or nil if none
      See Also:
    • setBackRef

      @Deprecated public IRubyObject setBackRef(IRubyObject match)
      Deprecated.