opal 1.3.1 → 1.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (243) hide show
  1. checksums.yaml +4 -4
  2. data/.eslintrc.js +1 -0
  3. data/.github/workflows/build.yml +20 -21
  4. data/.rubocop.yml +5 -1
  5. data/CHANGELOG.md +119 -11
  6. data/UNRELEASED.md +4 -9
  7. data/benchmark-ips/bm_truthy.rb +30 -0
  8. data/bin/opal-mspec +1 -3
  9. data/bin/opal-repl +1 -2
  10. data/bin/remove-filters +1 -4
  11. data/docs/compiled_ruby.md +37 -22
  12. data/docs/faq.md +1 -1
  13. data/docs/headless_chrome.md +11 -21
  14. data/docs/jquery.md +1 -6
  15. data/docs/opal_parser.md +3 -1
  16. data/docs/promises.md +2 -0
  17. data/docs/releasing.md +3 -0
  18. data/docs/roda-sprockets.md +0 -1
  19. data/docs/source_maps.md +10 -11
  20. data/docs/static_applications.md +2 -2
  21. data/docs/unsupported_features.md +4 -0
  22. data/exe/opal-repl +1 -3
  23. data/lib/opal/ast/builder.rb +1 -1
  24. data/lib/opal/cli.rb +2 -2
  25. data/lib/opal/cli_runners/nodejs.rb +9 -2
  26. data/lib/opal/cli_runners/source-map-support-browser.js +80 -216
  27. data/lib/opal/cli_runners/source-map-support-node.js +80 -216
  28. data/lib/opal/cli_runners/source-map-support.js +5 -1
  29. data/lib/opal/cli_runners/system_runner.rb +10 -4
  30. data/lib/opal/compiler.rb +3 -5
  31. data/lib/opal/fragment.rb +5 -1
  32. data/lib/opal/nodes/args/extract_block_arg.rb +1 -8
  33. data/lib/opal/nodes/args/extract_kwoptarg.rb +1 -3
  34. data/lib/opal/nodes/args/extract_optarg.rb +1 -3
  35. data/lib/opal/nodes/args/extract_post_arg.rb +2 -5
  36. data/lib/opal/nodes/args/extract_post_optarg.rb +2 -7
  37. data/lib/opal/nodes/args/initialize_iterarg.rb +1 -3
  38. data/lib/opal/nodes/args/prepare_post_args.rb +5 -1
  39. data/lib/opal/nodes/base.rb +3 -2
  40. data/lib/opal/nodes/call.rb +20 -9
  41. data/lib/opal/nodes/call_special.rb +50 -0
  42. data/lib/opal/nodes/class.rb +24 -15
  43. data/lib/opal/nodes/constants.rb +23 -5
  44. data/lib/opal/nodes/def.rb +20 -23
  45. data/lib/opal/nodes/defined.rb +5 -5
  46. data/lib/opal/nodes/definitions.rb +2 -2
  47. data/lib/opal/nodes/defs.rb +2 -5
  48. data/lib/opal/nodes/helpers.rb +48 -18
  49. data/lib/opal/nodes/if.rb +113 -8
  50. data/lib/opal/nodes/iter.rb +23 -16
  51. data/lib/opal/nodes/literal.rb +18 -4
  52. data/lib/opal/nodes/logic.rb +2 -1
  53. data/lib/opal/nodes/masgn.rb +4 -9
  54. data/lib/opal/nodes/module.rb +29 -19
  55. data/lib/opal/nodes/node_with_args.rb +1 -7
  56. data/lib/opal/nodes/scope.rb +54 -15
  57. data/lib/opal/nodes/singleton_class.rb +5 -3
  58. data/lib/opal/nodes/super.rb +12 -12
  59. data/lib/opal/nodes/top.rb +34 -31
  60. data/lib/opal/nodes/variables.rb +2 -2
  61. data/lib/opal/nodes/x_string.rb +30 -28
  62. data/lib/opal/nodes.rb +0 -1
  63. data/lib/opal/parser/patch.rb +75 -0
  64. data/lib/opal/parser/with_ruby_lexer.rb +1 -1
  65. data/lib/opal/regexp_anchors.rb +7 -7
  66. data/lib/opal/requires.rb +19 -0
  67. data/lib/opal/rewriters/pattern_matching.rb +1 -1
  68. data/lib/opal/rewriters/returnable_logic.rb +102 -4
  69. data/lib/opal/util.rb +2 -2
  70. data/lib/opal/version.rb +1 -1
  71. data/lib/opal.rb +1 -17
  72. data/opal/corelib/array/pack.rb +11 -11
  73. data/opal/corelib/array.rb +193 -152
  74. data/opal/corelib/basic_object.rb +19 -15
  75. data/opal/corelib/binding.rb +7 -7
  76. data/opal/corelib/boolean.rb +12 -15
  77. data/opal/corelib/class.rb +23 -1
  78. data/opal/corelib/comparable.rb +8 -8
  79. data/opal/corelib/complex/base.rb +2 -2
  80. data/opal/corelib/complex.rb +79 -88
  81. data/opal/corelib/constants.rb +9 -9
  82. data/opal/corelib/dir.rb +4 -3
  83. data/opal/corelib/enumerable.rb +140 -127
  84. data/opal/corelib/enumerator/arithmetic_sequence.rb +177 -0
  85. data/opal/corelib/enumerator/chain.rb +42 -0
  86. data/opal/corelib/enumerator/generator.rb +35 -0
  87. data/opal/corelib/enumerator/lazy.rb +243 -0
  88. data/opal/corelib/enumerator/yielder.rb +36 -0
  89. data/opal/corelib/enumerator.rb +45 -300
  90. data/opal/corelib/error/errno.rb +47 -0
  91. data/opal/corelib/error.rb +62 -60
  92. data/opal/corelib/file.rb +26 -12
  93. data/opal/corelib/hash.rb +98 -107
  94. data/opal/corelib/helpers.rb +62 -13
  95. data/opal/corelib/io.rb +48 -35
  96. data/opal/corelib/kernel/format.rb +29 -29
  97. data/opal/corelib/kernel.rb +86 -83
  98. data/opal/corelib/main.rb +14 -12
  99. data/opal/corelib/marshal/read_buffer.rb +15 -15
  100. data/opal/corelib/marshal/write_buffer.rb +45 -44
  101. data/opal/corelib/marshal.rb +3 -3
  102. data/opal/corelib/math.rb +50 -50
  103. data/opal/corelib/method.rb +12 -8
  104. data/opal/corelib/module.rb +79 -75
  105. data/opal/corelib/nil.rb +9 -11
  106. data/opal/corelib/number.rb +113 -118
  107. data/opal/corelib/numeric.rb +37 -33
  108. data/opal/corelib/object_space.rb +11 -10
  109. data/opal/corelib/pack_unpack/format_string_parser.rb +3 -3
  110. data/opal/corelib/pattern_matching/base.rb +7 -7
  111. data/opal/corelib/pattern_matching.rb +1 -1
  112. data/opal/corelib/proc.rb +15 -16
  113. data/opal/corelib/process/base.rb +2 -2
  114. data/opal/corelib/process/status.rb +21 -0
  115. data/opal/corelib/process.rb +5 -5
  116. data/opal/corelib/random/formatter.rb +11 -11
  117. data/opal/corelib/random/math_random.js.rb +1 -1
  118. data/opal/corelib/random/mersenne_twister.rb +3 -3
  119. data/opal/corelib/random/seedrandom.js.rb +3 -3
  120. data/opal/corelib/random.rb +17 -17
  121. data/opal/corelib/range.rb +51 -35
  122. data/opal/corelib/rational/base.rb +4 -4
  123. data/opal/corelib/rational.rb +61 -62
  124. data/opal/corelib/regexp.rb +54 -45
  125. data/opal/corelib/runtime.js +247 -141
  126. data/opal/corelib/string/encoding.rb +21 -21
  127. data/opal/corelib/string/unpack.rb +19 -14
  128. data/opal/corelib/string.rb +137 -130
  129. data/opal/corelib/struct.rb +59 -46
  130. data/opal/corelib/time.rb +47 -57
  131. data/opal/corelib/trace_point.rb +2 -2
  132. data/opal/corelib/unsupported.rb +31 -120
  133. data/opal/corelib/variables.rb +3 -3
  134. data/opal/opal/base.rb +9 -8
  135. data/opal/opal/full.rb +8 -8
  136. data/opal/opal/mini.rb +17 -17
  137. data/opal/opal.rb +17 -18
  138. data/opal.gemspec +1 -1
  139. data/spec/filters/bugs/array.rb +4 -24
  140. data/spec/filters/bugs/basicobject.rb +0 -1
  141. data/spec/filters/bugs/bigdecimal.rb +0 -23
  142. data/spec/filters/bugs/binding.rb +0 -1
  143. data/spec/filters/bugs/boolean.rb +3 -0
  144. data/spec/filters/bugs/class.rb +2 -0
  145. data/spec/filters/bugs/date.rb +0 -5
  146. data/spec/filters/bugs/encoding.rb +8 -50
  147. data/spec/filters/bugs/enumerable.rb +4 -1
  148. data/spec/filters/bugs/enumerator.rb +3 -36
  149. data/spec/filters/bugs/exception.rb +0 -2
  150. data/spec/filters/bugs/file.rb +0 -2
  151. data/spec/filters/bugs/float.rb +0 -3
  152. data/spec/filters/bugs/hash.rb +5 -3
  153. data/spec/filters/bugs/integer.rb +2 -3
  154. data/spec/filters/bugs/kernel.rb +2 -31
  155. data/spec/filters/bugs/language.rb +29 -49
  156. data/spec/filters/bugs/main.rb +0 -2
  157. data/spec/filters/bugs/marshal.rb +2 -3
  158. data/spec/filters/bugs/matrix.rb +0 -36
  159. data/spec/filters/bugs/module.rb +7 -61
  160. data/spec/filters/bugs/numeric.rb +0 -7
  161. data/spec/filters/bugs/objectspace.rb +1 -1
  162. data/spec/filters/bugs/pack_unpack.rb +0 -4
  163. data/spec/filters/bugs/proc.rb +0 -9
  164. data/spec/filters/bugs/random.rb +0 -5
  165. data/spec/filters/bugs/range.rb +1 -6
  166. data/spec/filters/bugs/regexp.rb +0 -3
  167. data/spec/filters/bugs/set.rb +8 -1
  168. data/spec/filters/bugs/string.rb +9 -34
  169. data/spec/filters/bugs/stringscanner.rb +8 -7
  170. data/spec/filters/bugs/struct.rb +2 -3
  171. data/spec/filters/bugs/symbol.rb +0 -1
  172. data/spec/filters/bugs/time.rb +0 -8
  173. data/spec/filters/bugs/unboundmethod.rb +0 -8
  174. data/spec/filters/bugs/warnings.rb +1 -7
  175. data/spec/filters/unsupported/freeze.rb +24 -0
  176. data/spec/filters/unsupported/integer.rb +1 -0
  177. data/spec/filters/unsupported/kernel.rb +12 -0
  178. data/spec/filters/unsupported/privacy.rb +3 -0
  179. data/spec/filters/unsupported/string.rb +2 -0
  180. data/spec/lib/builder_spec.rb +2 -2
  181. data/spec/lib/cli_spec.rb +1 -1
  182. data/spec/lib/compiler_spec.rb +37 -37
  183. data/spec/lib/simple_server_spec.rb +2 -2
  184. data/spec/lib/source_map/file_spec.rb +1 -1
  185. data/spec/opal/compiler/irb_spec.rb +2 -2
  186. data/spec/opal/core/io/read_spec.rb +69 -0
  187. data/spec/opal/core/kernel/puts_spec.rb +90 -0
  188. data/spec/opal/core/language/super_spec.rb +21 -0
  189. data/spec/opal/core/language/xstring_spec.rb +13 -0
  190. data/spec/opal/core/language_spec.rb +14 -0
  191. data/spec/opal/core/string/gsub_spec.rb +8 -0
  192. data/spec/ruby_specs +4 -2
  193. data/spec/support/rewriters_helper.rb +1 -1
  194. data/stdlib/bigdecimal.rb +7 -11
  195. data/stdlib/buffer/view.rb +2 -2
  196. data/stdlib/buffer.rb +2 -2
  197. data/stdlib/date.rb +5 -6
  198. data/stdlib/erb.rb +1 -0
  199. data/stdlib/js.rb +2 -1
  200. data/stdlib/native.rb +7 -8
  201. data/stdlib/nodejs/argf.rb +4 -4
  202. data/stdlib/nodejs/base.rb +29 -0
  203. data/stdlib/nodejs/dir.rb +1 -1
  204. data/stdlib/nodejs/env.rb +6 -9
  205. data/stdlib/nodejs/file.rb +23 -17
  206. data/stdlib/nodejs/fileutils.rb +3 -3
  207. data/stdlib/nodejs/io.rb +2 -20
  208. data/stdlib/nodejs/irb.rb +0 -0
  209. data/stdlib/nodejs/kernel.rb +2 -37
  210. data/stdlib/nodejs.rb +1 -3
  211. data/stdlib/opal/miniracer.rb +2 -0
  212. data/stdlib/opal/platform.rb +6 -13
  213. data/stdlib/opal/replutils.rb +16 -5
  214. data/stdlib/opal-parser.rb +2 -2
  215. data/stdlib/optparse/ac.rb +54 -0
  216. data/stdlib/optparse/date.rb +14 -0
  217. data/stdlib/optparse/kwargs.rb +22 -0
  218. data/stdlib/optparse/shellwords.rb +7 -0
  219. data/stdlib/optparse/time.rb +15 -0
  220. data/stdlib/optparse/uri.rb +7 -0
  221. data/stdlib/optparse/version.rb +69 -0
  222. data/stdlib/optparse.rb +2279 -0
  223. data/stdlib/pathname.rb +5 -6
  224. data/stdlib/pp.rb +18 -2
  225. data/stdlib/promise/v2.rb +18 -29
  226. data/stdlib/promise.rb +15 -21
  227. data/stdlib/quickjs/io.rb +0 -2
  228. data/stdlib/quickjs/kernel.rb +0 -2
  229. data/stdlib/quickjs.rb +2 -0
  230. data/stdlib/set.rb +32 -32
  231. data/stdlib/shellwords.rb +240 -0
  232. data/stdlib/stringio.rb +3 -6
  233. data/stdlib/strscan.rb +5 -8
  234. data/stdlib/template.rb +2 -2
  235. data/stdlib/thread.rb +7 -9
  236. data/tasks/linting-parse-eslint-results.js +1 -0
  237. data/tasks/linting.rake +0 -10
  238. data/tasks/performance.rake +5 -2
  239. data/tasks/testing/mspec_special_calls.rb +0 -12
  240. data/tasks/testing.rake +55 -37
  241. data/test/nodejs/test_file.rb +11 -0
  242. metadata +55 -8
  243. data/lib/opal/nodes/case.rb +0 -114
