public
Description: embedding Lua in Ruby, via FFI
Homepage: http://rufus.rubyforge.org
Clone URL: git://github.com/jmettraux/rufus-lua.git
jmettraux (author)
Thu Mar 26 22:44:37 -0700 2009
commit  e0af4fe0d2198aa9e7423e891491956f8922d902
tree    e43dcb2cc3fe7021419ac28e2c4e133032bf70f5
parent  a6290e48aa678050c844149b828fd3f8bc5ed7f0
rufus-lua / README.txt
100644 215 lines (113 sloc) 3.843 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
= rufus-lua
 
Lua embedded in Ruby, via Ruby FFI
 
Tested with
  ruby 1.8.6, ruby 1.9.1p0, jruby 1.2.0
  jruby 1.1.6 has an issue with errors raised inside of Ruby functions (callbacks)
 
 
== Lua
 
http://www.lua.org/about.html says :
 
"""
Lua is a powerful, fast, lightweight, embeddable scripting language.
 
Lua combines simple procedural syntax with powerful data description constructs based on associative arrays and extensible semantics. Lua is dynamically typed, runs by interpreting bytecode for a register-based virtual machine, and has automatic memory management with incremental garbage collection, making it ideal for configuration, scripting, and rapid prototyping.
"""
 
http://www.lua.org/
 
 
== other Ruby and Lua bridges / connectors
 
 
  http://rubyluabridge.rubyforge.org/
  http://raa.ruby-lang.org/project/ruby-lua
 
 
== using rufus-lua
 
If you don't have liblua.dylib on your system, scroll until "compiling liblua.dylib" to learn how to get it.
 
  sudo gem install rufus-lua
 
then
 
  require 'rubygems'
  require 'rufus/lua'
 
  s = Rufus::Lua::State.new
 
  puts s.eval("return table.concat({ 'hello', 'from', 'Lua' }, ' ')")
    #
    # => "Hello from Lua"
 
  s.close
 
 
=== binding Ruby code as Lua functions
 
  require 'rubygems'
  require 'rufus/lua'
  
  s = Rufus::Lua::State.new
  
  s.function 'key_up' do |table|
    table.inject({}) do |h, (k, v)|
      h[k.to_s.upcase] = v
    end
  end
  
  p s.eval(%{
    local table = { CoW = 2, pigs = 3, DUCKS = 'none' }
    return key_up(table) -- calling Ruby from Lua...
  }).to_h
    # => { 'COW' => 2.0, 'DUCKS => 'none', 'PIGS' => 3.0 }
  
  s.close
 
 
It's OK to bind a function inside of a table (library) :
 
  require 'rubygems'
  require 'rufus/lua'
  
  s = Rufus::Lua::State.new
 
  s.eval("rubies = {}")
  s.function 'add' do |x, y|
    x + y
  end
 
  s.eval("rubies.add(1, 2)")
    # => 3.0
 
  s.close
 
 
You can omit the table definition (only 1 level allowed here though) :
 
  require 'rubygems'
  require 'rufus/lua'
  
  s = Rufus::Lua::State.new
 
  s.function 'rubies.add' do |x, y|
    x + y
  end
 
  s.eval("rubies.add(1, 2)")
    # => 3.0
 
  s.close
  
 
 
The specs contain more examples :
 
http://github.com/jmettraux/rufus-lua/tree/master/spec/
 
rufus-lua's rdoc is at :
 
http://rufus.rubyforge.org/rufus-lua/
 
 
== compiling liblua.dylib
 
original instructions by Adrian Perez at :
 
http://lua-users.org/lists/lua-l/2006-09/msg00894.html
 
get the source at
 
http://www.lua.org/ftp/lua-5.1.4.tar.gz
 
then
 
  tar xzvf lua-5.1.4.tar.gz
  cd lua-5.1.4
 
modify the file src/Makefile as per http://lua-users.org/lists/lua-l/2006-09/msg00894.html
 
  make
  make masocx # or make linux ...
  make -C src src liblua.dylib
 
  sudo cp src/liblua.dylib /usr/local/lib/
 
 
== build dependencies
 
You need to add the github gems to your gem sources
  gem sources -a http://gems.github.com
 
The following gems are needed to run the specs
  mislav-hanna
  install bacon
 
 
== tested with
 
ruby 1.8.6, ruby 1.9.1p0, jruby 1.2.0
jruby 1.1.6 has an issue with errors raised inside of Ruby functions (callbacks)
 
 
== dependencies
 
the ruby gem 'ffi'
 
 
== mailing list
 
On the rufus-ruby list :
 
  http://groups.google.com/group/rufus-ruby
 
 
== issue tracker
 
  http://rubyforge.org/tracker/?atid=18584&group_id=4812&func=browse
 
 
== irc
 
irc.freenode.net #ruote
 
 
== source
 
http://github.com/jmettraux/rufus-lua
 
  git clone git://github.com/jmettraux/rufus-lua.git
 
 
== credits
 
many thanks to the authors of Ruby FFI, and of Lua
 
http://kenai.com/projects/ruby-ffi/
 
http://lua.org/
 
 
== authors
 
John Mettraux, jmettraux@gmail.com, http://jmettraux.wordpress.com
Alain Hoang, http://blogs.law.harvard.edu/hoanga/
 
 
== the rest of Rufus
 
http://rufus.rubyforge.org
 
 
== license
 
MIT
 
Lua itself is licensed under the MIT license as well :
 
http://www.lua.org/license.html