Skip to content

Commit 44e6e34

Browse files
committed
py/emitglue: Implement persistent saving and loading of const objects.
1 parent 39a8deb commit 44e6e34

File tree

1 file changed

+54
-29
lines changed

1 file changed

+54
-29
lines changed

py/emitglue.c

Lines changed: 54 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,13 @@ STATIC void extract_prelude(const byte **ip, const byte **ip2, bytecode_prelude_
226226

227227
#if MICROPY_PERSISTENT_CODE_LOAD
228228

229+
#include "py/parsenum.h"
229230
#include "py/bc0.h"
230231

232+
STATIC int read_byte(mp_reader_t *reader) {
233+
return reader->read_byte(reader->data);
234+
}
235+
231236
STATIC void read_bytes(mp_reader_t *reader, byte *buf, size_t len) {
232237
while (len-- > 0) {
233238
*buf++ = reader->read_byte(reader->data);
@@ -256,9 +261,23 @@ STATIC qstr load_qstr(mp_reader_t *reader) {
256261
}
257262

258263
STATIC mp_obj_t load_obj(mp_reader_t *reader) {
259-
(void)reader;
260-
assert(0);
261-
return MP_OBJ_NULL;
264+
byte obj_type = read_byte(reader);
265+
if (obj_type == 'e') {
266+
return (mp_obj_t)&mp_const_ellipsis_obj;
267+
} else {
268+
size_t len = read_uint(reader);
269+
vstr_t vstr;
270+
vstr_init_len(&vstr, len);
271+
read_bytes(reader, (byte*)vstr.buf, len);
272+
if (obj_type == 's' || obj_type == 'b') {
273+
return mp_obj_new_str_from_vstr(obj_type == 's' ? &mp_type_str : &mp_type_bytes, &vstr);
274+
} else if (obj_type == 'i') {
275+
return mp_parse_num_integer(vstr.buf, vstr.len, 10, NULL);
276+
} else {
277+
assert(obj_type == 'f' || obj_type == 'c');
278+
return mp_parse_num_decimal(vstr.buf, vstr.len, obj_type == 'c', false, NULL);
279+
}
280+
}
262281
}
263282

264283
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) {
453472
#endif // MICROPY_PERSISTENT_CODE_LOAD
454473

455474
#if MICROPY_PERSISTENT_CODE_SAVE
475+
476+
#include "py/objstr.h"
477+
456478
STATIC void mp_print_bytes(mp_print_t *print, const byte *data, size_t len) {
457479
print->print_strn(print->data, (const char*)data, len);
458480
}
@@ -477,38 +499,41 @@ STATIC void save_qstr(mp_print_t *print, qstr qst) {
477499
}
478500

479501
STATIC void save_obj(mp_print_t *print, mp_obj_t o) {
480-
if (MP_OBJ_IS_STR(o)) {
481-
byte buf[] = {'s'};
482-
mp_print_bytes(print, buf, 1);
483-
mp_uint_t len;
484-
const char *str = mp_obj_str_get_data(o, &len);
485-
mp_print_uint(print, len);
486-
mp_print_bytes(print, (const byte*)str, len);
487-
} else if (MP_OBJ_IS_TYPE(o, &mp_type_bytes)) {
488-
byte buf[] = {'b'};
489-
mp_print_bytes(print, buf, 1);
502+
if (MP_OBJ_IS_STR_OR_BYTES(o)) {
503+
byte obj_type;
504+
if (MP_OBJ_IS_STR(o)) {
505+
obj_type = 's';
506+
} else {
507+
obj_type = 'b';
508+
}
490509
mp_uint_t len;
491510
const char *str = mp_obj_str_get_data(o, &len);
511+
mp_print_bytes(print, &obj_type, 1);
492512
mp_print_uint(print, len);
493513
mp_print_bytes(print, (const byte*)str, len);
494-
} else if (MP_OBJ_IS_TYPE(o, &mp_type_int)) {
495-
byte buf[] = {'i'};
496-
mp_print_bytes(print, buf, 1);
497-
// TODO
498-
} else if (MP_OBJ_IS_TYPE(o, &mp_type_float)) {
499-
byte buf[] = {'f'};
500-
mp_print_bytes(print, buf, 1);
501-
// TODO
502-
} else if (MP_OBJ_IS_TYPE(o, &mp_type_complex)) {
503-
byte buf[] = {'c'};
504-
mp_print_bytes(print, buf, 1);
505-
// TODO
506514
} else if (o == &mp_const_ellipsis_obj) {
507-
byte buf[] = {'e'};
508-
mp_print_bytes(print, buf, 1);
515+
byte obj_type = 'e';
516+
mp_print_bytes(print, &obj_type, 1);
509517
} else {
510-
mp_obj_print(o, PRINT_STR);
511-
assert(0);
518+
// we save numbers using a simplistic text representation
519+
// TODO could be improved
520+
byte obj_type;
521+
if (MP_OBJ_IS_TYPE(o, &mp_type_int)) {
522+
obj_type = 'i';
523+
} else if (MP_OBJ_IS_TYPE(o, &mp_type_float)) {
524+
obj_type = 'f';
525+
} else {
526+
assert(MP_OBJ_IS_TYPE(o, &mp_type_complex));
527+
obj_type = 'c';
528+
}
529+
vstr_t vstr;
530+
mp_print_t pr;
531+
vstr_init_print(&vstr, 10, &pr);
532+
mp_obj_print_helper(&pr, o, PRINT_REPR);
533+
mp_print_bytes(print, &obj_type, 1);
534+
mp_print_uint(print, vstr.len);
535+
mp_print_bytes(print, (const byte*)vstr.buf, vstr.len);
536+
vstr_clear(&vstr);
512537
}
513538
}
514539

0 commit comments

Comments
 (0)