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
@@ -4,14 +4,14 @@ class Enumerator
4
4
  def initialize(obj, method = :each, *args)
5
5
  @object = obj
6
6
  @method = method
7
- @args = args
7
+ @args = args
8
8
  end
9
9
 
10
10
  def each(&block)
11
11
  return enum_for :each unless block_given?
12
12
 
13
- @object.__send__(@method, *@args) do |e|
14
- block.call e
13
+ @object.__send__(@method, *@args) do |*e|
14
+ block.call(*e)
15
15
  end
16
16
  end
17
17
 
@@ -43,6 +43,7 @@ class StandardError < Exception; end
43
43
  class StopIteration < IndexError; end
44
44
  class KeyError < IndexError; end
45
45
  class RangeError < StandardError; end
46
+ class IOError < StandardError; end
46
47
 
47
48
  class ScriptError < Exception; end
48
49
  class SyntaxError < ScriptError; end
@@ -2,9 +2,13 @@ class Hash
2
2
  include Enumerable
3
3
 
4
4
  %x{
5
- var __hash = Opal.hash = function() {
5
+ var $hash = Opal.hash = function() {
6
+ if (arguments.length == 1 && arguments[0]._klass == Hash) {
7
+ return arguments[0];
8
+ }
9
+
6
10
  var hash = new Hash._alloc,
7
- args = __slice.call(arguments),
11
+ args = $slice.call(arguments),
8
12
  keys = [],
9
13
  assocs = {};
10
14
 
@@ -30,7 +34,7 @@ class Hash
30
34
  # compile time, so they are just added here by the constructor
31
35
  # function
32
36
  %x{
33
- var __hash2 = Opal.hash2 = function(keys, map) {
37
+ var $hash2 = Opal.hash2 = function(keys, map) {
34
38
  var hash = new Hash._alloc;
35
39
  hash.keys = keys;
36
40
  hash.map = map;
@@ -38,23 +42,26 @@ class Hash
38
42
  };
39
43
  }
40
44
 
41
- `var __hasOwn = {}.hasOwnProperty`
45
+ `var $hasOwn = {}.hasOwnProperty`
42
46
 
43
47
  def self.[](*objs)
44
- `__hash.apply(null, objs)`
48
+ `$hash.apply(null, objs)`
45
49
  end
46
50
 
47
51
  def self.allocate
48
- `__hash()`
52
+ %x{
53
+ var hash = new #{self}._alloc;
54
+ hash.map = {};
55
+ hash.keys = [];
56
+ return hash;
57
+ }
49
58
  end
50
59
 
51
- def self.new(defaults = undefined, &block)
60
+ def initialize(defaults = undefined, &block)
52
61
  %x{
53
- var hash = __hash();
54
-
55
62
  if (defaults != null) {
56
63
  if (defaults.constructor == Object) {
57
- var map = hash.map, keys = hash.keys;
64
+ var map = #{self}.map, keys = #{self}.keys;
58
65
 
59
66
  for (var key in defaults) {
60
67
  keys.push(key);
@@ -62,14 +69,14 @@ class Hash
62
69
  }
63
70
  }
64
71
  else {
65
- hash.none = defaults;
72
+ #{self}.none = defaults;
66
73
  }
67
74
  }
68
75
  else if (block !== nil) {
69
- hash.proc = block;
76
+ #{self}.proc = block;
70
77
  }
71
78
 
72
- return hash;
79
+ return #{self};
73
80
  }
74
81
  end
75
82
 
@@ -124,7 +131,7 @@ class Hash
124
131
  %x{
125
132
  var map = #{self}.map;
126
133
 
127
- if (!__hasOwn.call(map, key)) {
134
+ if (!$hasOwn.call(map, key)) {
128
135
  #{self}.keys.push(key);
129
136
  }
130
137
 
@@ -160,7 +167,7 @@ class Hash
160
167
 
161
168
  def clone
162
169
  %x{
163
- var result = __hash(),
170
+ var result = $hash(),
164
171
  map = #{self}.map,
165
172
  map2 = result.map,
166
173
  keys2 = result.keys;
@@ -206,14 +213,16 @@ class Hash
206
213
  end
207
214
 
208
215
  def delete_if(&block)
216
+ return enum_for :delete_if unless block
217
+
209
218
  %x{
210
219
  var map = #{self}.map, keys = #{self}.keys, value;
211
220
 
212
221
  for (var i = 0, length = keys.length; i < length; i++) {
213
222
  var key = keys[i], obj = map[key];
214
223
 
215
- if ((value = block(key, obj)) === __breaker) {
216
- return __breaker.$v;
224
+ if ((value = block(key, obj)) === $breaker) {
225
+ return $breaker.$v;
217
226
  }
218
227
 
219
228
  if (value !== false && value !== nil) {
@@ -232,15 +241,15 @@ class Hash
232
241
  alias dup clone
233
242
 
234
243
  def each(&block)
244
+ return enum_for :each unless block
245
+
235
246
  %x{
236
247
  var map = #{self}.map, keys = #{self}.keys;
237
248
 
238
249
  for (var i = 0, length = keys.length; i < length; i++) {
239
250
  var key = keys[i];
240
251
 
241
- if (block(key, map[key]) === __breaker) {
242
- return __breaker.$v;
243
- }
252
+ #{yield [`key`, `map[key]`]};
244
253
  }
245
254
 
246
255
  return #{self};
@@ -248,14 +257,16 @@ class Hash
248
257
  end
249
258
 
250
259
  def each_key(&block)
260
+ return enum_for :each_key unless block
261
+
251
262
  %x{
252
263
  var keys = #{self}.keys;
253
264
 
254
265
  for (var i = 0, length = keys.length; i < length; i++) {
255
266
  var key = keys[i];
256
267
 
257
- if (block(key) === __breaker) {
258
- return __breaker.$v;
268
+ if (block(key) === $breaker) {
269
+ return $breaker.$v;
259
270
  }
260
271
  }
261
272
 
@@ -266,12 +277,14 @@ class Hash
266
277
  alias each_pair each
267
278
 
268
279
  def each_value(&block)
280
+ return enum_for :each_value unless block
281
+
269
282
  %x{
270
283
  var map = #{self}.map, keys = #{self}.keys;
271
284
 
272
285
  for (var i = 0, length = keys.length; i < length; i++) {
273
- if (block(map[keys[i]]) === __breaker) {
274
- return __breaker.$v;
286
+ if (block(map[keys[i]]) === $breaker) {
287
+ return $breaker.$v;
275
288
  }
276
289
  }
277
290
 
@@ -280,9 +293,7 @@ class Hash
280
293
  end
281
294
 
282
295
  def empty?
283
- %x{
284
- return #{self}.keys.length === 0;
285
- }
296
+ `#{self}.keys.length === 0`
286
297
  end
287
298
 
288
299
  alias eql? ==
@@ -298,8 +309,8 @@ class Hash
298
309
  if (block !== nil) {
299
310
  var value;
300
311
 
301
- if ((value = block(key)) === __breaker) {
302
- return __breaker.$v;
312
+ if ((value = block(key)) === $breaker) {
313
+ return $breaker.$v;
303
314
  }
304
315
 
305
316
  return value;
@@ -403,8 +414,13 @@ class Hash
403
414
  var inspect = [], keys = #{self}.keys, map = #{self}.map;
404
415
 
405
416
  for (var i = 0, length = keys.length; i < length; i++) {
406
- var key = keys[i];
407
- inspect.push(#{`key`.inspect} + '=>' + #{`map[key]`.inspect});
417
+ var key = keys[i], val = map[key];
418
+
419
+ if (val === #{self}) {
420
+ inspect.push(#{`key`.inspect} + '=>' + '{...}');
421
+ } else {
422
+ inspect.push(#{`key`.inspect} + '=>' + #{`map[key]`.inspect});
423
+ }
408
424
  }
409
425
 
410
426
  return '{' + inspect.join(', ') + '}';
@@ -413,7 +429,7 @@ class Hash
413
429
 
414
430
  def invert
415
431
  %x{
416
- var result = __hash(), keys = #{self}.keys, map = #{self}.map,
432
+ var result = $hash(), keys = #{self}.keys, map = #{self}.map,
417
433
  keys2 = result.keys, map2 = result.map;
418
434
 
419
435
  for (var i = 0, length = keys.length; i < length; i++) {
@@ -428,14 +444,16 @@ class Hash
428
444
  end
429
445
 
430
446
  def keep_if(&block)
447
+ return enum_for :keep_if unless block
448
+
431
449
  %x{
432
450
  var map = #{self}.map, keys = #{self}.keys, value;
433
451
 
434
452
  for (var i = 0, length = keys.length; i < length; i++) {
435
453
  var key = keys[i], obj = map[key];
436
454
 
437
- if ((value = block(key, obj)) === __breaker) {
438
- return __breaker.$v;
455
+ if ((value = block(key, obj)) === $breaker) {
456
+ return $breaker.$v;
439
457
  }
440
458
 
441
459
  if (value === false || value === nil) {
@@ -472,7 +490,7 @@ class Hash
472
490
  def merge(other, &block)
473
491
  %x{
474
492
  var keys = #{self}.keys, map = #{self}.map,
475
- result = __hash(), keys2 = result.keys, map2 = result.map;
493
+ result = $hash(), keys2 = result.keys, map2 = result.map;
476
494
 
477
495
  for (var i = 0, length = keys.length; i < length; i++) {
478
496
  var key = keys[i];
@@ -563,15 +581,17 @@ class Hash
563
581
  end
564
582
 
565
583
  def reject(&block)
584
+ return enum_for :reject unless block
585
+
566
586
  %x{
567
587
  var keys = #{self}.keys, map = #{self}.map,
568
- result = __hash(), map2 = result.map, keys2 = result.keys;
588
+ result = $hash(), map2 = result.map, keys2 = result.keys;
569
589
 
570
590
  for (var i = 0, length = keys.length; i < length; i++) {
571
591
  var key = keys[i], obj = map[key], value;
572
592
 
573
- if ((value = block(key, obj)) === __breaker) {
574
- return __breaker.$v;
593
+ if ((value = block(key, obj)) === $breaker) {
594
+ return $breaker.$v;
575
595
  }
576
596
 
577
597
  if (value === false || value === nil) {
@@ -599,15 +619,17 @@ class Hash
599
619
  end
600
620
 
601
621
  def select(&block)
622
+ return enum_for :select unless block
623
+
602
624
  %x{
603
625
  var keys = #{self}.keys, map = #{self}.map,
604
- result = __hash(), map2 = result.map, keys2 = result.keys;
626
+ result = $hash(), map2 = result.map, keys2 = result.keys;
605
627
 
606
628
  for (var i = 0, length = keys.length; i < length; i++) {
607
629
  var key = keys[i], obj = map[key], value;
608
630
 
609
- if ((value = block(key, obj)) === __breaker) {
610
- return __breaker.$v;
631
+ if ((value = block(key, obj)) === $breaker) {
632
+ return $breaker.$v;
611
633
  }
612
634
 
613
635
  if (value !== false && value !== nil) {
@@ -621,14 +643,16 @@ class Hash
621
643
  end
622
644
 
623
645
  def select!(&block)
646
+ return enum_for :select! unless block
647
+
624
648
  %x{
625
649
  var map = #{self}.map, keys = #{self}.keys, value, result = nil;
626
650
 
627
651
  for (var i = 0, length = keys.length; i < length; i++) {
628
652
  var key = keys[i], obj = map[key];
629
653
 
630
- if ((value = block(key, obj)) === __breaker) {
631
- return __breaker.$v;
654
+ if ((value = block(key, obj)) === $breaker) {
655
+ return $breaker.$v;
632
656
  }
633
657
 
634
658
  if (value === false || value === nil) {
@@ -681,19 +705,6 @@ class Hash
681
705
  self
682
706
  end
683
707
 
684
- def to_json
685
- %x{
686
- var inspect = [], keys = #{self}.keys, map = #{self}.map;
687
-
688
- for (var i = 0, length = keys.length; i < length; i++) {
689
- var key = keys[i];
690
- inspect.push(#{`key`.to_json} + ': ' + #{`map[key]`.to_json});
691
- }
692
-
693
- return '{' + inspect.join(', ') + '}';
694
- }
695
- end
696
-
697
708
  def to_n
698
709
  %x{
699
710
  var result = {}, keys = #{self}.keys, map = #{self}.map, bucket, value;
data/corelib/io.rb ADDED
@@ -0,0 +1,39 @@
1
+ class IO
2
+ SEEK_SET = 0
3
+ SEEK_CUR = 1
4
+ SEEK_END = 2
5
+
6
+ module Writable
7
+ def <<(string)
8
+ write(string)
9
+
10
+ self
11
+ end
12
+
13
+ def print(*args)
14
+ write args.map { |arg| String(arg) }.join($,)
15
+ end
16
+
17
+ def puts(*args)
18
+ write args.map { |arg| String(arg) }.join($/)
19
+ end
20
+ end
21
+
22
+ module Readable
23
+ def readbyte
24
+ getbyte
25
+ end
26
+
27
+ def readchar
28
+ getc
29
+ end
30
+
31
+ def readline(sep = $/)
32
+ raise NotImplementedError
33
+ end
34
+
35
+ def readpartial(integer, outbuf = nil)
36
+ raise NotImplementedError
37
+ end
38
+ end
39
+ end
@@ -20,16 +20,12 @@ module Kernel
20
20
  `#{self} == other`
21
21
  end
22
22
 
23
- def as_json
24
- nil
25
- end
26
-
27
23
  def method(name)
28
24
  %x{
29
25
  var recv = #{self},
30
26
  meth = recv['$' + name],
31
27
  func = function() {
32
- return meth.apply(recv, __slice.call(arguments, 0));
28
+ return meth.apply(recv, $slice.call(arguments, 0));
33
29
  };
34
30
 
35
31
  if (!meth) {
@@ -58,19 +54,29 @@ module Kernel
58
54
  }
59
55
  end
60
56
 
61
- def Array(object)
57
+ def Array(object, func = undefined, length = :length)
62
58
  %x{
63
- if (object === nil) {
59
+ if (object == null || object === nil) {
64
60
  return [];
65
61
  }
66
- else if (object.$to_ary) {
62
+ else if (#{native?(object)} && object[length] != null) {
63
+ var result = [];
64
+
65
+ for (var i = 0, length = object[length]; i < length; i++) {
66
+ result.push(func ? object[func](i) : object[i]);
67
+ }
68
+
69
+ return result;
70
+ }
71
+ else if (#{object.respond_to? :to_ary}) {
67
72
  return #{object.to_ary};
68
73
  }
69
- else if (object.$to_a) {
74
+ else if (#{object.respond_to? :to_a}) {
70
75
  return #{object.to_a};
71
76
  }
72
-
73
- return [object];
77
+ else {
78
+ return [object];
79
+ }
74
80
  }
75
81
  end
76
82
 
@@ -81,7 +87,7 @@ module Kernel
81
87
  def define_singleton_method(name, &body)
82
88
  %x{
83
89
  if (body === nil) {
84
- no_block_given();
90
+ throw new Error("no block given");
85
91
  }
86
92
 
87
93
  var jsid = '$' + name;
@@ -259,7 +265,7 @@ module Kernel
259
265
  end
260
266
 
261
267
  def instance_variable_defined?(name)
262
- `__hasOwn.call(#{self}, name.substr(1))`
268
+ `#{self}.hasOwnProperty(name.substr(1))`
263
269
  end
264
270
 
265
271
  def instance_variable_get(name)
@@ -288,6 +294,10 @@ module Kernel
288
294
  }
289
295
  end
290
296
 
297
+ def Integer(str)
298
+ `parseInt(str)`
299
+ end
300
+
291
301
  def is_a?(klass)
292
302
  %x{
293
303
  var search = #{self}._klass;
@@ -345,20 +355,12 @@ module Kernel
345
355
  end
346
356
 
347
357
  def puts(*strs)
348
- %x{
349
- for (var i = 0; i < strs.length; i++) {
350
- if(strs[i] instanceof Array) {
351
- #{ puts *`strs[i]` }
352
- } else {
353
- __opal.puts(#{ `strs[i]`.to_s });
354
- }
355
- }
356
- }
357
- nil
358
+ $stdout.puts(*strs)
358
359
  end
359
360
 
360
361
  def p(*args)
361
- `console.log.apply(console, args);`
362
+ args.each { |obj| $stdout.puts obj.inspect }
363
+
362
364
  args.length <= 1 ? args[0] : args
363
365
  end
364
366
 
@@ -393,7 +395,10 @@ module Kernel
393
395
  end
394
396
 
395
397
  def respond_to?(name)
396
- `!!#{self}['$' + name]`
398
+ %x{
399
+ var body = #{self}['$' + name];
400
+ return (!!body) && !body.rb_stub;
401
+ }
397
402
  end
398
403
 
399
404
  alias send __send__
@@ -406,14 +411,16 @@ module Kernel
406
411
  return #{self}._singleton;
407
412
  }
408
413
 
409
- var meta = new __opal.Class._alloc;
410
- meta._klass = __opal.Class;
414
+ var meta = new $opal.Class._alloc;
415
+ meta._klass = $opal.Class;
411
416
  #{self}._singleton = meta;
412
417
  // FIXME - is this right? (probably - methods defined on
413
418
  // class' singleton should also go to subclasses?)
414
419
  meta._proto = #{self}.constructor.prototype;
415
420
  meta._isSingleton = true;
416
421
 
422
+ meta._scope = #{self}._scope;
423
+
417
424
  return meta;
418
425
  }
419
426
 
@@ -436,6 +443,7 @@ module Kernel
436
443
  meta._proto = #{self};
437
444
  #{self}._singleton = meta;
438
445
  meta._klass = orig_class._klass;
446
+ meta._scope = orig_class._scope;
439
447
 
440
448
  return meta;
441
449
  }
@@ -453,10 +461,6 @@ module Kernel
453
461
  self
454
462
  end
455
463
 
456
- def to_json
457
- to_s.to_json
458
- end
459
-
460
464
  def to_proc
461
465
  self
462
466
  end