opal 1.0.5 → 1.1.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (394) hide show
  1. checksums.yaml +4 -4
  2. data/.codeclimate.yml +14 -12
  3. data/.gitattributes +1 -1
  4. data/.github/workflows/build.yml +15 -10
  5. data/.jshintrc +1 -1
  6. data/.overcommit.yml +35 -0
  7. data/.rubocop.yml +44 -7
  8. data/{.rubocop_todo.yml → .rubocop/todo.yml} +4 -5
  9. data/CHANGELOG.md +87 -2
  10. data/Gemfile +1 -2
  11. data/HACKING.md +2 -9
  12. data/README.md +20 -17
  13. data/UNRELEASED.md +108 -0
  14. data/benchmark-ips/bm_array_pop_1.rb +8 -0
  15. data/benchmark-ips/bm_array_shift.rb +7 -0
  16. data/benchmark-ips/class_shovel_vs_singleton_class.rb +16 -0
  17. data/bin/build-browser-source-map-support +5 -0
  18. data/bin/format-filters +54 -0
  19. data/bin/remove-filters +39 -0
  20. data/docs/roda-sprockets.md +86 -0
  21. data/docs/sinatra.md +5 -12
  22. data/examples/rack/Gemfile +1 -0
  23. data/examples/rack/app/application.rb +3 -0
  24. data/examples/rack/config.ru +5 -4
  25. data/exe/opal-repl +2 -83
  26. data/lib/opal/ast/builder.rb +1 -1
  27. data/lib/opal/builder.rb +2 -10
  28. data/lib/opal/builder_processors.rb +1 -1
  29. data/lib/opal/cli_runners.rb +27 -37
  30. data/lib/opal/cli_runners/applescript.rb +5 -44
  31. data/lib/opal/cli_runners/chrome.js +7 -35
  32. data/lib/opal/cli_runners/chrome.rb +75 -17
  33. data/lib/opal/cli_runners/compiler.rb +17 -0
  34. data/lib/opal/cli_runners/nashorn.rb +9 -43
  35. data/lib/opal/cli_runners/nodejs.rb +18 -47
  36. data/lib/opal/cli_runners/server.rb +18 -6
  37. data/lib/opal/cli_runners/source-map-support-browser.js +6445 -0
  38. data/lib/opal/cli_runners/source-map-support.js +639 -0
  39. data/lib/opal/cli_runners/system_runner.rb +45 -0
  40. data/lib/opal/compiler.rb +57 -29
  41. data/lib/opal/config.rb +0 -5
  42. data/lib/opal/erb.rb +1 -1
  43. data/lib/opal/magic_comments.rb +34 -0
  44. data/lib/opal/nodes/args/arity_check.rb +2 -1
  45. data/lib/opal/nodes/base.rb +1 -1
  46. data/lib/opal/nodes/call.rb +1 -4
  47. data/lib/opal/nodes/def.rb +2 -0
  48. data/lib/opal/nodes/iter.rb +1 -1
  49. data/lib/opal/nodes/literal.rb +19 -14
  50. data/lib/opal/nodes/logic.rb +5 -80
  51. data/lib/opal/nodes/masgn.rb +2 -0
  52. data/lib/opal/nodes/rescue.rb +1 -1
  53. data/lib/opal/nodes/super.rb +24 -10
  54. data/lib/opal/nodes/top.rb +5 -4
  55. data/lib/opal/parser/with_c_lexer.rb +2 -1
  56. data/lib/opal/parser/with_ruby_lexer.rb +1 -1
  57. data/lib/opal/path_reader.rb +2 -2
  58. data/lib/opal/paths.rb +8 -5
  59. data/lib/opal/repl.rb +103 -0
  60. data/lib/opal/rewriter.rb +4 -0
  61. data/lib/opal/rewriters/for_rewriter.rb +1 -1
  62. data/lib/opal/rewriters/js_reserved_words.rb +7 -7
  63. data/lib/opal/rewriters/numblocks.rb +31 -0
  64. data/lib/opal/rewriters/returnable_logic.rb +33 -0
  65. data/lib/opal/version.rb +1 -1
  66. data/opal.gemspec +25 -25
  67. data/opal/corelib/array.rb +160 -118
  68. data/opal/corelib/array/pack.rb +5 -3
  69. data/opal/corelib/basic_object.rb +4 -4
  70. data/opal/corelib/class.rb +2 -1
  71. data/opal/corelib/comparable.rb +49 -31
  72. data/opal/corelib/constants.rb +5 -5
  73. data/opal/corelib/enumerable.rb +108 -46
  74. data/opal/corelib/enumerator.rb +27 -12
  75. data/opal/corelib/file.rb +3 -1
  76. data/opal/corelib/hash.rb +6 -1
  77. data/opal/corelib/helpers.rb +8 -28
  78. data/opal/corelib/io.rb +12 -7
  79. data/opal/corelib/kernel.rb +45 -14
  80. data/opal/corelib/kernel/format.rb +3 -1
  81. data/opal/corelib/math.rb +8 -6
  82. data/opal/corelib/method.rb +8 -0
  83. data/opal/corelib/module.rb +17 -2
  84. data/opal/corelib/number.rb +3 -12
  85. data/opal/corelib/proc.rb +16 -0
  86. data/opal/corelib/random/mersenne_twister.rb +147 -0
  87. data/opal/corelib/range.rb +3 -13
  88. data/opal/corelib/regexp.rb +10 -6
  89. data/opal/corelib/runtime.js +208 -81
  90. data/opal/corelib/string.rb +55 -49
  91. data/opal/corelib/string/encoding.rb +109 -32
  92. data/opal/corelib/string/unpack.rb +2 -17
  93. data/opal/corelib/struct.rb +14 -1
  94. data/opal/corelib/time.rb +4 -0
  95. data/opal/opal.rb +1 -1
  96. data/opal/opal/mini.rb +1 -1
  97. data/package.json +1 -0
  98. data/spec/README.md +10 -0
  99. data/spec/filters/bugs/array.rb +76 -0
  100. data/spec/filters/bugs/base64.rb +10 -0
  101. data/spec/filters/bugs/basicobject.rb +12 -0
  102. data/spec/filters/bugs/bigdecimal.rb +248 -0
  103. data/spec/filters/bugs/class.rb +12 -0
  104. data/spec/filters/bugs/complex.rb +7 -0
  105. data/spec/filters/bugs/date.rb +104 -0
  106. data/spec/filters/bugs/encoding.rb +259 -0
  107. data/spec/filters/bugs/enumerable.rb +26 -0
  108. data/spec/filters/bugs/enumerator.rb +48 -0
  109. data/spec/filters/bugs/exception.rb +120 -0
  110. data/spec/filters/bugs/file.rb +48 -0
  111. data/spec/filters/bugs/float.rb +74 -0
  112. data/spec/filters/bugs/hash.rb +60 -0
  113. data/spec/filters/bugs/integer.rb +78 -0
  114. data/spec/filters/bugs/io.rb +9 -0
  115. data/spec/filters/bugs/kernel.rb +401 -0
  116. data/spec/filters/bugs/language.rb +451 -0
  117. data/spec/filters/bugs/marshal.rb +50 -0
  118. data/spec/filters/bugs/math.rb +4 -0
  119. data/spec/filters/bugs/method.rb +79 -0
  120. data/spec/filters/bugs/module.rb +281 -0
  121. data/spec/filters/bugs/nilclass.rb +4 -0
  122. data/spec/filters/bugs/numeric.rb +27 -0
  123. data/spec/filters/bugs/openstruct.rb +8 -0
  124. data/spec/filters/bugs/pack_unpack.rb +138 -0
  125. data/spec/filters/bugs/pathname.rb +9 -0
  126. data/spec/filters/bugs/proc.rb +80 -0
  127. data/spec/filters/bugs/random.rb +20 -0
  128. data/spec/filters/bugs/range.rb +139 -0
  129. data/spec/filters/bugs/rational.rb +12 -0
  130. data/spec/filters/bugs/regexp.rb +91 -0
  131. data/spec/filters/bugs/set.rb +51 -0
  132. data/spec/filters/bugs/singleton.rb +7 -0
  133. data/spec/filters/bugs/string.rb +341 -0
  134. data/spec/filters/bugs/stringscanner.rb +78 -0
  135. data/spec/filters/bugs/struct.rb +26 -0
  136. data/spec/filters/bugs/symbol.rb +7 -0
  137. data/spec/filters/bugs/time.rb +109 -0
  138. data/spec/filters/bugs/unboundmethod.rb +33 -0
  139. data/spec/filters/bugs/warnings.rb +30 -0
  140. data/spec/filters/unsupported/array.rb +168 -0
  141. data/spec/filters/unsupported/basicobject.rb +15 -0
  142. data/spec/filters/unsupported/bignum.rb +55 -0
  143. data/spec/filters/unsupported/class.rb +5 -0
  144. data/spec/filters/unsupported/delegator.rb +6 -0
  145. data/spec/filters/unsupported/enumerable.rb +12 -0
  146. data/spec/filters/unsupported/enumerator.rb +14 -0
  147. data/spec/filters/unsupported/file.rb +4 -0
  148. data/spec/filters/unsupported/fixnum.rb +15 -0
  149. data/spec/filters/unsupported/float.rb +47 -0
  150. data/spec/filters/unsupported/freeze.rb +259 -0
  151. data/spec/filters/unsupported/hash.rb +44 -0
  152. data/spec/filters/unsupported/integer.rb +101 -0
  153. data/spec/filters/unsupported/kernel.rb +35 -0
  154. data/spec/filters/unsupported/language.rb +25 -0
  155. data/spec/filters/unsupported/marshal.rb +44 -0
  156. data/spec/filters/unsupported/matchdata.rb +63 -0
  157. data/spec/filters/unsupported/math.rb +4 -0
  158. data/spec/filters/unsupported/pathname.rb +4 -0
  159. data/spec/filters/unsupported/privacy.rb +287 -0
  160. data/spec/filters/unsupported/proc.rb +4 -0
  161. data/spec/filters/unsupported/random.rb +5 -0
  162. data/spec/filters/unsupported/range.rb +8 -0
  163. data/spec/filters/unsupported/regexp.rb +70 -0
  164. data/spec/filters/unsupported/set.rb +5 -0
  165. data/spec/filters/unsupported/singleton.rb +7 -0
  166. data/spec/filters/unsupported/string.rb +687 -0
  167. data/spec/filters/unsupported/struct.rb +7 -0
  168. data/spec/filters/unsupported/symbol.rb +21 -0
  169. data/spec/filters/unsupported/taint.rb +162 -0
  170. data/spec/filters/unsupported/thread.rb +10 -0
  171. data/spec/filters/unsupported/time.rb +204 -0
  172. data/spec/filters/unsupported/usage_of_files.rb +262 -0
  173. data/spec/lib/builder_processors_spec.rb +44 -0
  174. data/spec/lib/builder_spec.rb +133 -0
  175. data/spec/lib/cli_runners/server_spec.rb +25 -0
  176. data/spec/lib/cli_runners_spec.rb +16 -0
  177. data/spec/lib/cli_spec.rb +256 -0
  178. data/spec/lib/compiler_spec.rb +693 -0
  179. data/spec/lib/config_spec.rb +112 -0
  180. data/spec/lib/dependency_resolver_spec.rb +43 -0
  181. data/spec/lib/deprecations_spec.rb +17 -0
  182. data/spec/lib/fixtures/complex_sprockets.js.rb.erb +4 -0
  183. data/spec/lib/fixtures/file_with_directives.js +2 -0
  184. data/spec/lib/fixtures/jst_file.js.jst +1 -0
  185. data/spec/lib/fixtures/no_requires.rb +1 -0
  186. data/spec/lib/fixtures/opal_file.rb +2 -0
  187. data/spec/lib/fixtures/require_tree_test.rb +3 -0
  188. data/spec/lib/fixtures/required_file.js +1 -0
  189. data/spec/lib/fixtures/required_tree_test/required_file1.rb +1 -0
  190. data/spec/lib/fixtures/required_tree_test/required_file2.rb +1 -0
  191. data/spec/lib/fixtures/requires.rb +7 -0
  192. data/spec/lib/fixtures/source_location_test.rb +7 -0
  193. data/spec/lib/fixtures/source_map.rb +1 -0
  194. data/spec/lib/fixtures/source_map/subfolder/other_file.rb +1 -0
  195. data/spec/lib/fixtures/sprockets_file.js.rb +3 -0
  196. data/spec/lib/fixtures/sprockets_require_tree_test.rb +3 -0
  197. data/spec/lib/path_reader_spec.rb +47 -0
  198. data/spec/lib/paths_spec.rb +18 -0
  199. data/spec/lib/repl_spec.rb +28 -0
  200. data/spec/lib/rewriters/base_spec.rb +68 -0
  201. data/spec/lib/rewriters/binary_operator_assignment_spec.rb +153 -0
  202. data/spec/lib/rewriters/block_to_iter_spec.rb +28 -0
  203. data/spec/lib/rewriters/dot_js_syntax_spec.rb +108 -0
  204. data/spec/lib/rewriters/explicit_writer_return_spec.rb +186 -0
  205. data/spec/lib/rewriters/for_rewriter_spec.rb +92 -0
  206. data/spec/lib/rewriters/hashes/key_duplicates_rewriter_spec.rb +47 -0
  207. data/spec/lib/rewriters/js_reserved_words_spec.rb +119 -0
  208. data/spec/lib/rewriters/logical_operator_assignment_spec.rb +202 -0
  209. data/spec/lib/rewriters/opal_engine_check_spec.rb +84 -0
  210. data/spec/lib/rewriters/returnable_logic_spec.rb +52 -0
  211. data/spec/lib/rewriters/rubyspec/filters_rewriter_spec.rb +59 -0
  212. data/spec/lib/simple_server_spec.rb +56 -0
  213. data/spec/lib/source_map/file_spec.rb +67 -0
  214. data/spec/lib/source_map/index_spec.rb +80 -0
  215. data/spec/lib/spec_helper.rb +105 -0
  216. data/spec/mspec-opal/formatters.rb +197 -0
  217. data/spec/mspec-opal/runner.rb +172 -0
  218. data/spec/opal/compiler/irb_spec.rb +44 -0
  219. data/spec/opal/compiler/unicode_spec.rb +10 -0
  220. data/spec/opal/core/array/dup_spec.rb +23 -0
  221. data/spec/opal/core/array/intersection_spec.rb +38 -0
  222. data/spec/opal/core/array/minus_spec.rb +38 -0
  223. data/spec/opal/core/array/union_spec.rb +38 -0
  224. data/spec/opal/core/array/uniq_spec.rb +49 -0
  225. data/spec/opal/core/class/inherited_spec.rb +18 -0
  226. data/spec/opal/core/enumerable/all_break_spec.rb +5 -0
  227. data/spec/opal/core/enumerable/any_break_spec.rb +5 -0
  228. data/spec/opal/core/enumerable/collect_break_spec.rb +13 -0
  229. data/spec/opal/core/enumerable/count_break_spec.rb +5 -0
  230. data/spec/opal/core/enumerable/detect_break_spec.rb +5 -0
  231. data/spec/opal/core/enumerable/drop_while_break_spec.rb +5 -0
  232. data/spec/opal/core/enumerable/each_slice_break_spec.rb +6 -0
  233. data/spec/opal/core/enumerable/each_with_index_break_spec.rb +5 -0
  234. data/spec/opal/core/enumerable/each_with_object_break_spec.rb +5 -0
  235. data/spec/opal/core/enumerable/find_all_break_spec.rb +5 -0
  236. data/spec/opal/core/enumerable/find_index_break_spec.rb +5 -0
  237. data/spec/opal/core/enumerable/grep_break_spec.rb +5 -0
  238. data/spec/opal/core/enumerable/max_break_spec.rb +5 -0
  239. data/spec/opal/core/enumerable/max_by_break_spec.rb +5 -0
  240. data/spec/opal/core/enumerable/min_break_spec.rb +5 -0
  241. data/spec/opal/core/enumerable/min_by_break_spec.rb +5 -0
  242. data/spec/opal/core/enumerable/none_break_spec.rb +5 -0
  243. data/spec/opal/core/enumerable/one_break_spec.rb +5 -0
  244. data/spec/opal/core/enumerable/reduce_break_spec.rb +5 -0
  245. data/spec/opal/core/enumerable/take_while_break_spec.rb +5 -0
  246. data/spec/opal/core/enumerator/with_index_spec.rb +6 -0
  247. data/spec/opal/core/exception_spec.rb +8 -0
  248. data/spec/opal/core/fixtures/require_tree_files/file 1.rb +1 -0
  249. data/spec/opal/core/fixtures/require_tree_files/file 2.rb +1 -0
  250. data/spec/opal/core/fixtures/require_tree_files/file 3.rb +1 -0
  251. data/spec/opal/core/fixtures/require_tree_files/file 4.rb +1 -0
  252. data/spec/opal/core/fixtures/require_tree_files/file 5.rb +1 -0
  253. data/spec/opal/core/fixtures/require_tree_files/nested/nested 1.rb +1 -0
  254. data/spec/opal/core/fixtures/require_tree_files/nested/nested 2.rb +1 -0
  255. data/spec/opal/core/fixtures/require_tree_files/other/other 1.rb +1 -0
  256. data/spec/opal/core/fixtures/require_tree_with_dot/file 1.rb +1 -0
  257. data/spec/opal/core/fixtures/require_tree_with_dot/file 2.rb +1 -0
  258. data/spec/opal/core/fixtures/require_tree_with_dot/file 3.rb +1 -0
  259. data/spec/opal/core/fixtures/require_tree_with_dot/index.rb +3 -0
  260. data/spec/opal/core/hash/internals_spec.rb +339 -0
  261. data/spec/opal/core/helpers_spec.rb +14 -0
  262. data/spec/opal/core/iterable_props_spec.rb +53 -0
  263. data/spec/opal/core/kernel/at_exit_spec.rb +70 -0
  264. data/spec/opal/core/kernel/freeze_spec.rb +15 -0
  265. data/spec/opal/core/kernel/instance_variables_spec.rb +110 -0
  266. data/spec/opal/core/kernel/methods_spec.rb +25 -0
  267. data/spec/opal/core/kernel/public_methods_spec.rb +25 -0
  268. data/spec/opal/core/kernel/require_tree_spec.rb +18 -0
  269. data/spec/opal/core/kernel/respond_to_spec.rb +15 -0
  270. data/spec/opal/core/language/DATA/characters_support_spec.rb +9 -0
  271. data/spec/opal/core/language/DATA/empty___END___spec.rb +7 -0
  272. data/spec/opal/core/language/DATA/multiple___END___spec.rb +10 -0
  273. data/spec/opal/core/language/arguments/mlhs_arg_spec.rb +18 -0
  274. data/spec/opal/core/language/keyword_arguments_spec.rb +9 -0
  275. data/spec/opal/core/language/numblocks_spec.rb +16 -0
  276. data/spec/opal/core/language/safe_navigator_spec.rb +7 -0
  277. data/spec/opal/core/language/while_spec.rb +31 -0
  278. data/spec/opal/core/language_spec.rb +29 -0
  279. data/spec/opal/core/marshal/dump_spec.rb +81 -0
  280. data/spec/opal/core/marshal/load_spec.rb +13 -0
  281. data/spec/opal/core/module_spec.rb +27 -0
  282. data/spec/opal/core/object_id_spec.rb +56 -0
  283. data/spec/opal/core/regexp/interpolation_spec.rb +40 -0
  284. data/spec/opal/core/regexp/match_spec.rb +78 -0
  285. data/spec/opal/core/runtime/bridged_classes_spec.rb +123 -0
  286. data/spec/opal/core/runtime/constants_spec.rb +16 -0
  287. data/spec/opal/core/runtime/eval_spec.rb +5 -0
  288. data/spec/opal/core/runtime/exit_spec.rb +29 -0
  289. data/spec/opal/core/runtime/is_a_spec.rb +48 -0
  290. data/spec/opal/core/runtime/loaded_spec.rb +20 -0
  291. data/spec/opal/core/runtime/main_methods_spec.rb +39 -0
  292. data/spec/opal/core/runtime/method_missing_spec.rb +68 -0
  293. data/spec/opal/core/runtime/rescue_spec.rb +37 -0
  294. data/spec/opal/core/runtime/string_spec.rb +25 -0
  295. data/spec/opal/core/runtime/truthy_spec.rb +61 -0
  296. data/spec/opal/core/runtime_spec.rb +58 -0
  297. data/spec/opal/core/string/each_byte_spec.rb +19 -0
  298. data/spec/opal/core/string/gsub_spec.rb +35 -0
  299. data/spec/opal/core/string/to_sym_spec.rb +9 -0
  300. data/spec/opal/core/string_spec.rb +28 -0
  301. data/spec/opal/core/struct/dup_spec.rb +11 -0
  302. data/spec/opal/core/time_spec.rb +68 -0
  303. data/spec/opal/stdlib/erb/erb_spec.rb +30 -0
  304. data/spec/opal/stdlib/erb/inline_block.opalerb +3 -0
  305. data/spec/opal/stdlib/erb/quoted.opalerb +1 -0
  306. data/spec/opal/stdlib/erb/simple.opalerb +1 -0
  307. data/spec/opal/stdlib/js_spec.rb +72 -0
  308. data/spec/opal/stdlib/json/ext_spec.rb +55 -0
  309. data/spec/opal/stdlib/json/parse_spec.rb +37 -0
  310. data/spec/opal/stdlib/logger/logger_spec.rb +308 -0
  311. data/spec/opal/stdlib/native/alias_native_spec.rb +27 -0
  312. data/spec/opal/stdlib/native/array_spec.rb +11 -0
  313. data/spec/opal/stdlib/native/date_spec.rb +12 -0
  314. data/spec/opal/stdlib/native/deprecated_include_spec.rb +8 -0
  315. data/spec/opal/stdlib/native/each_spec.rb +13 -0
  316. data/spec/opal/stdlib/native/element_reference_spec.rb +16 -0
  317. data/spec/opal/stdlib/native/exposure_spec.rb +33 -0
  318. data/spec/opal/stdlib/native/ext_spec.rb +19 -0
  319. data/spec/opal/stdlib/native/hash_spec.rb +67 -0
  320. data/spec/opal/stdlib/native/initialize_spec.rb +17 -0
  321. data/spec/opal/stdlib/native/method_missing_spec.rb +51 -0
  322. data/spec/opal/stdlib/native/native_alias_spec.rb +26 -0
  323. data/spec/opal/stdlib/native/native_class_spec.rb +18 -0
  324. data/spec/opal/stdlib/native/native_module_spec.rb +13 -0
  325. data/spec/opal/stdlib/native/native_reader_spec.rb +22 -0
  326. data/spec/opal/stdlib/native/native_writer_spec.rb +30 -0
  327. data/spec/opal/stdlib/native/new_spec.rb +92 -0
  328. data/spec/opal/stdlib/native/struct_spec.rb +12 -0
  329. data/spec/opal/stdlib/pp_spec.rb +5 -0
  330. data/spec/opal/stdlib/promise/always_spec.rb +49 -0
  331. data/spec/opal/stdlib/promise/error_spec.rb +15 -0
  332. data/spec/opal/stdlib/promise/rescue_spec.rb +53 -0
  333. data/spec/opal/stdlib/promise/then_spec.rb +79 -0
  334. data/spec/opal/stdlib/promise/trace_spec.rb +51 -0
  335. data/spec/opal/stdlib/promise/value_spec.rb +15 -0
  336. data/spec/opal/stdlib/promise/when_spec.rb +34 -0
  337. data/spec/opal/stdlib/source_map_spec.rb +8 -0
  338. data/spec/opal/stdlib/strscan/scan_spec.rb +11 -0
  339. data/spec/opal/stdlib/template/paths_spec.rb +10 -0
  340. data/spec/opal/stdlib/thread/mutex_spec.rb +40 -0
  341. data/spec/opal/stdlib/thread/thread_queue_spec.rb +32 -0
  342. data/spec/opal/stdlib/thread/thread_spec.rb +60 -0
  343. data/spec/ruby_specs +183 -0
  344. data/spec/spec_helper.rb +31 -0
  345. data/spec/support/guard_platform.rb +4 -0
  346. data/spec/support/match_helpers.rb +57 -0
  347. data/spec/support/mspec_rspec_adapter.rb +33 -0
  348. data/spec/support/rewriters_helper.rb +54 -0
  349. data/spec/support/source_map_helper.rb +190 -0
  350. data/stdlib/base64.rb +2 -2
  351. data/stdlib/bigdecimal.rb +15 -3
  352. data/stdlib/bigdecimal/bignumber.js.rb +1 -1
  353. data/stdlib/bigdecimal/util.rb +148 -0
  354. data/stdlib/delegate.rb +8 -0
  355. data/stdlib/nodejs/fileutils.rb +1 -1
  356. data/stdlib/nodejs/kernel.rb +0 -13
  357. data/stdlib/nodejs/stacktrace.rb +4 -179
  358. data/stdlib/ostruct.rb +5 -0
  359. data/stdlib/pp.rb +586 -19
  360. data/stdlib/prettyprint.rb +556 -0
  361. data/stdlib/rbconfig/sizeof.rb +2 -0
  362. data/stdlib/securerandom.rb +32 -0
  363. data/stdlib/set.rb +36 -0
  364. data/stdlib/strscan.rb +15 -0
  365. data/tasks/benchmarking.rake +1 -1
  366. data/tasks/releasing.rake +0 -3
  367. data/tasks/testing.rake +16 -11
  368. data/tasks/testing/mspec_special_calls.rb +1 -19
  369. data/test/nodejs/fixtures/cat.png +0 -0
  370. data/test/nodejs/fixtures/hello.rb +1 -0
  371. data/test/nodejs/fixtures/iso88591.txt +1 -0
  372. data/test/nodejs/fixtures/utf8.txt +1 -0
  373. data/test/nodejs/fixtures/win1258.txt +1 -0
  374. data/test/nodejs/test_dir.rb +39 -0
  375. data/test/nodejs/test_env.rb +62 -0
  376. data/test/nodejs/test_error.rb +29 -0
  377. data/test/nodejs/test_file.rb +206 -0
  378. data/test/nodejs/test_file_encoding.rb +20 -0
  379. data/test/nodejs/test_io.rb +18 -0
  380. data/test/nodejs/test_opal_builder.rb +12 -0
  381. data/test/nodejs/test_pathname.rb +16 -0
  382. data/test/opal/cat.png +0 -0
  383. data/test/opal/http_server.rb +52 -0
  384. data/test/opal/test_base64.rb +115 -0
  385. data/test/opal/test_keyword.rb +590 -0
  386. data/test/opal/test_matrix.rb +661 -0
  387. data/test/opal/test_openuri.rb +53 -0
  388. data/test/opal/unsupported_and_bugs.rb +39 -0
  389. data/yarn.lock +1114 -3
  390. metadata +889 -31
  391. data/lib/opal/nodes/runtime_helpers.rb +0 -51
  392. data/opal/corelib/random/MersenneTwister.js +0 -137
  393. data/opal/corelib/random/mersenne_twister.js.rb +0 -13
  394. data/stdlib/bigdecimal/kernel.rb +0 -5
