frozenmod.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2015 Paul Sokolovsky
  7. * Copyright (c) 2016 Damien P. George
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #include <string.h>
  28. #include <stdint.h>
  29. #include "py/lexer.h"
  30. #include "py/frozenmod.h"
  31. #if MICROPY_MODULE_FROZEN_STR
  32. #ifndef MICROPY_MODULE_FROZEN_LEXER
  33. #define MICROPY_MODULE_FROZEN_LEXER mp_lexer_new_from_str_len
  34. #else
  35. mp_lexer_t *MICROPY_MODULE_FROZEN_LEXER(qstr src_name, const char *str, mp_uint_t len, mp_uint_t free_len);
  36. #endif
  37. extern const char mp_frozen_str_names[];
  38. extern const uint32_t mp_frozen_str_sizes[];
  39. extern const char mp_frozen_str_content[];
  40. // On input, *len contains size of name, on output - size of content
  41. const char *mp_find_frozen_str(const char *str, size_t *len) {
  42. const char *name = mp_frozen_str_names;
  43. size_t offset = 0;
  44. for (int i = 0; *name != 0; i++) {
  45. size_t l = strlen(name);
  46. if (l == *len && !memcmp(str, name, l)) {
  47. *len = mp_frozen_str_sizes[i];
  48. return mp_frozen_str_content + offset;
  49. }
  50. name += l + 1;
  51. offset += mp_frozen_str_sizes[i] + 1;
  52. }
  53. return NULL;
  54. }
  55. STATIC mp_lexer_t *mp_lexer_frozen_str(const char *str, size_t len) {
  56. size_t name_len = len;
  57. const char *content = mp_find_frozen_str(str, &len);
  58. if (content == NULL) {
  59. return NULL;
  60. }
  61. qstr source = qstr_from_strn(str, name_len);
  62. mp_lexer_t *lex = MICROPY_MODULE_FROZEN_LEXER(source, content, len, 0);
  63. return lex;
  64. }
  65. #endif
  66. #if MICROPY_MODULE_FROZEN_MPY
  67. #include "py/emitglue.h"
  68. extern const char mp_frozen_mpy_names[];
  69. extern const mp_raw_code_t *const mp_frozen_mpy_content[];
  70. STATIC const mp_raw_code_t *mp_find_frozen_mpy(const char *str, size_t len) {
  71. const char *name = mp_frozen_mpy_names;
  72. for (size_t i = 0; *name != 0; i++) {
  73. size_t l = strlen(name);
  74. if (l == len && !memcmp(str, name, l)) {
  75. return mp_frozen_mpy_content[i];
  76. }
  77. name += l + 1;
  78. }
  79. return NULL;
  80. }
  81. #endif
  82. #if MICROPY_MODULE_FROZEN
  83. STATIC mp_import_stat_t mp_frozen_stat_helper(const char *name, const char *str) {
  84. size_t len = strlen(str);
  85. for (int i = 0; *name != 0; i++) {
  86. size_t l = strlen(name);
  87. if (l >= len && !memcmp(str, name, len)) {
  88. if (name[len] == 0) {
  89. return MP_IMPORT_STAT_FILE;
  90. } else if (name[len] == '/') {
  91. return MP_IMPORT_STAT_DIR;
  92. }
  93. }
  94. name += l + 1;
  95. }
  96. return MP_IMPORT_STAT_NO_EXIST;
  97. }
  98. mp_import_stat_t mp_frozen_stat(const char *str) {
  99. mp_import_stat_t stat;
  100. #if MICROPY_MODULE_FROZEN_STR
  101. stat = mp_frozen_stat_helper(mp_frozen_str_names, str);
  102. if (stat != MP_IMPORT_STAT_NO_EXIST) {
  103. return stat;
  104. }
  105. #endif
  106. #if MICROPY_MODULE_FROZEN_MPY
  107. stat = mp_frozen_stat_helper(mp_frozen_mpy_names, str);
  108. if (stat != MP_IMPORT_STAT_NO_EXIST) {
  109. return stat;
  110. }
  111. #endif
  112. return MP_IMPORT_STAT_NO_EXIST;
  113. }
  114. int mp_find_frozen_module(const char *str, size_t len, void **data) {
  115. #if MICROPY_MODULE_FROZEN_STR
  116. mp_lexer_t *lex = mp_lexer_frozen_str(str, len);
  117. if (lex != NULL) {
  118. *data = lex;
  119. return MP_FROZEN_STR;
  120. }
  121. #endif
  122. #if MICROPY_MODULE_FROZEN_MPY
  123. const mp_raw_code_t *rc = mp_find_frozen_mpy(str, len);
  124. if (rc != NULL) {
  125. *data = (void*)rc;
  126. return MP_FROZEN_MPY;
  127. }
  128. #endif
  129. return MP_FROZEN_NONE;
  130. }
  131. #endif