opal 1.3.2 → 1.4.0.alpha1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (222) hide show
  1. checksums.yaml +4 -4
  2. data/.eslintrc.js +1 -0
  3. data/.github/workflows/build.yml +0 -3
  4. data/.rubocop.yml +5 -1
  5. data/UNRELEASED.md +66 -2
  6. data/benchmark-ips/bm_truthy.rb +30 -0
  7. data/bin/opal-mspec +1 -3
  8. data/bin/opal-repl +1 -2
  9. data/bin/remove-filters +1 -4
  10. data/docs/compiled_ruby.md +10 -6
  11. data/exe/opal-repl +1 -3
  12. data/lib/opal/ast/builder.rb +1 -1
  13. data/lib/opal/cli.rb +2 -2
  14. data/lib/opal/cli_runners/nodejs.rb +9 -2
  15. data/lib/opal/cli_runners/source-map-support-browser.js +80 -216
  16. data/lib/opal/cli_runners/source-map-support-node.js +80 -216
  17. data/lib/opal/cli_runners/source-map-support.js +5 -1
  18. data/lib/opal/cli_runners/system_runner.rb +10 -4
  19. data/lib/opal/compiler.rb +3 -5
  20. data/lib/opal/fragment.rb +5 -1
  21. data/lib/opal/nodes/args/extract_block_arg.rb +1 -8
  22. data/lib/opal/nodes/args/extract_kwoptarg.rb +1 -3
  23. data/lib/opal/nodes/args/extract_optarg.rb +1 -3
  24. data/lib/opal/nodes/args/extract_post_arg.rb +2 -5
  25. data/lib/opal/nodes/args/extract_post_optarg.rb +2 -7
  26. data/lib/opal/nodes/args/initialize_iterarg.rb +1 -3
  27. data/lib/opal/nodes/args/prepare_post_args.rb +5 -1
  28. data/lib/opal/nodes/base.rb +3 -2
  29. data/lib/opal/nodes/call.rb +20 -9
  30. data/lib/opal/nodes/call_special.rb +50 -0
  31. data/lib/opal/nodes/class.rb +24 -15
  32. data/lib/opal/nodes/constants.rb +23 -5
  33. data/lib/opal/nodes/def.rb +20 -23
  34. data/lib/opal/nodes/defined.rb +5 -5
  35. data/lib/opal/nodes/definitions.rb +2 -2
  36. data/lib/opal/nodes/defs.rb +2 -5
  37. data/lib/opal/nodes/helpers.rb +48 -18
  38. data/lib/opal/nodes/if.rb +109 -8
  39. data/lib/opal/nodes/iter.rb +23 -16
  40. data/lib/opal/nodes/literal.rb +18 -4
  41. data/lib/opal/nodes/logic.rb +2 -1
  42. data/lib/opal/nodes/masgn.rb +4 -9
  43. data/lib/opal/nodes/module.rb +29 -19
  44. data/lib/opal/nodes/node_with_args.rb +1 -7
  45. data/lib/opal/nodes/scope.rb +54 -15
  46. data/lib/opal/nodes/singleton_class.rb +5 -3
  47. data/lib/opal/nodes/super.rb +3 -3
  48. data/lib/opal/nodes/top.rb +34 -31
  49. data/lib/opal/nodes/variables.rb +2 -2
  50. data/lib/opal/nodes/x_string.rb +3 -0
  51. data/lib/opal/nodes.rb +0 -1
  52. data/lib/opal/parser/patch.rb +75 -0
  53. data/lib/opal/parser/with_ruby_lexer.rb +1 -1
  54. data/lib/opal/regexp_anchors.rb +7 -7
  55. data/lib/opal/requires.rb +19 -0
  56. data/lib/opal/rewriters/pattern_matching.rb +1 -1
  57. data/lib/opal/rewriters/returnable_logic.rb +102 -4
  58. data/lib/opal/util.rb +2 -2
  59. data/lib/opal/version.rb +1 -1
  60. data/lib/opal.rb +1 -17
  61. data/opal/corelib/array/pack.rb +11 -11
  62. data/opal/corelib/array.rb +193 -152
  63. data/opal/corelib/basic_object.rb +14 -14
  64. data/opal/corelib/binding.rb +7 -7
  65. data/opal/corelib/boolean.rb +12 -15
  66. data/opal/corelib/class.rb +23 -1
  67. data/opal/corelib/comparable.rb +8 -8
  68. data/opal/corelib/complex/base.rb +2 -2
  69. data/opal/corelib/complex.rb +79 -88
  70. data/opal/corelib/constants.rb +9 -9
  71. data/opal/corelib/dir.rb +4 -3
  72. data/opal/corelib/enumerable.rb +140 -127
  73. data/opal/corelib/enumerator/arithmetic_sequence.rb +177 -0
  74. data/opal/corelib/enumerator/chain.rb +42 -0
  75. data/opal/corelib/enumerator/generator.rb +35 -0
  76. data/opal/corelib/enumerator/lazy.rb +243 -0
  77. data/opal/corelib/enumerator/yielder.rb +36 -0
  78. data/opal/corelib/enumerator.rb +45 -300
  79. data/opal/corelib/error/errno.rb +47 -0
  80. data/opal/corelib/error.rb +62 -60
  81. data/opal/corelib/file.rb +26 -12
  82. data/opal/corelib/hash.rb +98 -107
  83. data/opal/corelib/helpers.rb +62 -13
  84. data/opal/corelib/io.rb +47 -34
  85. data/opal/corelib/kernel/format.rb +29 -29
  86. data/opal/corelib/kernel.rb +86 -83
  87. data/opal/corelib/main.rb +14 -12
  88. data/opal/corelib/marshal/read_buffer.rb +15 -15
  89. data/opal/corelib/marshal/write_buffer.rb +45 -44
  90. data/opal/corelib/marshal.rb +3 -3
  91. data/opal/corelib/math.rb +50 -50
  92. data/opal/corelib/method.rb +12 -8
  93. data/opal/corelib/module.rb +96 -79
  94. data/opal/corelib/nil.rb +9 -11
  95. data/opal/corelib/number.rb +113 -118
  96. data/opal/corelib/numeric.rb +37 -33
  97. data/opal/corelib/object_space.rb +11 -10
  98. data/opal/corelib/pack_unpack/format_string_parser.rb +3 -3
  99. data/opal/corelib/pattern_matching/base.rb +7 -7
  100. data/opal/corelib/pattern_matching.rb +1 -1
  101. data/opal/corelib/proc.rb +15 -16
  102. data/opal/corelib/process/base.rb +2 -2
  103. data/opal/corelib/process/status.rb +21 -0
  104. data/opal/corelib/process.rb +5 -5
  105. data/opal/corelib/random/formatter.rb +11 -11
  106. data/opal/corelib/random/math_random.js.rb +1 -1
  107. data/opal/corelib/random/mersenne_twister.rb +3 -3
  108. data/opal/corelib/random/seedrandom.js.rb +3 -3
  109. data/opal/corelib/random.rb +17 -17
  110. data/opal/corelib/range.rb +51 -35
  111. data/opal/corelib/rational/base.rb +4 -4
  112. data/opal/corelib/rational.rb +61 -62
  113. data/opal/corelib/regexp.rb +47 -38
  114. data/opal/corelib/runtime.js +245 -139
  115. data/opal/corelib/string/encoding.rb +21 -21
  116. data/opal/corelib/string/unpack.rb +19 -14
  117. data/opal/corelib/string.rb +135 -128
  118. data/opal/corelib/struct.rb +59 -46
  119. data/opal/corelib/time.rb +47 -57
  120. data/opal/corelib/trace_point.rb +2 -2
  121. data/opal/corelib/unsupported.rb +31 -120
  122. data/opal/corelib/variables.rb +3 -3
  123. data/opal/opal/base.rb +9 -8
  124. data/opal/opal/full.rb +8 -8
  125. data/opal/opal/mini.rb +17 -17
  126. data/opal/opal.rb +17 -18
  127. data/opal.gemspec +1 -1
  128. data/spec/filters/bugs/array.rb +4 -24
  129. data/spec/filters/bugs/bigdecimal.rb +0 -23
  130. data/spec/filters/bugs/binding.rb +0 -1
  131. data/spec/filters/bugs/boolean.rb +3 -0
  132. data/spec/filters/bugs/class.rb +2 -0
  133. data/spec/filters/bugs/date.rb +0 -5
  134. data/spec/filters/bugs/encoding.rb +8 -50
  135. data/spec/filters/bugs/enumerable.rb +4 -1
  136. data/spec/filters/bugs/enumerator.rb +3 -36
  137. data/spec/filters/bugs/exception.rb +0 -2
  138. data/spec/filters/bugs/file.rb +0 -2
  139. data/spec/filters/bugs/float.rb +0 -3
  140. data/spec/filters/bugs/hash.rb +5 -3
  141. data/spec/filters/bugs/integer.rb +2 -3
  142. data/spec/filters/bugs/kernel.rb +2 -31
  143. data/spec/filters/bugs/language.rb +29 -49
  144. data/spec/filters/bugs/main.rb +0 -2
  145. data/spec/filters/bugs/marshal.rb +2 -3
  146. data/spec/filters/bugs/matrix.rb +0 -36
  147. data/spec/filters/bugs/module.rb +7 -61
  148. data/spec/filters/bugs/numeric.rb +0 -7
  149. data/spec/filters/bugs/objectspace.rb +1 -1
  150. data/spec/filters/bugs/pack_unpack.rb +0 -4
  151. data/spec/filters/bugs/proc.rb +0 -9
  152. data/spec/filters/bugs/random.rb +0 -5
  153. data/spec/filters/bugs/range.rb +1 -6
  154. data/spec/filters/bugs/regexp.rb +0 -3
  155. data/spec/filters/bugs/set.rb +8 -1
  156. data/spec/filters/bugs/string.rb +9 -34
  157. data/spec/filters/bugs/stringscanner.rb +8 -7
  158. data/spec/filters/bugs/struct.rb +2 -3
  159. data/spec/filters/bugs/symbol.rb +0 -1
  160. data/spec/filters/bugs/time.rb +0 -8
  161. data/spec/filters/bugs/unboundmethod.rb +0 -8
  162. data/spec/filters/bugs/warnings.rb +1 -7
  163. data/spec/filters/unsupported/freeze.rb +24 -0
  164. data/spec/filters/unsupported/integer.rb +1 -0
  165. data/spec/filters/unsupported/kernel.rb +12 -0
  166. data/spec/filters/unsupported/privacy.rb +3 -0
  167. data/spec/filters/unsupported/string.rb +2 -0
  168. data/spec/lib/builder_spec.rb +2 -2
  169. data/spec/lib/cli_spec.rb +1 -1
  170. data/spec/lib/compiler_spec.rb +37 -37
  171. data/spec/lib/simple_server_spec.rb +2 -2
  172. data/spec/lib/source_map/file_spec.rb +1 -1
  173. data/spec/opal/compiler/irb_spec.rb +2 -2
  174. data/spec/opal/core/kernel/puts_spec.rb +90 -0
  175. data/spec/opal/core/language/super_spec.rb +24 -0
  176. data/spec/ruby_specs +4 -2
  177. data/spec/support/rewriters_helper.rb +1 -1
  178. data/stdlib/bigdecimal.rb +7 -11
  179. data/stdlib/buffer/view.rb +2 -2
  180. data/stdlib/buffer.rb +2 -2
  181. data/stdlib/date.rb +5 -6
  182. data/stdlib/erb.rb +1 -0
  183. data/stdlib/js.rb +2 -1
  184. data/stdlib/native.rb +7 -8
  185. data/stdlib/nodejs/argf.rb +4 -4
  186. data/stdlib/nodejs/base.rb +29 -0
  187. data/stdlib/nodejs/dir.rb +1 -1
  188. data/stdlib/nodejs/env.rb +6 -9
  189. data/stdlib/nodejs/file.rb +23 -17
  190. data/stdlib/nodejs/fileutils.rb +3 -3
  191. data/stdlib/nodejs/io.rb +2 -20
  192. data/stdlib/nodejs/irb.rb +0 -0
  193. data/stdlib/nodejs/kernel.rb +2 -37
  194. data/stdlib/nodejs.rb +1 -3
  195. data/stdlib/opal/miniracer.rb +2 -0
  196. data/stdlib/opal/platform.rb +6 -13
  197. data/stdlib/opal/replutils.rb +16 -5
  198. data/stdlib/opal-parser.rb +2 -2
  199. data/stdlib/optparse/ac.rb +54 -0
  200. data/stdlib/optparse/date.rb +14 -0
  201. data/stdlib/optparse/kwargs.rb +22 -0
  202. data/stdlib/optparse/shellwords.rb +7 -0
  203. data/stdlib/optparse/time.rb +15 -0
  204. data/stdlib/optparse/uri.rb +7 -0
  205. data/stdlib/optparse/version.rb +69 -0
  206. data/stdlib/optparse.rb +2279 -0
  207. data/stdlib/pathname.rb +5 -6
  208. data/stdlib/pp.rb +18 -2
  209. data/stdlib/promise/v2.rb +18 -22
  210. data/stdlib/promise.rb +15 -21
  211. data/stdlib/set.rb +32 -32
  212. data/stdlib/shellwords.rb +240 -0
  213. data/stdlib/stringio.rb +3 -6
  214. data/stdlib/strscan.rb +5 -8
  215. data/stdlib/template.rb +2 -2
  216. data/stdlib/thread.rb +7 -9
  217. data/tasks/performance.rake +5 -2
  218. data/tasks/testing/mspec_special_calls.rb +0 -12
  219. data/tasks/testing.rake +55 -37
  220. data/test/nodejs/test_file.rb +11 -0
  221. metadata +55 -10
  222. data/lib/opal/nodes/case.rb +0 -114
