opal 0.8.1 → 0.9.0.beta1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (331) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -2
  3. data/.gitmodules +3 -3
  4. data/.jshintrc +17 -20
  5. data/.travis.yml +22 -11
  6. data/CHANGELOG.md +51 -1
  7. data/CODE_OF_CONDUCT.md +15 -0
  8. data/CONTRIBUTING.md +125 -9
  9. data/Gemfile +1 -1
  10. data/Guardfile +2 -2
  11. data/README.md +95 -29
  12. data/Rakefile +1 -1
  13. data/benchmark/benchmarks +103 -0
  14. data/benchmark/bm_array_flatten.rb +9 -0
  15. data/benchmark/bm_array_intersection_numbers.rb +7 -0
  16. data/benchmark/bm_array_intersection_objects.rb +7 -0
  17. data/benchmark/bm_array_intersection_strings.rb +7 -0
  18. data/benchmark/bm_array_join_ary.rb +9 -0
  19. data/benchmark/bm_array_minus_numbers.rb +7 -0
  20. data/benchmark/bm_array_minus_objects.rb +7 -0
  21. data/benchmark/bm_array_minus_strings.rb +7 -0
  22. data/benchmark/bm_array_union_numbers.rb +7 -0
  23. data/benchmark/bm_array_union_objects.rb +7 -0
  24. data/benchmark/bm_array_union_strings.rb +7 -0
  25. data/benchmark/bm_array_uniq_bang_numbers.rb +5 -0
  26. data/benchmark/bm_array_uniq_bang_objects.rb +5 -0
  27. data/benchmark/bm_array_uniq_bang_strings.rb +5 -0
  28. data/benchmark/bm_array_uniq_numbers.rb +5 -0
  29. data/benchmark/bm_array_uniq_objects.rb +5 -0
  30. data/benchmark/bm_array_uniq_strings.rb +5 -0
  31. data/benchmark/bm_dispatch_bind_table.rb +57 -0
  32. data/benchmark/bm_dispatch_code_gen.rb +65 -0
  33. data/benchmark/bm_dispatch_code_gen_if.rb +64 -0
  34. data/benchmark/bm_dispatch_hardcoded.rb +44 -0
  35. data/benchmark/bm_dispatch_send.rb +38 -0
  36. data/benchmark/bm_dispatch_send_table.rb +57 -0
  37. data/benchmark/bm_hash_assoc_object.rb +11 -0
  38. data/benchmark/bm_hash_assoc_string.rb +9 -0
  39. data/benchmark/bm_hash_clone_object.rb +9 -0
  40. data/benchmark/bm_hash_clone_string.rb +9 -0
  41. data/benchmark/bm_hash_delete_object.rb +11 -0
  42. data/benchmark/bm_hash_delete_string.rb +9 -0
  43. data/benchmark/bm_hash_each_key_object.rb +9 -0
  44. data/benchmark/bm_hash_each_key_string.rb +9 -0
  45. data/benchmark/bm_hash_each_object.rb +9 -0
  46. data/benchmark/bm_hash_each_string.rb +9 -0
  47. data/benchmark/bm_hash_each_value_object.rb +9 -0
  48. data/benchmark/bm_hash_each_value_string.rb +9 -0
  49. data/benchmark/bm_hash_element_reference_object.rb +11 -0
  50. data/benchmark/bm_hash_element_reference_string.rb +9 -0
  51. data/benchmark/bm_hash_element_set_object.rb +5 -0
  52. data/benchmark/bm_hash_element_set_string.rb +5 -0
  53. data/benchmark/bm_hash_equal_value_object.rb +14 -0
  54. data/benchmark/bm_hash_equal_value_string.rb +11 -0
  55. data/benchmark/bm_hash_fetch_object.rb +11 -0
  56. data/benchmark/bm_hash_fetch_string.rb +9 -0
  57. data/benchmark/bm_hash_flatten_object.rb +9 -0
  58. data/benchmark/bm_hash_flatten_string.rb +9 -0
  59. data/benchmark/bm_hash_has_key_object.rb +11 -0
  60. data/benchmark/bm_hash_has_key_string.rb +9 -0
  61. data/benchmark/bm_hash_has_value_object.rb +9 -0
  62. data/benchmark/bm_hash_has_value_string.rb +9 -0
  63. data/benchmark/bm_hash_hash_object.rb +9 -0
  64. data/benchmark/bm_hash_hash_string.rb +9 -0
  65. data/benchmark/bm_hash_inspect_object.rb +9 -0
  66. data/benchmark/bm_hash_inspect_string.rb +9 -0
  67. data/benchmark/bm_hash_invert_object.rb +9 -0
  68. data/benchmark/bm_hash_invert_string.rb +9 -0
  69. data/benchmark/bm_hash_keep_if_object.rb +9 -0
  70. data/benchmark/bm_hash_keep_if_string.rb +9 -0
  71. data/benchmark/bm_hash_key_object.rb +9 -0
  72. data/benchmark/bm_hash_key_string.rb +9 -0
  73. data/benchmark/bm_hash_keys_object.rb +9 -0
  74. data/benchmark/bm_hash_keys_string.rb +9 -0
  75. data/benchmark/bm_hash_literal_mixed_large.rb +3 -0
  76. data/benchmark/bm_hash_literal_mixed_small.rb +3 -0
  77. data/benchmark/bm_hash_literal_object_large.rb +4 -0
  78. data/benchmark/bm_hash_literal_object_small.rb +3 -0
  79. data/benchmark/bm_hash_literal_string_large.rb +4 -0
  80. data/benchmark/bm_hash_literal_string_small.rb +3 -0
  81. data/benchmark/bm_hash_merge_object.rb +22 -0
  82. data/benchmark/bm_hash_merge_string.rb +18 -0
  83. data/benchmark/bm_hash_rassoc_object.rb +9 -0
  84. data/benchmark/bm_hash_rassoc_string.rb +9 -0
  85. data/benchmark/bm_hash_rehash_object.rb +9 -0
  86. data/benchmark/bm_hash_rehash_string.rb +9 -0
  87. data/benchmark/bm_hash_reject_bang_object.rb +9 -0
  88. data/benchmark/bm_hash_reject_bang_string.rb +9 -0
  89. data/benchmark/bm_hash_reject_object.rb +9 -0
  90. data/benchmark/bm_hash_reject_string.rb +9 -0
  91. data/benchmark/bm_hash_replace_object.rb +18 -0
  92. data/benchmark/bm_hash_replace_string.rb +14 -0
  93. data/benchmark/bm_hash_select_bang_object.rb +9 -0
  94. data/benchmark/bm_hash_select_bang_string.rb +9 -0
  95. data/benchmark/bm_hash_select_object.rb +9 -0
  96. data/benchmark/bm_hash_select_string.rb +9 -0
  97. data/benchmark/bm_hash_shift_object.rb +10 -0
  98. data/benchmark/bm_hash_shift_string.rb +10 -0
  99. data/benchmark/bm_hash_to_a_object.rb +9 -0
  100. data/benchmark/bm_hash_to_a_string.rb +9 -0
  101. data/benchmark/bm_hash_to_h_object.rb +10 -0
  102. data/benchmark/bm_hash_to_h_string.rb +10 -0
  103. data/benchmark/bm_hash_values_object.rb +9 -0
  104. data/benchmark/bm_hash_values_string.rb +9 -0
  105. data/benchmark/run.rb +48 -0
  106. data/bin/opal-mspec +1 -1
  107. data/bin/opal-repl +4 -4
  108. data/docs/compiled_ruby.md +214 -56
  109. data/docs/configuring_gems.md +2 -2
  110. data/docs/faq.md +2 -2
  111. data/docs/getting_started.md +19 -2
  112. data/docs/jquery.md +5 -5
  113. data/docs/opal_parser.md +53 -0
  114. data/docs/unsupported_features.md +2 -2
  115. data/docs/upgrading.md +22 -0
  116. data/docs/using_sprockets.md +15 -0
  117. data/examples/rack/config.ru +13 -0
  118. data/examples/sinatra/config.ru +4 -5
  119. data/lib/mspec/opal/runner.rb +54 -11
  120. data/lib/opal.rb +1 -1
  121. data/lib/opal/builder.rb +1 -1
  122. data/lib/opal/builder_processors.rb +1 -1
  123. data/lib/opal/cli.rb +17 -13
  124. data/lib/opal/cli_options.rb +1 -1
  125. data/lib/opal/compiler.rb +12 -0
  126. data/lib/opal/config.rb +4 -0
  127. data/lib/opal/nodes/arglist.rb +5 -7
  128. data/lib/opal/nodes/call.rb +6 -1
  129. data/lib/opal/nodes/call_special.rb +74 -0
  130. data/lib/opal/nodes/def.rb +35 -28
  131. data/lib/opal/nodes/definitions.rb +3 -5
  132. data/lib/opal/nodes/for.rb +13 -0
  133. data/lib/opal/nodes/helpers.rb +15 -1
  134. data/lib/opal/nodes/if.rb +5 -5
  135. data/lib/opal/nodes/iter.rb +6 -1
  136. data/lib/opal/nodes/literal.rb +1 -1
  137. data/lib/opal/nodes/logic.rb +2 -2
  138. data/lib/opal/nodes/masgn.rb +1 -2
  139. data/lib/opal/nodes/module.rb +2 -1
  140. data/lib/opal/nodes/rescue.rb +10 -1
  141. data/lib/opal/nodes/scope.rb +8 -2
  142. data/lib/opal/nodes/singleton_class.rb +1 -1
  143. data/lib/opal/nodes/top.rb +11 -0
  144. data/lib/opal/nodes/variables.rb +4 -4
  145. data/lib/opal/parser.rb +21 -3
  146. data/lib/opal/parser/grammar.rb +3115 -2961
  147. data/lib/opal/parser/grammar.y +29 -6
  148. data/lib/opal/parser/lexer.rb +18 -8
  149. data/lib/opal/sprockets.rb +85 -0
  150. data/lib/opal/sprockets/processor.rb +11 -35
  151. data/lib/opal/sprockets/server.rb +3 -15
  152. data/lib/opal/version.rb +2 -2
  153. data/opal.gemspec +4 -4
  154. data/opal/README.md +9 -0
  155. data/opal/corelib/array.rb +433 -181
  156. data/opal/corelib/basic_object.rb +48 -4
  157. data/opal/corelib/boolean.rb +15 -6
  158. data/opal/corelib/class.rb +6 -5
  159. data/opal/corelib/comparable.rb +12 -0
  160. data/opal/corelib/complex.rb +282 -0
  161. data/opal/corelib/constants.rb +9 -0
  162. data/opal/corelib/enumerable.rb +83 -34
  163. data/opal/corelib/enumerator.rb +3 -1
  164. data/opal/corelib/error.rb +49 -10
  165. data/opal/corelib/file.rb +1 -0
  166. data/opal/corelib/hash.rb +353 -577
  167. data/opal/corelib/helpers.rb +20 -0
  168. data/opal/corelib/kernel.rb +114 -59
  169. data/opal/corelib/math.rb +470 -0
  170. data/opal/corelib/method.rb +11 -2
  171. data/opal/corelib/module.rb +96 -96
  172. data/opal/corelib/{nil_class.rb → nil.rb} +20 -1
  173. data/opal/corelib/number.rb +751 -0
  174. data/opal/corelib/numeric.rb +77 -437
  175. data/opal/corelib/proc.rb +81 -1
  176. data/opal/corelib/process.rb +27 -0
  177. data/opal/corelib/rational.rb +358 -0
  178. data/opal/corelib/regexp.rb +156 -27
  179. data/opal/corelib/runtime.js +724 -335
  180. data/opal/corelib/string.rb +93 -104
  181. data/opal/corelib/string/encoding.rb +177 -0
  182. data/opal/corelib/string/inheritance.rb +2 -0
  183. data/opal/corelib/struct.rb +105 -18
  184. data/opal/corelib/time.rb +267 -146
  185. data/opal/corelib/unsupported.rb +216 -0
  186. data/opal/corelib/variables.rb +0 -6
  187. data/opal/opal.rb +8 -22
  188. data/opal/opal/base.rb +9 -0
  189. data/opal/opal/mini.rb +17 -0
  190. data/spec/README.md +1 -1
  191. data/spec/filters/bugs/array.rb +38 -136
  192. data/spec/filters/bugs/{basic_object.rb → basicobject.rb} +14 -15
  193. data/spec/filters/bugs/class.rb +6 -12
  194. data/spec/filters/bugs/complex.rb +3 -0
  195. data/spec/filters/bugs/date.rb +162 -10
  196. data/spec/filters/bugs/enumerable.rb +31 -58
  197. data/spec/filters/bugs/enumerator.rb +42 -0
  198. data/spec/filters/bugs/exception.rb +66 -10
  199. data/spec/filters/bugs/float.rb +17 -0
  200. data/spec/filters/bugs/hash.rb +11 -97
  201. data/spec/filters/bugs/inheritance.rb +5 -0
  202. data/spec/filters/bugs/integer.rb +28 -0
  203. data/spec/filters/bugs/kernel.rb +304 -12
  204. data/spec/filters/bugs/language.rb +133 -399
  205. data/spec/filters/bugs/language_opal.rb +88 -0
  206. data/spec/filters/bugs/module.rb +203 -62
  207. data/spec/filters/bugs/numeric.rb +32 -0
  208. data/spec/filters/bugs/proc.rb +39 -0
  209. data/spec/filters/bugs/range.rb +148 -0
  210. data/spec/filters/bugs/regexp.rb +168 -0
  211. data/spec/filters/bugs/set.rb +46 -3
  212. data/spec/filters/bugs/singleton.rb +1 -2
  213. data/spec/filters/bugs/string.rb +59 -90
  214. data/spec/filters/bugs/strscan.rb +80 -0
  215. data/spec/filters/bugs/struct.rb +10 -20
  216. data/spec/filters/bugs/time.rb +17 -184
  217. data/spec/filters/bugs/unboundmethod.rb +22 -0
  218. data/spec/filters/unsupported/array.rb +163 -0
  219. data/spec/filters/unsupported/basicobject.rb +14 -0
  220. data/spec/filters/unsupported/bignum.rb +46 -0
  221. data/spec/filters/unsupported/class.rb +4 -0
  222. data/spec/filters/unsupported/delegator.rb +5 -0
  223. data/spec/filters/unsupported/enumerable.rb +11 -0
  224. data/spec/filters/unsupported/enumerator.rb +8 -9
  225. data/spec/filters/unsupported/fixnum.rb +14 -0
  226. data/spec/filters/unsupported/float.rb +41 -7
  227. data/spec/filters/unsupported/freeze.rb +45 -0
  228. data/spec/filters/unsupported/hash.rb +50 -0
  229. data/spec/filters/unsupported/integer.rb +3 -0
  230. data/spec/filters/unsupported/kernel.rb +31 -0
  231. data/spec/filters/unsupported/language.rb +17 -0
  232. data/spec/filters/unsupported/matchdata.rb +30 -0
  233. data/spec/filters/unsupported/math.rb +3 -0
  234. data/spec/filters/unsupported/module.rb +5 -3
  235. data/spec/filters/unsupported/pathname.rb +3 -0
  236. data/spec/filters/unsupported/privacy.rb +136 -0
  237. data/spec/filters/unsupported/proc.rb +3 -0
  238. data/spec/filters/unsupported/regexp.rb +59 -0
  239. data/spec/filters/unsupported/set.rb +4 -0
  240. data/spec/filters/unsupported/{marshal.rb → singleton.rb} +4 -2
  241. data/spec/filters/unsupported/{mutable_strings.rb → string.rb} +456 -336
  242. data/spec/filters/unsupported/struct.rb +3 -0
  243. data/spec/filters/unsupported/symbol.rb +5 -0
  244. data/spec/filters/unsupported/taint.rb +16 -0
  245. data/spec/filters/unsupported/thread.rb +5 -0
  246. data/spec/filters/unsupported/time.rb +197 -16
  247. data/spec/lib/cli_spec.rb +14 -4
  248. data/spec/lib/compiler_spec.rb +9 -1
  249. data/spec/lib/parser/call_spec.rb +18 -0
  250. data/spec/lib/parser/not_spec.rb +2 -8
  251. data/spec/lib/sprockets_spec.rb +24 -0
  252. data/spec/opal/core/array/intersection_spec.rb +38 -0
  253. data/spec/opal/core/array/minus_spec.rb +38 -0
  254. data/spec/opal/core/array/union_spec.rb +38 -0
  255. data/spec/opal/core/array/uniq_spec.rb +49 -0
  256. data/spec/opal/core/exception_spec.rb +7 -0
  257. data/spec/opal/core/fixtures/require_tree_with_dot/file 1.rb +1 -0
  258. data/spec/opal/core/fixtures/require_tree_with_dot/file 2.rb +1 -0
  259. data/spec/opal/core/fixtures/require_tree_with_dot/file 3.rb +1 -0
  260. data/spec/opal/core/fixtures/require_tree_with_dot/index.rb +3 -0
  261. data/spec/opal/core/hash/internals_spec.rb +332 -0
  262. data/spec/opal/core/helpers_spec.rb +14 -0
  263. data/spec/opal/core/kernel/freeze_spec.rb +1 -1
  264. data/spec/opal/core/kernel/raise_spec.rb +13 -0
  265. data/spec/opal/core/kernel/require_tree_spec.rb +9 -0
  266. data/spec/opal/core/language/class_spec.rb +55 -0
  267. data/spec/opal/core/language/fixtures/send.rb +1 -0
  268. data/spec/opal/core/language/keyword_arguments_spec.rb +11 -0
  269. data/spec/opal/core/language/send_spec.rb +5 -0
  270. data/spec/opal/core/method/to_proc_spec.rb +28 -0
  271. data/spec/opal/core/module/name_spec.rb +0 -17
  272. data/spec/opal/core/runtime/bridged_classes_spec.rb +2 -2
  273. data/spec/opal/core/runtime/eval_spec.rb +1 -1
  274. data/spec/opal/core/runtime/method_missing_spec.rb +6 -0
  275. data/spec/opal/core/runtime_spec.rb +51 -0
  276. data/spec/opal/stdlib/js_spec.rb +66 -0
  277. data/spec/opal/stdlib/native/hash_spec.rb +36 -0
  278. data/spec/rubyspecs +152 -273
  279. data/spec/spec_helper.rb +10 -11
  280. data/stdlib/base64.rb +9 -9
  281. data/stdlib/benchmark.rb +551 -4
  282. data/stdlib/console.rb +94 -0
  283. data/stdlib/date.rb +1 -1
  284. data/stdlib/encoding.rb +1 -170
  285. data/stdlib/js.rb +56 -0
  286. data/stdlib/json.rb +9 -14
  287. data/stdlib/math.rb +1 -370
  288. data/stdlib/native.rb +133 -63
  289. data/stdlib/nodejs/file.rb +5 -0
  290. data/stdlib/nodejs/fileutils.rb +13 -6
  291. data/stdlib/nodejs/node_modules/js-yaml/node_modules/argparse/README.md +1 -1
  292. data/stdlib/opal-parser.rb +1 -2
  293. data/stdlib/ostruct.rb +65 -6
  294. data/stdlib/pp.rb +2 -4
  295. data/stdlib/rbconfig.rb +1 -3
  296. data/stdlib/strscan.rb +164 -28
  297. data/tasks/benchmarking.rake +88 -0
  298. data/tasks/testing.rake +181 -55
  299. data/{lib/mspec/opal/special_calls.rb → tasks/testing/mspec_special_calls.rb} +1 -1
  300. data/{lib/mspec/opal/sprockets.js → tasks/testing/phantomjs1-sprockets.js} +17 -6
  301. data/test/opal/test_keyword.rb +590 -0
  302. data/vendored-minitest/minitest.rb +2 -2
  303. data/vendored-minitest/test/unit.rb +5 -0
  304. metadata +229 -62
  305. data/benchmarks/operators.rb +0 -11
  306. data/benchmarks/prova.js.rb +0 -13
  307. data/docs/libraries.md +0 -36
  308. data/lib/mspec/opal/new.html.erb +0 -1
  309. data/lib/mspec/opal/rake_task.rb +0 -248
  310. data/opal/corelib/match_data.rb +0 -128
  311. data/spec/filters/bugs/math.rb +0 -95
  312. data/spec/filters/bugs/nil.rb +0 -7
  313. data/spec/filters/bugs/opal.rb +0 -9
  314. data/spec/filters/bugs/regular_expressions.rb +0 -41
  315. data/spec/filters/bugs/stringscanner.rb +0 -33
  316. data/spec/filters/unsupported/encoding.rb +0 -102
  317. data/spec/filters/unsupported/frozen.rb +0 -92
  318. data/spec/filters/unsupported/hash_compare_by_identity.rb +0 -16
  319. data/spec/filters/unsupported/integer_size.rb +0 -59
  320. data/spec/filters/unsupported/method_added.rb +0 -10
  321. data/spec/filters/unsupported/private_constants.rb +0 -30
  322. data/spec/filters/unsupported/private_methods.rb +0 -55
  323. data/spec/filters/unsupported/random.rb +0 -4
  324. data/spec/filters/unsupported/rational_numbers.rb +0 -4
  325. data/spec/filters/unsupported/regular_expressions.rb +0 -137
  326. data/spec/filters/unsupported/ruby_exe.rb +0 -5
  327. data/spec/filters/unsupported/symbols.rb +0 -17
  328. data/spec/filters/unsupported/tainted.rb +0 -180
  329. data/spec/filters/unsupported/trusted.rb +0 -88
  330. data/stdlib/process.rb +0 -10
  331. data/tasks/documenting.rake +0 -37
