使用lua提高开发效率

10
lua介绍 Author: 罗进 <[email protected]> Version: 1 使用lua提高开发效率 议程 缘起 •lua特点 •lua语法 •lua库,c扩展 • iphone-wax开发介绍 迅雷插件开发介绍 • wireshark插件开发介绍 页码,1/10 lua介绍 2011/5/12 file:///C:/lua.htm

Upload: gowell

Post on 01-Jul-2015

3.042 views

Category:

Technology


4 download

TRANSCRIPT

Page 1: 使用Lua提高开发效率

lua介介介介绍绍绍绍

Author: 罗进 <[email protected]>

Version: 1

使用lua提高开发效率

议议议议程程程程

•缘起• lua特点• lua语法• lua库,c扩展• iphone-wax开发介绍•迅雷插件开发介绍• wireshark插件开发介绍

页码,1/10lua介绍

2011/5/12file:///C:/lua.htm

Page 2: 使用Lua提高开发效率

缘缘缘缘起起起起

•工作中使用c++•新产品需求不定•时间浪费在编译、部署、执行上• --lua可以在应用程序运行期修改代码

lua特特特特点点点点

•目的:嵌入宿主程序执行,只是个核。•快:基于寄存器的虚拟机,与宿主程序在栈上交互•小:164k,可裁剪更小。python2100k•省:自动垃圾收集、弱类型、单一数据结构,扩展c简单•稳定:5.1版2006发布

页码,2/10lua介绍

2011/5/12file:///C:/lua.htm

Page 3: 使用Lua提高开发效率

lua语语语语法:法:法:法:与与与与python区区区区别别别别

lua

• local,if then end, for do end,建议2个空格缩进•迭代器少,只有一个tabel数据结构,强大的闭包•继承使用prototype,即metatable中的__index(),用table模拟•文档少,第三方库少,源代码好,与c结合好,调试方便

python

• global,if :,for :,统一缩进•生成式[d for d in data],listdicttuple...,够用的lambda,•文档多,第三方库多,源代码不好

页码,3/10lua介绍

2011/5/12file:///C:/lua.htm

Page 4: 使用Lua提高开发效率

lua库库库库,,,,c扩扩扩扩展展展展

• lua for windows:第三方库非官方合集• alien:同python的ctypes

• penlight:自称battery include,借鉴python

• luarocks:同python的easy_install(PEAK)

•自己扩展

L=luaL_newstate(); luaL_openlibs(L); // initluaL_dofile("") // readfile and compilelua_getglobal(L, func_name) // find func in global tablelua_pushinteger() // parameter push to stack for calledlua_pcall(L, narg, nresult, 0) // calllua_tonumber() // result

页码,4/10lua介绍

2011/5/12file:///C:/lua.htm

Page 5: 使用Lua提高开发效率

iphone-wax开发开发开发开发介介介介绍绍绍绍

为为为为什什什什么么么么用用用用lua写写写写iphone• objc不如lua好用•轻松垃圾收集(java),非引用计数(python)

•更少的代码,你懂得• cocoa无缝集成,NSDictionary等的自动转换,自定义控件•更强的HTTP功能•闭包•正则

页码,5/10lua介绍

2011/5/12file:///C:/lua.htm

Page 6: 使用Lua提高开发效率

iphone代代代代码码码码流流流流

UIApplication->

UIAppDelegate->

applicationDidFinishlaunching()

// add controllers for callbacks

UITableViewController *tableViewController;

UIWindow *window;

tableViewController = [[UITableViewController alloc]];

[window addSubview:tableViewController.view];

[window make key and visible]

objc class目录:app由多个controller+view组成,含.h .m文件lua script目录:appDelegate.lua,viewController.lua

页码,6/10lua介绍

2011/5/12file:///C:/lua.htm

Page 7: 使用Lua提高开发效率

wax代代代代码码码码流流流流 main.m

wax_start(char *initScript, lua_CFunction extension, ...);wax_setup();

luaopen_wax_class(L); //lua访问cocoaFinds an ObjC class, //__index()Creates a new ObjC class//__call()

luaopen_wax_instance(L); //Cocoa 对象luaopen_wax_struct(L); //cocoa struct

Load extentions, stdlib //lua的json、xml、http等库luaL_dostring(L, initScript) //加载用户代码

wax_startWithServer(); //交互命令控制显示

wax_end(); //lua_close(wax_currentLuaState());

• waxClass{"TwitterTableViewController", UITableViewController}

•继承 类 , 功能 数

迅雷迅雷迅雷迅雷插插插插件件件件开发开发开发开发介介介介绍绍绍绍图图图图

页码,7/10lua介绍

2011/5/12file:///C:/lua.htm

Page 8: 使用Lua提高开发效率

迅雷迅雷迅雷迅雷插插插插件件件件开发开发开发开发介介介介绍绍绍绍

addin.cfg //应用的相关信息XAR/onload.lua // main()

function OnMyIconClick(self)local tabCtrl = XLGetGlobal("xunlei.UIControlProvider").GetMainWndTab()local index = tabCtrl:GetItemPosByTitle("MyApp")

tabCtrl:SelectItem(index)end

local addin_mgr = XLGetGlobal("xunlei.AddinManagerHelper")addin_mgr.RegisterService("MyApp", "我的应用", "icon", "", OnMyIconClick, nil)

XAR/layout/Control.xml //界面XAR/layout/Control.cml.lua //逻辑

wireshark插插插插件件件件开发开发开发开发介介介介绍绍绍绍图图图图

页码,8/10lua介绍

2011/5/12file:///C:/lua.htm

Page 9: 使用Lua提高开发效率

wireshark插插插插件件件件开发开发开发开发介介介介绍绍绍绍

dofile("myProto.lua") // init.lua, wireshake调用

local protocal = Proto("myProto","myProto xxx") //新协议local field1 = ProtoField.bytes("myProto.identifier","Identifier") //协议字段protocal.fields = { field1, ..., } //协议构造

function protocal.dissector(buf,pkt,root) //回调:解析协议后,将数据加入指定字段local t = root:add(protocal,buf)t:add(field1,v_identifier)...

end

Ref

• lua http://lua-users.org/wiki/SampleCode

• internal http://simohayha.iteye.com/category/84720

• wax https://github.com/probablycorey/wax

•迅雷 http://xldoc.xl7.xunlei.com/0000000024/00000000240000700004.html

• wireshark http://yoursunny.com/study/IS409/ScoreBoard.htm]]

• S5 http://meyerweb.com/eric/tools/s5/

• rst http://docutils.sourceforge.net/rst.html

• rst2s5 http://docutils.sourceforge.net/docs/user/slide-shows.html

• s5定 http://blog.zoomquiet.org/pyblosxom/utility/py4str/StructuredText/rst2s5-usage-2010-09-18-23-23.html

页码,9/10lua介绍

2011/5/12file:///C:/lua.htm

Page 10: 使用Lua提高开发效率

FAQ

FAQ

页码,10/10lua介绍

2011/5/12file:///C:/lua.htm