@@ -1,4 +1,6 @@
1
- class BasicObject
1
+ # use_strict: true
2
+
3
+ class ::BasicObject
2
4
  def initialize(*)
3
5
  end
4
6
 
@@ -17,7 +19,7 @@ class BasicObject
17
19
  if (self.$$id != null) {
18
20
  return self.$$id;
19
21
  }
20
- Opal.defineProperty(self, '$$id', Opal.uid());
22
+ Opal.prop(self, '$$id', Opal.uid());
21
23
  return self.$$id;
22
24
  }
23
25
  end
@@ -63,12 +65,11 @@ class BasicObject
63
65
  compiling_options = __OPAL_COMPILER_CONFIG__.merge(default_eval_options)
64
66
  compiled = ::Opal.compile string, compiling_options
65
67
  block = ::Kernel.proc do
66
- %x{
67
- return (function(self) {
68
- return eval(compiled);
69
- })(self)
70
- }
68
+ %x{new Function("Opal,self", "return " + compiled)(Opal, self)}
71
69
  end
70
+ elsif block.nil? && args.length >= 1 && args.first[0] == '@'
71
+ # get instance variable
72
+ return instance_variable_get(args.first)
72
73
  elsif args.any?
73
74
  ::Kernel.raise ::ArgumentError, "wrong number of arguments (#{args.size} for 0)"
