Extending C/C++ with Lua 5.1 Ong Hean Kuan Unified Communications Email: mysurface@gmail.com
Who Am I ? <ul><li>Software Engineer in Unified Communication </li></ul><ul><li>Telecommunication services solution provid...
What is Lua? <ul><li>Programming language </li></ul><ul><li>Scripting language </li></ul><ul><li>Extensible embedded progr...
Ivan's brain was written in Lua <ul><li>Crazy Ivan was the robot that won the RoboCup 2000 in Denmark. Crazy Ivan has a &q...
Applications <ul><li>Wireshark, Ettercap, Nmap, Snort </li></ul><ul><li>Used as in-game levels, AI, and as a Rules Engine ...
Fast and Lightweight?
Performance ~ Benchmarking <ul><li>http://shootout.alioth.debian.org/gp4/benchmark.php </li></ul>
Performance ~ Benchmarking 2
The Language <ul><li>Flexible data structure:  Table </li></ul><ul><li>Table can be Array, Structure, Dictionary. </li></u...
Types and Values <ul><li>Nil </li></ul><ul><li>Boolean </li></ul><ul><li>Numbers </li></ul><ul><li>String </li></ul><ul><l...
Table <ul><li>Array or List </li></ul><ul><ul><li>a = { 1,2,3,4,5,6}; print(a[1])  </li></ul></ul><ul><ul><li>A = { [0]=1,...
Statement <ul><li>if then else </li></ul><ul><li>while </li></ul><ul><li>for ( Numeric, generic) </li></ul><ul><li>repeat ...
If then else <ul><li>if type(a)== “table” then   io.write(“a is table
”) else if type(a)==”number” then   io.write(“a is n...
while <ul><li>a = { 1,2,3,4,5,6} i = 1 while a[i] do   print(a[i])   i = i + 1 end </li></ul>
for <ul><li>for a=1,10,2 do   print(a) end  </li></ul><ul><li>a = { 'a','b','c','d','e' } for index,value in pairs(a) do  ...
C API <ul><li>Lua Stack </li></ul><ul><li>Accessing Lua global variables </li></ul><ul><li>Calling C from Lua </li></ul><u...
Lua Stack <ul><li>All value passing through Lua Stack </li></ul><ul><li>Last IN First OUT </li></ul><ul><li>Push and Pop <...
Simple c++ calling lua script extern &quot;C&quot; { #include &quot;lua.h&quot; #include &quot;lualib.h&quot; #include &qu...
Accessing Lua global variables int width=0,height=0; lua_State *L = lua_open(); luaL_openlibs(L); if (luaL_loadfile(L, &qu...
Calling c from Lua int L_MSleep(lua_State* l) { int milisec=0; struct timespec req={0}; time_t sec; milisec=luaL_optint(l,...
Calling Lua from c int main() { double z; lua_State *L = lua_open(); luaL_openlibs(L); if (luaL_loadfile(L, &quot;last.lua...
References <ul><li>Official Web </li></ul><ul><ul><li>http://www.lua.org/ </li></ul></ul><ul><li>Lua 5.1 Online Reference ...
Thank you ;)
Upcoming SlideShare
Loading in...5
×

Lua by Ong Hean Kuan

1,964

Published on

Presented at the MyOSS March 2009 Meetup

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,964
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
53
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Lua by Ong Hean Kuan

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

    Clipping is a handy way to collect important slides you want to go back to later.

×