lib/byebug/dap/helpers/value_helpers.rb in byebug-dap-0.1.3 vs lib/byebug/dap/helpers/value_helpers.rb in byebug-dap-0.1.4

- old
+ new

@@ -1,17 +1,23 @@ module Byebug::DAP + # Methods to prepare values for DAP responses. + # @api private module ValueHelpers + # Safely inspect a value and retrieve its class name, class and instance + # variables, and indexed members. {Scalar} values do not have variables or + # members. Only {std:Array arrays} and {std:Hash hashes} have members. + # @return `val.inspect`, `val.class.name`, variables, and members. def prepare_value(val) str = safe(val, :inspect) { safe(val, :to_s) { return yield } } cls = safe(val, :class) { nil } typ = safe(cls, :name) { safe(cls, :to_s) { nil } } scalar = safe(-> { Scalar === val }, :call) { true } return str, typ, [], [] if scalar - named = safe(val, :instance_variables) { [] } - named += safe(val, :class_variables) { [] } + named = safe(val, :instance_variables) { [] } || [] + named += safe(val, :class_variables) { [] } || [] # named += safe(val, :constants) { [] } indexed = safe(-> { return (0...val.size).to_a if val.is_a?(Array) return val.keys if val.respond_to?(:keys) && val.respond_to?(:[]) @@ -19,10 +25,19 @@ }, :call) { [] } return str, typ, named, indexed end + # Prepare a {Protocol::Variable} or {Protocol::EvaluateResponseBody} for a + # calculated value. For global variables and evaluations, `thnum` and + # `frnum` should be 0. Local variables and evaluations are + # {Command#execute_on_thread executed on the specified thread}. + # @param thnum [std:Integer] the thread number + # @param frnum [std:Integer] the frame number + # @param kind [std:Symbol] `:variable` or `:evaluate` + # @param name [std:String] the variable name (ignored for evaluations) + # @yield retrieves an variable or evaluates an expression def prepare_value_response(thnum, frnum, kind, name: nil, &block) err = nil raw = execute_on_thread(thnum, block) { |e| err = e; nil } if err.nil? @@ -37,13 +52,13 @@ end end case kind when :variable - klazz = ::DAP::Variable + klazz = Protocol::Variable args = { name: safe(name, :to_s) { safe(name, :inspect) { '???' } }, value: value, type: type } when :evaluate - klazz = ::DAP::EvaluateResponseBody + klazz = Protocol::EvaluateResponseBody args = { result: value, type: type } end if named.empty? && indexed.empty? args[:variablesReference] = 0