@@ -1,10 +1,7 @@
1
- # helpers: truthy, coerce_to, respond_to
2
-
3
- module Kernel
4
- def method_missing(symbol, *args, &block)
5
- raise NoMethodError.new("undefined method `#{symbol}' for #{inspect}", symbol, args), nil, caller(1)
6
- end
1
+ # helpers: truthy, coerce_to, respond_to, Opal
2
+ # use_strict: true
7
3
 
4
+ module ::Kernel
8
5
  def =~(obj)
9
6
  false
10
7
  end
@@ -37,10 +34,10 @@ module Kernel
37
34
  var meth = self['$' + name];
38
35
 
39
36
  if (!meth || meth.$$stub) {
40
- #{raise NameError.new("undefined method `#{name}' for class `#{self.class}'", name)};
37
+ #{::Kernel.raise ::NameError.new("undefined method `#{name}' for class `#{self.class}'", name)};
41
38
  }
42
39
 
43
- return #{Method.new(self, `meth.$$owner || #{self.class}`, `meth`, name)};
40
+ return #{::Method.new(self, `meth.$$owner || #{self.class}`, `meth`, name)};
44
41
  }
45
42
  end
46
43
 
@@ -76,10 +73,10 @@ module Kernel
76
73
  return object;
77
74
  }
