|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2025 Damien P. George |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + * |
| 26 | + */ |
| 27 | + |
| 28 | +#include "py/objcode.h" |
| 29 | +#include "py/objfun.h" |
| 30 | +#include "py/persistentcode.h" |
| 31 | +#include "py/runtime.h" |
| 32 | + |
| 33 | +#if MICROPY_PY_MARSHAL |
| 34 | + |
| 35 | +static mp_obj_t marshal_dumps(mp_obj_t value_in) { |
| 36 | + if (mp_obj_is_type(value_in, &mp_type_code)) { |
| 37 | + mp_obj_code_t *code = MP_OBJ_TO_PTR(value_in); |
| 38 | + const void *proto_fun = mp_code_get_proto_fun(code); |
| 39 | + const uint8_t *bytecode; |
| 40 | + if (mp_proto_fun_is_bytecode(proto_fun)) { |
| 41 | + bytecode = proto_fun; |
| 42 | + } else { |
| 43 | + const mp_raw_code_t *rc = proto_fun; |
| 44 | + if (!(rc->kind == MP_CODE_BYTECODE && rc->children == NULL)) { |
| 45 | + mp_raise_ValueError(MP_ERROR_TEXT("function must be bytecode with no children")); |
| 46 | + } |
| 47 | + bytecode = rc->fun_data; |
| 48 | + } |
| 49 | + return mp_raw_code_save_fun_to_bytes(mp_code_get_constants(code), bytecode); |
| 50 | + } else { |
| 51 | + mp_raise_ValueError(MP_ERROR_TEXT("unmarshallable object")); |
| 52 | + } |
| 53 | +} |
| 54 | +static MP_DEFINE_CONST_FUN_OBJ_1(marshal_dumps_obj, marshal_dumps); |
| 55 | + |
| 56 | +static mp_obj_t marshal_loads(mp_obj_t data_in) { |
| 57 | + mp_buffer_info_t bufinfo; |
| 58 | + mp_get_buffer_raise(data_in, &bufinfo, MP_BUFFER_READ); |
| 59 | + mp_module_context_t ctx; |
| 60 | + ctx.module.globals = mp_globals_get(); |
| 61 | + mp_compiled_module_t cm = { .context = &ctx }; |
| 62 | + mp_raw_code_load_mem(bufinfo.buf, bufinfo.len, &cm); |
| 63 | + #if MICROPY_PY_BUILTINS_CODE <= MICROPY_PY_BUILTINS_CODE_BASIC |
| 64 | + return mp_obj_new_code(ctx.constants, cm.rc); |
| 65 | + #else |
| 66 | + mp_module_context_t *ctx_ptr = m_new_obj(mp_module_context_t); |
| 67 | + *ctx_ptr = ctx; |
| 68 | + return mp_obj_new_code(ctx_ptr, cm.rc, true); |
| 69 | + #endif |
| 70 | +} |
| 71 | +static MP_DEFINE_CONST_FUN_OBJ_1(marshal_loads_obj, marshal_loads); |
| 72 | + |
| 73 | +static const mp_rom_map_elem_t mod_marshal_globals_table[] = { |
| 74 | + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_marshal) }, |
| 75 | + { MP_ROM_QSTR(MP_QSTR_dumps), MP_ROM_PTR(&marshal_dumps_obj) }, |
| 76 | + { MP_ROM_QSTR(MP_QSTR_loads), MP_ROM_PTR(&marshal_loads_obj) }, |
| 77 | +}; |
| 78 | + |
| 79 | +static MP_DEFINE_CONST_DICT(mod_marshal_globals, mod_marshal_globals_table); |
| 80 | + |
| 81 | +const mp_obj_module_t mp_module_marshal = { |
| 82 | + .base = { &mp_type_module }, |
| 83 | + .globals = (mp_obj_dict_t *)&mod_marshal_globals, |
| 84 | +}; |
| 85 | + |
| 86 | +MP_REGISTER_MODULE(MP_QSTR_marshal, mp_module_marshal); |
| 87 | + |
| 88 | +#endif // MICROPY_PY_MARSHAL |
0 commit comments