evanphx / rubinius

Rubinius, the Ruby VM

rubinius / kernel / common / block_environment.rb
100644 76 lines (59 sloc) 1.537 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
##
# Describes the environment a block was created in. BlockEnvironment is used
# to create a BlockContext.
 
module Rubinius
  class BlockEnvironment
    attr_accessor :local_count
    attr_accessor :method # The CompiledMethod object that we were called from
 
    attr_accessor :initial_ip
    attr_accessor :last_ip
    attr_accessor :post_send
    attr_accessor :scope
    attr_accessor :top_scope
 
    attr_accessor :proc_environment
    attr_accessor :metadata_container
 
    def from_proc?
      @proc_environment
    end
 
    def under_context(scope, cmethod)
      @top_scope = scope
      @scope = scope
      @method = cmethod
      @local_count = cmethod.local_count
 
      return self
    end
 
    def receiver=(obj)
      @top_scope.self = obj
    end
 
    ##
    # First field of the tuple in @metadata_container holds a boolean
    # indicating if the context is from eval
 
    def from_eval?
      @metadata_container and @metadata_container[0]
    end
 
    def from_eval!
      @metadata_container = Tuple.new(1) unless @metadata_container
      @metadata_container[0] = true
    end
 
    def make_independent
      @top_scope = @top_scope.dup
      @scope = @scope.dup
    end
 
    def call_on_instance(obj, *args)
      call_under obj, method.scope, *args
    end
 
    def arity
      method.required_args
    end
 
    def disable_long_return!
      @post_send = nil
    end
 
    # TODO: are file,line actually used?
    def file
      method.file
    end
 
    def line
      method.line_from_ip(0)
    end
  end
end