luat_sysp.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include "luat_base.h"
  2. #include "luat_fs.h"
  3. #define LUAT_LOG_TAG "sysp"
  4. #include "luat_log.h"
  5. extern lua_State *L;
  6. int luat_main_call(void);
  7. /**
  8. * 这里的luat被当成库, 首先执行luat_sysp_init完成基础的初始化, 然后每隔tick时间, 调用一次luat_sysp_loop
  9. * require "sys"
  10. *
  11. * ... 用户代码 ....
  12. *
  13. * -- 注意, 没有sys.run()
  14. */
  15. int luat_sysp_init(void) {
  16. LLOGI("LuatOS@%s %s, Build: " __DATE__ " " __TIME__, luat_os_bsp(), LUAT_VERSION);
  17. // 1. 初始化文件系统
  18. luat_fs_init();
  19. return luat_main_call();
  20. // 没有重启, 没有退出
  21. }
  22. static void l_message (const char *pname, const char *msg) {
  23. if (pname) LLOGE("%s: ", pname);
  24. LLOGE("%s", msg);
  25. }
  26. static int safeRun(lua_State *L) {
  27. //LLOGD("CALL C safeRun\n");
  28. lua_settop(L, 0); // 清空堆栈
  29. lua_getglobal(L, "sys");
  30. if (lua_isnil(L, -1)) {
  31. return 0;
  32. }
  33. lua_getfield(L, -1, "safeRun");
  34. if (lua_isfunction(L, -1)) {
  35. //LLOGD("CALL safeRun\n");
  36. lua_call(L, 0, 0);
  37. lua_pushboolean(L, 1);
  38. return 1;
  39. }
  40. else {
  41. LLOGE("sys.safeRun NOT FOUND!!\n");
  42. }
  43. return 0;
  44. }
  45. int luat_sysp_loop(void) {
  46. lua_pushcfunction(L, &safeRun);
  47. int status = lua_pcall(L, 0, 1, 0); /* do the call */
  48. int result = lua_toboolean(L, -1);
  49. if (status != LUA_OK) {
  50. const char *msg = lua_tostring(L, -1);
  51. l_message("LUAT", msg);
  52. lua_pop(L, 1); /* remove message */
  53. }
  54. //LLOGD("status %d result %d", status, result);
  55. if (!result)
  56. return LUA_ERRERR;
  57. return status;
  58. }