SlideShare a Scribd company logo
Extending C/C++ with Lua 5.1 Ong Hean Kuan Unified Communications Email: mysurface@gmail.com
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
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
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.
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.
Fast and Lightweight?
Performance ~ Benchmarking http://shootout.alioth.debian.org/gp4/benchmark.php
Performance ~ Benchmarking 2
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
Types and Values Nil Boolean Numbers String Table Functions Userdata and Coroutines Use type() to check: print(type(a))
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”)
Statement if then else while for ( Numeric, generic) repeat break
If then else if type(a)== “table” then   io.write(“a is table\n”) else if type(a)==”number” then   io.write(“a is number\n”) end
while a = { 1,2,3,4,5,6} i = 1 while a[i] do   print(a[i])   i = i + 1 end
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
C API Lua Stack Accessing Lua global variables Calling C from Lua Calling Lua from C
Lua Stack All value passing through Lua Stack Last IN First OUT Push and Pop
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("\nI am done with Lua in C++.\n"); lua_close(L); return 0; } -- foo.lua io.write(“Happy Hacking with Lua\n”) g++ -o simple{,.cc} -llua -ldl
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\n"); return -1; } if (!lua_isnumber(L, -1)) { printf("`height' should be a number\n"); return -1; } width = (int)lua_tonumber(L, -2); height = (int)lua_tonumber(L, -1); printf("width: %d\nheight: %d\n", width, height); lua_close(L); return 0; -- config.lua width = 10 height = 5
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\n",i)) misc.msleep(1000) -- sleep 1 sec end
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\n",lua_tostring(L, -1)); return -1; } /* retrieve result */ if (!lua_isnumber(L, -1)) { printf("function `f' must return a number\n"); return -1; } z = lua_tonumber(L, -1); printf("Result: %f\n",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
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/
Thank you ;)

More Related Content

PDF
Python programming Workshop SITTTR - Kalamassery
PDF
Python basic
PPTX
Python ppt
PDF
Introduction to Python Programming | InsideAIML
PPTX
Python Workshop - Learn Python the Hard Way
PPTX
Python Basics
PPT
Python ppt
Python programming Workshop SITTTR - Kalamassery
Python basic
Python ppt
Introduction to Python Programming | InsideAIML
Python Workshop - Learn Python the Hard Way
Python Basics
Python ppt

What's hot (20)

PPTX
Shell programming 2
PPTX
Shell programming 2
PPTX
Rust Intro
PPTX
Introduction to Python Programming
PPTX
Basic Python Programming: Part 01 and Part 02
PPSX
Symbolic mathematics
PPTX
Python Basics
PPT
python.ppt
PPTX
Python in 30 minutes!
PPTX
PPTX
Python programing
PPTX
C Programming Homework Help
PPT
Introduction to Python - Part Two
PPT
Introduction to Python
ODP
Introduction to Python - Training for Kids
PDF
Chapter 1 Basic Programming (Python Programming Lecture)
PPTX
PPT on Data Science Using Python
ODP
An Intro to Python in 30 minutes
PPTX
Python advance
PDF
E6
Shell programming 2
Shell programming 2
Rust Intro
Introduction to Python Programming
Basic Python Programming: Part 01 and Part 02
Symbolic mathematics
Python Basics
python.ppt
Python in 30 minutes!
Python programing
C Programming Homework Help
Introduction to Python - Part Two
Introduction to Python
Introduction to Python - Training for Kids
Chapter 1 Basic Programming (Python Programming Lecture)
PPT on Data Science Using Python
An Intro to Python in 30 minutes
Python advance
E6
Ad

Viewers also liked (7)

PDF
High Level Application Scripting With EFL and LuaJIT
ODP
igdshare 110220: LuaJIT intro
PDF
What's New in LuaRocks - Lua Workshop 2014 - Hisham Muhammad
PPTX
Api Design Anti-Patterns
PDF
Hands on lua
PDF
Roll Your Own API Management Platform with nginx and Lua
PPT
Accounting concepts conventions & principles
High Level Application Scripting With EFL and LuaJIT
igdshare 110220: LuaJIT intro
What's New in LuaRocks - Lua Workshop 2014 - Hisham Muhammad
Api Design Anti-Patterns
Hands on lua
Roll Your Own API Management Platform with nginx and Lua
Accounting concepts conventions & principles
Ad

