Skip to content

Commit

Permalink
Update lib/debug/server_dap.rb
Browse files Browse the repository at this point in the history
Co-authored-by: Koichi Sasada <[email protected]>
  • Loading branch information
marianosimone and ko1 committed Oct 25, 2022
1 parent fbe7f08 commit f1c8927
Showing 1 changed file with 4 additions and 14 deletions.
18 changes: 4 additions & 14 deletions lib/debug/server_dap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -687,22 +687,12 @@ def register_vars vars, tid
class ThreadClient
def value_inspect obj
# TODO: max length should be configuarable?
value = DEBUGGER__.safe_inspect obj, short: true, max_length: 4 * 1024

# Given that this is going to be transmitted in a JSON string, it needs to be unicode-encoded
# (and "UTF-8" is the default).
if value.encoding != Encoding::UTF_8
# If the string we got is frozen, we need to make a copy first
value = value.dup if value.frozen?
value.force_encoding(Encoding::UTF_8)
end
str = DEBUGGER__.safe_inspect obj, short: true, max_length: 4 * 1024

if value.valid_encoding?
value
if str.encoding == Encoding::UTF_8
str.scrub
else
# If a variable contains non-unicode data, at least we can send it partially and signal that
# the encoding was unexpected.
"[Invalid encoding] #{value.encode("UTF-8", invalid: :replace, undef: :replace)}"
str.encode(Encoding::UTF_8, invalid: :replace, undef: :replace)
end
end

Expand Down

0 comments on commit f1c8927

Please sign in to comment.