2323# manifests in other languages as well.
2424
2525# 2016-05-01: super hacky -- prefaced all global variables with a munged
26- # name prefix like '__opt_ruby_global__ ' to prevent name clashes with
26+ # name prefix like '__opt_global__ ' to prevent name clashes with
2727# the user's script. right now the script is apparently eval'ed in the
2828# same scope as the tracer, so it can shadow the tracer's variables,
2929# leading to bad results. the real fix is to get the bindings right, but
3737
3838require 'debug_inspector' # gem install debug_inspector, use on Ruby 2.X
3939
40- trace_output_fn = 'stdout' # default
41- script_name = nil
42- cod = nil
40+ # always preface globals with __opt_global__ (hacky!)
41+ __opt_global__trace_output_fn = 'stdout' # default
42+ __opt_global__script_name = nil
43+ __opt_global__cod = nil
4344
4445OptionParser . new do |opts |
4546 opts . banner = "Usage: ./ruby pg_logger.rb [options]"
4647
4748 opts . on ( "-t" , "--tracefile" , "Create a test-trace.js trace file" ) do |t |
48- trace_output_fn = "../../../v3/test-trace.js" if t
49+ __opt_global__trace_output_fn = "../../../v3/test-trace.js" if t
4950 end
5051
5152 opts . on ( "-f" , "--file FILE" , "Execute a .rb file" ) do |s |
52- script_name = s
53+ __opt_global__script_name = s
5354 end
5455
5556 opts . on ( "-c" , "--code CODE" , String , "Execute Ruby code from string" ) do |c |
56- cod = c
57+ __opt_global__cod = c
5758 end
5859
5960end . parse!
6061
6162# -c gets precedence over -f
62- if !cod
63- cod = File . open ( script_name ) . read
63+ if !__opt_global__cod
64+ __opt_global__cod = File . open ( __opt_global__script_name ) . read
6465end
6566
66- cur_trace = [ ]
67- res = { 'code' => cod . dup , # make a snapshot of the code NOW before it's modified
68- 'trace' => cur_trace }
67+ # always preface globals with __opt_global__ (hacky!)
68+ __opt_global__cur_trace = [ ]
69+ __opt_global__res = { 'code' => __opt_global__cod . dup , # make a snapshot of the code NOW before it's modified
70+ 'trace' => __opt_global__cur_trace }
6971
7072# canonicalize to pretty-print
71- cur_frame_id = 1
72- ordered_frame_ids = { }
73+ __opt_global__cur_frame_id = 1
74+ __opt_global__ordered_frame_ids = { }
7375
74- stdout_buffer = StringIO . new
76+ __opt_global__stdout_buffer = StringIO . new
7577
76- n_steps = 0
77- #MAX_STEPS = 300
78- MAX_STEPS = 1000 # on 2016-05-01, I increased the limit from 300 to 1000 for Ruby due to popular user demand! and I also improved the warning message
78+ __opt_global__n_steps = 0
79+ #__opt_global__MAX_STEPS = 300
80+ __opt_global__MAX_STEPS = 1000 # on 2016-05-01, I increased the limit from 300 to 1000 for Ruby due to popular user demand! and I also improved the warning message
7981
80- n_lines_added = nil
82+ __opt_global__n_lines_added = nil
8183
8284class MaxStepsException < RuntimeError
8385end
8486
85- PRIMITIVES = [ Fixnum , Float , String , TrueClass , FalseClass , NilClass ]
87+ OPT_GLOBAL_PRIMITIVES = [ Fixnum , Float , String , TrueClass , FalseClass , NilClass ]
8688
8789# ported from ../../../v3/pg_encoder.py
8890class ObjectEncoder
@@ -109,7 +111,7 @@ def reset_heap
109111 # return either a primitive object or an object reference;
110112 # and as a side effect, update encoded_heap_objects
111113 def encode ( dat )
112- if PRIMITIVES . include? dat . class
114+ if OPT_GLOBAL_PRIMITIVES . include? dat . class
113115 if dat . class == Float
114116 if dat == Float ::INFINITY
115117 return [ 'SPECIAL_FLOAT' , 'Infinity' ]
@@ -305,7 +307,7 @@ def encode(dat)
305307
306308# do NOT rename this anything other than pg_encoder since we refer to it
307309# later by name
308- pg_encoder = ObjectEncoder . new
310+ __opt_global__pg_encoder = ObjectEncoder . new
309311
310312
311313# end all of my own definitions here so that I can set the following vars ...
@@ -319,21 +321,21 @@ def encode(dat)
319321base_methods_set = Object . private_methods # toplevel defined methods are set on 'Object'
320322
321323
322- # do NOT rename this anything other than pg_tracer since we refer to it
324+ # do NOT rename this anything other than __opt_global__pg_tracer since we refer to it
323325# later by name
324- pg_tracer = TracePoint . new ( :line , :class , :end , :call , :return , :raise , :b_call , :b_return ) do |tp |
326+ __opt_global__pg_tracer = TracePoint . new ( :line , :class , :end , :call , :return , :raise , :b_call , :b_return ) do |tp |
325327 next if tp . path != '(eval)' # 'next' is a 'return' from a block
326328
327- raise MaxStepsException if n_steps > MAX_STEPS
329+ raise MaxStepsException if __opt_global__n_steps > __opt_global__MAX_STEPS
328330
329331 # TODO: look into tp.defined_class and tp.method_id attrs
330332
331- pg_encoder . reset_heap ( ) # VERY VERY VERY IMPORTANT, or else we won't properly
333+ __opt_global__pg_encoder . reset_heap ( ) # VERY VERY VERY IMPORTANT, or else we won't properly
332334 # capture heap object mutations in the trace!
333335
334336 retval = nil
335337 if tp . event == :return || tp . event == :b_return
336- retval = pg_encoder . encode ( tp . return_value )
338+ retval = __opt_global__pg_encoder . encode ( tp . return_value )
337339 end
338340
339341 evt_type = case tp . event
@@ -350,11 +352,11 @@ def encode(dat)
350352 entry [ 'event' ] = evt_type
351353
352354 # make a copy to take a snapshot at this point in time
353- entry [ 'stdout' ] = stdout_buffer . string . dup
355+ entry [ 'stdout' ] = __opt_global__stdout_buffer . string . dup
354356
355357 stack = [ ]
356358 entry [ 'stack_to_render' ] = stack
357- entry [ 'heap' ] = pg_encoder . get_heap
359+ entry [ 'heap' ] = __opt_global__pg_encoder . get_heap
358360
359361 # globals
360362 globals = { }
@@ -365,15 +367,15 @@ def encode(dat)
365367
366368 true_globals . each . with_index do |varname , i |
367369 val = eval ( varname . to_s ) # TODO: is there a better way? this seems hacky!
368- globals [ varname . to_s ] = pg_encoder . encode ( val )
370+ globals [ varname . to_s ] = __opt_global__pg_encoder . encode ( val )
369371 end
370372
371373 # toplevel constants (stuff them in globals)
372374 toplevel_constants = ( Module . constants - base_constants_set ) # set difference
373375 entry [ 'ordered_globals' ] . concat ( toplevel_constants . map { |e | e . to_s } )
374376 toplevel_constants . each do |varname |
375377 val = Module . const_get ( varname )
376- globals [ varname . to_s ] = pg_encoder . encode ( val )
378+ globals [ varname . to_s ] = __opt_global__pg_encoder . encode ( val )
377379 end
378380
379381 # toplevel methods, class vars, and instance vars
@@ -391,17 +393,17 @@ def encode(dat)
391393
392394 toplevel_methods . each do |varname |
393395 val = Object . method ( varname )
394- globals [ varname . to_s ] = pg_encoder . encode ( val )
396+ globals [ varname . to_s ] = __opt_global__pg_encoder . encode ( val )
395397 end
396398
397399 toplevel_class_vars . each do |varname |
398400 val = Object . class_variable_get ( varname )
399- globals [ varname . to_s ] = pg_encoder . encode ( val )
401+ globals [ varname . to_s ] = __opt_global__pg_encoder . encode ( val )
400402 end
401403
402404 toplevel_inst_vars . each do |varname |
403405 val = self . instance_variable_get ( varname )
404- globals [ varname . to_s ] = pg_encoder . encode ( val )
406+ globals [ varname . to_s ] = __opt_global__pg_encoder . encode ( val )
405407 end
406408
407409 if tp . event == :raise
@@ -438,11 +440,11 @@ def self.iseq_local_variables iseq
438440 b = dc . frame_binding ( i )
439441
440442 # frame_id field exists only in my hacked Ruby interpreter!
441- canonical_fid = ordered_frame_ids [ b . frame_id ]
443+ canonical_fid = __opt_global__ordered_frame_ids [ b . frame_id ]
442444 if !canonical_fid
443- canonical_fid = cur_frame_id
444- ordered_frame_ids [ b . frame_id ] = cur_frame_id
445- cur_frame_id += 1
445+ canonical_fid = __opt_global__cur_frame_id
446+ __opt_global__ordered_frame_ids [ b . frame_id ] = __opt_global__cur_frame_id
447+ __opt_global__cur_frame_id += 1
446448 end
447449
448450 # special-case handling for the toplevel '<main>' frame
@@ -469,7 +471,7 @@ def self.iseq_local_variables iseq
469471 begin
470472 begin
471473 v = b . local_variable_get ( lv )
472- r [ lv ] = pg_encoder . encode ( v )
474+ r [ lv ] = __opt_global__pg_encoder . encode ( v )
473475 rescue
474476 # iterating over range values will make 'lv' into
475477 # potentially literal values such as integers, so they
@@ -492,18 +494,18 @@ def self.iseq_local_variables iseq
492494
493495 # OK this is RIDICULOUSLY RIDICULOUSLY hacky. if this frame is
494496 # defined at the toplevel, then its parent frame will include
495- # pg_tracer and pg_encoder , which is defined in THIS VERY FILE!!!
497+ # __opt_global__pg_tracer and __opt_global__pg_encoder , which is defined in THIS VERY FILE!!!
496498 # if that's the case, then totally punt since we don't want to
497499 # print out spurious data that's not even in the user's program.
498500 # ughhh this is sooooooo hacky!
499- if ( !all_local_vars . include? ( :pg_tracer ) &&
500- !all_local_vars . include? ( :pg_encoder ) )
501+ if ( !all_local_vars . include? ( :__opt_global__pg_tracer ) &&
502+ !all_local_vars . include? ( :__opt_global__pg_encoder ) )
501503 parent_frame_local_vars = ( all_local_vars - lvs )
502504 parent_lvs_val = parent_frame_local_vars . inject ( lvs_val ) { |r , lv |
503505 begin
504506 varname = 'parent:' + lv . to_s
505507 v = b . local_variable_get ( lv )
506- r [ varname ] = pg_encoder . encode ( v )
508+ r [ varname ] = __opt_global__pg_encoder . encode ( v )
507509 stack_entry [ 'ordered_varnames' ] << varname
508510 rescue NameError
509511 # ignore
@@ -516,7 +518,7 @@ def self.iseq_local_variables iseq
516518 my_self = b . eval ( 'self' )
517519 if my_self != self # ignore default global 'self' for brevity
518520 stack_entry [ 'ordered_varnames' ] . insert ( 0 , 'self' ) # insert at front
519- stack_entry [ 'encoded_locals' ] [ 'self' ] = pg_encoder . encode ( my_self )
521+ stack_entry [ 'encoded_locals' ] [ 'self' ] = __opt_global__pg_encoder . encode ( my_self )
520522 end
521523
522524 # just fold everything into globals rather than creating a
@@ -550,31 +552,31 @@ def self.iseq_local_variables iseq
550552 # now REVERSE the stack so that it grows downward
551553 stack . reverse!
552554
553- n_steps += 1
555+ __opt_global__n_steps += 1
554556
555- cur_trace << entry
557+ __opt_global__cur_trace << entry
556558end
557559
558560
559561begin
560562 # we are redirecting stdout so we need to print all warnings to stderr!
561- pg_tracer . enable
562- $stdout = stdout_buffer
563+ __opt_global__pg_tracer . enable
564+ $stdout = __opt_global__stdout_buffer
563565
564566 # super-hack: add an extra 'nil' line to execute at the end so that the
565567 # tracer can easily pick up on the final executed line in '(eval)'
566568 # - this isn't a 'real' line number in the user's code since we've
567569 # inserted an extra line
568- if cod [ -1 ] == "\n "
569- cod << "nil"
570- n_lines_added = 1
570+ if __opt_global__cod [ -1 ] == "\n "
571+ __opt_global__cod << "nil"
572+ __opt_global__n_lines_added = 1
571573 else
572- cod << "\n nil"
573- n_lines_added = 1 # pretty sure this is 1 and not 2
574+ __opt_global__cod << "\n nil"
575+ __opt_global__n_lines_added = 1 # pretty sure this is 1 and not 2
574576 end
575577
576- eval ( cod ) # the filename of the user's code is '(eval)'
577- pg_tracer . disable
578+ eval ( __opt_global__cod ) # the filename of the user's code is '(eval)'
579+ __opt_global__pg_tracer . disable
578580rescue SyntaxError
579581 $stdout = STDOUT
580582 exc_object = $!
@@ -596,19 +598,19 @@ def self.iseq_local_variables iseq
596598 # display exception_msg there. So if you want to indicate a syntax error,
597599 # then create a trace with exactly ONE entry that's an uncaught_exception.
598600 singleton_entry = { }
599- # mutate cur_trace in place rather than reassigning, since res already includes it
600- cur_trace . clear
601- cur_trace << singleton_entry
601+ # mutate __opt_global__cur_trace in place rather than reassigning, since __opt_global__res already includes it
602+ __opt_global__cur_trace . clear
603+ __opt_global__cur_trace << singleton_entry
602604 singleton_entry [ 'event' ] = 'uncaught_exception'
603605 singleton_entry [ 'line' ] = lineno
604606 singleton_entry [ 'exception_msg' ] = exc_message
605607rescue MaxStepsException
606608 $stdout = STDOUT
607609
608610 # take the final trace entry & make it into a instruction_limit_reached event
609- if cur_trace . length > 0
610- cur_trace [ -1 ] [ 'event' ] = 'instruction_limit_reached'
611- cur_trace [ -1 ] [ 'exception_msg' ] = "Stopped after running %d steps. Please shorten your code,\n since Python Tutor is not designed to handle long-running code." % MAX_STEPS
611+ if __opt_global__cur_trace . length > 0
612+ __opt_global__cur_trace [ -1 ] [ 'event' ] = 'instruction_limit_reached'
613+ __opt_global__cur_trace [ -1 ] [ 'exception_msg' ] = "Stopped after running %d steps. Please shorten your code,\n since Python Tutor is not designed to handle long-running code." % __opt_global__MAX_STEPS
612614 end
613615rescue
614616 $stdout = STDOUT
@@ -623,18 +625,18 @@ def self.iseq_local_variables iseq
623625 # final instruction's line number to the last line of the file
624626 # (only do this if the trace isn't a single element, which likely
625627 # indicates some sort of uncaught_exception)
626- if cur_trace . length > 1 && n_lines_added
627- cur_trace [ -1 ] [ 'line' ] -= n_lines_added
628+ if __opt_global__cur_trace . length > 1 && __opt_global__n_lines_added
629+ __opt_global__cur_trace [ -1 ] [ 'line' ] -= __opt_global__n_lines_added
628630 end
629631
630632 # postprocessing into a trace
631- trace_json = JSON . pretty_generate ( res )
632- if trace_output_fn == 'stdout'
633+ trace_json = JSON . pretty_generate ( __opt_global__res )
634+ if __opt_global__trace_output_fn == 'stdout'
633635 STDOUT . write ( trace_json )
634636 else
635- File . open ( trace_output_fn , 'w' ) do |f |
637+ File . open ( __opt_global__trace_output_fn , 'w' ) do |f |
636638 f . write ( 'var trace = ' + trace_json )
637639 end
638- puts "Trace written to " + trace_output_fn
640+ puts "Trace written to " + __opt_global__trace_output_fn
639641 end
640642end
0 commit comments