Class: Sevgi::Executor::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/sevgi/executor/scope.rb

Overview

Holds one isolated Sevgi script execution scope and its result.

Scope objects belong to one executor run and are pushed onto the current fiber's executor stack. They are not shared between concurrent executions.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope = nil) ⇒ void

Note:

When no module is supplied, the internal module reports its name as Sevgi::Main for readable Ruby error messages without publishing a process-global Sevgi::Main constant.

Creates a script execution scope.

Parameters:

  • scope (Module, nil) (defaults to: nil)

    existing module to evaluate source in



23
24
25
26
27
28
# File 'lib/sevgi/executor/scope.rb', line 23

def initialize(scope = nil)
  @scope = scope || main
  @recent = nil
  @error = nil
  @stack = {}
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



16
# File 'lib/sevgi/executor/scope.rb', line 16

attr_reader :scope, :recent, :error

#recentObject? (readonly)

Returns last expression result from the executed source.

Returns:

  • (Object, nil)

    last expression result from the executed source



16
# File 'lib/sevgi/executor/scope.rb', line 16

attr_reader :scope, :recent, :error

#scopeModule (readonly)

Returns isolated module where script source is evaluated.

Returns:

  • (Module)

    isolated module where script source is evaluated



16
17
18
# File 'lib/sevgi/executor/scope.rb', line 16

def scope
  @scope
end

Instance Method Details

#call(source, receiver = nil) { ... } ⇒ Sevgi::Executor::Scope

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Executes one source object in this scope.

Parameters:

  • source (Sevgi::Executor::Source)

    source to evaluate

  • receiver (Object, nil) (defaults to: nil)

    receiver used while booting the DSL

Yields:

  • optional boot block that installs DSL methods before evaluation

Yield Returns:

  • (void)

Returns:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/sevgi/executor/scope.rb', line 41

def call(source, receiver = nil, &boot)
  push(source)

  tap do
    @recent = run(source, receiver, &boot)

    # rubocop:disable Lint/RescueException
  rescue Exception => e
    @error = Executor::Error.new(e, self)

    throw(:result, self)
    # rubocop:enable Lint/RescueException
  end
end

#capture(source, error) ⇒ Sevgi::Executor::Scope

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Captures a preprocessing failure for this scope.

Parameters:

  • source (Sevgi::Executor::Source)

    source active when preprocessing failed

  • error (Exception)

    original preprocessing exception

Returns:



61
62
63
64
65
# File 'lib/sevgi/executor/scope.rb', line 61

def capture(source, error)
  push(source)
  @error = Executor::Error.new(error, self)
  self
end

#error?Boolean

Reports whether execution captured an error.

Returns:

  • (Boolean)

    true when execution failed



32
# File 'lib/sevgi/executor/scope.rb', line 32

def error? = !error.nil?

#load(file) { ... } ⇒ Sevgi::Executor::Scope

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

File-read failures are captured as Error on this scope.

Loads a file into this existing execution scope.

Parameters:

  • file (String)

    source file to read and evaluate

Yields:

  • optional boot block that installs DSL methods before evaluation

Yield Returns:

  • (void)

Returns:



74
75
76
77
78
79
# File 'lib/sevgi/executor/scope.rb', line 74

def load(file, &block)
  call(Source.load(file), &block)
rescue ::SystemCallError => e
  capture(Source.new(string: "", file:, line: 1), e)
  throw(:result, self)
end

#peekSevgi::Executor::Source?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the most recently pushed source.

Returns:

  • (Sevgi::Executor::Source, nil)

    most recent source object



89
# File 'lib/sevgi/executor/scope.rb', line 89

def peek = @stack[@stack.keys.last]

#stackArray<String>

Note:

The stack is owned by this scope and is not shared with concurrent executions.

Returns the unique source stack for this execution.

Returns:

  • (Array<String>)

    source file keys in load order



84
# File 'lib/sevgi/executor/scope.rb', line 84

def stack = @stack.keys