ruby to lua
With the help from Alain I just released rufus-lua 0.1.0.
This ruby gem talks to Lua via Ruby FFI. One could say it’s “embedding Lua”, as Lua is described as “Lua is a powerful, fast, lightweight, embeddable scripting language.”
I feel tempted by Lua, there’s the speed, the austerity and that javascript feeling.
Rufus-lua is not the first bridge between Lua and Ruby, there are also :
* http://rubyluabridge.rubyforge.org/
* http://raa.ruby-lang.org/project/ruby-lua
They are worth a look, but I was a bit disappointed, they are not available via ‘sudo gem install …’.
Rufus-lua is available via Rubygems, but since it’s a FFI thing, it needs to talk to the dynamic library packaging of Lua. Well, it’s not available by default, there’s some work involved (but not too much).
Something great about FFI is that it works for Ruby 1.8.6, Ruby 1.9.1 and JRuby (1.1.6) (and will work soon for Rubinius, its original platform).
Once liblua.dylib is on your system, along with rufus-lua (sudo gem install rufus-lua), you can play with code like :
require 'rubygems'
require 'rufus/lua'
s = Rufus::Lua::State.new
t = s.eval(%{
return { message = { 'hello', 'from', 'Lua' },
funk = function (x) return 2^x end }
})
p t['message'].to_a # => ["hello", "from", "lua"]
p t['funk'].call(8) # => 256.0
s.close
Well, it’s only a 0.1.0 release…
As rufus-lua is using FFI it’s slower at the “frontier”, here is a naive benchmark for computing a Fibonacci suite, with Fiber (Ruby 1.9) and Coroutines on the Lua side :
require 'benchmark'
require 'rubygems'
require 'rufus/lua'
RUBYFIBS = Fiber.new do
n1 = n2 = 1
loop do
Fiber.yield n1
n1, n2 = n2, n1+n2
end
end
s = %{
co = coroutine.create(function ()
local n1 = 1; local n2 = 1
while true do
coroutine.yield(n1)
n1, n2 = n2, n1+n2
end
end)
return co
}
LUA = Rufus::Lua::State.new
LUAFIBS = LUA.eval(s)
N = 10_000
Benchmark.benchmark(' ' * 31 + Benchmark::Tms::CAPTION, 31) do |b|
b.report('ruby') do
N.times { RUBYFIBS.resume }
end
b.report('lua via ruby') do
N.times { LUAFIBS.resume }
end
b.report('lua') do
LUA.eval("for i = 0, #{N} do coroutine.resume(co) end")
end
end
LUA.close
For the original. Running it on my board yields something along the lines of :
$ ruby19 test/bm_fiber.rb
user system total real
ruby 0.050000 0.010000 0.060000 ( 0.052032)
lua via ruby 0.180000 0.000000 0.180000 ( 0.195411)
lua 0.010000 0.000000 0.010000 ( 0.006394)
Well, nothing spectacular, that just states that it’s better to not cross the border too often : the “lua via ruby” bench has Ruby resuming the Lua coroutine 10_000 times and it’s costly.
rufus-lua,
on github : http://github.com/jmettraux/rufus-lua/
rdoc : http://rufus.rubyforge.org/rufus-lua/
the rest of rufus : http://rufus.rubyforge.org
lua : http://www.lua.org
ffi : http://kenai.com/projects/ruby-ffi/
[...] the hall of promising Ruby libraries: an FFI binding to Lua, Ruby to Lua, a neat framework for building Twitter bots, TwiBot and some sugar over the Cascading library [...]
The Real Adam – A few promising Ruby libraries
March 18, 2009 at 12:31 am
“Well, it’s only a 0.1.0 release…”
I still think your lua binding is pretty cool.
Plus gem installing from github is so much nicer than mucking with raa.
And i’ve always had trouble with the other binding: rubyluabridge.
Ben
March 18, 2009 at 7:02 pm
Ben : thanks a lot ! Now working on callbacks from Lua to Ruby. But that will continue after I fix the issue you reported about rufus-tokyo. :)
John Mettraux
March 18, 2009 at 11:28 pm
[...] روابـــ(6)ــط April 2, 2009 at 7:14 pm | In روابــــــط | No Comments سلام من الله عليكم ورحمته وبركاته ومغفرته ورضوانه * كيف تصبح عالماً؟ * العدد الأول من مجلة Adur[ IT ] Magazine * Twitter Tools and Tutorials For Designers and Developers * drailsV1.0 مُتاح للتحميل *لماذا لا نستخدم محركات البحث العربيه؟ * Ruby Mini Guide * Juno أطار بالبايثون لتطبيقات الويب شبيه بـSinatra * ruby to lua [...]
روابـــ(6)ــط « Mutati0N
April 2, 2009 at 7:14 pm