@@ -3,205 +3,36 @@ require 'corelib/comparable'
3
3
  class Numeric
4
4
  include Comparable
5
5
 
6
- `def.$$is_number = true`
7
-
8
- def __id__
9
- `(self * 2) + 1`
10
- end
11
- alias object_id __id__
12
-
13
- def coerce(other, type = :operation)
14
- %x{
15
- if (other.$$is_number) {
16
- return [self, other];
17
- }
18
- else {
19
- return #{other.coerce(self)};
20
- }
21
- }
22
- rescue
23
- case type
24
- when :operation
25
- raise TypeError, "#{other.class} can't be coerced into Numeric"
26
-
27
- when :comparison
28
- raise ArgumentError, "comparison of #{self.class} with #{other.class} failed"
6
+ def coerce(other)
7
+ if other.instance_of? self.class
8
+ return [other, self]
29
9
  end
10
+
11
+ [Float(other), Float(self)]
30
12
  end
31
13
 
32
- def send_coerced(method, other)
33
- type = case method
14
+ def __coerced__(method, other)
15
+ begin
16
+ a, b = other.coerce(self)
17
+ rescue
18
+ case method
34
19
  when :+, :-, :*, :/, :%, :&, :|, :^, :**
35
- :operation
20
+ raise TypeError, "#{other.class} can't be coerce into Numeric"
36
21
 
