/usr/lib/ruby/vendor_ruby/web_console/integration/jruby.rb is in ruby-web-console 2.2.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | require 'English'
require 'active_support/core_ext/string/strip'
java_import org.jruby.RubyInstanceConfig
module WebConsole
module JRuby
class << self
# Returns whether JRuby is ran in interpreted mode.
def interpreted_mode?
compile_mode = ::JRuby.runtime.instance_config.compile_mode
interpreted_mode = RubyInstanceConfig::CompileMode::OFF
compile_mode == interpreted_mode
end
# A proc to be used in Kernel#set_trace_func.
#
# It sets Exception#bindings for an error with all the bindings the
# current ThreadContext contains.
def set_exception_bindings_trace_func
proc do |event, file, line, id, binding, classname|
case event
when 'raise'
if $ERROR_INFO.bindings.empty?
# binding_of_caller will generate an improperly built binding at
# caller[1]. Every call to a non existent method, constant or a
# local variable will result in a Java NullPointerException.
#
# The binding that Kernel#set_trace_func is giving us is properly
# built, so we can use in place of the broken one.
bindings = ::Kernel.binding.callers.drop(2).unshift(binding)
$ERROR_INFO.instance_variable_set(:@bindings, bindings)
end
end
end
end
end
# A fake binding for JRuby in non interpreted mode.
#
# It won't actually evaluate any code, rather it will tell the user how to
# enable interpreted mode.
class FakeJRubyBinding
TURN_ON_INTERPRETED_MODE_TEXT = <<-END.strip_heredoc
JRuby needs to run in interpreted mode for introspection support.
To turn on interpreted mode, put -J-Djruby.compile.mode=OFF in the
JRUBY_OPTS environment variable.
END
def TURN_ON_INTERPRETED_MODE_TEXT.inspect
self
end
TURN_ON_INTERPRETED_MODE_TEXT.freeze
def eval(*)
TURN_ON_INTERPRETED_MODE_TEXT
end
end
# A fake array of FakeJRubyBinding objects.
#
# It is used in Exception#bindings to make sure that when users switch
# bindings on the UI, they get a FakeJRubyBinding notifying them what to do
# if they want introspection.
class FakeJRubyBindingsArray < Array
def [](*)
FakeJRubyBinding.new
end
# For Kernel#Array and other implicit conversion API. JRuby expects it to
# return an object that is_a? an Array. This is the reasoning of
# FakeJRubyBindingsArray being a subclass of Array.
def to_ary
self
end
end
end
end
if WebConsole::JRuby.interpreted_mode?
::Exception.class_eval do
def bindings
@bindings || []
end
end
# Kernel#set_trace_func will complain about not being able to capture all the
# events without the JRuby --debug flag.
silence_warnings do
set_trace_func WebConsole::JRuby.set_exception_bindings_trace_func
end
else
::Exception.class_eval do
def bindings
WebConsole::JRuby::FakeJRubyBindingsArray.new
end
end
::Binding.class_eval do
def of_caller(*)
WebConsole::JRuby::FakeJRubyBinding.new
end
def callers
WebConsole::JRuby::FakeJRubyBindingsArray.new
end
end
end
|