78
75
 
79
- coerced = #{Opal.coerce_to?(object, Array, :to_ary)};
76
+ coerced = #{::Opal.coerce_to?(object, ::Array, :to_ary)};
80
77
  if (coerced !== nil) { return coerced; }
81
78
 
82
- coerced = #{Opal.coerce_to?(object, Array, :to_a)};
79
+ coerced = #{::Opal.coerce_to?(object, ::Array, :to_a)};
83
80
  if (coerced !== nil) { return coerced; }
84
81
 
85
82
  return [object];
@@ -189,11 +186,9 @@ module Kernel
189
186
  end
190
187
 
191
188
  def enum_for(method = :each, *args, &block)
192
- Enumerator.for(self, method, *args, &block)
189
+ ::Enumerator.for(self, method, *args, &block)
193
190
  end
194
191
 
195
- alias to_enum enum_for
196
-
197
192
  def equal?(other)
198
193
  `self === other`
199
194
  end
@@ -210,7 +205,7 @@ module Kernel
210
205
  if (status.$$is_boolean) {
211
206
  status = status ? 0 : 1;
212
207
  } else {
213
- status = $coerce_to(status, #{Integer}, 'to_int')
208
+ status = $coerce_to(status, #{::Integer}, 'to_int')
214
209
  }
215
210
 
216
211
  Opal.exit(status);
@@ -226,7 +221,7 @@ module Kernel
226
221
  var mod = mods[i];
227
222
 
228
223
  if (!mod.$$is_module) {
229
- #{raise TypeError, "wrong argument type #{`mod`.class} (expected Module)"};
224
+ #{::Kernel.raise ::TypeError, "wrong argument type #{`mod`.class} (expected Module)"};
230
225
  }
231
226
 
232
227
  #{`mod`.append_features `singleton`};
@@ -249,20 +244,33 @@ module Kernel
249
244
  def initialize_copy(other)
250
245
  end
251
246
 
247
+ `var inspect_stack = []`
248
+
252
249
  def inspect
253
250
  ivs = ''
254
- instance_variables.each do |i|
255
- ivs += " #{i}=#{instance_variable_get(i).inspect}"
251
+ id = __id__
252
+ if `inspect_stack`.include? id
253
+ ivs = ' ...'
254
+ else
255
+ `inspect_stack` << id
256
+ pushed = true
257
+ instance_variables.each do |i|
258
+ ivar = instance_variable_get(i)
259
+ inspect = Opal.inspect(ivar)
260
+ ivs += " #{i}=#{inspect}"
261
+ end
256
262
  end
257
- "#<#{self.class}:0x#{__id__.to_s(16)}#{ivs}>"
258
- rescue
259
- "#<#{self.class}:0x#{__id__.to_s(16)}>"
263
+ "#<#{self.class}:0x#{id.to_s(16)}#{ivs}>"
264
+ rescue => e
265
+ "#<#{self.class}:0x#{id.to_s(16)}>"
266
+ ensure
267
+ `inspect_stack`.pop if pushed
260
268
  end
261
269
 
262
270
  def instance_of?(klass)
263
271
  %x{
264
272
  if (!klass.$$is_class && !klass.$$is_module) {
265
- #{raise TypeError, 'class or module required'};
273
+ #{::Kernel.raise ::TypeError, 'class or module required'};
266
274
  }
267
275
 
268
276
  return self.$$class === klass;
@@ -270,13 +278,13 @@ module Kernel
270
278
  end
271
279
 
272
280
  def instance_variable_defined?(name)
273
- name = Opal.instance_variable_name!(name)
281
+ name = ::Opal.instance_variable_name!(name)
274
282
 
275
283
  `Opal.hasOwnProperty.call(self, name.substr(1))`
276
284
  end
277
285
 
278
286
  def instance_variable_get(name)
279
- name = Opal.instance_variable_name!(name)
287
+ name = ::Opal.instance_variable_name!(name)
280
288
 
281
289
  %x{
282
290
  var ivar = self[Opal.ivar(name.substr(1))];
@@ -286,13 +294,13 @@ module Kernel
286
294
  end
287
295
 
288
296
  def instance_variable_set(name, value)
289
- name = Opal.instance_variable_name!(name)
297
+ name = ::Opal.instance_variable_name!(name)
290
298
 
291
299
  `self[Opal.ivar(name.substr(1))] = value`
292
300
  end
293
301
 
294
302
  def remove_instance_variable(name)
295
- name = Opal.instance_variable_name!(name)
303
+ name = ::Opal.instance_variable_name!(name)
296
304
 
297
305
  %x{
298
306
  var key = Opal.ivar(name.substr(1)),
@@ -304,7 +312,7 @@ module Kernel
304
312
  }
305
313
  }
306
314
 
307
- raise NameError, "instance variable #{name} not defined"
315
+ ::Kernel.raise ::NameError, "instance variable #{name} not defined"
308
316
  end
309
317
 
310
318
  def instance_variables
@@ -332,14 +340,14 @@ module Kernel
332
340
 
333
341
  if (!value.$$is_string) {
334
342
  if (base !== undefined) {
335
- #{raise ArgumentError, 'base specified for non string value'}
343
+ #{::Kernel.raise ::ArgumentError, 'base specified for non string value'}
336
344
  }
337
345
  if (value === nil) {
338
- #{raise TypeError, "can't convert nil into Integer"}
346
+ #{::Kernel.raise ::TypeError, "can't convert nil into Integer"}
339
347
  }
340
348
  if (value.$$is_number) {
341
349
  if (value === Infinity || value === -Infinity || isNaN(value)) {
342
- #{raise FloatDomainError, value}
350
+ #{::Kernel.raise ::FloatDomainError, value}
343
351
  }
344
352
  return Math.floor(value);
345
353
  }
@@ -349,7 +357,7 @@ module Kernel
349
357
  return i;
350
358
  }
351
359
  }
352
- return #{Opal.coerce_to!(value, Integer, :to_i)};
360
+ return #{::Opal.coerce_to!(value, ::Integer, :to_i)};
353
361
  }
354
362
 
355
363
  if (value === "0") {
@@ -359,9 +367,9 @@ module Kernel
359
367
  if (base === undefined) {
360
368
  base = 0;
361
369
  } else {
362
- base = $coerce_to(base, #{Integer}, 'to_int');
370
+ base = $coerce_to(base, #{::Integer}, 'to_int');
363
371
  if (base === 1 || base < 0 || base > 36) {
364
- #{raise ArgumentError, "invalid radix #{base}"}
372
+ #{::Kernel.raise ::ArgumentError, "invalid radix #{base}"}
365
373
  }
366
374
  }
367
375
 
@@ -397,7 +405,7 @@ module Kernel
397
405
  }
398
406
  // no-break
399
407
  }
400
- #{raise ArgumentError, "invalid value for Integer(): \"#{value}\""}
408
+ #{::Kernel.raise ::ArgumentError, "invalid value for Integer(): \"#{value}\""}
401
409
  });
