Introduction to Lua Rings Library

Source: Internet
Author: User
Tags hosting

rings Demand

If there is a LUA script code that is inherently unreliable, may have security issues, or is not as polluting the executing LUA environment as the code, then the LUA rings tool is required to be shipped out of the factory.

In the main LUA environment, that is, in the host script, call the Rings library to create a sub-LUA environment, run the unreliable LUA code in the sub-LUA environment, run the environment to destroy, do not affect the host environment.

Rings is a library which provides a-to-create new LUA states from within Lua. It also offers a simple-to communicate between the creator (master) and the created (slave) states.

Resources

Website introduction, Helpful documentation:

Http://keplerproject.github.io/rings/manual.html

git hub can download the latest source code:

Https://github.com/keplerproject/rings

Interface

Basic interface

Rings.new (ENV)---Create a LUA environment
Returns a newly created Lua state. Takes an optional environment to being used by remotedostring . If the environment is nil, it defaults to the master _M or _G tables.
State: Close ()---Destroy the created LUA environment
Closes the state.
State:d ostring (String, ...) ----Execute a LUA code in the LUA environment
Executes a string in the slave state. The arguments could is accessed exactly as in a vararg function. Valid types of arguments and return values Are:number, String, Boolean, nil and UserData (which is converted to Lightuse Rdata).
Returns A Boolean indicating the status of the operation, followed by the returned values or an error message in case of E Rror.

This plug-in also takes into account how the sub-environment stores data, ensuring that data can be exchanged with the hosting environment:

Stable

Stable is a simple API which provides a-a-to-a slave the state to store and the retrieve data to and from the IT master state. This library isn't 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.

Experiment

Refer to the simplest example of its source code Sample.lua

-$Id: Sample.lua,v1.4  -/ to/ -  -: -: toCarregal EXP $require"rings"S=rings.new () data= { A, -, -, }Print(S:dostring ([[aux = {}for I, V in ipairs {...} do Table.insert (aux, 1, v) Endreturn unpack (aux)]],Unpack(data)))
--This is explained primarily here that the sub-environment does not pollute the hosting environment, and that the execution of unreliable code in the hosting environment will contaminate the host environment itselfif Nil= = aux Then Print("aux do not exist in Global environment!"); F=LoadString([[aux = {1}]]) Print(f ())--> Do LUA code Print("After call global dostring, aux created, aux[0]==".. aux[1]);Ends:close ()Print("ok!")~

Experimental results:

xx:~/share_windows/opensource/lua/rings-master/tests$ Lua Sample.lua
True 14 13 12
Aux do not exist in Global environment!

After call global dostring, aux created, aux[0]==1
Ok!

Introduction to Lua Rings Library

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.