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(stack = [], &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(stack=[], &block)
  @stack = 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.



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

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.



49
50
51
# File 'lib/fOOrth/library/fiber_library.rb', line 49

def yield
  Fiber.yield(ALIVE)
end

#yield_value(value) ⇒ Object

Yield a value back to the thread.



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

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