402
410
 
403
411
  base = (base === 0 ? 10 : base);
@@ -405,13 +413,13 @@ module Kernel
405
413
  base_digits = '0-' + (base <= 10 ? base - 1 : '9a-' + String.fromCharCode(97 + (base - 11)));
406
414
 
407
415
  if (!(new RegExp('^\\s*[+-]?[' + base_digits + ']+\\s*$')).test(str)) {
408
- #{raise ArgumentError, "invalid value for Integer(): \"#{value}\""}
416
+ #{::Kernel.raise ::ArgumentError, "invalid value for Integer(): \"#{value}\""}
409
417
  }
410
418
 
411
419
  i = parseInt(str, base);
412
420
 
413
421
  if (isNaN(i)) {
414
- #{raise ArgumentError, "invalid value for Integer(): \"#{value}\""}
422
+ #{::Kernel.raise ::ArgumentError, "invalid value for Integer(): \"#{value}\""}
415
423
  }
416
424
 
417
425
  return i;
@@ -423,7 +431,7 @@ module Kernel
423
431
  var str;
424
432
 
425
433
  if (value === nil) {
426
- #{raise TypeError, "can't convert nil into Float"}
434
+ #{::Kernel.raise ::TypeError, "can't convert nil into Float"}
427
435
  }