74
75
  end
@@ -139,15 +140,14 @@ class BasicObject
139
140
  end
140
141
 
141
142
  def method_missing(symbol, *args, &block)
142
- message = if `self.$inspect && !self.$inspect.$$stub`
143
- "undefined method `#{symbol}' for #{inspect}:#{`self.$$class`}"
144
- else
145
- "undefined method `#{symbol}' for #{`self.$$class`}"
146
- end
147
-
148
- ::Kernel.raise ::NoMethodError.new(message, symbol)
143
+ inspect_result = ::Opal.inspect(self)
144
+ ::Kernel.raise ::NoMethodError.new(
145
+ "undefined method `#{symbol}' for #{inspect_result}", symbol, args
146
+ ), nil, ::Kernel.caller(1)
149
147
  end
150
148
 
149
+ ::Opal.pristine(self, :method_missing)
150
+
151
151
  def respond_to_missing?(method_name, include_all = false)
152
152
  false
153
153
  end
@@ -1,4 +1,4 @@
1
- class Binding
1
+ class ::Binding
2
2
  # @private
3
3
  def initialize(jseval, scope_variables, receiver, source_location)
4
4
  @jseval, @scope_variables, @receiver, @source_location = \
@@ -9,14 +9,14 @@ class Binding
9
9
  if @jseval
