forked from rubinius/rubinius
/
autoload.rb
41 lines (35 loc) · 914 Bytes
/
autoload.rb
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
##
# Used to implement Module#autoload.
class Autoload
def self.allocate
Ruby.primitive :autoload_allocate
raise PrimtiveFailure, "Autoload.allocate primitive failed"
end
attr_reader :name
attr_reader :scope
attr_reader :path
def initialize(name, scope, path)
@name = name
@scope = scope
@path = path
end
##
# When any code that finds a constant sees an instance of Autoload as its match,
# it calls this method on us
def call(honor_require=false)
# Remove the autoload object from the constant table it was in, so
# we don't recurse back into ourself.
scope.constant_table.delete @name
Rubinius.inc_global_serial
worked = Rubinius::CodeLoader.require @path
if !honor_require or worked
scope.const_get @name
end
end
##
#
# Change the file to autoload. Used by Module#autoload
def set_path(path)
@path = path
end
end