Skip to content

Commit 1dc7108

Browse files
committed
displays closures
1 parent 7f76f91 commit 1dc7108

2 files changed

Lines changed: 37 additions & 13 deletions

File tree

‎v4-cokapi/Dockerfile‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ RUN chmod a+x /tmp/run-java-backend.sh
7474

7575
# Ruby backend
7676
COPY backends/ruby/pg_logger.rb /tmp/ruby/
77+
RUN cd /tmp/ruby && ln -s ruby-2.2.2-custom-OPT/bin/ruby
7778

78-
# To properly run tests, become root:
79+
# To properly run Ruby tests, become root:
7980
# $ docker run -t -i --rm --net=none --cap-drop all pgbovine/cokapi:v1 bash
80-
COPY backends/ruby/golden_test.py /tmp/ruby/
81-
RUN mkdir /tmp/ruby/tests
82-
ADD backends/ruby/tests /tmp/ruby/tests
83-
RUN cd /tmp/ruby && ln -s ruby-2.2.2-custom-OPT/bin/ruby
81+
#COPY backends/ruby/golden_test.py /tmp/ruby/
82+
#RUN mkdir /tmp/ruby/tests
83+
#ADD backends/ruby/tests /tmp/ruby/tests
8484

8585
RUN useradd netuser

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

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,10 @@
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
303296
end
304297

298+
# do NOT rename this anything other than pg_encoder since we refer to it
299+
# later by name
305300
pg_encoder = ObjectEncoder.new
306301

307302

@@ -316,6 +311,8 @@ def encode(dat)
316311
base_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
319316
pg_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

Comments
 (0)