@@ -1,3 +1,5 @@
1
+ # helpers: breaker, slice, falsy, truthy, coerce_to
2
+
1
3
  require 'corelib/enumerable'
2
4
 
3
5
  class Enumerator
@@ -25,8 +27,8 @@ class Enumerator
25
27
  @args = []
26
28
  @size = `arguments[0] || nil`
27
29
 
28
- if @size
29
- @size = Opal.coerce_to @size, Integer, :to_int
30
+ if @size && !@size.respond_to?(:call)
31
+ @size = `$coerce_to(#{@size}, #{Integer}, 'to_int')`
30
32
  end
31
33
  else
32
34
  @object = `arguments[0]`
@@ -47,12 +49,12 @@ class Enumerator
47
49
  end
48
50
 
49
51
  def size
50
- Proc === @size ? @size.call(*@args) : @size
52
+ @size.respond_to?(:call) ? @size.call(*@args) : @size
51
53
  end
52
54
 
53
55
  def with_index(offset = 0, &block)
54
56
  offset = if offset
55
- Opal.coerce_to offset, Integer, :to_int
57
+ `$coerce_to(offset, #{Integer}, 'to_int')`
56
58
  else
57
59
  0
58
60
  end
@@ -77,6 +79,13 @@ class Enumerator
77
79
 
