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.



2560
2561
2562
2563
# File 'lib/syntax_tree/yarv/instructions.rb', line 2560

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

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



2558
2559
2560
# File 'lib/syntax_tree/yarv/instructions.rb', line 2558

def cache
  @cache
end

#iseqObject (readonly)

Returns the value of attribute iseq.



2558
2559
2560
# File 'lib/syntax_tree/yarv/instructions.rb', line 2558

def iseq
  @iseq
end

Instance Method Details

#==(other) ⇒ Object



2578
2579
2580
# File 'lib/syntax_tree/yarv/instructions.rb', line 2578

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

#call(vm) ⇒ Object



2590
2591
2592
2593
2594
# File 'lib/syntax_tree/yarv/instructions.rb', line 2590

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

#deconstruct_keys(_keys) ⇒ Object



2574
2575
2576
# File 'lib/syntax_tree/yarv/instructions.rb', line 2574

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

#disasm(fmt) ⇒ Object



2565
2566
2567
2568
# File 'lib/syntax_tree/yarv/instructions.rb', line 2565

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

#lengthObject



2582
2583
2584
# File 'lib/syntax_tree/yarv/instructions.rb', line 2582

def length
  3
end

#pushesObject



2586
2587
2588
# File 'lib/syntax_tree/yarv/instructions.rb', line 2586

def pushes
  1
end

#to_a(_iseq) ⇒ Object



2570
2571
2572
# File 'lib/syntax_tree/yarv/instructions.rb', line 2570

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