428
436
 
429
437
  if (value.$$is_string) {
@@ -433,30 +441,30 @@ module Kernel
433
441
 
434
442
  //Special case for hex strings only:
435
443
  if (/^\s*[-+]?0[xX][0-9a-fA-F]+\s*$/.test(str)) {
436
- return #{Integer(`str`)};
444
+ return #{::Kernel.Integer(`str`)};
437
445
  }
438
446
 
439
447
  if (!/^\s*[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?\s*$/.test(str)) {
440
- #{raise ArgumentError, "invalid value for Float(): \"#{value}\""}
448
+ #{::Kernel.raise ::ArgumentError, "invalid value for Float(): \"#{value}\""}
441
449
  }
442
450
 
443
451
  return parseFloat(str);
444
452
  }
445
453
 
446
- return #{Opal.coerce_to!(value, Float, :to_f)};
454
+ return #{::Opal.coerce_to!(value, ::Float, :to_f)};
447
455
  }
448
456
  end
449
457
 
450
458
  def Hash(arg)
451
459
  return {} if arg.nil? || arg == []
452
- return arg if Hash === arg
453
- Opal.coerce_to!(arg, Hash, :to_hash)
460
+ return arg if ::Hash === arg
461
+ ::Opal.coerce_to!(arg, ::Hash, :to_hash)
454
462
  end
