rufus-lua 1.1.0 → 1.1.1
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.
- data/CHANGELOG.txt +17 -0
- data/CREDITS.txt +7 -2
- data/LICENSE.txt +1 -1
- data/README.md +263 -0
- data/Rakefile +82 -0
- data/lib/rufus/lua.rb +4 -1
- data/lib/rufus/lua/error.rb +86 -0
- data/lib/rufus/lua/lib.rb +34 -14
- data/lib/rufus/lua/objects.rb +11 -12
- data/lib/rufus/lua/state.rb +74 -55
- data/lib/rufus/lua/utils.rb +3 -3
- data/lib/rufus/lua/version.rb +32 -0
- data/rufus-lua.gemspec +36 -0
- data/spec/coroutines_spec.rb +90 -0
- data/spec/eval_spec.rb +125 -0
- data/spec/functions_spec.rb +119 -0
- data/spec/gc_spec.rb +41 -0
- data/spec/lib_spec.rb +32 -0
- data/spec/rfunctions_spec.rb +282 -0
- data/spec/spec_base.rb +10 -0
- data/spec/state_spec.rb +106 -0
- data/spec/string_spec.rb +46 -0
- data/spec/tables_spec.rb +165 -0
- data/spec/utils_spec.rb +36 -0
- data/test/bm0.rb +41 -0
- data/test/bm_fiber.rb +78 -0
- data/test/bm_gc_stop.rb +43 -0
- data/test/gc0.rb +19 -0
- data/test/t.rb +19 -0
- metadata +105 -56
- data/README.rdoc +0 -210
data/CHANGELOG.txt
CHANGED
@@ -2,6 +2,23 @@
|
|
2
2
|
= rufus-lua CHANGELOG.txt
|
3
3
|
|
4
4
|
|
5
|
+
== rufus-lua - 1.1.1 released 2014/07/01
|
6
|
+
|
7
|
+
- move from bacon to rspec
|
8
|
+
- remove double VERSION (Nathanael Jones - https://github.com/nathanaeljones)
|
9
|
+
- load only one Lua lib (Thanks Nathanael)
|
10
|
+
- add Rufus::Lua::Lib.path to learn which Lua lib got loaded (idea Nathanael)
|
11
|
+
- set `ffi_lib_flags(:lazy, :global)` before loading Lua (thanks Nathanael)
|
12
|
+
- fix panic error when creating new Rufus::Lua::State with specific
|
13
|
+
list of lib to load (fix by Matthew Nielsen - https://github.com/xunker)
|
14
|
+
- read back Lua strings containing \0 bytes (thanks Nathanael Jones)
|
15
|
+
- let Ruby strings containing \0 bytes go to the Lua side freely (thanks NJ)
|
16
|
+
- enhanced Rufus::Lua::LuaError by adding filename and lineno - based on
|
17
|
+
a suggestion by Nathanael Jones)
|
18
|
+
- enhanced Rufus::Lua::State#eval by adding optional 'filename' and 'lineno'
|
19
|
+
arguments (like Kernel.eval) (hint by Nathanael Jones)
|
20
|
+
|
21
|
+
|
5
22
|
== rufus-lua - 1.1.0 released 2009/09/30
|
6
23
|
|
7
24
|
- todo : Ruby symbols are passed to Lua as strings (Scott)
|
data/CREDITS.txt
CHANGED
@@ -4,11 +4,16 @@
|
|
4
4
|
|
5
5
|
== authors
|
6
6
|
|
7
|
-
John Mettraux http://jmettraux.
|
7
|
+
John Mettraux http://jmettraux.lambda.io
|
8
8
|
Alain Hoang http://blogs.law.harvard.edu/hoanga/
|
9
|
-
Scott Persinger
|
9
|
+
Scott Persinger https://github.com/scottpersinger
|
10
10
|
|
11
11
|
|
12
|
+
== contributors
|
13
|
+
|
14
|
+
Matthew Nielsen https://github.com/xunker
|
15
|
+
Nathanael Jones https://github.com/nathanaeljones
|
16
|
+
|
12
17
|
== inspiration
|
13
18
|
|
14
19
|
http://rubyluabridge.rubyforge.org/
|
data/LICENSE.txt
CHANGED
data/README.md
ADDED
@@ -0,0 +1,263 @@
|
|
1
|
+
|
2
|
+
# rufus-lua
|
3
|
+
|
4
|
+
[](http://badge.fury.io/rb/rufus-lua)
|
5
|
+
|
6
|
+
Lua embedded in Ruby, via Ruby FFI.
|
7
|
+
|
8
|
+
(Lua 5.1.x only, sorry).
|
9
|
+
|
10
|
+
|
11
|
+
## Lua
|
12
|
+
|
13
|
+
http://www.lua.org/about.html says :
|
14
|
+
|
15
|
+
|
16
|
+
> Lua is a powerful, fast, lightweight, embeddable scripting language.
|
17
|
+
>
|
18
|
+
> Lua combines simple procedural syntax with powerful data description
|
19
|
+
> constructs based on associative arrays and extensible semantics. Lua is
|
20
|
+
> dynamically typed, runs by interpreting bytecode for a register-based
|
21
|
+
> virtual machine, and has automatic memory management with incremental
|
22
|
+
> garbage collection, making it ideal for configuration, scripting, and
|
23
|
+
> rapid prototyping.
|
24
|
+
|
25
|
+
http://www.lua.org/
|
26
|
+
|
27
|
+
|
28
|
+
## other Ruby and Lua bridges / connectors
|
29
|
+
|
30
|
+
* https://github.com/glejeune/ruby-lua by Gregoire Lejeune
|
31
|
+
* http://rubyluabridge.rubyforge.org/ by Evan Wies
|
32
|
+
* https://github.com/whitequark/rlua by Peter Zotov
|
33
|
+
|
34
|
+
|
35
|
+
## getting Lua on your system
|
36
|
+
|
37
|
+
On Debian GNU/Linux, I do
|
38
|
+
|
39
|
+
```
|
40
|
+
sudo apt-get install liblua5.1-0
|
41
|
+
```
|
42
|
+
|
43
|
+
If your system's package manager doesn't have some version (5.1.x) of Lua around, jump to [compiling liblua.dylib](#compiling-libluadylib) below.
|
44
|
+
|
45
|
+
Rufus-lua will look for library in a [list of know places](https://github.com/jmettraux/rufus-lua/blob/9ddf26cde9f4a73115032504ad7f7eb688849b73/lib/rufus/lua/lib.rb#L38-L50).
|
46
|
+
|
47
|
+
If it doesn't find the Lua dynamic library or if it picks the wrong one, it's OK to set the `LUA_LIB` environment variable. For example:
|
48
|
+
|
49
|
+
```bash
|
50
|
+
LUA_LIB=~/mystuff/lualib.5.1.4.so ruby myluacode.rb
|
51
|
+
```
|
52
|
+
or
|
53
|
+
```bash
|
54
|
+
export LUA_LIB=~/mystuff/lualib.5.1.4.so
|
55
|
+
# ...
|
56
|
+
ruby myluacode.rb
|
57
|
+
```
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
## using rufus-lua
|
62
|
+
|
63
|
+
```
|
64
|
+
gem install rufus-lua
|
65
|
+
```
|
66
|
+
|
67
|
+
or add to your Gemfile:
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
gem 'rufus-lua'
|
71
|
+
```
|
72
|
+
|
73
|
+
then
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
require 'rufus-lua'
|
77
|
+
|
78
|
+
s = Rufus::Lua::State.new
|
79
|
+
|
80
|
+
puts s.eval("return table.concat({ 'hello', 'from', 'Lua' }, ' ')")
|
81
|
+
#
|
82
|
+
# => "Hello from Lua"
|
83
|
+
|
84
|
+
s.close
|
85
|
+
```
|
86
|
+
|
87
|
+
|
88
|
+
### binding Ruby code as Lua functions
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
require 'rufus-lua'
|
92
|
+
|
93
|
+
s = Rufus::Lua::State.new
|
94
|
+
|
95
|
+
s.function 'key_up' do |table|
|
96
|
+
table.inject({}) do |h, (k, v)|
|
97
|
+
h[k.to_s.upcase] = v
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
p s.eval(%{
|
102
|
+
local table = { CoW = 2, pigs = 3, DUCKS = 'none' }
|
103
|
+
return key_up(table) -- calling Ruby from Lua...
|
104
|
+
}).to_h
|
105
|
+
# => { 'COW' => 2.0, 'DUCKS => 'none', 'PIGS' => 3.0 }
|
106
|
+
|
107
|
+
s.close
|
108
|
+
```
|
109
|
+
|
110
|
+
|
111
|
+
It's OK to bind a function inside of a table (library):
|
112
|
+
|
113
|
+
```ruby
|
114
|
+
require 'rufus-lua'
|
115
|
+
|
116
|
+
s = Rufus::Lua::State.new
|
117
|
+
|
118
|
+
s.eval("rubies = {}")
|
119
|
+
s.function 'add' do |x, y|
|
120
|
+
x + y
|
121
|
+
end
|
122
|
+
|
123
|
+
s.eval("rubies.add(1, 2)")
|
124
|
+
# => 3.0
|
125
|
+
|
126
|
+
s.close
|
127
|
+
```
|
128
|
+
|
129
|
+
|
130
|
+
You can omit the table definition (only 1 level allowed here though):
|
131
|
+
|
132
|
+
```ruby
|
133
|
+
require 'rufus-lua'
|
134
|
+
|
135
|
+
s = Rufus::Lua::State.new
|
136
|
+
|
137
|
+
s.function 'rubies.add' do |x, y|
|
138
|
+
x + y
|
139
|
+
end
|
140
|
+
|
141
|
+
s.eval("rubies.add(1, 2)")
|
142
|
+
# => 3.0
|
143
|
+
|
144
|
+
s.close
|
145
|
+
```
|
146
|
+
|
147
|
+
|
148
|
+
The specs contain more examples:
|
149
|
+
|
150
|
+
https://github.com/jmettraux/rufus-lua/tree/master/spec/
|
151
|
+
|
152
|
+
|
153
|
+
### eval(code[, binding[, filename[, lineno ]]])
|
154
|
+
|
155
|
+
The examples so far have shown `Rufus::Lua::State#eval` being used with a single argument, a piece of code.
|
156
|
+
|
157
|
+
But this rufus-lua eval mimics the Ruby `eval` and lets one specify binding, filename and lineno.
|
158
|
+
|
159
|
+
(TODO) Binding hasn't yet been implemented. It'll probaby be with `setfenv` but nothing sure yet. Stick a `nil` to it for now.
|
160
|
+
|
161
|
+
The string of Lua code may come from wild places, it may help to flag it with arbitrary filename and lineno.
|
162
|
+
|
163
|
+
```ruby
|
164
|
+
require 'rufus-lua'
|
165
|
+
|
166
|
+
lua = Rufus::Lua::State.new
|
167
|
+
|
168
|
+
lua.eval('print("hello")', nil, 'myluastuff/hello.lua', 77)
|
169
|
+
```
|
170
|
+
|
171
|
+
|
172
|
+
## compiling liblua.dylib
|
173
|
+
|
174
|
+
original instructions by Adrian Perez at:
|
175
|
+
|
176
|
+
http://lua-users.org/lists/lua-l/2006-09/msg00894.html
|
177
|
+
|
178
|
+
get the source at:
|
179
|
+
|
180
|
+
http://www.lua.org/ftp/lua-5.1.4.tar.gz
|
181
|
+
|
182
|
+
then
|
183
|
+
|
184
|
+
```
|
185
|
+
tar xzvf lua-5.1.4.tar.gz
|
186
|
+
cd lua-5.1.4
|
187
|
+
```
|
188
|
+
|
189
|
+
Modify the file `src/Makefile` as per http://lua-users.org/lists/lua-l/2006-09/msg00894.html
|
190
|
+
|
191
|
+
It's mostly about adding that rule to the `src/Makefile`:
|
192
|
+
```make
|
193
|
+
liblua.dylib: $(CORE_O) $(LIB_O)
|
194
|
+
$(CC) -dynamiclib -o $@ $^ $(LIBS)
|
195
|
+
```
|
196
|
+
|
197
|
+
Here's how to build the library file and deploy it:
|
198
|
+
```
|
199
|
+
make
|
200
|
+
make macosx # or make linux ...
|
201
|
+
make -C src liblua.dylib
|
202
|
+
sudo cp src/liblua.dylib /usr/local/lib/
|
203
|
+
|
204
|
+
sudo make macosx install
|
205
|
+
```
|
206
|
+
|
207
|
+
I tend to copy the lib with
|
208
|
+
|
209
|
+
```
|
210
|
+
sudo cp src/liblua.dylib /usr/local/lib/liblua.5.1.4.dylib
|
211
|
+
|
212
|
+
# instead of
|
213
|
+
#sudo cp src/liblua.dylib /usr/local/lib/
|
214
|
+
```
|
215
|
+
|
216
|
+
|
217
|
+
## tested with
|
218
|
+
|
219
|
+
ruby 1.8.7p72, ruby 1.9.1p0, jruby 1.2.0
|
220
|
+
jruby 1.1.6 has an issue with errors raised inside of Ruby functions (callbacks)
|
221
|
+
|
222
|
+
ruby-ffi 0.4.0 and 0.5.0
|
223
|
+
|
224
|
+
I run the specs with
|
225
|
+
|
226
|
+
```
|
227
|
+
bundle install # first time only
|
228
|
+
bundle exec rspec
|
229
|
+
```
|
230
|
+
|
231
|
+
|
232
|
+
## dependencies
|
233
|
+
|
234
|
+
the ruby gem 'ffi'
|
235
|
+
|
236
|
+
|
237
|
+
## issue tracker
|
238
|
+
|
239
|
+
http://github.com/jmettraux/rufus-lua/issues
|
240
|
+
|
241
|
+
|
242
|
+
## source
|
243
|
+
|
244
|
+
http://github.com/jmettraux/rufus-lua
|
245
|
+
|
246
|
+
```
|
247
|
+
git clone git://github.com/jmettraux/rufus-lua.git
|
248
|
+
```
|
249
|
+
|
250
|
+
|
251
|
+
## authors and credits
|
252
|
+
|
253
|
+
see [CREDITS.txt](CREDITS.txt)
|
254
|
+
|
255
|
+
|
256
|
+
## license
|
257
|
+
|
258
|
+
MIT
|
259
|
+
|
260
|
+
Lua itself is licensed under the MIT license as well :
|
261
|
+
|
262
|
+
http://www.lua.org/license.html
|
263
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
|
2
|
+
$:.unshift('.') # 1.9.2
|
3
|
+
|
4
|
+
require 'rake'
|
5
|
+
require 'rake/clean'
|
6
|
+
require 'rspec/core/rake_task'
|
7
|
+
|
8
|
+
|
9
|
+
#
|
10
|
+
# clean
|
11
|
+
|
12
|
+
CLEAN.include('pkg', 'rdoc')
|
13
|
+
|
14
|
+
|
15
|
+
#
|
16
|
+
# test / spec
|
17
|
+
|
18
|
+
RSpec::Core::RakeTask.new
|
19
|
+
|
20
|
+
task :test => :spec
|
21
|
+
task :default => :spec
|
22
|
+
|
23
|
+
|
24
|
+
#
|
25
|
+
# gem
|
26
|
+
|
27
|
+
GEMSPEC_FILE = Dir['*.gemspec'].first
|
28
|
+
GEMSPEC = eval(File.read(GEMSPEC_FILE))
|
29
|
+
GEMSPEC.validate
|
30
|
+
|
31
|
+
|
32
|
+
desc %{
|
33
|
+
builds the gem and places it in pkg/
|
34
|
+
}
|
35
|
+
task :build do
|
36
|
+
|
37
|
+
sh "gem build #{GEMSPEC_FILE}"
|
38
|
+
sh "mkdir pkg" rescue nil
|
39
|
+
sh "mv #{GEMSPEC.name}-#{GEMSPEC.version}.gem pkg/"
|
40
|
+
end
|
41
|
+
|
42
|
+
desc %{
|
43
|
+
builds the gem and pushes it to rubygems.org
|
44
|
+
}
|
45
|
+
task :push => :build do
|
46
|
+
|
47
|
+
sh "gem push pkg/#{GEMSPEC.name}-#{GEMSPEC.version}.gem"
|
48
|
+
end
|
49
|
+
|
50
|
+
|
51
|
+
##
|
52
|
+
## rdoc
|
53
|
+
##
|
54
|
+
## make sure to have rdoc 2.5.x to run that
|
55
|
+
#
|
56
|
+
#Rake::RDocTask.new do |rd|
|
57
|
+
#
|
58
|
+
# rd.main = 'README.txt'
|
59
|
+
# rd.rdoc_dir = "rdoc/#{GEMSPEC.name}"
|
60
|
+
#
|
61
|
+
# rd.rdoc_files.include('README.mdown', 'CHANGELOG.txt', 'lib/**/*.rb')
|
62
|
+
#
|
63
|
+
# rd.title = "#{GEMSPEC.name} #{GEMSPEC.version}"
|
64
|
+
#end
|
65
|
+
#
|
66
|
+
#
|
67
|
+
##
|
68
|
+
## upload_rdoc
|
69
|
+
#
|
70
|
+
#desc %{
|
71
|
+
# upload the rdoc to rubyforge
|
72
|
+
#}
|
73
|
+
#task :upload_rdoc => [ :clean, :rdoc ] do
|
74
|
+
#
|
75
|
+
# account = 'jmettraux@rubyforge.org'
|
76
|
+
# webdir = '/var/www/gforge-projects/rufus'
|
77
|
+
#
|
78
|
+
# sh "rsync -azv -e ssh rdoc/#{GEMSPEC.name} #{account}:#{webdir}/"
|
79
|
+
#end
|
80
|
+
#
|
81
|
+
# keep that in the fridge for now
|
82
|
+
|
data/lib/rufus/lua.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
#--
|
2
|
-
# Copyright (c) 2009, John Mettraux, Alain Hoang.
|
2
|
+
# Copyright (c) 2009-2014, John Mettraux, Alain Hoang.
|
3
3
|
#
|
4
4
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
5
5
|
# of this software and associated documentation files (the "Software"), to deal
|
@@ -22,8 +22,11 @@
|
|
22
22
|
# Made in Japan.
|
23
23
|
#++
|
24
24
|
|
25
|
+
require 'rufus/lua/version'
|
26
|
+
|
25
27
|
require 'rufus/lua/lib'
|
26
28
|
|
29
|
+
require 'rufus/lua/error'
|
27
30
|
require 'rufus/lua/state'
|
28
31
|
require 'rufus/lua/utils'
|
29
32
|
require 'rufus/lua/objects'
|
@@ -0,0 +1,86 @@
|
|
1
|
+
#--
|
2
|
+
# Copyright (c) 2009-2014, John Mettraux, Alain Hoang.
|
3
|
+
#
|
4
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
5
|
+
# of this software and associated documentation files (the "Software"), to deal
|
6
|
+
# in the Software without restriction, including without limitation the rights
|
7
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
|
+
# copies of the Software, and to permit persons to whom the Software is
|
9
|
+
# furnished to do so, subject to the following conditions:
|
10
|
+
#
|
11
|
+
# The above copyright notice and this permission notice shall be included in
|
12
|
+
# all copies or substantial portions of the Software.
|
13
|
+
#
|
14
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
15
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
16
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
17
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
18
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
19
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
20
|
+
# THE SOFTWARE.
|
21
|
+
#
|
22
|
+
# Made in Japan.
|
23
|
+
#++
|
24
|
+
|
25
|
+
|
26
|
+
module Rufus::Lua
|
27
|
+
|
28
|
+
#
|
29
|
+
# An error class for rufus-lua.
|
30
|
+
#
|
31
|
+
class LuaError < RuntimeError
|
32
|
+
|
33
|
+
attr_reader :kind, :errcode, :msg
|
34
|
+
attr_reader :binding, :filename, :lineno
|
35
|
+
|
36
|
+
attr_reader :original_backtrace
|
37
|
+
|
38
|
+
def initialize(kind, errcode, msg, binding, filename, lineno)
|
39
|
+
|
40
|
+
super("#{kind} : '#{msg}' (#{errcode})")
|
41
|
+
|
42
|
+
@kind = kind
|
43
|
+
@errcode = errcode
|
44
|
+
@msg = msg
|
45
|
+
|
46
|
+
@binding = binding
|
47
|
+
@filename = filename
|
48
|
+
@lineno = lineno
|
49
|
+
end
|
50
|
+
|
51
|
+
def filename
|
52
|
+
|
53
|
+
return @filename if @filename
|
54
|
+
|
55
|
+
m = CALLER_REX.match(backtrace.first || '')
|
56
|
+
return m ? m[1] : nil
|
57
|
+
end
|
58
|
+
|
59
|
+
def lineno
|
60
|
+
|
61
|
+
return @lineno if @lineno
|
62
|
+
|
63
|
+
m = CALLER_REX.match(backtrace.first || '')
|
64
|
+
return m ? m[2].to_i : -1
|
65
|
+
end
|
66
|
+
|
67
|
+
def set_backtrace(trace)
|
68
|
+
|
69
|
+
@original_backtrace = trace
|
70
|
+
|
71
|
+
trace =
|
72
|
+
trace.select { |line|
|
73
|
+
m = CALLER_REX.match(line)
|
74
|
+
( ! m) || File.dirname(m[1]) != DIR
|
75
|
+
}
|
76
|
+
|
77
|
+
trace.insert(0, "#{@filename}:#{@lineno}:") if @filename
|
78
|
+
|
79
|
+
super(trace)
|
80
|
+
end
|
81
|
+
|
82
|
+
CALLER_REX = /^(.+):(\d+):/
|
83
|
+
DIR = File.dirname(__FILE__)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|