Class: Peruby::Env

Inherits:
Object
  • Object
show all
Defined in:
lib/peruby/runtime/env.rb

Overview

Runtime lexical environment and package fallback.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(runtime, scopes: nil, want: :void, package: 'main', state_owner: nil, strict: nil) ⇒ Env

rubocop:disable Metrics/ParameterLists



8
9
10
11
12
13
14
15
16
17
# File 'lib/peruby/runtime/env.rb', line 8

def initialize(runtime, scopes: nil, want: :void, package: 'main', state_owner: nil, strict: nil) # rubocop:disable Metrics/ParameterLists
  @runtime = runtime
  @scopes = scopes || [{}]
  @want = want
  @package = package
  @state_owner = state_owner
  @strict = strict || Set.new
  @generation = 0
  @lookup_cache = {}
end

Instance Attribute Details

#generationObject (readonly)

Returns the value of attribute generation.



6
7
8
# File 'lib/peruby/runtime/env.rb', line 6

def generation
  @generation
end

#packageObject

Returns the value of attribute package.



6
7
8
# File 'lib/peruby/runtime/env.rb', line 6

def package
  @package
end

#runtimeObject (readonly)

Returns the value of attribute runtime.



6
7
8
# File 'lib/peruby/runtime/env.rb', line 6

def runtime
  @runtime
end

#state_ownerObject (readonly)

Returns the value of attribute state_owner.



6
7
8
# File 'lib/peruby/runtime/env.rb', line 6

def state_owner
  @state_owner
end

#wantObject (readonly)

Returns the value of attribute want.



6
7
8
# File 'lib/peruby/runtime/env.rb', line 6

def want
  @want
end

Instance Method Details

#bind(sigil, name, value) ⇒ Object



57
58
59
60
# File 'lib/peruby/runtime/env.rb', line 57

def bind(sigil, name, value)
  invalidate
  @scopes.last[[sigil, name]] = value
end

#call_frame(arguments, want:, package:, state_owner:) ⇒ Object



66
67
68
69
70
# File 'lib/peruby/runtime/env.rb', line 66

def call_frame(arguments, want:, package:, state_owner:)
  arguments_scope = { [:array, '_'].freeze => PerlArray.new(arguments) }
  self.class.new(@runtime, scopes: @scopes.dup << arguments_scope, want:, package:, state_owner:,
                           strict: @strict.dup)
end

#captureObject



72
73
74
75
# File 'lib/peruby/runtime/env.rb', line 72

def capture
  self.class.new(@runtime, scopes: @scopes.map(&:dup), want: @want, package: @package, state_owner: @state_owner,
                           strict: @strict.dup)
end

#configure_strict(categories, disable: false) ⇒ Object



44
45
46
47
# File 'lib/peruby/runtime/env.rb', line 44

def configure_strict(categories, disable: false)
  categories = %w[refs subs vars] if categories.empty?
  disable ? @strict.subtract(categories) : @strict.merge(categories)
end

#declare(sigil, name) ⇒ Object



52
53
54
55
# File 'lib/peruby/runtime/env.rb', line 52

def declare(sigil, name)
  invalidate
  @scopes.last[[sigil, name]] = empty_value(sigil)
end

#fetch(sigil, name) ⇒ Object



77
78
79
# File 'lib/peruby/runtime/env.rb', line 77

def fetch(sigil, name)
  fetch_key([sigil, name])
end

#fetch_key(key) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/peruby/runtime/env.rb', line 81

def fetch_key(key)
  sigil, name = key
  match_value = @runtime.match_state.fetch(sigil, name)
  return match_value if match_value

  cached = @lookup_cache[key]
  return cached if cached

  scope = @scopes.reverse_each.find { |candidate| candidate.key?(key) }
  return @lookup_cache[key] = scope[key] if scope

  @lookup_cache[key] = glob_value(sigil, name)
end

#fork(want: :void, package: @package, state_owner: nil) ⇒ Object



62
63
64
# File 'lib/peruby/runtime/env.rb', line 62

def fork(want: :void, package: @package, state_owner: nil)
  self.class.new(@runtime, scopes: @scopes.dup << {}, want:, package:, state_owner:, strict: @strict.dup)
end

#strict_categoriesObject



50
# File 'lib/peruby/runtime/env.rb', line 50

def strict_categories = @strict.dup

#strict_refs?Boolean

Returns:

  • (Boolean)


49
# File 'lib/peruby/runtime/env.rb', line 49

def strict_refs? = @strict.include?('refs')

#with_package(package) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/peruby/runtime/env.rb', line 31

def with_package(package)
  previous = @package
  self.package = package
  yield
ensure
  self.package = previous
end

#with_scopeObject



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/peruby/runtime/env.rb', line 19

def with_scope(&)
  package = @package
  strict = @strict.dup
  @scopes << {}
  @runtime.local_stack.within { @runtime.with_match_scope(&) }
ensure
  changed_package = @package != package
  @package = package
  @strict = strict
  invalidate if changed_package || !@scopes.pop.empty?
end