Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 3 additions & 33 deletions bricks/_common/micropython.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "py/mphal.h"
#include "py/objmodule.h"
#include "py/persistentcode.h"
#include "py/reader.h"
#include "py/repl.h"
#include "py/runtime.h"
#include "py/stackctrl.h"
Expand Down Expand Up @@ -74,35 +75,6 @@ bool pbsys_main_stdin_event(uint8_t c) {
return false;
}

// The following defines a reader for use by micropython/py/persistentcode.c.
typedef struct _mp_vfs_map_minimal_t {
const byte *cur;
const byte *end;
} mp_vfs_map_minimal_t;

mp_uint_t mp_vfs_map_minimal_readbyte(void *data) {
mp_vfs_map_minimal_t *blob = (mp_vfs_map_minimal_t *)data;
return (blob->cur < blob->end) ? *blob->cur++ : MP_READER_EOF;
}

const uint8_t *mp_vfs_map_minimal_read_bytes(mp_reader_t *reader, size_t len) {
mp_vfs_map_minimal_t *blob = (mp_vfs_map_minimal_t *)reader->data;
const uint8_t *ptr = blob->cur;
blob->cur += len;
return ptr;
}

static void mp_vfs_map_minimal_close(void *data) {
}

static void mp_vfs_map_minimal_new_reader(mp_reader_t *reader, mp_vfs_map_minimal_t *data, const byte *buf, size_t len) {
data->cur = buf;
data->end = buf + len;
reader->data = data;
reader->readbyte = mp_vfs_map_minimal_readbyte;
reader->close = mp_vfs_map_minimal_close;
}

// Prints the exception that ended the program.
static void print_final_exception(mp_obj_t exc, int ret) {
nlr_buf_t nlr;
Expand Down Expand Up @@ -270,8 +242,7 @@ static void run_user_program(void) {

// This is similar to __import__ except we don't push/pop globals
mp_reader_t reader;
mp_vfs_map_minimal_t data;
mp_vfs_map_minimal_new_reader(&reader, &data, mpy_data_get_buf(info), pbio_get_uint32_le(info->mpy_size));
mp_reader_new_mem(&reader, mpy_data_get_buf(info), pbio_get_uint32_le(info->mpy_size), MP_READER_IS_ROM);
mp_module_context_t *context = m_new_obj(mp_module_context_t);
context->module.globals = mp_globals_get();
mp_compiled_module_t compiled_module;
Expand Down Expand Up @@ -472,8 +443,7 @@ mp_obj_t pb_builtin_import(size_t n_args, const mp_obj_t *args) {
if (info) {
// Parse the static script data.
mp_reader_t reader;
mp_vfs_map_minimal_t data;
mp_vfs_map_minimal_new_reader(&reader, &data, mpy_data_get_buf(info), pbio_get_uint32_le(info->mpy_size));
mp_reader_new_mem(&reader, mpy_data_get_buf(info), pbio_get_uint32_le(info->mpy_size), MP_READER_IS_ROM);

// Create new module and execute in its own context.
mp_obj_t module_obj = mp_obj_new_module(module_name_qstr);
Expand Down
3 changes: 2 additions & 1 deletion bricks/_common/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@
#define MICROPY_PERSISTENT_CODE_LOAD (1)
#define MICROPY_ENABLE_EXTERNAL_IMPORT (0)
#define MICROPY_HAS_FILE_READER (0)
#define MICROPY_VFS_MAP_MINIMAL (1)
#define MICROPY_VFS_ROM (1)
#define MICROPY_VFS_ROM_IOCTL (0)
#if PYBRICKS_OPT_CUSTOM_IMPORT
#define mp_builtin___import__ pb_builtin_import
#endif
Expand Down