10
10
  @jseval.call(*args)
11
11
  else
12
- raise 'Evaluation on a Proc#binding is not supported'
12
+ ::Kernel.raise 'Evaluation on a Proc#binding is not supported'
13
13
  end
14
14
  end
15
15
 
16
16
  def local_variable_get(symbol)
17
17
  js_eval(symbol)
18
- rescue Exception
19
- raise NameError, "local variable `#{symbol}' is not defined for #{inspect}"
18
+ rescue ::Exception
19
+ ::Kernel.raise ::NameError, "local variable `#{symbol}' is not defined for #{inspect}"
20
20
  end
21
21
 
22
22
  def local_variable_set(symbol, value)
@@ -34,15 +34,15 @@ class Binding
34
34
  def eval(str, file = nil, line = nil)
35
35
  return receiver if str == 'self'
36
36
 
37
- Kernel.eval(str, self, file, line)
37
+ ::Kernel.eval(str, self, file, line)
38
38
  end
39
39
 
40
40
  attr_reader :receiver, :source_location
41
41
  end
42
42
 
43
- module Kernel
43
+ module ::Kernel
44
44
  def binding
45
- raise "Opal doesn't support dynamic calls to binding"
45
+ ::Kernel.raise "Opal doesn't support dynamic calls to binding"
46
46
  end
47
47
  end
48
48
 
@@ -1,5 +1,7 @@
1
- class Boolean < `Boolean`
2
- `Opal.defineProperty(self.$$prototype, '$$is_boolean', true)`
1
+ # use_strict: true
2
+
3
+ class ::Boolean < `Boolean`
4
+ `Opal.prop(self.$$prototype, '$$is_boolean', true)`
3
5
 
