Class: Peruby::Op::State

Inherits:
Base
  • Object
show all
Defined in:
lib/peruby/op/variable.rb

Overview

Persistent lexical declaration, scoped independently for each closure.

Instance Method Summary collapse

Methods inherited from Base

#argument_cells, #local_slot, #lvalue, #run_subroutine, #to_callable, #warning_directive?

Constructor Details

#initialize(sigil, name, initializer, predeclared = nil) ⇒ State

Returns a new instance of State.



338
339
340
341
342
343
344
# File 'lib/peruby/op/variable.rb', line 338

def initialize(sigil, name, initializer, predeclared = nil)
  @sigil = sigil
  @name = name
  @initializer = initializer
  @predeclared = predeclared
  @values = ObjectSpace::WeakMap.new
end

Instance Method Details

#run(env, context) ⇒ Object



346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/peruby/op/variable.rb', line 346

def run(env, context)
  owner = env.state_owner || env.runtime
  value = @values[owner]
  unless value
    value = initial_value(owner)
    initialize_value(value, env)
    @values[owner] = value
  end
  env.bind(@sigil, @name, value)
  return Array(value.respond_to?(:cells) ? value.cells : value) if context == :list

  scalar_value(value)
end