Skip to content

Commit fe1df97

Browse files
author
小野 直人
committed
Make multidimensional hashes and arrays easier to read in Chrome
1 parent dfe4d62 commit fe1df97

File tree

3 files changed

+84
-35
lines changed

3 files changed

+84
-35
lines changed

lib/debug/server_cdp.rb

Lines changed: 81 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -254,17 +254,15 @@ def process_protocol_request req
254254
@tc << [:cdp, :evaluate, req, expr]
255255
when 'Runtime.getProperties'
256256
oid = req.dig('params', 'objectId')
257-
case oid
258-
when /(\d?):local/
259-
@tc << [:cdp, :properties, req, $1.to_i]
260-
when /\d?:script/
261-
# TODO: Support a script type
262-
@ui.respond req
263-
return :retry
264-
when /\d?:global/
265-
# TODO: Support a global type
257+
case @scope_map[oid]
258+
when 'local', 'eval'
259+
@tc << [:cdp, :properties, req, oid]
260+
when 'script', 'global'
261+
# TODO: Support script and global types
266262
@ui.respond req
267263
return :retry
264+
else
265+
raise "Unknown object id #{oid}"
268266
end
269267
end
270268
end
@@ -289,12 +287,27 @@ def cdp_event args
289287
hash: src.hash
290288
@script_paths << s_id
291289
end
290+
291+
frame[:scopeChain].each {|s|
292+
oid = s.dig(:object, :objectId)
293+
@scope_map[oid] = s[:type]
294+
}
292295
end
293296
result[:reason] = 'other'
294297
@ui.fire_event 'Debugger.paused', **result
295298
when :evaluate
296-
@ui.respond req, result: result
299+
result.each {|r|
300+
if oid = r.dig(:objectId)
301+
@scope_map[oid] = 'eval'
302+
end
303+
}
304+
@ui.respond req, result: result[0]
297305
when :properties
306+
result.each {|r|
307+
if oid = r.dig(:value, :objectId)
308+
@scope_map[oid] = 'local' # TODO: Change this part because it is not necessarily `local`.
309+
end
310+
}
298311
@ui.respond req, result: result
299312
end
300313
end
@@ -329,28 +342,33 @@ def process_cdp args
329342
type: 'local',
330343
object: {
331344
type: 'object',
332-
objectId: "#{i}:local"
345+
objectId: rand.to_s
333346
}
334347
},
335348
{
336349
type: 'script',
337350
object: {
338351
type: 'object',
339-
objectId: "#{i}:script"
352+
objectId: rand.to_s
340353
}
341354
},
342355
{
343356
type: 'global',
344357
object: {
345358
type: 'object',
346-
objectId: "#{i}:global"
359+
objectId: rand.to_s
347360
}
348361
}
349362
],
350363
this: {
351364
type: 'object'
352365
}
353366
}
367+
local_scope[:scopeChain].each {|s|
368+
oid = s.dig(:object, :objectId)
369+
@frame_id_map[oid] = i
370+
}
371+
local_scope
354372
}
355373
}
356374
when :evaluate
@@ -360,26 +378,32 @@ def process_cdp args
360378
rescue Exception => e
361379
result = e
362380
end
363-
event! :cdp_result, :evaluate, req, evaluate_result(result)
381+
event! :cdp_result, :evaluate, req, [evaluate_result(result)]
364382
when :properties
365-
fid = args.shift
366-
frame = @target_frames[fid]
367-
if b = frame.binding
368-
vars = b.local_variables.map{|name|
369-
v = b.local_variable_get(name)
370-
variable(name, v)
371-
}
372-
vars.unshift variable('%raised', frame.raised_exception) if frame.has_raised_exception
373-
vars.unshift variable('%return', frame.return_value) if frame.has_return_value
374-
vars.unshift variable('%self', b.receiver)
375-
elsif lvars = frame.local_variables
376-
vars = lvars.map{|var, val|
377-
variable(var, val)
378-
}
383+
oid = args.shift
384+
if fid = @frame_id_map[oid]
385+
frame = @target_frames[fid]
386+
if b = frame.binding
387+
vars = b.local_variables.map{|name|
388+
v = b.local_variable_get(name)
389+
variable(name, v)
390+
}
391+
vars.unshift variable('%raised', frame.raised_exception) if frame.has_raised_exception
392+
vars.unshift variable('%return', frame.return_value) if frame.has_return_value
393+
vars.unshift variable('%self', b.receiver)
394+
elsif lvars = frame.local_variables
395+
vars = lvars.map{|var, val|
396+
variable(var, val)
397+
}
398+
else
399+
vars = [variable('%self', frame.self)]
400+
vars.push variable('%raised', frame.raised_exception) if frame.has_raised_exception
401+
vars.push variable('%return', frame.return_value) if frame.has_return_value
402+
end
403+
elsif objs = @obj_map[oid]
404+
vars = parse_object(objs)
379405
else
380-
vars = [variable('%self', frame.self)]
381-
vars.push variable('%raised', frame.raised_exception) if frame.has_raised_exception
382-
vars.push variable('%return', frame.return_value) if frame.has_return_value
406+
raise "Unknown object id #{oid}"
383407
end
384408
event! :cdp_result, :properties, req, vars
385409
end
@@ -390,21 +414,43 @@ def evaluate_result r
390414
v[:value]
391415
end
392416

393-
def variable_ name, obj, type, use_short: true
394-
{
417+
def parse_object objs
418+
case objs
419+
when Array
420+
objs.map.with_index{|obj, i| variable i.to_s, obj}
421+
when Hash
422+
objs.map{|k, v| variable k, v}
423+
end
424+
end
425+
426+
def variable_ name, obj, type, description: nil, use_short: true
427+
prop = {
395428
name: name,
396429
value: {
397430
type: type,
398-
value: DEBUGGER__.short_inspect(obj, use_short)
431+
description: obj.inspect,
432+
value: obj,
399433
},
400434
configurable: true,
401435
enumerable: true
402436
}
437+
if description
438+
v = prop[:value]
439+
v[:description] = description
440+
v[:objectId] = oid = rand.to_s
441+
v[:className] = obj.class
442+
@obj_map[oid] = obj
443+
end
444+
prop
403445
end
404446

405447
def variable name, obj
406448
case obj
407-
when Array, Hash, Range, NilClass, Time
449+
when Array
450+
variable_ name, obj, 'object', description: "Array(#{obj.size})"
451+
when Hash
452+
variable_ name, obj, 'object', description: 'Object'
453+
when Range, NilClass, Time
408454
variable_ name, obj, 'object'
409455
when String
410456
variable_ name, obj, 'string', use_short: false

lib/debug/session.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ def initialize ui
110110
@src_map = {} # {id => src}
111111

112112
@script_paths = [File.absolute_path($0)] # for CDP
113+
@scope_map = {} # { object_id => scope } for CDP
113114

114115
@tp_thread_begin = nil
115116
@tp_load_script = TracePoint.new(:script_compiled){|tp|

lib/debug/thread_client.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ def initialize id, q_evt, q_cmd, thr = Thread.current
8484
@output = []
8585
@frame_formatter = method(:default_frame_formatter)
8686
@var_map = {} # { thread_local_var_id => obj } for DAP
87+
@obj_map = {} # { object_id => obj } for CDP
88+
@frame_id_map = {} # { object_id => frame_id } for CDP
8789
@recorder = nil
8890
@mode = :waiting
8991
set_mode :running

0 commit comments

Comments
 (0)