stack.cpp 837 B

12345678910111213141516171819202122232425262728293031323334
  1. #include <lua.hpp>
  2. #include <luwra.hpp>
  3. #include <iostream>
  4. using namespace luwra;
  5. static
  6. lua_Integer sum3(lua_Integer a, lua_Integer b, lua_Integer c) {
  7. return a + b + c;
  8. }
  9. int main() {
  10. lua_State* state = luaL_newstate();
  11. luaL_openlibs(state);
  12. // Build stack
  13. lua_pushinteger(state, 13);
  14. lua_pushinteger(state, 37);
  15. lua_pushinteger(state, 42);
  16. // Each value can be retrieved individually.
  17. std::cout << "a = " << Value<lua_Integer>::read(state, 1) << std::endl;
  18. std::cout << "b = " << Value<lua_Integer>::read(state, 2) << std::endl;
  19. std::cout << "c = " << Value<lua_Integer>::read(state, 3) << std::endl;
  20. // ... which is a little cumbersome. Instead we might apply a fitting function to our stack.
  21. std::cout << "a + b + c = "
  22. << apply(state, sum3)
  23. << std::endl;
  24. lua_close(state);
  25. return 0;
  26. }