Sha256: c53c6568068a7e58eeb46645fa141846445f05660d87c8aa98dcd2979a4281c3

Contents?: true

Size: 1.44 KB

Versions: 9

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

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?
      `ps axw`.split("\n").find { |ps| ps[/\A\s*#{$$}/] }.then do |command|
        next unless command
        command.tr! '\012', "\n"
        command.tr! "\\", "\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)
      TOPLEVEL_BINDING.eval(new_e_script, $0)
      exit!(0)
    end
  end

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

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
ruby-next-core-0.5.3 lib/uby-next.rb
ruby-next-core-0.5.2 lib/uby-next.rb
ruby-next-core-0.5.1 lib/uby-next.rb
ruby-next-core-0.5.0 lib/uby-next.rb
ruby-next-core-0.4.0 lib/uby-next.rb
ruby-next-0.3.0 lib/uby-next.rb
ruby-next-core-0.3.0 lib/uby-next.rb
ruby-next-0.2.0 lib/uby-next.rb
ruby-next-core-0.2.0 lib/uby-next.rb