37
22
  when :>, :>=, :<, :<=, :<=>
38
- :comparison
23
+ raise ArgumentError, "comparison of #{self.class} with #{other.class} failed"
24
+ end
39
25
  end
40
26
 
41
- a, b = coerce(other, type)
42
27
  a.__send__ method, b
43
28
  end
44
29
 
45
- def +(other)
46
- %x{
47
- if (other.$$is_number) {
48
- return self + other;
49
- }
50
- else {
51
- return #{send_coerced :+, other};
52
- }
53
- }
54
- end
55
-
56
- def -(other)
57
- %x{
58
- if (other.$$is_number) {
59
- return self - other;
60
- }
61
- else {
62
- return #{send_coerced :-, other};
63
- }
64
- }
65
- end
66
-
67
- def *(other)
68
- %x{
69
- if (other.$$is_number) {
70
- return self * other;
71
- }
72
- else {
73
- return #{send_coerced :*, other};
74
- }
75
- }
76
- end
77
-
78
- def /(other)
79
- %x{
80
- if (other.$$is_number) {
81
- return self / other;
82
- }
83
- else {
84
- return #{send_coerced :/, other};
85
- }
86
- }
87
- end
88
-
89
- def %(other)
90
- %x{
91
- if (other.$$is_number) {
92
- if (other < 0 || self < 0) {
93
- return (self % other + other) % other;
94
- }
95
- else {
96
- return self % other;
97
- }
98
- }
99
- else {
100
- return #{send_coerced :%, other};
101
- }
102
- }
103
- end
104
-
105
- def &(other)
106
- %x{
107
- if (other.$$is_number) {
108
- return self & other;
109
- }
110
- else {
111
- return #{send_coerced :&, other};
112
- }
113
- }
114
- end
115
-
116
- def |(other)
117
- %x{
118
- if (other.$$is_number) {
119
- return self | other;
120
- }
121
- else {
122
- return #{send_coerced :|, other};
123
- }
124
- }
125
- end
126
-
127
- def ^(other)
128
- %x{
129
- if (other.$$is_number) {
130
- return self ^ other;
131
- }
132
- else {
133
- return #{send_coerced :^, other};
134
- }
135
- }
136
- end
137
-
138
- def <(other)
139
- %x{
140
- if (other.$$is_number) {
141
- return self < other;
142
- }
143
- else {
144
- return #{send_coerced :<, other};
145
- }
146
- }
147
- end
148
-
149
- def <=(other)
150
- %x{
151
- if (other.$$is_number) {
152
- return self <= other;
153
- }
154
- else {
155
- return #{send_coerced :<=, other};
156
- }
157
- }
158
- end
159
-
160
- def >(other)
161
- %x{
162
- if (other.$$is_number) {
163
- return self > other;
164
- }
165
- else {
166
- return #{send_coerced :>, other};
167
- }
168
- }
169
- end
170
-
171
- def >=(other)
172
- %x{
173
- if (other.$$is_number) {
174
- return self >= other;
175
- }
176
- else {
177
- return #{send_coerced :>=, other};
178
- }
179
- }
180
- end
181
-
182
30
  def <=>(other)