4
6
  %x{
5
7
  var properties = ['$$class', '$$meta'];
@@ -29,7 +31,7 @@ class Boolean < `Boolean`
29
31
 
30
32
  class << self
31
33
  def allocate
32
- raise TypeError, "allocator undefined for #{name}"
34
+ ::Kernel.raise ::TypeError, "allocator undefined for #{name}"
33
35
  end
34
36
 
35
37
  undef :new
@@ -39,8 +41,6 @@ class Boolean < `Boolean`
39
41
  `self.valueOf() ? 2 : 0`
40
42
  end
41
43
 
42
- alias object_id __id__
43
-
44
44
  def !
45
45
  `self != true`
46
46
  end
@@ -61,9 +61,6 @@ class Boolean < `Boolean`
61
61
  `(self == true) === other.valueOf()`
62
62
  end
63
63
 
64
- alias equal? ==
65
- alias eql? ==
66
-
67
64
  def singleton_class
68
65
  `self.$$meta`
69
66
  end
@@ -72,8 +69,6 @@ class Boolean < `Boolean`
72
69
  `(self == true) ? 'true' : 'false'`
73
70
  end
74
71
 
75
- alias inspect to_s
76
-
77
72
  def dup
78
73
  self
79
74
  end
@@ -104,10 +99,12 @@ class Boolean < `Boolean`
104
99
  `var body = self.$$class.$$prototype['$' + #{method}]`
105
100
  `typeof body !== 'undefined' && !body.$$stub`
106
101
  end
107
- end
108
102
 
109
- class TrueClass < Boolean; end
110
- class FalseClass < Boolean; end
103
+ alias eql? ==
104
+ alias equal? ==
105
+ alias inspect to_s
106
+ alias object_id __id__
107
+ end
111
108
 
112
- TRUE = true
113
- FALSE = false
109
+ class ::TrueClass < ::Boolean; end
110
+ class ::FalseClass < ::Boolean; end
@@ -1,6 +1,6 @@
1
1
  require 'corelib/module'
2
2
 
3
- class Class
3
+ class ::Class
4
4
  def self.new(superclass = Object, &block)
5
5
  %x{
6
6
  if (!superclass.$$is_class) {
@@ -22,6 +22,10 @@ class Class
22
22
  }
23
23
  end
24
24
 
25
+ def descendants
26
+ subclasses + subclasses.map(&:descendants).flatten
27
+ end
28
+
25
29
  def inherited(cls)
26
30
  end
27
31
 
@@ -41,6 +45,24 @@ class Class
41
45
  }
42
46
  end
43
47
 
48
+ def subclasses
49
+ %x{
50
+ if (typeof WeakRef !== 'undefined') {
51
+ var i, subclass, out = [];
52
+ for (i = 0; i < self.$$subclasses.length; i++) {
53
+ subclass = self.$$subclasses[i].deref();
54
+ if (subclass !== undefined) {
55
+ out.push(subclass);
56
+ }
57
+ }
58
+ return out;
59
+ }
60
+ else {
61
+ return self.$$subclasses;
62
+ }
63
+ }
64
+ end
65
+
44
66
  def superclass
45
67
  `self.$$super || nil`
46
68
  end
@@ -1,6 +1,6 @@
1
- # helpers: falsy
1
+ # helpers: truthy
2
2
 
3
- module Comparable
3
+ module ::Comparable
4
4
  %x{
5
5
  function normalize(what) {
6
6
  if (Opal.is_a(what, Opal.Integer)) { return what; }
@@ -14,18 +14,18 @@ module Comparable
14
14
  var class_name;
15
15
  #{
16
16
  case `rhs`
17
- when nil, true, false, Integer, Float
17
+ when nil, true, false, ::Integer, ::Float
18
18
  `class_name = rhs.$inspect()`
19
19
  else
20
20
  `class_name = rhs.$$class`
21
21
  end
22
22
  }
23
- #{raise ArgumentError, "comparison of #{`lhs`.class} with #{`class_name`} failed"}
23
+ #{::Kernel.raise ::ArgumentError, "comparison of #{`lhs`.class} with #{`class_name`} failed"}
24
24
  }
25
25
 
26
26
  function cmp_or_fail(lhs, rhs) {
27
27
  var cmp = #{`lhs` <=> `rhs`};
28
- if ($falsy(cmp)) fail_comparison(lhs, rhs);
28
+ if (!$truthy(cmp)) fail_comparison(lhs, rhs);
29
29
  return normalize(cmp);
30
30
  }
31
31
  }
@@ -81,7 +81,7 @@ module Comparable
81
81
  // provide a single Range argument instead of 2 Comparables.
82
82
 
83
83
  if (!Opal.is_a(min, Opal.Range)) {
84
- #{raise TypeError, "wrong argument type #{min.class} (expected Range)"}
84
+ #{::Kernel.raise ::TypeError, "wrong argument type #{min.class} (expected Range)"}
85
85
  }
86
86
 
87
87
  excl = min.excl;
@@ -89,12 +89,12 @@ module Comparable
89
89
  min = min.begin;
90
90
 
91
91
  if (max !== nil && excl) {
92
- #{raise ArgumentError, 'cannot clamp with an exclusive range'}
92
+ #{::Kernel.raise ::ArgumentError, 'cannot clamp with an exclusive range'}
93
93
  }
94
94
  }
95
95
 
96
96
  if (min !== nil && max !== nil && cmp_or_fail(min, max) > 0) {
97
- #{raise ArgumentError, 'min argument must be smaller than max argument'}
97
+ #{::Kernel.raise ::ArgumentError, 'min argument must be smaller than max argument'}
98
98
  }
99
99
 
