From b5b1f2c527f26dbf56ef1fcb0100eccc8ad0b97e Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 20 Nov 2015 12:44:20 +0000 Subject: [PATCH] py/emitglue: Add mp_raw_code_load_mem to load raw-code from memory. --- py/emitglue.c | 20 ++++++++++++++++++++ py/emitglue.h | 1 + 2 files changed, 21 insertions(+) diff --git a/py/emitglue.c b/py/emitglue.c index 4731af992d..a0f0e004eb 100644 --- a/py/emitglue.c +++ b/py/emitglue.c @@ -324,6 +324,26 @@ mp_raw_code_t *mp_raw_code_load(mp_reader_t *reader) { return load_raw_code(reader); } +typedef struct _mp_mem_reader_t { + const byte *cur; + const byte *end; +} mp_mem_reader_t; + +STATIC mp_uint_t mp_mem_reader_next_byte(void *br_in) { + mp_mem_reader_t *br = br_in; + if (br->cur < br->end) { + return *br->cur++; + } else { + return (mp_uint_t)-1; + } +} + +mp_raw_code_t *mp_raw_code_load_mem(const byte *buf, size_t len) { + mp_mem_reader_t mr = {buf, buf + len}; + mp_reader_t reader = {&mr, mp_mem_reader_next_byte}; + return mp_raw_code_load(&reader); +} + // here we define mp_raw_code_load_file depending on the port // TODO abstract this away properly diff --git a/py/emitglue.h b/py/emitglue.h index 6da0f6cb48..b31e8dbb22 100644 --- a/py/emitglue.h +++ b/py/emitglue.h @@ -62,6 +62,7 @@ typedef struct _mp_reader_t { } mp_reader_t; mp_raw_code_t *mp_raw_code_load(mp_reader_t *reader); +mp_raw_code_t *mp_raw_code_load_mem(const byte *buf, size_t len); mp_raw_code_t *mp_raw_code_load_file(const char *filename); #endif