frozenmod.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. * Copyright (c) 2021 Jim Mussared
  9. *
  10. * Permission is hereby granted, free of charge, to any person obtaining a copy
  11. * of this software and associated documentation files (the "Software"), to deal
  12. * in the Software without restriction, including without limitation the rights
  13. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. * copies of the Software, and to permit persons to whom the Software is
  15. * furnished to do so, subject to the following conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be included in
  18. * all copies or substantial portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  26. * THE SOFTWARE.
  27. */
  28. #include <string.h>
  29. #include <stdint.h>
  30. #include "py/lexer.h"
  31. #include "py/frozenmod.h"
  32. #if MICROPY_MODULE_FROZEN
  33. // Null-separated frozen file names. All string-type entries are listed first,
  34. // followed by mpy-type entries. Use mp_frozen_str_sizes to determine how
  35. // many string entries.
  36. extern const char mp_frozen_names[];
  37. #if MICROPY_MODULE_FROZEN_STR
  38. #ifndef MICROPY_MODULE_FROZEN_LEXER
  39. #define MICROPY_MODULE_FROZEN_LEXER mp_lexer_new_from_str_len
  40. #else
  41. mp_lexer_t *MICROPY_MODULE_FROZEN_LEXER(qstr src_name, const char *str, mp_uint_t len, mp_uint_t free_len);
  42. #endif
  43. // Size in bytes of each string entry, followed by a zero (terminator).
  44. extern const uint32_t mp_frozen_str_sizes[];
  45. // Null-separated string content.
  46. extern const char mp_frozen_str_content[];
  47. #endif // MICROPY_MODULE_FROZEN_STR
  48. #if MICROPY_MODULE_FROZEN_MPY
  49. #include "py/emitglue.h"
  50. extern const mp_frozen_module_t *const mp_frozen_mpy_content[];
  51. #endif // MICROPY_MODULE_FROZEN_MPY
  52. // Search for "str" as a frozen entry, returning the stat result
  53. // (no-exist/file/dir), as well as the type (none/str/mpy) and data.
  54. // frozen_type can be NULL if its value isn't needed (and then data is assumed to be NULL).
  55. mp_import_stat_t mp_find_frozen_module(const char *str, int *frozen_type, void **data) {
  56. size_t len = strlen(str);
  57. const char *name = mp_frozen_names;
  58. if (frozen_type != NULL) {
  59. *frozen_type = MP_FROZEN_NONE;
  60. }
  61. // Count the number of str lengths we have to find how many str entries.
  62. size_t num_str = 0;
  63. #if MICROPY_MODULE_FROZEN_STR && MICROPY_MODULE_FROZEN_MPY
  64. for (const uint32_t *s = mp_frozen_str_sizes; *s != 0; ++s) {
  65. ++num_str;
  66. }
  67. #endif
  68. for (size_t i = 0; *name != 0; i++) {
  69. size_t entry_len = strlen(name);
  70. if (entry_len >= len && memcmp(str, name, len) == 0) {
  71. // Query is a prefix of the current entry.
  72. if (entry_len == len) {
  73. // Exact match --> file.
  74. if (frozen_type != NULL) {
  75. #if MICROPY_MODULE_FROZEN_STR
  76. if (i < num_str) {
  77. *frozen_type = MP_FROZEN_STR;
  78. // Use the size table to figure out where this index starts.
  79. size_t offset = 0;
  80. for (size_t j = 0; j < i; ++j) {
  81. offset += mp_frozen_str_sizes[j] + 1;
  82. }
  83. size_t content_len = mp_frozen_str_sizes[i];
  84. const char *content = &mp_frozen_str_content[offset];
  85. // Note: str & len have been updated by find_frozen_entry to strip
  86. // the ".frozen/" prefix (to avoid this being a distinct qstr to
  87. // the original path QSTR in frozen_content.c).
  88. qstr source = qstr_from_strn(str, len);
  89. mp_lexer_t *lex = MICROPY_MODULE_FROZEN_LEXER(source, content, content_len, 0);
  90. *data = lex;
  91. }
  92. #endif
  93. #if MICROPY_MODULE_FROZEN_MPY
  94. if (i >= num_str) {
  95. *frozen_type = MP_FROZEN_MPY;
  96. // Load the corresponding index as a raw_code, taking
  97. // into account any string entries to offset by.
  98. *data = (void *)mp_frozen_mpy_content[i - num_str];
  99. }
  100. #endif
  101. }
  102. return MP_IMPORT_STAT_FILE;
  103. } else if (name[len] == '/') {
  104. // Matches up to directory separator, this is a valid
  105. // directory path.
  106. return MP_IMPORT_STAT_DIR;
  107. }
  108. }
  109. // Skip null separator.
  110. name += entry_len + 1;
  111. }
  112. return MP_IMPORT_STAT_NO_EXIST;
  113. }
  114. #endif // MICROPY_MODULE_FROZEN