ruby-internal 0.8.2 → 0.8.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,27 +0,0 @@
1
- require 'internal/vm'
2
-
3
- if defined?(RubyVM::InstructionSequence) then
4
- require 'internal/vm/bytedecoder'
5
-
6
- class RubyVM
7
- class InstructionSequence
8
- def as_code(indent=0)
9
- env = Internal::ByteDecoder::Environment.new(local_table())
10
- opt_pc = self.opt_pc
11
- self.bytedecode(env, opt_pc)
12
- expressions = env.expressions + env.stack
13
- if expressions.length == 0 then
14
- return nil
15
- elsif expressions.length == 1 and
16
- expressions[0].is_a?(Internal::ByteDecoder::Expression::Literal) and
17
- expressions[0].value == nil then
18
- return nil
19
- else
20
- expressions.map! { |e| "#{' '*indent}#{e}" }
21
- return expressions.join("\n")
22
- end
23
- end
24
- end
25
- end
26
- end
27
-
@@ -1,26 +0,0 @@
1
- require 'internal/vm'
2
-
3
- if defined?(RubyVM::InstructionSequence) then
4
- require 'internal/vm/bytedecoder'
5
-
6
- class RubyVM
7
- class InstructionSequence
8
- def as_expression
9
- env = Internal::ByteDecoder::Environment.new(local_table())
10
- opt_pc = self.opt_pc
11
- self.bytedecode(env, opt_pc)
12
- expressions = env.expressions + env.stack
13
- if expressions.length == 0 then
14
- return nil
15
- elsif expressions.length == 1 and
16
- expressions[0].is_a?(Internal::ByteDecoder::Expression::Literal) and
17
- expressions[0].value == nil then
18
- return nil
19
- else
20
- return expressions.join('; ')
21
- end
22
- end
23
- end
24
- end
25
- end
26
-
@@ -1,160 +0,0 @@
1
- require 'rbconfig'
2
-
3
- class TEST_CLASS
4
- FOO = 10
5
- end
6
-
7
- EXPRESSION_SAMPLES = {
8
- :lit => '42',
9
- :vcall => 'foo',
10
- :fcall => 'foo()',
11
- :fcall_many => 'foo(1, 2, 3)',
12
- :fcall_many2 => 'foo 1, 2, 3',
13
- :fcall_splat => 'a = [2, 3]; foo(1, *a)',
14
- :fcall_block => 'foo() { nil }',
15
- :fcall_block_with_args => 'foo() { |x,y| [x, y] }',
16
- :plus => '1+1',
17
- :minus => '54-2',
18
- :times => '12*6',
19
- :div => '16/7',
20
- :less => '8 < 5',
21
- :less_eq => '8 <= 5',
22
- :greater => '8 > 5',
23
- :greater_eq => '8 >= 5',
24
- :equal => '8 == 5',
25
- :threequal => '8 === 5',
26
- :spaceship => "8 <=\> 5", # the \> is for Align.vim
27
- :lshift => '8 << 5 ',
28
- :rshift => '8 >> 5',
29
- :bit_and => '8 & 5',
30
- :bit_or => '8 | 5',
31
- :bit_xor => '8 ^ 5',
32
- :mod => '8 % 5',
33
- :not_equal => '8 != 5',
34
- # TODO: :match => ''foo' =~ /foo|bar/',
35
- # TODO: :not_match => ''foo' !~ /foo|bar/',
36
- :call => 'self.foo',
37
- # TODO: super
38
- # TODO: zsuper
39
- # TODO: redo
40
- # TODO: retry
41
- :sym_not => '!true',
42
- :sym_and => 'true && false',
43
- :sym_or => 'true || false',
44
- :lit_not => 'not true',
45
- :lit_and => 'true and false',
46
- :lit_or => 'true or false',
47
- :array => '[1, 2, 3]',
48
- :zarray => '[]',
49
- :array_splat => '[1, 2, *[3, 4]]',
50
- :array_splat_var => 'a = [3, 4]; [1, 2, *a]',
51
- # TODO: block
52
- :hash => '{ 1 => 2, 2 => 3 }',
53
- :zhash => '{}',
54
- :ternary => 'true ? false : nil',
55
- :if => 'if true then false; else nil; end',
56
- :if2 => '42 if true',
57
- :true => 'true',
58
- :false => 'false',
59
- :nil => 'nil',
60
- :self => 'self',
61
- :dot2 => '5..8',
62
- :dot3 => '5...8',
63
- :gvar => '$stdout',
64
- :ivar => '@foo',
65
- :cvar => '@@foo',
66
- :dvar => 'foo = 1; foo',
67
- :nth_ref => '$1',
68
- :back_ref => '$`',
69
- :dasgn_curr => 'foo = 1',
70
- # TODO: dasgn
71
- :iasgn => '@foo = 6',
72
- # TODO: lasgn
73
- :masgn => 'a, b = 1, 2',
74
- :masgn_with_one_value => 'a, b = 1',
75
- :cdecl => 'remove_foo; FOO = 1',
76
- :cvdecl => '@@foo = 1',
77
- # TODO: cvasgn
78
- :attrasgn => 'h = {}; h.default = true; h',
79
- :const => 'FOO',
80
- :colon2 => 'self::FOO',
81
- :colon3 => '::TEST_CLASS::FOO',
82
- # TODO: lvar
83
- # TODO: newline
84
- :str => '"foo"',
85
- :regx => '/foo/',
86
- # TODO: :regx_cflag => '/foo/i',
87
- :regx_once => '/foo/o',
88
- # :regx_once_cflag => '/foo/oi',
89
- # TODO: xstr
90
- :dstr => 'a = 1; b = 2; "#{a}#{b}"',
91
- :dregx => 'a = 1; b = 2; /#{a}#{b}/',
92
- # :dregx_cflag => 'a = 1; b = 2; /#{a}#{b}/i',
93
- :dregx_once => 'a = 1; b = 2; /#{a}#{b}/',
94
- # :dregx_once_cflag => 'a = 1; b = 2; /#{a}#{b}/oi',
95
- # TODO dregx with next set
96
- # TODO: dxstr
97
- # TODO: evstr
98
- :iter => 'loop { break }',
99
- :iter_x => 'loop { break 10 }',
100
- :while_1 => 'while true; break; end',
101
- :while_1_x => 'while true; break 7; end',
102
- :while_1_do => 'while true do; break; end',
103
- :while_1_do_x => 'while true do; break 7; end',
104
- :while_0 => 'begin; break; end while true',
105
- :while_0_x => 'begin; break 7; end while true',
106
- :until_1 => 'until false; break; end',
107
- :until_1_x => 'until false; break 7; end',
108
- :until_1_do => 'until false do; break; end',
109
- :until_1_do_x => 'until false do; break 7; end',
110
- :until_0 => 'begin; break; end until false',
111
- :until_0_x => 'begin; break 7; end until false',
112
- :break => 'break',
113
- :break_x => 'break 49',
114
- :catch => 'catch(:foo) { throw :foo; 42 }',
115
- # TODO: yield
116
- :begin => 'begin; 42; end',
117
- :begin_empty => 'begin; end',
118
- :begin_ensure => 'begin; 7; ensure; 42; end',
119
- :begin_ensure_empty_head => 'begin; ensure; 42; end',
120
- :begin_ensure_empty_body => 'begin; ensure; end',
121
- :rescue => 'raise "foo" rescue 7',
122
- :rescue2 => 'raise "foo"; 42 rescue 7',
123
- :begin_rescue => 'begin; raise "foo"; rescue; 42; end',
124
- :begin_rescue_multi => 'begin; raise "foo"; 6; rescue; 42; end',
125
- :begin_rescue_empty_head => 'begin; rescue; end',
126
- :begin_rescue_empty_resq => 'begin; 42; rescue; end',
127
- :resbody => 'begin; raise "foo"; rescue; 42; end',
128
- :case_when => 'case 42; when TrueClass then 6; when Fixnum then 42; end',
129
- :case_when_multi_arg => 'case 42; when TrueClass, Fixnum then 42; end',
130
- :case_when_empty_body => 'case 42; when TrueClass; when Fixnum then 42; end',
131
- # TODO: alias
132
- # TODO: valias
133
- :undef => 'define_method(:a) { }; undef :a',
134
- # TODO: class
135
- # TODO: sclass
136
- # TODO: scope
137
- # TODO: defn
138
- # TODO: defs
139
- :defined => 'defined?(NilClass)',
140
- # TODO: match3
141
- :op_asgn2 => 'OWFA.foo = 0; OWFA.foo += 1',
142
- }
143
-
144
-
145
- major = Config::CONFIG['MAJOR'].to_i
146
- minor = Config::CONFIG['MINOR'].to_i
147
- teeny = Config::CONFIG['TEENY'].to_i
148
- ruby_version_code = major * 100 + minor * 10 + teeny
149
-
150
- if ruby_version_code < 170 then
151
- EXPRESSION_SAMPLES.delete(:break_x)
152
- EXPRESSION_SAMPLES.delete(:iter_x)
153
- EXPRESSION_SAMPLES.delete(:while_1_x)
154
- EXPRESSION_SAMPLES.delete(:while_1_do_x)
155
- EXPRESSION_SAMPLES.delete(:while_0_x)
156
- EXPRESSION_SAMPLES.delete(:until_1_x)
157
- EXPRESSION_SAMPLES.delete(:until_1_do_x)
158
- EXPRESSION_SAMPLES.delete(:until_0_x)
159
- end
160
-
data/test/test_as_code.rb DELETED
@@ -1,261 +0,0 @@
1
- require 'test/unit'
2
- require 'timeout'
3
-
4
- dir = File.dirname(__FILE__)
5
- $:.unshift(dir) if not $:.include?(dir)
6
- $:.unshift("#{dir}/../lib") if not $:.include?("#{dir}/../lib")
7
- $:.unshift("#{dir}/../ext") if not $:.include?("#{dir}/../ext")
8
-
9
- require 'internal/node/as_code'
10
- require 'internal/method/as_code'
11
- require 'internal/module/as_code'
12
- require 'internal/proc/as_code'
13
-
14
- require 'expression_samples'
15
-
16
- $stdout.sync = true
17
- $stderr.sync = true
18
-
19
- class TC_As_Code < Test::Unit::TestCase
20
- MAJOR = Config::CONFIG['MAJOR'].to_i
21
- MINOR = Config::CONFIG['MINOR'].to_i
22
- TEENY = Config::CONFIG['TEENY'].to_i
23
- RUBY_VERSION_CODE = MAJOR * 100 + MINOR * 10 + TEENY
24
-
25
- extend Test::Unit::Assertions
26
-
27
- # Some of the samples use this
28
- def foo(*a, &b)
29
- return b.call(1, 2) if b
30
- return a
31
- end
32
-
33
- EXPRESSION_SAMPLES.each do |name, code|
34
- p = proc {
35
- p_orig = eval("proc { #{code} }")
36
- code_new = p_orig.body.as_code
37
- # p code, code_new
38
- p_new = eval("proc { #{code_new} }")
39
- result_orig = result_new = nil
40
- exc_orig = exc_new = nil
41
- timeout(1) { begin; result_orig = p_orig.call; rescue; exc_orig = $!; end }
42
- timeout(1) { begin; result_new = p_new.call; rescue; exc_new = $!; end }
43
- assert_equal(
44
- exc_orig.class,
45
- exc_new.class,
46
- "Expected #{exc_orig.inspect} but got #{exc_new.inspect} " + \
47
- "(code_orig=#{code}, code_new=#{code_new})")
48
- if exc_orig and exc_new then
49
- assert_equal(exc_orig.message, exc_new.message)
50
- end
51
- assert_equal(result_orig, result_new)
52
- }
53
- define_method "test_#{name}", p
54
- end
55
-
56
- def initialize(test_method_name)
57
- # TODO: This seems to be the only way to get tests defined with #
58
- # define_method to run on 1.8.1 and earlier.
59
- catch(:invalid_test) { super(test_method_name) }
60
- end
61
-
62
- def method_no_args
63
- 42
64
- end
65
-
66
- def test_method_no_args_as_code
67
- m = method(:method_no_args)
68
- assert_equal "def method_no_args()\n 42\nend", m.as_code
69
- end
70
-
71
- def method_one_arg(a)
72
- 42
73
- end
74
-
75
- def test_method_one_arg_as_code
76
- m = method(:method_one_arg)
77
- assert_equal "def method_one_arg(a)\n 42\nend", m.as_code
78
- end
79
-
80
- def method_two_args(a, b)
81
- 42
82
- end
83
-
84
- def test_method_two_args_as_code
85
- m = method(:method_two_args)
86
- assert_equal "def method_two_args(a, b)\n 42\nend", m.as_code
87
- end
88
-
89
- def method_rest_arg(*rest)
90
- 42
91
- end
92
-
93
- def test_method_rest_arg_as_code
94
- m = method(:method_rest_arg)
95
- assert_equal "def method_rest_arg(*rest)\n 42\nend", m.as_code
96
- end
97
-
98
- def method_block_arg(&block)
99
- 42
100
- end
101
-
102
- def test_method_block_arg_as_code
103
- m = method(:method_block_arg)
104
- assert_equal "def method_block_arg(&block)\n 42\nend", m.as_code
105
- end
106
-
107
- def method_rest_and_block_arg(*rest, &block)
108
- 42
109
- end
110
-
111
- def test_method_rest_and_block_arg_as_code
112
- m = method(:method_rest_and_block_arg)
113
- assert_equal "def method_rest_and_block_arg(*rest, &block)\n 42\nend", m.as_code
114
- end
115
-
116
- def method_two_args_and_rest_and_block_arg(a, b, *rest, &block)
117
- 42
118
- end
119
-
120
- def test_method_two_args_and_rest_and_block_arg_as_code
121
- m = method(:method_two_args_and_rest_and_block_arg)
122
- assert_equal "def method_two_args_and_rest_and_block_arg(a, b, *rest, &block)\n 42\nend", m.as_code
123
- end
124
-
125
- def method_with_body(a, b)
126
- a + b
127
- end
128
-
129
- def test_method_with_body_as_code
130
- m = method(:method_with_body)
131
- assert_equal "def method_with_body(a, b)\n a + b\nend", m.as_code
132
- end
133
-
134
- def method_begin_ensure_as_code(a, b, *rest, &block)
135
- begin
136
- if not a and not b then
137
- raise "Need more input!"
138
- end
139
- return a + b
140
- ensure
141
- puts "In ensure block"
142
- end
143
- end
144
-
145
- def test_method_begin_ensure_as_code
146
- m = method(:method_begin_ensure_as_code)
147
- if RUBY_VERSION_CODE >= 190 then
148
- ret = ''
149
- else
150
- ret = 'return '
151
- end
152
-
153
- assert_equal <<-END.chomp, m.as_code(3)
154
- def method_begin_ensure_as_code(a, b, *rest, &block)
155
- begin
156
- (raise("Need more input!")) if (not a and not b)
157
- #{ret}a + b
158
- ensure
159
- puts("In ensure block")
160
- end
161
- end
162
- END
163
- end
164
-
165
- def test_proc_no_args_as_code
166
- p = proc { }
167
- assert_equal "proc do\nend", p.as_code
168
- end
169
-
170
- def test_proc_empty_args_as_code
171
- if not defined?(RubyVM) then
172
- # indistinguishable from proc { } on YARV
173
- p = proc { || }
174
- assert_equal "proc do ||\nend", p.as_code
175
- end
176
- end
177
-
178
- def test_proc_one_arg_as_code
179
- p = proc { |a| }
180
- assert_equal "proc do |a|\nend", p.as_code
181
- end
182
-
183
- def test_proc_one_array_arg_as_expression
184
- p = proc { |a,| }
185
- assert_equal "proc do |a,|\nend", p.as_code
186
- end
187
-
188
- def test_proc_two_args_as_code
189
- p = proc { |a, b| }
190
- assert_equal "proc do |a, b|\nend", p.as_code
191
- end
192
-
193
- def test_proc_rest_arg_as_code
194
- p = proc { |*rest| }
195
- assert_equal "proc do |*rest|\nend", p.as_code
196
- end
197
-
198
- def test_proc_two_args_and_rest_arg_as_code
199
- p = proc { |a, b, *rest| }
200
- assert_equal "proc do |a, b, *rest|\nend", p.as_code
201
- end
202
-
203
- def test_proc_with_body_as_code
204
- p = proc { |a, b| a + b }
205
- assert_equal "proc do |a, b|\n a + b\nend", p.as_code
206
- end
207
-
208
- def setup
209
- @foo = 42
210
- end
211
-
212
- # 1.7 and later
213
- def foo(*args)
214
- return args
215
- end
216
-
217
- def remove_foo
218
- self.class.remove_foo
219
- end
220
-
221
- # 1.6
222
- def self.foo(*args)
223
- return args
224
- end
225
-
226
- def self.remove_foo
227
- self.class_eval { remove_const(:FOO) if const_defined?(:FOO) }
228
- end
229
-
230
- @@foo = 10
231
-
232
- FOO = 57
233
-
234
- class ObjectWithFooAccessor
235
- attr_accessor :foo
236
- end
237
-
238
- OWFA = ObjectWithFooAccessor.new
239
- end
240
-
241
- if __FILE__ == $0 then
242
- require 'test/unit/ui/console/testrunner'
243
-
244
- if Test::Unit.const_defined?(:AutoRunner) then
245
- exit Test::Unit::AutoRunner.run
246
- else
247
- if ARGV.empty? then
248
- suite = TC_As_Code.suite
249
- else
250
- suite = Test::Unit::TestSuite.new('TC_As_Code')
251
- TC_As_Code.suite.tests.each do |test|
252
- ARGV.each do |arg|
253
- suite << test if /#{arg}/ =~ test.name
254
- end
255
- end
256
- end
257
- result = Test::Unit::UI::Console::TestRunner.run(suite)
258
- exit(result.error_count + result.failure_count)
259
- end
260
- end
261
-