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不可以在中途暂停然后再调度,既不被中断操作,要不执行完成,要不就不执行. 如果一个操作时原子性的,那么多线程并 ...
随机推荐
- ACM: FZU 2148 Moon Game - 海伦公式
FZU 2148 Moon Game Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64 ...
- 【CF】148D Bag of mice
http://codeforces.com/problemset/problem/148/D 题意:w个白b个黑,公主和龙轮流取,公主先取,等概率取到一个.当龙取完后,会等概率跳出一只.(0<= ...
- Tomcat_启动多个tomcat时,会报StandardServer.await: Invalid command '' received错误
解决方案如下:将tomcat下的server.xml文件中的端口有问题,修改规则按以下标准显示“http的端口修改为6000 to 6800之间,shutdown的端口修改为3000 to 3300之 ...
- Express 路由
路由 路由是指如何定义应用的端点(URIs)以及如何响应客户端的请求. 路由是由一个 URI.HTTP 请求(GET.POST等)和若干个句柄组成,它的结构如下: app.METHOD(path, [ ...
- Jenkins 2.7.3 LTS 发布
更新如下: Stop A/B testing of the remoting JNLP3 protocol due to the known issues. The protocol can be e ...
- Sortable Observable Collection in C#
Sorting outside the collection protected override void OnNavigatedTo(NavigationEventArgs e) { if (Se ...
- 如何用bat批处理编译swf项目
平时用FB等IDE编译多模块的游戏项目时,除了添加移除模块的操作很繁琐外,编译速度也非常之慢.而用bat来编译swf项目,速度非常快,稳定. 在此分享自己工作用的bat,每次运行会重新编译主模块Gam ...
- [LintCode] Reverse Pairs 翻转对
For an array A, if i < j, and A [i] > A [j], called (A [i], A [j]) is a reverse pair.return to ...
- Many2one类型的fields Compute得到的值 搜索
v8 默认情况下compute的值不存储于数据库中,在高级搜索中也不可以进行搜索 想要对这种类型的值搜索,需要在field的定义中添加search参数,在search的函数中编写搜索逻辑. 例子: r ...
- Lua数据结构
lua中的table不是一种简单的数据结构,它可以作为其他数据结构的基础,如:数组,记录,链表,队列等都可以用它来表示. 1.数组 在lua中,table的索引可以有很多种表示方式.如果用整数来表示t ...