CLuaHandler.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #include "stdafx.h"
  2. #include "lua.h"
  3. #include "lualib.h"
  4. #include "lauxlib.h"
  5. #include <luabind/luabind.hpp>
  6. #include <luabind/function.hpp>
  7. #include <luabind/class.hpp>
  8. #include "CLuaHandler.h"
  9. void piszpowitanie2(std::string i) //simple global function for testing
  10. {
  11. std::cout<<"powitanie2zc++. Liczba dnia to " << i;
  12. }
  13. CLuaHandler::CLuaHandler()
  14. {
  15. }
  16. CLuaHandler::~CLuaHandler()
  17. {
  18. }
  19. void CLuaHandler::test()
  20. {
  21. int iErr = 0;
  22. lua_State *lua = lua_open (); // Open Lua
  23. LUA_OPEN_LIB(lua, luaopen_base);
  24. LUA_OPEN_LIB(lua, luaopen_io);
  25. if ((iErr = luaL_loadfile (lua, "test.lua")) == 0)
  26. {
  27. // Call main...
  28. if ((iErr = lua_pcall (lua, 0, LUA_MULTRET, 0)) == 0)
  29. {
  30. luabind::open(lua);
  31. luabind::module(lua)
  32. [
  33. luabind::def("powitanie",&piszpowitanie2)
  34. ];
  35. //int ret = luabind::call_function<int>(lua, "helloWorld2");
  36. lua_pushstring (lua, "helloWorld2");
  37. lua_gettable (lua, LUA_GLOBALSINDEX);
  38. lua_pcall (lua, 0, 0, 0);
  39. // Push the function name onto the stack
  40. lua_pushstring (lua, "helloWorld");
  41. lua_gettable (lua, LUA_GLOBALSINDEX);
  42. lua_pcall (lua, 0, 0, 0);
  43. }
  44. }
  45. lua_close (lua);
  46. }