25 Şubat 2010 Perşembe

Lua ve C++'ın Birlikte Kullanımıyla İlgili Kısa Bir Ders

Lua'nın kullanımı oldukça kolay. Bu derste C++ içerisinde Lua callbackleriyle çalışan bir host programı nasıl yazacağımızı göreceğiz.

Statik Lua kütüphaneleri C'de yazıldığı için bunları programımıza aşağıdaki gibi import ediyoruz:

extern "C" {
   #include "lua.h"
}

int main()
{
   lua_State *L = lua_open();
   lua_close(L);
   return 0;
}


Yukardaki örneği derleyebilmek için bazı sistemlerde lualib.h ve lauxlib.h kütüphanelerini koda dahil etmeniz gerekmekte.

extern "C" {
   #include "lualib.h"
   #include "lauxlib.h"
}


 
GNU g++ ile derleme ve bağlama işlemi şu şekildedir:
 
g++ host.cpp -o host -Ilua-5.0.2/include/ -Llua-5.0.2/lib/ -llua


lualib.h ve lauxlib.h kütüphanelerinin koda dahil edilmesi iyi çalışır bir host programının yazılması işini oldukça kolaylaştıracaktır.

#include <iostream>

extern "C" {
   #include "lua.h"
   #include "lualib.h"
   #include "lauxlib.h"
}

void report_errors(lua_State *L, int status)
{
   if ( status!=0 ) {
      std::cerr << "-- " << lua_tostring(L, -1) << std::endl;
      lua_pop(L, 1); // remove error message
   }
}

int main(int argc, char** argv)
{
   for ( int n=1; n
      const char* file = argv[n];
      lua_State *L = lua_open();
      luaopen_io(L); // provides io.*
      luaopen_base(L);
      luaopen_table(L);
      luaopen_string(L);
      luaopen_math(L);
      luaopen_loadlib(L);
     
      std::cerr << "-- Loading file: " << file << std::endl;
      int s = luaL_loadfile(L, file);

      if ( s==0 ) {
         // execute Lua program
         s = lua_pcall(L, 0, LUA_MULTRET, 0);
      }
     
      report_errors(L, s);
      lua_close(L);
     
      std::cerr << std::endl;
   }
  
   return 0;
}

 
Derleme ve bağlama işlemi:
 
g++ host.cpp -o host -Ilua-5.0.2/include/ -Llua-5.0.2/lib/ -llua -llualib



LUA Programlarının Çalıştırılması
Şimdi yaptığımız bu programı birkaç lua koduyla test edelim. Kullanacağımız kod lua dağıtımıyla birlikte gelen hello.lua kodu.

-- the first program in every language
io.write("Hello world, from ",_VERSION,"!\n")

Host isimli programımızla iki lua kodunun birlikte çalıştırılması:

[csl@eris:~/dev/lua/lua-5.0.2]$ ./host test/hello.lua test/printf.lua



-- Loading file: test/hello.lua


Hello world, from Lua 5.0.2!






-- Loading file: test/printf.lua


Hello csl from Lua 5.0.2 on Wed Mar 2 13:13:05 2005

 
Lua İçinden C Fonksiyonlarını Çağırma
Lua programları kendi hazırladığımız fonksiyonları çağırdığında oldukça ilgi çekici bir hal almakta. Aşağıdaki programda my_function() isminde bir fonksiyon tanımlıyoruz ve bunu lua_register() Lua fonksiyonuyla Lua'ya register ediyoruz. Fonksiyonumuz aldığı argümanları string olarak ekrana basmakta ve 123 integer değerini döndürmekte.
 
#include <iostream>


extern "C"{
   #include "lua.h"
   #include "lualib.h"
   #include "lauxlib.h"
}

int my_function(lua_State *L)
{
   int argc = lua_gettop(L);
   std::cerr << "-- my_function() called with " << argc << " arguments:" << std::endl;
  
   for ( int n=1; n<=argc; ++n ) {
      std::cerr << "-- argument " << n << ": " << lua_tostring(L, n) << std::endl;
   }
  
   lua_pushnumber(L, 123); // return value
  
   return 1; // number of return values
}


void report_errors(lua_State *L, int status)
{
   if ( status!=0 ) {
      std::cerr << "-- " << lua_tostring(L, -1) << std::endl;
      lua_pop(L, 1); // remove error message
   }
}


int main(int argc, char** argv)
{
   for ( int n=1; n<argc; ++n ) {
      const char* file = argv[n];
      lua_State *L = lua_open();
      luaopen_io(L); // provides io.*
      luaopen_base(L);
      luaopen_table(L);
      luaopen_string(L);
      luaopen_math(L);
      luaopen_loadlib(L);
     
      // make my_function() available to Lua programs
      lua_register(L, "my_function", my_function);


      std::cerr << "-- Loading file: " << file << std::endl;


      int s = luaL_loadfile(L, file);


      if ( s==0 ) {
         // execute Lua program
         s = lua_pcall(L, 0, LUA_MULTRET, 0);
      }


      report_errors(L, s);
      lua_close(L);


      std::cerr << std::endl;
   }
   return 0;
}


Şimdi hazırladığımız my_function() isimli fonksiyonu çağıracak ufak bir lua programı yazalım. ismi test.lua olsun.

io.write("Running ", _VERSION, "\n")
a = my_function(1, 2, 3, "abc", "def")

io.write("my_function() returned ", a, "\n")

 
Oluşturduğumuz host programla şimdi test.lua programımızı çalıştırıyoruz:
 
[csl@eris:~/dev/lua/lua-5.0.2]$ ./host test.lua



-- Loading file: test.lua


Running Lua 5.0.2


-- my_function() called with 5 arguments:


-- argument 1: 1


-- argument 2: 2


-- argument 3: 3


-- argument 4: abc


-- argument 5: def


my_function() returned 123

Hiç yorum yok: