lua & c++
DESCRIPTION
Slides Integração com Lua e C++ Vinicius JarinaTRANSCRIPT
Integrando Lua & C++Vinicius Jarina ([email protected])
@viniciusjarina
Saturday, September 17, 2011
Overview linguagem Lua
Vantagens de utilizar Lua
Entendendo melhor o Lua
Introdução a API C
Chamando funções Lua do C
Chamando funções C do Lua
Usando modelo OO
Utilizando geradores de código (toLua++)
Saturday, September 17, 2011
Linguagem leve e rápida
Portável (ANSI C)
Facilmente embarcável
Licença livre (MIT)
Projeto ativo e crescendo
Lua
0
0.5
1
1.5
2
20032006
20092011/Q2
Normalized fraction of total hits (%)
Saturday, September 17, 2011
Alguns exemplos de projetos que usam Lua
SimCity / The Sims
World of Warcraft
Adobe Lightroom
CryENGINE
Lua
Saturday, September 17, 2011
Vantagens
Mas afinal para que utilizar uma linguagem script na minha aplicação?
Isolamento
Abstração
Extensão e personalização
Saturday, September 17, 2011
Primeiros PassosExemplo código Lua:local count = 10-- comentário
g = 2*count print("count = ", count, " g = ", g)
http://www.lua.org/demo.html
Saturday, September 17, 2011
Tipos de dadoLua suporta 8 tipos básico de dado: nil, boolean, number, string, userdata, function, thread, table
print(type(x)) -- nil (variáveis nascem nil)x = "farandula"print(type(x)) -- stringx = 10print(type(x)) -- numberx = printx(type(x)) -- function
Saturday, September 17, 2011
Tabelas
Tabelas são arrays associativos, podem ser indexados por qualquer valor, exceto nil.
t = {} -- cria uma tabela, associa uma ref. a tt2 = { x = 20, y = "foo" }
t["bar"] = "value" -- sintáxe dicionáriot.campo = 10 -- sintáxe registro
print(t.bar) -- valueprint(t["campo"]) -- 10print(t2.x, t2["y"])-- 20 foo
Saturday, September 17, 2011
FunçõesDefinindo funções:
function soma(a, b)return a + b
end
soma = function(a, b) return a + bend
function func(a, b) return b, a, 10end
x, y, z = func(15, 20) -- x = 20, y = 15 , z = 10p, q, r, s = func(2,1) -- p = 1,q = 2,r = 10, s = nil
Retornando múltiplos resultados:
Saturday, September 17, 2011
+FunçõesFunções anônimas:
function inverso(f) return function(x) return 1/(f(x)) endend
function square(x) return x*x end
f = inverso(square)
print(f(2), f(3)) -- 0.25 0.11111111111111
Exemplo:
Saturday, September 17, 2011
ClosuresExemplo de closure (usando up-values):
function fibbo_gen() local i = 0 local j = 1 return function() local k = i + j i = j j = k return k endend
f1 = fibbo_gen()print(f1()) -- 1print(f1()) -- 2print(f1()) -- 3print(f1()) -- 5
Exemplo:
Saturday, September 17, 2011
MetatabelasPermitem alterar comportamento através de metametodos ex: __tostring, __index, __add, __eq, etc...
t = {}mt = { __index = function(table, key) return "fixo" end }
setmetatable(t, mt)
print(t[1]) -- fixo (não encontrou 1, invoca __index)t[1] = "casa"print(t[1]) -- casa
Exemplo:
Saturday, September 17, 2011
ObjetosUsando tabela com cara de objeto:
t = { }t.func = function (a, ...) print("func:", a, ...) endt.membro = 10function t:getmembro() return self.membro end-- t.getmembro = function(self) return self.membro end
t.func(10) -- func:! 10t:func(11) -- func:! table: 0x256a3f0! 11t.func(t, 12) -- func:! table: 0x256a3f0! 12
print(t:getmembro()) -- 10
Exemplo:
Saturday, September 17, 2011
MódulosDefinindo um módulo:
module (..., package.seeall) -- size.lua function new (x, y) return {x = x, y = y}end
function area(s) return s.x * s.yend
require ‘size’
local s1 = size.new(10,20)
print(size.area(s1)) -- 200
Usando módulo:
Saturday, September 17, 2011
Compilação DinâmicaCompilando uma string:
i = 10 -- global if = loadstring(“i = i + 10; print(i)”)g = function() i = i + 10; print(i) end
f() -- 20
Compilando uma arquivo Lua:
-- file_foo.luafunction foo() print(“inside foo”) end
f = loadfile(“file_foo.lua”) -- compilou foo mas não executouprint(foo) -- nil, pois foo ainda nao existef() -- definiu foofoo() -- inside foo
Saturday, September 17, 2011
Tratando ErrosUsando pcall e error
i = 101status, err = pcall( function() -- pcall (try) i = i + 1 if(i > 100) then error(“i maior que 100”) -- error (throw) end print(“i = “,i ) -- i será sempre menor que 100 end)if not status then -- (catch, se pcall false, houve erro) print(err) -- i maior que 100end
Saturday, September 17, 2011
Overview linguagem Lua
Vantagens de utilizar Lua
Entendendo melhor o Lua
Introdução a API C
Chamando funções Lua do C
Chamando funções C do Lua
Utilizando geradores de código (toLua++)
Saturday, September 17, 2011
API CFunções API Lua lua_* e luaL_*
#include “lua.h” /* header principal do Lua */#include “lauxlib.h” /* header lib auxiliar do lua (luaL) */#include “lualib.h” /* header para usar std libs do lua */
int main() { lua_State * L = luaL_newstate(); /* */ luaL_openlibs(L); /* carrega libs std (string, table, etc*/ const char * szScript = “ i = 0 ; function test(a) \n“ “ return 2*a + 1 \n end \n“ “ print(test(i)) \n” int nErr = luaL_loadstring(L,szScript) || lua_pcall(L, 0, 0, 0); if(nErr) { printf(lua_tostring(L, -1)); lua_pop(L, 1); } lua_close(L); return 0;}
Saturday, September 17, 2011
PilhaEmpilhando e desempilhando valores
lua_pushboolean(L, 1); lua_pushnumber(L, 10); lua_pushnil(L); lua_pushstring(L, "hello");
lua_pushvalue(L, -4); lua_replace(L, 3); lua_settop(L, 6);lua_remove(L, -3); lua_settop(L, -5);
true
10nil
“hello”
true
nilnil
1-1
2
-2
3
-3
4
-4
5
-5
6
5
-5
-6
Saturday, September 17, 2011
Chamando funções LuaChamando uma função global:
luaL_dofile(“file_modulo.lua”); // compila e executa o // arquivo file_modulo.lua double real = 3; double imag = 5; lua_getglobal(L,“modulo”); // empilha a global modulo lua_pushnumber(L, real); // empilha param 1 (real) lua_pushnumber(L, imag); // empilha param 2 (imag)
lua_pcall(L, 2, 1, 0); // chama modulo(2 entradas, 1 saída)
double mod = lua_tonumber(L, -1); // mod == 5
lua_pop(L, 1);
-- file_modulo.luafunction modulo(real, imag) return math.sqrt(real*real + imag*imag)end
Saturday, September 17, 2011
Chamando funções CRegistrando função C
static int pow_l(lua_State *L) { double a = luaL_checknumber (L, 1); double b = luaL_checknumber (L, 2); lua_pushnumber (L, pow (a, b)); return 1;}static const struct luaL_Reg mylib [] = { {"pow", pow_l}, {NULL, NULL}};
int luaopen_mylib (lua_State *L) { luaL_register (L, "mylib", mylib); return 1;}
print(“chamando mylib.pow”, mylib.pow(3))-- chamando mylib.pow 9
Saturday, September 17, 2011
Usando toLua++Exportando uma classe:
class car { // tolua_export int m_speed; public:! ! //tolua_begin car(); ~ car(); void set_speed(int speed); int get_speed();}; // tolua_end
#>toluapp -o tolua_car.cpp -H tolua_car.h -n car car.pkg
// arquivo: car.pkg$cfile "car.h"
-- cria um novo carro, e seta velocidadelocal c1 = car:new_local("McQueen")c1:set_speed(10)print("Carro: " .. c1:name() .. " velocidade:" .. c1:speed());-- Carro: McQueen velocidade: 10
Saturday, September 17, 2011
DEMOSaturday, September 17, 2011
Dúvidas?
Saturday, September 17, 2011
Fimhttp://lua-users.org/wiki/Programming in Lua, 2Nd Editionhttp://www.lua.org/docs.html
Saturday, September 17, 2011