Introduction
Rings is a library which provides a way to create new Lua states from within Lua. It also offers a simple way to communicate between the creator (master) and the created (slave) states.
Rings is free software and uses the same license as Lua 5.x (MIT).
Rings also offers Stable, a very simple API to manage a shared table at the master state.
Building
Rings works with Lua 5.1, 5.2, 5.3, and LuaJIT. In order to build it the language library and header files for the desired Lua version must be installed properly.
If you wish to install by hand, the distribution provides a
Makefile
prepared to compile the library and install it.
The file config
should be edited to suit the particularities of the target platform
before running make
.
This file has some definitions like paths to the external libraries,
compiler options and the like.
One important definition is the Lua version,
which is not obtained from the installed software.
Installation
If you are using LuaRocks, just type
luarocks install rings
If you prefer to install manually, the compiled binary file should be copied to a directory in your
C path.
The file stable.lua
should be copied to a directory in your
Lua path.
Reference
Master functions
Rings offers a single function which creates a new Lua state and returns an object representing it. The state which creates other states is called the master and the created ones are called slaves. The master can execute code in any of its slaves but each slave only has direct access to its master (or its own slaves).
All standard Lua libraries are opened automatically in a new state; other libraries have to be loaded explicitly.
The object representing a slave state has a method (dostring
)
which can execute Lua code in the corresponding state.
This method can receive arguments (only numbers, strings, booleans and userdata,
which are converted to lightuserdata) and always returns a boolean indicating
whether the code executed correctly or not, followed by eventual return values
or an error message.
- rings.new (env)
- Returns a newly created Lua state. Takes an optional environment to be used by
remotedostring
. If the environment is nil, it defaults to the master_M or _G
tables. - state:close ()
- Closes the state.
- state:dostring (string, ...)
- Executes a string in the slave state.
The arguments could be accessed exactly as in a
vararg
function. Valid types of arguments and return values are:
number, string, boolean, nil and userdata (which are converted
to lightuserdata).
Returns a boolean indicating the status of the operation, followed by the returned values or an error message in case of error.
Slave function
The following function is registered in the newly created slave state.
- remotedostring (string, ...)
- Executes a string in the master state. Behaves exactly as the method dostring except that it acts in the master state.
Stable
Stable is a simple API which provides a way for a slave state to store and retrieve data to and from its master state. This library is not opened automatically in a slave state.
- stable.get (key)
- Returns the value of a given key.
- stable.set (key, value)
- Stores a value associated to a key. Returns nothing.
Examples
The following sample shows how to execute code in another state passing arguments and returning values:
local rings = require "rings" local code = [[ local args_reversed = {} for _, value in ipairs({...}) do table.insert(args_reversed, 1, value) end return table.unpack(args_reversed) ]] local state = rings.new() print(state:dostring(code, 1, 2, 3)) -- true, 3, 2, 1 state:close()
The following example uses Stable to store a value in the master state:
local rings = require "rings" local code = [[ local stable = require "stable" local count = stable.get("shared_counter") or 0 stable.set("shared_counter", count + 1) return count ]] local state = rings.new() print(state:dostring(code)) -- true, 0 print(state:dostring(code)) -- true, 1 state:close() local another_state = rings.new() print(another_state:dostring(code)) -- true, 2 another_state:close()