Revert "fixed bug in lib/thread Queue"
[rbx.git] / kernel / bootstrap / module.rb
blobb6a12dc8a7007f61c90b6e23b0bb99ad86d97b03
1 # depends on: vm.rb
3 class Module
4   ivar_as_index :method_table => 1, :name => 3, :constants => 4, :encloser => 5, :superclass => 6
6   def method_table   ; @method_table ; end
7   def constant_table ; @constants    ; end
8   def encloser       ; @encloser     ; end
9   def name           ; @name.to_s    ; end
11   def self.allocate
12     Ruby.primitive :allocate_module
13     raise PrimitiveFailure, "Module.allocate primitive failed"
14   end
16   # Ultra simple private
17   def private(name)
18     if cm = @method_table[name]
19       if cm.kind_of? Tuple
20         cm.put 0, :private
21       else
22         tup = Tuple.new(2)
23         tup.put 0, :private
24         tup.put 1, cm
25         @method_table[name] = tup
26       end
27     end
28   end
30   # Ultra simple protected
31   def protected(name)
32     if cm = @method_table[name]
33       if cm.kind_of? Tuple
34         cm.put 0, :protected
35       else
36         tup = Tuple.new(2)
37         tup.put 0, :protected
38         tup.put 1, cm
39         @method_table[name] = tup
40       end
41     end
42   end
44   # Ultra simple module_function
45   def module_function(name)
46     if cm = @method_table[name]
47       if cm.kind_of? Tuple
48         cm = cm[1]
49       end
50       meta = class << self; self; end
51       meta.method_table[name] = cm
52       private name
53     end
54   end
56   def __find_method(namesym)
57     Ruby.primitive :find_method
58     raise PrimitiveFailure, "primitive failed"
59   end
61   def alias_method(new_name, current_name)
62     unless meth = @method_table[current_name]
63       mod = direct_superclass()
64       while !meth and mod
65         meth = mod.method_table[current_name]
66         mod = mod.direct_superclass
67       end
68     end
70     unless meth
71       raise NoMethodError, "Unable to find method '#{current_name}' to alias to '#{new_name}'"
72     end
73     @method_table[new_name] = meth
74     Rubinius::VM.reset_method_cache(new_name)
75   end
77   # 'superclass' method defined in class.rb,
78   # because it is more complex than a mere accessor
79   def superclass=(other)
80     @superclass = other
81   end
83   # This may be either an included Module or then
84   # an inherited Class.
85   def direct_superclass
86     @superclass
87   end
89   def append_features(mod)
90     im = IncludedModule.new(self)
91     im.attach_to mod
92   end
94   def included(mod); end
96   def include(mod)
97     mod.append_features(self)
98     mod.included(self)
99     self
100   end
102   def attr_reader(name)
103     sym = "@#{name}".to_sym
104     meth = AccessVarMethod.get_ivar(sym, name)
105     @method_table[name] = meth
106     return nil
107   end
109   def attr_writer(name)
110     sym = "@#{name}".to_sym
111     meth = AccessVarMethod.set_ivar(sym, "#{name}=")
112     @method_table["#{name}=".to_sym] = meth
113     return nil
114   end
116   def attr_accessor(name)
117     attr_reader(name)
118     attr_writer(name)
119     return true
120   end