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
libname
equal 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_register
creates 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不可以在中途暂停然后再调度,既不被中断操作,要不执行完成,要不就不执行. 如果一个操作时原子性的,那么多线程并 ...
随机推荐
- Android在智能终端领域的关键技术专题讲座(成都站)
Android系统引领了终端智能化的浪潮,在民用.公 共及工业等诸多领域得到了广泛的应用,涉及手持终端.电视.汽车导航.工业控制等,在云计算.设备智能化等方面表现卓越.Android也凭借着自身的优 ...
- docker 报错Failed to start Docker Storage Setup. 的处理基本都是容器满了
:: localhost docker-storage-setup: Volume group extents): required. Apr :: localhost systemd: docker ...
- OBject copy 和retain区别
@interface Person : NSObject //retian : release 旧值,retain 新值 @property(nonatomic,retain) Book *book; ...
- C/C++:[2]enum-枚举量声明、定义和使用
C/C++:[2]enum-枚举量声明.定义和使用 转自:http://jingyan.baidu.com/article/e75aca85526c1b142edac6d9.html 众所周知,C/C ...
- Node.js 手册查询-1-核心模块方法
Node.js 学习手册 标签(空格分隔): node.js 模块 核心模块 核心模块是被编译成二进制代码,引用的时候只需require表示符即可 os 系统基本信息 os模块可提供操作系统的一些基本 ...
- 【Go语言】I/O专题
本文目录 1.bytes包:字节切片.Buffer和Reader 1_1.字节切片处理函数 1_1_1.基本处理函数 1_1_2.字节切片比较函数 1_1_3.字节切片前后缀检查函数 1_1_4.字节 ...
- [LintCode] Maximum Gap 求最大间距
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- Point Grey FlyCapture 实例汇总
Example Language Description AsyncTriggerEx C++ Demonstrates some of the basic asynchronous trigger ...
- Oracle三大经典表连接适用情况
1.1环境准备 1.2 Nested Loops Join 从上面的试验来看,nested loop jion基本上是没有限制的,可以支持所有的运算. 1.3 Hash Join 1.4 Merge ...
- Learn ZYNC (4)
最近整理出一些适合学习zed的实例(所有的例程都基于Vivado2013.4开发环境) (1)关于zed双核的测试案例: 官方链接:地址1.11.standalone,地址1.12.linux 修改源 ...