Class ThreadContext

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

public final class ThreadContext extends Object
  • Field Details

    • 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
    • traceEvents

      public final TraceEventManager traceEvents
    • CALL_SPLATS

      public static final int CALL_SPLATS
      See Also:
    • CALL_KEYWORD

      public static final int CALL_KEYWORD
      See Also:
    • CALL_KEYWORD_REST

      public static final int CALL_KEYWORD_REST
      See Also:
    • CALL_KEYWORD_EMPTY

      public static final int CALL_KEYWORD_EMPTY
      See Also:
    • callInfo

      public int callInfo
    • secureRandom

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

      public final JavaSites sites
    • callNumber

      public int callNumber
    • WALKER

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

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

      public boolean exceptionRequiresBacktrace
  • 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)
    • clearErrorInfo

      public void clearErrorInfo()
    • 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()
    • pushScope

      public void pushScope(DynamicScope scope)
    • pushNewScope

      public DynamicScope pushNewScope(StaticScope staticScope)
    • popScope

      public void popScope()
    • 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)
    • 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
    • pushEvalSimpleFrame

      public void pushEvalSimpleFrame(IRubyObject executeObject)
    • pushFrame

      public void pushFrame()
    • popFrame

      public void popFrame()
    • 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 $_
    • 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.
    • getCompositeName

      public String getCompositeName()
      A string representing the name of the current method and the name under which it was called. If not called via an alias, this will be a single string. If called via an alias, it will be encoded as "\0alias\0original" and must be unpacked for its components.
      Returns:
      a representation of the current method's name and the name it was called under
      See Also:
    • getFrameName

      public String getFrameName()
      The currently-running method's original name, used for stack traces, super calls, and __method__, and trace hooks.
      Returns:
      the current method's name
    • getCalleeName

      public String getCalleeName()
      The name under which the current method was called, if called via an alias. Used for __callee__.
      Returns:
      the name under which the current method was called
    • 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)
    • getFileAndLine

      public String getFileAndLine()
    • 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
    • eachCallerLocation

      public void eachCallerLocation(Stream<com.headius.backport9.stack.StackWalker.StackFrame> stackStream, Consumer<RubyThread.Location> consumer)
      Like createCallerLocations(int, Integer, Stream) but accepts a lambda to yield each location and yields all stack elements until the loop ends or is broken early.
      Parameters:
      stackStream - the stream of StackFrame objects from JVM
      consumer - the consumer of RubyThread.Location objects
    • 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
    • getSingleBacktraceExact

      public RubyStackTraceElement getSingleBacktraceExact(int level)
      Return the trace of level or null.
    • 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)
    • 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)
    • 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:
    • resetCallInfo

      @Deprecated public int resetCallInfo()
      Deprecated.
      use the trivially-inlinable static version
      Reset call info state and return the value of call info right before it is reset.
      Returns:
      the old call info
    • resetCallInfo

      public static int resetCallInfo(ThreadContext context)
      Reset call info state and return the value of call info right before it is reset. This method is static to make it trivially inlinable on most JVM JITs
      Returns:
      the old call info
    • clearCallInfo

      @Deprecated public void clearCallInfo()
      Deprecated.
      use the trivially-inlinable static version
      Clear call info state (set to 0).
    • clearCallInfo

      public static void clearCallInfo(ThreadContext context)
      Clear call info state (set to 0). This method is static to make it trivially inlinable on most JVM JITs
    • hasKeywords

      public static boolean hasKeywords(int callInfo)
    • setBackRef

      @Deprecated public IRubyObject setBackRef(IRubyObject match)
      Deprecated.