Class: SyntaxTree::YARV::Once
- Inherits:
-
Instruction
- Object
- Instruction
- SyntaxTree::YARV::Once
- Defined in:
- lib/syntax_tree/yarv/instructions.rb
Overview
### Summary
‘once` is an instruction that wraps an instruction sequence and ensures that is it only ever executed once for the lifetime of the program. It uses a cache to ensure that it is only executed once. It pushes the result of running the instruction sequence onto the stack.
### Usage
~~~ruby END { puts “END” } ~~~
Instance Attribute Summary collapse
-
#cache ⇒ Object
readonly
Returns the value of attribute cache.
-
#iseq ⇒ Object
readonly
Returns the value of attribute iseq.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #call(vm) ⇒ Object
- #deconstruct_keys(_keys) ⇒ Object
- #disasm(fmt) ⇒ Object
-
#initialize(iseq, cache) ⇒ Once
constructor
A new instance of Once.
- #length ⇒ Object
- #pushes ⇒ Object
- #to_a(_iseq) ⇒ Object
Methods inherited from Instruction
#branch_targets, #canonical, #falls_through?, #leaves?, #pops, #side_effects?
Constructor Details
#initialize(iseq, cache) ⇒ Once
Returns a new instance of Once.
2618 2619 2620 2621 |
# File 'lib/syntax_tree/yarv/instructions.rb', line 2618 def initialize(iseq, cache) @iseq = iseq @cache = cache end |
Instance Attribute Details
#cache ⇒ Object (readonly)
Returns the value of attribute cache.
2616 2617 2618 |
# File 'lib/syntax_tree/yarv/instructions.rb', line 2616 def cache @cache end |
#iseq ⇒ Object (readonly)
Returns the value of attribute iseq.
2616 2617 2618 |
# File 'lib/syntax_tree/yarv/instructions.rb', line 2616 def iseq @iseq end |
Instance Method Details
#==(other) ⇒ Object
2636 2637 2638 |
# File 'lib/syntax_tree/yarv/instructions.rb', line 2636 def ==(other) other.is_a?(Once) && other.iseq == iseq && other.cache == cache end |
#call(vm) ⇒ Object
2648 2649 2650 2651 2652 |
# File 'lib/syntax_tree/yarv/instructions.rb', line 2648 def call(vm) return if @executed vm.push(vm.run_block_frame(iseq, vm.frame)) @executed = true end |
#deconstruct_keys(_keys) ⇒ Object
2632 2633 2634 |
# File 'lib/syntax_tree/yarv/instructions.rb', line 2632 def deconstruct_keys(_keys) { iseq: iseq, cache: cache } end |
#disasm(fmt) ⇒ Object
2623 2624 2625 2626 |
# File 'lib/syntax_tree/yarv/instructions.rb', line 2623 def disasm(fmt) fmt.enqueue(iseq) fmt.instruction("once", [iseq.name, fmt.inline_storage(cache)]) end |
#length ⇒ Object
2640 2641 2642 |
# File 'lib/syntax_tree/yarv/instructions.rb', line 2640 def length 3 end |
#pushes ⇒ Object
2644 2645 2646 |
# File 'lib/syntax_tree/yarv/instructions.rb', line 2644 def pushes 1 end |
#to_a(_iseq) ⇒ Object
2628 2629 2630 |
# File 'lib/syntax_tree/yarv/instructions.rb', line 2628 def to_a(_iseq) [:once, iseq.to_a, cache] end |