100
100
  if (min !== nil) {
@@ -1,4 +1,4 @@
1
- module Kernel
1
+ module ::Kernel
2
2
  def Complex(real, imag = nil)
3
3
  if imag
4
4
  Complex.new(real, imag)
@@ -8,7 +8,7 @@ module Kernel
8
8
  end
9
9
  end
10
10
 
11
- class String
11
+ class ::String
12
12
  def to_c
13
13
  Complex.from_string(self)
14
14
  end
@@ -1,25 +1,21 @@
1
1
  require 'corelib/numeric'
2
2
  require 'corelib/complex/base'
3
3
 
4
- class Complex < Numeric
4
+ class ::Complex < ::Numeric
5
5
  def self.rect(real, imag = 0)
6
- unless Numeric === real && real.real? && Numeric === imag && imag.real?
7
- raise TypeError, 'not a real'
6
+ unless ::Numeric === real && real.real? && ::Numeric === imag && imag.real?
7
+ ::Kernel.raise ::TypeError, 'not a real'
8
8
  end
9
9
 
10
10
  new(real, imag)
11
11
  end
12
12
 
13
- class << self
14
- alias rectangular rect
15
- end
16
-
17
13
  def self.polar(r, theta = 0)
18
- unless Numeric === r && r.real? && Numeric === theta && theta.real?
19
- raise TypeError, 'not a real'
14
+ unless ::Numeric === r && r.real? && ::Numeric === theta && theta.real?
15
+ ::Kernel.raise ::TypeError, 'not a real'
20
16
  end
21
17
 
22
- new(r * Math.cos(theta), r * Math.sin(theta))
18
+ new(r * ::Math.cos(theta), r * ::Math.sin(theta))
23
19
  end
24
20
 
25
21
  attr_reader :real, :imag
@@ -30,19 +26,19 @@ class Complex < Numeric
30
26
  end
31
27
 
32
28
  def coerce(other)
33
- if Complex === other
29
+ if ::Complex === other
34
30
  [other, self]
35
- elsif Numeric === other && other.real?
36
- [Complex.new(other, 0), self]
31
+ elsif ::Numeric === other && other.real?
32
+ [::Complex.new(other, 0), self]
37
33
  else
38
- raise TypeError, "#{other.class} can't be coerced into Complex"
34
+ ::Kernel.raise ::TypeError, "#{other.class} can't be coerced into Complex"
39
35
  end
40
36
  end
41
37
 
42
38
  def ==(other)
43
- if Complex === other
39
+ if ::Complex === other
44
40
  @real == other.real && @imag == other.imag
45
- elsif Numeric === other && other.real?
41
+ elsif ::Numeric === other && other.real?
46
42
  @real == other && @imag == 0
47
43
  else
48
44
  other == self
@@ -50,51 +46,51 @@ class Complex < Numeric
50
46
  end
51
47
 
52
48
  def -@
53
- Complex(-@real, -@imag)
49
+ ::Kernel.Complex(-@real, -@imag)
54
50
  end
55
51
 
56
52
  def +(other)
57
- if Complex === other
58
- Complex(@real + other.real, @imag + other.imag)
59
- elsif Numeric === other && other.real?
60
- Complex(@real + other, @imag)
53
+ if ::Complex === other
54
+ ::Kernel.Complex(@real + other.real, @imag + other.imag)
55
+ elsif ::Numeric === other && other.real?
56
+ ::Kernel.Complex(@real + other, @imag)
61
57
  else
62
58
  __coerced__ :+, other
63
59
  end
64
60
  end
65
61
 
66
62
  def -(other)
67
- if Complex === other
68
- Complex(@real - other.real, @imag - other.imag)
69
- elsif Numeric === other && other.real?
70
- Complex(@real - other, @imag)
63
+ if ::Complex === other
64
+ ::Kernel.Complex(@real - other.real, @imag - other.imag)
65
+ elsif ::Numeric === other && other.real?
66
+ ::Kernel.Complex(@real - other, @imag)
71
67
  else
72
68
  __coerced__ :-, other
73
69
  end
74
70
  end
75
71
 
76
72
  def *(other)
77
- if Complex === other
78
- Complex(@real * other.real - @imag * other.imag,
73
+ if ::Complex === other
74
+ ::Kernel.Complex(@real * other.real - @imag * other.imag,
79
75
  @real * other.imag + @imag * other.real,
80
76
  )
81
- elsif Numeric === other && other.real?
82
- Complex(@real * other, @imag * other)
77
+ elsif ::Numeric === other && other.real?
78
+ ::Kernel.Complex(@real * other, @imag * other)
83
79
  else
84
80
  __coerced__ :*, other
85
81
  end
86
82
  end
87
83
 
88
84
  def /(other)
89
- if Complex === other
90
- if (Number === @real && @real.nan?) || (Number === @imag && @imag.nan?) ||
91
- (Number === other.real && other.real.nan?) || (Number === other.imag && other.imag.nan?)
92
- Complex.new(Float::NAN, Float::NAN)
85
+ if ::Complex === other
86
+ if (::Number === @real && @real.nan?) || (::Number === @imag && @imag.nan?) ||
87
+ (::Number === other.real && other.real.nan?) || (::Number === other.imag && other.imag.nan?)
88
+ ::Complex.new(::Float::NAN, ::Float::NAN)
93
89
  else
94
90
  self * other.conj / other.abs2
95
91
  end
96
- elsif Numeric === other && other.real?
97
- Complex(@real.quo(other), @imag.quo(other))
92
+ elsif ::Numeric === other && other.real?
93
+ ::Kernel.Complex(@real.quo(other), @imag.quo(other))
98
94
  else
99
95
  __coerced__ :/, other
100
96
  end
@@ -102,18 +98,18 @@ class Complex < Numeric
102
98
 
103
99
  def **(other)
104
100
  if other == 0
105
- return Complex.new(1, 0)
101
+ return ::Complex.new(1, 0)
106
102
  end
107
103
 
108
- if Complex === other
104
+ if ::Complex === other
109
105
  r, theta = polar
110
106
  ore = other.real
111
107
  oim = other.imag
112
- nr = Math.exp(ore * Math.log(r) - oim * theta)
113
- ntheta = theta * ore + oim * Math.log(r)
108
+ nr = ::Math.exp(ore * ::Math.log(r) - oim * theta)
109
+ ntheta = theta * ore + oim * ::Math.log(r)
114
110
 
115
- Complex.polar(nr, ntheta)
116
- elsif Integer === other
111
+ ::Complex.polar(nr, ntheta)
112
+ elsif ::Integer === other
117
113
  if other > 0
118
114
  x = self
119
115
  z = x
@@ -122,7 +118,7 @@ class Complex < Numeric
122
118
  while n != 0
123
119
  div, mod = n.divmod(2)
124
120
  while mod == 0
125
- x = Complex(x.real * x.real - x.imag * x.imag, 2 * x.real * x.imag)
121
+ x = ::Kernel.Complex(x.real * x.real - x.imag * x.imag, 2 * x.real * x.imag)
126
122
  n = div
127
123
  div, mod = n.divmod(2)
128
124
  end
@@ -133,19 +129,19 @@ class Complex < Numeric
133
129
 
134
130
  z
135
131
  else
136
- (Rational.new(1, 1) / self)**-other
132
+ (::Rational.new(1, 1) / self)**-other
137
133
  end
138
- elsif Float === other || Rational === other
134
+ elsif ::Float === other || ::Rational === other
139
135
  r, theta = polar
140
136
 
141
- Complex.polar(r**other, theta * other)
137
+ ::Complex.polar(r**other, theta * other)
142
138
  else
143
139
  __coerced__ :**, other
144
140
  end
145
141
  end
146
142
 
147
143
  def abs
148
- Math.hypot(@real, @imag)
144
+ ::Math.hypot(@real, @imag)
149
145
  end
150
146
 
151
147
  def abs2
@@ -153,30 +149,24 @@ class Complex < Numeric
153
149
  end
154
150
 
155
151
  def angle
156
- Math.atan2(@imag, @real)
152
+ ::Math.atan2(@imag, @real)
157
153
  end
158
154
 
159
- alias arg angle
160
-
161
155
  def conj
162
- Complex(@real, -@imag)
156
+ ::Kernel.Complex(@real, -@imag)
163
157
  end
164
158
 
165
- alias conjugate conj
166
-
167
159
  def denominator
168
160
  @real.denominator.lcm(@imag.denominator)
169
161
  end
170
162
 
171
- alias divide /
172
-
173
163
  def eql?(other)
174
164
  Complex === other && @real.class == @imag.class && self == other
175
165
  end
176
166
 
177
167
  def fdiv(other)
178
- unless Numeric === other
179
- raise TypeError, "#{other.class} can't be coerced into Complex"
168
+ unless ::Numeric === other
169
+ ::Kernel.raise ::TypeError, "#{other.class} can't be coerced into Complex"
180
170
  end
181
171
 
182
172
  self / other
@@ -190,8 +180,6 @@ class Complex < Numeric
190
180
  "Complex:#{@real}:#{@imag}"
191
181
  end
192
182
 
193
- alias imaginary imag
194
-
195
183
  def infinite?
196
184
  @real.infinite? || @imag.infinite?
197
185
  end
@@ -200,37 +188,27 @@ class Complex < Numeric
200
188
  "(#{self})"
201
189
  end
202
190
 
203
- alias magnitude abs
204
-
205
- undef negative?
206
-
207
191
  def numerator
208
192
  d = denominator
209
193
 
210
- Complex(@real.numerator * (d / @real.denominator),
194
+ ::Kernel.Complex(@real.numerator * (d / @real.denominator),
211
195
  @imag.numerator * (d / @imag.denominator),
212
196
  )
213
197
  end
214
198
 
215
- alias phase arg
216
-
217
199
  def polar
218
200
  [abs, arg]
219
201
  end
220
202
 
221
- undef positive?
222
-
223
- alias quo /
224
-
225
203
  def rationalize(eps = undefined)
226
204
  %x{
227
205
  if (arguments.length > 1) {
228
- #{raise ArgumentError, "wrong number of arguments (#{`arguments.length`} for 0..1)"};
206
+ #{::Kernel.raise ::ArgumentError, "wrong number of arguments (#{`arguments.length`} for 0..1)"};
229
207
  }
230
208
  }
231
209
 
232
210
  if @imag != 0
233
- raise RangeError, "can't' convert #{self} into Rational"
211
+ ::Kernel.raise ::RangeError, "can't convert #{self} into Rational"
234
212
  end
235
213
 
236
214
  real.rationalize(eps)
@@ -244,13 +222,9 @@ class Complex < Numeric
244
222
  [@real, @imag]
245
223
  end
246
224
 
247
- alias rectangular rect
248
-
249
- undef step
250
-
251
225
  def to_f
252
226
  unless @imag == 0
253
- raise RangeError, "can't convert #{self} into Float"
227
+ ::Kernel.raise ::RangeError, "can't convert #{self} into Float"
254
228
  end
255
229
 
256
230
  @real.to_f
@@ -258,7 +232,7 @@ class Complex < Numeric
258
232
 
259
233
  def to_i
260
234
  unless @imag == 0
261
- raise RangeError, "can't convert #{self} into Integer"
235
+ ::Kernel.raise ::RangeError, "can't convert #{self} into Integer"
262
236
  end
263
237
 
264
238
  @real.to_i
@@ -266,7 +240,7 @@ class Complex < Numeric
266
240
 
267
241
  def to_r
268
242
  unless @imag == 0
269
- raise RangeError, "can't convert #{self} into Rational"
243
+ ::Kernel.raise ::RangeError, "can't convert #{self} into Rational"
270
244
  end
271
245
 
272
246
  @real.to_r
@@ -276,7 +250,7 @@ class Complex < Numeric
276
250
  result = @real.inspect
277
251
 
278
252
  result +=
279
- if (Number === @imag && @imag.nan?) || @imag.positive? || @imag.zero?
253
+ if (::Number === @imag && @imag.nan?) || @imag.positive? || @imag.zero?
280
254
  '+'
281
255
  else
282
256
  '-'
@@ -284,7 +258,7 @@ class Complex < Numeric
284
258
 
285
259
  result += @imag.abs.inspect
286
260
 
287
- if Number === @imag && (@imag.nan? || @imag.infinite?)
261
+ if ::Number === @imag && (@imag.nan? || @imag.infinite?)
288
262
  result += '*'
289
263
  end
290
264
 
@@ -321,7 +295,7 @@ class Complex < Numeric
321
295
 
322
296
  if (isFloat()) {
323
297
  var denominator = parseFloat(cutFloat());
324
- return #{Rational(`numerator`, `denominator`)};
298
+ return #{::Kernel.Rational(`numerator`, `denominator`)};
325
299
  } else {
326
300
  // reverting '/'
327
301
  str = '/' + str;
@@ -341,33 +315,50 @@ class Complex < Numeric
341
315
  if (!real) {
342
316
  if (str[0] === 'i') {
343
317
  // i => Complex(0, 1)
344
- return #{Complex(0, 1)};
318
+ return #{::Kernel.Complex(0, 1)};
345
319
  }
346
320
  if (str[0] === '-' && str[1] === 'i') {
347
321
  // -i => Complex(0, -1)
348
- return #{Complex(0, -1)};
322
+ return #{::Kernel.Complex(0, -1)};
349
323
  }
350
324
  if (str[0] === '+' && str[1] === 'i') {
351
325
  // +i => Complex(0, 1)
352
- return #{Complex(0, 1)};
326
+ return #{::Kernel.Complex(0, 1)};
353
327
  }
354
328
  // anything => Complex(0, 0)
355
- return #{Complex(0, 0)};
329
+ return #{::Kernel.Complex(0, 0)};
356
330
  }
357
331
 
358
332
  imag = cutNumber();
359
333
  if (!imag) {
360
334
  if (str[0] === 'i') {
361
335
  // 3i => Complex(0, 3)
362
- return #{Complex(0, `real`)};
336
+ return #{::Kernel.Complex(0, `real`)};
363
337
  } else {
364
338
  // 3 => Complex(3, 0)
365
- return #{Complex(`real`, 0)};
339
+ return #{::Kernel.Complex(`real`, 0)};
366
340
  }
367
341
  } else {
368
342
  // 3+2i => Complex(3, 2)
369
- return #{Complex(`real`, `imag`)};
343
+ return #{::Kernel.Complex(`real`, `imag`)};
370
344
  }
371
345
  }
372
346
  end
347
+
348
+ class << self
349
+ alias rectangular rect
350
+ end
351
+
352
+ alias arg angle
353
+ alias conjugate conj
354
+ alias divide /
355
+ alias imaginary imag
356
+ alias magnitude abs
357
+ alias phase arg
358
+ alias quo /
359
+ alias rectangular rect
360
+
361
+ undef negative?
362
+ undef positive?
363
+ undef step
373
364
  end
@@ -1,9 +1,9 @@
1
- RUBY_PLATFORM = 'opal'
2
- RUBY_ENGINE = 'opal'
3
- RUBY_VERSION = '3.0.2'
4
- RUBY_ENGINE_VERSION = '1.3.2'
5
- RUBY_RELEASE_DATE = '2021-11-10'
6
- RUBY_PATCHLEVEL = 0
7
- RUBY_REVISION = '0'
8
- RUBY_COPYRIGHT = 'opal - Copyright (C) 2013-2021 Adam Beynon and the Opal contributors'
9
- RUBY_DESCRIPTION = "opal #{RUBY_ENGINE_VERSION} (#{RUBY_RELEASE_DATE} revision #{RUBY_REVISION})"
1
+ ::RUBY_PLATFORM = 'opal'
2
+ ::RUBY_ENGINE = 'opal'
3
+ ::RUBY_VERSION = '3.1.0'
4
+ ::RUBY_ENGINE_VERSION = '1.4.0.alpha1'
5
+ ::RUBY_RELEASE_DATE = '2021-12-21'
6
+ ::RUBY_PATCHLEVEL = 0
7
+ ::RUBY_REVISION = '0'
8
+ ::RUBY_COPYRIGHT = 'opal - Copyright (C) 2013-2021 Adam Beynon and the Opal contributors'
9
+ ::RUBY_DESCRIPTION = "opal #{::RUBY_ENGINE_VERSION} (#{::RUBY_RELEASE_DATE} revision #{::RUBY_REVISION})"