Sha256: 3ead840ff3b32ae9e111e25d3a70bf9d472cc4a8e47ce9365ec7bf82f4b720d4

Contents?: true

Size: 1.65 KB

Versions: 19

Compression:

Stored size: 1.65 KB

Contents

# frozen_string_literal: true

# Activate all features
ENV["RUBY_NEXT_EDGE"] = "1"
ENV["RUBY_NEXT_PROPOSED"] = "1"

require "ruby-next/language/runtime"
require "ruby-next/core/runtime"

using RubyNext

RubyNext::Language.watch_dirs << Dir.pwd

require "stringio"

# Hijack stderr to avoid printing exceptions while executing ruby files
stderr = StringIO.new

orig_stderr, $stderr = $stderr, stderr

# Capture source code passed via `-e` option
e_script = nil

if $0 == "-e"
  begin
    TracePoint.new(:script_compiled) do |tp|
      next unless tp.path == "-e"
      e_script = tp.eval_script
      tp.disable
    end.enable
  rescue ArgumentError
    # script_compiled event is not supported
  end
end

at_exit do
  $stderr = orig_stderr

  if NoMethodError === $! || SyntaxError === $!
    if $0 && File.exist?($0)
      load($0)
      exit!(0)
    end

    if $0 == "-e" && e_script.nil?
      if File.file?("/proc/self/cmdline")
        File.read("/proc/self/cmdline")
      else
        `ps axw`.split("\n").find { |ps| ps[/\A\s*#{$$}/] }
      end.then do |command|
        next unless command
        command.gsub!(/(\\012|\u0000)/, "\n")
        command.match(/-e(.*)/m)
      end.then do |matches|
        next unless matches

        args = ["-e", matches[1]]
        require "optparse"
        OptionParser.new do |o|
          o.on("-e SOURCE") do |v|
            e_script = v
          end
        end.parse!(args)
      end
    end

    if e_script
      new_e_script = RubyNext::Language::Runtime.transform(e_script)
      RubyNext.debug_source new_e_script, $0
      TOPLEVEL_BINDING.eval(new_e_script, $0)
      exit!(0)
    end
  end

  puts(stderr.tap(&:rewind).read)
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
ruby-next-core-0.15.3 lib/uby-next.rb
ruby-next-core-0.15.2 lib/uby-next.rb
ruby-next-core-0.15.1 lib/uby-next.rb
ruby-next-core-0.15.0 lib/uby-next.rb
ruby-next-core-0.14.1 lib/uby-next.rb
ruby-next-core-0.14.0 lib/uby-next.rb
ruby-next-core-0.13.3 lib/uby-next.rb
ruby-next-core-0.13.2 lib/uby-next.rb
ruby-next-core-0.13.1 lib/uby-next.rb
ruby-next-core-0.13.0 lib/uby-next.rb
ruby-next-core-0.12.0 lib/uby-next.rb
ruby-next-core-0.11.1 lib/uby-next.rb
ruby-next-core-0.11.0 lib/uby-next.rb
ruby-next-core-0.10.5 lib/uby-next.rb
ruby-next-core-0.10.4 lib/uby-next.rb
ruby-next-core-0.10.3 lib/uby-next.rb
ruby-next-core-0.10.2 lib/uby-next.rb
ruby-next-core-0.10.1 lib/uby-next.rb
ruby-next-core-0.10.0 lib/uby-next.rb