luat_airui_main.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include "luat_base.h"
  2. #include "luat_airui.h"
  3. #include "luat_malloc.h"
  4. #define LUAT_LOG_TAG "airui"
  5. #include "luat_log.h"
  6. static jsmn_parser parser;
  7. int luat_airui_load_buff(luat_airui_ctx_t** _ctx, int backend, const char* screen_name, const char* buff, size_t len) {
  8. int ret;
  9. luat_airui_ctx_t *ctx = luat_heap_malloc(sizeof(luat_airui_ctx_t));
  10. if (ctx == NULL) {
  11. LLOGW("out of memory when malloc luat_airui_ctx_t");
  12. return -1;
  13. }
  14. // 首先, 初始化处理器
  15. jsmn_init(&parser);
  16. // 然后,先扫描一遍, 获取总的token数量, 若处理失败,会返回负数
  17. ret = jsmn_parse(&parser, buff, len, NULL, 0);
  18. if (ret <= 0) {
  19. LLOGW("invaild json ret %d", ret);
  20. return -2;
  21. }
  22. LLOGD("found json token count %d", ret);
  23. // 再然后, 分配内存
  24. jsmntok_t *tok = luat_heap_malloc(sizeof(jsmntok_t) * ret);
  25. if (tok == NULL) {
  26. luat_heap_free(ctx);
  27. LLOGW("out of memory when malloc jsmntok_t");
  28. return -3;
  29. }
  30. // 真正的解析, 肯定不会出错
  31. jsmn_init(&parser);
  32. ret = jsmn_parse(&parser, buff, len, tok, ret);
  33. if (ret <= 0) {
  34. // 还是防御一下吧
  35. luat_heap_free(tok);
  36. luat_heap_free(ctx);
  37. LLOGW("invaild json ret %d", ret);
  38. return -2;
  39. }
  40. // 现在解析完成了, 开始生成的组件树
  41. LLOGD("json parse complete, begin components jobs ...");
  42. ctx->data = buff;
  43. ctx->screen_name = screen_name;
  44. ret = luat_airui_load_components(ctx, tok, ret);
  45. LLOGD("json parse complete, end components jobs, ret %d", ret);
  46. luat_heap_free(tok);
  47. if (ret == 0) {
  48. *_ctx = ctx;
  49. }
  50. else {
  51. luat_heap_free(ctx);
  52. }
  53. return ret;
  54. }
  55. int luat_airui_load_file(luat_airui_ctx_t** ctx, int backend, const char* screen_name, const char* path) {
  56. return -1;
  57. }
  58. int luat_airui_get(luat_airui_ctx_t* ctx, const char* key) {
  59. return -1;
  60. }