lib/byebug/helpers/eval.rb in byebug-5.0.0 vs lib/byebug/helpers/eval.rb in byebug-6.0.0

- old
+ new

@@ -1,47 +1,88 @@ module Byebug module Helpers # - # Utilities used by the eval command + # Utilities to assist evaluation of code strings # module EvalHelper # - # Run block temporarily ignoring all TracePoint events. + # Evaluates +expression+ that might manipulate threads # - # Used to evaluate stuff within Byebug's prompt. Otherwise, any code - # creating new threads won't be properly evaluated because new threads - # will get blocked by byebug's main thread. + # @param expression [String] Expression to evaluate # - def allowing_other_threads - Byebug.unlock - res = yield - Byebug.lock - res + def thread_safe_eval(expression) + allowing_other_threads { single_thread_eval(expression) } end # - # Get current binding and yield it to the given block + # Evaluates an +expression+ that doesn't deal with threads # - def run_with_binding - binding = get_binding - yield binding + # @param expression [String] Expression to evaluate + # + def single_thread_eval(expression) + return error_eval(expression) if Setting[:stack_on_error] + + warning_eval(expression) end # - # Evaluate +expression+ using +binding+ + # Evaluates a string containing Ruby code in a specific binding, + # returning nil in an error happens. # - # @param binding [Binding] Context where to evaluate the expression - # @param expression [String] Expression to evaluation - # @param stack_on_error [Boolean] Whether to show a stack trace on error. + def silent_eval(str, binding = frame._binding) + binding.eval(str) + rescue StandardError, ScriptError + nil + end + # - def eval_with_setting(binding, expression, stack_on_error) - allowing_other_threads do - if stack_on_error - bb_eval(expression, binding) - else - bb_warning_eval(expression, binding) - end - end + # Evaluates a string containing Ruby code in a specific binding, + # handling the errors at an error level. + # + def error_eval(str, binding = frame._binding) + safe_eval(str, binding) { |e| error_msg(e) } + end + + # + # Evaluates a string containing Ruby code in a specific binding, + # handling the errors at a warning level. + # + def warning_eval(str, binding = frame._binding) + safe_eval(str, binding) { |e| warning_msg(e) } + end + + private + + def safe_eval(str, binding) + binding.eval(str) + rescue StandardError, ScriptError => e + raise(e, yield(e)) + end + + def error_msg(e) + at = e.backtrace + locations = ["#{at.shift}: #{e.class} Exception(#{e.message})"] + locations += at.map { |path| "\tfrom #{path}" } + + pr('eval.exception', text_message: locations.join("\n")) + end + + def warning_msg(e) + pr('eval.exception', text_message: "#{e.class} Exception: #{e.message}") + end + + # + # Run block temporarily ignoring all TracePoint events. + # + # Used to evaluate stuff within Byebug's prompt. Otherwise, any code + # creating new threads won't be properly evaluated because new threads + # will get blocked by byebug's main thread. + # + def allowing_other_threads + Byebug.unlock + res = yield + Byebug.lock + res end end end end