fastruby 0.0.18 → 0.0.19

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.
Files changed (35) hide show
  1. data/CHANGELOG +10 -0
  2. data/Rakefile +2 -1
  3. data/benchmarks/benchmark.rb~ +2 -4
  4. data/benchmarks/benchmark3.rb~ +2 -4
  5. data/ext/fastruby_base/fastruby_base.inl +1 -1
  6. data/lib/fastruby/builder.rb +8 -7
  7. data/lib/fastruby/fastruby_sexp.rb +18 -0
  8. data/lib/fastruby/inliner/modules/call.rb +254 -67
  9. data/lib/fastruby/inliner/modules/recursive.rb +5 -0
  10. data/lib/fastruby/reductor/modules/case.rb +2 -1
  11. data/lib/fastruby/translator/modules/block.rb +65 -59
  12. data/lib/fastruby/translator/modules/call.rb +7 -4
  13. data/lib/fastruby/translator/modules/exceptions.rb +66 -64
  14. data/lib/fastruby/translator/modules/flow.rb +8 -8
  15. data/lib/fastruby/translator/modules/literal.rb +5 -4
  16. data/lib/fastruby/translator/modules/method_group.rb +6 -5
  17. data/lib/fastruby/translator/modules/nonlocal.rb +153 -11
  18. data/lib/fastruby/translator/modules/static.rb +8 -8
  19. data/lib/fastruby/translator/modules/variable.rb +15 -15
  20. data/lib/fastruby/translator/translator.rb +156 -59
  21. data/lib/fastruby.rb +1 -1
  22. data/lib/fastruby.rb~ +36 -0
  23. data/spec/fastruby/inliner/modules/call_spec.rb +0 -0
  24. data/spec/fastruby/translator/modules/nonlocal_spec.rb +0 -0
  25. data/spec/fastruby/translator/translator_spec.rb +0 -0
  26. data/spec/ruby/base_spec.rb~ +5 -5
  27. data/spec/ruby/block/break_spec.rb~ +236 -0
  28. data/spec/ruby/block/lambda_spec.rb~ +38 -0
  29. data/spec/ruby/block/next_spec.rb~ +85 -0
  30. data/spec/ruby/call/base_call_spec.rb~ +83 -0
  31. data/spec/ruby/defn/replacement_spec.rb +52 -2
  32. data/spec/ruby/defn/replacement_spec.rb~ +52 -2
  33. data/spec/ruby/exception/base_spec.rb +22 -1
  34. data/spec/ruby/return_spec.rb~ +99 -0
  35. metadata +30 -10
