fastruby 0.0.17 → 0.0.18
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +10 -0
- data/Rakefile +4 -2
- data/benchmarks/benchmark.rb +20 -44
- data/benchmarks/benchmark.rb~ +47 -0
- data/benchmarks/benchmark2.rb +19 -45
- data/benchmarks/benchmark2.rb~ +46 -0
- data/benchmarks/benchmark3.rb +19 -45
- data/benchmarks/benchmark3.rb~ +46 -0
- data/benchmarks/benchmark4.rb +30 -65
- data/benchmarks/benchmark4.rb~ +61 -0
- data/benchmarks/benchmark5.rb +25 -55
- data/benchmarks/benchmark5.rb~ +54 -0
- data/benchmarks/benchmark6.rb +19 -40
- data/benchmarks/benchmark6.rb~ +41 -0
- data/benchmarks/benchmark7.rb +23 -52
- data/benchmarks/benchmark7.rb~ +48 -0
- data/ext/fastruby_base/fastruby_base.inl +1 -0
- data/lib/fastruby/builder.rb +128 -76
- data/lib/fastruby/cache/cache.rb +9 -5
- data/lib/fastruby/fastruby_sexp.rb +18 -0
- data/lib/fastruby/inliner/inliner.rb +68 -0
- data/lib/fastruby/inliner/modules/call.rb +150 -0
- data/lib/fastruby/inliner/modules/recursive.rb +35 -0
- data/lib/fastruby/object.rb +17 -32
- data/lib/fastruby/reductor/modules/case.rb +49 -0
- data/lib/{fastruby.rb~ → fastruby/reductor/modules/for.rb} +11 -13
- data/lib/fastruby/reductor/modules/nontree.rb +31 -0
- data/lib/fastruby/reductor/modules/recursive.rb +31 -0
- data/lib/fastruby/reductor/reductor.rb +39 -0
- data/lib/fastruby/set_tree.rb +7 -1
- data/lib/fastruby/sexp_extension.rb +6 -0
- data/lib/fastruby/translator/modules/block.rb +4 -4
- data/lib/fastruby/translator/modules/call.rb +9 -26
- data/lib/fastruby/translator/modules/defn.rb +5 -3
- data/lib/fastruby/translator/modules/directive.rb +42 -0
- data/lib/fastruby/translator/modules/exceptions.rb +12 -30
- data/lib/fastruby/translator/modules/flow.rb +33 -83
- data/lib/fastruby/translator/modules/iter.rb +4 -7
- data/lib/fastruby/translator/modules/literal.rb +11 -25
- data/lib/fastruby/translator/modules/logical.rb +5 -3
- data/lib/fastruby/translator/modules/method_group.rb +4 -3
- data/lib/fastruby/translator/modules/nonlocal.rb +7 -5
- data/lib/fastruby/translator/modules/static.rb +242 -0
- data/lib/fastruby/translator/modules/variable.rb +16 -9
- data/lib/fastruby/translator/scope_mode_helper.rb +2 -27
- data/lib/fastruby/translator/translator.rb +131 -60
- data/lib/fastruby/translator/translator_modules.rb +6 -2
- data/lib/fastruby.rb +1 -1
- data/spec/reductor/base_spec.rb +46 -0
- data/spec/ruby/base_spec.rb~ +394 -0
- data/spec/ruby/block/arguments_spec.rb~ +214 -0
- data/spec/ruby/block/proc_as_block_spec.rb~ +23 -0
- data/spec/ruby/block/retry_spec.rb~ +43 -0
- data/spec/ruby/block_spec.rb~ +520 -0
- data/spec/ruby/defn/replacement_spec.rb~ +102 -0
- data/spec/ruby/integrity_spec.rb~ +40 -0
- data/spec/ruby/singleton_spec.rb~ +76 -0
- data/spec/scope_mode/base_spec.rb +14 -5
- data/spec/scope_mode/block_spec.rb +18 -9
- data/spec/scope_mode/call_spec.rb +11 -2
- data/spec/scope_mode/exception_spec.rb +11 -2
- data/spec/scope_mode/flow_spec.rb +18 -8
- data/spec/scope_mode/optimization_spec.rb +21 -13
- data/spec/static/base_spec.rb +54 -0
- data/spec/static/flow_spec.rb +48 -0
- data/spec/static/operator_spec.rb +104 -0
- metadata +58 -8
@@ -0,0 +1,520 @@
|
|
1
|
+
require "fastruby"
|
2
|
+
|
3
|
+
describe FastRuby, "fastruby" do
|
4
|
+
class ::X6
|
5
|
+
fastruby "
|
6
|
+
def foo(ary)
|
7
|
+
ary.each do |a|
|
8
|
+
end
|
9
|
+
0
|
10
|
+
end
|
11
|
+
"
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should compile blocks" do
|
15
|
+
::X6.new.foo([1,2,3]).should be == 0
|
16
|
+
end
|
17
|
+
|
18
|
+
class ::X7
|
19
|
+
fastruby "
|
20
|
+
def foo(ary)
|
21
|
+
ary.map do |a|
|
22
|
+
0
|
23
|
+
end
|
24
|
+
end
|
25
|
+
"
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should compile blocks with code inside" do
|
29
|
+
::X7.new.foo([1,2,3]).should be == [0,0,0]
|
30
|
+
end
|
31
|
+
|
32
|
+
class ::X8
|
33
|
+
fastruby "
|
34
|
+
def foo(ary)
|
35
|
+
ary.map do |a|
|
36
|
+
a
|
37
|
+
end
|
38
|
+
end
|
39
|
+
"
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should compile blocks with code inside refering block arguments" do
|
43
|
+
::X8.new.foo([1,2,3]).should be == [1,2,3]
|
44
|
+
end
|
45
|
+
|
46
|
+
class ::X9
|
47
|
+
fastruby "
|
48
|
+
def foo(hash)
|
49
|
+
hash.map do |k,v|
|
50
|
+
k+v
|
51
|
+
end
|
52
|
+
end
|
53
|
+
"
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should compile blocks with code inside refering multiple block arguments" do
|
57
|
+
::X9.new.foo({1 => 2, 3 => 4}).sort.should be == [3,7]
|
58
|
+
end
|
59
|
+
|
60
|
+
class ::X9_11
|
61
|
+
fastruby "
|
62
|
+
def foo
|
63
|
+
proc do |k,v|
|
64
|
+
k+v
|
65
|
+
end
|
66
|
+
end
|
67
|
+
"
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should compile blocks with code inside refering multiple block arguments (proc)" do
|
71
|
+
pr = ::X9_11.new.foo
|
72
|
+
{1 => 2, 3 => 4}.map(&pr).sort.should be == [3,7]
|
73
|
+
end
|
74
|
+
|
75
|
+
|
76
|
+
class ::Y10
|
77
|
+
def bar(arg1)
|
78
|
+
yield
|
79
|
+
arg1
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
class ::X10
|
84
|
+
fastruby "
|
85
|
+
def foo(obj, arg1)
|
86
|
+
obj.bar(arg1) do |a|
|
87
|
+
end
|
88
|
+
end
|
89
|
+
"
|
90
|
+
end
|
91
|
+
|
92
|
+
it "should compile iter calls with arguments" do
|
93
|
+
::X10.new.foo(::Y10.new, 10).should be == 10
|
94
|
+
end
|
95
|
+
|
96
|
+
class ::Y11
|
97
|
+
def bar(arg1, arg2)
|
98
|
+
yield
|
99
|
+
arg1+arg2
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
class ::X11
|
104
|
+
fastruby "
|
105
|
+
def foo(obj, arg1, arg2)
|
106
|
+
obj.bar(arg1, arg2) do |a|
|
107
|
+
end
|
108
|
+
end
|
109
|
+
"
|
110
|
+
end
|
111
|
+
|
112
|
+
it "should compile iter calls with multiple arguments" do
|
113
|
+
::X11.new.foo(::Y11.new, 10, 9).should be == 19
|
114
|
+
end
|
115
|
+
|
116
|
+
class ::X12
|
117
|
+
fastruby "
|
118
|
+
def foo(ary)
|
119
|
+
a = 1
|
120
|
+
ary.map do |x|
|
121
|
+
a
|
122
|
+
end
|
123
|
+
end
|
124
|
+
"
|
125
|
+
end
|
126
|
+
|
127
|
+
it "should allow accessing local variables from block" do
|
128
|
+
::X12.new.foo([1,2,3,4]).should be == [1,1,1,1]
|
129
|
+
end
|
130
|
+
|
131
|
+
class ::X13
|
132
|
+
fastruby "
|
133
|
+
def foo(ary)
|
134
|
+
a = 1
|
135
|
+
ary.map do |x|
|
136
|
+
a+x
|
137
|
+
end
|
138
|
+
end
|
139
|
+
"
|
140
|
+
end
|
141
|
+
|
142
|
+
it "should allow accessing local variables and block parameters from block" do
|
143
|
+
::X13.new.foo([1,2,3,4]).should be == [2,3,4,5]
|
144
|
+
end
|
145
|
+
|
146
|
+
class ::Y14
|
147
|
+
fastruby "
|
148
|
+
def bar
|
149
|
+
block_given?
|
150
|
+
end
|
151
|
+
"
|
152
|
+
end
|
153
|
+
|
154
|
+
class ::X14
|
155
|
+
fastruby "
|
156
|
+
def foo(y)
|
157
|
+
y.bar
|
158
|
+
end
|
159
|
+
"
|
160
|
+
end
|
161
|
+
|
162
|
+
it "method calls should not repass blocks" do
|
163
|
+
::X14.new.foo(::Y14.new){ }.should be == false
|
164
|
+
end
|
165
|
+
|
166
|
+
class ::X15
|
167
|
+
fastruby "
|
168
|
+
def foo
|
169
|
+
bar
|
170
|
+
end
|
171
|
+
"
|
172
|
+
|
173
|
+
private
|
174
|
+
def bar
|
175
|
+
true
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
it "should allow calls to private methods" do
|
180
|
+
::X15.new.foo.should be == true
|
181
|
+
end
|
182
|
+
|
183
|
+
class ::X16
|
184
|
+
fastruby "
|
185
|
+
def foo
|
186
|
+
bar do
|
187
|
+
12
|
188
|
+
end
|
189
|
+
end
|
190
|
+
"
|
191
|
+
|
192
|
+
def bar
|
193
|
+
yield
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
it "should allow calls with block to self methods" do
|
198
|
+
::X16.new.foo.should be == 12
|
199
|
+
end
|
200
|
+
|
201
|
+
class ::X17
|
202
|
+
fastruby "
|
203
|
+
def foo(z)
|
204
|
+
i = 9
|
205
|
+
z.each do |x|
|
206
|
+
i = x
|
207
|
+
0
|
208
|
+
end
|
209
|
+
i
|
210
|
+
end
|
211
|
+
"
|
212
|
+
end
|
213
|
+
|
214
|
+
it "should allow assignment of locals from blocks" do
|
215
|
+
::X17.new.foo([1,2,3]).should be == 3
|
216
|
+
end
|
217
|
+
|
218
|
+
class ::X18
|
219
|
+
fastruby "
|
220
|
+
def foo
|
221
|
+
yield
|
222
|
+
end
|
223
|
+
"
|
224
|
+
end
|
225
|
+
|
226
|
+
it "should allow block calls" do
|
227
|
+
::X18.new.foo{ 9 }.should be == 9
|
228
|
+
end
|
229
|
+
|
230
|
+
class ::Y19
|
231
|
+
fastruby "
|
232
|
+
def bar
|
233
|
+
yield
|
234
|
+
end
|
235
|
+
"
|
236
|
+
end
|
237
|
+
|
238
|
+
class ::X19
|
239
|
+
fastruby "
|
240
|
+
def foo(y)
|
241
|
+
y.bar {
|
242
|
+
9
|
243
|
+
}
|
244
|
+
end
|
245
|
+
"
|
246
|
+
end
|
247
|
+
|
248
|
+
it "should execute block class between fastruby methods when no block is passed" do
|
249
|
+
::X19.new.foo(::Y19.new).should be == 9
|
250
|
+
end
|
251
|
+
|
252
|
+
it "should execute block class between fastruby methods when block is passed" do
|
253
|
+
::X19.new.foo(::Y19.new){}.should be == 9
|
254
|
+
end
|
255
|
+
|
256
|
+
class ::X20
|
257
|
+
fastruby "
|
258
|
+
def foo
|
259
|
+
yield(1)
|
260
|
+
end
|
261
|
+
"
|
262
|
+
end
|
263
|
+
|
264
|
+
it "should execute block from fastruby methods with one argument" do
|
265
|
+
::X20.new.foo do |n1|
|
266
|
+
n1.should be == 1
|
267
|
+
end
|
268
|
+
end
|
269
|
+
|
270
|
+
class ::X21
|
271
|
+
fastruby "
|
272
|
+
def foo
|
273
|
+
yield(1,2)
|
274
|
+
end
|
275
|
+
"
|
276
|
+
end
|
277
|
+
|
278
|
+
it "should execute block from fastruby methods with two arguments" do
|
279
|
+
::X21.new.foo do |n1,n2|
|
280
|
+
n1.should be == 1
|
281
|
+
n2.should be == 2
|
282
|
+
end
|
283
|
+
end
|
284
|
+
|
285
|
+
class ::Y22
|
286
|
+
fastruby "
|
287
|
+
def foo
|
288
|
+
yield
|
289
|
+
end
|
290
|
+
"
|
291
|
+
|
292
|
+
fastruby "
|
293
|
+
def bar(x)
|
294
|
+
i = 10
|
295
|
+
lvar_type(i,Fixnum)
|
296
|
+
x.foo do
|
297
|
+
i = i - 1
|
298
|
+
end
|
299
|
+
i
|
300
|
+
end
|
301
|
+
"
|
302
|
+
end
|
303
|
+
|
304
|
+
it "should execute block calls after lvar_type directive" do
|
305
|
+
y22 = ::Y22.new
|
306
|
+
y22.bar(y22).should be == 9
|
307
|
+
end
|
308
|
+
|
309
|
+
class ::Y23
|
310
|
+
def foo
|
311
|
+
yield
|
312
|
+
end
|
313
|
+
|
314
|
+
def foo2
|
315
|
+
77
|
316
|
+
end
|
317
|
+
|
318
|
+
fastruby "
|
319
|
+
def bar(x)
|
320
|
+
i = 0
|
321
|
+
x.foo do
|
322
|
+
i = foo2
|
323
|
+
end
|
324
|
+
i
|
325
|
+
end
|
326
|
+
"
|
327
|
+
end
|
328
|
+
|
329
|
+
it "should call self methods from inside a block" do
|
330
|
+
y23 = ::Y23.new
|
331
|
+
y23.bar(y23).should be == 77
|
332
|
+
end
|
333
|
+
|
334
|
+
class ::Y24
|
335
|
+
def foo
|
336
|
+
yield
|
337
|
+
end
|
338
|
+
|
339
|
+
fastruby "
|
340
|
+
def bar(x)
|
341
|
+
i = 0
|
342
|
+
x.foo do
|
343
|
+
i = block_given?
|
344
|
+
end
|
345
|
+
i
|
346
|
+
end
|
347
|
+
"
|
348
|
+
end
|
349
|
+
|
350
|
+
it "should call block_given? from inside a block when a block is not passed should return false" do
|
351
|
+
y24 = ::Y24.new
|
352
|
+
y24.bar(y24).should be == false
|
353
|
+
end
|
354
|
+
|
355
|
+
it "should call block_given? from inside a block when a block is not passed should return true" do
|
356
|
+
y24 = ::Y24.new
|
357
|
+
y24.bar(y24){}.should be == true
|
358
|
+
end
|
359
|
+
|
360
|
+
class ::Y25
|
361
|
+
def foo
|
362
|
+
yield
|
363
|
+
end
|
364
|
+
|
365
|
+
fastruby "
|
366
|
+
def bar(x)
|
367
|
+
i = 0
|
368
|
+
x.foo do
|
369
|
+
i = block_given? do
|
370
|
+
end
|
371
|
+
end
|
372
|
+
i
|
373
|
+
end
|
374
|
+
"
|
375
|
+
end
|
376
|
+
|
377
|
+
it "should call block_given? with block from inside a block when a block is not passed should return false " do
|
378
|
+
y25 = ::Y25.new
|
379
|
+
y25.bar(y25).should be == false
|
380
|
+
end
|
381
|
+
|
382
|
+
it "should call block_given? with block from inside a block when a block is not passed should return true" do
|
383
|
+
y25 = ::Y25.new
|
384
|
+
y25.bar(y25){}.should be == true
|
385
|
+
end
|
386
|
+
|
387
|
+
class ::Y26
|
388
|
+
def bar
|
389
|
+
yield
|
390
|
+
end
|
391
|
+
|
392
|
+
fastruby "
|
393
|
+
def foo
|
394
|
+
bar do
|
395
|
+
yield
|
396
|
+
end
|
397
|
+
end
|
398
|
+
"
|
399
|
+
end
|
400
|
+
|
401
|
+
it "should call yield from inside a block" do
|
402
|
+
y26 = ::Y26.new
|
403
|
+
|
404
|
+
block_num_calls = 0
|
405
|
+
|
406
|
+
y26.foo do
|
407
|
+
block_num_calls = block_num_calls + 1
|
408
|
+
end
|
409
|
+
|
410
|
+
block_num_calls.should be == 1
|
411
|
+
end
|
412
|
+
|
413
|
+
class ::Y27
|
414
|
+
fastruby "
|
415
|
+
def foo(x)
|
416
|
+
x
|
417
|
+
end
|
418
|
+
"
|
419
|
+
end
|
420
|
+
|
421
|
+
class ::Y28
|
422
|
+
fastruby "
|
423
|
+
def foo(y27, a)
|
424
|
+
y27.foo(a) do
|
425
|
+
end
|
426
|
+
end
|
427
|
+
"
|
428
|
+
end
|
429
|
+
|
430
|
+
it "should pass arguments when call with block" do
|
431
|
+
y28 = ::Y28.new
|
432
|
+
y28.foo(::Y27.new, 713).should be == 713
|
433
|
+
end
|
434
|
+
|
435
|
+
class ::Y29
|
436
|
+
fastruby "
|
437
|
+
def foo(ary)
|
438
|
+
cc = nil
|
439
|
+
ary.each do |x|
|
440
|
+
cc = x
|
441
|
+
end
|
442
|
+
cc
|
443
|
+
end
|
444
|
+
"
|
445
|
+
end
|
446
|
+
|
447
|
+
it "should assign variables from inside a block" do
|
448
|
+
::Y29.new.foo([1,2,3]).should be == 3
|
449
|
+
end
|
450
|
+
|
451
|
+
|
452
|
+
class ::Y30
|
453
|
+
attr_accessor :a, :b, :c
|
454
|
+
|
455
|
+
def bar
|
456
|
+
begin
|
457
|
+
doo do
|
458
|
+
yield
|
459
|
+
end
|
460
|
+
ensure
|
461
|
+
@a = 15
|
462
|
+
end
|
463
|
+
end
|
464
|
+
|
465
|
+
fastruby "
|
466
|
+
|
467
|
+
def doo
|
468
|
+
begin
|
469
|
+
yield
|
470
|
+
ensure
|
471
|
+
@b = 16
|
472
|
+
end
|
473
|
+
end
|
474
|
+
|
475
|
+
def foo
|
476
|
+
cc = nil
|
477
|
+
bar do
|
478
|
+
return
|
479
|
+
end
|
480
|
+
ensure
|
481
|
+
@c = 17
|
482
|
+
end
|
483
|
+
"
|
484
|
+
end
|
485
|
+
|
486
|
+
it "should assign variables from inside a block" do
|
487
|
+
y = ::Y30.new
|
488
|
+
y.foo
|
489
|
+
|
490
|
+
y.a.should be == 15
|
491
|
+
y.b.should be == 16
|
492
|
+
y.c.should be == 17
|
493
|
+
end
|
494
|
+
|
495
|
+
class ::Y31
|
496
|
+
fastruby "
|
497
|
+
|
498
|
+
def bar
|
499
|
+
begin
|
500
|
+
yield
|
501
|
+
rescue
|
502
|
+
end
|
503
|
+
end
|
504
|
+
|
505
|
+
def foo
|
506
|
+
bar do
|
507
|
+
return 8
|
508
|
+
end
|
509
|
+
return 0
|
510
|
+
end
|
511
|
+
"
|
512
|
+
end
|
513
|
+
|
514
|
+
it "should return values from block through rescue" do
|
515
|
+
y = ::Y31.new
|
516
|
+
y.foo.should be == 8
|
517
|
+
end
|
518
|
+
|
519
|
+
|
520
|
+
end
|
@@ -0,0 +1,102 @@
|
|
1
|
+
require "fastruby"
|
2
|
+
|
3
|
+
describe FastRuby, "fastruby" do
|
4
|
+
it "should allow replace methods after they are called and compiled at runtime" do
|
5
|
+
fastruby "
|
6
|
+
class JU1
|
7
|
+
def foo
|
8
|
+
6
|
9
|
+
end
|
10
|
+
end
|
11
|
+
"
|
12
|
+
|
13
|
+
::JU1.new.foo
|
14
|
+
|
15
|
+
fastruby "
|
16
|
+
class JU1
|
17
|
+
def foo
|
18
|
+
9
|
19
|
+
end
|
20
|
+
end
|
21
|
+
"
|
22
|
+
|
23
|
+
::JU1.new.foo.should be == 9
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should allow replace methods after they are called and compiled at runtime (through other method)" do
|
27
|
+
fastruby "
|
28
|
+
class JU2
|
29
|
+
def foo
|
30
|
+
6
|
31
|
+
end
|
32
|
+
|
33
|
+
def bar
|
34
|
+
foo
|
35
|
+
end
|
36
|
+
end
|
37
|
+
"
|
38
|
+
|
39
|
+
::JU2.new.bar
|
40
|
+
|
41
|
+
fastruby "
|
42
|
+
class JU2
|
43
|
+
def foo
|
44
|
+
9
|
45
|
+
end
|
46
|
+
end
|
47
|
+
"
|
48
|
+
|
49
|
+
::JU2.new.bar.should be == 9
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should allow replace methods using ruby after they are called and compiled at runtime (through other method)" do
|
53
|
+
fastruby "
|
54
|
+
class ::JU3
|
55
|
+
def foo
|
56
|
+
6
|
57
|
+
end
|
58
|
+
|
59
|
+
def bar
|
60
|
+
foo
|
61
|
+
end
|
62
|
+
end
|
63
|
+
"
|
64
|
+
|
65
|
+
::JU3.new.bar
|
66
|
+
|
67
|
+
class ::JU3
|
68
|
+
def foo
|
69
|
+
9
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
::JU3.new.bar.should be == 9
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should allow replace methods using indentical code string" do
|
77
|
+
code = "
|
78
|
+
class ::JU4
|
79
|
+
def foo
|
80
|
+
6
|
81
|
+
end
|
82
|
+
end
|
83
|
+
"
|
84
|
+
|
85
|
+
fastruby code
|
86
|
+
|
87
|
+
::JU4.new.foo.should be == 6
|
88
|
+
|
89
|
+
fastruby "class ::JU4
|
90
|
+
def foo
|
91
|
+
9
|
92
|
+
end
|
93
|
+
end
|
94
|
+
"
|
95
|
+
|
96
|
+
::JU4.new.foo.should be == 9
|
97
|
+
|
98
|
+
fastruby code
|
99
|
+
|
100
|
+
::JU4.new.foo.should be == 6
|
101
|
+
end
|
102
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require "fastruby"
|
2
|
+
|
3
|
+
describe FastRuby, "fastruby" do
|
4
|
+
|
5
|
+
|
6
|
+
class ::Y2
|
7
|
+
fastruby "
|
8
|
+
def foo(x)
|
9
|
+
i = 0
|
10
|
+
lvar_type(i,Fixnum)
|
11
|
+
i = x
|
12
|
+
0
|
13
|
+
end
|
14
|
+
", :validate_lvar_types => true
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should deny wrong type assignments at runtime when validate_lvar_types is true" do
|
18
|
+
lambda {
|
19
|
+
::Y2.new.foo("test string")
|
20
|
+
}.should raise_error(FastRuby::TypeMismatchAssignmentException)
|
21
|
+
end
|
22
|
+
|
23
|
+
class ::Y3
|
24
|
+
fastruby "
|
25
|
+
def foo(x)
|
26
|
+
i = 0
|
27
|
+
lvar_type(i,Fixnum)
|
28
|
+
i = x
|
29
|
+
0
|
30
|
+
end
|
31
|
+
", :validate_lvar_types => false
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should NOT deny wrong type assignments at runtime when validate_lvar_types is false" do
|
35
|
+
lambda {
|
36
|
+
::Y3.new.foo("test string")
|
37
|
+
}.should_not raise_error
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require "fastruby"
|
2
|
+
|
3
|
+
describe FastRuby, "fastruby" do
|
4
|
+
|
5
|
+
it "should allow defining class methods" do
|
6
|
+
fastruby '
|
7
|
+
class X
|
8
|
+
def self.foo
|
9
|
+
9
|
10
|
+
end
|
11
|
+
end
|
12
|
+
'
|
13
|
+
X.foo.should be == 9
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should allow defining class methods with blocks" do
|
17
|
+
fastruby '
|
18
|
+
class R2
|
19
|
+
def self.bar
|
20
|
+
yield
|
21
|
+
end
|
22
|
+
end
|
23
|
+
'
|
24
|
+
R2.bar{67}.should be == 67
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should call singleton methods from outside" do
|
28
|
+
class R3
|
29
|
+
def bar
|
30
|
+
12
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
fastruby '
|
35
|
+
class R4
|
36
|
+
def foo(x)
|
37
|
+
def x.bar
|
38
|
+
24
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
'
|
43
|
+
|
44
|
+
r3 = R3.new
|
45
|
+
R4.new.foo(r3)
|
46
|
+
r3.bar.should be == 24
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should call singleton methods from inside" do
|
50
|
+
fastruby '
|
51
|
+
class R5
|
52
|
+
def bar
|
53
|
+
12
|
54
|
+
end
|
55
|
+
end
|
56
|
+
class R6
|
57
|
+
def foo(x)
|
58
|
+
def x.bar
|
59
|
+
240
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
class R7
|
64
|
+
def foo(x)
|
65
|
+
x.bar+1
|
66
|
+
end
|
67
|
+
end
|
68
|
+
'
|
69
|
+
|
70
|
+
r5 = R5.new
|
71
|
+
R7.new.foo(r5).should be == 13
|
72
|
+
R6.new.foo(r5)
|
73
|
+
R7.new.foo(r5).should be == 241
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|