Class: Peruby::LocalStack
- Inherits:
-
Object
- Object
- Peruby::LocalStack
- Defined in:
- lib/peruby/runtime/local_stack.rb
Overview
Restores dynamically localized values in reverse order.
Instance Method Summary collapse
-
#initialize ⇒ LocalStack
constructor
A new instance of LocalStack.
- #localize_array(array, index) ⇒ Object
- #localize_array_value(array) ⇒ Object
-
#localize_entire_glob(glob) ⇒ Object
rubocop:disable Metrics/AbcSize.
- #localize_glob(glob, slot, replacement = nil) ⇒ Object
- #localize_hash(hash, key) ⇒ Object
- #localize_hash_value(hash) ⇒ Object
- #localize_scalar(scalar) ⇒ Object
- #mark ⇒ Object
- #unwind_to(mark) ⇒ Object
- #within ⇒ Object
Constructor Details
#initialize ⇒ LocalStack
Returns a new instance of LocalStack.
6 7 8 |
# File 'lib/peruby/runtime/local_stack.rb', line 6 def initialize @restorers = [] end |
Instance Method Details
#localize_array(array, index) ⇒ Object
27 28 29 30 31 32 |
# File 'lib/peruby/runtime/local_stack.rb', line 27 def localize_array(array, index) index += array.size if index.negative? old = array.cells[index] @restorers << -> { array.replace_cell(index, old) } array.replace_cell(index, Scalar.new) end |
#localize_array_value(array) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/peruby/runtime/local_stack.rb', line 40 def localize_array_value(array) old = array.cells.dup @restorers << -> { array.cells.replace(old) } array.cells.clear array end |
#localize_entire_glob(glob) ⇒ Object
rubocop:disable Metrics/AbcSize
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/peruby/runtime/local_stack.rb', line 54 def localize_entire_glob(glob) # rubocop:disable Metrics/AbcSize scalar = glob.scalar array = glob.array hash = glob.hash old = [scalar.get, array.cells.dup, hash.cells.dup, glob.code, glob.io] @restorers << lambda do scalar.set(old[0]) array.cells.replace(old[1]) hash.cells.replace(old[2]) glob.code = old[3] glob.io = old[4] end scalar.set(nil) array.cells.clear hash.cells.clear glob.code = glob.io = nil glob end |
#localize_glob(glob, slot, replacement = nil) ⇒ Object
14 15 16 17 18 |
# File 'lib/peruby/runtime/local_stack.rb', line 14 def localize_glob(glob, slot, replacement = nil) old = glob.public_send(slot) @restorers << -> { glob.replace(slot, old) } glob.replace(slot, replacement || default_for(slot)) end |
#localize_hash(hash, key) ⇒ Object
20 21 22 23 24 25 |
# File 'lib/peruby/runtime/local_stack.rb', line 20 def localize_hash(hash, key) key = key.to_s old = hash.cells[key] @restorers << -> { hash.replace_cell(key, old) } hash.replace_cell(key, Scalar.new) end |
#localize_hash_value(hash) ⇒ Object
47 48 49 50 51 52 |
# File 'lib/peruby/runtime/local_stack.rb', line 47 def localize_hash_value(hash) old = hash.cells.dup @restorers << -> { hash.cells.replace(old) } hash.cells.clear hash end |
#localize_scalar(scalar) ⇒ Object
34 35 36 37 38 |
# File 'lib/peruby/runtime/local_stack.rb', line 34 def localize_scalar(scalar) old = scalar.get @restorers << -> { scalar.set(old) } scalar.set(nil) end |
#mark ⇒ Object
10 11 12 |
# File 'lib/peruby/runtime/local_stack.rb', line 10 def mark @restorers.size end |
#unwind_to(mark) ⇒ Object
73 74 75 |
# File 'lib/peruby/runtime/local_stack.rb', line 73 def unwind_to(mark) @restorers.pop.call while @restorers.size > mark end |
#within ⇒ Object
77 78 79 80 81 82 |
# File 'lib/peruby/runtime/local_stack.rb', line 77 def within saved = mark yield ensure unwind_to(saved) end |