Skip to content

Commit 39a8deb

Browse files
committed
py/emitglue: Add feature-flag header to .mpy to detect bytecode compat.
Loading .mpy files will now check to make sure that the target VM can support the bytecode.
1 parent 9f10d3f commit 39a8deb

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

py/emitglue.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,12 +319,16 @@ STATIC mp_raw_code_t *load_raw_code(mp_reader_t *reader) {
319319
}
320320

321321
mp_raw_code_t *mp_raw_code_load(mp_reader_t *reader) {
322-
byte header[2];
323-
read_bytes(reader, header, 2);
322+
byte header[3];
323+
read_bytes(reader, header, 3);
324324
if (strncmp((char*)header, "M\x00", 2) != 0) {
325325
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
326326
"invalid .mpy file"));
327327
}
328+
if (header[2] != MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
329+
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
330+
"incompatible .mpy file"));
331+
}
328332
return load_raw_code(reader);
329333
}
330334

@@ -558,7 +562,13 @@ STATIC void save_raw_code(mp_print_t *print, mp_raw_code_t *rc) {
558562
}
559563

560564
void mp_raw_code_save(mp_raw_code_t *rc, mp_print_t *print) {
561-
mp_print_bytes(print, (const byte*)"M\x00", 2);
565+
// header contains:
566+
// byte 'M'
567+
// byte version
568+
// byte feature flags (right now just OPT_CACHE_MAP_LOOKUP_IN_BYTECODE)
569+
byte header[3] = {'M', 0, MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE};
570+
mp_print_bytes(print, header, 3);
571+
562572
save_raw_code(print, rc);
563573
}
564574

0 commit comments

Comments
 (0)