Similar to Lua by Ong Hean Kuan (20)

PDF
20120710 - Lua_C
PDF
Lua زبان برنامه نویسی
PPTX
Use of Lua in Lab Devices
PDF
Lua and its Ecosystem
PPT
A brief history of Lua - Roberto Ierusalimschy (PUC Rio)
PPTX
Script up your application with Lua! -- RyanE -- OpenWest 2014
PDF
Lua as a business logic language in high load application
PDF
Programming in Lua 3rd Edition Roberto Ierusalimschy
PPTX
Lua. The Splendors and Miseries of Game Scripting
PDF
Programming in Lua 2nd Edition Roberto Ierusalimschy
PDF
Why Lua?
PDF
Get started with Lua programming
PDF
Introduction to lua
PPTX
Lua Express Guide
PDF
Programming in Lua 3rd Edition Roberto Ierusalimschy
PDF
Programming in Lua 3rd Edition Roberto Ierusalimschy
PPTX
Lua patient zero bret mogilefsky (scea)
PDF
Programming in Lua 2nd Edition Roberto Ierusalimschy
PPTX
Lua Programming Language.pptx
PPTX
Lua and fable jonathan shaw (lionhead)
20120710 - Lua_C
Lua زبان برنامه نویسی
Use of Lua in Lab Devices
Lua and its Ecosystem
A brief history of Lua - Roberto Ierusalimschy (PUC Rio)
Script up your application with Lua! -- RyanE -- OpenWest 2014
Lua as a business logic language in high load application
Programming in Lua 3rd Edition Roberto Ierusalimschy
Lua. The Splendors and Miseries of Game Scripting
Programming in Lua 2nd Edition Roberto Ierusalimschy
Why Lua?
Get started with Lua programming
Introduction to lua
Lua Express Guide
Programming in Lua 3rd Edition Roberto Ierusalimschy
Programming in Lua 3rd Edition Roberto Ierusalimschy
Lua patient zero bret mogilefsky (scea)
Programming in Lua 2nd Edition Roberto Ierusalimschy
Lua Programming Language.pptx
Lua and fable jonathan shaw (lionhead)

Recently uploaded (20)

PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Electronic commerce courselecture one. Pdf
PDF
Modernizing your data center with Dell and AMD
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Big Data Technologies - Introduction.pptx
PDF
cuic standard and advanced reporting.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Approach and Philosophy of On baking technology
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Encapsulation theory and applications.pdf
PDF
KodekX | Application Modernization Development
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Diabetes mellitus diagnosis method based random forest with bat algorithm
Understanding_Digital_Forensics_Presentation.pptx
Electronic commerce courselecture one. Pdf
Modernizing your data center with Dell and AMD
NewMind AI Weekly Chronicles - August'25 Week I
Review of recent advances in non-invasive hemoglobin estimation
Big Data Technologies - Introduction.pptx
cuic standard and advanced reporting.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Approach and Philosophy of On baking technology
Network Security Unit 5.pdf for BCA BBA.
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
MYSQL Presentation for SQL database connectivity
Unlocking AI with Model Context Protocol (MCP)
Encapsulation theory and applications.pdf
KodekX | Application Modernization Development
Reach Out and Touch Someone: Haptics and Empathic Computing
Agricultural_Statistics_at_a_Glance_2022_0.pdf

Lua by Ong Hean Kuan

  • 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.
  • 7. Performance ~ Benchmarking http://shootout.alioth.debian.org/gp4/benchmark.php
  • 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\n”) else if type(a)==”number” then io.write(“a is number\n”) 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("\nI am done with Lua in C++.\n"); lua_close(L); return 0; } -- foo.lua io.write(“Happy Hacking with Lua\n”) 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\n"); return -1; } if (!lua_isnumber(L, -1)) { printf("`height' should be a number\n"); return -1; } width = (int)lua_tonumber(L, -2); height = (int)lua_tonumber(L, -1); printf("width: %d\nheight: %d\n", 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\n",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\n",lua_tostring(L, -1)); return -1; } /* retrieve result */ if (!lua_isnumber(L, -1)) { printf("function `f' must return a number\n"); return -1; } z = lua_tonumber(L, -1); printf("Result: %f\n",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/