lua语言三则特性
2016-06-30 00:14
330 查看
pack和unpack
对于一个函数, 要将其入参转换为一个表, 则pack函数合适。对于一个表要将其转换为 一个函数的入参, 则 lua原生提供的 unpack函数可以实现。
do arrayData = {"a", "b", "c", "d", "e"}; function pack(...) return {...}; end print( pack("aa", "bb")[2] ); --print((returnMoreValues())); --print(arrayData); -- print the address of the arrayData --print(unpack(arrayData)); -- print all the elements of the arrayData print(unpack(arrayData, 2)); --the second param is the index of the arrayData end >lua -e "io.stdout:setvbuf 'no'" "luatest.lua" b c d e >Exit code: 0 >lua -e "io.stdout:setvbuf 'no'" "luatest.lua" aa b c d e >Exit code: 0 >lua -e "io.stdout:setvbuf 'no'" "luatest.lua" bb b c d e >Exit code: 0
lua脚本函数也可以序列化
函数序列化, 可以实现函数的跨进程功能。 如果此函数是动态的产生的, 并且需要跨进程访问。http://www.cnblogs.com/luweimy/p/4104642.html
序列化加密
这就是加密的代码,因为
其实就是使用了
string.dump(loadstring(ret))
这就是加密的代码,因为
string.dump参数必须是
function, 所以使用
loadstring将字符串加载成chunk,然后在由
string.dump导成字节码
其实就是使用了
string.dump函数,它可以把
function导成二进制字节码,使用它处理一下就可以把明文字符串转成字节码了
local function ser(var, enc)
assert(type(var)=="table")
-- 标记所有出现的table, 并记录其key, 用于处理循环引用
local mark = {}
-- 用于记录循环引用的赋值语句
local assign = {}
-- 序列化表, ret字符串必须与后面的loca ret=%s中的ret相同,因为这个ret可能也会组织到结果字符串中。
local ret = table_ser(var, "ret", mark, assign)
local ret = string.format("local ret=%s %s return ret", ret, table.concat(assign, ";"))
return (enc==nil or enc==true) and string.dump(loadstring(ret)) or ret
end
https://github.com/Luweimy/luaser/blob/master/luaser.lua
luaL_register
接口 可以多次调用 给指定的模块 添加新接口
http://www.lua.org/manual/5.1/manual.html#lua_registervoid luaL_register (lua_State *L, const char *libname, const luaL_Reg *l);
Opens a library.
When called with
libnameequal to
NULL, it simply registers all functions in the list
l(see
luaL_Reg) into the table on the top of the stack.
When called with a non-null
libname,
luaL_registercreates a new table
t, sets it as the value of the global variable
libname, sets it as the value of
package.loaded[libname], and registers on it all functions in the list
l. If there is a table in
package.loaded[libname]or in variable
libname, reuses this table instead of creating a new one.
In any case the function leaves the table on the top of the stack.
If there is a table in
package.loaded[libname]or in variable
libname, reuses this table instead of creating a new one.
相关文章推荐
- 圆点旋转 loading
- Lua自己实现如果对象是指定类或其子类的实例,返回 true
- Lua自己实现载入一个模块
- Lua创建一个类 继承
- slua与ulua对比
- lua学习笔记--两个实例(markov chain 和 递归上台阶算法)
- lua学习笔记四--协同程序
- lua学习笔记二--函数
- lua学习笔记一 -- 基本数据类型和操作符介绍
- lua 判断颜色相似
- 在C/C++项目中集成Lua
- Uniyt热更新——LuaFrameWork学习(一)
- 用protoc-gen-lua生成PB的lua代码
- Lua脚本中释放binary文件
- lua高阶用法 OO的实现
- Lua自己实现深度克隆一个值
- lua table对象
- lua树型打印table
- openresty+lua做接口调用权限限制
- 100行lua代码实现简单目录浏览器