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 libname equal 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_register creates 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.

lua语言三则特性的更多相关文章

  1. 理解 Lua 的那些坑爹特性

    按:最近看到了依云的文章,一方面,为Lua被人误解而感到十分难过,另一方面,也为我的好友, 依云没有能够体会到Lua的绝妙和优雅之处而感到很遗憾,因此我写了这篇文章,逐条款地说明了 依云理解中出现的一 ...

  2. 【quick-cocos2d-x】Lua 语言基础

    版权声明:本文为博主原创文章,转载请注明出处. 使用quick-x开发游戏有两年时间了,quick-x是cocos2d-Lua的一个豪华升级版的框架,使用Lua编程.相比于C++,lua的开发确实快速 ...

  3. 编译并使用Lua语言

    Lua是一个小巧的脚本语言,该语言设计的目的是为了嵌入应用程序中,从而为应用程序提供灵活的扩展和定制功能. 可扩展性.Lua的扩展性非常卓越,可以通过Lua代码或C代码扩展,很多功能可以通过外部库来扩 ...

  4. lua 语言笔记

    Lua语言基础汇总(1) -- 类型与值 基础介绍 Lua是一种动态类型的语言.在语言中没有类型定义的语法,每个值都带有其自身的类型信息.在Lua中有8种基本类型,分别是: nil(空)类型 bool ...

  5. Lua语言中文手册 转载自网络

    Programming in LuaCopyright ® 2005, Translation Team, www.luachina.net Programming in LuaProgramming ...

  6. 【转载】【游戏开发】在Lua中实现面向对象特性——模拟类、继承、多态

    [游戏开发]在Lua中实现面向对象特性——模拟类.继承.多态   阅读目录 一.简介 二.前提知识 三.Lua中实现类.继承.多态 四.总结 回到顶部 一.简介 Lua是一门非常强大.非常灵活的脚本语 ...

  7. lua视频教程三套高清新

    目录 1. 下载地址 2. 某网校 Lua 经典教程 3. lua脚本语言零基础开发教程19课全 4. LUA完整视频+Cocos2d-x项目实战 1. 下载地址 https://www.cnblog ...

  8. lua语言自学知识点----Lua与.Net相互调用

    知识点: LuaInterface作用是用来完成Lua与C#的相互调用. LuaInterface核心库:1.luainterface.dll 用于C#读取lua(放在bin目录同级) 2.luane ...

  9. java多线程中的三种特性

    java多线程中的三种特性 原子性(Atomicity) 原子性是指在一个操作中就是cpu不可以在中途暂停然后再调度,既不被中断操作,要不执行完成,要不就不执行. 如果一个操作时原子性的,那么多线程并 ...

随机推荐

  1. ACM: 限时训练题解-Heavy Coins-枚举子集-暴力枚举

    Heavy Coins   Bahosain has a lot of coins in his pocket. These coins are really heavy, so he always ...

  2. 【BZOJ】3670: [Noi2014]动物园

    http://www.lydsy.com/JudgeOnline/problem.php?id=3670 题意:太水了= = #include <bits/stdc++.h> using ...

  3. 【BZOJ2438】 [中山市选2011]杀人游戏 tarjan强连通分量+缩点

    Description 一位冷血的杀手潜入 Na-wiat,并假装成平民.警察希望能在 N 个人里面,查出谁是杀手. 警察能够对每一个人进行查证,假如查证的对象是平民,他会告诉警察,他认识的人, 谁是 ...

  4. 【Java】RuleSource约束常用方法整理

    1-常用约束规则RuleSource的设置方法   例如: addRules(new Rules(ProgramFeeItem.class){ protected void initRules() { ...

  5. Js中的window.parent ,window.top,window.self 详解

    在应用有frameset或者iframe的页面时,parent是父窗口,top是最顶级父窗口(有的窗口中套了好几层frameset或者iframe),self是当前窗口, opener是用open方法 ...

  6. 在Windows7下启动MongoDB服务的解决方案

    1:首先去官网下载程序,我用的是1.4.3版本,地址: http://downloads.mongodb.org/win32/mongodb-win32-i386-1.4.3.zip 2:创建一个DB ...

  7. android-ListView控件的使用

    一.深刻理解ListView 1.职责:将数据填充到布局.响应用户操作 2.ListView的实现需要:布局.数据源.适配器 3.常见适配器: ArrayAdapter<T>  用来绑定一 ...

  8. Jfinal中手动提交/回滚 事物

    在Jfinal中有个Tx类为事物声明类 在方法或controller上面加@Before({Tx.class})即可,可是这样并不能满足有的业务场景 下面是今天写的手动提交的事物处理方法,希望对大家有 ...

  9. php随笔(一)

    之前的开发一直用的都是Thinkphp框架,对原生的php很不了解,近日打算把以前的项目拿一个出来用原生php再重写一次,顺便再把TP框架拆开好好分析分析. 之前的android开发虽说对面向对象的思 ...

  10. HDU1978 记忆化搜索

    How many ways Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...