Reworking how "run list" works saves a dozen bytes

This commit is contained in:
Jeff Epler 2022-11-09 08:28:56 -06:00
parent fb66a6bfe5
commit d2e2a61075
No known key found for this signature in database
GPG Key ID: D5BF15AB975AB4DE
1 changed files with 21 additions and 20 deletions

41
main.c
View File

@ -218,12 +218,10 @@ void supervisor_execution_status(void) {
} }
#endif #endif
#define STRING_LIST(...) {__VA_ARGS__, ""}
// Look for the first file that exists in the list of filenames, using mp_import_stat(). // Look for the first file that exists in the list of filenames, using mp_import_stat().
// Return its index. If no file found, return -1. // Return its index. If no file found, return -1.
STATIC const char *first_existing_file_in_list(const char *const *filenames) { STATIC const char *first_existing_file_in_list(const char *const *filenames, size_t n_filenames) {
for (int i = 0; filenames[i] != (char *)""; i++) { for (size_t i = 0; i < n_filenames; i++) {
mp_import_stat_t stat = mp_import_stat(filenames[i]); mp_import_stat_t stat = mp_import_stat(filenames[i]);
if (stat == MP_IMPORT_STAT_FILE) { if (stat == MP_IMPORT_STAT_FILE) {
return filenames[i]; return filenames[i];
@ -232,11 +230,11 @@ STATIC const char *first_existing_file_in_list(const char *const *filenames) {
return NULL; return NULL;
} }
STATIC bool maybe_run_list(const char *const *filenames) { STATIC bool maybe_run_list(const char *const *filenames, size_t n_filenames) {
_exec_result.return_code = 0; _exec_result.return_code = 0;
_exec_result.exception = MP_OBJ_NULL; _exec_result.exception = MP_OBJ_NULL;
_exec_result.exception_line = 0; _exec_result.exception_line = 0;
_current_executing_filename = first_existing_file_in_list(filenames); _current_executing_filename = first_existing_file_in_list(filenames, n_filenames);
if (_current_executing_filename == NULL) { if (_current_executing_filename == NULL) {
return false; return false;
} }
@ -391,12 +389,14 @@ STATIC bool run_code_py(safe_mode_t safe_mode, bool *simulate_reset) {
filesystem_flush(); filesystem_flush();
} }
if (safe_mode == NO_SAFE_MODE && !autoreload_pending()) { if (safe_mode == NO_SAFE_MODE && !autoreload_pending()) {
static const char *const supported_filenames[] = STRING_LIST( static const char *const supported_filenames[] = {
"code.txt", "code.py", "main.py", "main.txt"); "code.txt", "code.py", "main.py", "main.txt"
};
#if CIRCUITPY_FULL_BUILD #if CIRCUITPY_FULL_BUILD
static const char *const double_extension_filenames[] = STRING_LIST( static const char *const double_extension_filenames[] = {
"code.txt.py", "code.py.txt", "code.txt.txt","code.py.py", "code.txt.py", "code.py.txt", "code.txt.txt","code.py.py",
"main.txt.py", "main.py.txt", "main.txt.txt","main.py.py"); "main.txt.py", "main.py.txt", "main.txt.txt","main.py.py"
};
#endif #endif
supervisor_allocation *heap = allocate_remaining_memory(); supervisor_allocation *heap = allocate_remaining_memory();
@ -410,14 +410,15 @@ STATIC bool run_code_py(safe_mode_t safe_mode, bool *simulate_reset) {
// Check if a different run file has been allocated // Check if a different run file has been allocated
if (next_code_allocation) { if (next_code_allocation) {
((next_code_info_t *)next_code_allocation->ptr)->options &= ~SUPERVISOR_NEXT_CODE_OPT_NEWLY_SET; next_code_info_t *info = ((next_code_info_t *)next_code_allocation->ptr);
next_code_options = ((next_code_info_t *)next_code_allocation->ptr)->options; info->options &= ~SUPERVISOR_NEXT_CODE_OPT_NEWLY_SET;
if (((next_code_info_t *)next_code_allocation->ptr)->filename[0] != '\0') { next_code_options = info->options;
const char *next_list[] = {((next_code_info_t *)next_code_allocation->ptr)->filename, ""}; if (info->filename[0] != '\0') {
// This is where the user's python code is actually executed: // This is where the user's python code is actually executed:
found_main = maybe_run_list(next_list); const char *const filenames[] = { info->filename };
found_main = maybe_run_list(filenames, MP_ARRAY_SIZE(filenames));
if (!found_main) { if (!found_main) {
serial_write(((next_code_info_t *)next_code_allocation->ptr)->filename); serial_write(info->filename);
serial_write_compressed(translate(" not found.\n")); serial_write_compressed(translate(" not found.\n"));
} }
} }
@ -425,11 +426,11 @@ STATIC bool run_code_py(safe_mode_t safe_mode, bool *simulate_reset) {
// Otherwise, default to the standard list of filenames // Otherwise, default to the standard list of filenames
if (!found_main) { if (!found_main) {
// This is where the user's python code is actually executed: // This is where the user's python code is actually executed:
found_main = maybe_run_list(supported_filenames); found_main = maybe_run_list(supported_filenames, MP_ARRAY_SIZE(supported_filenames));
// If that didn't work, double check the extensions // If that didn't work, double check the extensions
#if CIRCUITPY_FULL_BUILD #if CIRCUITPY_FULL_BUILD
if (!found_main) { if (!found_main) {
found_main = maybe_run_list(double_extension_filenames); found_main = maybe_run_list(double_extension_filenames, MP_ARRAY_SIZE(double_extension_filenames));
if (found_main) { if (found_main) {
serial_write_compressed(translate("WARNING: Your code filename has two extensions\n")); serial_write_compressed(translate("WARNING: Your code filename has two extensions\n"));
} }
@ -741,7 +742,7 @@ STATIC void __attribute__ ((noinline)) run_boot_py(safe_mode_t safe_mode) {
&& safe_mode == NO_SAFE_MODE && safe_mode == NO_SAFE_MODE
&& MP_STATE_VM(vfs_mount_table) != NULL; && MP_STATE_VM(vfs_mount_table) != NULL;
static const char *const boot_py_filenames[] = STRING_LIST("boot.py", "boot.txt"); static const char *const boot_py_filenames[] = {"boot.py", "boot.txt"};
// Do USB setup even if boot.py is not run. // Do USB setup even if boot.py is not run.
@ -778,7 +779,7 @@ STATIC void __attribute__ ((noinline)) run_boot_py(safe_mode_t safe_mode) {
port_boot_info(); port_boot_info();
#endif #endif
bool found_boot = maybe_run_list(boot_py_filenames); bool found_boot = maybe_run_list(boot_py_filenames, MP_ARRAY_SIZE(boot_py_filenames));
(void)found_boot; (void)found_boot;