opal 0.6.3 → 0.7.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (221) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +0 -1
  3. data/.spectator +2 -0
  4. data/.spectator-mspec +3 -0
  5. data/.travis.yml +8 -11
  6. data/CHANGELOG.md +33 -0
  7. data/CONTRIBUTING.md +8 -43
  8. data/Gemfile +15 -4
  9. data/Guardfile +77 -0
  10. data/README.md +15 -9
  11. data/Rakefile +36 -12
  12. data/benchmarks/operators.rb +11 -0
  13. data/bin/opal +10 -13
  14. data/bin/opal-build +4 -4
  15. data/bin/opal-mspec +10 -0
  16. data/bin/opal-repl +4 -3
  17. data/examples/sinatra/Gemfile +1 -1
  18. data/examples/sinatra/config.ru +3 -3
  19. data/lib/mspec/opal/main.rb.erb +2 -2
  20. data/lib/mspec/opal/rake_task.rb +31 -24
  21. data/lib/mspec/opal/runner.rb +18 -1
  22. data/lib/mspec/opal/sprockets.js +17 -0
  23. data/lib/opal.rb +1 -34
  24. data/lib/opal/builder.rb +92 -58
  25. data/lib/opal/builder_processors.rb +165 -0
  26. data/lib/opal/cli.rb +85 -144
  27. data/lib/opal/cli_options.rb +136 -90
  28. data/lib/opal/cli_runners.rb +10 -0
  29. data/lib/opal/cli_runners/nodejs.rb +56 -0
  30. data/lib/opal/cli_runners/phantom.js +35 -0
  31. data/lib/opal/cli_runners/phantomjs.rb +28 -0
  32. data/lib/opal/cli_runners/server.rb +54 -0
  33. data/lib/opal/compiler.rb +35 -16
  34. data/lib/opal/erb.rb +29 -15
  35. data/lib/opal/hike_path_finder.rb +18 -0
  36. data/lib/opal/nodes.rb +1 -0
  37. data/lib/opal/nodes/call.rb +107 -26
  38. data/lib/opal/nodes/call_special.rb +31 -6
  39. data/lib/opal/nodes/class.rb +2 -2
  40. data/lib/opal/nodes/constants.rb +5 -20
  41. data/lib/opal/nodes/def.rb +4 -4
  42. data/lib/opal/nodes/defined.rb +3 -3
  43. data/lib/opal/nodes/definitions.rb +1 -1
  44. data/lib/opal/nodes/for.rb +35 -0
  45. data/lib/opal/nodes/helpers.rb +2 -2
  46. data/lib/opal/nodes/iter.rb +3 -3
  47. data/lib/opal/nodes/literal.rb +10 -2
  48. data/lib/opal/nodes/masgn.rb +2 -2
  49. data/lib/opal/nodes/module.rb +2 -2
  50. data/lib/opal/nodes/scope.rb +1 -0
  51. data/lib/opal/nodes/singleton_class.rb +2 -2
  52. data/lib/opal/nodes/super.rb +2 -2
  53. data/lib/opal/nodes/top.rb +30 -3
  54. data/lib/opal/parser.rb +15 -1
  55. data/lib/opal/parser/grammar.rb +2571 -2452
  56. data/lib/opal/parser/grammar.y +37 -5
  57. data/lib/opal/parser/keywords.rb +2 -0
  58. data/lib/opal/parser/lexer.rb +21 -11
  59. data/lib/opal/path_reader.rb +28 -0
  60. data/lib/opal/paths.rb +38 -0
  61. data/lib/opal/source_map.rb +32 -15
  62. data/lib/opal/sprockets/environment.rb +9 -2
  63. data/lib/opal/sprockets/erb.rb +1 -2
  64. data/lib/opal/sprockets/path_reader.rb +34 -0
  65. data/lib/opal/sprockets/processor.rb +40 -39
  66. data/lib/opal/sprockets/server.rb +47 -33
  67. data/lib/opal/version.rb +1 -1
  68. data/opal.gemspec +10 -5
  69. data/opal/README.md +6 -0
  70. data/opal/corelib/array.rb +36 -4
  71. data/opal/corelib/array/inheritance.rb +6 -6
  72. data/opal/corelib/basic_object.rb +9 -9
  73. data/opal/corelib/boolean.rb +1 -1
  74. data/opal/corelib/class.rb +12 -12
  75. data/opal/corelib/dir.rb +20 -0
  76. data/opal/corelib/enumerable.rb +42 -42
  77. data/opal/corelib/enumerator.rb +1 -1
  78. data/opal/corelib/error.rb +2 -2
  79. data/opal/corelib/file.rb +56 -0
  80. data/opal/corelib/hash.rb +5 -5
  81. data/opal/corelib/helpers.rb +3 -3
  82. data/opal/corelib/io.rb +13 -10
  83. data/opal/corelib/kernel.rb +44 -68
  84. data/opal/corelib/method.rb +1 -1
  85. data/opal/corelib/module.rb +89 -114
  86. data/opal/corelib/nil_class.rb +1 -1
  87. data/opal/corelib/numeric.rb +27 -23
  88. data/opal/corelib/proc.rb +5 -5
  89. data/opal/corelib/range.rb +8 -4
  90. data/opal/corelib/regexp.rb +5 -5
  91. data/opal/corelib/runtime.js +589 -272
  92. data/opal/corelib/string.rb +52 -37
  93. data/opal/corelib/string/inheritance.rb +5 -5
  94. data/opal/corelib/time.rb +102 -52
  95. data/opal/corelib/variables.rb +3 -3
  96. data/opal/opal.rb +2 -0
  97. data/package.json +9 -0
  98. data/spec/filters/bugs/array.rb +0 -6
  99. data/spec/filters/bugs/language.rb +4 -0
  100. data/spec/filters/bugs/numeric.rb +7 -6
  101. data/spec/filters/bugs/opal.rb +2 -0
  102. data/spec/filters/bugs/regexp.rb +4 -0
  103. data/spec/filters/bugs/string.rb +0 -7
  104. data/spec/filters/bugs/stringscanner.rb +4 -1
  105. data/spec/filters/unsupported/private_methods.rb +2 -0
  106. data/spec/lib/builder_processors_spec.rb +27 -0
  107. data/spec/lib/builder_spec.rb +66 -0
  108. data/spec/{cli → lib}/cli_spec.rb +60 -5
  109. data/spec/{cli → lib}/compiler_spec.rb +66 -5
  110. data/spec/{cli → lib}/dependency_resolver_spec.rb +1 -1
  111. data/spec/lib/fixtures/no_requires.rb +1 -0
  112. data/spec/{cli → lib}/fixtures/opal_file.rb +0 -0
  113. data/spec/lib/fixtures/require_tree_test.rb +3 -0
  114. data/spec/lib/fixtures/required_tree_test/required_file1.rb +1 -0
  115. data/spec/lib/fixtures/required_tree_test/required_file2.rb +1 -0
  116. data/spec/lib/fixtures/requires.rb +7 -0
  117. data/spec/{cli → lib}/fixtures/sprockets_file.js.rb +0 -0
  118. data/spec/lib/fixtures/sprockets_require_tree_test.rb +3 -0
  119. data/spec/lib/hike_path_finder_spec.rb +23 -0
  120. data/spec/{cli → lib}/lexer_spec.rb +1 -1
  121. data/spec/{cli → lib}/parser/alias_spec.rb +1 -1
  122. data/spec/{cli → lib}/parser/and_spec.rb +1 -1
  123. data/spec/{cli → lib}/parser/attrasgn_spec.rb +1 -1
  124. data/spec/{cli → lib}/parser/begin_spec.rb +1 -1
  125. data/spec/{cli → lib}/parser/block_spec.rb +1 -1
  126. data/spec/{cli → lib}/parser/break_spec.rb +1 -1
  127. data/spec/{cli → lib}/parser/call_spec.rb +1 -1
  128. data/spec/{cli → lib}/parser/class_spec.rb +1 -1
  129. data/spec/{cli → lib}/parser/comments_spec.rb +1 -1
  130. data/spec/{cli → lib}/parser/def_spec.rb +1 -1
  131. data/spec/{cli → lib}/parser/if_spec.rb +1 -1
  132. data/spec/{cli → lib}/parser/iter_spec.rb +1 -1
  133. data/spec/{cli → lib}/parser/lambda_spec.rb +1 -1
  134. data/spec/{cli → lib}/parser/literal_spec.rb +1 -1
  135. data/spec/{cli → lib}/parser/masgn_spec.rb +1 -1
  136. data/spec/{cli → lib}/parser/module_spec.rb +1 -1
  137. data/spec/{cli → lib}/parser/not_spec.rb +1 -1
  138. data/spec/{cli → lib}/parser/op_asgn1_spec.rb +1 -1
  139. data/spec/{cli → lib}/parser/op_asgn2_spec.rb +1 -1
  140. data/spec/{cli → lib}/parser/or_spec.rb +1 -1
  141. data/spec/{cli → lib}/parser/return_spec.rb +1 -1
  142. data/spec/{cli → lib}/parser/sclass_spec.rb +1 -1
  143. data/spec/{cli → lib}/parser/string_spec.rb +8 -1
  144. data/spec/{cli → lib}/parser/super_spec.rb +1 -1
  145. data/spec/lib/parser/unary_spec.rb +48 -0
  146. data/spec/{cli → lib}/parser/undef_spec.rb +1 -1
  147. data/spec/{cli → lib}/parser/unless_spec.rb +1 -1
  148. data/spec/{cli → lib}/parser/variables_spec.rb +1 -1
  149. data/spec/{cli → lib}/parser/while_spec.rb +1 -1
  150. data/spec/{cli → lib}/parser/yield_spec.rb +1 -1
  151. data/spec/lib/path_reader_spec.rb +24 -0
  152. data/spec/lib/shared/path_finder_shared.rb +19 -0
  153. data/spec/lib/shared/path_reader_shared.rb +31 -0
  154. data/spec/lib/spec_helper.rb +9 -0
  155. data/spec/lib/sprockets/environment_spec.rb +30 -0
  156. data/spec/{cli → lib}/sprockets/erb_spec.rb +1 -1
  157. data/spec/lib/sprockets/path_reader_spec.rb +25 -0
  158. data/spec/{cli → lib}/sprockets/processor_spec.rb +9 -2
  159. data/spec/lib/sprockets/server_spec.rb +20 -0
  160. data/spec/opal/compiler/irb_spec.rb +11 -11
  161. data/spec/opal/core/fixtures/require_tree_files/file 1.rb +1 -0
  162. data/spec/opal/core/fixtures/require_tree_files/file 2.rb +1 -0
  163. data/spec/opal/core/fixtures/require_tree_files/file 3.rb +1 -0
  164. data/spec/opal/core/fixtures/require_tree_files/file 4.rb +1 -0
  165. data/spec/opal/core/fixtures/require_tree_files/file 5.rb +1 -0
  166. data/spec/opal/core/kernel/require_tree_spec.rb +7 -0
  167. data/spec/opal/core/kernel/respond_to_spec.rb +2 -2
  168. data/spec/opal/core/runtime/method_missing_spec.rb +19 -0
  169. data/spec/opal/core/source_map_spec.rb +2 -2
  170. data/spec/opal/core/string_spec.rb +11 -0
  171. data/spec/opal/stdlib/erb/erb_spec.rb +0 -1
  172. data/spec/opal/stdlib/thread/mutex_spec.rb +40 -0
  173. data/spec/opal/stdlib/thread/thread_queue_spec.rb +32 -0
  174. data/spec/opal/stdlib/thread/thread_spec.rb +60 -0
  175. data/spec/rubyspecs +54 -11
  176. data/spec/spec_helper.rb +18 -3
  177. data/spec/support/mspec_rspec_adapter.rb +33 -0
  178. data/spec/{cli/spec_helper.rb → support/parser_helpers.rb} +10 -10
  179. data/stdlib/README.md +3 -0
  180. data/stdlib/benchmark.rb +10 -0
  181. data/stdlib/date.rb +2 -2
  182. data/stdlib/dir.rb +1 -5
  183. data/stdlib/file.rb +1 -7
  184. data/stdlib/json.rb +10 -1
  185. data/stdlib/native.rb +5 -5
  186. data/stdlib/nodejs.rb +5 -0
  187. data/stdlib/nodejs/dir.rb +13 -0
  188. data/stdlib/nodejs/file.rb +98 -0
  189. data/stdlib/nodejs/fileutils.rb +26 -0
  190. data/stdlib/nodejs/io.rb +2 -0
  191. data/stdlib/nodejs/irb.rb +45 -0
  192. data/stdlib/nodejs/process.rb +16 -0
  193. data/stdlib/nodejs/require.rb +32 -0
  194. data/stdlib/nodejs/rubygems.rb +68 -0
  195. data/stdlib/nodejs/runtime.rb +25 -0
  196. data/stdlib/nodejs/yaml.rb +11 -0
  197. data/stdlib/opal-parser.rb +1 -2
  198. data/stdlib/opal-source-maps.rb +2 -0
  199. data/stdlib/phantomjs.rb +8 -0
  200. data/stdlib/process.rb +10 -0
  201. data/stdlib/promise.rb +12 -4
  202. data/stdlib/set.rb +27 -0
  203. data/stdlib/source_map.rb +5 -63
  204. data/stdlib/source_map/map.rb +220 -0
  205. data/stdlib/source_map/mapping.rb +26 -0
  206. data/stdlib/source_map/offset.rb +88 -0
  207. data/stdlib/source_map/version.rb +3 -0
  208. data/stdlib/source_map/vlq.rb +77 -101
  209. data/stdlib/sourcemap.rb +1 -0
  210. data/stdlib/strscan.rb +7 -1
  211. data/stdlib/template.rb +1 -1
  212. data/stdlib/thread.rb +147 -7
  213. metadata +238 -104
  214. data/lib/mspec/opal/mspec_fixes.rb +0 -87
  215. data/spec/cli/sprockets/environment_spec.rb +0 -14
  216. data/spec/filters/bugs/symbol.rb +0 -5
  217. data/spec/opal/core/kernel/warn_spec.rb +0 -83
  218. data/spec/opal/core/language/numbers_spec.rb +0 -60
  219. data/stdlib/opal-source-maps.js.erb +0 -2
  220. data/stdlib/source_map/generator.rb +0 -251
  221. data/stdlib/source_map/parser.rb +0 -102