455
463
 
456
464
  def is_a?(klass)
457
465
  %x{
458
466
  if (!klass.$$is_class && !klass.$$is_module) {
459
- #{raise TypeError, 'class or module required'};
467
+ #{::Kernel.raise ::TypeError, 'class or module required'};
460
468
  }
461
469
 
462
470
  return Opal.is_a(self, klass);
@@ -467,24 +475,22 @@ module Kernel
467
475
  self
468
476
  end
469
477
 
470
- alias kind_of? is_a?
471
-
472
478
  def lambda(&block)
473
479
  `Opal.lambda(block)`
474
480
  end
475
481
 
476
482
  def load(file)
477
- file = Opal.coerce_to!(file, String, :to_str)
483
+ file = ::Opal.coerce_to!(file, ::String, :to_str)
478
484
  `Opal.load(#{file})`
479
485
  end
480
486
 
481
487
  def loop
482
- return enum_for(:loop) { Float::INFINITY } unless block_given?
488
+ return enum_for(:loop) { ::Float::INFINITY } unless block_given?
483
489
 
484
490
  while true
485
491
  begin
486
492
  yield
487
- rescue StopIteration => e
493
+ rescue ::StopIteration => e
488
494
  return e.result
489
495
  end
490
496
  end
@@ -496,8 +502,6 @@ module Kernel
496
502
  false
497
503
  end
498
504
 
499
- alias object_id __id__
500
-
501
505
  def printf(*args)
502
506
  if args.any?
503
507
  print format(*args)
@@ -508,7 +512,7 @@ module Kernel
508
512
 
509
513
  def proc(&block)
510
514
  unless block
511
- raise ArgumentError, 'tried to create Proc object without a block'
515
+ ::Kernel.raise ::ArgumentError, 'tried to create Proc object without a block'
512
516
  end
513
517
 
514
518
  `block.$$is_lambda = false`
@@ -535,8 +539,8 @@ module Kernel
535
539
 
536
540
  def warn(*strs, uplevel: nil)
537
541
  if uplevel
538
- uplevel = Opal.coerce_to!(uplevel, Integer, :to_str)
539
- raise ArgumentError, "negative level (#{uplevel})" if uplevel < 0
542
+ uplevel = ::Opal.coerce_to!(uplevel, ::Integer, :to_str)
543
+ ::Kernel.raise ::ArgumentError, "negative level (#{uplevel})" if uplevel < 0
540
544
  location = caller(uplevel + 1, 1).first&.split(':in `')&.first
541
545
  location = "#{location}: " if location
542
546
  strs = strs.map { |s| "#{location}warning: #{s}" }
@@ -551,10 +555,10 @@ module Kernel
551
555
  throw #{$!};
552
556
  }
553
557
  if (exception == null) {
554
- exception = #{RuntimeError.new ''};
558
+ exception = #{::RuntimeError.new ''};
555
559
  }
556
560
  else if ($respond_to(exception, '$to_str')) {
557
- exception = #{RuntimeError.new exception.to_str};
561
+ exception = #{::RuntimeError.new exception.to_str};
558
562
  }
559
563
  // using respond_to? and not an undefined check to avoid method_missing matching as true
560
564
  else if (exception.$$is_class && $respond_to(exception, '$exception')) {
@@ -564,7 +568,7 @@ module Kernel
564
568
  // exception is fine
565
569
  }
566
570
  else {
567
- exception = #{TypeError.new 'exception class/object expected'};
571
+ exception = #{::TypeError.new 'exception class/object expected'};
568
572
  }
569
573
 
570
574
  if (backtrace !== nil) {
@@ -582,12 +586,10 @@ module Kernel
582
586
  }
583
587
  end
584
588
 
585
- alias fail raise
586
-
587
589
  def rand(max = undefined)
588
590
  %x{
589
591
  if (max === undefined) {
590
- return #{Random::DEFAULT.rand};
592
+ return #{::Random::DEFAULT.rand};
591
593
  }
592
594
 
593
595
  if (max.$$is_number) {
@@ -604,7 +606,7 @@ module Kernel
604
606
  }
605
607
  }
