luat_base.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #include "luat_base.h"
  2. #include "rotable.h"
  3. #include "rotable2.h"
  4. #include "luat_msgbus.h"
  5. #include "luat_mem.h"
  6. #define LUAT_LOG_TAG "main"
  7. #include "luat_log.h"
  8. #include "rotable.h"
  9. void luat_newlib(lua_State* l, const rotable_Reg* reg) {
  10. #ifdef LUAT_CONF_DISABLE_ROTABLE
  11. luaL_newlibtable(l,reg);
  12. int i;
  13. int nup = 0;
  14. luaL_checkstack(l, nup, "too many upvalues");
  15. for (; reg->name != NULL; reg++) { /* fill the table with given functions */
  16. for (i = 0; i < nup; i++) /* copy upvalues to the top */
  17. lua_pushvalue(l, -nup);
  18. if (reg->func)
  19. lua_pushcclosure(l, reg->func, nup); /* closure with those upvalues */
  20. else
  21. lua_pushinteger(l, reg->value);
  22. lua_setfield(l, -(nup + 2), reg->name);
  23. }
  24. lua_pop(l, nup); /* remove upvalues */
  25. #else
  26. rotable_newlib(l, reg);
  27. #endif
  28. }
  29. #include "rotable2.h"
  30. void luat_newlib2(lua_State* l, const rotable_Reg_t* reg) {
  31. #ifdef LUAT_CONF_DISABLE_ROTABLE
  32. luaL_newlibtable(l,reg);
  33. int i;
  34. int nup = 0;
  35. luaL_checkstack(l, nup, "too many upvalues");
  36. for (; reg->name != NULL; reg++) { /* fill the table with given functions */
  37. for (i = 0; i < nup; i++) /* copy upvalues to the top */
  38. lua_pushvalue(l, -nup);
  39. switch (reg->value.type) {
  40. case LUA_TFUNCTION:
  41. lua_pushcfunction( l, reg->value.value.func );
  42. break;
  43. case LUA_TINTEGER:
  44. lua_pushinteger( l, reg->value.value.intvalue );
  45. break;
  46. case LUA_TSTRING:
  47. lua_pushstring( l, reg->value.value.strvalue );
  48. break;
  49. case LUA_TNUMBER:
  50. lua_pushnumber( l, reg->value.value.numvalue );
  51. break;
  52. case LUA_TLIGHTUSERDATA:
  53. lua_pushlightuserdata(l, reg->value.value.ptr);
  54. break;
  55. default:
  56. lua_pushinteger( l, 0 );
  57. break;
  58. }
  59. lua_setfield(l, -(nup + 2), reg->name);
  60. }
  61. lua_pop(l, nup); /* remove upvalues */
  62. #else
  63. rotable2_newlib(l, reg);
  64. #endif
  65. }
  66. void luat_os_print_heapinfo(const char* tag) {
  67. size_t total; size_t used; size_t max_used;
  68. luat_meminfo_luavm(&total, &used, &max_used);
  69. LLOGD("%s luavm %ld %ld %ld", tag, total, used, max_used);
  70. luat_meminfo_sys(&total, &used, &max_used);
  71. LLOGD("%s sys %ld %ld %ld", tag, total, used, max_used);
  72. #ifdef LUAT_USE_PSRAM
  73. luat_meminfo_opt_sys(LUAT_HEAP_PSRAM, &total, &used, &max_used);
  74. LLOGD("%s psram %ld %ld %ld", tag, total, used, max_used);
  75. #endif
  76. }
  77. //唯一c等待接口使用的id
  78. uint64_t c_wait_id = 0;
  79. //c等待接口
  80. //获取一个消息等待的唯一id
  81. //并向栈中推入一个可等待对象
  82. //返回值为0时,表示用户没有启用sys库,无法使用该功能
  83. //[-0, +1, –]
  84. uint64_t luat_pushcwait(lua_State *L) {
  85. if(lua_getglobal(L, "sys_cw") != LUA_TFUNCTION)
  86. {
  87. LLOGE("sys lib not found!");
  88. return 0;
  89. }
  90. c_wait_id++;
  91. // char* topic = (char*)luat_heap_malloc(1 + sizeof(uint64_t));
  92. char topic[10] = {0};
  93. topic[0] = 0x01;//前缀用一个不可见字符,防止和用户用的重复
  94. memcpy(topic + 1,&c_wait_id,sizeof(uint64_t));
  95. lua_pushlstring(L,topic,1 + sizeof(uint64_t));
  96. // luat_heap_free(topic);
  97. lua_call(L,1,1);
  98. return c_wait_id;
  99. }
  100. //c等待接口,直接向用户返回错误的对象
  101. //使用时推入需要返回的所有参数
  102. //该函数会向栈中推入一个可等待对象,但该对象会直接返回结果,无需等待
  103. //用户没有启用sys库,不会进行任何操作([-0, +0, –])
  104. //[-arg_num, +1, –]
  105. void luat_pushcwait_error(lua_State *L, int arg_num) {
  106. if(lua_getglobal(L, "sys_cw") != LUA_TFUNCTION)
  107. {
  108. LLOGE("sys lib not found!");
  109. return;
  110. }
  111. lua_pushnil(L);
  112. lua_rotate(L,-arg_num-2,2);
  113. lua_call(L,arg_num+1,1);
  114. }
  115. //c等待接口,对指定id进行回调响应
  116. //使用时推入需要返回的所有参数
  117. //调用时传入消息id和参数个数
  118. //该函数会调用sys_pub代为发送消息事件
  119. //用户没有启用sys库,不会进行任何操作([-0, +0, –]),并返回0
  120. //[-arg_num, +0, –] 成功返回1
  121. int luat_cbcwait(lua_State *L, uint64_t id, int arg_num) {
  122. if(lua_getglobal(L, "sys_pub") != LUA_TFUNCTION)
  123. return 0;
  124. char* topic = (char*)luat_heap_malloc(1 + sizeof(uint64_t));
  125. topic[0] = 0x01;
  126. memcpy(topic + 1,&id,sizeof(uint64_t));
  127. lua_pushlstring(L,topic,1 + sizeof(uint64_t));
  128. luat_heap_free(topic);
  129. lua_rotate(L,-arg_num-2,2);
  130. lua_call(L, arg_num + 1, 0);
  131. return 1;
  132. }
  133. /*
  134. @sys_pub sys
  135. 用于luatos内部的系统消息传递
  136. 以0x01为第一个字节开头
  137. @args 返回的数据
  138. @usage
  139. --此为系统内部使用的消息,请勿在外部使用
  140. */
  141. static int luat_cbcwait_cb(lua_State *L, void* ptr) {
  142. (void)ptr;
  143. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  144. if(lua_getglobal(L, "sys_pub") != LUA_TFUNCTION)
  145. return 0;
  146. char* topic = (char*)luat_heap_malloc(1 + sizeof(uint64_t));
  147. topic[0] = 0x01;
  148. memcpy(topic + 1,msg->ptr,sizeof(uint64_t));
  149. lua_pushlstring(L,topic,1 + sizeof(uint64_t));
  150. luat_heap_free(topic);
  151. luat_heap_free(msg->ptr);
  152. lua_call(L, 1, 0);
  153. return 0;
  154. }
  155. //c等待接口,无参数的回调,可不传入lua栈
  156. void luat_cbcwait_noarg(uint64_t id) {
  157. if(id == 0)
  158. return;
  159. rtos_msg_t msg = {0};
  160. msg.handler = luat_cbcwait_cb;
  161. uint64_t* idp = (uint64_t*)luat_heap_malloc(sizeof(uint64_t));
  162. memcpy(idp, &id, sizeof(uint64_t));
  163. msg.ptr = (void*)idp;
  164. luat_msgbus_put(&msg, 0);
  165. }