Class: RKelly::Runtime::ScopeChain

Inherits:
Object
  • Object
show all
Includes:
JS
Defined in:
lib/rkelly/runtime/scope_chain.rb

Instance Method Summary collapse

Constructor Details

#initialize(scope = Scope.new) ⇒ ScopeChain

Returns a new instance of ScopeChain.



6
7
8
# File 'lib/rkelly/runtime/scope_chain.rb', line 6

def initialize(scope = Scope.new)
  @chain = [GlobalObject.new]
end

Instance Method Details

#<<(scope) ⇒ Object



10
11
12
# File 'lib/rkelly/runtime/scope_chain.rb', line 10

def <<(scope)
  @chain << scope
end

#[](name) ⇒ Object



21
22
23
24
25
# File 'lib/rkelly/runtime/scope_chain.rb', line 21

def [](name)
  property = has_property?(name)
  return property if property
  @chain.last.properties[name]
end

#[]=(name, value) ⇒ Object



27
28
29
# File 'lib/rkelly/runtime/scope_chain.rb', line 27

def []=(name, value)
  @chain.last.properties[name] = value
end

#has_property?(name) ⇒ Boolean

Returns:



14
15
16
17
18
19
# File 'lib/rkelly/runtime/scope_chain.rb', line 14

def has_property?(name)
  scope = @chain.reverse.find { |x|
    x.has_property?(name)
  }
  scope ? scope[name] : nil
end

#new_scope(&block) ⇒ Object



39
40
41
42
43
44
# File 'lib/rkelly/runtime/scope_chain.rb', line 39

def new_scope(&block)
  @chain << Scope.new
  result = yield(self)
  @chain.pop
  result
end

#popObject



31
32
33
# File 'lib/rkelly/runtime/scope_chain.rb', line 31

def pop
  @chain.pop
end

#returnObject



50
# File 'lib/rkelly/runtime/scope_chain.rb', line 50

def return; @chain.last.return; end

#return=(value) ⇒ Object



46
47
48
# File 'lib/rkelly/runtime/scope_chain.rb', line 46

def return=(value)
  @chain.last.return = value
end

#returned?Boolean

Returns:



52
53
54
# File 'lib/rkelly/runtime/scope_chain.rb', line 52

def returned?
  @chain.last.returned?
end

#thisObject



35
36
37
# File 'lib/rkelly/runtime/scope_chain.rb', line 35

def this
  @chain.last
end