# frozen_string_literal: true

module WebConsole
  # Simple Ruby code evaluator.
  #
  # This class wraps a +Binding+ object and evaluates code inside of it. The
  # difference of a regular +Binding+ eval is that +Evaluator+ will always
  # return a string and will format exception output.
  class Evaluator
    # Cleanses exceptions raised inside #eval.
    cattr_reader :cleaner, default: begin
      cleaner = ActiveSupport::BacktraceCleaner.new
      cleaner.add_silencer { |line| line.start_with?(File.expand_path("..", __FILE__)) }
      cleaner
    end

    def initialize(binding = TOPLEVEL_BINDING)
      @binding = binding
    end

    def eval(input)
      "=> #{@binding.eval(input).inspect}\n"
    rescue Exception => exc
      format_exception(exc)
    end

    private

      def format_exception(exc)
        backtrace = cleaner.clean(Array(exc.backtrace) - caller)

        format = "#{exc.class.name}: #{exc}\n".dup
        format << backtrace.map { |trace| "\tfrom #{trace}\n" }.join
        format
      end
  end
end

# frozen_string_literal: true

module WebConsole
  # Simple Ruby code evaluator.
  #
  # This class wraps a +Binding+ object and evaluates code inside of it. The
  # difference of a regular +Binding+ eval is that +Evaluator+ will always
  # return a string and will format exception output.
  class Evaluator
    # Cleanses exceptions raised inside #eval.
    cattr_reader :cleaner, default: begin
      cleaner = ActiveSupport::BacktraceCleaner.new
      cleaner.add_silencer { |line| line.start_with?(File.expand_path("..", __FILE__)) }
      cleaner
    end

    def initialize(binding = TOPLEVEL_BINDING)
      @binding = binding
    end

    def eval(input)
      # Binding#source_location is available since Ruby 2.6. But if this should
      # be removed the `WebConsole::SessionTest#test_use_first_binding_if_no_application_bindings`
      # must be adjusted.
      if @binding.respond_to? :source_location
        "=> #{@binding.eval(input, *@binding.source_location).inspect}\n"
      else
        "=> #{@binding.eval(input).inspect}\n"
      end
    rescue Exception => exc
      format_exception(exc)
    end

    private

      def format_exception(exc)
        backtrace = cleaner.clean(Array(exc.backtrace) - caller)

        format = "#{exc.class.name}: #{exc}\n".dup
        format << backtrace.map { |trace| "\tfrom #{trace}\n" }.join
        format
      end
  end
end

Make binding.eval Ruby 3.0 compatible.
Make binding.eval Ruby 3.0 compatible.

Ruby 3.0 changes behavior of `binding.eval` [[1]] resulting in test
failure:

~~~
WebConsole::EvaluatorTest#test_Evaluator_callers_are_cleaned_up_of_unneeded_backtraces [/home/travis/build/rails/web-console/test/web_console/evaluator_test.rb:63]:
--- expected
+++ actual
@@ -1,3 +1,3 @@
 "RuntimeError: oops
-\tfrom /home/travis/build/rails/web-console/test/web_console/evaluator_test.rb:61:in `block in <class:EvaluatorTest>'
+\tfrom (eval):1:in `block in <class:EvaluatorTest>'
~~~

Fixes #301

[1]: https://bugs.ruby-lang.org/issues/17419
