types.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include <luwra.hpp>
  2. #include <iostream>
  3. namespace luwra {
  4. // You may add custom specializations of luwra::Value in order to retrieve them from the stack.
  5. template <>
  6. struct Value<char> {
  7. /**
  8. * Retrieve the value at position `n`.
  9. */
  10. static inline
  11. char read(lua_State* state, int n) {
  12. auto str = Value<std::string>::read(state, n);
  13. if (str.length() < 1) {
  14. luaL_argerror(state, n, "Given empty string instead of character");
  15. }
  16. return str[0];
  17. }
  18. /**
  19. * push the value onto the stack.
  20. */
  21. static inline
  22. size_t push(lua_State* state, char val) {
  23. if (val == 0)
  24. return 0;
  25. lua_pushlstring(state, &val, 1);
  26. return 1;
  27. }
  28. };
  29. }
  30. static
  31. void read_chars(char a, char b) {
  32. std::cout << "Got '" << a << b << "'" << std::endl;
  33. }
  34. int main() {
  35. luwra::StateWrapper state;
  36. state.loadStandardLibrary();
  37. // Build stack
  38. luwra::push(state, 'H');
  39. luwra::push(state, 'i');
  40. // apply function to stack values
  41. luwra::apply(state, read_chars);
  42. lua_pop(state, 2);
  43. // Build stack again
  44. luwra::push(state, 'Y', 'o');
  45. luwra::apply(state, read_chars);
  46. return 0;
  47. }