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 # This is the end of the kernel and the beginning of specified
42 # code. We read out of ARGV to figure out what the user is
45 Signal.action("INT") do |thr|
46 # We can't raise on ourselves, we raise on main.
47 if thr == Thread.current
51 # The current thread might be dead if all other live threads are
52 # a sleep when the current one died.
56 # If main is dead too. Wow. Ok.. well... tell the user.
58 puts "Signal received, but the main thread is dead."
59 puts "Unable to continue."
64 # Push the output down a little bit, makes things look more
65 # obvious that the system was interrupted.
67 thr.raise Interrupt, "Thread has been interrupted"
74 # The main stdlib location
75 additions << Rubinius::CODE_PATH
77 $LOAD_PATH.insert($LOAD_PATH.index('.'), *additions)
79 if ENV['RUBYLIB'] and not ENV['RUBYLIB'].empty? then
80 $LOAD_PATH.unshift(*ENV['RUBYLIB'].split(':'))
83 # Pull it out now so that later unshifts don't obsure it.
84 main_lib = $LOAD_PATH.first
86 # Allow system wide code preloading
88 ['/etc/rbxrc',"#{ENV['HOME']}/.rbxrc",ENV['RBX_PRELOAD']].each do |file|
90 load file if file and File.exist?(file)
98 Usage: rubinius [options] [file]
99 File may be any valid Ruby source file (.rb) or a compiled Ruby file (.rbc).
102 -d Enable debugging output and set $DEBUG to true.
103 -dc Display debugging information for the compiler.
104 -dl Display debugging information for the loader.
105 -debug Launch the debugger.
106 -remote-debug Run the program under the control of a remote debugger.
107 -e 'code' Directly compile and execute code (no file provided).
108 -Idir1[:dir2] Add directories to $LOAD_PATH.
109 -S script Run script using PATH environment variable to find it.
111 -ps Run the Selector profiler.
112 -pss Run the SendSite profiler.
113 -rlibrary Require library before execution.
114 -w Enable warnings. (currently does nothing--compatibility)
115 -v Display the version and set $VERBOSE to true.
121 show_selectors = false
122 show_sendsites = false
124 # Setup the proper staticscope
125 MethodContext.current.method.staticscope = StaticScope.new(Object)
127 TOPLEVEL_BINDING = binding()
133 script_debug_requested = false
143 puts "rubinius #{RBX_VERSION} (ruby #{RUBY_VERSION} compatible) (#{Rubinius::BUILDREV[0..8]}) (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"
145 exit 0 if ARGV.empty?
149 puts "[Compiler debugging enabled]"
150 $DEBUG_COMPILER = true
152 $DEBUG_LOADING = true
153 puts "[Code loading debugging enabled]"
157 require 'debugger/interface'
158 Debugger::CmdLineInterface.new
159 script_debug_requested = true
161 require 'debugger/debug_server'
162 if port = (ARGV.first =~ /^\d+$/ and ARGV.shift)
163 $DEBUG_SERVER = Debugger::Server.new(port.to_i)
165 $DEBUG_SERVER = Debugger::Server.new
168 script_debug_requested = true
172 count = (ARGV.first =~ /^\d+$/) ? ARGV.shift : '30'
173 show_selectors = count.to_i
175 count = (ARGV.first =~ /^\d+$/) ? ARGV.shift : '30'
176 show_sendsites = count.to_i
179 sep = File::PATH_SEPARATOR
180 path = ENV['PATH'].split(sep).map { |d| File.join(d, script) }
181 file = path.find { |path| File.exist? path }
185 # if missing, let it die a natural death
186 ARGV.unshift file ? file : script
189 eval_code = ARGV.shift
194 $LOAD_PATH.unshift(ARGV.shift)
196 more.split(":").reverse_each do |path|
197 $LOAD_PATH.unshift(path)
200 elsif arg.prefix? "-r"
209 Compile.execute STDIN.read
210 elsif arg.prefix? "-"
211 puts "Invalid switch '#{arg}'"
222 # If someone used -e, run that code.
224 # If we also caught a script to run, we just treat it like
226 ARGV.unshift script if script
228 Compile.execute eval_code
230 if File.exists?(script)
232 Compile.debug_script! if script_debug_requested
233 Compile.load_from_extension arg
235 if script.suffix?(".rb")
236 puts "Unable to find '#{script}'"
239 prog = File.join main_lib, "bin", script
244 raise LoadError, "Unable to find a script '#{script}' to run"
251 if Rubinius::Terminal
252 repr = ENV['RBX_REPR'] || "bin/irb"
254 prog = File.join main_lib, repr
257 rescue LoadError => e
258 STDERR.puts "Unable to find repr named '#{repr}' to load."
263 Compile.execute "p #{STDIN.read}"
267 rescue SystemExit => e
271 if e.kind_of? Exception or e.kind_of? ThrownValue
274 msg = "strange object detected as exception: #{e.inspect}"
276 if e.kind_of? SyntaxError
277 puts "A syntax error has occured:"
279 puts " near line #{e.file}:#{e.line}, column #{e.column}"
280 puts "\nCode:\n#{e.code}"
282 puts((" " * (e.column - 1)) + "^")
285 puts "An exception has occurred:"
286 puts " #{msg} (#{e.class})"
289 puts e.awesome_backtrace.show
292 puts "Unable to build backtrace due to errors"
293 puts "Original Exception: #{e.inspect} (#{e.class})"
294 puts "New Exception: #{e2.inspect} (#{e.class})"
300 Rubinius::AtExit.shift.call until Rubinius::AtExit.empty?
301 rescue SystemExit => e
304 puts "An exception occurred inside an at_exit handler:"
305 puts " #{e.message} (#{e.class})"
307 puts e.awesome_backtrace.show
312 ObjectSpace.run_finalizers
314 puts "An exception occured while running object finalizers:"
315 puts " #{e.message} (#{e.class})"
317 puts e.awesome_backtrace.show
322 ps = Sampler::Selectors.new
324 ps.show_stats show_selectors
326 puts "An exception occured while running selector profiler:"
327 puts " #{e.message} (#{e.class})"
329 puts e.awesome_backtrace.show
335 ps = Sampler::SendSites.new
337 ps.show_stats show_sendsites
339 puts "An exception occured while running sendsite profiler:"
340 puts " #{e.message} (#{e.class})"
342 puts e.awesome_backtrace.show
347 Process.exit(code || 0)