functions.cpp 987 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include <lua.hpp>
  2. #include <luwra.hpp>
  3. #include <iostream>
  4. using namespace luwra;
  5. static
  6. void my_function_1() {
  7. std::cout << "my_function_1()" << std::endl;
  8. }
  9. static
  10. Integer my_function_2() {
  11. return 1338;
  12. }
  13. static
  14. Integer my_function_3(Integer a, Integer b) {
  15. return a + b;
  16. }
  17. int main() {
  18. lua_State* state = luaL_newstate();
  19. // Register 'my_function_1'
  20. auto wrapped_1 = WrapFunction<void(), my_function_1>;
  21. lua_pushcfunction(state, wrapped_1);
  22. lua_setglobal(state, "my_function_1");
  23. // Register 'my_function_2'
  24. auto wrapped_2 = WrapFunction<Integer(), my_function_2>;
  25. lua_pushcfunction(state, wrapped_2);
  26. lua_setglobal(state, "my_function_2");
  27. // Register 'my_function_3'
  28. auto wrapped_3 = WrapFunction<Integer(Integer, Integer), my_function_3>;
  29. lua_pushcfunction(state, wrapped_3);
  30. lua_setglobal(state, "my_function_3");
  31. // Invoke the attached script
  32. luaL_loadfile(state, "functions.lua");
  33. lua_call(state, 0, LUA_MULTRET);
  34. lua_close(state);
  35. return 0;
  36. }