SlideShare a Scribd company logo
lua介绍                                                  页码,1/10




                                                  介
                                               lua介绍
             Author: 罗进 <luojinlj@gmail.com>
             Version: 1


使用lua提高开发效率




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




file:///C:/lua.htm                                      2011/5/12
lua介绍                            页码,2/10




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


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




file:///C:/lua.htm                2011/5/12
lua介绍                                                    页码,3/10




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,
    • 文档多,第三方库多,源代码不好




file:///C:/lua.htm                                        2011/5/12
lua介绍                                                                                        页码,4/10




lua库,c扩展
   库 扩
   • lua for windows:第三方库非官方合集
           • alien:同python的ctypes
           • penlight:自称battery include,借鉴python

   • luarocks:同python的easy_install(PEAK)

   • 自己扩展

       L=luaL_newstate(); luaL_openlibs(L);        //   init
       luaL_dofile("")                             //   readfile and compile
       lua_getglobal(L, func_name)                 //   find func in global table
       lua_pushinteger()                           //   parameter push to stack for called
       lua_pcall(L, narg, nresult, 0)              //   call
       lua_tonumber()                              //   result




file:///C:/lua.htm                                                                            2011/5/12
lua介绍                                    页码,5/10




          开发介
iphone-wax开发介绍
          开发
为什么用lua写iphone
         写
  • objc不如lua好用
  • 轻松垃圾收集(java),非引用计数(python)
  • 更少的代码,你懂得
  • cocoa无缝集成,NSDictionary等的自动转换,自定义控件
  • 更强的HTTP功能
  • 闭包
  • 正则




file:///C:/lua.htm                        2011/5/12
lua介绍                                                                         页码,6/10




      代
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




file:///C:/lua.htm                                                             2011/5/12
lua介绍                                                                         页码,7/10




   代
wax代码流 main.m
       wax_start(char *initScript, lua_CFunction extension, ...);
           wax_setup();
               luaopen_wax_class(L);       //lua访问cocoa
                   Finds 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}
   • 继承 类 ,            功能 数


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



file:///C:/lua.htm                                                             2011/5/12
lua介绍                                                                              页码,8/10




迅雷插 开发介
迅雷插件开发介绍
       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插件开发介绍 图




file:///C:/lua.htm                                                                  2011/5/12
lua介绍                                                                                                           页码,9/10




         插 开发介
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



file:///C:/lua.htm                                                                                               2011/5/12
lua介绍                页码,10/10




FAQ
FAQ




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

More Related Content

PDF
Lucene 全文检索实践
PDF
自动生成 Makefile 的全过程详解!
ODP
GNU Autoconf / Automake #1
PDF
test
PPTX
MySQL aio
PPT
PDF
六步教你学会简单Rmi
PDF
Spring 2.0 技術手冊第九章 - API 封裝
Lucene 全文检索实践
自动生成 Makefile 的全过程详解!
GNU Autoconf / Automake #1
test
MySQL aio
六步教你学会简单Rmi
Spring 2.0 技術手冊第九章 - API 封裝

What's hot (20)

PDF
自由軟體鑄造場_20111023_Subversion版本控制系統之操作_曾義峰(ant)
PDF
啟動 Laravel 與環境設定
PDF
Spring 2.0 技術手冊導讀
PPTX
Jetty服务器架构及调优.v2 2011-5
ODP
GNU Autoconf / Automake #4
PDF
COSCUP 2016 Laravel 部署工作坊 - 部署指南
PDF
Phpconf 2011 introduction_to_codeigniter
PDF
PHP 語法基礎與物件導向
PDF
Openshift by mtchang
DOCX
Spring4.x + hibernate4.x_配置详解
PPTX
lua & ngx_lua 的介绍与应用
PDF
Oracle试题Exam Adminv1.1
PPT
Mysql展示功能与源码对应
PDF
Linux chapt3
PDF
凌波微步:wagon + VS Code 的輕功哲學
PDF
前端工程師一定要知道的 Docker 虛擬化容器技巧
PDF
View 與 Blade 樣板引擎
PDF
开源Pass平台flynn功能简介
PDF
[Modern Web 2016] 讓你的 PHP 開發流程再次潮起來
PPTX
1, shell intro
自由軟體鑄造場_20111023_Subversion版本控制系統之操作_曾義峰(ant)
啟動 Laravel 與環境設定
Spring 2.0 技術手冊導讀
Jetty服务器架构及调优.v2 2011-5
GNU Autoconf / Automake #4
COSCUP 2016 Laravel 部署工作坊 - 部署指南
Phpconf 2011 introduction_to_codeigniter
PHP 語法基礎與物件導向
Openshift by mtchang
Spring4.x + hibernate4.x_配置详解
lua & ngx_lua 的介绍与应用
Oracle试题Exam Adminv1.1
Mysql展示功能与源码对应
Linux chapt3
凌波微步:wagon + VS Code 的輕功哲學
前端工程師一定要知道的 Docker 虛擬化容器技巧
View 與 Blade 樣板引擎
开源Pass平台flynn功能简介
[Modern Web 2016] 讓你的 PHP 開發流程再次潮起來
1, shell intro
Ad