183
- %x{
184
- if (other.$$is_number) {
185
- return self > other ? 1 : (self < other ? -1 : 0);
186
- }
187
- else {
188
- return #{send_coerced :<=>, other};
189
- }
190
- }
191
- rescue ArgumentError
192
- nil
193
- end
194
-
195
- def <<(count)
196
- count = Opal.coerce_to! count, Integer, :to_int
197
-
198
- `#{count} > 0 ? self << #{count} : self >> -#{count}`
199
- end
200
-
201
- def >>(count)
202
- count = Opal.coerce_to! count, Integer, :to_int
31
+ if equal? other
32
+ return 0
33
+ end
203
34
 
204
- `#{count} > 0 ? self >> #{count} : self << -#{count}`
35
+ nil
205
36
  end
206
37
 
207
38
  def [](bit)
@@ -213,52 +44,33 @@ class Numeric
213
44
  end
214
45
 
215
46
  def +@
216
- `+self`
47
+ self
217
48
  end
218
49
 
219
50
  def -@
220
- `-self`
51
+ 0 - self
221
52
  end
222
53
 
223
- def ~
224
- `~self`
54
+ def %(other)
55
+ self - other * self.div(other)
225
56
  end
226
57
 
227
- def **(other)
228
- %x{
229
- if (other.$$is_number) {
230
- return Math.pow(self, other);
231
- }
232
- else {
233
- return #{send_coerced :**, other};
234
- }
235
- }
58
+ def abs
59
+ self < 0 ? -self : self
236
60
  end
