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. why cpp is a shitty language

    // the below is a standard template for any of my writings about c++ cpp_is_a_shitty_language_as { t ...

  2. 洛谷 P1198 [JSOI2008]最大数 Label:线段树

    题目描述 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作. 语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值. 限制:L不超过当前数列的长度. 2. 插入操作 ...

  3. 4分钟了解nano编辑器和简单命令 2015.10.6

    nano感觉并不常用,但是偶尔遇到过几次. nano命令是一个类似VI的编辑器,但是更简单,其中的i,a等命令似乎可以用,但是有些命令不可以用.保存和退出最大的区别在于退出方式,如果你要保存所做的修改 ...

  4. JS:原型

      function Box(){ } //使用字面量的方式创建原型对象,这里{}就是对象,是Object, new Object相当于{} Box.prototype = { name:" ...

  5. BZOJ4500: 矩阵

    Description 有一个n*m的矩阵,初始每个格子的权值都为0,可以对矩阵执行两种操作: 1. 选择一行, 该行每个格子的权值加1或减1. 2. 选择一列, 该列每个格子的权值加1或减1. 现在 ...

  6. HDU-1466 计算直线的交点数 经典dp

    1.HDU-1466   计算直线的交点数 2.链接:http://acm.hdu.edu.cn/showproblem.php?pid=1466 3.总结:不会推这个,看了题解.. 状态转移: m条 ...

  7. 谷歌浏览器安装adblock广告屏蔽插件

    访问不到google的应用商店的话,就从网上直接查找adblock for chrome.打开浏览器的开发者模式,下载解压后直接拖拽到浏览器即可.下载地址http://www.cr173.com/so ...

  8. 【HDU4419 Colourful Rectangle】 线段树面积并

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4419 题目大意:给你n个矩形,每个矩形都有一种颜色,矩形覆盖会出现另外一种颜色,问你所有矩形中不同的颜 ...

  9. 2016HUAS暑假集训训练2 L - Points on Cycle

    题目链接:http://acm.hust.edu.cn/vjudge/contest/121192#problem/L 这是一道很有意思的题,就是给定一个以原点为圆心的圆,然后给定 一个点  求最大三 ...

  10. Hibernate简单实例

    1.配置hibernate.cfg.xml文件(名字必须这么写): <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernat ...