opal 0.9.4 → 0.10.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitattributes +1 -0
- data/.gitignore +2 -3
- data/.gitmodules +5 -2
- data/.jshintrc +1 -8
- data/.rspec +1 -1
- data/.travis.yml +15 -23
- data/CHANGELOG.md +511 -326
- data/CODE_OF_CONDUCT.md +13 -15
- data/CONTRIBUTING.md +26 -216
- data/Gemfile +20 -12
- data/Guardfile +2 -2
- data/HACKING.md +230 -0
- data/README.md +6 -7
- data/bin/opal-mspec +1 -1
- data/config.ru +2 -2
- data/docs/faq.md +1 -1
- data/docs/source_maps.md +1 -1
- data/lib/opal.rb +1 -0
- data/lib/opal/builder.rb +1 -1
- data/lib/opal/cli.rb +30 -28
- data/lib/opal/cli_options.rb +3 -0
- data/lib/opal/cli_runners.rb +14 -1
- data/lib/opal/cli_runners/{apple_script.rb → applescript.rb} +3 -3
- data/lib/opal/cli_runners/nashorn.rb +2 -2
- data/lib/opal/cli_runners/nodejs.rb +2 -2
- data/lib/opal/cli_runners/phantom.js +24 -0
- data/lib/opal/cli_runners/phantomjs.rb +10 -10
- data/lib/opal/cli_runners/server.rb +3 -3
- data/lib/opal/compiler.rb +43 -4
- data/lib/opal/config.rb +3 -1
- data/lib/opal/errors.rb +13 -0
- data/lib/opal/fragment.rb +0 -13
- data/lib/opal/nodes.rb +10 -0
- data/lib/opal/nodes/args/initialize_kwargs.rb +28 -0
- data/lib/opal/nodes/args/kwarg.rb +29 -0
- data/lib/opal/nodes/args/kwoptarg.rb +29 -0
- data/lib/opal/nodes/args/kwrestarg.rb +39 -0
- data/lib/opal/nodes/args/mlhsarg.rb +79 -0
- data/lib/opal/nodes/args/normarg.rb +26 -0
- data/lib/opal/nodes/args/optarg.rb +27 -0
- data/lib/opal/nodes/args/post_args.rb +200 -0
- data/lib/opal/nodes/args/post_kwargs.rb +31 -0
- data/lib/opal/nodes/args/restarg.rb +33 -0
- data/lib/opal/nodes/base.rb +12 -0
- data/lib/opal/nodes/call.rb +92 -33
- data/lib/opal/nodes/def.rb +26 -169
- data/lib/opal/nodes/hash.rb +10 -4
- data/lib/opal/nodes/helpers.rb +6 -3
- data/lib/opal/nodes/inline_args.rb +61 -0
- data/lib/opal/nodes/iter.rb +73 -82
- data/lib/opal/nodes/logic.rb +12 -2
- data/lib/opal/nodes/masgn.rb +1 -2
- data/lib/opal/nodes/node_with_args.rb +141 -0
- data/lib/opal/nodes/rescue.rb +121 -43
- data/lib/opal/nodes/scope.rb +24 -5
- data/lib/opal/nodes/super.rb +122 -54
- data/lib/opal/nodes/top.rb +0 -12
- data/lib/opal/nodes/yield.rb +2 -13
- data/lib/opal/parser.rb +67 -39
- data/lib/opal/parser/grammar.rb +3319 -2961
- data/lib/opal/parser/grammar.y +234 -46
- data/lib/opal/parser/lexer.rb +105 -17
- data/lib/opal/parser/sexp.rb +4 -0
- data/lib/opal/paths.rb +4 -0
- data/lib/opal/regexp_anchors.rb +19 -1
- data/lib/opal/sprockets.rb +21 -18
- data/lib/opal/sprockets/environment.rb +0 -8
- data/lib/opal/sprockets/processor.rb +13 -16
- data/lib/opal/sprockets/server.rb +6 -12
- data/lib/opal/version.rb +1 -1
- data/opal.gemspec +1 -0
- data/opal/corelib/array.rb +209 -131
- data/opal/corelib/basic_object.rb +7 -3
- data/opal/corelib/class.rb +11 -17
- data/opal/corelib/constants.rb +2 -2
- data/opal/corelib/enumerable.rb +178 -355
- data/opal/corelib/enumerator.rb +3 -46
- data/opal/corelib/error.rb +2 -2
- data/opal/corelib/file.rb +13 -1
- data/opal/corelib/hash.rb +26 -56
- data/opal/corelib/helpers.rb +10 -0
- data/opal/corelib/kernel.rb +6 -3
- data/opal/corelib/module.rb +62 -31
- data/opal/corelib/number.rb +7 -16
- data/opal/corelib/proc.rb +24 -9
- data/opal/corelib/range.rb +4 -13
- data/opal/corelib/runtime.js +515 -378
- data/opal/corelib/string.rb +21 -49
- data/opal/corelib/struct.rb +50 -35
- data/opal/corelib/unsupported.rb +18 -30
- data/opal/opal.rb +0 -1
- data/opal/opal/mini.rb +1 -0
- data/spec/README.md +6 -4
- data/spec/filters/bugs/array.rb +0 -42
- data/spec/filters/bugs/basicobject.rb +0 -2
- data/spec/filters/bugs/bigdecimal.rb +160 -0
- data/spec/filters/bugs/class.rb +0 -5
- data/spec/filters/bugs/date.rb +1 -48
- data/spec/filters/bugs/enumerable.rb +4 -12
- data/spec/filters/bugs/enumerator.rb +0 -1
- data/spec/filters/bugs/exception.rb +4 -3
- data/spec/filters/bugs/float.rb +4 -2
- data/spec/filters/bugs/kernel.rb +25 -10
- data/spec/filters/bugs/language.rb +119 -68
- data/spec/filters/bugs/method.rb +135 -0
- data/spec/filters/bugs/module.rb +13 -28
- data/spec/filters/bugs/proc.rb +18 -8
- data/spec/filters/bugs/range.rb +0 -3
- data/spec/filters/bugs/rational.rb +4 -0
- data/spec/filters/bugs/regexp.rb +68 -36
- data/spec/filters/bugs/string.rb +1 -1
- data/spec/filters/bugs/struct.rb +0 -12
- data/spec/filters/bugs/time.rb +1 -0
- data/spec/filters/bugs/unboundmethod.rb +2 -1
- data/spec/filters/unsupported/freeze.rb +3 -1
- data/spec/filters/unsupported/language.rb +0 -7
- data/spec/filters/unsupported/privacy.rb +7 -6
- data/spec/filters/unsupported/string.rb +10 -0
- data/spec/filters/unsupported/struct.rb +3 -0
- data/spec/filters/unsupported/symbol.rb +9 -0
- data/spec/filters/unsupported/taint.rb +0 -3
- data/spec/filters/unsupported/thread.rb +1 -0
- data/spec/lib/cli_runners/phantomjs_spec.rb +39 -0
- data/spec/lib/cli_spec.rb +42 -1
- data/spec/lib/compiler/call_spec.rb +700 -0
- data/spec/lib/compiler_spec.rb +46 -28
- data/spec/lib/config_spec.rb +13 -0
- data/spec/lib/parser/call_spec.rb +18 -0
- data/spec/lib/parser/def_spec.rb +29 -0
- data/spec/lib/parser/iter_spec.rb +15 -15
- data/spec/lib/parser/lambda_spec.rb +153 -12
- data/spec/lib/parser/string_spec.rb +5 -0
- data/spec/lib/parser/undef_spec.rb +1 -1
- data/spec/lib/parser/variables_spec.rb +24 -0
- data/spec/lib/paths_spec.rb +12 -5
- data/spec/lib/spec_helper.rb +5 -0
- data/spec/lib/sprockets/processor_spec.rb +6 -5
- data/spec/lib/sprockets_spec.rb +8 -0
- data/spec/mspec-opal/formatters.rb +188 -0
- data/spec/mspec-opal/runner.rb +193 -0
- data/spec/opal/core/enumerator/with_index_spec.rb +6 -0
- data/spec/opal/core/kernel/define_singleton_method_spec.rb +1 -1
- data/spec/opal/core/kernel/instance_variables_spec.rb +14 -0
- data/spec/opal/core/kernel/loop_spec.rb +1 -1
- data/spec/opal/core/kernel/raise_spec.rb +1 -1
- data/spec/opal/core/language/heredoc_spec.rb +42 -0
- data/spec/opal/core/language/rescue_spec.rb +18 -0
- data/spec/opal/core/language_spec.rb +22 -0
- data/spec/opal/core/module/const_defined_spec.rb +1 -2
- data/spec/opal/core/module/name_spec.rb +6 -0
- data/spec/opal/core/runtime/bridged_classes_spec.rb +14 -2
- data/spec/opal/core/runtime/rescue_spec.rb +12 -2
- data/spec/opal/core/runtime/super_spec.rb +1 -0
- data/spec/opal/core/string_spec.rb +21 -0
- data/spec/opal/stdlib/js_spec.rb +1 -1
- data/spec/opal/stdlib/native/hash_spec.rb +7 -0
- data/spec/opal/stdlib/promise/always_spec.rb +24 -5
- data/spec/opal/stdlib/promise/rescue_spec.rb +15 -6
- data/spec/opal/stdlib/promise/then_spec.rb +13 -5
- data/spec/opal/stdlib/promise/trace_spec.rb +5 -6
- data/spec/opal/stdlib/strscan/scan_spec.rb +1 -1
- data/spec/ruby_specs +122 -0
- data/spec/spec_helper.rb +3 -15
- data/stdlib/base64.rb +51 -121
- data/stdlib/bigdecimal.rb +231 -0
- data/stdlib/bigdecimal/bignumber.js.rb +11 -0
- data/stdlib/bigdecimal/kernel.rb +5 -0
- data/stdlib/date.rb +252 -10
- data/stdlib/native.rb +38 -38
- data/stdlib/nodejs/dir.rb +8 -6
- data/stdlib/nodejs/file.rb +28 -3
- data/stdlib/nodejs/node_modules/.bin/js-yaml +1 -0
- data/stdlib/nodejs/node_modules/js-yaml/node_modules/.bin/esparse +1 -0
- data/stdlib/nodejs/node_modules/js-yaml/node_modules/.bin/esvalidate +1 -0
- data/stdlib/nodejs/require.rb +1 -1
- data/stdlib/nodejs/yaml.rb +3 -2
- data/stdlib/opal-parser.rb +7 -2
- data/stdlib/pathname.rb +23 -1
- data/stdlib/phantomjs.rb +10 -0
- data/stdlib/promise.rb +38 -23
- data/tasks/building.rake +3 -3
- data/tasks/testing.rake +27 -14
- data/tasks/testing/mspec_special_calls.rb +1 -1
- data/tasks/testing/sprockets-phantomjs.js +4 -0
- data/test/opal/test_keyword.rb +110 -110
- data/test/opal/unsupported_and_bugs.rb +30 -0
- data/vendored-minitest/minitest/assertions.rb +1 -1
- metadata +65 -15
- data/.spectator +0 -2
- data/.spectator-mspec +0 -3
- data/opal/corelib/array/inheritance.rb +0 -127
- data/spec/rubyspecs +0 -139
data/spec/spec_helper.rb
CHANGED
@@ -4,10 +4,10 @@ require 'opal-parser'
|
|
4
4
|
require 'mspec'
|
5
5
|
require 'mspec/version'
|
6
6
|
require 'support/mspec_rspec_adapter'
|
7
|
-
require 'mspec
|
7
|
+
require 'mspec-opal/runner'
|
8
8
|
|
9
9
|
# Node v0.12 as well as Google Chrome/V8 42.0.2311.135 (64-bit)
|
10
|
-
# showed to need more tolerance (
|
10
|
+
# showed to need more tolerance (ruby spec default is 0.00003)
|
11
11
|
TOLERANCE = 0.00004
|
12
12
|
|
13
13
|
ENV['MSPEC_RUNNER'] = true
|
@@ -23,16 +23,4 @@ module Kernel
|
|
23
23
|
end
|
24
24
|
|
25
25
|
require 'mspec/utils/script' # Needed by DottedFormatter
|
26
|
-
|
27
|
-
|
28
|
-
require 'mspec/opal/formatters'
|
29
|
-
# Uncomment one of the following to use a different formatter:
|
30
|
-
#
|
31
|
-
#formatter_class = BrowserFormatter
|
32
|
-
#formatter_class = NodeJSFormatter
|
33
|
-
#formatter_class = NodeJSDocFormatter
|
34
|
-
#formatter_class = PhantomFormatter
|
35
|
-
#formatter_class = PhantomDocFormatter
|
36
|
-
|
37
|
-
# As soon as this file loads, tell the runner the specs are starting
|
38
|
-
OSpecRunner.main(formatter_class).will_start
|
26
|
+
require 'mspec-opal/formatters'
|
data/stdlib/base64.rb
CHANGED
@@ -1,129 +1,59 @@
|
|
1
1
|
module Base64
|
2
|
+
# FROM https://github.com/davidchambers/Base64.js/blob/69262ec7e1fa4541de5700a1b0b03b0de0e3f5aa/base64.js
|
2
3
|
%x{
|
3
|
-
var
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
break;
|
25
|
-
}
|
26
|
-
|
27
|
-
buffer.push(((b & 15) << 4) | (c >> 2));
|
28
|
-
|
29
|
-
d = lookup[string.charAt(++i)];
|
30
|
-
|
31
|
-
if (d == 64) {
|
32
|
-
break;
|
33
|
-
}
|
34
|
-
|
35
|
-
buffer.push(((c & 3) << 6) | d);
|
36
|
-
}
|
37
|
-
|
38
|
-
return buffer;
|
39
|
-
}
|
40
|
-
|
41
|
-
function decode(string) {
|
42
|
-
var buffer = from(string),
|
43
|
-
result = [], a, b, c;
|
44
|
-
|
45
|
-
for (var i = 0, length = buffer.length; i < length; i++) {
|
46
|
-
if (buffer[i] < 128) {
|
47
|
-
result.push(String.fromCharCode(buffer[i]));
|
48
|
-
}
|
49
|
-
else if (buffer[i] > 191 && buffer[i] < 224) {
|
50
|
-
a = (buffer[i] & 31) << 6;
|
51
|
-
b = buffer[++i] & 63;
|
52
|
-
|
53
|
-
result.push(String.fromCharCode(a | b));
|
54
|
-
}
|
55
|
-
else {
|
56
|
-
a = (buffer[i] & 15) << 12;
|
57
|
-
b = (buffer[++i] & 63) << 6;
|
58
|
-
c = buffer[++i] & 63;
|
59
|
-
|
60
|
-
result.push(String.fromCharCode(a | b | c));
|
4
|
+
var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
|
5
|
+
var encode, decode;
|
6
|
+
|
7
|
+
// encoder
|
8
|
+
// [https://gist.github.com/999166] by [https://github.com/nignag]
|
9
|
+
encode = Opal.global.btoa || function (input) {
|
10
|
+
var str = String(input);
|
11
|
+
/* jshint ignore:start */
|
12
|
+
for (
|
13
|
+
// initialize result and counter
|
14
|
+
var block, charCode, idx = 0, map = chars, output = '';
|
15
|
+
// if the next str index does not exist:
|
16
|
+
// change the mapping table to "="
|
17
|
+
// check if d has no fractional digits
|
18
|
+
str.charAt(idx | 0) || (map = '=', idx % 1);
|
19
|
+
// "8 - idx % 1 * 8" generates the sequence 2, 4, 6, 8
|
20
|
+
output += map.charAt(63 & block >> 8 - idx % 1 * 8)
|
21
|
+
) {
|
22
|
+
charCode = str.charCodeAt(idx += 3/4);
|
23
|
+
if (charCode > 0xFF) {
|
24
|
+
#{raise ArgumentError, 'invalid character (failed: The string to be encoded contains characters outside of the Latin1 range.)'};
|
61
25
|
}
|
26
|
+
block = block << 8 | charCode;
|
62
27
|
}
|
63
|
-
|
64
|
-
|
65
|
-
}
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
}
|
74
|
-
}
|
75
|
-
else {
|
76
|
-
for (i = 0, length = string.length; i < length; i++) {
|
77
|
-
var ch = string.charCodeAt(i);
|
78
|
-
|
79
|
-
if (ch < 128) {
|
80
|
-
buffer.push(ch);
|
81
|
-
}
|
82
|
-
else if (ch < 2048) {
|
83
|
-
buffer.push((ch >> 6) | 192);
|
84
|
-
buffer.push((ch & 63) | 128);
|
85
|
-
}
|
86
|
-
else {
|
87
|
-
buffer.push((ch >> 12) | 224);
|
88
|
-
buffer.push(((ch >> 6) & 63) | 128);
|
89
|
-
buffer.push((ch & 63) | 128);
|
90
|
-
}
|
91
|
-
}
|
28
|
+
return output;
|
29
|
+
/* jshint ignore:end */
|
30
|
+
};
|
31
|
+
|
32
|
+
// decoder
|
33
|
+
// [https://gist.github.com/1020396] by [https://github.com/atk]
|
34
|
+
decode = Opal.global.atob || function (input) {
|
35
|
+
var str = String(input).replace(/=+$/, '');
|
36
|
+
if (str.length % 4 == 1) {
|
37
|
+
#{raise ArgumentError, 'invalid base64 (failed: The string to be decoded is not correctly encoded.)'};
|
92
38
|
}
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
f = 0,
|
108
|
-
g = 0;
|
109
|
-
|
110
|
-
if (isNaN(a)) {
|
111
|
-
f = g = 64;
|
112
|
-
}
|
113
|
-
else {
|
114
|
-
c = buffer[++i];
|
115
|
-
f = ((b & 15) << 2) | (c >> 6);
|
116
|
-
g = isNaN(c) ? 64 : c & 63;
|
117
|
-
}
|
118
|
-
|
119
|
-
result.push(charset.charAt(d));
|
120
|
-
result.push(charset.charAt(e));
|
121
|
-
result.push(charset.charAt(f));
|
122
|
-
result.push(charset.charAt(g));
|
39
|
+
/* jshint ignore:start */
|
40
|
+
for (
|
41
|
+
// initialize result and counters
|
42
|
+
var bc = 0, bs, buffer, idx = 0, output = '';
|
43
|
+
// get next character
|
44
|
+
buffer = str.charAt(idx++);
|
45
|
+
// character found in table? initialize bit storage and add its ascii value;
|
46
|
+
~buffer && (bs = bc % 4 ? bs * 64 + buffer : buffer,
|
47
|
+
// and if not first of each 4 characters,
|
48
|
+
// convert the first 8 bits to one ascii character
|
49
|
+
bc++ % 4) ? output += String.fromCharCode(255 & bs >> (-2 * bc & 6)) : 0
|
50
|
+
) {
|
51
|
+
// try to find character in table (0-63, not found => -1)
|
52
|
+
buffer = chars.indexOf(buffer);
|
123
53
|
}
|
124
|
-
|
125
|
-
|
126
|
-
}
|
54
|
+
return output;
|
55
|
+
/* jshint ignore:end */
|
56
|
+
};
|
127
57
|
}
|
128
58
|
|
129
59
|
def self.decode64(string)
|
@@ -131,7 +61,7 @@ module Base64
|
|
131
61
|
end
|
132
62
|
|
133
63
|
def self.encode64(string)
|
134
|
-
`encode(string).replace(/(.{60})/g, "$1\n")`
|
64
|
+
`encode(string).replace(/(.{60})/g, "$1\n").replace(/([^\n])$/g, "$1\n")`
|
135
65
|
end
|
136
66
|
|
137
67
|
def self.strict_decode64(string)
|
@@ -0,0 +1,231 @@
|
|
1
|
+
class BigDecimal < Numeric; end
|
2
|
+
|
3
|
+
require 'js'
|
4
|
+
require 'bigdecimal/kernel'
|
5
|
+
require 'bigdecimal/bignumber.js'
|
6
|
+
|
7
|
+
class BigDecimal
|
8
|
+
ROUND_MODE = 256
|
9
|
+
|
10
|
+
# NOTE: the numeric values of the ROUND_* constants
|
11
|
+
# follow BigNumber.js, they are NOT the same as MRI
|
12
|
+
ROUND_UP = 0
|
13
|
+
ROUND_DOWN = 1
|
14
|
+
ROUND_CEILING = 2
|
15
|
+
ROUND_FLOOR = 3
|
16
|
+
ROUND_HALF_UP = 4
|
17
|
+
ROUND_HALF_DOWN = 5
|
18
|
+
ROUND_HALF_EVEN = 6
|
19
|
+
|
20
|
+
SIGN_NaN = 0
|
21
|
+
SIGN_POSITIVE_ZERO = 1
|
22
|
+
SIGN_NEGATIVE_ZERO = -1
|
23
|
+
SIGN_POSITIVE_FINITE = 2
|
24
|
+
SIGN_NEGATIVE_FINITE = -2
|
25
|
+
SIGN_POSITIVE_INFINITE = 3
|
26
|
+
SIGN_NEGATIVE_INFINITE = -3
|
27
|
+
|
28
|
+
def self.limit(digits = nil)
|
29
|
+
@digits = digits if digits
|
30
|
+
@digits
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.mode(mode, value = nil)
|
34
|
+
case mode
|
35
|
+
when ROUND_MODE
|
36
|
+
@round_mode = value if value
|
37
|
+
@round_mode || ROUND_HALF_UP
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
attr_reader :bignumber
|
42
|
+
|
43
|
+
def initialize(initial, digits = 0)
|
44
|
+
@bignumber = JS.new(BigNumber, initial)
|
45
|
+
end
|
46
|
+
|
47
|
+
def ==(other)
|
48
|
+
case other
|
49
|
+
when self.class
|
50
|
+
bignumber.JS.equals(other.bignumber)
|
51
|
+
when Number
|
52
|
+
bignumber.JS.equals(other)
|
53
|
+
else
|
54
|
+
false
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
alias === ==
|
59
|
+
|
60
|
+
def <=>(other)
|
61
|
+
case other
|
62
|
+
when self.class
|
63
|
+
result = bignumber.JS.comparedTo(other.bignumber)
|
64
|
+
when Number
|
65
|
+
result = bignumber.JS.comparedTo(other)
|
66
|
+
else
|
67
|
+
result = nil
|
68
|
+
end
|
69
|
+
`#{result} === null ? nil : #{result}`
|
70
|
+
end
|
71
|
+
|
72
|
+
def <(other)
|
73
|
+
return false if nan? || other && other.nan?
|
74
|
+
super
|
75
|
+
end
|
76
|
+
|
77
|
+
def <=(other)
|
78
|
+
return false if nan? || other && other.nan?
|
79
|
+
super
|
80
|
+
end
|
81
|
+
|
82
|
+
def >(other)
|
83
|
+
return false if nan? || other && other.nan?
|
84
|
+
super
|
85
|
+
end
|
86
|
+
|
87
|
+
def >=(other)
|
88
|
+
return false if nan? || other && other.nan?
|
89
|
+
super
|
90
|
+
end
|
91
|
+
|
92
|
+
def abs
|
93
|
+
self.class.new(bignumber.JS.abs)
|
94
|
+
end
|
95
|
+
|
96
|
+
def add(other, digits = 0)
|
97
|
+
if digits == nil
|
98
|
+
raise TypeError, 'wrong argument type nil (expected Fixnum)'
|
99
|
+
end
|
100
|
+
|
101
|
+
if digits < 0
|
102
|
+
raise ArgumentError, 'argument must be positive'
|
103
|
+
end
|
104
|
+
|
105
|
+
other, _ = coerce(other)
|
106
|
+
|
107
|
+
result = bignumber.JS.plus(other.bignumber)
|
108
|
+
|
109
|
+
if digits > 0
|
110
|
+
result = result.JS.toDigits(digits, self.class.mode(ROUND_MODE))
|
111
|
+
end
|
112
|
+
|
113
|
+
self.class.new(result)
|
114
|
+
end
|
115
|
+
|
116
|
+
alias + add
|
117
|
+
|
118
|
+
def ceil(n = nil)
|
119
|
+
unless bignumber.JS.isFinite
|
120
|
+
raise FloatDomainError, "Computation results to 'Infinity'"
|
121
|
+
end
|
122
|
+
|
123
|
+
if n == nil
|
124
|
+
bignumber.JS.round(0, ROUND_CEILING).JS.toNumber
|
125
|
+
elsif n >= 0
|
126
|
+
self.class.new(bignumber.JS.round(n, ROUND_CEILING))
|
127
|
+
else
|
128
|
+
self.class.new(bignumber.JS.round(0, ROUND_CEILING))
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
def coerce(other)
|
133
|
+
case other
|
134
|
+
when self.class
|
135
|
+
[other, self]
|
136
|
+
when Number
|
137
|
+
[self.class.new(other), self]
|
138
|
+
else
|
139
|
+
raise TypeError, "#{other.class} can't be coerced into #{self.class}"
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
def div(other, digits = nil)
|
144
|
+
return self / other if digits == 0
|
145
|
+
|
146
|
+
other, _ = coerce(other)
|
147
|
+
|
148
|
+
if nan? || other.nan?
|
149
|
+
raise FloatDomainError, "Computation results to 'NaN'(Not a Number)"
|
150
|
+
end
|
151
|
+
|
152
|
+
if digits == nil
|
153
|
+
if other.zero?
|
154
|
+
raise ZeroDivisionError, "divided by 0"
|
155
|
+
end
|
156
|
+
|
157
|
+
if infinite?
|
158
|
+
raise FloatDomainError, "Computation results to 'Infinity'"
|
159
|
+
end
|
160
|
+
|
161
|
+
return self.class.new(bignumber.JS.dividedToIntegerBy(other.bignumber))
|
162
|
+
end
|
163
|
+
|
164
|
+
self.class.new(bignumber.JS.dividedBy(other.bignumber).JS.round(digits, self.class.mode(ROUND_MODE)))
|
165
|
+
end
|
166
|
+
|
167
|
+
def finite?
|
168
|
+
bignumber.JS.isFinite
|
169
|
+
end
|
170
|
+
|
171
|
+
def infinite?
|
172
|
+
return nil if finite? || nan?
|
173
|
+
bignumber.JS.isNegative ? -1 : 1
|
174
|
+
end
|
175
|
+
|
176
|
+
def minus(other)
|
177
|
+
other, _ = coerce(other)
|
178
|
+
self.class.new(bignumber.JS.minus(other.bignumber))
|
179
|
+
end
|
180
|
+
|
181
|
+
alias - minus
|
182
|
+
|
183
|
+
def mult(other, digits = nil)
|
184
|
+
other, _ = coerce(other)
|
185
|
+
|
186
|
+
if digits == nil
|
187
|
+
return self.class.new(bignumber.JS.times(other.bignumber))
|
188
|
+
end
|
189
|
+
|
190
|
+
self.class.new(bignumber.JS.times(other.bignumber).JS.round(digits, self.class.mode(ROUND_MODE)))
|
191
|
+
end
|
192
|
+
|
193
|
+
alias * mult
|
194
|
+
|
195
|
+
def nan?
|
196
|
+
bignumber.JS.isNaN
|
197
|
+
end
|
198
|
+
|
199
|
+
def quo(other)
|
200
|
+
other, _ = coerce(other)
|
201
|
+
self.class.new(bignumber.JS.dividedBy(other.bignumber))
|
202
|
+
end
|
203
|
+
|
204
|
+
alias / quo
|
205
|
+
|
206
|
+
def sign
|
207
|
+
if bignumber.JS.isNaN
|
208
|
+
return SIGN_NaN
|
209
|
+
end
|
210
|
+
if bignumber.JS.isZero
|
211
|
+
return bignumber.JS.isNegative ? SIGN_NEGATIVE_ZERO : SIGN_POSITIVE_ZERO
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
215
|
+
def sub(other, precision)
|
216
|
+
other, _ = coerce(other)
|
217
|
+
self.class.new(bignumber.JS.minus(other.bignumber))
|
218
|
+
end
|
219
|
+
|
220
|
+
def to_f
|
221
|
+
bignumber.JS.toNumber
|
222
|
+
end
|
223
|
+
|
224
|
+
def to_s(s = '')
|
225
|
+
bignumber.JS.toString
|
226
|
+
end
|
227
|
+
|
228
|
+
def zero?
|
229
|
+
bignumber.JS.isZero
|
230
|
+
end
|
231
|
+
end
|