237
61
 
238
- def ==(other)
239
- %x{
240
- if (other.$$is_number) {
241
- return self == Number(other);
242
- }
243
- else if (#{other.respond_to? :==}) {
244
- return #{other == self};
245
- }
246
- else {
247
- return false;
248
- }
249
- }
62
+ def abs2
63
+ self * self
250
64
  end
251
65
 
252
- def abs
253
- `Math.abs(self)`
66
+ def angle
67
+ self < 0 ? Math::PI : 0
254
68
  end
255
69
 
256
- def ceil
257
- `Math.ceil(self)`
258
- end
70
+ alias arg angle
259
71
 
260
- def chr(encoding=undefined)
261
- `String.fromCharCode(self)`
72
+ def ceil
73
+ to_f.ceil
262
74
  end
263
75
 
264
76
  def conj
@@ -267,283 +79,111 @@ class Numeric
267
79
 
268
80
  alias conjugate conj
269
81
 
270
- def downto(finish, &block)
271
- return enum_for :downto, finish unless block
82
+ def denominator
83
+ to_r.denominator
84
+ end
272
85
 
273
- %x{
274
- if (!finish.$$is_number) {
275
- #{raise ArgumentError, "comparison of #{self.class} with #{finish.class} failed"}
276
- }
277
- for (var i = self; i >= finish; i--) {
278
- if (block(i) === $breaker) {
279
- return $breaker.$v;
280
- }
281
- }
282
- }
86
+ def div(other)
87
+ raise ZeroDivisionError, "divided by o" if other == 0
283
88
 
