|
9 | 9 | # raise "error msg" unless <condition to assert> |
10 | 10 |
|
11 | 11 | # TODO |
12 | | -# - put syntax error information into trace |
13 | 12 | # - display constants defined INSIDE OF modules or classes |
14 | 13 | # - display class and instance variables |
15 | 14 | # - display the 'binding' within a proc/lambda object, which represents |
16 | 15 | # its closure |
17 | | -# - toplevel methods, instance, and class variables belong to 'Object', |
18 | | -# so how do we cleanly visualize this? maybe with a special 'Object' |
19 | | -# frame?!? |
| 16 | +# - for simplicity, maybe consolidate the '<main>' frame into the global |
| 17 | +# frame, so that 'locals' defined in main and methods appear together |
20 | 18 | # - support gets() for user input using the restart hack mechanism |
21 | 19 | # - user input stored in $_ |
22 | 20 | # - support 'include'-ing a module and bringing in variables into namespace |
@@ -67,8 +65,12 @@ class MaxStepsException < RuntimeError |
67 | 65 |
|
68 | 66 |
|
69 | 67 | # collect the sets of these variables RIGHT BEFORE the user's code runs |
| 68 | +# so that we can do a set difference later to see what the user defined |
70 | 69 | base_globals_set = global_variables |
71 | 70 | base_constants_set = Module.constants |
| 71 | +base_inst_vars_set = self.instance_variables # toplevel instance variables are set on 'self' |
| 72 | +base_class_vars_set = Object.class_variables # toplevel class variables are set on 'Object' |
| 73 | +base_methods_set = Object.private_methods # toplevel defined methods are set on 'Object' |
72 | 74 |
|
73 | 75 |
|
74 | 76 | pg_tracer = TracePoint.new(:line,:class,:end,:call,:return,:raise,:b_call,:b_return) do |tp| |
@@ -131,13 +133,50 @@ class MaxStepsException < RuntimeError |
131 | 133 | STDERR.print 'Constants: ' |
132 | 134 | STDERR.puts toplevel_constants.inspect |
133 | 135 | entry['ordered_globals'] += toplevel_constants.map { |e| e.to_s } |
134 | | - toplevel_constants.each.with_index do |varname, i| |
| 136 | + toplevel_constants.each do |varname| |
135 | 137 | val = eval(varname.to_s) # TODO: is there a better way? this seems hacky! |
136 | 138 | STDERR.print varname, ' -> ', val.inspect |
137 | 139 | STDERR.puts |
138 | 140 | globals[varname.to_s] = val.inspect # TODO: make into true objects later |
139 | 141 | end |
140 | 142 |
|
| 143 | + # toplevel methods, class vars, and instance vars |
| 144 | + # just stuff them in globals, even though technically they're not |
| 145 | + # really global variables in Ruby; they're part of the weird 'self' |
| 146 | + # instance and also 'Object' itself ... quirky |
| 147 | + toplevel_methods = Object.private_methods - base_methods_set |
| 148 | + toplevel_class_vars = Object.class_variables - base_class_vars_set |
| 149 | + toplevel_inst_vars = self.instance_variables - base_inst_vars_set |
| 150 | + |
| 151 | + STDERR.puts 'toplevel_methods: %s' % toplevel_methods.inspect |
| 152 | + STDERR.puts 'toplevel_class_vars: %s' % toplevel_class_vars.inspect |
| 153 | + STDERR.puts 'toplevel_inst_vars: %s' % toplevel_inst_vars.inspect |
| 154 | + |
| 155 | + entry['ordered_globals'] += toplevel_methods.map { |e| e.to_s } |
| 156 | + entry['ordered_globals'] += toplevel_class_vars.map { |e| e.to_s } |
| 157 | + entry['ordered_globals'] += toplevel_inst_vars.map { |e| e.to_s } |
| 158 | + |
| 159 | + |
| 160 | + toplevel_methods.each do |varname| |
| 161 | + val = Object.method(varname) |
| 162 | + STDERR.print varname, ' -> ', val.inspect |
| 163 | + STDERR.puts |
| 164 | + globals[varname.to_s] = val.inspect # TODO: make into true objects later |
| 165 | + end |
| 166 | + |
| 167 | + toplevel_class_vars.each do |varname| |
| 168 | + val = Object.class_variable_get(varname) |
| 169 | + STDERR.print varname, ' -> ', val.inspect |
| 170 | + STDERR.puts |
| 171 | + globals[varname.to_s] = val.inspect # TODO: make into true objects later |
| 172 | + end |
| 173 | + |
| 174 | + toplevel_inst_vars.each do |varname| |
| 175 | + val = self.instance_variable_get(varname) |
| 176 | + STDERR.print varname, ' -> ', val.inspect |
| 177 | + STDERR.puts |
| 178 | + globals[varname.to_s] = val.inspect # TODO: make into true objects later |
| 179 | + end |
141 | 180 |
|
142 | 181 | if tp.event == :raise |
143 | 182 | STDERR.print 'RAISE!!! ' |
@@ -280,17 +319,20 @@ def self.iseq_local_variables iseq |
280 | 319 | puts lineno |
281 | 320 | puts exc_message |
282 | 321 |
|
283 | | - # TODO: put syntax error information into trace |
284 | | - # "If the trace has exactly 1 entry and it's an uncaught_exception, then |
285 | | - # the OPT frontend doesn't switch to the visualization at all ... |
286 | | - # instead it displays a "syntax error"-like thingy in the code editor. |
287 | | - # It will highlight the faulting line, indicated by the line field and |
288 | | - # display exception_msg there. So if you want to indicate a syntax |
289 | | - # error, then create a trace with exactly ONE entry that's an |
290 | | - # uncaught_exception. (If there's more than one trace entry and the last |
291 | | - # one happens to be uncaught_exception, then right now OPT still won't |
292 | | - # switch to the visualization and instead issue an error message. TODO: |
293 | | - # figure out what's a more desirable behavior here.)" |
| 322 | + # From: https://github.com/pgbovine/OnlinePythonTutor/blob/master/v3/docs/opt-trace-format.md |
| 323 | + # "If the trace has exactly 1 entry and it's an uncaught_exception, then |
| 324 | + # the OPT frontend doesn't switch to the visualization at all ... |
| 325 | + # instead it displays a "syntax error"-like thingy in the code editor. |
| 326 | + # It will highlight the faulting line, indicated by the line field and |
| 327 | + # display exception_msg there. So if you want to indicate a syntax error, |
| 328 | + # then create a trace with exactly ONE entry that's an uncaught_exception. |
| 329 | + singleton_entry = {} |
| 330 | + # mutate cur_trace in place rather than reassigning, since res already includes it |
| 331 | + cur_trace.clear |
| 332 | + cur_trace << singleton_entry |
| 333 | + singleton_entry['event'] = 'uncaught_exception' |
| 334 | + singleton_entry['line'] = lineno |
| 335 | + singleton_entry['exception_msg'] = exc_message |
294 | 336 | rescue MaxStepsException |
295 | 337 | $stdout = STDOUT |
296 | 338 |
|
|
0 commit comments