summaryrefslogtreecommitdiffstatshomepage
path: root/py
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2015-11-23 11:54:12 +0000
committerDamien George <damien.p.george@gmail.com>2015-11-23 11:54:12 +0000
commit44e6e348d6fe29c0ba4d47b81a0ac3fc435de1c5 (patch)
tree222204d8dcf3f8a5592aebd28be2d6deda7bca1b /py
parent39a8deb95f9003d5af2d7d4d6e624101b49a6269 (diff)
downloadmicropython-44e6e348d6fe29c0ba4d47b81a0ac3fc435de1c5.tar.gz
micropython-44e6e348d6fe29c0ba4d47b81a0ac3fc435de1c5.zip
py/emitglue: Implement persistent saving and loading of const objects.
Diffstat (limited to 'py')
-rw-r--r--py/emitglue.c83
1 files changed, 54 insertions, 29 deletions
diff --git a/py/emitglue.c b/py/emitglue.c
index 9483e1160c..344a3d96d7 100644
--- a/py/emitglue.c
+++ b/py/emitglue.c
@@ -226,8 +226,13 @@ STATIC void extract_prelude(const byte **ip, const byte **ip2, bytecode_prelude_
#if MICROPY_PERSISTENT_CODE_LOAD
+#include "py/parsenum.h"
#include "py/bc0.h"
+STATIC int read_byte(mp_reader_t *reader) {
+ return reader->read_byte(reader->data);
+}
+
STATIC void read_bytes(mp_reader_t *reader, byte *buf, size_t len) {
while (len-- > 0) {
*buf++ = reader->read_byte(reader->data);
@@ -256,9 +261,23 @@ STATIC qstr load_qstr(mp_reader_t *reader) {
}
STATIC mp_obj_t load_obj(mp_reader_t *reader) {
- (void)reader;
- assert(0);
- return MP_OBJ_NULL;
+ byte obj_type = read_byte(reader);
+ if (obj_type == 'e') {
+ return (mp_obj_t)&mp_const_ellipsis_obj;
+ } else {
+ size_t len = read_uint(reader);
+ vstr_t vstr;
+ vstr_init_len(&vstr, len);
+ read_bytes(reader, (byte*)vstr.buf, len);
+ if (obj_type == 's' || obj_type == 'b') {
+ return mp_obj_new_str_from_vstr(obj_type == 's' ? &mp_type_str : &mp_type_bytes, &vstr);
+ } else if (obj_type == 'i') {
+ return mp_parse_num_integer(vstr.buf, vstr.len, 10, NULL);
+ } else {
+ assert(obj_type == 'f' || obj_type == 'c');
+ return mp_parse_num_decimal(vstr.buf, vstr.len, obj_type == 'c', false, NULL);
+ }
+ }
}
STATIC void load_bytecode_qstrs(mp_reader_t *reader, byte *ip, byte *ip_top) {
@@ -453,6 +472,9 @@ mp_raw_code_t *mp_raw_code_load_file(const char *filename) {
#endif // MICROPY_PERSISTENT_CODE_LOAD
#if MICROPY_PERSISTENT_CODE_SAVE
+
+#include "py/objstr.h"
+
STATIC void mp_print_bytes(mp_print_t *print, const byte *data, size_t len) {
print->print_strn(print->data, (const char*)data, len);
}
@@ -477,38 +499,41 @@ STATIC void save_qstr(mp_print_t *print, qstr qst) {
}
STATIC void save_obj(mp_print_t *print, mp_obj_t o) {
- if (MP_OBJ_IS_STR(o)) {
- byte buf[] = {'s'};
- mp_print_bytes(print, buf, 1);
- mp_uint_t len;
- const char *str = mp_obj_str_get_data(o, &len);
- mp_print_uint(print, len);
- mp_print_bytes(print, (const byte*)str, len);
- } else if (MP_OBJ_IS_TYPE(o, &mp_type_bytes)) {
- byte buf[] = {'b'};
- mp_print_bytes(print, buf, 1);
+ if (MP_OBJ_IS_STR_OR_BYTES(o)) {
+ byte obj_type;
+ if (MP_OBJ_IS_STR(o)) {
+ obj_type = 's';
+ } else {
+ obj_type = 'b';
+ }
mp_uint_t len;
const char *str = mp_obj_str_get_data(o, &len);
+ mp_print_bytes(print, &obj_type, 1);
mp_print_uint(print, len);
mp_print_bytes(print, (const byte*)str, len);
- } else if (MP_OBJ_IS_TYPE(o, &mp_type_int)) {
- byte buf[] = {'i'};
- mp_print_bytes(print, buf, 1);
- // TODO
- } else if (MP_OBJ_IS_TYPE(o, &mp_type_float)) {
- byte buf[] = {'f'};
- mp_print_bytes(print, buf, 1);
- // TODO
- } else if (MP_OBJ_IS_TYPE(o, &mp_type_complex)) {
- byte buf[] = {'c'};
- mp_print_bytes(print, buf, 1);
- // TODO
} else if (o == &mp_const_ellipsis_obj) {
- byte buf[] = {'e'};
- mp_print_bytes(print, buf, 1);
+ byte obj_type = 'e';
+ mp_print_bytes(print, &obj_type, 1);
} else {
- mp_obj_print(o, PRINT_STR);
- assert(0);
+ // we save numbers using a simplistic text representation
+ // TODO could be improved
+ byte obj_type;
+ if (MP_OBJ_IS_TYPE(o, &mp_type_int)) {
+ obj_type = 'i';
+ } else if (MP_OBJ_IS_TYPE(o, &mp_type_float)) {
+ obj_type = 'f';
+ } else {
+ assert(MP_OBJ_IS_TYPE(o, &mp_type_complex));
+ obj_type = 'c';
+ }
+ vstr_t vstr;
+ mp_print_t pr;
+ vstr_init_print(&vstr, 10, &pr);
+ mp_obj_print_helper(&pr, o, PRINT_REPR);
+ mp_print_bytes(print, &obj_type, 1);
+ mp_print_uint(print, vstr.len);
+ mp_print_bytes(print, (const byte*)vstr.buf, vstr.len);
+ vstr_clear(&vstr);
}
}