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.
- checksums.yaml +4 -4
- data/.eslintrc.js +1 -0
- data/.github/workflows/build.yml +0 -3
- data/.rubocop.yml +5 -1
- data/UNRELEASED.md +66 -2
- data/benchmark-ips/bm_truthy.rb +30 -0
- data/bin/opal-mspec +1 -3
- data/bin/opal-repl +1 -2
- data/bin/remove-filters +1 -4
- data/docs/compiled_ruby.md +10 -6
- data/exe/opal-repl +1 -3
- data/lib/opal/ast/builder.rb +1 -1
- data/lib/opal/cli.rb +2 -2
- data/lib/opal/cli_runners/nodejs.rb +9 -2
- data/lib/opal/cli_runners/source-map-support-browser.js +80 -216
- data/lib/opal/cli_runners/source-map-support-node.js +80 -216
- data/lib/opal/cli_runners/source-map-support.js +5 -1
- data/lib/opal/cli_runners/system_runner.rb +10 -4
- data/lib/opal/compiler.rb +3 -5
- data/lib/opal/fragment.rb +5 -1
- data/lib/opal/nodes/args/extract_block_arg.rb +1 -8
- data/lib/opal/nodes/args/extract_kwoptarg.rb +1 -3
- data/lib/opal/nodes/args/extract_optarg.rb +1 -3
- data/lib/opal/nodes/args/extract_post_arg.rb +2 -5
- data/lib/opal/nodes/args/extract_post_optarg.rb +2 -7
- data/lib/opal/nodes/args/initialize_iterarg.rb +1 -3
- data/lib/opal/nodes/args/prepare_post_args.rb +5 -1
- data/lib/opal/nodes/base.rb +3 -2
- data/lib/opal/nodes/call.rb +20 -9
- data/lib/opal/nodes/call_special.rb +50 -0
- data/lib/opal/nodes/class.rb +24 -15
- data/lib/opal/nodes/constants.rb +23 -5
- data/lib/opal/nodes/def.rb +20 -23
- data/lib/opal/nodes/defined.rb +5 -5
- data/lib/opal/nodes/definitions.rb +2 -2
- data/lib/opal/nodes/defs.rb +2 -5
- data/lib/opal/nodes/helpers.rb +48 -18
- data/lib/opal/nodes/if.rb +109 -8
- data/lib/opal/nodes/iter.rb +23 -16
- data/lib/opal/nodes/literal.rb +18 -4
- data/lib/opal/nodes/logic.rb +2 -1
- data/lib/opal/nodes/masgn.rb +4 -9
- data/lib/opal/nodes/module.rb +29 -19
- data/lib/opal/nodes/node_with_args.rb +1 -7
- data/lib/opal/nodes/scope.rb +54 -15
- data/lib/opal/nodes/singleton_class.rb +5 -3
- data/lib/opal/nodes/super.rb +3 -3
- data/lib/opal/nodes/top.rb +34 -31
- data/lib/opal/nodes/variables.rb +2 -2
- data/lib/opal/nodes/x_string.rb +3 -0
- data/lib/opal/nodes.rb +0 -1
- data/lib/opal/parser/patch.rb +75 -0
- data/lib/opal/parser/with_ruby_lexer.rb +1 -1
- data/lib/opal/regexp_anchors.rb +7 -7
- data/lib/opal/requires.rb +19 -0
- data/lib/opal/rewriters/pattern_matching.rb +1 -1
- data/lib/opal/rewriters/returnable_logic.rb +102 -4
- data/lib/opal/util.rb +2 -2
- data/lib/opal/version.rb +1 -1
- data/lib/opal.rb +1 -17
- data/opal/corelib/array/pack.rb +11 -11
- data/opal/corelib/array.rb +193 -152
- data/opal/corelib/basic_object.rb +14 -14
- data/opal/corelib/binding.rb +7 -7
- data/opal/corelib/boolean.rb +12 -15
- data/opal/corelib/class.rb +23 -1
- data/opal/corelib/comparable.rb +8 -8
- data/opal/corelib/complex/base.rb +2 -2
- data/opal/corelib/complex.rb +79 -88
- data/opal/corelib/constants.rb +9 -9
- data/opal/corelib/dir.rb +4 -3
- data/opal/corelib/enumerable.rb +140 -127
- data/opal/corelib/enumerator/arithmetic_sequence.rb +177 -0
- data/opal/corelib/enumerator/chain.rb +42 -0
- data/opal/corelib/enumerator/generator.rb +35 -0
- data/opal/corelib/enumerator/lazy.rb +243 -0
- data/opal/corelib/enumerator/yielder.rb +36 -0
- data/opal/corelib/enumerator.rb +45 -300
- data/opal/corelib/error/errno.rb +47 -0
- data/opal/corelib/error.rb +62 -60
- data/opal/corelib/file.rb +26 -12
- data/opal/corelib/hash.rb +98 -107
- data/opal/corelib/helpers.rb +62 -13
- data/opal/corelib/io.rb +47 -34
- data/opal/corelib/kernel/format.rb +29 -29
- data/opal/corelib/kernel.rb +86 -83
- data/opal/corelib/main.rb +14 -12
- data/opal/corelib/marshal/read_buffer.rb +15 -15
- data/opal/corelib/marshal/write_buffer.rb +45 -44
- data/opal/corelib/marshal.rb +3 -3
- data/opal/corelib/math.rb +50 -50
- data/opal/corelib/method.rb +12 -8
- data/opal/corelib/module.rb +96 -79
- data/opal/corelib/nil.rb +9 -11
- data/opal/corelib/number.rb +113 -118
- data/opal/corelib/numeric.rb +37 -33
- data/opal/corelib/object_space.rb +11 -10
- data/opal/corelib/pack_unpack/format_string_parser.rb +3 -3
- data/opal/corelib/pattern_matching/base.rb +7 -7
- data/opal/corelib/pattern_matching.rb +1 -1
- data/opal/corelib/proc.rb +15 -16
- data/opal/corelib/process/base.rb +2 -2
- data/opal/corelib/process/status.rb +21 -0
- data/opal/corelib/process.rb +5 -5
- data/opal/corelib/random/formatter.rb +11 -11
- data/opal/corelib/random/math_random.js.rb +1 -1
- data/opal/corelib/random/mersenne_twister.rb +3 -3
- data/opal/corelib/random/seedrandom.js.rb +3 -3
- data/opal/corelib/random.rb +17 -17
- data/opal/corelib/range.rb +51 -35
- data/opal/corelib/rational/base.rb +4 -4
- data/opal/corelib/rational.rb +61 -62
- data/opal/corelib/regexp.rb +47 -38
- data/opal/corelib/runtime.js +245 -139
- data/opal/corelib/string/encoding.rb +21 -21
- data/opal/corelib/string/unpack.rb +19 -14
- data/opal/corelib/string.rb +135 -128
- data/opal/corelib/struct.rb +59 -46
- data/opal/corelib/time.rb +47 -57
- data/opal/corelib/trace_point.rb +2 -2
- data/opal/corelib/unsupported.rb +31 -120
- data/opal/corelib/variables.rb +3 -3
- data/opal/opal/base.rb +9 -8
- data/opal/opal/full.rb +8 -8
- data/opal/opal/mini.rb +17 -17
- data/opal/opal.rb +17 -18
- data/opal.gemspec +1 -1
- data/spec/filters/bugs/array.rb +4 -24
- data/spec/filters/bugs/bigdecimal.rb +0 -23
- data/spec/filters/bugs/binding.rb +0 -1
- data/spec/filters/bugs/boolean.rb +3 -0
- data/spec/filters/bugs/class.rb +2 -0
- data/spec/filters/bugs/date.rb +0 -5
- data/spec/filters/bugs/encoding.rb +8 -50
- data/spec/filters/bugs/enumerable.rb +4 -1
- data/spec/filters/bugs/enumerator.rb +3 -36
- data/spec/filters/bugs/exception.rb +0 -2
- data/spec/filters/bugs/file.rb +0 -2
- data/spec/filters/bugs/float.rb +0 -3
- data/spec/filters/bugs/hash.rb +5 -3
- data/spec/filters/bugs/integer.rb +2 -3
- data/spec/filters/bugs/kernel.rb +2 -31
- data/spec/filters/bugs/language.rb +29 -49
- data/spec/filters/bugs/main.rb +0 -2
- data/spec/filters/bugs/marshal.rb +2 -3
- data/spec/filters/bugs/matrix.rb +0 -36
- data/spec/filters/bugs/module.rb +7 -61
- data/spec/filters/bugs/numeric.rb +0 -7
- data/spec/filters/bugs/objectspace.rb +1 -1
- data/spec/filters/bugs/pack_unpack.rb +0 -4
- data/spec/filters/bugs/proc.rb +0 -9
- data/spec/filters/bugs/random.rb +0 -5
- data/spec/filters/bugs/range.rb +1 -6
- data/spec/filters/bugs/regexp.rb +0 -3
- data/spec/filters/bugs/set.rb +8 -1
- data/spec/filters/bugs/string.rb +9 -34
- data/spec/filters/bugs/stringscanner.rb +8 -7
- data/spec/filters/bugs/struct.rb +2 -3
- data/spec/filters/bugs/symbol.rb +0 -1
- data/spec/filters/bugs/time.rb +0 -8
- data/spec/filters/bugs/unboundmethod.rb +0 -8
- data/spec/filters/bugs/warnings.rb +1 -7
- data/spec/filters/unsupported/freeze.rb +24 -0
- data/spec/filters/unsupported/integer.rb +1 -0
- data/spec/filters/unsupported/kernel.rb +12 -0
- data/spec/filters/unsupported/privacy.rb +3 -0
- data/spec/filters/unsupported/string.rb +2 -0
- data/spec/lib/builder_spec.rb +2 -2
- data/spec/lib/cli_spec.rb +1 -1
- data/spec/lib/compiler_spec.rb +37 -37
- data/spec/lib/simple_server_spec.rb +2 -2
- data/spec/lib/source_map/file_spec.rb +1 -1
- data/spec/opal/compiler/irb_spec.rb +2 -2
- data/spec/opal/core/kernel/puts_spec.rb +90 -0
- data/spec/opal/core/language/super_spec.rb +24 -0
- data/spec/ruby_specs +4 -2
- data/spec/support/rewriters_helper.rb +1 -1
- data/stdlib/bigdecimal.rb +7 -11
- data/stdlib/buffer/view.rb +2 -2
- data/stdlib/buffer.rb +2 -2
- data/stdlib/date.rb +5 -6
- data/stdlib/erb.rb +1 -0
- data/stdlib/js.rb +2 -1
- data/stdlib/native.rb +7 -8
- data/stdlib/nodejs/argf.rb +4 -4
- data/stdlib/nodejs/base.rb +29 -0
- data/stdlib/nodejs/dir.rb +1 -1
- data/stdlib/nodejs/env.rb +6 -9
- data/stdlib/nodejs/file.rb +23 -17
- data/stdlib/nodejs/fileutils.rb +3 -3
- data/stdlib/nodejs/io.rb +2 -20
- data/stdlib/nodejs/irb.rb +0 -0
- data/stdlib/nodejs/kernel.rb +2 -37
- data/stdlib/nodejs.rb +1 -3
- data/stdlib/opal/miniracer.rb +2 -0
- data/stdlib/opal/platform.rb +6 -13
- data/stdlib/opal/replutils.rb +16 -5
- data/stdlib/opal-parser.rb +2 -2
- data/stdlib/optparse/ac.rb +54 -0
- data/stdlib/optparse/date.rb +14 -0
- data/stdlib/optparse/kwargs.rb +22 -0
- data/stdlib/optparse/shellwords.rb +7 -0
- data/stdlib/optparse/time.rb +15 -0
- data/stdlib/optparse/uri.rb +7 -0
- data/stdlib/optparse/version.rb +69 -0
- data/stdlib/optparse.rb +2279 -0
- data/stdlib/pathname.rb +5 -6
- data/stdlib/pp.rb +18 -2
- data/stdlib/promise/v2.rb +18 -22
- data/stdlib/promise.rb +15 -21
- data/stdlib/set.rb +32 -32
- data/stdlib/shellwords.rb +240 -0
- data/stdlib/stringio.rb +3 -6
- data/stdlib/strscan.rb +5 -8
- data/stdlib/template.rb +2 -2
- data/stdlib/thread.rb +7 -9
- data/tasks/performance.rake +5 -2
- data/tasks/testing/mspec_special_calls.rb +0 -12
- data/tasks/testing.rake +55 -37
- data/test/nodejs/test_file.rb +11 -0
- metadata +55 -10
- data/lib/opal/nodes/case.rb +0 -114
data/opal/corelib/helpers.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# helpers: type_error, coerce_to
|
2
2
|
|
3
|
-
module Opal
|
3
|
+
module ::Opal
|
4
4
|
def self.bridge(constructor, klass)
|
5
5
|
`Opal.bridge(constructor, klass)`
|
6
6
|
end
|
@@ -9,7 +9,7 @@ module Opal
|
|
9
9
|
coerced = `$coerce_to(object, type, method, args)`
|
10
10
|
|
11
11
|
unless type === coerced
|
12
|
-
raise `$type_error(object, type, method, coerced)`
|
12
|
+
::Kernel.raise `$type_error(object, type, method, coerced)`
|
13
13
|
end
|
14
14
|
|
15
15
|
coerced
|
@@ -23,7 +23,7 @@ module Opal
|
|
23
23
|
return if coerced.nil?
|
24
24
|
|
25
25
|
unless type === coerced
|
26
|
-
raise `$type_error(object, type, method, coerced)`
|
26
|
+
::Kernel.raise `$type_error(object, type, method, coerced)`
|
27
27
|
end
|
28
28
|
|
29
29
|
coerced
|
@@ -41,7 +41,7 @@ module Opal
|
|
41
41
|
compare = a <=> b
|
42
42
|
|
43
43
|
if `compare === nil`
|
44
|
-
raise ArgumentError, "comparison of #{a.class} with #{b.class} failed"
|
44
|
+
::Kernel.raise ::ArgumentError, "comparison of #{a.class} with #{b.class} failed"
|
45
45
|
end
|
46
46
|
|
47
47
|
compare
|
@@ -75,20 +75,20 @@ module Opal
|
|
75
75
|
end
|
76
76
|
|
77
77
|
def self.instance_variable_name!(name)
|
78
|
-
name = Opal.coerce_to!(name, String, :to_str)
|
78
|
+
name = ::Opal.coerce_to!(name, ::String, :to_str)
|
79
79
|
|
80
80
|
unless `/^@[a-zA-Z_][a-zA-Z0-9_]*?$/.test(name)`
|
81
|
-
raise NameError.new("'#{name}' is not allowed as an instance variable name", name)
|
81
|
+
::Kernel.raise ::NameError.new("'#{name}' is not allowed as an instance variable name", name)
|
82
82
|
end
|
83
83
|
|
84
84
|
name
|
85
85
|
end
|
86
86
|
|
87
87
|
def self.class_variable_name!(name)
|
88
|
-
name = Opal.coerce_to!(name, String, :to_str)
|
88
|
+
name = ::Opal.coerce_to!(name, ::String, :to_str)
|
89
89
|
|
90
90
|
if `name.length < 3 || name.slice(0,2) !== '@@'`
|
91
|
-
raise NameError.new("`#{name}' is not allowed as a class variable name", name)
|
91
|
+
::Kernel.raise ::NameError.new("`#{name}' is not allowed as a class variable name", name)
|
92
92
|
end
|
93
93
|
|
94
94
|
name
|
@@ -97,7 +97,7 @@ module Opal
|
|
97
97
|
def self.const_name?(const_name)
|
98
98
|
%x{
|
99
99
|
if (typeof const_name !== 'string') {
|
100
|
-
#{const_name = Opal.coerce_to!(const_name, String, :to_str)}
|
100
|
+
#{const_name = ::Opal.coerce_to!(const_name, ::String, :to_str)}
|
101
101
|
}
|
102
102
|
|
103
103
|
return #{const_name}[0] === #{const_name}[0].toUpperCase()
|
@@ -105,11 +105,13 @@ module Opal
|
|
105
105
|
end
|
106
106
|
|
107
107
|
def self.const_name!(const_name)
|
108
|
-
const_name = Opal.coerce_to!(const_name, String, :to_str)
|
108
|
+
const_name = ::Opal.coerce_to!(const_name, ::String, :to_str) if defined? ::String
|
109
109
|
|
110
|
-
|
111
|
-
|
112
|
-
|
110
|
+
%x{
|
111
|
+
if (!const_name || const_name[0] != const_name[0].toUpperCase()) {
|
112
|
+
#{raise ::NameError, "wrong constant name #{const_name}"}
|
113
|
+
}
|
114
|
+
}
|
113
115
|
|
114
116
|
const_name
|
115
117
|
end
|
@@ -154,4 +156,51 @@ module Opal
|
|
154
156
|
}
|
155
157
|
nil
|
156
158
|
end
|
159
|
+
|
160
|
+
`var inspect_stack = []`
|
161
|
+
|
162
|
+
# Performs a safe call to inspect for any value, whether
|
163
|
+
# native or Opal-wrapped.
|
164
|
+
#
|
165
|
+
# @param value [Object]
|
166
|
+
# @return [String]
|
167
|
+
def self.inspect(value = undefined)
|
168
|
+
`var pushed = false`
|
169
|
+
begin
|
170
|
+
%x{
|
171
|
+
if (value === null) {
|
172
|
+
// JS null value
|
173
|
+
return 'null';
|
174
|
+
}
|
175
|
+
else if (value === undefined) {
|
176
|
+
// JS undefined value
|
177
|
+
return 'undefined';
|
178
|
+
}
|
179
|
+
else if (typeof value.$$class === 'undefined') {
|
180
|
+
// JS object / other value that is not bridged
|
181
|
+
return Object.prototype.toString.apply(value);
|
182
|
+
}
|
183
|
+
else if (typeof value.$inspect !== 'function' || value.$inspect.$$stub) {
|
184
|
+
// BasicObject and friends
|
185
|
+
return #{"#<#{`value.$$class`}:0x#{value.__id__.to_s(16)}>"}
|
186
|
+
}
|
187
|
+
else if (inspect_stack.indexOf(#{value.__id__}) !== -1) {
|
188
|
+
// inspect recursing inside inspect to find out about the
|
189
|
+
// same object
|
190
|
+
return #{"#<#{`value.$$class`}:0x#{value.__id__.to_s(16)}>"}
|
191
|
+
}
|
192
|
+
else {
|
193
|
+
// anything supporting Opal
|
194
|
+
inspect_stack.push(#{value.__id__});
|
195
|
+
pushed = true;
|
196
|
+
return value.$inspect();
|
197
|
+
}
|
198
|
+
}
|
199
|
+
nil
|
200
|
+
rescue ::Exception => e # rubocop:disable Lint/RescueException
|
201
|
+
"#<#{`value.$$class`}:0x#{value.__id__.to_s(16)}>"
|
202
|
+
ensure
|
203
|
+
`if (pushed) inspect_stack.pop()`
|
204
|
+
end
|
205
|
+
end
|
157
206
|
end
|
data/opal/corelib/io.rb
CHANGED
@@ -1,12 +1,16 @@
|
|
1
|
-
class IO
|
2
|
-
SEEK_SET = 0
|
3
|
-
SEEK_CUR = 1
|
4
|
-
SEEK_END = 2
|
5
|
-
SEEK_DATA = 3
|
6
|
-
SEEK_HOLE = 4
|
1
|
+
class ::IO
|
2
|
+
self::SEEK_SET = 0
|
3
|
+
self::SEEK_CUR = 1
|
4
|
+
self::SEEK_END = 2
|
5
|
+
self::SEEK_DATA = 3
|
6
|
+
self::SEEK_HOLE = 4
|
7
7
|
|
8
|
-
READABLE = 1
|
9
|
-
WRITABLE = 4
|
8
|
+
self::READABLE = 1
|
9
|
+
self::WRITABLE = 4
|
10
|
+
|
11
|
+
attr_reader :eof
|
12
|
+
|
13
|
+
attr_accessor :read_proc, :sync, :tty, :write_proc
|
10
14
|
|
11
15
|
def initialize(fd, flags = 'r')
|
12
16
|
@fd = fd
|
@@ -28,19 +32,11 @@ class IO
|
|
28
32
|
`self.tty == true`
|
29
33
|
end
|
30
34
|
|
31
|
-
attr_accessor :write_proc
|
32
|
-
attr_accessor :read_proc
|
33
|
-
|
34
35
|
def write(string)
|
35
36
|
`self.write_proc(string)`
|
36
37
|
string.size
|
37
38
|
end
|
38
39
|
|
39
|
-
attr_accessor :sync, :tty
|
40
|
-
|
41
|
-
attr_reader :eof
|
42
|
-
alias eof? eof
|
43
|
-
|
44
40
|
def flush
|
45
41
|
# noop
|
46
42
|
end
|
@@ -53,7 +49,7 @@ class IO
|
|
53
49
|
def print(*args)
|
54
50
|
%x{
|
55
51
|
for (var i = 0, ii = args.length; i < ii; i++) {
|
56
|
-
args[i] = #{String(`args[i]`)}
|
52
|
+
args[i] = #{::Kernel.String(`args[i]`)}
|
57
53
|
}
|
58
54
|
self.$write(args.join(#{$,}));
|
59
55
|
}
|
@@ -62,10 +58,26 @@ class IO
|
|
62
58
|
|
63
59
|
def puts(*args)
|
64
60
|
%x{
|
65
|
-
|
66
|
-
|
61
|
+
var line
|
62
|
+
if (args.length === 0) {
|
63
|
+
#{write "\n"};
|
64
|
+
return nil;
|
65
|
+
} else {
|
66
|
+
for (var i = 0, ii = args.length; i < ii; i++) {
|
67
|
+
if (args[i].$$is_array){
|
68
|
+
var ary = #{`args[i]`.flatten}
|
69
|
+
if (ary.length > 0) #{puts(*`ary`)}
|
70
|
+
} else {
|
71
|
+
if (args[i].$$is_string) {
|
72
|
+
line = args[i].valueOf();
|
73
|
+
} else {
|
74
|
+
line = #{::Kernel.String(`args[i]`)};
|
75
|
+
}
|
76
|
+
if (!line.endsWith("\n")) line += "\n"
|
77
|
+
#{write `line`}
|
78
|
+
}
|
79
|
+
}
|
67
80
|
}
|
68
|
-
self.$write(args.concat([nil]).join(#{$/}));
|
69
81
|
}
|
70
82
|
nil
|
71
83
|
end
|
@@ -99,11 +111,11 @@ class IO
|
|
99
111
|
end
|
100
112
|
|
101
113
|
def readchar
|
102
|
-
getc || raise(EOFError, 'end of file reached')
|
114
|
+
getc || ::Kernel.raise(::EOFError, 'end of file reached')
|
103
115
|
end
|
104
116
|
|
105
117
|
def readline(*args)
|
106
|
-
gets(*args) || raise(EOFError, 'end of file reached')
|
118
|
+
gets(*args) || ::Kernel.raise(::EOFError, 'end of file reached')
|
107
119
|
end
|
108
120
|
|
109
121
|
def gets(sep = false, limit = nil, opts = {})
|
@@ -164,14 +176,14 @@ class IO
|
|
164
176
|
def sysread(integer)
|
165
177
|
`self.read_proc(integer)` || begin
|
166
178
|
@eof = true
|
167
|
-
raise EOFError, 'end of file reached'
|
179
|
+
::Kernel.raise ::EOFError, 'end of file reached'
|
168
180
|
end
|
169
181
|
end
|
170
182
|
|
171
183
|
# @private
|
172
184
|
def sysread_noraise(integer)
|
173
185
|
sysread(integer)
|
174
|
-
rescue EOFError
|
186
|
+
rescue ::EOFError
|
175
187
|
nil
|
176
188
|
end
|
177
189
|
|
@@ -216,8 +228,6 @@ class IO
|
|
216
228
|
self
|
217
229
|
end
|
218
230
|
|
219
|
-
alias each_line each
|
220
|
-
|
221
231
|
def each_byte(&block)
|
222
232
|
return enum_for :each_byte unless block_given?
|
223
233
|
|
@@ -275,24 +285,27 @@ class IO
|
|
275
285
|
# @private
|
276
286
|
def check_writable
|
277
287
|
if closed_write?
|
278
|
-
raise IOError, 'not opened for writing'
|
288
|
+
::Kernel.raise ::IOError, 'not opened for writing'
|
279
289
|
end
|
280
290
|
end
|
281
291
|
|
282
292
|
# @private
|
283
293
|
def check_readable
|
284
294
|
if closed_read?
|
285
|
-
raise IOError, 'not opened for reading'
|
295
|
+
::Kernel.raise ::IOError, 'not opened for reading'
|
286
296
|
end
|
287
297
|
end
|
298
|
+
|
299
|
+
alias each_line each
|
300
|
+
alias eof? eof
|
288
301
|
end
|
289
302
|
|
290
|
-
STDIN = $stdin = IO.new(0, 'r')
|
291
|
-
STDOUT = $stdout = IO.new(1, 'w')
|
292
|
-
STDERR = $stderr = IO.new(2, 'w')
|
303
|
+
::STDIN = $stdin = ::IO.new(0, 'r')
|
304
|
+
::STDOUT = $stdout = ::IO.new(1, 'w')
|
305
|
+
::STDERR = $stderr = ::IO.new(2, 'w')
|
293
306
|
|
294
307
|
`var console = Opal.global.console`
|
295
|
-
STDOUT.write_proc = `typeof(process) === 'object' && typeof(process.stdout) === 'object' ? function(s){process.stdout.write(s)} : function(s){console.log(s)}`
|
296
|
-
STDERR.write_proc = `typeof(process) === 'object' && typeof(process.stderr) === 'object' ? function(s){process.stderr.write(s)} : function(s){console.warn(s)}`
|
308
|
+
::STDOUT.write_proc = `typeof(process) === 'object' && typeof(process.stdout) === 'object' ? function(s){process.stdout.write(s)} : function(s){console.log(s)}`
|
309
|
+
::STDERR.write_proc = `typeof(process) === 'object' && typeof(process.stderr) === 'object' ? function(s){process.stderr.write(s)} : function(s){console.warn(s)}`
|
297
310
|
|
298
|
-
STDIN.read_proc = `function(s) { var p = prompt(); if (p !== null) return p + "\n"; return nil; }`
|
311
|
+
::STDIN.read_proc = `function(s) { var p = prompt(); if (p !== null) return p + "\n"; return nil; }`
|
@@ -1,9 +1,9 @@
|
|
1
1
|
# helpers: coerce_to
|
2
2
|
|
3
|
-
module Kernel
|
3
|
+
module ::Kernel
|
4
4
|
def format(format_string, *args)
|
5
5
|
if args.length == 1 && args[0].respond_to?(:to_ary)
|
6
|
-
ary = Opal.coerce_to?(args[0], Array, :to_ary)
|
6
|
+
ary = ::Opal.coerce_to?(args[0], ::Array, :to_ary)
|
7
7
|
args = ary.to_a unless ary.nil?
|
8
8
|
end
|
9
9
|
|
@@ -50,24 +50,24 @@ module Kernel
|
|
50
50
|
FPREC0 = 128;
|
51
51
|
|
52
52
|
function CHECK_FOR_FLAGS() {
|
53
|
-
if (flags&FWIDTH) { #{raise ArgumentError, 'flag after width'} }
|
54
|
-
if (flags&FPREC0) { #{raise ArgumentError, 'flag after precision'} }
|
53
|
+
if (flags&FWIDTH) { #{::Kernel.raise ::ArgumentError, 'flag after width'} }
|
54
|
+
if (flags&FPREC0) { #{::Kernel.raise ::ArgumentError, 'flag after precision'} }
|
55
55
|
}
|
56
56
|
|
57
57
|
function CHECK_FOR_WIDTH() {
|
58
|
-
if (flags&FWIDTH) { #{raise ArgumentError, 'width given twice'} }
|
59
|
-
if (flags&FPREC0) { #{raise ArgumentError, 'width after precision'} }
|
58
|
+
if (flags&FWIDTH) { #{::Kernel.raise ::ArgumentError, 'width given twice'} }
|
59
|
+
if (flags&FPREC0) { #{::Kernel.raise ::ArgumentError, 'width after precision'} }
|
60
60
|
}
|
61
61
|
|
62
62
|
function GET_NTH_ARG(num) {
|
63
|
-
if (num >= args.length) { #{raise ArgumentError, 'too few arguments'} }
|
63
|
+
if (num >= args.length) { #{::Kernel.raise ::ArgumentError, 'too few arguments'} }
|
64
64
|
return args[num];
|
65
65
|
}
|
66
66
|
|
67
67
|
function GET_NEXT_ARG() {
|
68
68
|
switch (pos_arg_num) {
|
69
|
-
case -1: #{raise ArgumentError, "unnumbered(#{`seq_arg_num`}) mixed with numbered"} // raise
|
70
|
-
case -2: #{raise ArgumentError, "unnumbered(#{`seq_arg_num`}) mixed with named"} // raise
|
69
|
+
case -1: #{::Kernel.raise ::ArgumentError, "unnumbered(#{`seq_arg_num`}) mixed with numbered"} // raise
|
70
|
+
case -2: #{::Kernel.raise ::ArgumentError, "unnumbered(#{`seq_arg_num`}) mixed with named"} // raise
|
71
71
|
}
|
72
72
|
pos_arg_num = seq_arg_num++;
|
73
73
|
return GET_NTH_ARG(pos_arg_num - 1);
|
@@ -75,13 +75,13 @@ module Kernel
|
|
75
75
|
|
76
76
|
function GET_POS_ARG(num) {
|
77
77
|
if (pos_arg_num > 0) {
|
78
|
-
#{raise ArgumentError, "numbered(#{`num`}) after unnumbered(#{`pos_arg_num`})"}
|
78
|
+
#{::Kernel.raise ::ArgumentError, "numbered(#{`num`}) after unnumbered(#{`pos_arg_num`})"}
|
79
79
|
}
|
80
80
|
if (pos_arg_num === -2) {
|
81
|
-
#{raise ArgumentError, "numbered(#{`num`}) after named"}
|
81
|
+
#{::Kernel.raise ::ArgumentError, "numbered(#{`num`}) after named"}
|
82
82
|
}
|
83
83
|
if (num < 1) {
|
84
|
-
#{raise ArgumentError, "invalid index - #{`num`}$"}
|
84
|
+
#{::Kernel.raise ::ArgumentError, "invalid index - #{`num`}$"}
|
85
85
|
}
|
86
86
|
pos_arg_num = -1;
|
87
87
|
return GET_NTH_ARG(num - 1);
|
@@ -95,13 +95,13 @@ module Kernel
|
|
95
95
|
var num, str = '';
|
96
96
|
for (;; i++) {
|
97
97
|
if (i === len) {
|
98
|
-
#{raise ArgumentError, 'malformed format string - %*[0-9]'}
|
98
|
+
#{::Kernel.raise ::ArgumentError, 'malformed format string - %*[0-9]'}
|
99
99
|
}
|
100
100
|
if (format_string.charCodeAt(i) < 48 || format_string.charCodeAt(i) > 57) {
|
101
101
|
i--;
|
102
102
|
num = parseInt(str, 10) || 0;
|
103
103
|
if (num > 2147483647) {
|
104
|
-
#{raise ArgumentError, "#{`label`} too big"}
|
104
|
+
#{::Kernel.raise ::ArgumentError, "#{`label`} too big"}
|
105
105
|
}
|
106
106
|
return num;
|
107
107
|
}
|
@@ -188,7 +188,7 @@ module Kernel
|
|
188
188
|
break format_sequence;
|
189
189
|
}
|
190
190
|
if (next_arg !== undefined) {
|
191
|
-
#{raise ArgumentError, "value given twice - %#{`tmp_num`}$"}
|
191
|
+
#{::Kernel.raise ::ArgumentError, "value given twice - %#{`tmp_num`}$"}
|
192
192
|
}
|
193
193
|
next_arg = GET_POS_ARG(tmp_num);
|
194
194
|
i++;
|
@@ -208,20 +208,20 @@ module Kernel
|
|
208
208
|
|
209
209
|
for (;; i++) {
|
210
210
|
if (i === len) {
|
211
|
-
#{raise ArgumentError, 'malformed name - unmatched parenthesis'}
|
211
|
+
#{::Kernel.raise ::ArgumentError, 'malformed name - unmatched parenthesis'}
|
212
212
|
}
|
213
213
|
if (format_string.charAt(i) === closing_brace_char) {
|
214
214
|
|
215
215
|
if (pos_arg_num > 0) {
|
216
|
-
#{raise ArgumentError, "named #{`hash_parameter_key`} after unnumbered(#{`pos_arg_num`})"}
|
216
|
+
#{::Kernel.raise ::ArgumentError, "named #{`hash_parameter_key`} after unnumbered(#{`pos_arg_num`})"}
|
217
217
|
}
|
218
218
|
if (pos_arg_num === -1) {
|
219
|
-
#{raise ArgumentError, "named #{`hash_parameter_key`} after numbered"}
|
219
|
+
#{::Kernel.raise ::ArgumentError, "named #{`hash_parameter_key`} after numbered"}
|
220
220
|
}
|
221
221
|
pos_arg_num = -2;
|
222
222
|
|
223
223
|
if (args[0] === undefined || !args[0].$$is_hash) {
|
224
|
-
#{raise ArgumentError, 'one hash required'}
|
224
|
+
#{::Kernel.raise ::ArgumentError, 'one hash required'}
|
225
225
|
}
|
226
226
|
|
227
227
|
next_arg = #{`args[0]`.fetch(`hash_parameter_key`)};
|
@@ -256,7 +256,7 @@ module Kernel
|
|
256
256
|
|
257
257
|
case '.':
|
258
258
|
if (flags&FPREC0) {
|
259
|
-
#{raise ArgumentError, 'precision given twice'}
|
259
|
+
#{::Kernel.raise ::ArgumentError, 'precision given twice'}
|
260
260
|
}
|
261
261
|
flags |= FPREC|FPREC0;
|
262
262
|
precision = 0;
|
@@ -275,7 +275,7 @@ module Kernel
|
|
275
275
|
case 'd':
|
276
276
|
case 'i':
|
277
277
|
case 'u':
|
278
|
-
arg = #{Integer(`GET_ARG()`)};
|
278
|
+
arg = #{::Kernel.Integer(`GET_ARG()`)};
|
279
279
|
if (arg >= 0) {
|
280
280
|
str = arg.toString();
|
281
281
|
while (str.length < precision) { str = '0' + str; }
|
@@ -336,7 +336,7 @@ module Kernel
|
|
336
336
|
base_neg_zero_digit = 'f';
|
337
337
|
break;
|
338
338
|
}
|
339
|
-
arg = #{Integer(`GET_ARG()`)};
|
339
|
+
arg = #{::Kernel.Integer(`GET_ARG()`)};
|
340
340
|
if (arg >= 0) {
|
341
341
|
str = arg.toString(base_number);
|
342
342
|
while (str.length < precision) { str = '0' + str; }
|
@@ -404,7 +404,7 @@ module Kernel
|
|
404
404
|
case 'E':
|
405
405
|
case 'g':
|
406
406
|
case 'G':
|
407
|
-
arg = #{Float(`GET_ARG()`)};
|
407
|
+
arg = #{::Kernel.Float(`GET_ARG()`)};
|
408
408
|
if (arg >= 0 || isNaN(arg)) {
|
409
409
|
if (arg === Infinity) {
|
410
410
|
str = 'Inf';
|
@@ -483,7 +483,7 @@ module Kernel
|
|
483
483
|
case 'a':
|
484
484
|
case 'A':
|
485
485
|
// Not implemented because there are no specs for this field type.
|
486
|
-
#{raise NotImplementedError, '`A` and `a` format field types are not implemented in Opal yet'}
|
486
|
+
#{::Kernel.raise ::NotImplementedError, '`A` and `a` format field types are not implemented in Opal yet'}
|
487
487
|
// raise
|
488
488
|
|
489
489
|
case 'c':
|
@@ -492,10 +492,10 @@ module Kernel
|
|
492
492
|
if (#{`arg`.respond_to?(:to_str)}) {
|
493
493
|
str = #{`arg`.to_str};
|
494
494
|
} else {
|
495
|
-
str = String.fromCharCode($coerce_to(arg, #{Integer}, 'to_int'));
|
495
|
+
str = String.fromCharCode($coerce_to(arg, #{::Integer}, 'to_int'));
|
496
496
|
}
|
497
497
|
if (str.length !== 1) {
|
498
|
-
#{raise ArgumentError, '%c requires a character'}
|
498
|
+
#{::Kernel.raise ::ArgumentError, '%c requires a character'}
|
499
499
|
}
|
500
500
|
if (flags&FMINUS) {
|
501
501
|
while (str.length < width) { str = str + ' '; }
|
@@ -525,12 +525,12 @@ module Kernel
|
|
525
525
|
break format_sequence;
|
526
526
|
|
527
527
|
default:
|
528
|
-
#{raise ArgumentError, "malformed format string - %#{`format_string.charAt(i)`}"}
|
528
|
+
#{::Kernel.raise ::ArgumentError, "malformed format string - %#{`format_string.charAt(i)`}"}
|
529
529
|
}
|
530
530
|
}
|
531
531
|
|
532
532
|
if (str === undefined) {
|
533
|
-
#{raise ArgumentError, 'malformed format string - %'}
|
533
|
+
#{::Kernel.raise ::ArgumentError, 'malformed format string - %'}
|
534
534
|
}
|
535
535
|
|
536
536
|
result += format_string.slice(begin_slice, end_slice) + str;
|
@@ -538,7 +538,7 @@ module Kernel
|
|
538
538
|
}
|
539
539
|
|
540
540
|
if (#{$DEBUG} && pos_arg_num >= 0 && seq_arg_num < args.length) {
|
541
|
-
#{raise ArgumentError, 'too many arguments for format string'}
|
541
|
+
#{::Kernel.raise ::ArgumentError, 'too many arguments for format string'}
|
542
542
|
}
|
543
543
|
|
544
544
|
return result + format_string.slice(begin_slice);
|