1 # Contained first is the system startup code.
3 # Re-setup all the stdio channels, to pull in new ivars
21 ObjectSpace.after_loaded
23 IO::BidirectionalPipe.after_loaded
30 ENV = EnvironmentVariables.new
32 # define a global "start time" to use for process calculation
33 $STARTUP_TIME = Time.now
35 STDOUT << "Error detected running loader startup stage:\n"
36 STDOUT << " #{e.message} (#{e.class})\n"
41 # Set up a handler for SIGINT that raises Interrupt on the main thread
42 Signal.action("INT") do |_|
46 thread.raise Interrupt, "Thread has been interrupted"
48 puts "Signal received, but the main thread is dead."
49 puts "Unable to continue."
54 # This is the end of the kernel and the beginning of specified
55 # code. We read out of ARGV to figure out what the user is
62 # The main stdlib location
63 additions << Rubinius::CODE_PATH
65 $LOAD_PATH.insert($LOAD_PATH.index('.'), *additions)
67 if ENV['RUBYLIB'] and not ENV['RUBYLIB'].empty? then
68 $LOAD_PATH.unshift(*ENV['RUBYLIB'].split(':'))
71 # Pull it out now so that later unshifts don't obsure it.
72 main_lib = $LOAD_PATH.first
74 # Allow system wide code preloading
76 ['/etc/rbxrc',"#{ENV['HOME']}/.rbxrc",ENV['RBX_PRELOAD']].each do |file|
78 load file if file and File.exist?(file)
86 Usage: rubinius [options] [file]
87 File may be any valid Ruby source file (.rb) or a compiled Ruby file (.rbc).
90 -d Enable debugging output and set $DEBUG to true.
91 -dc Display debugging information for the compiler.
92 -dl Display debugging information for the loader.
93 -debug Launch the debugger.
94 -remote-debug Run the program under the control of a remote debugger.
95 -e 'code' Directly compile and execute code (no file provided).
96 -Idir1[:dir2] Add directories to $LOAD_PATH.
97 -S script Run script using PATH environment variable to find it.
99 -ps Run the Selector profiler.
100 -pss Run the SendSite profiler.
101 -rlibrary Require library before execution.
102 -w Enable warnings. (currently does nothing--compatibility)
103 -v Display the version and set $VERBOSE to true.
109 show_selectors = false
110 show_sendsites = false
112 # Setup the proper staticscope
113 MethodContext.current.method.staticscope = StaticScope.new(Object)
115 TOPLEVEL_BINDING = binding()
121 script_debug_requested = false
131 puts "rubinius #{RBX_VERSION} (ruby #{RUBY_VERSION} compatible) (#{Rubinius::BUILDREV[0..8]}) (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"
133 exit 0 if ARGV.empty?
137 puts "[Compiler debugging enabled]"
138 $DEBUG_COMPILER = true
140 $DEBUG_LOADING = true
141 puts "[Code loading debugging enabled]"
145 require 'debugger/interface'
146 Debugger::CmdLineInterface.new
147 script_debug_requested = true
149 require 'debugger/debug_server'
150 if port = (ARGV.first =~ /^\d+$/ and ARGV.shift)
151 $DEBUG_SERVER = Debugger::Server.new(port.to_i)
153 $DEBUG_SERVER = Debugger::Server.new
156 script_debug_requested = true
160 count = (ARGV.first =~ /^\d+$/) ? ARGV.shift : '30'
161 show_selectors = count.to_i
163 count = (ARGV.first =~ /^\d+$/) ? ARGV.shift : '30'
164 show_sendsites = count.to_i
167 sep = File::PATH_SEPARATOR
168 path = ENV['PATH'].split(sep).map { |d| File.join(d, script) }
169 file = path.find { |path| File.exist? path }
173 # if missing, let it die a natural death
174 ARGV.unshift file ? file : script
177 eval_code = ARGV.shift
182 $LOAD_PATH.unshift(ARGV.shift)
184 more.split(":").reverse_each do |path|
185 $LOAD_PATH.unshift(path)
188 elsif arg.prefix? "-r"
197 Compile.execute STDIN.read
198 elsif arg.prefix? "-"
199 puts "Invalid switch '#{arg}'"
210 # If someone used -e, run that code.
212 # If we also caught a script to run, we just treat it like
214 ARGV.unshift script if script
216 Compile.execute eval_code
218 if File.exists?(script)
220 Compile.debug_script! if script_debug_requested
221 Compile.load_from_extension arg
223 if script.suffix?(".rb")
224 puts "Unable to find '#{script}'"
227 prog = File.join main_lib, "bin", script
232 raise LoadError, "Unable to find a script '#{script}' to run"
239 if Rubinius::Terminal
240 repr = ENV['RBX_REPR'] || "bin/irb"
242 prog = File.join main_lib, repr
245 rescue LoadError => e
246 STDERR.puts "Unable to find repr named '#{repr}' to load."
251 Compile.execute "p #{STDIN.read}"
255 rescue SystemExit => e
259 if e.kind_of? Exception or e.kind_of? ThrownValue
262 msg = "strange object detected as exception: #{e.inspect}"
264 if e.kind_of? SyntaxError
265 puts "A syntax error has occured:"
267 puts " near line #{e.file}:#{e.line}, column #{e.column}"
268 puts "\nCode:\n#{e.code}"
270 puts((" " * (e.column - 1)) + "^")
273 puts "An exception has occurred:"
274 puts " #{msg} (#{e.class})"
277 puts e.awesome_backtrace.show
280 puts "Unable to build backtrace due to errors"
281 puts "Original Exception: #{e.inspect} (#{e.class})"
282 puts "New Exception: #{e2.inspect} (#{e.class})"
288 Rubinius::AtExit.shift.call until Rubinius::AtExit.empty?
289 rescue SystemExit => e
292 puts "An exception occurred inside an at_exit handler:"
293 puts " #{e.message} (#{e.class})"
295 puts e.awesome_backtrace.show
300 ObjectSpace.run_finalizers
302 puts "An exception occured while running object finalizers:"
303 puts " #{e.message} (#{e.class})"
305 puts e.awesome_backtrace.show
310 ps = Sampler::Selectors.new
312 ps.show_stats show_selectors
314 puts "An exception occured while running selector profiler:"
315 puts " #{e.message} (#{e.class})"
317 puts e.awesome_backtrace.show
323 ps = Sampler::SendSites.new
325 ps.show_stats show_sendsites
327 puts "An exception occured while running sendsite profiler:"
328 puts " #{e.message} (#{e.class})"
330 puts e.awesome_backtrace.show
335 Process.exit(code || 0)