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
@@ -34,6 +34,14 @@ class Method
34
34
 
35
35
  alias [] call
36
36
 
37
+ def >>(other)
38
+ @method >> other
39
+ end
40
+
41
+ def <<(other)
42
+ @method << other
43
+ end
44
+
37
45
  def unbind
38
46
  UnboundMethod.new(@receiver.class, @owner, @method, @name)
39
47
  end
@@ -1,3 +1,5 @@
1
+ # helpers: truthy, coerce_to
2
+
1
3
  class Module
2
4
  def self.allocate
3
5
  %x{
@@ -82,6 +84,8 @@ class Module
82
84
  end
83
85
 
84
86
  def alias_method(newname, oldname)
87
+ newname = `$coerce_to(newname, #{String}, 'to_str')`
88
+ oldname = `$coerce_to(oldname, #{String}, 'to_str')`
85
89
  `Opal.alias(self, newname, oldname)`
86
90
 
87
91
  self
@@ -107,7 +111,18 @@ class Module
107
111
  attr_writer(*names)
108
112
  end
109
113
 
110
- alias attr attr_accessor
114
+ def attr(*args)
115
+ %x{
116
+ if (args.length == 2 && (args[1] === true || args[1] === false)) {
117
+ #{warn 'optional boolean argument is obsoleted', uplevel: 1}
118
+
119
+ args[1] ? #{attr_accessor(`args[0]`)} : #{attr_reader(`args[0]`)};
120
+ return nil;
121
+ }
122
+ }
123
+
124
+ attr_reader(*args)
125
+ end
111
126
 
112
127
  def attr_reader(*names)
113
128
  %x{
@@ -443,7 +458,7 @@ class Module
443
458
 
444
459
  def instance_methods(include_super = true)
445
460
  %x{
446
- if (#{Opal.truthy?(include_super)}) {
461
+ if ($truthy(#{include_super})) {
447
462
  return Opal.instance_methods(self);
448
463
  } else {
449
464
  return Opal.own_instance_methods(self);
@@ -460,7 +460,7 @@ class Number < Numeric
460
460
  end
461
461
 
462
462
  def gcdlcm(other)
463
- [gcd, lcm]
463
+ [gcd(other), lcm(other)]
464
464
  end
465
465
 
466
466
  def integer?
@@ -631,7 +631,7 @@ class Number < Numeric
631
631
  }
632
632
 
633
633
  var f = Math.pow(10, ndigits),
634
- x = Math.floor((Math.abs(x) + f / 2) / f) * f;
634
+ x = Math.floor((Math.abs(self) + f / 2) / f) * f;
635
635
 
636
636
  return self < 0 ? -x : x;
637
637
  }
@@ -1010,6 +1010,7 @@ Fixnum = Number
1010
1010
 
1011
1011
  class Integer < Numeric
1012
1012
  `self.$$is_number_class = true`
1013
+ `self.$$is_integer_class = true`
1013
1014
 
1014
1015
  class << self
1015
1016
  def allocate
@@ -1018,16 +1019,6 @@ class Integer < Numeric
1018
1019
 
1019
1020
  undef :new
1020
1021
 
1021
- def ===(other)
1022
- %x{
1023
- if (!other.$$is_number) {
1024
- return false;
1025
- }
1026
-
1027
- return (other % 1) === 0;
1028
- }
1029
- end
1030
-
1031
1022
  def sqrt(n)
1032
1023
  n = Opal.coerce_to!(n, Integer, :to_int)
1033
1024
  %x{
@@ -1,3 +1,5 @@
1
+ # helpers: slice
2
+
1
3
  class Proc < `Function`
2
4
  `Opal.defineProperty(self.$$prototype, '$$is_proc', true)`
3
5
  `Opal.defineProperty(self.$$prototype, '$$is_lambda', false)`
@@ -52,6 +54,20 @@ class Proc < `Function`
52
54
  alias === call
53
55
  alias yield call
54
56
 
57
+ def >>(other)
58
+ proc do |*args, &block|
59
+ out = call(*args, &block)
60
+ other.call(out)
61
+ end
62
+ end
63
+
64
+ def <<(other)
65
+ proc do |*args, &block|
66
+ out = other.call(*args, &block)
67
+ call(out)
68
+ end
69
+ end
70
+
55
71
  def to_proc
56
72
  self
57
73
  end
@@ -0,0 +1,147 @@
1
+ # This is based on an adaptation of Makoto Matsumoto and Takuji Nishimura's code
2
+ # done by Sean McCullough <banksean@gmail.com> and Dave Heitzman
3
+ # <daveheitzman@yahoo.com>, subsequently readapted from an updated version of
4
+ # ruby's random.c (rev c38a183032a7826df1adabd8aa0725c713d53e1c).
5
+ #
6
+ # The original copyright notice from random.c follows.
7
+ #
8
+ # This is based on trimmed version of MT19937. To get the original version,
9
+ # contact <http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html>.
10
+ #
11
+ # The original copyright notice follows.
12
+ #
13
+ # A C-program for MT19937, with initialization improved 2002/2/10.
14
+ # Coded by Takuji Nishimura and Makoto Matsumoto.
15
+ # This is a faster version by taking Shawn Cokus's optimization,
16
+ # Matthe Bellew's simplification, Isaku Wada's real version.
17
+ #
18
+ # Before using, initialize the state by using init_genrand(mt, seed)
19
+ # or init_by_array(mt, init_key, key_length).
20
+ #
21
+ # Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
22
+ # All rights reserved.
23
+ #
24
+ # Redistribution and use in source and binary forms, with or without
25
+ # modification, are permitted provided that the following conditions
26
+ # are met:
27
+ #
28
+ # 1. Redistributions of source code must retain the above copyright
29
+ # notice, this list of conditions and the following disclaimer.
30
+ #
31
+ # 2. Redistributions in binary form must reproduce the above copyright
32
+ # notice, this list of conditions and the following disclaimer in the
33
+ # documentation and/or other materials provided with the distribution.
34
+ #
35
+ # 3. The names of its contributors may not be used to endorse or promote
36
+ # products derived from this software without specific prior written
37
+ # permission.
38
+ #
39
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
40
+ # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
41
+ # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
42
+ # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
43
+ # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
44
+ # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
45
+ # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
46
+ # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
47
+ # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
48
+ # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
49
+ # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50
+ #
51
+ #
52
+ # Any feedback is very welcome.
53
+ # http://www.math.keio.ac.jp/matumoto/emt.html
54
+ # email: matumoto@math.keio.ac.jp
55
+ mersenne_twister = %x{(function() {
56
+ /* Period parameters */
57
+ var N = 624;
58
+ var M = 397;
59
+ var MATRIX_A = 0x9908b0df; /* constant vector a */
60
+ var UMASK = 0x80000000; /* most significant w-r bits */
61
+ var LMASK = 0x7fffffff; /* least significant r bits */
62
+ var MIXBITS = function(u,v) { return ( ((u) & UMASK) | ((v) & LMASK) ); };
63
+ var TWIST = function(u,v) { return (MIXBITS((u),(v)) >>> 1) ^ ((v & 0x1) ? MATRIX_A : 0x0); };
64
+
65
+ function init(s) {
66
+ var mt = {left: 0, next: N, state: new Array(N)};
67
+ init_genrand(mt, s);
68
+ return mt;
69
+ }
70
+
71
+ /* initializes mt[N] with a seed */
72
+ function init_genrand(mt, s) {
73
+ var j, i;
74
+ mt.state[0] = s >>> 0;
75
+ for (j=1; j<N; j++) {
76
+ mt.state[j] = (1812433253 * ((mt.state[j-1] ^ (mt.state[j-1] >> 30) >>> 0)) + j);
77
+ /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
78
+ /* In the previous versions, MSBs of the seed affect */
79
+ /* only MSBs of the array state[]. */
80
+ /* 2002/01/09 modified by Makoto Matsumoto */
81
+ mt.state[j] &= 0xffffffff; /* for >32 bit machines */
82
+ }
83
+ mt.left = 1;
84
+ mt.next = N;
85
+ }
86
+
87
+ /* generate N words at one time */
88
+ function next_state(mt) {
89
+ var p = 0, _p = mt.state;
90
+ var j;
91
+
92
+ mt.left = N;
93
+ mt.next = 0;
94
+
95
+ for (j=N-M+1; --j; p++)
96
+ _p[p] = _p[p+(M)] ^ TWIST(_p[p+(0)], _p[p+(1)]);
97
+
98
+ for (j=M; --j; p++)
99
+ _p[p] = _p[p+(M-N)] ^ TWIST(_p[p+(0)], _p[p+(1)]);
100
+
101
+ _p[p] = _p[p+(M-N)] ^ TWIST(_p[p+(0)], _p[0]);
102
+ }
103
+
104
+ /* generates a random number on [0,0xffffffff]-interval */
105
+ function genrand_int32(mt) {
106
+ /* mt must be initialized */
107
+ var y;
108
+
109
+ if (--mt.left <= 0) next_state(mt);
110
+ y = mt.state[mt.next++];
111
+
112
+ /* Tempering */
113
+ y ^= (y >>> 11);
114
+ y ^= (y << 7) & 0x9d2c5680;
115
+ y ^= (y << 15) & 0xefc60000;
116
+ y ^= (y >>> 18);
117
+
118
+ return y >>> 0;
119
+ }
120
+
121
+ function int_pair_to_real_exclusive(a, b) {
122
+ a >>>= 5;
123
+ b >>>= 6;
124
+ return(a*67108864.0+b)*(1.0/9007199254740992.0);
125
+ }
126
+
127
+ // generates a random number on [0,1) with 53-bit resolution
128
+ function genrand_real(mt) {
129
+ /* mt must be initialized */
130
+ var a = genrand_int32(mt), b = genrand_int32(mt);
131
+ return int_pair_to_real_exclusive(a, b);
132
+ }
133
+
134
+ return { genrand_real: genrand_real, init: init };
135
+ })()}
136
+
137
+ class Random
138
+ `var MAX_INT = Number.MAX_SAFE_INTEGER || Math.pow(2, 53) - 1`
139
+
140
+ MERSENNE_TWISTER_GENERATOR = `{
141
+ new_seed: function() { return Math.round(Math.random() * MAX_INT); },
142
+ reseed: function(seed) { return mersenne_twister.init(seed); },
143
+ rand: function(mt) { return mersenne_twister.genrand_real(mt); }
144
+ }`
145
+
146
+ self.generator = MERSENNE_TWISTER_GENERATOR
147
+ end
@@ -16,18 +16,6 @@ class Range
16
16
  @excl = exclude
17
17
  end
18
18
 
19
- def ==(other)
20
- %x{
21
- if (!other.$$is_range) {
22
- return false;
23
- }
24
-
25
- return self.excl === other.excl &&
26
- self.begin == other.begin &&
27
- self.end == other.end;
28
- }
29
- end
30
-
31
19
  def ===(value)
32
20
  include? value
33
21
  end
@@ -44,7 +32,7 @@ class Range
44
32
  end
45
33
 
46
34
  def each(&block)
47
- return enum_for :each unless block_given?
35
+ return enum_for(:each) { size } unless block_given?
48
36
 
49
37
  %x{
50
38
  var i, limit;
@@ -93,6 +81,8 @@ class Range
93
81
  @end.eql?(other.end)
94
82
  end
95
83
 
84
+ alias == eql?
85
+
96
86
  def exclude_end?
97
87
  @excl
98
88
  end
@@ -1,3 +1,5 @@
1
+ # helpers: coerce_to
2
+
1
3
  class RegexpError < StandardError; end
2
4
 
3
5
  class Regexp < `RegExp`
@@ -100,6 +102,8 @@ class Regexp < `RegExp`
100
102
  return new RegExp(regexp, options);
101
103
  }
102
104
  end
105
+
106
+ alias compile new
103
107
  end
104
108
 
105
109
  def ==(other)
@@ -162,7 +166,7 @@ class Regexp < `RegExp`
162
166
 
163
167
  if (pos === undefined) {
164
168
  if (string === nil) return #{$~ = nil};
165
- var m = self.exec(#{Opal.coerce_to(string, String, :to_str)});
169
+ var m = self.exec($coerce_to(string, #{String}, 'to_str'));
166
170
  if (m) {
167
171
  #{$~ = MatchData.new(`self`, `m`)};
168
172
  return block === nil ? #{$~} : #{yield $~};
@@ -171,13 +175,13 @@ class Regexp < `RegExp`
171
175
  }
172
176
  }
173
177
 
174
- pos = #{Opal.coerce_to(pos, Integer, :to_int)};
178
+ pos = $coerce_to(pos, #{Integer}, 'to_int');
175
179
 
176
180
  if (string === nil) {
177
181
  return #{$~ = nil};
178
182
  }
179
183
 
180
- string = #{Opal.coerce_to(string, String, :to_str)};
184
+ string = $coerce_to(string, #{String}, 'to_str');
181
185
 
182
186
  if (pos < 0) {
183
187
  pos += string.length;
@@ -210,16 +214,16 @@ class Regexp < `RegExp`
210
214
  }
211
215
 
212
216
  if (pos === undefined) {
213
- return string === nil ? false : self.test(#{Opal.coerce_to(string, String, :to_str)});
217
+ return string === nil ? false : self.test($coerce_to(string, #{String}, 'to_str'));
214
218
  }
215
219
 
216
- pos = #{Opal.coerce_to(pos, Integer, :to_int)};
220
+ pos = $coerce_to(pos, #{Integer}, 'to_int');
217
221
 
218
222
  if (string === nil) {
219
223
  return false;
220
224
  }
221
225
 
222
- string = #{Opal.coerce_to(string, String, :to_str)};
226
+ string = $coerce_to(string, #{String}, 'to_str');
223
227
 
224
228
  if (pos < 0) {
225
229
  pos += string.length;
@@ -1,4 +1,6 @@
1
- (function(undefined) {
1
+ (function(global_object) {
2
+ "use strict";
3
+
2
4
  // @note
3
5
  // A few conventions for the documentation of this file:
4
6
  // 1. Always use "//" (in contrast with "/**/")
@@ -13,11 +15,12 @@
13
15
  // The way the code is digested before going through Yardoc is a secret kept
14
16
  // in the docs repo (https://github.com/opal/docs/tree/master).
15
17
 
16
- var global_object = this, console;
18
+ var console;
17
19
 
18
20
  // Detect the global object
19
- if (typeof(global) !== 'undefined') { global_object = global; }
20
- if (typeof(window) !== 'undefined') { global_object = window; }
21
+ if (typeof(globalThis) !== 'undefined') { global_object = globalThis; }
22
+ else if (typeof(global) !== 'undefined') { global_object = global; }
23
+ else if (typeof(window) !== 'undefined') { global_object = window; }
21
24
 
22
25
  // Setup a dummy console object if missing
23
26
  if (typeof(global_object.console) === 'object') {
@@ -31,9 +34,9 @@
31
34
  if (!('log' in console)) { console.log = function () {}; }
32
35
  if (!('warn' in console)) { console.warn = console.log; }
33
36
 
34
- if (typeof(this.Opal) !== 'undefined') {
37
+ if (typeof(global_object.Opal) !== 'undefined') {
35
38
  console.warn('Opal already loaded. Loading twice can cause troubles, please fix your setup.');
36
- return this.Opal;
39
+ return global_object.Opal;
37
40
  }
38
41
 
39
42
  var nil;
@@ -52,13 +55,13 @@
52
55
  var Class;
53
56
 
54
57
  // The Opal object that is exposed globally
55
- var Opal = this.Opal = {};
58
+ var Opal = global_object.Opal = {};
56
59
 
57
60
  // This is a useful reference to global object inside ruby files
58
61
  Opal.global = global_object;
59
62
  global_object.Opal = Opal;
60
63
 
61
- // Configure runtime behavior with regards to require and unsupported fearures
64
+ // Configure runtime behavior with regards to require and unsupported features
62
65
  Opal.config = {
63
66
  missing_require_severity: 'error', // error, warning, ignore
64
67
  unsupported_features_severity: 'warning', // error, warning, ignore
@@ -66,11 +69,11 @@
66
69
  };
67
70
 
68
71
  // Minify common function calls
69
- var $hasOwn = Object.hasOwnProperty;
70
- var $bind = Function.prototype.bind;
71
- var $setPrototype = Object.setPrototypeOf;
72
- var $slice = Array.prototype.slice;
73
- var $splice = Array.prototype.splice;
72
+ var $has_own = Object.hasOwnProperty;
73
+ var $bind = Function.prototype.bind;
74
+ var $set_proto = Object.setPrototypeOf;
75
+ var $slice = Array.prototype.slice;
76
+ var $splice = Array.prototype.splice;
74
77
 
75
78
  // Nil object id is always 4
76
79
  var nil_id = 4;
@@ -135,7 +138,7 @@
135
138
  // String class is the only class that:
136
139
  // + compiles to JS primitive
137
140
  // + allows method definition directly on instances
138
- // numbers, true, false and nil do not support it.
141
+ // numbers, true, false and null do not support it.
139
142
  object[name] = initialValue;
140
143
  } else {
141
144
  Object.defineProperty(object, name, {
@@ -152,7 +155,7 @@
152
155
  Opal.slice = $slice;
153
156
 
154
157
 
155
- // Truth
158
+ // Helpers
156
159
  // -----
157
160
 
158
161
  Opal.truthy = function(val) {
@@ -163,6 +166,49 @@
163
166
  return (val === nil || val == null || (val.$$is_boolean && val == false))
164
167
  };
165
168
 
169
+ Opal.type_error = function(object, type, method, coerced) {
170
+ object = object.$$class;
171
+
172
+ if (coerced && method) {
173
+ coerced = coerced.$$class;
174
+ return Opal.TypeError.$new(
175
+ "can't convert " + object + " into " + type +
176
+ " (" + object + "#" + method + " gives " + coerced + ")"
177
+ )
178
+ } else {
179
+ return Opal.TypeError.$new(
180
+ "no implicit conversion of " + object + " into " + type
181
+ )
182
+ }
183
+ };
184
+
185
+ Opal.coerce_to = function(object, type, method, args) {
186
+ if (type['$==='](object)) return object;
187
+
188
+ if (!object['$respond_to?'](method)) {
189
+ throw Opal.type_error(object, type);
190
+ }
191
+
192
+ if (args == null) args = [];
193
+ return Opal.send(object, method, args);
194
+ }
195
+
196
+ Opal.respond_to = function(obj, jsid, include_all) {
197
+ if (obj == null || !obj.$$class) return false;
198
+ include_all = !!include_all;
199
+ var body = obj[jsid];
200
+
201
+ if (obj['$respond_to?'].$$pristine) {
202
+ if (obj['$respond_to_missing?'].$$pristine) {
203
+ return typeof(body) === "function" && !body.$$stub;
204
+ } else {
205
+ return Opal.send(obj, obj['$respond_to_missing?'], [jsid.substr(1), include_all]);
206
+ }
207
+ } else {
208
+ return Opal.send(obj, obj['$respond_to?'], [jsid.substr(1), include_all]);
209
+ }
210
+ }
211
+
166
212
 
167
213
  // Constants
168
214
  // ---------
@@ -183,7 +229,7 @@
183
229
 
184
230
  // Walk up the nesting array looking for the constant
185
231
  function const_lookup_nesting(nesting, name) {
186
- var i, ii, result, constant;
232
+ var i, ii, constant;
187
233
 
188
234
  if (nesting.length === 0) return;
189
235
 
@@ -197,14 +243,14 @@
197
243
 
198
244
  // Walk up the ancestors chain looking for the constant
199
245
  function const_lookup_ancestors(cref, name) {
200
- var i, ii, result, ancestors;
246
+ var i, ii, ancestors;
201
247
 
202
248
  if (cref == null) return;
203
249
 
204
250
  ancestors = Opal.ancestors(cref);
205
251
 
206
252
  for (i = 0, ii = ancestors.length; i < ii; i++) {
207
- if (ancestors[i].$$const && $hasOwn.call(ancestors[i].$$const, name)) {
253
+ if (ancestors[i].$$const && $has_own.call(ancestors[i].$$const, name)) {
208
254
  return ancestors[i].$$const[name];
209
255
  }
210
256
  }
@@ -333,7 +379,7 @@
333
379
  Opal.constants = function(cref, inherit) {
334
380
  if (inherit == null) inherit = true;
335
381
 
336
- var module, modules = [cref], module_constants, i, ii, constants = {}, constant;
382
+ var module, modules = [cref], i, ii, constants = {}, constant;
337
383
 
338
384
  if (inherit) modules = modules.concat(Opal.ancestors(cref));
339
385
  if (inherit && cref.$$is_module) modules = modules.concat([Opal.Object]).concat(Opal.ancestors(Opal.Object));
@@ -341,7 +387,7 @@
341
387
  for (i = 0, ii = modules.length; i < ii; i++) {
342
388
  module = modules[i];
343
389
 
344
- // Don not show Objects constants unless we're querying Object itself
390
+ // Do not show Objects constants unless we're querying Object itself
345
391
  if (cref !== _Object && module == _Object) break;
346
392
 
347
393
  for (constant in module.$$const) {
@@ -370,6 +416,10 @@
370
416
  throw Opal.NameError.$new("constant "+cref+"::"+cref.$name()+" not defined");
371
417
  };
372
418
 
419
+ // Setup some shortcuts to reduce compiled size
420
+ Opal.$$ = Opal.const_get_relative;
421
+ Opal.$$$ = Opal.const_get_qualified;
422
+
373
423
 
374
424
  // Modules & Classes
375
425
  // -----------------
@@ -410,7 +460,7 @@
410
460
  self = new ($bind.apply(superclass.$$constructor, [null].concat(args)))();
411
461
 
412
462
  // and replacing a __proto__ manually
413
- $setPrototype(self, klass.$$prototype);
463
+ $set_proto(self, klass.$$prototype);
414
464
  return self;
415
465
  }
416
466
  } else {
@@ -443,11 +493,11 @@
443
493
  // Later singleton methods generate a singleton_class
444
494
  // and inject it into ancestors chain
445
495
  if (Opal.Class) {
446
- $setPrototype(klass, Opal.Class.prototype);
496
+ $set_proto(klass, Opal.Class.prototype);
447
497
  }
448
498
 
449
499
  if (superclass != null) {
450
- $setPrototype(klass.$$prototype, superclass.$$prototype);
500
+ $set_proto(klass.$$prototype, superclass.$$prototype);
451
501
 
452
502
  if (superclass.$$meta) {
453
503
  // If superclass has metaclass then we have explicitely inherit it.
@@ -572,7 +622,7 @@
572
622
  $defineProperty(module, '$$ancestors', [module]);
573
623
  $defineProperty(module, '$$ancestors_cache_version', null);
574
624
 
575
- $setPrototype(module, Opal.Module.prototype);
625
+ $set_proto(module, Opal.Module.prototype);
576
626
 
577
627
  return module;
578
628
  };
@@ -633,7 +683,7 @@
633
683
  if (object.hasOwnProperty('$$is_class')) {
634
684
  return Opal.build_class_singleton_class(object);
635
685
  } else if (object.hasOwnProperty('$$is_module')) {
636
- return Opal.build_module_singletin_class(object);
686
+ return Opal.build_module_singleton_class(object);
637
687
  } else {
638
688
  return Opal.build_object_singleton_class(object);
639
689
  }
@@ -665,14 +715,14 @@
665
715
  $defineProperty(meta, '$$is_singleton', true);
666
716
  $defineProperty(meta, '$$singleton_of', klass);
667
717
  $defineProperty(klass, '$$meta', meta);
668
- $setPrototype(klass, meta.$$prototype);
718
+ $set_proto(klass, meta.$$prototype);
669
719
  // Restoring ClassName.class
670
720
  $defineProperty(klass, '$$class', Opal.Class);
671
721
 
672
722
  return meta;
673
723
  };
674
724
 
675
- Opal.build_module_singletin_class = function(mod) {
725
+ Opal.build_module_singleton_class = function(mod) {
676
726
  if (mod.$$meta) {
677
727
  return mod.$$meta;
678
728
  }
@@ -682,7 +732,7 @@
682
732
  $defineProperty(meta, '$$is_singleton', true);
683
733
  $defineProperty(meta, '$$singleton_of', mod);
684
734
  $defineProperty(mod, '$$meta', meta);
685
- $setPrototype(mod, meta.$$prototype);
735
+ $set_proto(mod, meta.$$prototype);
686
736
  // Restoring ModuleName.class
687
737
  $defineProperty(mod, '$$class', Opal.Module);
688
738
 
@@ -704,7 +754,7 @@
704
754
 
705
755
  $defineProperty(object, '$$meta', klass);
706
756
 
707
- $setPrototype(object, object.$$meta.$$prototype);
757
+ $set_proto(object, object.$$meta.$$prototype);
708
758
 
709
759
  return klass;
710
760
  };
@@ -823,7 +873,7 @@
823
873
  for (i = length - 2; i >= 0; i--) {
824
874
  var ancestor = ancestors[i];
825
875
 
826
- if ($hasOwn.call(ancestor.$$cvars, name)) {
876
+ if ($has_own.call(ancestor.$$cvars, name)) {
827
877
  ancestor.$$cvars[name] = value;
828
878
  return value;
829
879
  }
@@ -969,8 +1019,8 @@
969
1019
  end_chain_on = next_ancestor;
970
1020
  }
971
1021
 
972
- $setPrototype(start_chain_after, chain.first);
973
- $setPrototype(chain.last, end_chain_on);
1022
+ $set_proto(start_chain_after, chain.first);
1023
+ $set_proto(chain.last, end_chain_on);
974
1024
 
975
1025
  // recalculate own_included_modules cache
976
1026
  includer.$$own_included_modules = own_included_modules(includer);
@@ -1029,8 +1079,8 @@
1029
1079
  // dummy(prepender) -> previous_parent
1030
1080
  // to
1031
1081
  // dummy(prepender) -> iclass(prepender) -> previous_parent
1032
- $setPrototype(dummy_prepender, prepender_iclass);
1033
- $setPrototype(prepender_iclass, previous_parent);
1082
+ $set_proto(dummy_prepender, prepender_iclass);
1083
+ $set_proto(prepender_iclass, previous_parent);
1034
1084
  }
1035
1085
 
1036
1086
  var prepender_ancestors = Opal.ancestors(prepender);
@@ -1057,8 +1107,8 @@
1057
1107
  throw Opal.RuntimeError.$new("Prepending a module multiple times is not supported");
1058
1108
  }
1059
1109
 
1060
- $setPrototype(start_chain_after, chain.first);
1061
- $setPrototype(chain.last, end_chain_on);
1110
+ $set_proto(start_chain_after, chain.first);
1111
+ $set_proto(chain.last, end_chain_on);
1062
1112
 
1063
1113
  // recalculate own_prepended_modules cache
1064
1114
  prepender.$$own_prepended_modules = own_prepended_modules(prepender);
@@ -1124,7 +1174,7 @@
1124
1174
 
1125
1175
  for (var i = 1; i < length; i++) {
1126
1176
  var current = iclasses[i];
1127
- $setPrototype(previous, current);
1177
+ $set_proto(previous, current);
1128
1178
  previous = current;
1129
1179
  }
1130
1180
 
@@ -1154,12 +1204,6 @@
1154
1204
  throw Opal.ArgumentError.$new("already bridged");
1155
1205
  }
1156
1206
 
1157
- var klass_to_inject, klass_reference;
1158
-
1159
- klass_to_inject = klass.$$super || Opal.Object;
1160
- klass_reference = klass;
1161
- var original_prototype = klass.$$prototype;
1162
-
1163
1207
  // constructor is a JS function with a prototype chain like:
1164
1208
  // - constructor
1165
1209
  // - super
@@ -1176,7 +1220,7 @@
1176
1220
  // - null
1177
1221
  //
1178
1222
  $defineProperty(native_klass, '$$bridge', klass);
1179
- $setPrototype(native_klass.prototype, (klass.$$super || Opal.Object).$$prototype);
1223
+ $set_proto(native_klass.prototype, (klass.$$super || Opal.Object).$$prototype);
1180
1224
  $defineProperty(klass, '$$prototype', native_klass.prototype);
1181
1225
 
1182
1226
  $defineProperty(klass.$$prototype, '$$class', klass);
@@ -1296,7 +1340,10 @@
1296
1340
  // @param method_name [String] The js-name of the method to stub (e.g. "$foo")
1297
1341
  // @return [undefined]
1298
1342
  Opal.stub_for = function(method_name) {
1343
+
1299
1344
  function method_missing_stub() {
1345
+ /* jshint validthis: true */
1346
+
1300
1347
  // Copy any given block onto the method_missing dispatcher
1301
1348
  this.$method_missing.$$p = method_missing_stub.$$p;
1302
1349
 
@@ -1352,7 +1399,7 @@
1352
1399
  };
1353
1400
 
1354
1401
  // Super dispatcher
1355
- Opal.find_super_dispatcher = function(obj, mid, current_func, defcheck, defs) {
1402
+ Opal.find_super_dispatcher = function(obj, mid, current_func, defcheck, allow_stubs) {
1356
1403
  var jsid = '$' + mid, ancestors, super_method;
1357
1404
 
1358
1405
  if (obj.hasOwnProperty('$$meta')) {
@@ -1372,21 +1419,17 @@
1372
1419
  }
1373
1420
 
1374
1421
  if (proto.hasOwnProperty(jsid)) {
1375
- var method = proto[jsid];
1376
-
1377
- if (!method.$$stub) {
1378
- super_method = method;
1379
- }
1422
+ super_method = proto[jsid];
1380
1423
  break;
1381
1424
  }
1382
1425
  }
1383
1426
 
1384
- if (!defcheck && super_method == null && Opal.Kernel.$method_missing === obj.$method_missing) {
1427
+ if (!defcheck && super_method && super_method.$$stub && obj.$method_missing.$$pristine) {
1385
1428
  // method_missing hasn't been explicitly defined
1386
1429
  throw Opal.NoMethodError.$new('super: no superclass method `'+mid+"' for "+obj, mid);
1387
1430
  }
1388
1431
 
1389
- return super_method;
1432
+ return (super_method.$$stub && !allow_stubs) ? null : super_method;
1390
1433
  };
1391
1434
 
1392
1435
  // Iter dispatcher for super in a block
@@ -1505,7 +1548,7 @@
1505
1548
  }
1506
1549
 
1507
1550
  if (object.$$is_number && klass.$$is_number_class) {
1508
- return true;
1551
+ return (klass.$$is_integer_class) ? (object % 1) === 0 : true;
1509
1552
  }
1510
1553
 
1511
1554
  var i, length, ancestors = Opal.ancestors(object.$$is_class ? Opal.get_singleton_class(object) : (object.$$meta || object.$$class));
@@ -1601,7 +1644,7 @@
1601
1644
  //
1602
1645
  Opal.extract_kwargs = function(parameters) {
1603
1646
  var kwargs = parameters[parameters.length - 1];
1604
- if (kwargs != null && kwargs['$respond_to?']('to_hash', true)) {
1647
+ if (kwargs != null && Opal.respond_to(kwargs, '$to_hash', true)) {
1605
1648
  $splice.call(parameters, parameters.length - 1, 1);
1606
1649
  return kwargs.$to_hash();
1607
1650
  }
@@ -1662,16 +1705,28 @@
1662
1705
  // @param block [Function] ruby block
1663
1706
  // @return [Object] returning value of the method call
1664
1707
  Opal.send = function(recv, method, args, block) {
1665
- var body = (typeof(method) === 'string') ? recv['$'+method] : method;
1708
+ var body;
1666
1709
 
1667
- if (body != null) {
1668
- if (typeof block === 'function') {
1669
- body.$$p = block;
1670
- }
1671
- return body.apply(recv, args);
1710
+ if (typeof(method) === 'function') {
1711
+ body = method;
1712
+ method = null;
1713
+ } else if (typeof(method) === 'string') {
1714
+ body = recv['$'+method];
1715
+ } else {
1716
+ throw Opal.NameError.$new("Passed method should be a string or a function");
1672
1717
  }
1673
1718
 
1674
- return recv.$method_missing.apply(recv, [method].concat(args));
1719
+ return Opal.send2(recv, body, method, args, block);
1720
+ };
1721
+
1722
+ Opal.send2 = function(recv, body, method, args, block) {
1723
+ if (body == null && method != null && recv.$method_missing) {
1724
+ body = recv.$method_missing;
1725
+ args = [method].concat(args);
1726
+ }
1727
+
1728
+ if (typeof block === 'function') body.$$p = block;
1729
+ return body.apply(recv, args);
1675
1730
  };
1676
1731
 
1677
1732
  Opal.lambda = function(block) {
@@ -1771,7 +1826,7 @@
1771
1826
 
1772
1827
  // Called from #remove_method.
1773
1828
  Opal.rdef = function(obj, jsid) {
1774
- if (!$hasOwn.call(obj.$$prototype, jsid)) {
1829
+ if (!$has_own.call(obj.$$prototype, jsid)) {
1775
1830
  throw Opal.NameError.$new("method '" + jsid.substr(1) + "' not defined in " + obj.$name());
1776
1831
  }
1777
1832
 
@@ -1847,7 +1902,7 @@
1847
1902
  if (body.$$alias_of) body = body.$$alias_of;
1848
1903
 
1849
1904
  // We need a wrapper because otherwise properties
1850
- // would be ovrewritten on the original body.
1905
+ // would be overwritten on the original body.
1851
1906
  alias = function() {
1852
1907
  var block = alias.$$p, args, i, ii;
1853
1908
 
@@ -1861,9 +1916,17 @@
1861
1916
  return Opal.send(this, body, args, block);
1862
1917
  };
1863
1918
 
1919
+ // Assign the 'length' value with defineProperty because
1920
+ // in strict mode the property is not writable.
1921
+ // It doesn't work in older browsers (like Chrome 38), where
1922
+ // an exception is thrown breaking Opal altogether.
1923
+ try {
1924
+ Object.defineProperty(alias, 'length', { value: body.length });
1925
+ } catch (e) {}
1926
+
1864
1927
  // Try to make the browser pick the right name
1865
1928
  alias.displayName = name;
1866
- alias.length = body.length;
1929
+
1867
1930
  alias.$$arity = body.$$arity;
1868
1931
  alias.$$parameters = body.$$parameters;
1869
1932
  alias.$$source_location = body.$$source_location;
@@ -1918,7 +1981,7 @@
1918
1981
 
1919
1982
  Opal.hash_put = function(hash, key, value) {
1920
1983
  if (key.$$is_string) {
1921
- if (!$hasOwn.call(hash.$$smap, key)) {
1984
+ if (!$has_own.call(hash.$$smap, key)) {
1922
1985
  hash.$$keys.push(key);
1923
1986
  }
1924
1987
  hash.$$smap[key] = value;
@@ -1928,7 +1991,7 @@
1928
1991
  var key_hash, bucket, last_bucket;
1929
1992
  key_hash = hash.$$by_identity ? Opal.id(key) : key.$hash();
1930
1993
 
1931
- if (!$hasOwn.call(hash.$$map, key_hash)) {
1994
+ if (!$has_own.call(hash.$$map, key_hash)) {
1932
1995
  bucket = {key: key, key_hash: key_hash, value: value};
1933
1996
  hash.$$keys.push(bucket);
1934
1997
  hash.$$map[key_hash] = bucket;
@@ -1956,7 +2019,7 @@
1956
2019
 
1957
2020
  Opal.hash_get = function(hash, key) {
1958
2021
  if (key.$$is_string) {
1959
- if ($hasOwn.call(hash.$$smap, key)) {
2022
+ if ($has_own.call(hash.$$smap, key)) {
1960
2023
  return hash.$$smap[key];
1961
2024
  }
1962
2025
  return;
@@ -1965,7 +2028,7 @@
1965
2028
  var key_hash, bucket;
1966
2029
  key_hash = hash.$$by_identity ? Opal.id(key) : key.$hash();
1967
2030
 
1968
- if ($hasOwn.call(hash.$$map, key_hash)) {
2031
+ if ($has_own.call(hash.$$map, key_hash)) {
1969
2032
  bucket = hash.$$map[key_hash];
1970
2033
 
1971
2034
  while (bucket) {
@@ -1981,7 +2044,9 @@
1981
2044
  var i, keys = hash.$$keys, length = keys.length, value;
1982
2045
 
1983
2046
  if (key.$$is_string) {
1984
- if (!$hasOwn.call(hash.$$smap, key)) {
2047
+ if (typeof key !== "string") key = key.valueOf();
2048
+
2049
+ if (!$has_own.call(hash.$$smap, key)) {
1985
2050
  return;
1986
2051
  }
1987
2052
 
@@ -1999,7 +2064,7 @@
1999
2064
 
2000
2065
  var key_hash = key.$hash();
2001
2066
 
2002
- if (!$hasOwn.call(hash.$$map, key_hash)) {
2067
+ if (!$has_own.call(hash.$$map, key_hash)) {
2003
2068
  return;
2004
2069
  }
2005
2070
 
@@ -2074,7 +2139,7 @@
2074
2139
 
2075
2140
  hash.$$keys[i].key_hash = key_hash;
2076
2141
 
2077
- if (!$hasOwn.call(hash.$$map, key_hash)) {
2142
+ if (!$has_own.call(hash.$$map, key_hash)) {
2078
2143
  hash.$$map[key_hash] = hash.$$keys[i];
2079
2144
  continue;
2080
2145
  }
@@ -2128,7 +2193,7 @@
2128
2193
  if (arguments_length === 1) {
2129
2194
  args = arguments[0];
2130
2195
  for (key in args) {
2131
- if ($hasOwn.call(args, key)) {
2196
+ if ($has_own.call(args, key)) {
2132
2197
  value = args[key];
2133
2198
 
2134
2199
  Opal.hash_put(hash, key, value);
@@ -2257,6 +2322,33 @@
2257
2322
  return result;
2258
2323
  };
2259
2324
 
2325
+ // Combine multiple regexp parts together
2326
+ Opal.regexp = function(parts, flags) {
2327
+ var part;
2328
+ var ignoreCase = typeof flags !== 'undefined' && flags && flags.indexOf('i') >= 0;
2329
+
2330
+ for (var i = 0, ii = parts.length; i < ii; i++) {
2331
+ part = parts[i];
2332
+ if (part instanceof RegExp) {
2333
+ if (part.ignoreCase !== ignoreCase)
2334
+ Opal.Kernel.$warn(
2335
+ "ignore case doesn't match for " + part.source.$inspect(),
2336
+ Opal.hash({uplevel: 1})
2337
+ )
2338
+
2339
+ part = part.source;
2340
+ }
2341
+ if (part === '') part = '(?:' + part + ')';
2342
+ parts[i] = part;
2343
+ }
2344
+
2345
+ if (flags) {
2346
+ return new RegExp(parts.join(''), flags);
2347
+ } else {
2348
+ return new RegExp(parts.join(''));
2349
+ }
2350
+ };
2351
+
2260
2352
  // Require system
2261
2353
  // --------------
2262
2354
 
@@ -2340,6 +2432,37 @@
2340
2432
  };
2341
2433
 
2342
2434
 
2435
+ // Strings
2436
+ // -------
2437
+
2438
+ Opal.encodings = Object.create(null);
2439
+
2440
+ // Sets the encoding on a string, will treat string literals as frozen strings
2441
+ // raising a FrozenError.
2442
+ // @param str [String] the string on which the encoding should be set.
2443
+ // @param name [String] the canonical name of the encoding
2444
+ Opal.set_encoding = function(str, name) {
2445
+ if (typeof str === 'string')
2446
+ throw Opal.FrozenError.$new("can't modify frozen String");
2447
+
2448
+ var encoding = Opal.encodings[name];
2449
+
2450
+ if (encoding === str.encoding) { return str; }
2451
+
2452
+ str.encoding = encoding;
2453
+
2454
+ return str;
2455
+ };
2456
+
2457
+ // @returns a String object with the encoding set from a string literal
2458
+ Opal.enc = function(str, name) {
2459
+ var dup = new String(str);
2460
+ Opal.set_encoding(dup, name);
2461
+ dup.internal_encoding = dup.encoding;
2462
+ return dup
2463
+ }
2464
+
2465
+
2343
2466
  // Initialization
2344
2467
  // --------------
2345
2468
  function $BasicObject() {}
@@ -2352,10 +2475,10 @@
2352
2475
  Opal.Module = Module = Opal.allocate_class('Module', Opal.Object, $Module);
2353
2476
  Opal.Class = Class = Opal.allocate_class('Class', Opal.Module, $Class);
2354
2477
 
2355
- $setPrototype(Opal.BasicObject, Opal.Class.$$prototype);
2356
- $setPrototype(Opal.Object, Opal.Class.$$prototype);
2357
- $setPrototype(Opal.Module, Opal.Class.$$prototype);
2358
- $setPrototype(Opal.Class, Opal.Class.$$prototype);
2478
+ $set_proto(Opal.BasicObject, Opal.Class.$$prototype);
2479
+ $set_proto(Opal.Object, Opal.Class.$$prototype);
2480
+ $set_proto(Opal.Module, Opal.Class.$$prototype);
2481
+ $set_proto(Opal.Class, Opal.Class.$$prototype);
2359
2482
 
2360
2483
  // BasicObject can reach itself, avoid const_set to skip the $$base_module logic
2361
2484
  BasicObject.$$const["BasicObject"] = BasicObject;
@@ -2387,14 +2510,18 @@
2387
2510
  // other corelib files.
2388
2511
  $defineProperty(_Object.$$prototype, '$require', Opal.require);
2389
2512
 
2390
- // Add a short helper to navigate constants manually.
2391
- // @example
2392
- // Opal.$$.Regexp.$$.IGNORECASE
2393
- Opal.$$ = _Object.$$;
2394
-
2395
2513
  // Instantiate the main object
2396
2514
  Opal.top = new _Object();
2397
2515
  Opal.top.$to_s = Opal.top.$inspect = function() { return 'main' };
2516
+ Opal.top.$define_method = top_define_method;
2517
+
2518
+ // Foward calls to define_method on the top object to Object
2519
+ function top_define_method() {
2520
+ var args = Opal.slice.call(arguments, 0, arguments.length);
2521
+ var block = top_define_method.$$p;
2522
+ top_define_method.$$p = null;
2523
+ return Opal.send(_Object, 'define_method', args, block)
2524
+ };
2398
2525
 
2399
2526
 
2400
2527
  // Nil