284
- self
89
+ (self / other).floor
285
90
  end
286
91
 
287
- alias eql? ==
288
-
289
- def equal?(other)
290
- self == other || `isNaN(self) && isNaN(other)`
92
+ def divmod(other)
93
+ [div(other), self % other]
291
94
  end
292
95
 
293
- def even?
294
- `self % 2 === 0`
96
+ def fdiv(other)
97
+ self.to_f / other
295
98
  end
296
99
 
297
100
  def floor
298
- `Math.floor(self)`
101
+ to_f.floor
299
102
  end
300
103
 
301
- def gcd(other)
302
- unless Integer === other
303
- raise TypeError, 'not an integer'
304
- end
305
-
306
- %x{
307
- var min = Math.abs(self),
308
- max = Math.abs(other);
309
-
310
- while (min > 0) {
311
- var tmp = min;
312
-
313
- min = max % min;
314
- max = tmp;
315
- }
316
-
317
- return max;
318
- }
104
+ def i
105
+ Complex(0, self)
319
106
  end
320
107
 
321
- def gcdlcm(other)
322
- [gcd, lcm]
108
+ def imag
109
+ 0
323
110
  end
324
111
 
325
- def hash
326
- `'Numeric:'+self.toString()`
327
- end
112
+ alias imaginary imag
328
113
 
