99# raise "error msg" unless <condition to assert>
1010
1111# TODO
12+ # - put syntax error information into trace
1213# - display constants defined INSIDE OF modules or classes
1314# - display class and instance variables
1415# - display the 'binding' within a proc/lambda object, which represents
1920# - support gets() for user input using the restart hack mechanism
2021# - user input stored in $_
2122# - support 'include'-ing a module and bringing in variables into namespace
22- # - get rid of fake 'nil' on final line and adjust line numbers
2323#
2424# Useful notes from http://phrogz.net/programmingruby/frameset.html:
2525# - "First, every object has a unique object identifier (abbreviated as
4747trace_output_fn = ARGV [ 1 ] || "../../../v3/test-trace.js"
4848
4949cur_trace = [ ]
50- res = { 'code' => cod , 'trace' => cur_trace }
50+ res = { 'code' => cod . dup , # make a snapshot of the code NOW before it's modified
51+ 'trace' => cur_trace }
5152
5253# canonicalize to pretty-print
5354cur_frame_id = 1
5960#MAX_STEPS = 30
6061MAX_STEPS = 300
6162
63+ n_lines_added = nil
64+
6265class MaxStepsException < RuntimeError
6366end
6467
@@ -247,11 +250,15 @@ def self.iseq_local_variables iseq
247250
248251 # super-hack: add an extra 'nil' line to execute at the end so that the
249252 # tracer can easily pick up on the final executed line in '(eval)'
250- # - however, we need to patch up the line number and set event type to
251- # 'return' for the final line's execution.
252253 # - this isn't a 'real' line number in the user's code since we've
253254 # inserted an extra line
254- cod << "\n nil"
255+ if cod [ -1 ] == "\n "
256+ cod << "nil"
257+ n_lines_added = 1
258+ else
259+ cod << "\n nil"
260+ n_lines_added = 2
261+ end
255262
256263 eval ( cod ) # the filename of the user's code is '(eval)'
257264 pg_tracer . disable
@@ -273,7 +280,7 @@ def self.iseq_local_variables iseq
273280 puts lineno
274281 puts exc_message
275282
276- # TODO: put syntax error information on trace
283+ # TODO: put syntax error information into trace
277284 # "If the trace has exactly 1 entry and it's an uncaught_exception, then
278285 # the OPT frontend doesn't switch to the visualization at all ...
279286 # instead it displays a "syntax error"-like thingy in the code editor.
@@ -297,10 +304,19 @@ def self.iseq_local_variables iseq
297304 $stdout = STDOUT
298305 puts "other exception -- EEEEEEEE!!!"
299306 puts $!
300- # ignore since we've already handled a :raise event by now
307+ # ignore since we've already handled a :raise event in the trace by now
301308ensure
302309 $stdout = STDOUT
303310
311+ # super hack -- to account for the fact that we added an extra 'nil'
312+ # instruction at the very end of the user's code, we will set the
313+ # final instruction's line number to the last line of the file
314+ # (only do this if the trace isn't a single element, which likely
315+ # indicates some sort of uncaught_exception)
316+ if cur_trace . length > 1 && n_lines_added
317+ cur_trace [ -1 ] [ 'line' ] -= n_lines_added
318+ end
319+
304320 # postprocessing into a trace
305321 trace_json = JSON . pretty_generate ( res )
306322 File . open ( trace_output_fn , 'w' ) do |f |
0 commit comments