Giter Club home page Giter Club logo

rufus-lua's Introduction

rufus-lua

Lua embedded in Ruby, via Ruby FFI

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

getting Lua on your system

On Debian GNU/Linux, I do

  sudo apt-get install liblua5.1-0

If your system's package manager doesn't have some version (5.1.x) of Lua around, jump to "compiling liblua.dylib" below.

using rufus-lua

If you don't have liblua.dylib on your system, scroll until "compiling liblua.dylib" to learn how to get it.

  gem install rufus-lua

or add to your Gemfile:

  gem 'rufus-lua'

then

  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 '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 '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 '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 :

https://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 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 :

http://groups.google.com/group/rufus-ruby

issue tracker

http://github.com/jmettraux/rufus-lua/issues

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

authors

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

rufus-lua's People

Contributors

jmettraux avatar hoanga avatar xunker avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.