rufus-lua 1.1.2 → 1.1.3

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.
@@ -1,60 +0,0 @@
1
-
2
- #
3
- # Specifying rufus-lua
4
- #
5
- # Sat Jun 21 11:04:02 JST 2014
6
- #
7
-
8
- require 'spec_base'
9
-
10
- # adapted from https://github.com/jmettraux/rufus-lua/issues/14
11
-
12
-
13
- describe 'lua strings' do
14
-
15
- context 'and \0 bytes' do
16
-
17
- before :each do
18
- @s = Rufus::Lua::State.new
19
- end
20
- after :each do
21
- @s.close
22
- end
23
-
24
- it 'are not truncated when returned to Ruby' do
25
-
26
- s = @s.eval('return string.char(1, 0, 0, 2, 0, 0)')
27
-
28
- expect(s.bytes.to_a).to eq([ 1, 0, 0, 2, 0, 0 ])
29
- end
30
-
31
- it 'are not truncated when passed from Ruby to Lua and back' do
32
-
33
- s = [ 65, 66, 0, 67, 0, 0, 68, 0 ].pack('c*')
34
-
35
- f = @s.eval(%{
36
- f = function(s)
37
- return { s = s, l = string.len(s) }
38
- end
39
- return f
40
- })
41
-
42
- expect(f.call(s).to_h).to eq({ 's' => s, 'l' => 8.0 })
43
- end
44
-
45
- it 'accept symbols containing the \0 character' do
46
-
47
- s = [ 65, 66, 0, 67, 0, 0, 68, 0 ].pack('c*').to_sym
48
-
49
- f = @s.eval(%{
50
- f = function(s)
51
- return { s = s, l = string.len(s) }
52
- end
53
- return f
54
- })
55
-
56
- expect(f.call(s).to_h).to eq({ 's' => s.to_s, 'l' => 8.0 })
57
- end
58
- end
59
- end
60
-
@@ -1,165 +0,0 @@
1
-
2
- #
3
- # Specifying rufus-lua
4
- #
5
- # Fri Mar 13 23:42:29 JST 2009
6
- #
7
-
8
- require 'spec_base'
9
-
10
-
11
- describe Rufus::Lua::State do
12
-
13
- context 'tables' do
14
-
15
- before do
16
- @s = Rufus::Lua::State.new
17
- end
18
- after do
19
- @s.close
20
- end
21
-
22
- it 'finds a hash' do
23
-
24
- @s.eval('h = { a = "b", c = 2, 4 }')
25
-
26
- expect(@s['h'].to_h).to eq({ 'a' => 'b', 'c' => 2.0, 1.0 => 4.0 })
27
- end
28
-
29
- it 'turns a hash into an array' do
30
-
31
- @s.eval('a = { "a", "b", "c" }')
32
-
33
- expect(@s['a'].to_h).to eq({ 1.0 => 'a', 2.0 => 'b', 3.0 => 'c' })
34
- expect(@s['a'].to_a).to eq %w{ a b c }
35
- end
36
-
37
- it 'does nested lookups (2)' do
38
-
39
- @s.eval('a = { b = { c = 0 } }')
40
-
41
- expect(@s['a.b.c']).to eq 0
42
- end
43
-
44
- it 'returns Lua tables' do
45
-
46
- expect(@s.eval('return {}').class).to eq Rufus::Lua::Table
47
- end
48
-
49
- it 'turns Lua tables into Ruby hashes' do
50
-
51
- expect(@s.eval('return {}').to_h).to eq({})
52
- end
53
-
54
- it 'can free Lua tables' do
55
-
56
- t = @s.eval('t = {}; return t')
57
- t.free
58
-
59
- expect(t.ref).to eq nil
60
- expect(lambda { t.to_h }).to raise_error(RuntimeError)
61
- end
62
-
63
- it 'indexes tables' do
64
-
65
- t = @s.eval("return { a = 'A' }")
66
-
67
- expect(t['a']).to eq 'A'
68
- expect(t['b']).to eq nil
69
- end
70
-
71
- it 'iterates over Lua tables' do
72
-
73
- #t = @s.eval("return { a = 'A', b = 'B', c = 3, d = 3.1 }")
74
- t = @s.eval("return { a = 'A', b = 'B', c = 3 }")
75
-
76
- expect(t.values.sort_by { |v| v.to_s }).to eq [ 3.0, 'A', 'B' ]
77
- expect(t.keys.sort).to eq [ 'a', 'b', 'c' ]
78
- end
79
-
80
- it 'provides keys and values for tables' do
81
-
82
- t = @s.eval("return { a = 'A', b = 'B', c = 3 }")
83
-
84
- expect(t.collect { |k, v| v }.size).to eq 3
85
- end
86
-
87
- it 'provides the size of a table' do
88
-
89
- expect(@s.eval("return { a = 'A', b = 'B', c = 3 }").objlen).to eq 0.0
90
- expect(@s.eval("return { 1, 2 }").objlen).to eq 2
91
-
92
- expect(@s.eval("return { a = 'A', b = 'B', c = 3 }").size).to eq 3
93
- expect(@s.eval("return { a = 'A', b = 'B', c = 3 }").length).to eq 3
94
- expect(@s.eval("return { 1, 2 }").size).to eq 2
95
- expect(@s.eval("return { 1, 2 }").length).to eq 2
96
- end
97
-
98
- it 'lets setting values in Lua tables from Ruby' do
99
-
100
- t = @s.eval("return { a = 'A', b = 'B', c = 3 }")
101
- t['b'] = 4
102
-
103
- expect(t['b']).to eq 4.0
104
- end
105
-
106
- it 'indexes tables properly' do
107
-
108
- @s.eval("t = { 'a', 'b', 'c' }")
109
-
110
- expect(@s.eval("return t[0]")).to eq nil
111
- expect(@s.eval("return t[1]")).to eq 'a'
112
- expect(@s.eval("return t[3]")).to eq 'c'
113
- expect(@s.eval("return t[4]")).to eq nil
114
- end
115
-
116
- it 'replies to to_a(false) (pure = false)' do
117
-
118
- expect(@s.eval(
119
- "return { a = 'A', b = 'B', c = 3 }"
120
- ).to_a(false).sort).to eq(
121
- [ [ "a", "A" ], [ "b", "B" ], [ "c", 3.0 ] ]
122
- )
123
- expect(@s.eval(
124
- "return { 1, 2 }"
125
- ).to_a(false)).to eq(
126
- [ 1.0, 2.0 ]
127
- )
128
- expect(@s.eval(
129
- "return {}"
130
- ).to_a(false)).to eq(
131
- []
132
- )
133
- expect(@s.eval(
134
- "return { 1, 2, car = 'benz' }"
135
- ).to_a(false)).to eq(
136
- [ 1.0, 2.0, ["car", "benz"] ]
137
- )
138
- end
139
-
140
- it 'tries hard to honour #to_ruby' do
141
-
142
- expect(@s.eval(
143
- "return { a = 'A', b = 'B', c = 3 }"
144
- ).to_ruby).to eq(
145
- { "a" => "A", "b" => "B", "c" => 3.0 }
146
- )
147
- expect(@s.eval(
148
- "return { 1, 2 }"
149
- ).to_ruby).to eq(
150
- [ 1.0, 2.0 ]
151
- )
152
- expect(@s.eval(
153
- "return {}"
154
- ).to_ruby).to eq(
155
- []
156
- )
157
- expect(@s.eval(
158
- "return { 1, 2, car = 'benz' }"
159
- ).to_ruby).to eq(
160
- { 1.0 => 1.0, "car" => "benz", 2.0 => 2.0 }
161
- )
162
- end
163
- end
164
- end
165
-
@@ -1,45 +0,0 @@
1
-
2
- #
3
- # Specifying rufus-lua
4
- #
5
- # Sat Mar 14 11:39:15 JST 2009
6
- #
7
-
8
- require 'spec_base'
9
-
10
-
11
- describe Rufus::Lua do
12
-
13
- context '(utils)' do
14
-
15
- it 'turns Ruby arrays into Lua string representations' do
16
-
17
- expect(Rufus::Lua.to_lua_s(
18
- %w{ a b c }
19
- )).to eq(
20
- '{ "a", "b", "c" }'
21
- )
22
-
23
- # TODO : ["a"] is probably better...
24
- end
25
-
26
- it 'turns Ruby hashes into Lua string representations' do
27
-
28
- expect(Rufus::Lua.to_lua_s(
29
- { 'a' => 'A', 'b' => 2}
30
- )).to eq(
31
- '{ ["a"] = "A", ["b"] = 2 }'
32
- )
33
- end
34
-
35
- it 'turns Ruby NilClass into Lua nil representation' do
36
-
37
- expect(Rufus::Lua.to_lua_s(
38
- {'a' => nil}
39
- )).to eq(
40
- '{ ["a"] = nil }'
41
- )
42
- end
43
- end
44
- end
45
-
@@ -1,41 +0,0 @@
1
-
2
- #
3
- # benchmarking rufus-lua
4
- #
5
- # Thu Mar 12 15:40:26 JST 2009
6
- #
7
-
8
- $:.unshift('lib')
9
-
10
- require 'benchmark'
11
-
12
- require 'rubygems'
13
- require 'rufus/lua'
14
-
15
-
16
- Benchmark.benchmark(' ' * 31 + Benchmark::Tms::CAPTION, 31) do |b|
17
-
18
- b.report('ruby') do
19
- 1_000_000.times { |i| i }
20
- end
21
-
22
- s = Rufus::Lua::State.new
23
- b.report('lua') do
24
- s.eval('for i = 1, 1000000 do end')
25
- end
26
- s.close
27
- end
28
-
29
- # jmettraux@sanma ~/rufus/rufus-lua (master) $ ruby test/bm0.rb
30
- # user system total real
31
- # ruby 0.220000 0.000000 0.220000 ( 0.217909)
32
- # lua 0.010000 0.000000 0.010000 ( 0.013667)
33
- # jmettraux@sanma ~/rufus/rufus-lua (master) $ ruby19 test/bm0.rb
34
- # user system total real
35
- # ruby 0.120000 0.010000 0.130000 ( 0.123396)
36
- # lua 0.010000 0.000000 0.010000 ( 0.013869)
37
- # jmettraux@sanma ~/rufus/rufus-lua (master) $ ruby19 test/bm0.rb
38
- # user system total real
39
- # ruby 0.110000 0.000000 0.110000 ( 0.125229)
40
- # lua 0.020000 0.000000 0.020000 ( 0.012828)
41
-
@@ -1,78 +0,0 @@
1
-
2
- #
3
- # benchmarking rufus-lua
4
- #
5
- # Thu Mar 12 15:40:26 JST 2009
6
- #
7
-
8
- $:.unshift('lib')
9
-
10
- require 'benchmark'
11
-
12
- require 'rubygems'
13
- require 'rufus/lua'
14
-
15
- RUBYFIBS = Fiber.new do
16
- n1 = n2 = 1
17
- loop do
18
- Fiber.yield n1
19
- n1, n2 = n2, n1+n2
20
- end
21
- end
22
- #20.times { print RUBYFIBS.resume, ' ' }
23
-
24
- s = %{
25
- co = coroutine.create(function ()
26
- local n1 = 1; local n2 = 1
27
- while true do
28
- coroutine.yield(n1)
29
- n1, n2 = n2, n1+n2
30
- end
31
- end)
32
- return co
33
- }
34
-
35
- LUA = Rufus::Lua::State.new
36
- LUAFIBS = LUA.eval(s)
37
- #20.times { print LUAFIBS.resume, ' ' }
38
-
39
- N = 10_000
40
- Benchmark.benchmark(' ' * 31 + Benchmark::Tms::CAPTION, 31) do |b|
41
- b.report('ruby') do
42
- N.times { RUBYFIBS.resume }
43
- end
44
- b.report('lua via ruby') do
45
- N.times { LUAFIBS.resume }
46
- end
47
- b.report('lua') do
48
- LUA.eval("for i = 0, #{N} do coroutine.resume(co) end")
49
- end
50
- end
51
-
52
- LUA.close
53
-
54
-
55
- # $ ruby19 test/bm_fiber.rb
56
- # user system total real
57
- # ruby 0.050000 0.010000 0.060000 ( 0.054605)
58
- # lua via ruby 0.180000 0.000000 0.180000 ( 0.189010)
59
- # lua 0.010000 0.000000 0.010000 ( 0.005543)
60
- #
61
- # $ ruby19 test/bm_fiber.rb
62
- # user system total real
63
- # ruby 0.050000 0.000000 0.050000 ( 0.051531)
64
- # lua via ruby 0.180000 0.010000 0.190000 ( 0.194944)
65
- # lua 0.010000 0.000000 0.010000 ( 0.006325)
66
- #
67
- # $ ruby19 test/bm_fiber.rb
68
- # user system total real
69
- # ruby 0.050000 0.010000 0.060000 ( 0.052032)
70
- # lua via ruby 0.180000 0.000000 0.180000 ( 0.195411)
71
- # lua 0.010000 0.000000 0.010000 ( 0.006394)
72
- #
73
- # $ ruby19 test/bm_fiber.rb
74
- # user system total real
75
- # ruby 0.050000 0.010000 0.060000 ( 0.054892)
76
- # lua via ruby 0.180000 0.000000 0.180000 ( 0.267880)
77
- # lua 0.000000 0.000000 0.000000 ( 0.005865)
78
-
@@ -1,43 +0,0 @@
1
-
2
- #
3
- # benchmarking rufus-lua
4
- #
5
- # Tue Mar 17 15:02:27 JST 2009
6
- #
7
-
8
- $:.unshift('lib')
9
-
10
- require 'benchmark'
11
-
12
- require 'rubygems'
13
- require 'rufus/lua'
14
-
15
- code = %{
16
- for i = 1, 10000 do
17
- local a = {}
18
- for j = 1, 2000 do
19
- a[#a] = j
20
- end
21
- end
22
- }
23
-
24
- Benchmark.benchmark(' ' * 31 + Benchmark::Tms::CAPTION, 31) do |b|
25
-
26
- l = Rufus::Lua::State.new
27
-
28
- b.report('lua (GC on)') do
29
- l.eval(code)
30
- end
31
-
32
- l.close
33
-
34
- l = Rufus::Lua::State.new
35
- l.gc_stop
36
-
37
- b.report('lua (GC off)') do
38
- l.eval(code)
39
- end
40
-
41
- l.close
42
- end
43
-
@@ -1,19 +0,0 @@
1
- #
2
- # Test garbage collection API from rufus-lua
3
- #
4
-
5
- $:.unshift('lib')
6
-
7
- require 'rubygems'
8
- require 'rufus/lua'
9
-
10
-
11
- puts "Creating a new state..."
12
- s = Rufus::Lua::State.new
13
- puts " #{s.gc_count} KB in use by Lua interpreter"
14
- puts " Calling into Lua..."
15
- puts s.eval("return table.concat({ ' hello', 'from', 'Lua' }, ' ')")
16
- puts " #{s.gc_count} KB in use by Lua interpreter"
17
- puts "Performing forced garbage collection..."
18
- s.gc_collect!
19
- puts " #{s.gc_count} KB in use by Lua interpreter"