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) 返回 ...
随机推荐
- Mockito学习资料
官网:http://mockito.org/ https://dzone.com/refcardz/mockito
- golang github.com/go-sql-driver/mysql 遇到的数据库,设置库设计不合理的解决方法
golang github.com/go-sql-driver/mysql 遇到的数据库,设置库设计不合理的解决方法,查询中报了以下这个错 Scan error on column index 2: ...
- golang gc 问题(转的)
在实际使用go语言的过程中,碰到了一些看似奇怪的内存占用现象,于是决定对go语言的垃圾回收模型进行一些研究.本文对研究的结果进行一下总结. 什么是垃圾回收? 曾几何时,内存管理是程序员开发应用的一大难 ...
- C# PInvoke(DllImport使用) 进阶教程(一)转
我们曾经熟悉的WindowsAPI, 我们曾经花费了大量精力写的代码,难道我们就要轻易放弃吗 不过当下微软已经把向下兼容性放在很重要的位置.C#程序员使用已有的代码来作为自己程序的一部分是很普通的事情 ...
- android-View视图的根基类
- Hadoop.2.x_网站PV示例
一.网站基本指标(即针对于网站用户行为而产生的日志中进行统计分析) 1. PV:网页浏览量(Page View页面浏览次数,只要进入该网页就产生一条记录,不限IP,统计点每天(较多)/每周/每月/.. ...
- c#语句 类
知识点: 1.string类 2.Math类 3.DateTime 获取时间 for穷举 1.羽毛球拍15元,球3元,水2元.现有200元,每种至少买一个,共有多少种可能.
- Shell编程中括号判断中赋值语句和判断语句
#!/bin/bash declare var="xxx" # without space and use one = #1.judge whether the assignmen ...
- Password Attacker
Passwords are widely used in our lives: for ATMs, online forum logins, mobile device unlock and door ...
- Final-阶段站立会议2
组名:天天向上 组长:王森 组员:张政.张金生.林莉.胡丽娜 代码地址:HTTPS:https://git.coding.net/jx8zjs/llk.git SSH:git@git.coding.n ...