opal 0.4.3 → 0.4.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (303) hide show
  1. data/.rspec +3 -0
  2. data/.travis.yml +0 -4
  3. data/README.md +6 -0
  4. data/Rakefile +30 -6
  5. data/bin/opal +6 -82
  6. data/corelib/{opal/array.rb → array.rb} +40 -56
  7. data/corelib/{opal/basic_object.rb → basic_object.rb} +2 -2
  8. data/corelib/{opal/boolean.rb → boolean.rb} +0 -8
  9. data/corelib/class.rb +47 -0
  10. data/corelib/{opal/comparable.rb → comparable.rb} +0 -0
  11. data/corelib/{opal/enumerable.rb → enumerable.rb} +208 -141
  12. data/corelib/{opal/enumerator.rb → enumerator.rb} +3 -3
  13. data/corelib/{opal/error.rb → error.rb} +1 -0
  14. data/corelib/{opal/hash.rb → hash.rb} +67 -56
  15. data/corelib/io.rb +39 -0
  16. data/corelib/{opal/kernel.rb → kernel.rb} +35 -31
  17. data/corelib/{opal/class.rb → module.rb} +29 -57
  18. data/corelib/native.rb +148 -0
  19. data/corelib/{opal/nil_class.rb → nil_class.rb} +2 -10
  20. data/corelib/{opal/numeric.rb → numeric.rb} +39 -14
  21. data/corelib/opal.rb +42 -25
  22. data/corelib/{opal/proc.rb → proc.rb} +3 -3
  23. data/corelib/{opal/range.rb → range.rb} +5 -1
  24. data/corelib/{opal/regexp.rb → regexp.rb} +0 -0
  25. data/corelib/{opal/runtime.js → runtime.js} +96 -188
  26. data/corelib/{opal/string.rb → string.rb} +20 -15
  27. data/corelib/struct.rb +139 -0
  28. data/corelib/{opal/time.rb → time.rb} +0 -0
  29. data/lib/opal.rb +4 -0
  30. data/lib/opal/cli.rb +79 -32
  31. data/lib/opal/cli_options.rb +91 -0
  32. data/lib/opal/erb.rb +41 -8
  33. data/lib/opal/grammar.rb +24 -10
  34. data/lib/opal/grammar.y +15 -4
  35. data/lib/opal/grammar_helpers.rb +4 -0
  36. data/lib/opal/lexer.rb +214 -143
  37. data/lib/opal/parser.rb +596 -562
  38. data/lib/opal/require_parser.rb +1 -1
  39. data/lib/opal/source_map.rb +15 -30
  40. data/lib/opal/target_scope.rb +24 -5
  41. data/lib/opal/version.rb +1 -1
  42. data/mri_spec/cli_spec.rb +18 -0
  43. data/mri_spec/fixtures/opal_file.rb +2 -0
  44. data/mri_spec/spec_helper.rb +17 -0
  45. data/opal.gemspec +5 -2
  46. data/spec/{rubyspec/filters → filters}/bugs/alias.rb +0 -0
  47. data/spec/{rubyspec/filters → filters}/bugs/ancestors.rb +0 -0
  48. data/spec/{rubyspec/filters → filters}/bugs/array.rb +0 -0
  49. data/spec/filters/bugs/array/combination.rb +11 -0
  50. data/spec/filters/bugs/array/count.rb +3 -0
  51. data/spec/filters/bugs/array/delete_if.rb +3 -0
  52. data/spec/filters/bugs/array/drop.rb +3 -0
  53. data/spec/filters/bugs/array/drop_while.rb +5 -0
  54. data/spec/filters/bugs/array/eql.rb +3 -0
  55. data/spec/filters/bugs/array/flatten.rb +9 -0
  56. data/spec/filters/bugs/array/minus.rb +5 -0
  57. data/spec/filters/bugs/array/multipliy.rb +9 -0
  58. data/spec/filters/bugs/array/new.rb +3 -0
  59. data/spec/filters/bugs/array/pop.rb +6 -0
  60. data/spec/filters/bugs/array/rassoc.rb +4 -0
  61. data/spec/filters/bugs/array/rindex.rb +6 -0
  62. data/spec/filters/bugs/array/select.rb +3 -0
  63. data/spec/filters/bugs/array/shift.rb +7 -0
  64. data/spec/filters/bugs/array/shuffle.rb +11 -0
  65. data/spec/filters/bugs/array/slice.rb +7 -0
  66. data/spec/filters/bugs/array/take.rb +3 -0
  67. data/spec/filters/bugs/array/to_a.rb +3 -0
  68. data/spec/filters/bugs/array/try_convert.rb +7 -0
  69. data/spec/filters/bugs/array/uniq.rb +10 -0
  70. data/spec/filters/bugs/array/zip.rb +4 -0
  71. data/spec/{rubyspec/filters → filters}/bugs/array_delete.rb +1 -0
  72. data/spec/{rubyspec/filters → filters}/bugs/array_fetch.rb +0 -0
  73. data/spec/{rubyspec/filters → filters}/bugs/array_first.rb +0 -0
  74. data/spec/{rubyspec/filters → filters}/bugs/array_flatten.rb +0 -0
  75. data/spec/{rubyspec/filters → filters}/bugs/array_intersection.rb +0 -0
  76. data/spec/{rubyspec/filters → filters}/bugs/array_join.rb +0 -0
  77. data/spec/{rubyspec/filters → filters}/bugs/break.rb +0 -0
  78. data/spec/filters/bugs/case.rb +4 -0
  79. data/spec/{rubyspec/filters → filters}/bugs/coerce_integer.rb +0 -0
  80. data/spec/filters/bugs/enumerable_sort_by.rb +3 -0
  81. data/spec/{rubyspec/filters → filters}/bugs/kernel/instance_variables.rb +0 -0
  82. data/spec/filters/bugs/kernel/rand.rb +4 -0
  83. data/spec/filters/bugs/language/array.rb +3 -0
  84. data/spec/filters/bugs/language/block.rb +6 -0
  85. data/spec/filters/bugs/language/break.rb +3 -0
  86. data/spec/{rubyspec/filters → filters}/bugs/language/class.rb +3 -0
  87. data/spec/{rubyspec/filters → filters}/bugs/language/class_variables.rb +0 -0
  88. data/spec/filters/bugs/language/def.rb +27 -0
  89. data/spec/filters/bugs/language/defined.rb +3 -0
  90. data/spec/filters/bugs/language/ensure.rb +4 -0
  91. data/spec/filters/bugs/language/execution.rb +4 -0
  92. data/spec/filters/bugs/language/for.rb +18 -0
  93. data/spec/filters/bugs/language/if.rb +13 -0
  94. data/spec/filters/bugs/language/loop.rb +4 -0
  95. data/spec/filters/bugs/language/metaclass.rb +14 -0
  96. data/spec/filters/bugs/language/module.rb +6 -0
  97. data/spec/filters/bugs/language/next.rb +3 -0
  98. data/spec/filters/bugs/language/or.rb +3 -0
  99. data/spec/filters/bugs/language/order.rb +4 -0
  100. data/spec/filters/bugs/language/precedence.rb +10 -0
  101. data/spec/filters/bugs/language/proc.rb +24 -0
  102. data/spec/filters/bugs/language/redo.rb +5 -0
  103. data/spec/filters/bugs/language/rescue.rb +9 -0
  104. data/spec/filters/bugs/language/retry.rb +5 -0
  105. data/spec/filters/bugs/language/send.rb +10 -0
  106. data/spec/filters/bugs/language/super.rb +9 -0
  107. data/spec/filters/bugs/language/until.rb +8 -0
  108. data/spec/filters/bugs/language/variables.rb +37 -0
  109. data/spec/filters/bugs/language/while.rb +6 -0
  110. data/spec/filters/bugs/language/yield.rb +5 -0
  111. data/spec/{rubyspec/filters → filters}/bugs/module/class_variables.rb +0 -0
  112. data/spec/filters/bugs/module/method_defined.rb +6 -0
  113. data/spec/filters/bugs/parser.rb +10 -0
  114. data/spec/{rubyspec/filters → filters}/bugs/public_methods.rb +1 -0
  115. data/spec/filters/bugs/return.rb +7 -0
  116. data/spec/filters/bugs/singleton/instance.rb +4 -0
  117. data/spec/filters/bugs/source_map.rb +3 -0
  118. data/spec/filters/bugs/string/center.rb +4 -0
  119. data/spec/filters/bugs/string/lines.rb +3 -0
  120. data/spec/{rubyspec/filters → filters}/mspec/mocks.rb +0 -0
  121. data/spec/{rubyspec/filters → filters}/mspec/ruby_exe.rb +0 -0
  122. data/spec/{rubyspec/filters → filters}/mspec/should_receive.rb +0 -0
  123. data/spec/{rubyspec/filters → filters}/parser/block_args.rb +0 -0
  124. data/spec/{rubyspec/filters → filters}/unsupported/array_subclasses.rb +1 -0
  125. data/spec/filters/unsupported/frozen.rb +32 -0
  126. data/spec/filters/unsupported/immutable_strings.rb +3 -0
  127. data/spec/filters/unsupported/tainted.rb +17 -0
  128. data/spec/filters/unsupported/trusted.rb +15 -0
  129. data/spec/opal/class/constants_spec.rb +7 -0
  130. data/spec/opal/erb/erb_spec.rb +7 -1
  131. data/spec/opal/erb/inline_block.opalerb +3 -0
  132. data/spec/opal/hash/allocate_spec.rb +16 -0
  133. data/spec/opal/hash/new_spec.rb +10 -0
  134. data/spec/opal/hash/to_s_spec.rb +9 -0
  135. data/spec/opal/kernel/instance_variable_defined_spec.rb +15 -0
  136. data/spec/opal/kernel/rand_spec.rb +5 -5
  137. data/spec/opal/kernel/respond_to_spec.rb +14 -1
  138. data/spec/opal/language/block_spec.rb +13 -0
  139. data/spec/opal/language/rescue_spec.rb +27 -0
  140. data/spec/opal/language/return_spec.rb +38 -0
  141. data/spec/opal/language/singleton_class_spec.rb +13 -0
  142. data/spec/opal/language/super_spec.rb +99 -0
  143. data/spec/opal/language/variables_spec.rb +20 -0
  144. data/spec/opal/module/attr_accessor_spec.rb +8 -0
  145. data/spec/opal/module/constants_spec.rb +2 -2
  146. data/spec/opal/native/alias_native_spec.rb +18 -0
  147. data/spec/opal/native/each_spec.rb +14 -0
  148. data/spec/opal/native/element_reference_spec.rb +14 -0
  149. data/spec/opal/native/method_missing_spec.rb +39 -0
  150. data/spec/opal/native/new_spec.rb +19 -0
  151. data/spec/opal/native/nil_spec.rb +14 -0
  152. data/spec/opal/runtime2/class_hierarchy_spec.rb +2 -2
  153. data/spec/opal/source_map_spec.rb +3 -7
  154. data/spec/ospec/main.rb.erb +2 -5
  155. data/spec/parser/alias_spec.rb +4 -4
  156. data/spec/parser/and_spec.rb +2 -2
  157. data/spec/parser/array_spec.rb +6 -6
  158. data/spec/parser/attrasgn_spec.rb +8 -8
  159. data/spec/parser/begin_spec.rb +11 -11
  160. data/spec/parser/block_spec.rb +3 -3
  161. data/spec/parser/break_spec.rb +4 -4
  162. data/spec/parser/call_spec.rb +50 -48
  163. data/spec/parser/class_spec.rb +2 -2
  164. data/spec/parser/const_spec.rb +1 -1
  165. data/spec/parser/cvar_spec.rb +1 -1
  166. data/spec/parser/def_spec.rb +5 -5
  167. data/spec/parser/gvar_spec.rb +2 -2
  168. data/spec/parser/hash_spec.rb +4 -4
  169. data/spec/parser/iasgn_spec.rb +3 -3
  170. data/spec/parser/if_spec.rb +6 -6
  171. data/spec/parser/iter_spec.rb +6 -6
  172. data/spec/parser/lambda_spec.rb +5 -5
  173. data/spec/parser/lasgn_spec.rb +2 -2
  174. data/spec/parser/line_spec.rb +2 -2
  175. data/spec/parser/lvar_spec.rb +5 -5
  176. data/spec/parser/masgn_spec.rb +1 -1
  177. data/spec/parser/module_spec.rb +2 -2
  178. data/spec/parser/not_spec.rb +4 -4
  179. data/spec/parser/op_asgn1_spec.rb +2 -2
  180. data/spec/parser/op_asgn2_spec.rb +2 -2
  181. data/spec/parser/or_spec.rb +2 -2
  182. data/spec/parser/parse_spec.rb +66 -0
  183. data/spec/parser/parser_spec.rb +32 -38
  184. data/spec/parser/regexp_spec.rb +4 -4
  185. data/spec/parser/return_spec.rb +4 -4
  186. data/spec/parser/sclass_spec.rb +4 -4
  187. data/spec/parser/str_spec.rb +3 -3
  188. data/spec/parser/super_spec.rb +6 -6
  189. data/spec/parser/undef_spec.rb +3 -3
  190. data/spec/parser/unless_spec.rb +4 -4
  191. data/spec/parser/while_spec.rb +3 -3
  192. data/spec/parser/xstr_spec.rb +3 -3
  193. data/spec/parser/yield_spec.rb +6 -6
  194. data/spec/rubyspec/core/array/drop_spec.rb +1 -1
  195. data/spec/rubyspec/core/array/keep_if_spec.rb +1 -3
  196. data/spec/rubyspec/core/array/length_spec.rb +1 -3
  197. data/spec/rubyspec/core/array/map_spec.rb +2 -6
  198. data/spec/rubyspec/core/array/minus_spec.rb +3 -3
  199. data/spec/rubyspec/core/array/multiply_spec.rb +14 -16
  200. data/spec/rubyspec/core/array/new_spec.rb +3 -5
  201. data/spec/rubyspec/core/array/plus_spec.rb +2 -2
  202. data/spec/rubyspec/core/array/pop_spec.rb +4 -4
  203. data/spec/rubyspec/core/array/rassoc_spec.rb +2 -2
  204. data/spec/rubyspec/core/array/reject_spec.rb +2 -6
  205. data/spec/rubyspec/core/array/replace_spec.rb +1 -3
  206. data/spec/rubyspec/core/array/reverse_each_spec.rb +1 -3
  207. data/spec/rubyspec/core/array/reverse_spec.rb +4 -4
  208. data/spec/rubyspec/core/array/rindex_spec.rb +1 -1
  209. data/spec/rubyspec/core/array/select_spec.rb +3 -7
  210. data/spec/rubyspec/core/array/shared/collect.rb +7 -0
  211. data/spec/rubyspec/core/array/shared/index.rb +1 -1
  212. data/spec/rubyspec/core/array/shared/keep_if.rb +3 -0
  213. data/spec/rubyspec/core/array/shared/length.rb +3 -0
  214. data/spec/rubyspec/core/array/shared/replace.rb +3 -0
  215. data/spec/rubyspec/core/array/shared/slice.rb +3 -0
  216. data/spec/rubyspec/core/array/shuffle_spec.rb +1 -1
  217. data/spec/rubyspec/core/array/size_spec.rb +1 -3
  218. data/spec/rubyspec/core/array/slice_spec.rb +4 -6
  219. data/spec/rubyspec/core/array/sort_spec.rb +2 -2
  220. data/spec/rubyspec/core/array/to_a_spec.rb +1 -1
  221. data/spec/rubyspec/core/array/try_convert_spec.rb +7 -7
  222. data/spec/rubyspec/core/array/uniq_spec.rb +7 -7
  223. data/spec/rubyspec/core/array/zip_spec.rb +1 -1
  224. data/spec/rubyspec/core/enumerable/collect_spec.rb +24 -0
  225. data/spec/rubyspec/core/enumerable/count_spec.rb +35 -19
  226. data/spec/rubyspec/core/enumerable/find_spec.rb +5 -0
  227. data/spec/rubyspec/core/hash/each_pair_spec.rb +7 -7
  228. data/spec/rubyspec/core/hash/each_spec.rb +13 -7
  229. data/spec/rubyspec/core/module/method_defined_spec.rb +4 -4
  230. data/spec/rubyspec/core/module/public_method_defined_spec.rb +19 -0
  231. data/spec/rubyspec/core/module/remove_const_spec.rb +23 -0
  232. data/spec/rubyspec/core/numeric/step_spec.rb +14 -0
  233. data/spec/rubyspec/core/string/center_spec.rb +43 -65
  234. data/spec/rubyspec/core/string/lines_spec.rb +1 -1
  235. data/spec/rubyspec/core/string/ljust_spec.rb +25 -9
  236. data/spec/rubyspec/core/string/rjust_spec.rb +26 -10
  237. data/spec/rubyspec/core/struct/fixtures/classes.rb +26 -0
  238. data/spec/rubyspec/core/struct/initialize_spec.rb +11 -0
  239. data/spec/rubyspec/core/struct/new_spec.rb +24 -0
  240. data/spec/rubyspec/fixtures/constants.rb +7 -0
  241. data/spec/rubyspec/language/break_spec.rb +1 -1
  242. data/spec/rubyspec/language/case_spec.rb +30 -30
  243. data/spec/rubyspec/language/def_spec.rb +34 -34
  244. data/spec/rubyspec/language/defined_spec.rb +2 -2
  245. data/spec/rubyspec/language/ensure_spec.rb +2 -2
  246. data/spec/rubyspec/language/execution_spec.rb +2 -2
  247. data/spec/rubyspec/language/for_spec.rb +17 -17
  248. data/spec/rubyspec/language/hash_spec.rb +3 -3
  249. data/spec/rubyspec/language/if_spec.rb +11 -11
  250. data/spec/rubyspec/language/loop_spec.rb +3 -3
  251. data/spec/rubyspec/language/metaclass_spec.rb +14 -14
  252. data/spec/rubyspec/language/module_spec.rb +6 -6
  253. data/spec/rubyspec/language/next_spec.rb +5 -5
  254. data/spec/rubyspec/language/not_spec.rb +1 -1
  255. data/spec/rubyspec/language/or_spec.rb +1 -1
  256. data/spec/rubyspec/language/order_spec.rb +3 -5
  257. data/spec/rubyspec/language/precedence_spec.rb +19 -19
  258. data/spec/rubyspec/language/proc_spec.rb +29 -29
  259. data/spec/rubyspec/language/redo_spec.rb +3 -5
  260. data/spec/rubyspec/language/rescue_spec.rb +7 -7
  261. data/spec/rubyspec/language/retry_spec.rb +2 -2
  262. data/spec/rubyspec/language/return_spec.rb +12 -12
  263. data/spec/rubyspec/language/send_spec.rb +9 -9
  264. data/spec/rubyspec/language/singleton_class_spec.rb +3 -4
  265. data/spec/rubyspec/language/super_spec.rb +7 -11
  266. data/spec/rubyspec/language/until_spec.rb +10 -10
  267. data/spec/rubyspec/language/variables_spec.rb +59 -67
  268. data/spec/rubyspec/language/versions/array_1.9.rb +1 -1
  269. data/spec/rubyspec/language/versions/block_1.9.rb +0 -0
  270. data/spec/rubyspec/language/versions/break_1.9.rb +0 -0
  271. data/spec/rubyspec/language/versions/literal_lambda_1.9.rb +12 -12
  272. data/spec/rubyspec/language/versions/send_1.9.rb +23 -23
  273. data/spec/rubyspec/language/versions/symbol_1.9.rb +2 -2
  274. data/spec/rubyspec/language/versions/variables_1.9.rb +1 -1
  275. data/spec/rubyspec/language/while_spec.rb +4 -4
  276. data/spec/rubyspec/language/yield_spec.rb +3 -3
  277. data/spec/rubyspec/library/erb/util/html_escape_spec.rb +10 -0
  278. data/spec/rubyspec/library/singleton/clone_spec.rb +8 -0
  279. data/spec/rubyspec/library/singleton/dup_spec.rb +8 -0
  280. data/spec/rubyspec/library/singleton/fixtures/classes.rb +18 -0
  281. data/spec/rubyspec/library/singleton/instance_spec.rb +30 -0
  282. data/spec/spec_helper.rb +214 -0
  283. data/stdlib/buffer.rb +40 -0
  284. data/stdlib/buffer/array.rb +66 -0
  285. data/stdlib/buffer/view.rb +70 -0
  286. data/stdlib/erb.rb +11 -20
  287. data/stdlib/forwardable.rb +71 -0
  288. data/stdlib/json.rb +78 -0
  289. data/stdlib/ostruct.rb +69 -0
  290. data/stdlib/rbconfig.rb +1 -0
  291. data/stdlib/singleton.rb +40 -0
  292. data/stdlib/stringio.rb +173 -0
  293. data/stdlib/template.rb +44 -0
  294. metadata +285 -79
  295. data/corelib/opal/native.rb +0 -31
  296. data/spec/opal/class/bridge_class_spec.rb +0 -39
  297. data/spec/opal/native_spec.rb +0 -209
  298. data/spec/ospec/runner.rb +0 -223
  299. data/spec/rubyspec/filters/bugs/block_args.rb +0 -3
  300. data/spec/rubyspec/filters/bugs/case.rb +0 -8
  301. data/spec/rubyspec/filters/bugs/language/module.rb +0 -3
  302. data/spec/rubyspec/filters/unsupported/frozen.rb +0 -4
  303. data/spec/rubyspec/filters/unsupported/tainted.rb +0 -7
data/corelib/class.rb ADDED
@@ -0,0 +1,47 @@
1
+ class Class
2
+ def self.new(sup = Object, &block)
3
+ %x{
4
+ function AnonClass(){};
5
+ var klass = Opal.boot(sup, AnonClass)
6
+ klass._name = nil;
7
+ klass._scope = sup._scope;
8
+
9
+ sup.$inherited(klass);
10
+
11
+ if (block !== nil) {
12
+ var block_self = block._s;
13
+ block._s = null;
14
+ block.call(klass);
15
+ block._s = block_self;
16
+ }
17
+
18
+ return klass;
19
+ }
20
+ end
21
+
22
+ def allocate
23
+ %x{
24
+ var obj = new #{self}._alloc;
25
+ obj._id = Opal.uid();
26
+ return obj;
27
+ }
28
+ end
29
+
30
+ def inherited(cls)
31
+ end
32
+
33
+ def new(*args, &block)
34
+ %x{
35
+ var obj = #{allocate};
36
+ obj._id = Opal.uid();
37
+
38
+ obj.$initialize._p = block;
39
+ obj.$initialize.apply(obj, args);
40
+ return obj;
41
+ }
42
+ end
43
+
44
+ def superclass
45
+ `#{self}._super || nil`
46
+ end
47
+ end
File without changes
@@ -6,30 +6,27 @@ module Enumerable
6
6
  if (block !== nil) {
7
7
  proc = function(obj) {
8
8
  var value;
9
- var args = [];
10
- for(var i = 0; i < arguments.length; i ++) {
11
- args[i] = arguments[i];
12
- }
9
+ var args = $slice.call(arguments);
13
10
 
14
- if ((value = block.apply(#{self}, args)) === __breaker) {
15
- return __breaker.$v;
11
+ if ((value = block.apply(#{self}, args)) === $breaker) {
12
+ return $breaker.$v;
16
13
  }
17
14
 
18
15
  if (value === false || value === nil) {
19
16
  result = false;
20
- __breaker.$v = nil;
17
+ $breaker.$v = nil;
21
18
 
22
- return __breaker;
19
+ return $breaker;
23
20
  }
24
21
  }
25
22
  }
26
23
  else {
27
24
  proc = function(obj) {
28
25
  if ((obj === false || obj === nil) && arguments.length < 2) {
29
- result = false;
30
- __breaker.$v = nil;
26
+ result = false;
27
+ $breaker.$v = nil;
31
28
 
32
- return __breaker;
29
+ return $breaker;
33
30
  }
34
31
  }
35
32
  }
@@ -48,20 +45,17 @@ module Enumerable
48
45
  if (block !== nil) {
49
46
  proc = function(obj) {
50
47
  var value;
51
- var args = [];
52
- for(var i = 0; i < arguments.length; i ++) {
53
- args[i] = arguments[i];
54
- }
48
+ var args = $slice.call(arguments);
55
49
 
56
- if ((value = block.apply(#{self}, args)) === __breaker) {
57
- return __breaker.$v;
50
+ if ((value = block.apply(#{self}, args)) === $breaker) {
51
+ return $breaker.$v;
58
52
  }
59
53
 
60
54
  if (value !== false && value !== nil) {
61
55
  result = true;
62
- __breaker.$v = nil;
56
+ $breaker.$v = nil;
63
57
 
64
- return __breaker;
58
+ return $breaker;
65
59
  }
66
60
  }
67
61
  }
@@ -69,9 +63,9 @@ module Enumerable
69
63
  proc = function(obj) {
70
64
  if ((obj !== false && obj !== nil) || arguments.length >= 2) {
71
65
  result = true;
72
- __breaker.$v = nil;
66
+ $breaker.$v = nil;
73
67
 
74
- return __breaker;
68
+ return $breaker;
75
69
  }
76
70
  }
77
71
  }
@@ -84,14 +78,16 @@ module Enumerable
84
78
  end
85
79
 
86
80
  def collect(&block)
81
+ return enum_for :collect unless block_given?
82
+
87
83
  %x{
88
84
  var result = [];
89
85
 
90
86
  var proc = function() {
91
- var obj = __slice.call(arguments), value;
87
+ var value, args = $slice.call(arguments);
92
88
 
93
- if ((value = block.apply(nil, obj)) === __breaker) {
94
- return __breaker.$v;
89
+ if ((value = block.apply(null, arguments)) === $breaker) {
90
+ return $breaker.$v;
95
91
  }
96
92
 
97
93
  result.push(value);
@@ -109,13 +105,13 @@ module Enumerable
109
105
  var result = #{object} == undefined ? 0 : #{object};
110
106
 
111
107
  var proc = function() {
112
- var obj = __slice.call(arguments), value;
108
+ var obj = $slice.call(arguments), value;
113
109
 
114
- if ((value = block.apply(nil, [result].concat(obj))) === __breaker) {
115
- result = __breaker.$v;
116
- __breaker.$v = nil;
110
+ if ((value = block.apply(nil, [result].concat(obj))) === $breaker) {
111
+ result = $breaker.$v;
112
+ $breaker.$v = nil;
117
113
 
118
- return __breaker;
114
+ return $breaker;
119
115
  }
120
116
 
121
117
  result = value;
@@ -133,17 +129,22 @@ module Enumerable
133
129
  var result = 0;
134
130
 
135
131
  if (object != null) {
136
- block = function(obj) { return #{ `obj` == `object` }; };
132
+ block = function() {
133
+ var param = arguments.length == 1 ?
134
+ arguments[0] : $slice.call(arguments);
135
+
136
+ return #{ `param` == `object` };
137
+ };
137
138
  }
138
139
  else if (block === nil) {
139
140
  block = function() { return true; };
140
141
  }
141
142
 
142
- var proc = function(obj) {
143
- var value;
143
+ var proc = function() {
144
+ var value, param = $slice.call(arguments);
144
145
 
145
- if ((value = block(obj)) === __breaker) {
146
- return __breaker.$v;
146
+ if ((value = block.apply(null, param)) === $breaker) {
147
+ return $breaker.$v;
147
148
  }
148
149
 
149
150
  if (value !== false && value !== nil) {
@@ -162,18 +163,20 @@ module Enumerable
162
163
  %x{
163
164
  var result = nil;
164
165
 
165
- #{self}.$each._p = function(obj) {
166
+ #{self}.$each._p = function() {
166
167
  var value;
168
+ var param = arguments.length == 1 ?
169
+ arguments[0] : $slice.call(arguments);
167
170
 
168
- if ((value = block(obj)) === __breaker) {
169
- return __breaker.$v;
171
+ if ((value = block(param)) === $breaker) {
172
+ return $breaker.$v;
170
173
  }
171
174
 
172
175
  if (value !== false && value !== nil) {
173
- result = obj;
174
- __breaker.$v = nil;
176
+ result = param;
177
+ $breaker.$v = nil;
175
178
 
176
- return __breaker;
179
+ return $breaker;
177
180
  }
178
181
  };
179
182
 
@@ -184,7 +187,7 @@ module Enumerable
184
187
  }
185
188
 
186
189
  if (typeof(ifnone) === 'function') {
187
- return #{ ifnone.call };
190
+ return #{ifnone.call};
188
191
  }
189
192
 
190
193
  return ifnone == null ? nil : ifnone;
@@ -196,7 +199,7 @@ module Enumerable
196
199
  var result = [],
197
200
  current = 0;
198
201
 
199
- #{self}.$each._p = function(obj) {
202
+ #{self}.$each._p = function() {
200
203
  if (number < current) {
201
204
  result.push(e);
202
205
  }
@@ -214,19 +217,21 @@ module Enumerable
214
217
  %x{
215
218
  var result = [];
216
219
 
217
- #{self}.$each._p = function(obj) {
220
+ #{self}.$each._p = function() {
218
221
  var value;
222
+ var param = arguments.length == 1 ?
223
+ arguments[0] : $slice.call(arguments);
219
224
 
220
- if ((value = block(obj)) === __breaker) {
221
- return __breaker;
225
+ if ((value = block(param)) === $breaker) {
226
+ return $breaker;
222
227
  }
223
228
 
224
229
  if (value === false || value === nil) {
225
- result.push(obj);
230
+ result.push(param);
226
231
  return value;
227
232
  }
228
233
 
229
- return __breaker;
234
+ return $breaker;
230
235
  };
231
236
 
232
237
  #{self}.$each();
@@ -239,8 +244,11 @@ module Enumerable
239
244
  %x{
240
245
  var all = [];
241
246
 
242
- #{self}.$each._p = function(obj) {
243
- all.push(obj);
247
+ #{self}.$each._p = function() {
248
+ var param = arguments.length == 1 ?
249
+ arguments[0] : $slice.call(arguments);
250
+
251
+ all.push(param);
244
252
 
245
253
  if (all.length == n) {
246
254
  block(all.slice(0));
@@ -263,11 +271,13 @@ module Enumerable
263
271
  %x{
264
272
  var index = 0;
265
273
 
266
- #{self}.$each._p = function(obj) {
274
+ #{self}.$each._p = function() {
267
275
  var value;
276
+ var param = arguments.length == 1 ?
277
+ arguments[0] : $slice.call(arguments);
268
278
 
269
- if ((value = block(obj, index)) === __breaker) {
270
- return __breaker.$v;
279
+ if ((value = block(param, index)) === $breaker) {
280
+ return $breaker.$v;
271
281
  }
272
282
 
273
283
  index++;
@@ -280,11 +290,13 @@ module Enumerable
280
290
 
281
291
  def each_with_object(object, &block)
282
292
  %x{
283
- #{self}.$each._p = function(obj) {
293
+ #{self}.$each._p = function() {
284
294
  var value;
295
+ var param = arguments.length == 1 ?
296
+ arguments[0] : $slice.call(arguments);
285
297
 
286
- if ((value = block(obj, object)) === __breaker) {
287
- return __breaker.$v;
298
+ if ((value = block(param, object)) === $breaker) {
299
+ return $breaker.$v;
288
300
  }
289
301
  };
290
302
 
@@ -298,8 +310,13 @@ module Enumerable
298
310
  %x{
299
311
  var result = [];
300
312
 
301
- #{self}.$each._p = function(obj) {
302
- result.push(obj);
313
+ #{self}.$each._p = function() {
314
+ if (arguments.length == 1) {
315
+ result.push(arguments[0]);
316
+ }
317
+ else {
318
+ result.push($slice.call(arguments));
319
+ }
303
320
  };
304
321
 
305
322
  #{self}.$each();
@@ -314,15 +331,17 @@ module Enumerable
314
331
  %x{
315
332
  var result = [];
316
333
 
317
- #{self}.$each._p = function(obj) {
334
+ #{self}.$each._p = function() {
318
335
  var value;
336
+ var param = arguments.length == 1 ?
337
+ arguments[0] : $slice.call(arguments);
319
338
 
320
- if ((value = block(obj)) === __breaker) {
321
- return __breaker.$v;
339
+ if ((value = block(param)) === $breaker) {
340
+ return $breaker.$v;
322
341
  }
323
342
 
324
343
  if (value !== false && value !== nil) {
325
- result.push(obj);
344
+ result.push(param);
326
345
  }
327
346
  };
328
347
 
@@ -337,28 +356,35 @@ module Enumerable
337
356
  var proc, result = nil, index = 0;
338
357
 
339
358
  if (object != null) {
340
- proc = function (obj) {
341
- if (#{ `obj` == `object` }) {
359
+ proc = function() {
360
+ var param = arguments.length == 1 ?
361
+ arguments[0] : $slice.call(arguments);
362
+
363
+ if (#{ `param` == `object` }) {
342
364
  result = index;
343
- return __breaker;
365
+ return $breaker;
344
366
  }
367
+
345
368
  index += 1;
346
369
  };
347
370
  }
348
371
  else {
349
- proc = function(obj) {
372
+ proc = function() {
350
373
  var value;
374
+ var param = arguments.length == 1 ?
375
+ arguments[0] : $slice.call(arguments);
351
376
 
352
- if ((value = block(obj)) === __breaker) {
353
- return __breaker.$v;
377
+ if ((value = block(param)) === $breaker) {
378
+ return $breaker.$v;
354
379
  }
355
380
 
356
381
  if (value !== false && value !== nil) {
357
- result = index;
358
- __breaker.$v = index;
382
+ result = index;
383
+ $breaker.$v = index;
359
384
 
360
- return __breaker;
385
+ return $breaker;
361
386
  }
387
+
362
388
  index += 1;
363
389
  };
364
390
  }
@@ -372,25 +398,32 @@ module Enumerable
372
398
 
373
399
  def first(number = undefined)
374
400
  %x{
375
- var result = [],
401
+ var result = [],
376
402
  current = 0,
377
403
  proc;
378
404
 
379
405
  if (number == null) {
380
406
  result = nil;
381
- proc = function(obj) {
382
- result = obj; return __breaker;
383
- };
384
- } else {
385
- proc = function(obj) {
386
- if (number <= current) {
387
- return __breaker;
388
- }
407
+ proc = function() {
408
+ result = arguments.length == 1 ?
409
+ arguments[0] : $slice.call(arguments);
389
410
 
390
- result.push(obj);
411
+ return $breaker;
412
+ };
413
+ }
414
+ else {
415
+ proc = function() {
416
+ if (number <= current) {
417
+ return $breaker;
418
+ }
419
+
420
+ var param = arguments.length == 1 ?
421
+ arguments[0] : $slice.call(arguments);
391
422
 
392
- current++;
393
- };
423
+ result.push(param);
424
+
425
+ current++;
426
+ };
394
427
  }
395
428
 
396
429
  #{self}.$each._p = proc;
@@ -402,28 +435,39 @@ module Enumerable
402
435
 
403
436
  def grep(pattern, &block)
404
437
  %x{
405
- var result = [];
438
+ var result = [],
439
+ proc;
406
440
 
407
- #{self}.$each._p = (block !== nil
408
- ? function(obj) {
409
- var value = #{pattern === `obj`};
441
+ if (block !== nil) {
442
+ proc = function() {
443
+ var param = arguments.length == 1 ?
444
+ arguments[0] : $slice.call(arguments);
410
445
 
411
- if (value !== false && value !== nil) {
412
- if ((value = block(obj)) === __breaker) {
413
- return __breaker.$v;
414
- }
446
+ var value = #{pattern === `param`};
415
447
 
416
- result.push(value);
448
+ if (value !== false && value !== nil) {
449
+ if ((value = block(param)) === $breaker) {
450
+ return $breaker.$v;
417
451
  }
452
+
453
+ result.push(value);
418
454
  }
419
- : function(obj) {
420
- var value = #{pattern === `obj`};
455
+ };
456
+ }
457
+ else {
458
+ proc = function() {
459
+ var param = arguments.length == 1 ?
460
+ arguments[0] : $slice.call(arguments);
421
461
 
422
- if (value !== false && value !== nil) {
423
- result.push(obj);
424
- }
425
- });
462
+ var value = #{pattern === `param`};
426
463
 
464
+ if (value !== false && value !== nil) {
465
+ result.push(param);
466
+ }
467
+ };
468
+ }
469
+
470
+ #{self}.$each._p = proc;
427
471
  #{self}.$each();
428
472
 
429
473
  return result;
@@ -446,32 +490,44 @@ module Enumerable
446
490
  %x{
447
491
  var proc, result;
448
492
  var arg_error = false;
493
+
449
494
  if (block !== nil) {
450
- proc = function(obj) {
495
+ proc = function() {
496
+ var param = arguments.length == 1 ?
497
+ arguments[0] : $slice.call(arguments);
498
+
451
499
  if (result == undefined) {
452
- result = obj;
500
+ result = param;
453
501
  }
454
- else if ((value = block(obj, result)) === __breaker) {
455
- result = __breaker.$v;
456
- return __breaker;
502
+ else if ((value = block(param, result)) === $breaker) {
503
+ result = $breaker.$v;
504
+
505
+ return $breaker;
457
506
  }
458
507
  else {
459
508
  if (value > 0) {
460
- result = obj;
509
+ result = param;
461
510
  }
462
- __breaker.$v = nil;
511
+
512
+ $breaker.$v = nil;
463
513
  }
464
514
  }
465
515
  }
466
516
  else {
467
- proc = function(obj) {
468
- var modules = obj.$class().$included_modules;
517
+ proc = function() {
518
+ var param = arguments.length == 1 ?
519
+ arguments[0] : $slice.call(arguments);
520
+
521
+ var modules = param.$class().$included_modules;
522
+
469
523
  if (modules == undefined || modules.length == 0 || modules.indexOf(Opal.Comparable) == -1) {
470
524
  arg_error = true;
471
- return __breaker;
525
+
526
+ return $breaker;
472
527
  }
473
- if (result == undefined || obj > result) {
474
- result = obj;
528
+
529
+ if (result == undefined || #{`param` > `result`}) {
530
+ result = param;
475
531
  }
476
532
  }
477
533
  }
@@ -489,34 +545,47 @@ module Enumerable
489
545
 
490
546
  def min(&block)
491
547
  %x{
492
- var proc, result;
493
- var arg_error = false;
548
+ var proc,
549
+ result,
550
+ arg_error = false;
551
+
494
552
  if (block !== nil) {
495
- proc = function(obj) {
553
+ proc = function() {
554
+ var param = arguments.length == 1 ?
555
+ arguments[0] : $slice.call(arguments);
556
+
496
557
  if (result == undefined) {
497
- result = obj;
558
+ result = param;
498
559
  }
499
- else if ((value = block(obj, result)) === __breaker) {
500
- result = __breaker.$v;
501
- return __breaker;
560
+ else if ((value = block(param, result)) === $breaker) {
561
+ result = $breaker.$v;
562
+
563
+ return $breaker;
502
564
  }
503
565
  else {
504
566
  if (value < 0) {
505
- result = obj;
567
+ result = param;
506
568
  }
507
- __breaker.$v = nil;
569
+
570
+ $breaker.$v = nil;
508
571
  }
509
572
  }
510
573
  }
511
574
  else {
512
575
  proc = function(obj) {
513
- var modules = obj.$class().$included_modules;
576
+ var param = arguments.length == 1 ?
577
+ arguments[0] : $slice.call(arguments);
578
+
579
+ var modules = param.$class().$included_modules;
580
+
514
581
  if (modules == undefined || modules.length == 0 || modules.indexOf(Opal.Comparable) == -1) {
515
582
  arg_error = true;
516
- return __breaker;
583
+
584
+ return $breaker;
517
585
  }
518
- if (result == undefined || obj < result) {
519
- result = obj;
586
+
587
+ if (result == undefined || #{`param` < `result`}) {
588
+ result = param;
520
589
  }
521
590
  }
522
591
  }
@@ -528,49 +597,47 @@ module Enumerable
528
597
  #{raise ArgumentError, "Array#min"};
529
598
  }
530
599
 
531
- return (result == undefined ? nil : result);
600
+ return result == undefined ? nil : result;
532
601
  }
533
602
  end
534
603
 
535
604
  def none?(&block)
536
605
  %x{
537
- var result = true, proc;
606
+ var result = true,
607
+ proc;
538
608
 
539
609
  if (block !== nil) {
540
610
  proc = function(obj) {
541
- var value;
542
- var args = [];
543
- for(var i = 0; i < arguments.length; i ++) {
544
- args[i] = arguments[i];
545
- }
611
+ var value,
612
+ args = $slice.call(arguments);
546
613
 
547
- if ((value = block.apply(#{self}, args)) === __breaker) {
548
- return __breaker.$v;
614
+ if ((value = block.apply(#{self}, args)) === $breaker) {
615
+ return $breaker.$v;
549
616
  }
550
617
 
551
618
  if (value !== false && value !== nil) {
552
- result = false;
553
- __breaker.$v = nil;
619
+ result = false;
620
+ $breaker.$v = nil;
554
621
 
555
- return __breaker;
622
+ return $breaker;
556
623
  }
557
624
  }
558
625
  }
559
626
  else {
560
627
  proc = function(obj) {
561
628
  if (arguments.length == 1 && (obj !== false && obj !== nil)) {
562
- result = false;
563
- __breaker.$v = nil;
629
+ result = false;
630
+ $breaker.$v = nil;
564
631
 
565
- return __breaker;
632
+ return $breaker;
566
633
  }
567
634
  else {
568
635
  for (var i = 0, length = arguments.length; i < length; i++) {
569
636
  if (arguments[i] !== false && arguments[i] !== nil) {
570
- result = false;
571
- __breaker.$v = nil;
637
+ result = false;
638
+ $breaker.$v = nil;
572
639
 
573
- return __breaker;
640
+ return $breaker;
574
641
  }
575
642
  }
576
643
  }
@@ -584,7 +651,7 @@ module Enumerable
584
651
  }
585
652
  end
586
653
 
587
- def sort_by &block
654
+ def sort_by(&block)
588
655
  map { |*f|
589
656
  # FIXME: this should probably belongs to somewhere more
590
657
  f = `#{f}.length === 1 ? #{f}[0] : #{f}`