rotable2.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #ifndef ROTABLE2_H_
  2. #define ROTABLE2_H_
  3. #include "lua.h"
  4. typedef struct rotable_Reg_Value {
  5. int type;
  6. union
  7. {
  8. lua_CFunction func;
  9. lua_Number numvalue;
  10. const char* strvalue;
  11. lua_Integer intvalue;
  12. void* ptr;
  13. } value;
  14. }rotable_Reg_Value_t;
  15. /* exactly the same as luaL_Reg, but since we are on small embedded
  16. * microcontrollers, we don't assume that you have `lauxlib.h`
  17. * available in your build! */
  18. typedef struct rotable_Reg2 {
  19. char const* name;
  20. rotable_Reg_Value_t value;
  21. } rotable_Reg_t;
  22. #define ROREG_FUNC(fvalue) {.type=LUA_TFUNCTION, .value={.func=fvalue}}
  23. #define ROREG_NUM(fvalue) {.type=LUA_TNUMBER, .value={.numvalue=fvalue}}
  24. #define ROREG_INT(fvalue) {.type=LUA_TINTEGER, .value={.intvalue=fvalue}}
  25. #define ROREG_STR(fvalue) {.type=LUA_TSTRING, .value={.strvalue=fvalue}}
  26. #define ROREG_PTR(fvalue) {.type=LUA_TLIGHTUSERDATA, .value={.ptr=fvalue}}
  27. #ifndef ROTABLE_EXPORT
  28. # define ROTABLE_EXPORT extern
  29. #endif
  30. /* compatible with `luaL_newlib()`, and works with `luaL_Reg` *and*
  31. * `rotable_Reg` arrays (in case you don't use `lauxlib.h`) */
  32. ROTABLE_EXPORT void rotable2_newlib( lua_State* L, void const* reg );
  33. /* Since userdatas can not be used as `__index` meta methods directly
  34. * this function creates a C closure that looks up keys in a given
  35. * `rotable_Reg` array. */
  36. ROTABLE_EXPORT void rotable2_newidx( lua_State* L, void const* reg );
  37. #endif /* ROTABLE_H_ */