78
80
  alias with_object each_with_object
79
81
 
82
+ def each_with_index(&block)
83
+ return enum_for(:each_with_index) { size } unless block_given?
84
+
85
+ super
86
+ @object
87
+ end
88
+
80
89
  def inspect
81
90
  result = "#<#{self.class}: #{@object.inspect}:#{@method}"
82
91
 
@@ -213,7 +222,7 @@ class Enumerator
213
222
  end
214
223
 
215
224
  def drop(n)
216
- n = Opal.coerce_to n, Integer, :to_int
225
+ n = `$coerce_to(#{n}, #{Integer}, 'to_int')`
217
226
 
218
227
  if n < 0
219
228
  raise ArgumentError, 'attempt to drop negative size'
@@ -247,7 +256,7 @@ class Enumerator
247
256
  %x{
248
257
  var value = Opal.yieldX(block, args);
249
258
 
250
- if (#{Opal.falsy?(`value`)}) {
259
+ if ($falsy(value)) {
251
260
  succeeding = false;
252
261
 
253
262
  #{enum.yield(*args)};
@@ -263,6 +272,8 @@ class Enumerator
263
272
  self.class.for(self, method, *args, &block)
264
273
  end
265
274
 
275
+ alias filter find_all
276
+
266
277
  def find_all(&block)
267
278
  unless block
268
279
  raise ArgumentError, 'tried to call lazy select without a block'
@@ -272,7 +283,7 @@ class Enumerator
272
283
  %x{
273
284
  var value = Opal.yieldX(block, args);
274
285
 
275
- if (#{Opal.truthy?(`value`)}) {
286
+ if ($truthy(value)) {
276
287
  #{enum.yield(*args)};
277
288
  }
278
289
  }
@@ -288,7 +299,7 @@ class Enumerator
288
299
  var param = #{Opal.destructure(args)},
289
300
  value = #{pattern === `param`};
290
301
 
291
- if (#{Opal.truthy?(`value`)}) {
302
+ if ($truthy(value)) {
292
303
  value = Opal.yield1(block, param);
293
304
 
294
305
  #{enum.yield `Opal.yield1(block, param)`};
@@ -301,7 +312,7 @@ class Enumerator
301
312
  var param = #{Opal.destructure(args)},
302
313
  value = #{pattern === `param`};
303
314
 
304
- if (#{Opal.truthy?(`value`)}) {
315
+ if ($truthy(value)) {
305
316
  #{enum.yield `param`};
306
317
  }
307
318
  }
@@ -322,7 +333,7 @@ class Enumerator
322
333
  %x{
323
334
  var value = Opal.yieldX(block, args);
324
335
 
325
- if (#{Opal.falsy?(`value`)}) {
336
+ if ($falsy(value)) {
326
337
  #{enum.yield(*args)};
327
338
  }
328
339
  }
@@ -330,7 +341,7 @@ class Enumerator
330
341
  end
331
342
 
332
343
  def take(n)
333
- n = Opal.coerce_to n, Integer, :to_int
344
+ n = `$coerce_to(#{n}, #{Integer}, 'to_int')`
334
345
 
335
346
  if n < 0
336
347
  raise ArgumentError, 'attempt to take negative size'
@@ -363,7 +374,7 @@ class Enumerator
363
374
  %x{
364
375
  var value = Opal.yieldX(block, args);
365
376
 
366
- if (#{Opal.truthy?(`value`)}) {
377
+ if ($truthy(value)) {
367
378
  #{enum.yield(*args)};
368
379
  }
369
380
  else {
@@ -379,4 +390,8 @@ class Enumerator
379
390
  "#<#{self.class}: #{@enumerator.inspect}>"
380
391
  end
381
392
  end
393
+
394
+ class ArithmeticSequence < self
395
+ # We need to stub this for the time being
396
+ end
382
397
  end
@@ -1,3 +1,5 @@
1
+ # helpers: truthy
2
+
1
3
  class File < IO
2
4
  Separator = SEPARATOR = '/'
3
5
  ALT_SEPARATOR = nil
@@ -77,7 +79,7 @@ class File < IO
77
79
  %x{
78
80
  // Coerce a given path to a path string using #to_path and #to_str
79
81
  function $coerce_to_path(path) {
80
- if (#{Opal.truthy?(`path`.respond_to?(:to_path))}) {
82
+ if ($truthy(#{`path`.respond_to?(:to_path)})) {
81
83
  path = path.$to_path();
82
84
  }
83
85
 
@@ -988,6 +988,9 @@ class Hash
988
988
  }
989
989
  end
990
990
 
991
+ alias filter select
992
+ alias filter! select!
993
+
991
994
  def shift
992
995
  %x{
993
996
  var keys = self.$$keys,
@@ -1046,7 +1049,9 @@ class Hash
1046
1049
  }
1047
1050
  end
1048
1051
 
1049
- def to_h
1052
+ def to_h(&block)
1053
+ return map(&block).to_h if block_given?
1054
+
1050
1055
  %x{
1051
1056
  if (self.$$class === Opal.Hash) {
1052
1057
  return self;
@@ -1,45 +1,29 @@
1
+ # helpers: type_error, coerce_to
2
+
1
3
  module Opal
2
4
  def self.bridge(constructor, klass)
3
5
  `Opal.bridge(constructor, klass)`
4
6
  end
5
7
 
6
- def self.type_error(object, type, method = nil, coerced = nil)
7
- if method && coerced
8
- TypeError.new "can't convert #{object.class} into #{type} (#{object.class}##{method} gives #{coerced.class})"
9
- else
10
- TypeError.new "no implicit conversion of #{object.class} into #{type}"
11
- end
12
- end
13
-
14
- def self.coerce_to(object, type, method)
15
- return object if type === object
16
-
17
- unless object.respond_to? method
18
- raise type_error(object, type)
19
- end
20
-
21
- object.__send__ method
22
- end
23
-
24
- def self.coerce_to!(object, type, method)
25
- coerced = coerce_to(object, type, method)
8
+ def self.coerce_to!(object, type, method, *args)
9
+ coerced = `$coerce_to(object, type, method, args)`
26
10
 
27
11
  unless type === coerced
28
- raise type_error(object, type, method, coerced)
12
+ raise `$type_error(object, type, method, coerced)`
29
13
  end
30
14
 
31
15
  coerced
32
16
  end
33
17
 
34
- def self.coerce_to?(object, type, method)
18
+ def self.coerce_to?(object, type, method, *args)
35
19
  return unless object.respond_to? method
36
20
 
37
- coerced = coerce_to(object, type, method)
21
+ coerced = `$coerce_to(object, type, method, args)`
38
22
 
39
23
  return if coerced.nil?
40
24
 
41
25
  unless type === coerced
42
- raise type_error(object, type, method, coerced)
26
+ raise `$type_error(object, type, method, coerced)`
43
27
  end
44
28
 
45
29
  coerced
@@ -90,10 +74,6 @@ module Opal
90
74
  obj.respond_to?(method, include_all)
91
75
  end
92
76
 
93
- def self.inspect_obj(obj)
94
- `Opal.inspect(obj)`
95
- end
96
-
97
77
  def self.instance_variable_name!(name)
98
78
  name = Opal.coerce_to!(name, String, :to_str)
99
79
 
@@ -31,17 +31,22 @@ class IO
31
31
  end
32
32
 
33
33
  def print(*args)
34
- write args.map { |arg| String(arg) }.join($,)
34
+ %x{
35
+ for (var i = 0, ii = args.length; i < ii; i++) {
36
+ args[i] = #{String(`args[i]`)}
37
+ }
38
+ self.$write(args.join(#{$,}));
39
+ }
35
40
  nil
36
41
  end
37
42
 
38
43
  def puts(*args)
39
- newline = $/
40
- if args.empty?
41
- write $/
42
- else
43
- write args.map { |arg| String(arg).chomp }.concat([nil]).join(newline)
44
- end
44
+ %x{
45
+ for (var i = 0, ii = args.length; i < ii; i++) {
46
+ args[i] = #{String(`args[i]`).chomp}
47
+ }
48
+ self.$write(args.concat([nil]).join(#{$/}));
49
+ }
45
50
  nil
46
51
  end
47
52
  end
@@ -1,3 +1,5 @@
1
+ # helpers: truthy, coerce_to, respond_to
2
+
1
3
  module Kernel
2
4
  def method_missing(symbol, *args, &block)
3
5
  raise NoMethodError.new("undefined method `#{symbol}' for #{inspect}", symbol, args)
@@ -44,7 +46,7 @@ module Kernel
44
46
 
45
47
  def methods(all = true)
46
48
  %x{
47
- if (#{Opal.truthy?(all)}) {
49
+ if ($truthy(#{all})) {
48
50
  return Opal.methods(self);
49
51
  } else {
50
52
  return Opal.own_methods(self);
@@ -54,7 +56,7 @@ module Kernel
54
56
 
55
57
  def public_methods(all = true)
56
58
  %x{
57
- if (#{Opal.truthy?(all)}) {
59
+ if ($truthy(#{all})) {
58
60
  return Opal.methods(self);
59
61
  } else {
60
62
  return Opal.receiver_methods(self);
@@ -87,12 +89,25 @@ module Kernel
87
89
  def at_exit(&block)
88
90
  $__at_exit__ ||= []
89
91
  $__at_exit__ << block
92
+ block
90
93
  end
91
94
 
92
- # Opal does not support #caller, but we stub it as an empty array to not
93
- # break dependant libs
94
- def caller(*args)
95
- []
95
+ def caller(start = 1, length = nil)
96
+ %x{
97
+ var stack, result
98
+
99
+ stack = (new Error().stack || "").split("\n")
100
+ result = []
101
+
102
+ // Skip the initial line ("Error:") and Kernel#caller with i=3
103
+ for (var i = 3, ii = stack.length; i < ii; i++) {
104
+ if (!stack[i].match("runtime.js")) {
105
+ result.push(stack[i].replace(/^ *\w+ +/, ''))
106
+ if (length && result.length == length) break
107
+ }
108
+ }
109
+ return result
110
+ }
96
111
  end
97
112
 
98
113
  def class
@@ -196,7 +211,7 @@ module Kernel
196
211
  if (status.$$is_boolean) {
197
212
  status = status ? 0 : 1;
198
213
  } else {
199
- status = #{Opal.coerce_to(status, Integer, :to_int)}
214
+ status = $coerce_to(status, #{Integer}, 'to_int')
200
215
  }
201
216
 
202
217
  Opal.exit(status);
@@ -335,7 +350,7 @@ module Kernel
335
350
  if (base === undefined) {
336
351
  base = 0;
337
352
  } else {
338
- base = #{Opal.coerce_to(`base`, Integer, :to_int)};
353
+ base = $coerce_to(base, #{Integer}, 'to_int');
339
354
  if (base === 1 || base < 0 || base > 36) {
340
355
  #{raise ArgumentError, "invalid radix #{base}"}
341
356
  }
@@ -501,7 +516,15 @@ module Kernel
501
516
  $stdout.print(*strs)
502
517
  end
503
518
 
504
- def warn(*strs)
519
+ def warn(*strs, uplevel: nil)
520
+ if uplevel
521
+ uplevel = Opal.coerce_to!(uplevel, Integer, :to_str)
522
+ raise ArgumentError, "negative level (#{uplevel})" if uplevel < 0
523
+ location = caller(uplevel + 2, 1).first
524
+ location = "#{location}: " if location
525
+ strs = strs.map { |s| "#{location}warning: #{s}" }
526
+ end
527
+
505
528
  $stderr.puts(*strs) unless $VERBOSE.nil? || strs.empty?
506
529
  end
507
530
 
@@ -563,23 +586,27 @@ module Kernel
563
586
  end
564
587
 
565
588
  def respond_to?(name, include_all = false)
566
- return true if respond_to_missing?(name, include_all)
567
-
568
589
  %x{
569
590
  var body = self['$' + name];
570
591
 
571
592
  if (typeof(body) === "function" && !body.$$stub) {
572
593
  return true;
573
594
  }
574
- }
575
595
 
576
- false
596
+ if (self['$respond_to_missing?'].$$pristine === true) {
597
+ return false;
598
+ } else {
599
+ return #{respond_to_missing?(name, include_all)};
600
+ }
601
+ }
577
602
  end
578
603
 
579
604
  def respond_to_missing?(method_name, include_all = false)
580
605
  false
581
606
  end
582
607
 
608
+ Opal.pristine(self, :respond_to?, :respond_to_missing?)
609
+
583
610
  def require(file)
584
611
  file = Opal.coerce_to!(file, String, :to_str)
585
612
  `Opal.require(#{file})`
@@ -634,7 +661,7 @@ module Kernel
634
661
 
635
662
  var t = get_time();
636
663
  while (get_time() - t <= seconds * 1000);
637
- return seconds;
664
+ return Math.round(seconds);
638
665
  }
639
666
  end
640
667
 
@@ -680,6 +707,10 @@ module Kernel
680
707
  return enum_for(:yield_self) { 1 } unless block_given?
681
708
  yield self
682
709
  end
710
+
711
+ alias then yield_self
712
+
713
+ Opal.pristine(self, :method_missing)
683
714
  end
684
715
 
685
716
  class Object
@@ -1,3 +1,5 @@
1
+ # helpers: coerce_to
2
+
1
3
  module Kernel
2
4
  def format(format_string, *args)
3
5
  if args.length == 1 && args[0].respond_to?(:to_ary)
@@ -487,7 +489,7 @@ module Kernel
487
489
  if (#{`arg`.respond_to?(:to_str)}) {
488
490
  str = #{`arg`.to_str};
489
491
  } else {
490
- str = String.fromCharCode(#{Opal.coerce_to(`arg`, Integer, :to_int)});
492
+ str = String.fromCharCode($coerce_to(arg, #{Integer}, 'to_int'));
491
493
  }
492
494
  if (str.length !== 1) {
493
495
  #{raise ArgumentError, '%c requires a character'}
@@ -1,3 +1,5 @@
1
+ # helpers: type_error
2
+
1
3
  module Math
2
4
  E = `Math.E`
3
5
  PI = `Math.PI`
@@ -23,13 +25,13 @@ module Math
23
25
  def self.float!(value)
24
26
  Float(value)
25
27
  rescue ArgumentError
26
- raise Opal.type_error(value, Float)
28
+ raise `$type_error(value, #{Float})`
27
29
  end
28
30
 
29
31
  def self.integer!(value)
30
32
  Integer(value)
31
33
  rescue ArgumentError
32
- raise Opal.type_error(value, Integer)
34
+ raise `$type_error(value, #{Integer})`
33
35
  end
34
36
 
35
37
  module_function
@@ -368,14 +370,14 @@ module Math
368
370
 
369
371
  def log(x, base = undefined)
370
372
  if String === x
371
- raise Opal.type_error(x, Float)
373
+ raise `$type_error(x, #{Float})`
372
374
  end
373
375
 
374
376
  if `base == null`
375
377
  Math.checked :log, Math.float!(x)
376
378
  else
377
379
  if String === base
378
- raise Opal.type_error(base, Float)
380
+ raise `$type_error(base, #{Float})`
379
381
  end
380
382
 
381
383
  Math.checked(:log, Math.float!(x)) / Math.checked(:log, Math.float!(base))
@@ -392,7 +394,7 @@ module Math
392
394
 
393
395
  def log10(x)
394
396
  if String === x
395
- raise Opal.type_error(x, Float)
397
+ raise `$type_error(x, #{Float})`
396
398
  end
397
399
 
398
400
  Math.checked :log10, Math.float!(x)
@@ -408,7 +410,7 @@ module Math
408
410
 
409
411
  def log2(x)
410
412
  if String === x
411
- raise Opal.type_error(x, Float)
413
+ raise `$type_error(x, #{Float})`
412
414
  end
413
415
 
414
416
  Math.checked :log2, Math.float!(x)