Skip to content

Commit 53ebd28

Browse files
committed
bah
1 parent 5d339d3 commit 53ebd28

1 file changed

Lines changed: 58 additions & 16 deletions

File tree

‎v4-cokapi/backends/ruby/pg_logger.rb‎

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@
99
# raise "error msg" unless <condition to assert>
1010

1111
# TODO
12-
# - put syntax error information into trace
1312
# - display constants defined INSIDE OF modules or classes
1413
# - display class and instance variables
1514
# - display the 'binding' within a proc/lambda object, which represents
1615
# 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
2018
# - support gets() for user input using the restart hack mechanism
2119
# - user input stored in $_
2220
# - support 'include'-ing a module and bringing in variables into namespace
@@ -67,8 +65,12 @@ class MaxStepsException < RuntimeError
6765

6866

6967
# 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
7069
base_globals_set = global_variables
7170
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'
7274

7375

7476
pg_tracer = TracePoint.new(:line,:class,:end,:call,:return,:raise,:b_call,:b_return) do |tp|
@@ -131,13 +133,50 @@ class MaxStepsException < RuntimeError
131133
STDERR.print 'Constants: '
132134
STDERR.puts toplevel_constants.inspect
133135
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|
135137
val = eval(varname.to_s) # TODO: is there a better way? this seems hacky!
136138
STDERR.print varname, ' -> ', val.inspect
137139
STDERR.puts
138140
globals[varname.to_s] = val.inspect # TODO: make into true objects later
139141
end
140142

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
141180

142181
if tp.event == :raise
143182
STDERR.print 'RAISE!!! '
@@ -280,17 +319,20 @@ def self.iseq_local_variables iseq
280319
puts lineno
281320
puts exc_message
282321

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
294336
rescue MaxStepsException
295337
$stdout = STDOUT
296338

0 commit comments

Comments
 (0)