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.



2474
2475
2476
2477
# File 'lib/syntax_tree/yarv/instructions.rb', line 2474

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

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



2472
2473
2474
# File 'lib/syntax_tree/yarv/instructions.rb', line 2472

def cache
  @cache
end

#iseqObject (readonly)

Returns the value of attribute iseq.



2472
2473
2474
# File 'lib/syntax_tree/yarv/instructions.rb', line 2472

def iseq
  @iseq
end

Instance Method Details

#call(vm) ⇒ Object



2504
2505
2506
2507
2508
# File 'lib/syntax_tree/yarv/instructions.rb', line 2504

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

#canonicalObject



2500
2501
2502
# File 'lib/syntax_tree/yarv/instructions.rb', line 2500

def canonical
  self
end

#disasm(fmt) ⇒ Object



2479
2480
2481
2482
# File 'lib/syntax_tree/yarv/instructions.rb', line 2479

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

#lengthObject



2488
2489
2490
# File 'lib/syntax_tree/yarv/instructions.rb', line 2488

def length
  3
end

#popsObject



2492
2493
2494
# File 'lib/syntax_tree/yarv/instructions.rb', line 2492

def pops
  0
end

#pushesObject



2496
2497
2498
# File 'lib/syntax_tree/yarv/instructions.rb', line 2496

def pushes
  1
end

#to_a(_iseq) ⇒ Object



2484
2485
2486
# File 'lib/syntax_tree/yarv/instructions.rb', line 2484

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