github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

jmettraux / rufus-lua

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 26
    • 2
  • Source
  • Commits
  • Network (2)
  • Issues (0)
  • Downloads (3)
  • Wiki (1)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (1)
    • master ✓
  • Tags (3)
    • r1.1.0
    • r1.0.0
    • r0.1.0
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

embedding Lua in Ruby, via FFI — Read more

  cancel

http://rufus.rubyforge.org

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

release 1.1.0 
jmettraux (author)
Tue Sep 29 21:26:30 -0700 2009
commit  ec550f9bac9aac87f15673e3a205f36e73f79826
tree    797b7b571439a51fc310948dff18863ea540f50e
parent  24f00703629bb23261b2d1c1b470cb1cf10d550f
rufus-lua /
name age
history
message
file CHANGELOG.txt Tue Sep 29 21:24:29 -0700 2009 preparing release 1.1.0 [jmettraux]
file CREDITS.txt Tue Sep 29 21:24:29 -0700 2009 preparing release 1.1.0 [jmettraux]
file LICENSE.txt Loading commit data...
file README.rdoc Tue Sep 29 21:28:21 -0700 2009 release 1.1.0 [jmettraux]
file Rakefile Wed Aug 19 20:49:34 -0700 2009 switched from rdoc to yard [jmettraux]
file TODO.txt Thu Jul 23 23:15:06 -0700 2009 todo : state['a'] = [ 1, 2, 3 ] now possible [jmettraux]
directory doc/ Wed Mar 11 00:42:39 -0700 2009 eval() working [jmettraux]
directory lib/
file rufus-lua.gemspec
directory spec/ Tue Sep 29 17:30:42 -0700 2009 ensuring spec runs fine with Ruby 1.8 and 1.9 [jmettraux]
directory test/
README.rdoc

rufus-lua

Lua embedded in Ruby, via Ruby FFI

Lua

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. """

www.lua.org/

other Ruby and Lua bridges / connectors

rubyluabridge.rubyforge.org/ 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 :

github.com/jmettraux/rufus-lua/tree/master/spec/

rufus-lua’s rdoc is at :

rufus.rubyforge.org/rufus-lua/

compiling liblua.dylib

original instructions by Adrian Perez at :

lua-users.org/lists/lua-l/2006-09/msg00894.html

get the source at

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 lua-users.org/lists/lua-l/2006-09/msg00894.html

  make
  make macosx # or make linux ...
  make -C src liblua.dylib
  sudo cp src/liblua.dylib /usr/local/lib/

  sudo make macosx install

build dependencies

The following gems are needed to run the specs

  sudo gem install bacon

tested with

ruby 1.8.7p72, ruby 1.9.1p0, jruby 1.2.0 jruby 1.1.6 has an issue with errors raised inside of Ruby functions (callbacks)

ruby-ffi 0.4.0 and 0.5.0

dependencies

the ruby gem ‘ffi’

mailing list

On the rufus-ruby list :

groups.google.com/group/rufus-ruby

issue tracker

github.com/jmettraux/rufus-lua/issues

irc

irc.freenode.net #ruote

source

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

wiki.github.com/ffi/ffi lua.org/

authors

John Mettraux, jmettraux@gmail.com, jmettraux.wordpress.com Alain Hoang, blogs.law.harvard.edu/hoanga/ Scott Persinger, github.com/scottpersinger/

the rest of Rufus

rufus.rubyforge.org

license

MIT

Lua itself is licensed under the MIT license as well :

www.lua.org/license.html

Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server