Lua by Ong Hean Kuan

  • Upload
    fossmy

  • View
    2.425

  • Download
    0

Embed Size (px)

Citation preview

Extending C/C++ with Lua 5.1

Ong Hean KuanUnified CommunicationsEmail: [email protected]

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

-4 or 1-3 or 2-2 or 3-1 or 4

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.luaio.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.luawidth = 10height = 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.luafor i=1,9,1 do io.write(string.format("[%d] Hello\n",i)) misc.msleep(1000) -- sleep 1 secend

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.luafunction 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 ;)