@@ -61,7 +61,7 @@ class Enumerator
61
61
  %x{
62
62
  var result
63
63
 
64
- self.$each._p = function() {
64
+ self.$each.$$p = function() {
65
65
  var param = #{Opal.destructure(`arguments`)},
66
66
  value = block(param, index);
67
67
 
@@ -4,8 +4,8 @@ class Exception
4
4
  def self.new(message = '')
5
5
  %x{
6
6
  var err = new Error(message);
7
- err._klass = self;
8
- err.name = self._name;
7
+ err.$$class = self;
8
+ err.name = self.$$name;
9
9
  return err;
10
10
  }
11
11
  end
@@ -0,0 +1,56 @@
1
+ class File < IO
2
+ Separator = SEPARATOR = '/'
3
+
4
+ class << self
5
+ def expand_path(path, basedir = nil)
6
+ path = [basedir, path].compact.join(SEPARATOR)
7
+ parts = path.split(SEPARATOR)
8
+ new_parts = []
9
+ parts[0] = Dir.home if parts.first == '~'
10
+
11
+ parts.each do |part|
12
+ if part == '..'
13
+ new_parts.pop
14
+ else
15
+ new_parts << part
16
+ end
17
+ end
18
+ new_parts.join(SEPARATOR)
19
+ end
20
+
21
+ def dirname(path)
22
+ split(path)[0..-2]
23
+ end
24
+
25
+ def basename(path)
26
+ split(path)[-1]
27
+ end
28
+
29
+ def exist? path
30
+ `Opal.modules[#{path}] != null`
31
+ end
32
+ alias exists? exist?
33
+
34
+ def directory?(path)
35
+ files = []
36
+ %x{
37
+ for (var key in Opal.modules) {
38
+ #{files}.push(key)
39
+ }
40
+ }
41
+ path = path.gsub(%r{(^.#{SEPARATOR}+|#{SEPARATOR}+$)})
42
+ file = files.find do |file|
43
+ file =~ /^#{path}/
44
+ end
45
+ file
46
+ end
47
+
48
+ def join(*paths)
49
+ paths.join(SEPARATOR).gsub(%r{#{SEPARATOR}+}, SEPARATOR)
50
+ end
51
+
52
+ def split(path)
53
+ path.split(SEPARATOR)
54
+ end
55
+ end
56
+ end
data/opal/corelib/hash.rb CHANGED
@@ -9,7 +9,7 @@ class Hash
9
9
 
10
10
  def self.allocate
11
11
  %x{
12
- var hash = new self._alloc;
12
+ var hash = new self.$$alloc;
13
13
 
14
14
  hash.map = {};
15
15
  hash.keys = [];
@@ -125,7 +125,7 @@ class Hash
125
125
  map[key] = value;
126
126
  }
127
127
 
128
- var hash = new self._klass._alloc();
128
+ var hash = new self.$$class.$$alloc();
129
129
 
130
130
  hash.map = map;
131
131
  hash.keys = keys;
@@ -313,7 +313,7 @@ class Hash
313
313
 
314
314
  result.push(key);
315
315
 
316
- if (value._isArray) {
316
+ if (value.$$is_array) {
317
317
  if (level == null || level === 1) {
318
318
  result.push(value);
319
319
  }
@@ -347,7 +347,7 @@ class Hash
347
347
  end
348
348
 
349
349
  def hash
350
- `self._id`
350
+ `self.$$id`
351
351
  end
352
352
 
353
353
  alias include? has_key?
@@ -689,7 +689,7 @@ class Hash
689
689
 
690
690
  def to_h
691
691
  %x{
692
- var hash = new Opal.Hash._alloc,
692
+ var hash = new Opal.Hash.$$alloc,
693
693
  cloned = #{clone};
694
694
 
695
695
  hash.map = cloned.map;
@@ -64,7 +64,7 @@ module Opal
64
64
  if (args.length == 1) {
65
65
  return args[0];
66
66
  }
67
- else if (args._isArray) {
67
+ else if (args.$$is_array) {
68
68
  return args;
69
69
  }
70
70
  else {
@@ -75,7 +75,7 @@ module Opal
75
75
 
76
76
  def self.respond_to?(obj, method)
77
77
  %x{
78
- if (obj == null || !obj._klass) {
78
+ if (obj == null || !obj.$$class) {
79
79
  return false;
80
80
  }
81
81
  }
@@ -91,7 +91,7 @@ module Opal
91
91
  else if (obj === null) {
92
92
  return "null";
93
93
  }
94
- else if (!obj._klass) {
94
+ else if (!obj.$$class) {
95
95
  return obj.toString();
96
96
  }
97
97
  else {
data/opal/corelib/io.rb CHANGED
@@ -3,19 +3,28 @@ class IO
3
3
  SEEK_CUR = 1
4
4
  SEEK_END = 2
5
5
 
6
+ attr_accessor :write_proc
7
+
8
+ def write(string)
9
+ `self.write_proc(string)`
10
+ string.size
11
+ end
12
+
6
13
  module Writable
7
14
  def <<(string)
8
15
  write(string)
9
-
10
16
  self
11
17
  end
12
18
 
13
19
  def print(*args)
14
20
  write args.map { |arg| String(arg) }.join($,)
21
+ nil
15
22
  end
16
23
 
17
24
  def puts(*args)
18
- write args.map { |arg| String(arg) }.join($/)
25
+ newline = $/
26
+ write args.map { |arg| String(arg).chomp }.concat([nil]).join(newline)
27
+ nil
19
28
  end
20
29
  end
21
30
 
@@ -42,15 +51,9 @@ STDERR = $stderr = IO.new
42
51
  STDIN = $stdin = IO.new
43
52
  STDOUT = $stdout = IO.new
44
53
 
45
- def $stdout.write(string)
46
- `console.log(#{string.to_s});`
47
- nil
48
- end
49
54
 
50
- def $stderr.write(string)
51
- `console.warn(#{string.to_s});`
52
- nil
53
- end
55
+ $stdout.write_proc = `typeof(process) === 'object' ? function(s){process.stdout.write(s)} : function(s){console.log(s)}`
56
+ $stderr.write_proc = `typeof(process) === 'object' ? function(s){process.stderr.write(s)} : function(s){console.warn(s)}`
54
57
 
55
58
  $stdout.extend(IO::Writable)
56
59
  $stderr.extend(IO::Writable)
@@ -25,7 +25,7 @@ module Kernel
25
25
  %x{
26
26
  var meth = self['$' + name];
27
27
 
28
- if (!meth || meth.rb_stub) {
28
+ if (!meth || meth.$$stub) {
29
29
  #{raise NameError, "undefined method `#{name}' for class `#{self.class.name}'"};
30
30
  }
31
31
 
@@ -44,7 +44,7 @@ module Kernel
44
44
  continue;
45
45
  }
46
46
  }
47
- if (self[key].rb_stub === undefined) {
47
+ if (self[key].$$stub === undefined) {
48
48
  methods.push(key.substr(1));
49
49
  }
50
50
  }
@@ -78,16 +78,14 @@ module Kernel
78
78
  end
79
79
 
80
80
  def class
81
- `self._klass`
81
+ `self.$$class`
82
82
  end
83
83
 
84
84
  def copy_instance_variables(other)
85
85
  %x{
86
86
  for (var name in other) {
87
87
  if (name.charAt(0) !== '$') {
88
- if (name !== '_id' && name !== '_klass') {
89
- self[name] = other[name];
90
- }
88
+ self[name] = other[name];
91
89
  }
92
90
  }
93
91
  }
@@ -113,11 +111,11 @@ module Kernel
113
111
 
114
112
  %x{
115
113
  var jsid = '$' + name;
116
- body._jsid = name;
117
- body._s = null;
118
- body._def = body;
114
+ body.$$jsid = name;
115
+ body.$$s = null;
116
+ body.$$def = body;
119
117
 
120
- #{singleton_class}._proto[jsid] = body;
118
+ #{singleton_class}.$$proto[jsid] = body;
121
119
 
122
120
  return self;
123
121
  }
@@ -204,7 +202,7 @@ module Kernel
204
202
  switch (spec) {
205
203
  case 'c':
206
204
  obj = args[idx];
207
- if (obj._isString) {
205
+ if (obj.$$is_string) {
208
206
  str = obj.charAt(0);
209
207
  } else {
210
208
  str = String.fromCharCode(#{`obj`.to_i});
@@ -291,7 +289,7 @@ module Kernel
291
289
  end
292
290
 
293
291
  def hash
294
- `self._id`
292
+ `self.$$id`
295
293
  end
296
294
 
297
295
  def initialize_copy(other)
@@ -302,7 +300,7 @@ module Kernel
302
300
  end
303
301
 
304
302
  def instance_of?(klass)
305
- `self._klass === klass`
303
+ `self.$$class === klass`
306
304
  end
307
305
 
308
306
  def instance_variable_defined?(name)
@@ -327,7 +325,7 @@ module Kernel
327
325
 
328
326
  for (var name in self) {
329
327
  if (name.charAt(0) !== '$') {
330
- if (name !== '_klass' && name !== '_id') {
328
+ if (name !== '$$class' && name !== '$$id') {
331
329
  result.push('@' + name);
332
330
  }
333
331
  }
@@ -392,7 +390,7 @@ module Kernel
392
390
  alias kind_of? is_a?
393
391
 
394
392
  def lambda(&block)
395
- `block.is_lambda = true`
393
+ `block.$$is_lambda = true`
396
394
 
397
395
  block
398
396
  end
@@ -423,16 +421,17 @@ module Kernel
423
421
  nil
424
422
  end
425
423
 
426
- def private_methods
424
+ def private_methods(*)
427
425
  []
428
426
  end
427
+ alias private_instance_methods private_methods
429
428
 
430
429
  def proc(&block)
431
430
  unless block
432
431
  raise ArgumentError, "tried to create Proc object without a block"
433
432
  end
434
433
 
435
- `block.is_lambda = false`
434
+ `block.$$is_lambda = false`
436
435
  block
437
436
  end
438
437
 
@@ -452,7 +451,6 @@ module Kernel
452
451
 
453
452
  def warn(*strs)
454
453
  $stderr.puts(*strs) unless $VERBOSE.nil? || strs.empty?
455
- nil
456
454
  end
457
455
 
458
456
  def raise(exception = undefined, string = undefined)
@@ -460,7 +458,7 @@ module Kernel
460
458
  if (exception == null && #$!) {
461
459
  exception = #$!;
462
460
  }
463
- else if (exception._isString) {
461
+ else if (exception.$$is_string) {
464
462
  exception = #{RuntimeError.new exception};
465
463
  }
466
464
  else if (!#{exception.is_a? Exception}) {
@@ -479,7 +477,7 @@ module Kernel
479
477
  if (max === undefined) {
480
478
  return Math.random();
481
479
  }
482
- else if (max._isRange) {
480
+ else if (max.$$is_range) {
483
481
  var arr = #{max.to_a};
484
482
 
485
483
  return arr[#{rand(`arr.length`)}];
@@ -499,7 +497,7 @@ module Kernel
499
497
  %x{
500
498
  var body = self['$' + name];
501
499
 
502
- if (typeof(body) === "function" && !body.rb_stub) {
500
+ if (typeof(body) === "function" && !body.$$stub) {
503
501
  return true;
504
502
  }
505
503
  }
@@ -511,52 +509,7 @@ module Kernel
511
509
  alias public_send __send__
512
510
 
513
511
  def singleton_class
514
- %x{
515
- if (self._isClass) {
516
- if (self.__meta__) {
517
- return self.__meta__;
518
- }
519
-
520
- var meta = new $opal.Class._alloc;
521
- meta._klass = $opal.Class;
522
- self.__meta__ = meta;
523
- // FIXME - is this right? (probably - methods defined on
524
- // class' singleton should also go to subclasses?)
525
- meta._proto = self.constructor.prototype;
526
- meta._isSingleton = true;
527
- meta.__inc__ = [];
528
- meta._methods = [];
529
-
530
- meta._scope = self._scope;
531
-
532
- return meta;
533
- }
534
-
535
- if (self._isClass) {
536
- return self._klass;
537
- }
538
-
539
- if (self.__meta__) {
540
- return self.__meta__;
541
- }
542
-
543
- else {
544
- var orig_class = self._klass,
545
- class_id = "#<Class:#<" + orig_class._name + ":" + orig_class._id + ">>";
546
-
547
- var Singleton = function () {};
548
- var meta = Opal.boot(orig_class, Singleton);
549
- meta._name = class_id;
550
-
551
- meta._proto = self;
552
- self.__meta__ = meta;
553
- meta._klass = orig_class._klass;
554
- meta._scope = orig_class._scope;
555
- meta.__parent = orig_class;
556
-
557
- return meta;
558
- }
559
- }
512
+ %x{$opal.get_singleton_class(self)}
560
513
  end
561
514
 
562
515
  alias sprintf format
@@ -575,7 +528,7 @@ module Kernel
575
528
  end
576
529
 
577
530
  def to_s
578
- `"#<" + #{self.class.name} + ":" + self._id + ">"`
531
+ `"#<" + #{self.class.name} + ":" + self.$$id + ">"`
579
532
  end
580
533
 
581
534
  def freeze
@@ -590,4 +543,27 @@ module Kernel
590
543
  def respond_to_missing?(method_name)
591
544
  false
592
545
  end
546
+
547
+ def require file
548
+ `$opal.require( $opal.normalize_loadable_path(#{file}) )`
549
+ end
550
+
551
+ # `path` should be the ful path to be found in registered modules (`Opal.modules`)
552
+ def require_tree path
553
+ %x{
554
+ var file_name, file_names = Object.keys($opal.modules);
555
+ path = path.replace(/\/?$/, '/');
556
+ for (var i = 0; i < file_names.length; i++) {
557
+ file_name = file_names[i];
558
+ if (file_name['$start_with?'](path)) {
559
+ $opal.require(file_name);
560
+ }
561
+ }
562
+ }
563
+ nil
564
+ end
565
+
566
+ def load file
567
+ `$opal.load( $opal.normalize_loadable_path(#{file}) )`
568
+ end
593
569
  end
@@ -14,7 +14,7 @@ class Method
14
14
 
15
15
  def call(*args, &block)
16
16
  %x{
17
- #@method._p = block;
17
+ #@method.$$p = block;
18
18
 
19
19
  return #@method.apply(#@receiver, args);
20
20
  }
@@ -2,21 +2,21 @@ class Module
2
2
  def self.new(&block)
3
3
  %x{
4
4
  function AnonModule(){}
5
- var klass = Opal.boot(Opal.Module, AnonModule);
6
- klass._name = nil;
7
- klass._klass = Opal.Module;
8
- klass.__dep__ = []
9
- klass.__mod__ = true;
10
- klass._proto = {};
5
+ var klass = Opal.boot(Opal.Module, AnonModule);
6
+ klass.$$name = nil;
7
+ klass.$$class = Opal.Module;
8
+ klass.$$dep = []
9
+ klass.$$is_mod = true;
10
+ klass.$$proto = {};
11
11
 
12
12
  // inherit scope from parent
13
- $opal.create_scope(Opal.Module._scope, klass);
13
+ $opal.create_scope(Opal.Module.$$scope, klass);
14
14
 
15
15
  if (block !== nil) {
16
- var block_self = block._s;
17
- block._s = null;
16
+ var block_self = block.$$s;
17
+ block.$$s = null;
18
18
  block.call(klass);
19
- block._s = block_self;
19
+ block.$$s = block_self;
20
20
  }
21
21
 
22
22
  return klass;
@@ -38,7 +38,7 @@ class Module
38
38
  return true;
39
39
  }
40
40
 
41
- working = working.__parent;
41
+ working = working.$$parent;
42
42
  }
43
43
 
44
44
  return false;
@@ -47,9 +47,9 @@ class Module
47
47
 
48
48
  def alias_method(newname, oldname)
49
49
  %x{
50
- self._proto['$' + newname] = self._proto['$' + oldname];
50
+ self.$$proto['$' + newname] = self.$$proto['$' + oldname];
51
51
 
52
- if (self._methods) {
52
+ if (self.$$methods) {
53
53
  $opal.donate(self, ['$' + newname ])
54
54
  }
55
55
  }
@@ -57,7 +57,7 @@ class Module
57
57
  end
58
58
 
59
59
  def alias_native(mid, jsid = mid)
60
- `self._proto['$' + mid] = self._proto[jsid]`
60
+ `self.$$proto['$' + mid] = self.$$proto[jsid]`
61
61
  end
62
62
 
63
63
  def ancestors
@@ -67,9 +67,9 @@ class Module
67
67
 
68
68
  while (parent) {
69
69
  result.push(parent);
70
- result = result.concat(parent.__inc__);
70
+ result = result.concat(parent.$$inc);
71
71
 
72
- parent = parent._super;
72
+ parent = parent.$$super;
73
73
  }
74
74
 
75
75
  return result;
@@ -77,56 +77,7 @@ class Module
77
77
  end
78
78
 
79
79
  def append_features(klass)
80
- %x{
81
- var module = self,
82
- included = klass.__inc__;
83
-
84
- // check if this module is already included in the klass
85
- for (var i = 0, length = included.length; i < length; i++) {
86
- if (included[i] === module) {
87
- return;
88
- }
89
- }
90
-
91
- included.push(module);
92
- module.__dep__.push(klass);
93
-
94
- // iclass
95
- var iclass = {
96
- name: module._name,
97
-
98
- _proto: module._proto,
99
- __parent: klass.__parent,
100
- __iclass: true
101
- };
102
-
103
- klass.__parent = iclass;
104
-
105
- var donator = module._proto,
106
- prototype = klass._proto,
107
- methods = module._methods;
108
-
109
- for (var i = 0, length = methods.length; i < length; i++) {
110
- var method = methods[i];
111
-
112
- if (prototype.hasOwnProperty(method) && !prototype[method]._donated) {
113
- // if the target class already has a method of the same name defined
114
- // and that method was NOT donated, then it must be a method defined
115
- // by the class so we do not want to override it
116
- }
117
- else {
118
- prototype[method] = donator[method];
119
- prototype[method]._donated = true;
120
- }
121
- }
122
-
123
- if (klass.__dep__) {
124
- $opal.donate(klass, methods.slice(), true);
125
- }
126
-
127
- $opal.donate_constants(module, klass);
128
- }
129
-
80
+ `Opal.append_features(self, klass)`
130
81
  self
131
82
  end
132
83
 
@@ -137,13 +88,13 @@ class Module
137
88
 
138
89
  def attr_reader(*names)
139
90
  %x{
140
- var proto = self._proto, cls = self;
91
+ var proto = self.$$proto, cls = self;
141
92
  for (var i = 0, length = names.length; i < length; i++) {
142
93
  (function(name) {
143
94
  proto[name] = nil;
144
95
  var func = function() { return this[name] };
145
96
 
146
- if (cls._isSingleton) {
97
+ if (cls.$$is_singleton) {
147
98
  proto.constructor.prototype['$' + name] = func;
148
99
  }
149
100
  else {
@@ -159,13 +110,13 @@ class Module
159
110
 
160
111
  def attr_writer(*names)
161
112
  %x{
162
- var proto = self._proto, cls = self;
113
+ var proto = self.$$proto, cls = self;
163
114
  for (var i = 0, length = names.length; i < length; i++) {
164
115
  (function(name) {
165
116
  proto[name] = nil;
166
117
  var func = function(value) { return this[name] = value; };
167
118
 
168
- if (cls._isSingleton) {
119
+ if (cls.$$is_singleton) {
169
120
  proto.constructor.prototype['$' + name + '='] = func;
170
121
  }
171
122
  else {
@@ -180,8 +131,21 @@ class Module
180
131
 
181
132
  alias attr attr_accessor
182
133
 
134
+ def autoload(const, path)
135
+ %x{
136
+ var autoloaders;
137
+
138
+ if (!(autoloaders = self.$$autoload)) {
139
+ autoloaders = self.$$autoload = {};
140
+ }
141
+
142
+ autoloaders[#{const}] = #{path};
143
+ return nil;
144
+ }
145
+ end
146
+
183
147
  def constants
184
- `self._scope.constants`
148
+ `self.$$scope.constants`
185
149
  end
186
150
 
187
151
  # check for constant within current scope
@@ -190,12 +154,12 @@ class Module
190
154
  raise NameError, "wrong constant name #{name}" unless name =~ /^[A-Z]\w*$/
191
155
 
192
156
  %x{
193
- scopes = [self._scope];
157
+ scopes = [self.$$scope];
194
158
  if (inherit || self === Opal.Object) {
195
- var parent = self._super;
159
+ var parent = self.$$super;
196
160
  while (parent !== Opal.BasicObject) {
197
- scopes.push(parent._scope);
198
- parent = parent._super;
161
+ scopes.push(parent.$$scope);
162
+ parent = parent.$$super;
199
163
  }
200
164
  }
201
165
 
@@ -213,12 +177,12 @@ class Module
213
177
  raise NameError, "wrong constant name #{name}" unless name =~ /^[A-Z]\w*$/
214
178
 
215
179
  %x{
216
- var scopes = [self._scope];
180
+ var scopes = [self.$$scope];
217
181
  if (inherit || self == Opal.Object) {
218
- var parent = self._super;
182
+ var parent = self.$$super;
219
183
  while (parent !== Opal.BasicObject) {
220
- scopes.push(parent._scope);
221
- parent = parent._super;
184
+ scopes.push(parent.$$scope);
185
+ parent = parent.$$super;
222
186
  }
223
187
  }
224
188
 
@@ -233,7 +197,16 @@ class Module
233
197
  end
234
198
 
235
199
  def const_missing(const)
236
- name = `self._name`
200
+ %x{
201
+ var autoloader;
202
+
203
+ if (self.$$autoload && (autoloader = self.$$autoload[#{const}])) {
204
+ self.$require(autoloader);
205
+ return self.$$scope.get(#{const});
206
+ }
207
+ }
208
+
209
+ name = `self.$$name`
237
210
 
238
211
  raise NameError, "uninitialized constant #{name}::#{const}"
239
212
  end
@@ -264,11 +237,11 @@ class Module
264
237
  }
265
238
 
266
239
  var jsid = '$' + name;
267
- block._jsid = name;
268
- block._s = null;
269
- block._def = block;
240
+ block.$$jsid = name;
241
+ block.$$s = null;
242
+ block.$$def = block;
270
243
 
271
- self._proto[jsid] = block;
244
+ self.$$proto[jsid] = block;
272
245
  $opal.donate(self, [jsid]);
273
246
 
274
247
  return name;
@@ -278,8 +251,8 @@ class Module
278
251
  def remove_method(name)
279
252
  %x{
280
253
  var jsid = '$' + name;
281
- var current = self._proto[jsid];
282
- delete self._proto[jsid];
254
+ var current = self.$$proto[jsid];
255
+ delete self.$$proto[jsid];
283
256
 
284
257
  // Check if we need to reverse $opal.donate
285
258
  // $opal.retire(self, [jsid]);
@@ -307,8 +280,8 @@ class Module
307
280
  def include?(mod)
308
281
  %x{
309
282
  for (var cls = self; cls; cls = cls.parent) {
310
- for (var i = 0; i != cls.__inc__.length; i++) {
311
- var mod2 = cls.__inc__[i];
283
+ for (var i = 0; i != cls.$$inc.length; i++) {
284
+ var mod2 = cls.$$inc[i];
312
285
  if (mod === mod2) {
313
286
  return true;
314
287
  }
@@ -320,9 +293,9 @@ class Module
320
293
 
321
294
  def instance_method(name)
322
295
  %x{
323
- var meth = self._proto['$' + name];
296
+ var meth = self.$$proto['$' + name];
324
297
 
325
- if (!meth || meth.rb_stub) {
298
+ if (!meth || meth.$$stub) {
326
299
  #{raise NameError, "undefined method `#{name}' for class `#{self.name}'"};
327
300
  }
328
301
 
@@ -332,14 +305,14 @@ class Module
332
305
 
333
306
  def instance_methods(include_super = false)
334
307
  %x{
335
- var methods = [], proto = self._proto;
308
+ var methods = [], proto = self.$$proto;
336
309
 
337
- for (var prop in self._proto) {
310
+ for (var prop in self.$$proto) {
338
311
  if (!include_super && !proto.hasOwnProperty(prop)) {
339
312
  continue;
340
313
  }
341
314
 
342
- if (!include_super && proto[prop]._donated) {
315
+ if (!include_super && proto[prop].$$donated) {
343
316
  continue;
344
317
  }
345
318
 
@@ -362,12 +335,12 @@ class Module
362
335
  raise ArgumentError, 'no block given' unless block
363
336
 
364
337
  %x{
365
- var old = block._s,
338
+ var old = block.$$s,
366
339
  result;
367
340
 
368
- block._s = null;
341
+ block.$$s = null;
369
342
  result = block.call(self);
370
- block._s = old;
343
+ block.$$s = old;
371
344
 
372
345
  return result;
373
346
  }
@@ -381,11 +354,11 @@ class Module
381
354
  throw new Error("no block given");
382
355
  }
383
356
 
384
- var block_self = block._s, result;
357
+ var block_self = block.$$s, result;
385
358
 
386
- block._s = null;
359
+ block.$$s = null;
387
360
  result = block.apply(self, $slice.call(arguments));
388
- block._s = block_self;
361
+ block.$$s = block_self;
389
362
 
390
363
  return result;
391
364
  }
@@ -395,15 +368,15 @@ class Module
395
368
 
396
369
  def method_defined?(method)
397
370
  %x{
398
- var body = self._proto['$' + method];
399
- return (!!body) && !body.rb_stub;
371
+ var body = self.$$proto['$' + method];
372
+ return (!!body) && !body.$$stub;
400
373
  }
401
374
  end
402
375
 
403
376
  def module_function(*methods)
404
377
  %x{
405
378
  for (var i = 0, length = methods.length; i < length; i++) {
406
- var meth = methods[i], func = self._proto['$' + meth];
379
+ var meth = methods[i], func = self.$$proto['$' + meth];
407
380
 
408
381
  self.constructor.prototype['$' + meth] = func;
409
382
  }
@@ -414,20 +387,20 @@ class Module
414
387
 
415
388
  def name
416
389
  %x{
417
- if (self._full_name) {
418
- return self._full_name;
390
+ if (self.$$full_name) {
391
+ return self.$$full_name;
419
392
  }
420
393
 
421
394
  var result = [], base = self;
422
395
 
423
396
  while (base) {
424
- if (base._name === nil) {
397
+ if (base.$$name === nil) {
425
398
  return result.length === 0 ? nil : result.join('::');
426
399
  }
427
400
 
428
- result.unshift(base._name);
401
+ result.unshift(base.$$name);
429
402
 
430
- base = base._base_module;
403
+ base = base.$$base_module;
431
404
 
432
405
  if (base === $opal.Object) {
433
406
  break;
@@ -438,19 +411,21 @@ class Module
438
411
  return nil;
439
412
  }
440
413
 
441
- return self._full_name = result.join('::');
414
+ return self.$$full_name = result.join('::');
442
415
  }
443
416
  end
444
417
 
445
418
  def public(*)
446
419
  end
447
420
 
421
+ alias private public
422
+ alias protected public
423
+ alias nesting public
424
+
448
425
  def private_class_method(name)
449
426
  `self['$' + name] || nil`
450
427
  end
451
-
452
- alias private public
453
- alias protected public
428
+ alias public_class_method private_class_method
454
429
 
455
430
  def private_method_defined?(obj)
456
431
  false
@@ -470,8 +445,8 @@ class Module
470
445
 
471
446
  def remove_const(name)
472
447
  %x{
473
- var old = self._scope[name];
474
- delete self._scope[name];
448
+ var old = self.$$scope[name];
449
+ delete self.$$scope[name];
475
450
  return old;
476
451
  }
477
452
  end
@@ -481,7 +456,7 @@ class Module
481
456
  end
482
457
 
483
458
  def undef_method(symbol)
484
- `$opal.add_stub_for(self._proto, "$" + symbol)`
459
+ `$opal.add_stub_for(self.$$proto, "$" + symbol)`
485
460
  self
486
461
  end
487
462
  end