summaryrefslogtreecommitdiff
path: root/py
diff options
context:
space:
mode:
Diffstat (limited to 'py')
-rw-r--r--py/builtinhelp.c17
-rw-r--r--py/builtinimport.c50
-rw-r--r--py/frozenmod.c163
-rw-r--r--py/frozenmod.h4
-rw-r--r--py/qstrdefs.h4
5 files changed, 111 insertions, 127 deletions
diff --git a/py/builtinhelp.c b/py/builtinhelp.c
index 13735635e..84d69caf3 100644
--- a/py/builtinhelp.c
+++ b/py/builtinhelp.c
@@ -67,10 +67,10 @@ STATIC void mp_help_add_from_map(mp_obj_t list, const mp_map_t *map) {
#if MICROPY_MODULE_FROZEN
STATIC void mp_help_add_from_names(mp_obj_t list, const char *name) {
while (*name) {
- size_t l = strlen(name);
+ size_t len = strlen(name);
// name should end in '.py' and we strip it off
- mp_obj_list_append(list, mp_obj_new_str(name, l - 3));
- name += l + 1;
+ mp_obj_list_append(list, mp_obj_new_str(name, len - 3));
+ name += len + 1;
}
}
#endif
@@ -80,14 +80,9 @@ STATIC void mp_help_print_modules(void) {
mp_help_add_from_map(list, &mp_builtin_module_map);
- #if MICROPY_MODULE_FROZEN_STR
- extern const char mp_frozen_str_names[];
- mp_help_add_from_names(list, mp_frozen_str_names);
- #endif
-
- #if MICROPY_MODULE_FROZEN_MPY
- extern const char mp_frozen_mpy_names[];
- mp_help_add_from_names(list, mp_frozen_mpy_names);
+ #if MICROPY_MODULE_FROZEN
+ extern const char mp_frozen_names[];
+ mp_help_add_from_names(list, mp_frozen_names);
#endif
// sort the list so it's printed in alphabetical order
diff --git a/py/builtinimport.c b/py/builtinimport.c
index 755ce779a..3e336633d 100644
--- a/py/builtinimport.c
+++ b/py/builtinimport.c
@@ -50,6 +50,9 @@
// Must be a string of one byte.
#define PATH_SEP_CHAR "/"
+// Virtual sys.path entry that maps to the frozen modules.
+#define MP_FROZEN_PATH_PREFIX ".frozen/"
+
bool mp_obj_is_package(mp_obj_t module) {
mp_obj_t dest[2];
mp_load_method_maybe(module, MP_QSTR___path__, dest);
@@ -62,9 +65,10 @@ bool mp_obj_is_package(mp_obj_t module) {
// will return whether the path is a file, directory, or doesn't exist.
STATIC mp_import_stat_t stat_path_or_frozen(const char *path) {
#if MICROPY_MODULE_FROZEN
- mp_import_stat_t st = mp_frozen_stat(path);
- if (st != MP_IMPORT_STAT_NO_EXIST) {
- return st;
+ // Only try and load as a frozen module if it starts with .frozen/.
+ const int frozen_path_prefix_len = strlen(MP_FROZEN_PATH_PREFIX);
+ if (strncmp(path, MP_FROZEN_PATH_PREFIX, frozen_path_prefix_len) == 0) {
+ return mp_find_frozen_module(path + frozen_path_prefix_len, NULL, NULL);
}
#endif
return mp_import_stat(path);
@@ -193,32 +197,36 @@ STATIC void do_execute_raw_code(mp_obj_t module_obj, mp_raw_code_t *raw_code, co
STATIC void do_load(mp_obj_t module_obj, vstr_t *file) {
#if MICROPY_MODULE_FROZEN || MICROPY_ENABLE_COMPILER || (MICROPY_PERSISTENT_CODE_LOAD && MICROPY_HAS_FILE_READER)
- char *file_str = vstr_null_terminated_str(file);
+ const char *file_str = vstr_null_terminated_str(file);
#endif
// If we support frozen modules (either as str or mpy) then try to find the
// requested filename in the list of frozen module filenames.
#if MICROPY_MODULE_FROZEN
void *modref;
- int frozen_type = mp_find_frozen_module(file_str, file->len, &modref);
-
- // If we support frozen str modules and the compiler is enabled, and we
- // found the filename in the list of frozen files, then load and execute it.
- #if MICROPY_MODULE_FROZEN_STR
- if (frozen_type == MP_FROZEN_STR) {
- do_load_from_lexer(module_obj, modref);
- return;
- }
- #endif
+ int frozen_type;
+ const int frozen_path_prefix_len = strlen(MP_FROZEN_PATH_PREFIX);
+ if (strncmp(file_str, MP_FROZEN_PATH_PREFIX, frozen_path_prefix_len) == 0) {
+ mp_find_frozen_module(file_str + frozen_path_prefix_len, &frozen_type, &modref);
+
+ // If we support frozen str modules and the compiler is enabled, and we
+ // found the filename in the list of frozen files, then load and execute it.
+ #if MICROPY_MODULE_FROZEN_STR
+ if (frozen_type == MP_FROZEN_STR) {
+ do_load_from_lexer(module_obj, modref);
+ return;
+ }
+ #endif
- // If we support frozen mpy modules and we found a corresponding file (and
- // its data) in the list of frozen files, execute it.
- #if MICROPY_MODULE_FROZEN_MPY
- if (frozen_type == MP_FROZEN_MPY) {
- do_execute_raw_code(module_obj, modref, file_str);
- return;
+ // If we support frozen mpy modules and we found a corresponding file (and
+ // its data) in the list of frozen files, execute it.
+ #if MICROPY_MODULE_FROZEN_MPY
+ if (frozen_type == MP_FROZEN_MPY) {
+ do_execute_raw_code(module_obj, modref, file_str + frozen_path_prefix_len);
+ return;
+ }
+ #endif
}
- #endif
#endif // MICROPY_MODULE_FROZEN
diff --git a/py/frozenmod.c b/py/frozenmod.c
index a250c0215..6cb68d1ec 100644
--- a/py/frozenmod.c
+++ b/py/frozenmod.c
@@ -5,6 +5,7 @@
*
* Copyright (c) 2015 Paul Sokolovsky
* Copyright (c) 2016 Damien P. George
+ * Copyright (c) 2021 Jim Mussared
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -31,6 +32,13 @@
#include "py/lexer.h"
#include "py/frozenmod.h"
+#if MICROPY_MODULE_FROZEN
+
+// Null-separated frozen file names. All string-type entries are listed first,
+// followed by mpy-type entries. Use mp_frozen_str_sizes to determine how
+// many string entries.
+extern const char mp_frozen_names[];
+
#if MICROPY_MODULE_FROZEN_STR
#ifndef MICROPY_MODULE_FROZEN_LEXER
@@ -39,118 +47,89 @@
mp_lexer_t *MICROPY_MODULE_FROZEN_LEXER(qstr src_name, const char *str, mp_uint_t len, mp_uint_t free_len);
#endif
-extern const char mp_frozen_str_names[];
+// Size in bytes of each string entry, followed by a zero (terminator).
extern const uint32_t mp_frozen_str_sizes[];
+// Null-separated string content.
extern const char mp_frozen_str_content[];
-
-// On input, *len contains size of name, on output - size of content
-const char *mp_find_frozen_str(const char *str, size_t *len) {
- const char *name = mp_frozen_str_names;
-
- size_t offset = 0;
- for (int i = 0; *name != 0; i++) {
- size_t l = strlen(name);
- if (l == *len && !memcmp(str, name, l)) {
- *len = mp_frozen_str_sizes[i];
- return mp_frozen_str_content + offset;
- }
- name += l + 1;
- offset += mp_frozen_str_sizes[i] + 1;
- }
- return NULL;
-}
-
-STATIC mp_lexer_t *mp_lexer_frozen_str(const char *str, size_t len) {
- size_t name_len = len;
- const char *content = mp_find_frozen_str(str, &len);
-
- if (content == NULL) {
- return NULL;
- }
-
- qstr source = qstr_from_strn(str, name_len);
- mp_lexer_t *lex = MICROPY_MODULE_FROZEN_LEXER(source, content, len, 0);
- return lex;
-}
-
-#endif
+#endif // MICROPY_MODULE_FROZEN_STR
#if MICROPY_MODULE_FROZEN_MPY
#include "py/emitglue.h"
-extern const char mp_frozen_mpy_names[];
extern const mp_raw_code_t *const mp_frozen_mpy_content[];
-STATIC const mp_raw_code_t *mp_find_frozen_mpy(const char *str, size_t len) {
- const char *name = mp_frozen_mpy_names;
- for (size_t i = 0; *name != 0; i++) {
- size_t l = strlen(name);
- if (l == len && !memcmp(str, name, l)) {
- return mp_frozen_mpy_content[i];
- }
- name += l + 1;
- }
- return NULL;
-}
+#endif // MICROPY_MODULE_FROZEN_MPY
-#endif
+// Search for "str" as a frozen entry, returning the stat result
+// (no-exist/file/dir), as well as the type (none/str/mpy) and data.
+// frozen_type can be NULL if its value isn't needed (and then data is assumed to be NULL).
+mp_import_stat_t mp_find_frozen_module(const char *str, int *frozen_type, void **data) {
+ size_t len = strlen(str);
+ const char *name = mp_frozen_names;
-#if MICROPY_MODULE_FROZEN
+ if (frozen_type != NULL) {
+ *frozen_type = MP_FROZEN_NONE;
+ }
-STATIC mp_import_stat_t mp_frozen_stat_helper(const char *name, const char *str) {
- size_t len = strlen(str);
+ // Count the number of str lengths we have to find how many str entries.
+ size_t num_str = 0;
+ #if MICROPY_MODULE_FROZEN_STR && MICROPY_MODULE_FROZEN_MPY
+ for (const uint32_t *s = mp_frozen_str_sizes; *s != 0; ++s) {
+ ++num_str;
+ }
+ #endif
+
+ for (size_t i = 0; *name != 0; i++) {
+ size_t entry_len = strlen(name);
+ if (entry_len >= len && memcmp(str, name, len) == 0) {
+ // Query is a prefix of the current entry.
+ if (entry_len == len) {
+ // Exact match --> file.
+
+ if (frozen_type != NULL) {
+ #if MICROPY_MODULE_FROZEN_STR
+ if (i < num_str) {
+ *frozen_type = MP_FROZEN_STR;
+ // Use the size table to figure out where this index starts.
+ size_t offset = 0;
+ for (size_t j = 0; j < i; ++j) {
+ offset += mp_frozen_str_sizes[j] + 1;
+ }
+ size_t content_len = mp_frozen_str_sizes[i];
+ const char *content = &mp_frozen_str_content[offset];
+
+ // Note: str & len have been updated by find_frozen_entry to strip
+ // the ".frozen/" prefix (to avoid this being a distinct qstr to
+ // the original path QSTR in frozen_content.c).
+ qstr source = qstr_from_strn(str, len);
+ mp_lexer_t *lex = MICROPY_MODULE_FROZEN_LEXER(source, content, content_len, 0);
+ *data = lex;
+ }
+ #endif
+
+ #if MICROPY_MODULE_FROZEN_MPY
+ if (i >= num_str) {
+ *frozen_type = MP_FROZEN_MPY;
+ // Load the corresponding index as a raw_code, taking
+ // into account any string entries to offset by.
+ *data = (void *)mp_frozen_mpy_content[i - num_str];
+ }
+ #endif
+ }
- for (int i = 0; *name != 0; i++) {
- size_t l = strlen(name);
- if (l >= len && !memcmp(str, name, len)) {
- if (name[len] == 0) {
return MP_IMPORT_STAT_FILE;
} else if (name[len] == '/') {
+ // Matches up to directory separator, this is a valid
+ // directory path.
return MP_IMPORT_STAT_DIR;
}
}
- name += l + 1;
- }
- return MP_IMPORT_STAT_NO_EXIST;
-}
-
-mp_import_stat_t mp_frozen_stat(const char *str) {
- mp_import_stat_t stat;
-
- #if MICROPY_MODULE_FROZEN_STR
- stat = mp_frozen_stat_helper(mp_frozen_str_names, str);
- if (stat != MP_IMPORT_STAT_NO_EXIST) {
- return stat;
+ // Skip null separator.
+ name += entry_len + 1;
}
- #endif
-
- #if MICROPY_MODULE_FROZEN_MPY
- stat = mp_frozen_stat_helper(mp_frozen_mpy_names, str);
- if (stat != MP_IMPORT_STAT_NO_EXIST) {
- return stat;
- }
- #endif
return MP_IMPORT_STAT_NO_EXIST;
}
-int mp_find_frozen_module(const char *str, size_t len, void **data) {
- #if MICROPY_MODULE_FROZEN_STR
- mp_lexer_t *lex = mp_lexer_frozen_str(str, len);
- if (lex != NULL) {
- *data = lex;
- return MP_FROZEN_STR;
- }
- #endif
- #if MICROPY_MODULE_FROZEN_MPY
- const mp_raw_code_t *rc = mp_find_frozen_mpy(str, len);
- if (rc != NULL) {
- *data = (void *)rc;
- return MP_FROZEN_MPY;
- }
- #endif
- return MP_FROZEN_NONE;
-}
-
-#endif
+#endif // MICROPY_MODULE_FROZEN
diff --git a/py/frozenmod.h b/py/frozenmod.h
index 8a477d028..be735e85b 100644
--- a/py/frozenmod.h
+++ b/py/frozenmod.h
@@ -35,8 +35,6 @@ enum {
MP_FROZEN_MPY,
};
-int mp_find_frozen_module(const char *str, size_t len, void **data);
-const char *mp_find_frozen_str(const char *str, size_t *len);
-mp_import_stat_t mp_frozen_stat(const char *str);
+mp_import_stat_t mp_find_frozen_module(const char *str, int *frozen_type, void **data);
#endif // MICROPY_INCLUDED_PY_FROZENMOD_H
diff --git a/py/qstrdefs.h b/py/qstrdefs.h
index 5b4e0dc48..405813941 100644
--- a/py/qstrdefs.h
+++ b/py/qstrdefs.h
@@ -60,6 +60,10 @@ Q(<string>)
Q(<stdin>)
Q(utf-8)
+#if MICROPY_MODULE_FROZEN
+Q(.frozen)
+#endif
+
#if MICROPY_ENABLE_PYSTACK
Q(pystack exhausted)
#endif