606
608
  }
607
- Random::DEFAULT.rand(max)
609
+ ::Random::DEFAULT.rand(max)
608
610
  end
609
611
 
610
612
  def respond_to?(name, include_all = false)
@@ -627,22 +629,22 @@ module Kernel
627
629
  false
628
630
  end
629
631
 
630
- Opal.pristine(self, :respond_to?, :respond_to_missing?)
632
+ ::Opal.pristine(self, :respond_to?, :respond_to_missing?)
631
633
 
632
634
  def require(file)
633
635
  %x{
634
636
  // As Object.require refers to Kernel.require once Kernel has been loaded the String
635
637
  // class may not be available yet, the coercion requires both String and Array to be loaded.
636
638
  if (typeof #{file} !== 'string' && Opal.String && Opal.Array) {
637
- #{file = Opal.coerce_to!(file, String, :to_str) }
639
+ #{file = ::Opal.coerce_to!(file, ::String, :to_str) }
638
640
  }
639
641
  return Opal.require(#{file})
640
642
  }
641
643
  end
642
644
 
643
645
  def require_relative(file)
644
- Opal.try_convert!(file, String, :to_str)
645
- file = File.expand_path File.join(`Opal.current_file`, '..', file)
646
+ ::Opal.try_convert!(file, ::String, :to_str)
647
+ file = ::File.expand_path ::File.join(`Opal.current_file`, '..', file)
646
648
 
647
649
  `Opal.require(#{file})`
648
650
  end
@@ -652,7 +654,7 @@ module Kernel
652
654
  %x{
653
655
  var result = [];
654
656
 
655
- path = #{File.expand_path(path)}
657
+ path = #{::File.expand_path(path)}
656
658
  path = Opal.normalize(path);
657
659
  if (path === '.') path = '';
658
660
  for (var name in Opal.modules) {
@@ -669,9 +671,6 @@ module Kernel
669
671
  }
670
672
  end
671
673
 
672
- alias send __send__
673
- alias public_send __send__
674
-
675
674
  def singleton_class
676
675
  `Opal.get_singleton_class(self)`
677
676
  end
@@ -679,13 +678,13 @@ module Kernel
679
678
  def sleep(seconds = nil)
680
679
  %x{
681
680
  if (seconds === nil) {
682
- #{raise TypeError, "can't convert NilClass into time interval"}
681
+ #{::Kernel.raise ::TypeError, "can't convert NilClass into time interval"}
683
682
  }
684
683
  if (!seconds.$$is_number) {
685
- #{raise TypeError, "can't convert #{seconds.class} into time interval"}
684
+ #{::Kernel.raise ::TypeError, "can't convert #{seconds.class} into time interval"}
686
685
  }
687
686
  if (seconds < 0) {
688
- #{raise ArgumentError, 'time interval must be positive'}
687
+ #{::Kernel.raise ::ArgumentError, 'time interval must be positive'}
689
688
  }
690
689
  var get_time = Opal.global.performance ?
691
690
  function() {return performance.now()} :
@@ -698,12 +697,12 @@ module Kernel
698
697
  end
699
698
 
700
699
  def srand(seed = Random.new_seed)
701
- Random.srand(seed)
700
+ ::Random.srand(seed)
702
701
  end
703
702
 
704
703
  def String(str)
705
- Opal.coerce_to?(str, String, :to_str) ||
706
- Opal.coerce_to!(str, String, :to_s)
704
+ ::Opal.coerce_to?(str, ::String, :to_str) ||
705
+ ::Opal.coerce_to!(str, ::String, :to_s)
707
706
  end
708
707
 
709
708
  def tap(&block)
@@ -720,20 +719,20 @@ module Kernel
720
719
  end
721
720
 
722
721
  def catch(tag = nil)
723
- tag ||= Object.new
722
+ tag ||= ::Object.new
724
723
  yield(tag)
725
- rescue UncaughtThrowError => e
724
+ rescue ::UncaughtThrowError => e
726
725
  return e.value if e.tag == tag
727
- raise
726
+ ::Kernel.raise
728
727
  end
729
728
 
730
729
  def throw(tag, obj = nil)
731
- raise UncaughtThrowError.new(tag, obj)
730
+ ::Kernel.raise ::UncaughtThrowError.new(tag, obj)
732
731
  end
733
732
 
734
733
  # basic implementation of open, delegate to File.open
735
734
  def open(*args, &block)
736
- File.open(*args, &block)
735
+ ::File.open(*args, &block)
737
736
  end
738
737
 
739
738
  def yield_self
@@ -741,15 +740,19 @@ module Kernel
741
740
  yield self
742
741
  end
743
742
 
743
+ alias fail raise
744
+ alias kind_of? is_a?
745
+ alias object_id __id__
746
+ alias public_send __send__
747
+ alias send __send__
744
748
  alias then yield_self
745
-
746
- Opal.pristine(self, :method_missing)
749
+ alias to_enum enum_for
747
750
  end
748
751
 
749
- class Object
752
+ class ::Object
750
753
  # Object.require has been set to runtime.js Opal.require
751
754
  # Now we have Kernel loaded, make sure Object.require refers to Kernel.require
752
755
  # which is what ruby does and allows for overwriting by autoloaders
753
- `delete Opal.Object.$$prototype.$require`
754
- include Kernel
756
+ `delete $Object.$$prototype.$require`
757
+ include ::Kernel
755
758
  end
data/opal/corelib/main.rb CHANGED
@@ -1,16 +1,18 @@
1
- def self.to_s
2
- 'main'
3
- end
1
+ class << self
2
+ def to_s
3
+ 'main'
4
+ end
4
5
 
5
- def self.include(mod)
6
- Object.include mod
7
- end
6
+ def include(mod)
7
+ ::Object.include mod
8
+ end
8
9
 
9
- def self.autoload(*args)
10
- Object.autoload(*args)
11
- end
10
+ def autoload(*args)
11
+ `Opal.Object.$autoload.apply(Opal.Object, args)`
12
+ end
12
13
 
13
- # Compiler overrides this method
14
- def self.using(mod)
15
- raise 'main.using is permitted only at toplevel'
14
+ # Compiler overrides this method
15
+ def using(mod)
16
+ ::Kernel.raise 'main.using is permitted only at toplevel'
17
+ end
16
18
  end
@@ -1,8 +1,8 @@
1
1
  # https://github.com/ruby/ruby/blob/trunk/doc/marshal.rdoc
2
2
  # https://github.com/ruby/ruby/blob/trunk/marshal.c
3
3
 
4
- module Marshal
5
- class ReadBuffer
4
+ module ::Marshal
5
+ class self::ReadBuffer
6
6
  %x{
7
7
  function stringToBytes(string) {
8
8
  var i,
@@ -26,7 +26,7 @@ module Marshal
26
26
  major = read_byte
27
27
  minor = read_byte
28
28
  if major != MAJOR_VERSION || minor != MINOR_VERSION
29
- raise TypeError, "incompatible marshal file format (can't be read)"
29
+ ::Kernel.raise ::TypeError, "incompatible marshal file format (can't be read)"
30
30
  end
31
31
  @version = "#{major}.#{minor}"
32
32
  @object_cache = []
@@ -89,17 +89,17 @@ module Marshal
89
89
  when 'U'
90
90
  read_user_marshal
91
91
  when 'M'
92
- raise NotImplementedError, 'ModuleOld type cannot be demarshaled yet' # read_module_old
92
+ ::Kernel.raise ::NotImplementedError, 'ModuleOld type cannot be demarshaled yet' # read_module_old
93
93
  when 'd'
94
- raise NotImplementedError, 'Data type cannot be demarshaled'
94
+ ::Kernel.raise ::NotImplementedError, 'Data type cannot be demarshaled'
95
95
  else
96
- raise ArgumentError, 'dump format error'
96
+ ::Kernel.raise ::ArgumentError, 'dump format error'
97
97
  end
98
98
  end
99
99
 
100
100
  def read_byte
101
101
  if @index >= length
102
- raise ArgumentError, 'marshal data too short'
102
+ ::Kernel.raise ::ArgumentError, 'marshal data too short'
103
103
  end
104
104
  result = @buffer[@index]
105
105
  @index += 1
@@ -312,7 +312,7 @@ module Marshal
312
312
  string = read_string(cache: false)
313
313
  options = read_byte
314
314
 
315
- result = Regexp.new(string, options)
315
+ result = ::Regexp.new(string, options)
316
316
  @object_cache << result
317
317
  result
318
318
  end
@@ -345,8 +345,8 @@ module Marshal
345
345
  def read_class
346
346
  klass_name = read_string(cache: false)
347
347
  result = safe_const_get(klass_name)
348
- unless result.class == Class
349
- raise ArgumentError, "#{klass_name} does not refer to a Class"
348
+ unless result.class == ::Class
349
+ ::Kernel.raise ::ArgumentError, "#{klass_name} does not refer to a Class"
350
350
  end
351
351
  @object_cache << result
352
352
  result
@@ -362,8 +362,8 @@ module Marshal
362
362
  def read_module
363
363
  mod_name = read_string(cache: false)
364
364
  result = safe_const_get(mod_name)
365
- unless result.class == Module
366
- raise ArgumentError, "#{mod_name} does not refer to a Module"
365
+ unless result.class == ::Module
366
+ ::Kernel.raise ::ArgumentError, "#{mod_name} does not refer to a Module"
367
367
  end
368
368
  @object_cache << result
369
369
  result
@@ -554,9 +554,9 @@ module Marshal
554
554
  # re-raises Marshal-specific error when it's missing
555
555
  #
556
556
  def safe_const_get(const_name)
557
- Object.const_get(const_name)
558
- rescue NameError
559
- raise ArgumentError, "undefined class/module #{const_name}"
557
+ ::Object.const_get(const_name)
558
+ rescue ::NameError
559
+ ::Kernel.raise ::ArgumentError, "undefined class/module #{const_name}"
560
560
  end
561
561
  end
562
562
  end