Class: SyntaxTree::YARV::Once

Inherits:
Instruction show all
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

Instance Method Summary collapse

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.



2671
2672
2673
2674
# File 'lib/syntax_tree/yarv/instructions.rb', line 2671

def initialize(iseq, cache)
  @iseq = iseq
  @cache = cache
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



2669
2670
2671
# File 'lib/syntax_tree/yarv/instructions.rb', line 2669

def cache
  @cache
end

#iseqObject (readonly)

Returns the value of attribute iseq.



2669
2670
2671
# File 'lib/syntax_tree/yarv/instructions.rb', line 2669

def iseq
  @iseq
end

Instance Method Details

#==(other) ⇒ Object



2689
2690
2691
# File 'lib/syntax_tree/yarv/instructions.rb', line 2689

def ==(other)
  other.is_a?(Once) && other.iseq == iseq && other.cache == cache
end

#call(vm) ⇒ Object



2701
2702
2703
2704
2705
# File 'lib/syntax_tree/yarv/instructions.rb', line 2701

def call(vm)
  return if @executed
  vm.push(vm.run_block_frame(iseq, vm.frame))
  @executed = true
end

#deconstruct_keys(_keys) ⇒ Object



2685
2686
2687
# File 'lib/syntax_tree/yarv/instructions.rb', line 2685

def deconstruct_keys(_keys)
  { iseq: iseq, cache: cache }
end

#disasm(fmt) ⇒ Object



2676
2677
2678
2679
# File 'lib/syntax_tree/yarv/instructions.rb', line 2676

def disasm(fmt)
  fmt.enqueue(iseq)
  fmt.instruction("once", [iseq.name, fmt.inline_storage(cache)])
end

#lengthObject



2693
2694
2695
# File 'lib/syntax_tree/yarv/instructions.rb', line 2693

def length
  3
end

#pushesObject



2697
2698
2699
# File 'lib/syntax_tree/yarv/instructions.rb', line 2697

def pushes
  1
end

#to_a(_iseq) ⇒ Object



2681
2682
2683
# File 'lib/syntax_tree/yarv/instructions.rb', line 2681

def to_a(_iseq)
  [:once, iseq.to_a, cache]
end