Class: SyntaxTree::YARV::Once

Inherits:
Object
  • Object
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

Constructor Details

#initialize(iseq, cache) ⇒ Once

Returns a new instance of Once.



2806
2807
2808
2809
# File 'lib/syntax_tree/yarv/instructions.rb', line 2806

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

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



2804
2805
2806
# File 'lib/syntax_tree/yarv/instructions.rb', line 2804

def cache
  @cache
end

#iseqObject (readonly)

Returns the value of attribute iseq.



2804
2805
2806
# File 'lib/syntax_tree/yarv/instructions.rb', line 2804

def iseq
  @iseq
end

Instance Method Details

#==(other) ⇒ Object



2824
2825
2826
# File 'lib/syntax_tree/yarv/instructions.rb', line 2824

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

#call(vm) ⇒ Object



2844
2845
2846
2847
2848
# File 'lib/syntax_tree/yarv/instructions.rb', line 2844

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

#canonicalObject



2840
2841
2842
# File 'lib/syntax_tree/yarv/instructions.rb', line 2840

def canonical
  self
end

#deconstruct_keys(_keys) ⇒ Object



2820
2821
2822
# File 'lib/syntax_tree/yarv/instructions.rb', line 2820

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

#disasm(fmt) ⇒ Object



2811
2812
2813
2814
# File 'lib/syntax_tree/yarv/instructions.rb', line 2811

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

#lengthObject



2828
2829
2830
# File 'lib/syntax_tree/yarv/instructions.rb', line 2828

def length
  3
end

#popsObject



2832
2833
2834
# File 'lib/syntax_tree/yarv/instructions.rb', line 2832

def pops
  0
end

#pushesObject



2836
2837
2838
# File 'lib/syntax_tree/yarv/instructions.rb', line 2836

def pushes
  1
end

#to_a(_iseq) ⇒ Object



2816
2817
2818
# File 'lib/syntax_tree/yarv/instructions.rb', line 2816

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