Lua by Ong Hean Kuan

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    1 Favorite

    Lua by Ong Hean Kuan - Presentation Transcript

    1. Extending C/C++ with Lua 5.1 Ong Hean Kuan Unified Communications Email: mysurface@gmail.com
    2. Who Am I ?
      • Software Engineer in Unified Communication
      • Telecommunication services solution provider
      • Linux c/c++
      • Open Source Advocate
      • Blogs:
        • Linux by Examples http://linux.byexamples.com
        • C/C++ by Examples http://cc.byexamples.com
    3. What is Lua?
      • Programming language
      • Scripting language
      • Extensible embedded programming language.
      • Created by Roberto Ierusalimschy (Assoc Prof PUC-Rio)
      • Open Source, Lua 5.0 onwards are under MIT license.
      • Fast, Lightweight and simple
    4. Ivan's brain was written in Lua
      • Crazy Ivan was the robot that won the RoboCup 2000 in Denmark. Crazy Ivan has a "brain" that uses Lua for scripting language. All AI and logic is done in Lua. The brain is running on a Motorola Coldfire 5206e processor, and Lua is modified to use only int's as the Coldfire has no FPU.
    5. Applications
      • Wireshark, Ettercap, Nmap, Snort
      • Used as in-game levels, AI, and as a Rules Engine for game logic, game such as: Homeworld 2, Ragnarok Online, SimCity 4, World of Warcraft.
      • Fusion Developer 2, Adobe Photoshop lightroom, lighttpd.
      • Cisco uses Lua to implement Dynamic Access Policies within the Adaptive Security Appliance.
      • Etc, checkout wikipedia.org.
    6. Fast and Lightweight?
    7. Performance ~ Benchmarking
      • http://shootout.alioth.debian.org/gp4/benchmark.php
    8. Performance ~ Benchmarking 2
    9. The Language
      • Flexible data structure: Table
      • Table can be Array, Structure, Dictionary.
      • Object Oriented Programming
      • Global variables across the scripts
      • Standard lib such as io, math, file, string
    10. Types and Values
      • Nil
      • Boolean
      • Numbers
      • String
      • Table
      • Functions
      • Userdata and Coroutines
      • Use type() to check:
      • print(type(a))
    11. Table
      • Array or List
        • a = { 1,2,3,4,5,6}; print(a[1])
        • A = { [0]=1,2,3,4,5}; print(A[0])
      • Dictionary
        • d = {i=3,j=4,k=5}; print(d["i"])
        • d = {i=3,j=4,k=5}; print(d.i)
      • Elements in table can be anything, even a table or function, it can be construct to as an object.
        • Me = { say=print }; Me.say(“hi”)
    12. Statement
      • if then else
      • while
      • for ( Numeric, generic)
      • repeat
      • break
    13. If then else
      • if type(a)== “table” then io.write(“a is table ”) else if type(a)==”number” then io.write(“a is number ”) end
    14. while
      • a = { 1,2,3,4,5,6} i = 1 while a[i] do print(a[i]) i = i + 1 end
    15. for
      • for a=1,10,2 do print(a) end
      • a = { 'a','b','c','d','e' } for index,value in pairs(a) do print(index.." = "..value) end
    16. C API
      • Lua Stack
      • Accessing Lua global variables
      • Calling C from Lua
      • Calling Lua from C
    17. Lua Stack
      • All value passing through Lua Stack
      • Last IN First OUT
      • Push and Pop
    18. Simple c++ calling lua script extern "C" { #include "lua.h" #include "lualib.h" #include "lauxlib.h" } int main() { int s=0; lua_State *L = lua_open(); // load the libs luaL_openlibs(L); //run a Lua scrip here luaL_dofile(L,"foo.lua"); printf(" I am done with Lua in C++. "); lua_close(L); return 0; } -- foo.lua io.write(“Happy Hacking with Lua ”) g++ -o simple{,.cc} -llua -ldl
    19. Accessing Lua global variables int width=0,height=0; lua_State *L = lua_open(); luaL_openlibs(L); if (luaL_loadfile(L, "config.lua") || lua_pcall(L, 0, 0, 0)) printf("error: %s", lua_tostring(L, -1)); lua_getglobal(L, "width"); lua_getglobal(L, "height"); if (!lua_isnumber(L, -2)) { printf ("`width' should be a number "); return -1; } if (!lua_isnumber(L, -1)) { printf("`height' should be a number "); return -1; } width = (int)lua_tonumber(L, -2); height = (int)lua_tonumber(L, -1); printf("width: %d height: %d ", width, height); lua_close(L); return 0; -- config.lua width = 10 height = 5
    20. Calling c from Lua int L_MSleep(lua_State* l) { int milisec=0; struct timespec req={0}; time_t sec; milisec=luaL_optint(l,1,0); if (milisec==0) return 0; sec=(int)(milisec/1000); milisec=milisec-(sec*1000); req.tv_sec=sec; req.tv_nsec=milisec*1000000L; while(nanosleep(&req,&req)==-1) continue; return 1; } int main() { const static struct luaL_reg misc [] = { {"msleep", &L_MSleep}, {NULL,NULL} //must! }; lua_State *L = lua_open(); luaL_openlibs(L); //open your lib luaL_openlib(L, "misc", misc, 0); if (luaL_loadfile(L, "callc.lua") || lua_pcall(L, 0, 0, 0)) printf("error: %s", lua_tostring(L, -1)); lua_close(L); return 0; } -- callc.lua for i=1,9,1 do io.write(string.format("[%d] Hello ",i)) misc.msleep(1000) -- sleep 1 sec end
    21. Calling Lua from c int main() { double z; lua_State *L = lua_open(); luaL_openlibs(L); if (luaL_loadfile(L, "last.lua") || lua_pcall(L, 0, 0, 0)) { printf("error: %s", lua_tostring(L, -1)); return -1; } lua_getglobal(L, "f"); lua_pushnumber(L, 2); /* push 1st argument */ lua_pushnumber(L, 3); /* push 2nd argument */ /* do the call (2 arguments, 1 result) */ if (lua_pcall(L, 2, 1, 0) != 0) { printf("error running function `f': %s ",lua_tostring(L, -1)); return -1; } /* retrieve result */ if (!lua_isnumber(L, -1)) { printf("function `f' must return a number "); return -1; } z = lua_tonumber(L, -1); printf("Result: %f ",z); lua_pop(L, 1); lua_close(L); return 0; } -- last.lua function f (x, y) return (x^2 * math.sin(y))/(1 - x) end
    22. References
      • Official Web
        • http://www.lua.org/
      • Lua 5.1 Online Reference
        • http://www.lua.org/manual/5.1/
      • Programming in Lua
        • http://www.lua.org/pil/
      • Lua Community
        • http://lua-users.org/
    23. Thank you ;)
    SlideShare Zeitgeist 2009

    + fossmyfossmy Nominate

    custom

    489 views, 1 favs, 0 embeds more stats

    Presented at the MyOSS March 2009 Meetup

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 489
      • 489 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 1
    • Downloads 4
    Most viewed embeds

    more

    All embeds

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories

    Tags