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 _G tables.
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库介绍的更多相关文章

  1. Lua标准库(转)

    转载地址:http://www.yiibai.com/lua/lua_standard_libraries.html Lua的标准库提供了一组丰富的功能,与C的API直接实现,建立在Lua编程语言函数 ...

  2. Lua5.1基本函数库介绍

    Lua5.1基本函数库介绍assert (v [, message])功能:相当于C的断言,参数:v:当表达式v为nil或false将触发错误,message:发生错误时返回的信息,默认为" ...

  3. Android开发中用到的框架、库介绍

    Android开发中用到的框架介绍,主要记录一些比较生僻的不常用的框架,不断更新中...... 网路资源:http://www.kuqin.com/shuoit/20140907/341967.htm ...

  4. Lua语言的介绍和编程语言的归类

    Lua 本条目介绍的是一种编程语言.关于关于Lua在维基百科中的使用,请见"维基百科:Lua".关于"Lua"一词的其他意思,请见"卢阿". ...

  5. Lua 学习之基础篇七<Lua Module,Package介绍>

    Lua 之Module介绍 包管理库提供了从 Lua 中加载模块的基础库. 只有一个导出函数直接放在全局环境中: [require]. 所有其它的部分都导出在表 package 中. require ...

  6. DBoW2库介绍

    DBoW2库是University of Zaragoza里的Lopez等人开发的开源软件库. 由于在SLAM回环检测上的优异表现(特别是ORB-SLAM2),DBoW2库受到了广大SLAM爱好者的关 ...

  7. Alljoyn瘦客户端库介绍(官方文档翻译)

    Alljoyn瘦客户端库介绍(上) 1.简介 本文档对AllJoynTM瘦客户端的核心库文件(AJTCL)进行了详尽的介绍.本文档介绍了系统整体架构,AllJoyn框架结构,并着重于介绍如何将嵌入式设 ...

  8. Lua标准库- 模块(Modules)

    Lua包库为lua提供简易的加载及创建模块的方法,由require.module方法及package表组成 1.module (name [, ···]) 功能:建立一个模块. module的处理流程 ...

  9. Lua字符串库(整理)

    Lua字符串库小集 1. 基础字符串函数:    字符串库中有一些函数非常简单,如:    1). string.len(s) 返回字符串s的长度:    2). string.rep(s,n) 返回 ...

随机推荐

  1. 原生js添加和删除类

    原生js添加和删除类: this.className +=" "; this.className = this.className.replace(" 原来的类" ...

  2. ios cocos2d 画线出现闪烁问题

    根据http://www.merowing.info/2012/04/drawing-smooth-lines-with-cocos2d-ios-inspired-by-paper/ 用cocos2d ...

  3. Windows Phone 8 LongListSelector实现下拉自动刷新列表

    LongListSelector是一个加强版的列表控件,它可以实现分组的列表,如系统人脉列表的交互效果就可以利用LongListSelector控件去实现,同时LongListSelector也一样可 ...

  4. 【Java】RuleSource约束常用方法整理

    1-常用约束规则RuleSource的设置方法   例如: addRules(new Rules(ProgramFeeItem.class){ protected void initRules() { ...

  5. 通过Ajax post Json类型的数据到Controller

    View function postSimpleData() { $.ajax({ type: "POST", url: "/Service/SimpleData&quo ...

  6. poj 3254 状压dp入门题

    1.poj 3254  Corn Fields    状态压缩dp入门题 2.总结:二进制实在巧妙,以前从来没想过可以这样用. 题意:n行m列,1表示肥沃,0表示贫瘠,把牛放在肥沃处,要求所有牛不能相 ...

  7. MapReduce输入格式

    文件是 MapReduce 任务数据的初始存储地.正常情况下,输入文件一般是存储在 HDFS 里面.这些文件的格式可以是任意的:我们可以使用基于行的日志文件, 也可以使用二进制格式,多行输入记录或者其 ...

  8. [慢查优化]慎用MySQL子查询,尤其是看到DEPENDENT SUBQUERY标记时

    案例梳理时间:2013-9-25 写在前面的话: 在慢查优化1和2里都反复强调过 explain 的重要性,但有时候肉眼看不出 explain 结果如何指导优化,这时候还需要有一些其他基础知识的佐助, ...

  9. selenium grid中的多个线程同步执行

    需求:有一个工作流,每一步审批都需要多个领导参与,才能推流程到下一步去 代码思考:多个领导在自己的线程中运行,速度有的快有的慢,如何保证下一步的领导审批时,这个步骤已经激活 如下是代码:思路为:如果这 ...

  10. JSP 连接MySQL 5.1

    //数据库为cc, 数据表emp/*CREATE DATABASE `cc` /*!40100 COLLATE 'utf8_unicode_ci' */ CREATE TABLE `emp` ( `e ...