Lua Rings库介绍
Rings需求
如果有一段lua脚本代码, 本来来源不可靠, 可能有安全性问题, 或者不像让这份代码污染了正在执行的lua环境, 则需要lua rings工具出厂了。
其在主lua环境中,即在宿主脚本中, 调用rings库创建一个子的lua环境, 将不可靠的lua代码在子lua环境中运行, 运行完毕环境销毁, 一点都不影响宿主环境。
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.
资源
官网介绍,有帮助文档:
http://keplerproject.github.io/rings/manual.html
git hub可以下载最新源代码:
https://github.com/keplerproject/rings
接口
基本接口
- rings.new (env) --- 创建一个lua环境
- 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 _Gtables.- state:close () --- 销毁创建的lua环境
- Closes the state.
- state:dostring (string, ...) ---- 在lua环境中执行一段lua代码
- 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.
此插件还考虑到, 子环境如何存储 数据, 保证与宿主环境可以交换数据:
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.
实验
参考其源代码中最简单的例子sample.lua
- $Id: sample.lua,v 1.4 // :: carregal Exp $
require"rings"
S = rings.new ()
data = { , , , }
print (S:dostring ([[
aux = {}
for i, v in ipairs {...} do
table.insert (aux, 1, v)
end
return unpack (aux)]], unpack (data)))
-- 主要在此处说明, 子环境不会污染宿主环境, 同时说明了 宿主环境中执行不可靠代码, 将会污染宿主环境自身
if 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[]);
end
S:close ()
print("OK!")
~
实验结果:
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!
Lua Rings库介绍的更多相关文章
- Lua标准库(转)
转载地址:http://www.yiibai.com/lua/lua_standard_libraries.html Lua的标准库提供了一组丰富的功能,与C的API直接实现,建立在Lua编程语言函数 ...
- Lua5.1基本函数库介绍
Lua5.1基本函数库介绍assert (v [, message])功能:相当于C的断言,参数:v:当表达式v为nil或false将触发错误,message:发生错误时返回的信息,默认为" ...
- Android开发中用到的框架、库介绍
Android开发中用到的框架介绍,主要记录一些比较生僻的不常用的框架,不断更新中...... 网路资源:http://www.kuqin.com/shuoit/20140907/341967.htm ...
- Lua语言的介绍和编程语言的归类
Lua 本条目介绍的是一种编程语言.关于关于Lua在维基百科中的使用,请见"维基百科:Lua".关于"Lua"一词的其他意思,请见"卢阿". ...
- Lua 学习之基础篇七<Lua Module,Package介绍>
Lua 之Module介绍 包管理库提供了从 Lua 中加载模块的基础库. 只有一个导出函数直接放在全局环境中: [require]. 所有其它的部分都导出在表 package 中. require ...
- DBoW2库介绍
DBoW2库是University of Zaragoza里的Lopez等人开发的开源软件库. 由于在SLAM回环检测上的优异表现(特别是ORB-SLAM2),DBoW2库受到了广大SLAM爱好者的关 ...
- Alljoyn瘦客户端库介绍(官方文档翻译)
Alljoyn瘦客户端库介绍(上) 1.简介 本文档对AllJoynTM瘦客户端的核心库文件(AJTCL)进行了详尽的介绍.本文档介绍了系统整体架构,AllJoyn框架结构,并着重于介绍如何将嵌入式设 ...
- Lua标准库- 模块(Modules)
Lua包库为lua提供简易的加载及创建模块的方法,由require.module方法及package表组成 1.module (name [, ···]) 功能:建立一个模块. module的处理流程 ...
- Lua字符串库(整理)
Lua字符串库小集 1. 基础字符串函数: 字符串库中有一些函数非常简单,如: 1). string.len(s) 返回字符串s的长度: 2). string.rep(s,n) 返回 ...
随机推荐
- Android HttpURLConnection And HttpClient
Google的工程师的一个博客写到: HttpURLConnection和HttpClient Volley HTTP请求时:在Android 2.3及以上版本,使用的是HttpURLConnecti ...
- Codeforces Round #249 (Div. 2) B. Pasha Maximizes
看到题目的时候,以为类似插入排序,比较第i个元素和第i-1个元素, 如果第i个元素比第i-1个元素小,则不交换 如果第i个元素比第i-1个元素大,则交换第i个元素和第i-1个元素 继续比较第i-1个元 ...
- 旋转轮子 UIActivityIndicatorView
旋转轮子 *activityView = [[UIActivityIndicatorView alloc ]initWithActivityIndicatorStyle:UIActivityIndic ...
- uestc 1073 秋实大哥与线段树 Label:线段树
秋实大哥与线段树 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) “学习本无底, ...
- [BZOJ2790][Poi2012]Distance
2790: [Poi2012]Distance Time Limit: 40 Sec Memory Limit: 128 MBSubmit: 225 Solved: 115[Submit][Sta ...
- UVA 11609 - Teams(二项式系数)
题目链接 想了一会,应该是跟二项式系数有关系,无奈自己推的式子,构不成二项式的系数. 选1个人Cn1*1,选2个人Cn2*2....这样一搞,以为还要消项什么的... 搜了一下题解,先选队长Cn1,选 ...
- css比较容易搞混的三个选择器
直接后代选择器:> .grid>input[type="button"] 所有后代选择器:空格 .grid input[type="button"] ...
- 处理海量数据的高级排序之——快速排序(C++)
代码实现 ...
- Maven构建简单的多模块项目
复制于http://www.cnblogs.com/luxh/p/3506750.html 做个记录 一般web项目会进行分模块开发.这里简单分为domain(领域层).persist(持久层).se ...
- MySQL数据库基本指令
对MySQL的指令不太熟悉,在此特别整理了一下一些常用的指令: 约定:大写字母写SQL关键字和函数名,小写字母写数据库.数据表和数据列的名字.(下述代码更新不同步,部分代码未依据此约定) 1 数据库的 ...