Skip to content

Commit 6be00fe

Browse files
committedJun 17, 2014
Add initial support for Module#autoload()
1 parent ceee797 commit 6be00fe

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed
 

‎CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
* Remove `const_missing` option from compiler. All constant lookups are now strict.
1717

18+
* Add initial support for Module#autoload.
19+
1820
## 0.6.2 2014-04-25
1921

2022
* Added Range#size

‎lib/opal/nodes/call.rb

+1
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ def compile_default?
126126

127127
add_special :autoload do
128128
if scope.class_scope?
129+
compile_default!
129130
str = DependencyResolver.new(compiler, arglist[2]).resolve
130131
compiler.requires << str unless str.nil?
131132
push fragment('')

‎opal/corelib/module.rb

+22
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,19 @@ def attr_writer(*names)
180180

181181
alias attr attr_accessor
182182

183+
def autoload(const, path)
184+
%x{
185+
var autoloaders;
186+
187+
if (!(autoloaders = self.__autoload)) {
188+
autoloaders = self.__autoload = {};
189+
}
190+
191+
autoloaders[#{const}] = #{path};
192+
return nil;
193+
}
194+
end
195+
183196
def constants
184197
`self._scope.constants`
185198
end
@@ -233,6 +246,15 @@ def const_get(name, inherit = true)
233246
end
234247

235248
def const_missing(const)
249+
%x{
250+
var autoloader;
251+
252+
if (self.__autoload && (autoloader = self.__autoload[#{const}])) {
253+
console.log("autoloading: " + #{const});
254+
return self.$require(autoloader);
255+
}
256+
}
257+
236258
name = `self._name`
237259

238260
raise NameError, "uninitialized constant #{name}::#{const}"

0 commit comments

Comments
 (0)
Please sign in to comment.