329
114
  def integer?
330
- `self % 1 === 0`
331
- end
332
-
333
- def is_a?(klass)
334
- return true if klass == Fixnum && Integer === self
335
- return true if klass == Integer && Integer === self
336
- return true if klass == Float && Float === self
337
-
338
- super
339
- end
340
-
341
- alias kind_of? is_a?
342
-
343
- def instance_of?(klass)
344
- return true if klass == Fixnum && Integer === self
345
- return true if klass == Integer && Integer === self
346
- return true if klass == Float && Float === self
347
-
348
- super
349
- end
350
-
351
- def lcm(other)
352
- unless Integer === other
353
- raise TypeError, 'not an integer'
354
- end
355
-
356
- %x{
357
- if (self == 0 || other == 0) {
358
- return 0;
359
- }
360
- else {
361
- return Math.abs(self * other / #{gcd(other)});
362
- }
363
- }
115
+ false
364
116
  end
365
117
 
366
118
  alias magnitude abs
367
119
 
368
120
  alias modulo %
369
121
 
370
- def next
371
- `self + 1`
372
- end
373
-
374
122
  def nonzero?
375
- `self == 0 ? nil : self`
376
- end
377
-
378
- def odd?
379
- `self % 2 !== 0`
123
+ zero? ? nil : self
380
124
  end
381
125
 
382
- def ord
383
- self
126
+ def numerator
127
+ to_r.numerator
384
128
  end
385
129
 
386
- def pred
387
- `self - 1`
388
- end
130
+ alias phase arg
389
131
 
390
- def round(ndigits=0)
391
- %x{
392
- var scale = Math.pow(10, ndigits);
393
- return Math.round(self * scale) / scale;
394
- }
132
+ def polar
133
+ return abs, arg
395
134
  end
396
135
 
397
- def step(limit, step = 1, &block)
398
- return enum_for :step, limit, step unless block
399
-
400
- raise ArgumentError, 'step cannot be 0' if `step == 0`
401
-
402
- %x{
403
- var value = self;
404
-
405
- if (step > 0) {
406
- while (value <= limit) {
407
- block(value);
408
- value += step;
409
- }
410
- }
411
- else {
412
- while (value >= limit) {
413
- block(value);
414
- value += step;
415
- }
416
- }
417
- }
418
-
419
- self
136
+ def quo(other)
137
+ Opal.coerce_to!(self, Rational, :to_r) / other
420
138
  end
421
139
 
422
- alias succ next
423
-
424
- def times(&block)
425
- return enum_for :times unless block
426
-
427
- %x{
428
- for (var i = 0; i < self; i++) {
429
- if (block(i) === $breaker) {
430
- return $breaker.$v;
431
- }
432
- }
433
- }
434
-
140
+ def real
435
141
  self
436
142
  end
437
143
 
438
- def to_f
439
- self
144
+ def real?
145
+ true
440
146
  end
441
147
 
442
- def to_i
443
- `parseInt(self)`
148
+ def rect
149
+ [self, 0]
444
150
  end
445
151
 
446
- alias to_int to_i
152
+ alias rectangular rect
447
153
 
448
- def to_s(base = 10)
449
- if base < 2 || base > 36
450
- raise ArgumentError, 'base must be between 2 and 36'
451
- end
452
-
453
- `self.toString(base)`
154
+ def round(digits = undefined)
155
+ to_f.round(digits)
454
156
  end
455
157
 
456
- alias inspect to_s
457
-
458
- def divmod(rhs)
459
- q = (self / rhs).floor
460
- r = self % rhs
461
-
462
- [q, r]
463
- end
464
-
465
- def upto(finish, &block)
466
- return enum_for :upto, finish unless block
467
-
468
- %x{
469
- if (!finish.$$is_number) {
470
- #{raise ArgumentError, "comparison of #{self.class} with #{finish.class} failed"}
471
- }
472
- for (var i = self; i <= finish; i++) {
473
- if (block(i) === $breaker) {
474
- return $breaker.$v;
475
- }
476
- }
477
- }
478
-
479
- self
158
+ def to_c
159
+ Complex(self, 0)
480
160
  end
481
161
 
482
- def zero?
483
- `self == 0`
162
+ def to_int
163
+ to_i
484
164
  end
485
165
 
486
- # Since bitwise operations are 32 bit, declare it to be so.
487
- def size
488
- 4
166
+ def truncate
167
+ to_f.truncate
489
168
  end
490
169
 
491
- def nan?
492
- `isNaN(self)`
493
- end
494
-
495
- def finite?
496
- `self != Infinity && self != -Infinity`
497
- end
498
-
499
- def infinite?
500
- %x{
501
- if (self == Infinity) {
502
- return +1;
503
- }
504
- else if (self == -Infinity) {
505
- return -1;
506
- }
507
- else {
508
- return nil;
509
- }
510
- }
170
+ def zero?
171
+ self == 0
511
172
  end
512
173
 
513
174
  def positive?
514
- `1 / self > 0`
175
+ self > 0
515
176
  end
516
177
 
517
178
  def negative?
518
- `1 / self < 0`
179
+ self < 0
519
180
  end
520
- end
521
-
522
- Fixnum = Numeric
523
-
524
- class Integer < Numeric
525
- def self.===(other)
526
- %x{
527
- if (!other.$$is_number) {
528
- return false;
529
- }
530
181
 
531
- return (other % 1) === 0;
532
- }
182
+ def dup
183
+ raise TypeError, "can't dup #{self.class}"
533
184
  end
534
- end
535
-
536
- class Float < Numeric
537
- def self.===(other)
538
- `!!other.$$is_number`
539
- end
540
-
541
- INFINITY = `Infinity`
542
- NAN = `NaN`
543
185
 
544
- if defined?(`Number.EPSILON`)
545
- EPSILON = `Number.EPSILON`
546
- else
547
- EPSILON = `2.2204460492503130808472633361816E-16`
186
+ def clone
187
+ raise TypeError, "can't clone #{self.class}"
548
188
  end
549
189
  end