@@ -0,0 +1,236 @@
1
+ require "fastruby"
2
+
3
+ describe FastRuby, "fastruby" do
4
+ class ::V1
5
+ fastruby "
6
+ def foo(ary)
7
+ sum = 0
8
+ ary.each do |a|
9
+ sum = sum + a
10
+ end
11
+
12
+ sum
13
+ end
14
+ "
15
+ end
16
+
17
+ it "should execute basic test iterating an array" do
18
+ ::V1.new.foo([1,2,3]).should be == 6
19
+ end
20
+
21
+ class ::V2
22
+ fastruby "
23
+ def foo(ary)
24
+ sum = 0
25
+ ary.each do |a|
26
+ sum = sum + a
27
+ break
28
+ end
29
+
30
+ sum
31
+ end
32
+ "
33
+ end
34
+
35
+ it "should execute basic test iterating an array with a break" do
36
+ ::V2.new.foo([1,2,3]).should be == 1
37
+ end
38
+
39
+ class ::V3
40
+ fastruby "
41
+ def foo(ary)
42
+ sum = 0
43
+ ary.each do |a|
44
+ sum = sum + a
45
+ break if a == 2
46
+ end
47
+
48
+ sum
49
+ end
50
+ "
51
+ end
52
+
53
+ it "should execute basic test iterating an array with a conditional break" do
54
+ ::V3.new.foo([1,2,3]).should be == 3
55
+ end
56
+
57
+ class ::V4
58
+ fastruby "
59
+ def foo
60
+ break
61
+ end
62
+ "
63
+ end
64
+
65
+ it "should raise LocalJumpError with illegal break" do
66
+ lambda {
67
+ ::V4.new.foo
68
+ }.should raise_error(LocalJumpError)
69
+ end
70
+
71
+ class ::V5
72
+ fastruby "
73
+
74
+ def each
75
+ yield(1)
76
+ yield(2)
77
+ yield(3)
78
+ end
79
+
80
+ def foo
81
+ sum = 0
82
+ each do |a|
83
+ sum = sum + a
84
+ break if a == 2
85
+ end
86
+
87
+ sum
88
+ end
89
+ "
90
+ end
91
+
92
+ it "should execute basic test iterating an array with a conditional break (method with block on fastruby)" do
93
+ ::V5.new.foo.should be == 3
94
+ end
95
+
96
+ class ::V6
97
+ fastruby "
98
+ def foo(ary)
99
+ ary.each do |a|
100
+ break 85 if a == 2
101
+ end
102
+ end
103
+ "
104
+ end
105
+
106
+ it "should allow return value on parent method using break" do
107
+ ::V6.new.foo([1,2,3]).should be == 85
108
+ end
109
+
110
+ class ::V7
111
+
112
+ attr_reader :a
113
+
114
+ fastruby "
115
+
116
+ def each
117
+ yield(1)
118
+ yield(2)
119
+ yield(3)
120
+ ensure
121
+ @a = 87
122
+ end
123
+
124
+ def foo
125
+ each do |a|
126
+ break if a == 2
127
+ end
128
+ end
129
+ "
130
+ end
131
+
132
+ it "should execute ensure on parent frame when using break" do
133
+ x = ::V7.new
134
+
135
+ x.foo
136
+ x.a.should be == 87
137
+ end
138
+
139
+
140
+ class ::V8
141
+
142
+ attr_reader :a
143
+
144
+ def each
145
+ yield(1)
146
+ yield(2)
147
+ yield(3)
148
+ ensure
149
+ @a = 87
150
+ end
151
+
152
+ fastruby "
153
+
154
+ def foo
155
+ each do |x|
156
+ break if x == 2
157
+ end
158
+ end
159
+ "
160
+ end
161
+
162
+ it "should execute ensure on parent frame when using break (and called method is ruby)" do
163
+ x = ::V8.new
164
+
165
+ x.foo
166
+ x.a.should be == 87
167
+ end
168
+
169
+ class ::V9
170
+ fastruby "
171
+ def each
172
+ yield(1)
173
+ yield(2)
174
+ yield(3)
175
+ end
176
+
177
+ def foo
178
+ each do |a|
179
+ break 85
180
+ end
181
+ end
182
+ "
183
+ end
184
+
185
+ it "should allow return value on parent method using break when called method is fastruby" do
186
+ ::V9.new.foo.should be == 85
187
+ end
188
+
189
+
190
+
191
+ class ::V10
192
+
193
+ attr_accessor :a, :b, :c, :c_e, :b_e
194
+
195
+ fastruby "
196
+
197
+ def moo
198
+ yield(1)
199
+ @c = 32 # this should't be executed
200
+ ensure
201
+ @c_e = 4
202
+ end
203
+
204
+ def bar
205
+ yield(2)
206
+ @b = 16 # this should't be executed
207
+ ensure
208
+ @b_e = 4
209
+ end
210
+
211
+ def foo
212
+ ret = moo do |x|
213
+ bar do |y|
214
+ break 9
215
+ end
216
+ @a = 111
217
+ break 10
218
+ end
219
+ ret + 1
220
+ end
221
+
222
+ "
223
+ end
224
+
225
+ it "should break for nested blocks" do
226
+ v10 = ::V10.new
227
+ v10.foo.should be == 11
228
+ v10.a.should be == 111
229
+ v10.b.should be == nil
230
+ v10.c.should be == nil
231
+ v10.b_e.should be == 4
232
+ v10.c_e.should be == 4
233
+ end
234
+
235
+
236
+ end
@@ -0,0 +1,38 @@
1
+ require "fastruby"
2
+
3
+ describe FastRuby, "fastruby" do
4
+
5
+ fastruby "
6
+ class ::LL4
7
+ def foo
8
+ lambda {|x|
9
+ yield(x)
10
+ }
11
+ end
12
+
13
+ def bar
14
+ z = 99
15
+ foo do |x|
16
+ x+z
17
+ end
18
+ end
19
+
20
+ def xt
21
+ lambda_object = bar()
22
+ lambda_object.call(1)
23
+ end
24
+ end
25
+ "
26
+
27
+ it "lambda must be able to access local variables of parent scopes through yield (from fastruby)" do
28
+ ll4 = ::LL4.new
29
+ ll4.xt.should be == 100
30
+ end
31
+
32
+ it "lambda must be able to access local variables of parent scopes through yield" do
33
+ ll4 = ::LL4.new
34
+ lambda_object = ll4.bar
35
+ lambda_object.call(1).should be == 100
36
+ end
37
+
38
+ end
@@ -0,0 +1,85 @@
1
+ require "fastruby"
2
+
3
+ describe FastRuby, "fastruby" do
4
+ class ::W1
5
+ fastruby "
6
+ def foo(ary)
7
+ sum = 0
8
+ ary.each do |a|
9
+ next if a == 2
10
+ sum = sum + a
11
+ end
12
+
13
+ sum
14
+ end
15
+ "
16
+ end
17
+
18
+ it "should execute basic test iterating an array" do
19
+ ::W1.new.foo([1,2,3]).should be == 4
20
+ end
21
+
22
+ class ::W2
23
+ fastruby "
24
+ def foo
25
+ next
26
+ end
27
+ "
28
+ end
29
+
30
+ it "should raise LocalJumpError with illegal next" do
31
+ lambda {
32
+ ::W2.new.foo
33
+ }.should raise_error(LocalJumpError)
34
+ end
35
+
36
+ class ::W3
37
+ attr_reader :x, :y
38
+
39
+ fastruby "
40
+ def bar
41
+ @x = yield(1)
42
+ @y = yield(2)
43
+ end
44
+
45
+ def foo
46
+ bar do |a|
47
+ next 16 if a == 1
48
+ next 32 if a == 2
49
+ end
50
+ end
51
+ "
52
+ end
53
+
54
+ it "should return values on block using next" do
55
+ x = ::W3.new
56
+ x.foo
57
+ x.x.should be == 16
58
+ x.y.should be == 32
59
+ end
60
+
61
+ class ::W4
62
+ attr_reader :x, :y
63
+
64
+ fastruby "
65
+ def foo(ary)
66
+ sum = 0
67
+ ary.each do |a|
68
+ begin
69
+ next if a == 2
70
+ ensure
71
+ sum = sum + a
72
+ end
73
+ end
74
+
75
+ sum
76
+ end
77
+ "
78
+ end
79
+
80
+ it "should execute ensure when using next" do
81
+ x = ::W4.new
82
+ x.foo([1,2,3]).should be == 6
83
+ end
84
+
85
+ end
@@ -0,0 +1,83 @@
1
+ require "fastruby"
2
+
3
+ describe FastRuby, "fastruby" do
4
+ def self.test_arguments(x)
5
+ it "should allow #{x} arguments calling to cfunc" do
6
+
7
+ arguments = (0..x).map(&:to_s).join(",")
8
+
9
+ fastruby "
10
+ class ::CYR#{x}
11
+ def foo
12
+ a = []
13
+ a.infer(Array).push(#{arguments})
14
+ a
15
+ end
16
+ end
17
+ "
18
+
19
+ eval("::CYR#{x}").new.foo.should be == eval("[#{arguments}]")
20
+ end
21
+ end
22
+
23
+ test_arguments(10)
24
+ test_arguments(15)
25
+ test_arguments(20)
26
+
27
+ (23..27).each do |i|
28
+ test_arguments(i)
29
+ end
30
+
31
+ test_arguments(20)
32
+
33
+
34
+ def self.test_fastruby_arguments(argnum)
35
+ it "should allow #{argnum} arguments calling fastruby" do
36
+
37
+ arguments_name = (0..argnum-1).map{|x| "a"+x.to_s}.join(",")
38
+ arguments = (0..argnum-1).map(&:to_s).join(",")
39
+
40
+ fastruby "
41
+ class ::CYR1_#{argnum}
42
+ def foo(#{arguments_name})
43
+ [#{arguments_name}]
44
+ end
45
+ end
46
+ "
47
+
48
+ array = eval("[#{arguments}]")
49
+
50
+ eval("::CYR1_#{argnum}").new.foo(*array).should be == array
51
+ end
52
+ end
53
+
54
+ (8..12).each do |i|
55
+ test_fastruby_arguments(i)
56
+ end
57
+
58
+ def self.test_invalid_fastruby_arguments(argnum)
59
+ it "should allow #{argnum} arguments calling fastruby" do
60
+
61
+ arguments_name = (0..argnum-1).map{|x| "a"+x.to_s}.join(",")
62
+ arguments = (0..argnum-1).map(&:to_s).join(",")
63
+
64
+ fastruby "
65
+ class ::CYR1_#{argnum}
66
+ def foo(#{arguments_name})
67
+ [#{arguments_name}]
68
+ end
69
+ end
70
+ "
71
+
72
+ array = eval("[#{arguments}]")
73
+
74
+ lambda {
75
+ eval("::CYR1_#{argnum}").new.foo(*array).should be == array
76
+ }.should raise_error(ArgumentError)
77
+ end
78
+ end
79
+
80
+ #test_invalid_fastruby_arguments(15)
81
+
82
+ test_fastruby_arguments(1)
83
+ end
@@ -98,5 +98,55 @@ describe FastRuby, "fastruby" do
98
98
  fastruby code
99
99
 
100
100
  ::JU4.new.foo.should be == 6
101
- end
102
- end
101
+ end
102
+
103
+
104
+ it "should allow replaced called method when inlined from many functions" do
105
+ fastruby "
106
+ class ::JU5
107
+ def bar
108
+ 77
109
+ end
110
+ end
111
+ class ::JU6
112
+ def foo(a,b)
113
+ b.bar
114
+ end
115
+ end
116
+ "
117
+
118
+ ::JU6.new.foo(0,::JU5.new)
119
+ ::JU6.new.foo("0",::JU5.new)
120
+
121
+ fastruby "
122
+ class ::JU5
123
+ def bar
124
+ 99
125
+ end
126
+ end
127
+ "
128
+
129
+ ::JU6.new.foo(0,::JU5.new).should be == 99
130
+ ::JU6.new.foo("0",::JU5.new).should be == 99
131
+ end
132
+
133
+ it "should allow replace CFUNC methods using ruby after they are called and compiled at runtime (through other method)" do
134
+ fastruby "
135
+ class ::JU6
136
+ def foo(a)
137
+ a.conj
138
+ end
139
+ end
140
+ "
141
+
142
+ ::JU6.new.foo(0)
143
+
144
+ class Fixnum
145
+ def conj
146
+ 86
147
+ end
148
+ end
149
+
150
+ ::JU6.new.foo(0).should be == 86
151
+ end
152
+ end
@@ -98,5 +98,55 @@ describe FastRuby, "fastruby" do
98
98
  fastruby code
99
99
 
100
100
  ::JU4.new.foo.should be == 6
101
- end
102
- end
101
+ end
102
+
103
+
104
+ it "should allow replaced called method when inlined from many functions" do
105
+ fastruby "
106
+ class ::JU5
107
+ def bar
108
+ 77
109
+ end
110
+ end
111
+ class ::JU6
112
+ def foo(a,b)
113
+ b.bar
114
+ end
115
+ end
116
+ "
117
+
118
+ ::JU6.new.foo(0,::JU5.new)
119
+ ::JU6.new.foo("0",::JU5.new)
120
+
121
+ fastruby "
122
+ class ::JU5
123
+ def bar
124
+ 99
125
+ end
126
+ end
127
+ "
128
+
129
+ ::JU6.new.foo(0,::JU5.new).should be == 99
130
+ ::JU6.new.foo("0",::JU5.new).should be == 99
131
+ end
132
+
133
+ it "should allow replace CFUNC methods using ruby after they are called and compiled at runtime (through other method)" do
134
+ eval "
135
+ class ::JU6
136
+ def foo(a)
137
+ a.conj
138
+ end
139
+ end
140
+ "
141
+
142
+ ::JU6.new.foo(0)
143
+
144
+ class Fixnum
145
+ def conj
146
+ 86
147
+ end
148
+ end
149
+
150
+ ::JU6.new.foo(0).should be == 86
151
+ end
152
+ end
@@ -61,5 +61,26 @@ ENDSTR
61
61
  }.should raise_exception(RuntimeError)
62
62
 
63
63
  end
64
-
64
+
65
+ it "should allow return from rescue body" do
66
+
67
+ fastruby <<ENDSTR
68
+
69
+ class EXC3
70
+ def foo
71
+ begin
72
+ return 99
73
+ rescue
74
+ end
75
+ end
76
+
77
+ def bar
78
+ foo
79
+ end
80
+ end
81
+
82
+ ENDSTR
83
+
84
+ ::EXC3.new.bar.should be == 99
85
+ end
65
86
  end
@@ -0,0 +1,99 @@
1
+ require "fastruby"
2
+
3
+ describe FastRuby, "fastruby" do
4
+ it "should allow basic return" do
5
+ fastruby "
6
+ class ::P1
7
+ def foo
8
+ return 1
9
+ end
10
+ end
11
+ "
12
+ ::P1.new.foo.should be == 1
13
+ end
14
+
15
+ it "should allow return from inside a block" do
16
+ fastruby "
17
+ class ::P2
18
+ def bar
19
+ yield
20
+ end
21
+
22
+ def foo
23
+ bar do
24
+ return 8
25
+ end
26
+
27
+ return 0
28
+ end
29
+ end
30
+ "
31
+ ::P2.new.foo.should be == 8
32
+ end
33
+
34
+ it "should allow basic return on singleton method" do
35
+ fastruby "
36
+ class ::P3
37
+ end
38
+
39
+ class ::P31
40
+ def bar(x)
41
+ def x.foo
42
+ return 1
43
+ end
44
+ end
45
+ end
46
+ "
47
+
48
+ p3 = ::P3.new
49
+ ::P31.new.bar(p3)
50
+ p3.foo.should be == 1
51
+ end
52
+
53
+ it "should allow return from inside a block on a singleton method" do
54
+ fastruby "
55
+ class ::P4
56
+ def bar
57
+ yield
58
+ end
59
+ end
60
+
61
+ class ::P41
62
+ def bar(x)
63
+ def x.foo
64
+ bar do
65
+ return 8
66
+ end
67
+ return 0
68
+ end
69
+ end
70
+ end
71
+ "
72
+ p4 = ::P4.new
73
+ ::P41.new.bar(p4)
74
+ p4.foo.should be == 8
75
+ end
76
+
77
+ it "should execute ensure when ensure impl has a return" do
78
+ fastruby "
79
+ class ::P5
80
+ def a
81
+ @a
82
+ end
83
+
84
+ def foo
85
+ begin
86
+ return 16
87
+ ensure
88
+ @a = 9
89
+ end
90
+
91
+ return 32
92
+ end
93
+ end
94
+ "
95
+ p5 = ::P5.new
96
+ p5.foo.should be == 16
97
+ p5.a.should be ==9
98
+ end
99
+ end