lexamplelib.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * example of adding lua external library
  3. */
  4. #include "lua.h"
  5. #include "lauxlib.h"
  6. #include "lexlibs.h"
  7. #define VERSION 1
  8. int example_hello(lua_State *L)
  9. {
  10. rt_kprintf("Hello, Lua on RT-Thead!\n");
  11. return 0;
  12. }
  13. int example_print(lua_State *L)
  14. {
  15. int n = lua_gettop(L);
  16. int i;
  17. for (i=1; i<=n; i++)
  18. {
  19. if (i>1)
  20. rt_kprintf("\t");
  21. if (lua_isstring(L,i))
  22. rt_kprintf("%s",lua_tostring(L,i));
  23. else if (lua_isnumber(L, i))
  24. rt_kprintf("%d",lua_tointeger(L,i));
  25. else if (lua_isnil(L,i))
  26. rt_kprintf("%s","nil");
  27. else if (lua_isboolean(L,i))
  28. rt_kprintf("%s",lua_toboolean(L,i) ? "true" : "false");
  29. else
  30. rt_kprintf("%s:%p",luaL_typename(L,i),lua_topointer(L,i));
  31. }
  32. rt_kprintf("\n");
  33. return 0;
  34. }
  35. #define MIN_OPT_LEVEL 2
  36. #include "lrodefs.h"
  37. const LUA_REG_TYPE example_map[] =
  38. {
  39. {LSTRKEY("hello"), LFUNCVAL(example_hello)},
  40. {LSTRKEY("print"), LFUNCVAL(example_print)},
  41. #if LUA_OPTIMIZE_MEMORY > 0
  42. {LSTRKEY("version"), LNUMVAL(VERSION)},
  43. #endif
  44. {LNILKEY, LNILVAL}
  45. };
  46. /**
  47. * Open exmaple library
  48. */
  49. LUALIB_API int luaopen_example(lua_State *L)
  50. {
  51. #if LUA_OPTIMIZE_MEMORY > 0
  52. return 0;
  53. #else
  54. luaL_register(L, EXLIB_EXAMPLE, example_map);
  55. lua_pushnumber(L, VERSION);
  56. lua_setfield(L, -2, "version");
  57. return 1;
  58. #endif
  59. }