Class: XfOOrth::XfOOrth_Fiber

Inherits:
Object
  • Object
show all
Defined in:
lib/fOOrth/library/fiber_library.rb

Overview

The fOOrth Fiber class.

Constant Summary collapse

NEW =

Tag for newly created fibers.

"new".freeze
ALIVE =

Tag for running fibers.

"alive".freeze
DEAD =

Tag for defunct fibers.

"dead".freeze

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ XfOOrth_Fiber

Build up the fiber instance. A fiber is a light-weight coroutine.



22
23
24
25
26
# File 'lib/fOOrth/library/fiber_library.rb', line 22

def initialize(&block)
  @stack = []
  @fiber = Fiber.new &lambda{|vm| block.call(vm); nil}
  @status = NEW
end

Instance Method Details

#statusObject

What is the status of this fiber?



34
35
36
# File 'lib/fOOrth/library/fiber_library.rb', line 34

def status
  @status || DEAD
end

#step(vm) ⇒ Object

Let the fiber run for one step.
Endemic Code Smells

  • :reek:DuplicateMethodCall



41
42
43
44
45
46
47
48
# File 'lib/fOOrth/library/fiber_library.rb', line 41

def step(vm)
  vm.data_stack, vm.fiber, @save = @stack, self, vm.data_stack
  @status = @fiber.resume(vm)
rescue FiberError
  error "F72: The fiber is dead, no further steps can be taken."
ensure
  vm.data_stack, vm.fiber, @stack = @save, nil, vm.data_stack
end

#to_foorth_fiberObject

Return this fiber as a fiber.



29
30
31
# File 'lib/fOOrth/library/fiber_library.rb', line 29

def to_foorth_fiber
  self
end

#yieldObject

Yield back to the thread.
Endemic Code Smells

  • :reek:UtilityFunction



53
54
55
# File 'lib/fOOrth/library/fiber_library.rb', line 53

def yield
  Fiber.yield(ALIVE)
end

#yield_value(value) ⇒ Object

Yield a value back to the thread.



58
59
60
61
# File 'lib/fOOrth/library/fiber_library.rb', line 58

def yield_value(value)
  @save << value
  Fiber.yield(ALIVE)
end