99
1010# TODO
1111#
12- # - display the 'binding' within a proc/lambda object, which represents
13- # its closure. see tests/proc-return.rb
14- #
1512# - support gets() for taking user input using the restart hack mechanism
1613# that we used for Python. user input stored in $_
1714#
1815# Limitations/quirks:
19- # - no support yet for (lexical) environment pointers, since MRI doesn't
20- # seem to expose them. We can see only the current (dynamic) stack
21- # backtrace with debug_inspector.
22- # - NB: is this true? at least we have 'binding' for procs/lambdas
2316#
2417# - code keeps executing for a few more lines after an exception;
2518# dunno if that's standard Ruby behavior or not
@@ -302,6 +295,8 @@ def encode(dat)
302295 end
303296end
304297
298+ # do NOT rename this anything other than pg_encoder since we refer to it
299+ # later by name
305300pg_encoder = ObjectEncoder . new
306301
307302
@@ -316,6 +311,8 @@ def encode(dat)
316311base_methods_set = Object . private_methods # toplevel defined methods are set on 'Object'
317312
318313
314+ # do NOT rename this anything other than pg_tracer since we refer to it
315+ # later by name
319316pg_tracer = TracePoint . new ( :line , :class , :end , :call , :return , :raise , :b_call , :b_return ) do |tp |
320317 next if tp . path != '(eval)' # 'next' is a 'return' from a block
321318
@@ -455,6 +452,7 @@ def self.iseq_local_variables iseq
455452 stack_entry [ 'is_zombie' ] = false
456453 stack_entry [ 'parent_frame_id_list' ] = [ ]
457454
455+ # these include only your own frame's locals
458456 lvs = iseq_local_variables ( iseq )
459457 lvs_val = lvs . inject ( { } ) { |r , lv |
460458 begin
@@ -463,12 +461,38 @@ def self.iseq_local_variables iseq
463461 rescue NameError
464462 # ignore
465463 end
466- r
464+ r # very important!
467465 }
468466
469467 stack_entry [ 'ordered_varnames' ] = lvs . map { |e | e . to_s }
470468 stack_entry [ 'encoded_locals' ] = lvs_val
471469
470+ # handle closures ...
471+ # these also include variables captured in lexical parents' frames!!!
472+ all_local_vars = b . local_variables
473+
474+ # OK this is RIDICULOUSLY RIDICULOUSLY hacky. if this frame is
475+ # defined at the toplevel, then its parent frame will include
476+ # pg_tracer and pg_encoder, which is defined in THIS VERY FILE!!!
477+ # if that's the case, then totally punt since we don't want to
478+ # print out spurious data that's not even in the user's program.
479+ # ughhh this is sooooooo hacky!
480+ if ( !all_local_vars . include? ( :pg_tracer ) &&
481+ !all_local_vars . include? ( :pg_encoder ) )
482+ parent_frame_local_vars = ( all_local_vars - lvs )
483+ parent_lvs_val = parent_frame_local_vars . inject ( lvs_val ) { |r , lv |
484+ begin
485+ varname = 'parent:' + lv . to_s
486+ v = b . local_variable_get ( lv )
487+ r [ varname ] = pg_encoder . encode ( v )
488+ stack_entry [ 'ordered_varnames' ] << varname
489+ rescue NameError
490+ # ignore
491+ end
492+ r # very important!
493+ }
494+ end
495+
472496 # get the value of 'self' as seen by this frame
473497 my_self = b . eval ( 'self' )
474498 if my_self != self # ignore default global 'self' for brevity
0 commit comments