lua语言三则特性
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_register
void luaL_register (lua_State *L,
const char *libname,
const luaL_Reg *l);Opens a library.
When called with
libnameequal toNULL, it simply registers all functions in the listl(seeluaL_Reg) into the table on the top of the stack.When called with a non-null
libname,luaL_registercreates a new tablet, sets it as the value of the global variablelibname, sets it as the value ofpackage.loaded[libname], and registers on it all functions in the listl. If there is a table inpackage.loaded[libname]or in variablelibname, 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.
lua语言三则特性的更多相关文章
- 理解 Lua 的那些坑爹特性
按:最近看到了依云的文章,一方面,为Lua被人误解而感到十分难过,另一方面,也为我的好友, 依云没有能够体会到Lua的绝妙和优雅之处而感到很遗憾,因此我写了这篇文章,逐条款地说明了 依云理解中出现的一 ...
- 【quick-cocos2d-x】Lua 语言基础
版权声明:本文为博主原创文章,转载请注明出处. 使用quick-x开发游戏有两年时间了,quick-x是cocos2d-Lua的一个豪华升级版的框架,使用Lua编程.相比于C++,lua的开发确实快速 ...
- 编译并使用Lua语言
Lua是一个小巧的脚本语言,该语言设计的目的是为了嵌入应用程序中,从而为应用程序提供灵活的扩展和定制功能. 可扩展性.Lua的扩展性非常卓越,可以通过Lua代码或C代码扩展,很多功能可以通过外部库来扩 ...
- lua 语言笔记
Lua语言基础汇总(1) -- 类型与值 基础介绍 Lua是一种动态类型的语言.在语言中没有类型定义的语法,每个值都带有其自身的类型信息.在Lua中有8种基本类型,分别是: nil(空)类型 bool ...
- Lua语言中文手册 转载自网络
Programming in LuaCopyright ® 2005, Translation Team, www.luachina.net Programming in LuaProgramming ...
- 【转载】【游戏开发】在Lua中实现面向对象特性——模拟类、继承、多态
[游戏开发]在Lua中实现面向对象特性——模拟类.继承.多态 阅读目录 一.简介 二.前提知识 三.Lua中实现类.继承.多态 四.总结 回到顶部 一.简介 Lua是一门非常强大.非常灵活的脚本语 ...
- lua视频教程三套高清新
目录 1. 下载地址 2. 某网校 Lua 经典教程 3. lua脚本语言零基础开发教程19课全 4. LUA完整视频+Cocos2d-x项目实战 1. 下载地址 https://www.cnblog ...
- lua语言自学知识点----Lua与.Net相互调用
知识点: LuaInterface作用是用来完成Lua与C#的相互调用. LuaInterface核心库:1.luainterface.dll 用于C#读取lua(放在bin目录同级) 2.luane ...
- java多线程中的三种特性
java多线程中的三种特性 原子性(Atomicity) 原子性是指在一个操作中就是cpu不可以在中途暂停然后再调度,既不被中断操作,要不执行完成,要不就不执行. 如果一个操作时原子性的,那么多线程并 ...
随机推荐
- 1204. Maze Traversal
1204. Maze Traversal A common problem in artificial intelligence is negotiation of a maze. A maze ...
- 彻底弄明白之java多线程中的volatile
一. volatite 简述 Java 语言提供了一种稍弱的同步机制,即 volatile 变量.用来确保将变量的更新操作通知到其他线程,保证了新值能立即同步到主内存,以及每次使用前立即从主内存刷新. ...
- [BZOJ2792][Poi2012]Well
2792: [Poi2012]Well Time Limit: 40 Sec Memory Limit: 64 MBSubmit: 137 Solved: 61[Submit][Status][D ...
- LINQ to Entities 和LINQ to Objects 的区别
本文资料来源:http://www.codeproject.com/Articles/246861/LINQ-to-Entities-Basic-Concepts-and-Features) LINQ ...
- BZOJ4491: 我也不知道题目名字是什么
Description 给定一个序列A[i],每次询问l,r,求[l,r]内最长子串,使得该子串为不上升子串或不下降子串 Input 第一行n,表示A数组有多少元素接下来一行为n个整数A[i]接下来一 ...
- shell判断文件或者文件夹是否存在
#!/bin/sh myPath="/var/log/httpd/" myFile="/var /log/httpd/access.log" #这里的-x 参数 ...
- Thinkphp中自己组合的数据怎样使用框架的分页
做项目有时候,需要自己处理组合数据,不是直接从表中提取出来的.不能按照手册得方法分页显示数据.这时候就得想办法,正好看到他人的方法.地址为:http://www.thinkphp.cn/code/27 ...
- HTML5初学总结
基本标签的使用 <!doctype html> <!--这是HTML5的申明,大小写都可以--> <html> <head> <title> ...
- 使用xtrbackup 热备MySQL数据库 以及恢复和自动删除脚本
直接上脚本 热备(全备) #!/bin/bash user='root' passwd='123456' my_config='/etc/my.cnf' #mysql configure log=fu ...
- 一篇很全面的freemarker教程
以下内容全部是网上收集: FreeMarker的模板文件并不比HTML页面复杂多少,FreeMarker模板文件主要由如下4个部分组成:1,文本:直接输出的部分2,注释:<#-- ... --& ...