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