Viewers also liked (6)

PDF
How to configure an environment to cross-compile applications for beagleboard-xM
PDF
Company profile
PDF
Scrabble
PDF
Logging develop
PDF
The Rise of the Internet of Things
PDF
Casing3d opengl
How to configure an environment to cross-compile applications for beagleboard-xM
Company profile
Scrabble
Logging develop
The Rise of the Internet of Things
Casing3d opengl
Ad

Similar to 使用Lua提高开发效率 (7)

PDF
iOS中Lua脚本的应用
PPT
Lua 语言介绍
PDF
未标题 1
PDF
Ruby基础培训
PDF
Network and Multitasking
PPTX
Corona 初探 lua 語言,玩跨平台(iOS & android) 行動裝置開發工具
PDF
Ruby rails分享
iOS中Lua脚本的应用
Lua 语言介绍
未标题 1
Ruby基础培训
Network and Multitasking
Corona 初探 lua 語言,玩跨平台(iOS & android) 行動裝置開發工具
Ruby rails分享

More from gowell (6)

PDF
Kernel init
PDF
Logging introduce
PDF
Script meta
PDF
Script binding
PDF
Pytables
PDF
从动态说开去
Kernel init
Logging introduce
Script meta
Script binding
Pytables
从动态说开去

使用Lua提高开发效率

  • 1. lua介绍 页码,1/10 介 lua介绍 Author: 罗进 <luojinlj@gmail.com> Version: 1 使用lua提高开发效率 议程 • 缘起 • lua特点 • lua语法 • lua库,c扩展 • iphone-wax开发介绍 • 迅雷插件开发介绍 • wireshark插件开发介绍 file:///C:/lua.htm 2011/5/12
  • 2. lua介绍 页码,2/10 缘起 • 工作中使用c++ • 新产品需求不定 • 时间浪费在编译、部署、执行上 • --lua可以在应用程序运行期修改代码 lua特点 特 • 目的:嵌入宿主程序执行,只是个核。 • 快:基于寄存器的虚拟机,与宿主程序在栈上交互 • 小:164k,可裁剪更小。python2100k • 省:自动垃圾收集、弱类型、单一数据结构,扩展c简单 • 稳定:5.1版2006发布 file:///C:/lua.htm 2011/5/12
  • 3. lua介绍 页码,3/10 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, • 文档多,第三方库多,源代码不好 file:///C:/lua.htm 2011/5/12
  • 4. lua介绍 页码,4/10 lua库,c扩展 库 扩 • lua for windows:第三方库非官方合集 • alien:同python的ctypes • penlight:自称battery include,借鉴python • luarocks:同python的easy_install(PEAK) • 自己扩展 L=luaL_newstate(); luaL_openlibs(L); // init luaL_dofile("") // readfile and compile lua_getglobal(L, func_name) // find func in global table lua_pushinteger() // parameter push to stack for called lua_pcall(L, narg, nresult, 0) // call lua_tonumber() // result file:///C:/lua.htm 2011/5/12
  • 5. lua介绍 页码,5/10 开发介 iphone-wax开发介绍 开发 为什么用lua写iphone 写 • objc不如lua好用 • 轻松垃圾收集(java),非引用计数(python) • 更少的代码,你懂得 • cocoa无缝集成,NSDictionary等的自动转换,自定义控件 • 更强的HTTP功能 • 闭包 • 正则 file:///C:/lua.htm 2011/5/12
  • 6. lua介绍 页码,6/10 代 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 file:///C:/lua.htm 2011/5/12
  • 7. lua介绍 页码,7/10 代 wax代码流 main.m wax_start(char *initScript, lua_CFunction extension, ...); wax_setup(); luaopen_wax_class(L); //lua访问cocoa Finds 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} • 继承 类 , 功能 数 迅雷插 开发介 迅雷插件开发介绍 图 file:///C:/lua.htm 2011/5/12
  • 8. lua介绍 页码,8/10 迅雷插 开发介 迅雷插件开发介绍 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插件开发介绍 图 file:///C:/lua.htm 2011/5/12
  • 9. lua介绍 页码,9/10 插 开发介 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 file:///C:/lua.htm 2011/5/12
  • 10. lua介绍 页码,10/10 FAQ FAQ file:///C:/lua.htm 2011/5/12