Skip to content

Commit d2e2a61

Browse files
committed
Reworking how "run list" works saves a dozen bytes
1 parent fb66a6b commit d2e2a61

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

main.c

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,10 @@ void supervisor_execution_status(void) {
218218
}
219219
#endif
220220

221-
#define STRING_LIST(...) {__VA_ARGS__, ""}
222-
223221
// Look for the first file that exists in the list of filenames, using mp_import_stat().
224222
// Return its index. If no file found, return -1.
225-
STATIC const char *first_existing_file_in_list(const char *const *filenames) {
226-
for (int i = 0; filenames[i] != (char *)""; i++) {
223+
STATIC const char *first_existing_file_in_list(const char *const *filenames, size_t n_filenames) {
224+
for (size_t i = 0; i < n_filenames; i++) {
227225
mp_import_stat_t stat = mp_import_stat(filenames[i]);
228226
if (stat == MP_IMPORT_STAT_FILE) {
229227
return filenames[i];
@@ -232,11 +230,11 @@ STATIC const char *first_existing_file_in_list(const char *const *filenames) {
232230
return NULL;
233231
}
234232

235-
STATIC bool maybe_run_list(const char *const *filenames) {
233+
STATIC bool maybe_run_list(const char *const *filenames, size_t n_filenames) {
236234
_exec_result.return_code = 0;
237235
_exec_result.exception = MP_OBJ_NULL;
238236
_exec_result.exception_line = 0;
239-
_current_executing_filename = first_existing_file_in_list(filenames);
237+
_current_executing_filename = first_existing_file_in_list(filenames, n_filenames);
240238
if (_current_executing_filename == NULL) {
241239
return false;
242240
}
@@ -391,12 +389,14 @@ STATIC bool run_code_py(safe_mode_t safe_mode, bool *simulate_reset) {
391389
filesystem_flush();
392390
}
393391
if (safe_mode == NO_SAFE_MODE && !autoreload_pending()) {
394-
static const char *const supported_filenames[] = STRING_LIST(
395-
"code.txt", "code.py", "main.py", "main.txt");
392+
static const char *const supported_filenames[] = {
393+
"code.txt", "code.py", "main.py", "main.txt"
394+
};
396395
#if CIRCUITPY_FULL_BUILD
397-
static const char *const double_extension_filenames[] = STRING_LIST(
396+
static const char *const double_extension_filenames[] = {
398397
"code.txt.py", "code.py.txt", "code.txt.txt","code.py.py",
399-
"main.txt.py", "main.py.txt", "main.txt.txt","main.py.py");
398+
"main.txt.py", "main.py.txt", "main.txt.txt","main.py.py"
399+
};
400400
#endif
401401

402402
supervisor_allocation *heap = allocate_remaining_memory();
@@ -410,26 +410,27 @@ STATIC bool run_code_py(safe_mode_t safe_mode, bool *simulate_reset) {
410410

411411
// Check if a different run file has been allocated
412412
if (next_code_allocation) {
413-
((next_code_info_t *)next_code_allocation->ptr)->options &= ~SUPERVISOR_NEXT_CODE_OPT_NEWLY_SET;
414-
next_code_options = ((next_code_info_t *)next_code_allocation->ptr)->options;
415-
if (((next_code_info_t *)next_code_allocation->ptr)->filename[0] != '\0') {
416-
const char *next_list[] = {((next_code_info_t *)next_code_allocation->ptr)->filename, ""};
413+
next_code_info_t *info = ((next_code_info_t *)next_code_allocation->ptr);
414+
info->options &= ~SUPERVISOR_NEXT_CODE_OPT_NEWLY_SET;
415+
next_code_options = info->options;
416+
if (info->filename[0] != '\0') {
417417
// This is where the user's python code is actually executed:
418-
found_main = maybe_run_list(next_list);
418+
const char *const filenames[] = { info->filename };
419+
found_main = maybe_run_list(filenames, MP_ARRAY_SIZE(filenames));
419420
if (!found_main) {
420-
serial_write(((next_code_info_t *)next_code_allocation->ptr)->filename);
421+
serial_write(info->filename);
421422
serial_write_compressed(translate(" not found.\n"));
422423
}
423424
}
424425
}
425426
// Otherwise, default to the standard list of filenames
426427
if (!found_main) {
427428
// This is where the user's python code is actually executed:
428-
found_main = maybe_run_list(supported_filenames);
429+
found_main = maybe_run_list(supported_filenames, MP_ARRAY_SIZE(supported_filenames));
429430
// If that didn't work, double check the extensions
430431
#if CIRCUITPY_FULL_BUILD
431432
if (!found_main) {
432-
found_main = maybe_run_list(double_extension_filenames);
433+
found_main = maybe_run_list(double_extension_filenames, MP_ARRAY_SIZE(double_extension_filenames));
433434
if (found_main) {
434435
serial_write_compressed(translate("WARNING: Your code filename has two extensions\n"));
435436
}
@@ -741,7 +742,7 @@ STATIC void __attribute__ ((noinline)) run_boot_py(safe_mode_t safe_mode) {
741742
&& safe_mode == NO_SAFE_MODE
742743
&& MP_STATE_VM(vfs_mount_table) != NULL;
743744

744-
static const char *const boot_py_filenames[] = STRING_LIST("boot.py", "boot.txt");
745+
static const char *const boot_py_filenames[] = {"boot.py", "boot.txt"};
745746

746747
// Do USB setup even if boot.py is not run.
747748

@@ -778,7 +779,7 @@ STATIC void __attribute__ ((noinline)) run_boot_py(safe_mode_t safe_mode) {
778779
port_boot_info();
779780
#endif
780781

781-
bool found_boot = maybe_run_list(boot_py_filenames);
782+
bool found_boot = maybe_run_list(boot_py_filenames, MP_ARRAY_SIZE(boot_py_filenames));
782783
(void)found_boot;
783784

784785

0 commit comments

Comments
 (0)