|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2019 Bernhard Boser |
| 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 | +#include "py/runtime.h" |
| 28 | +#include "py/smallint.h" |
| 29 | +#include "py/objproperty.h" |
| 30 | +#include "shared-bindings/msgpack/ExtType.h" |
| 31 | + |
| 32 | +//| class ExtType: |
| 33 | +//| """ExtType represents ext type in msgpack.""" |
| 34 | +//| def __init__(self, code: int, data: bytes) -> None: |
| 35 | +//| """Constructor""" |
| 36 | +//| |
| 37 | +//| :param int code: type code in range 0~127. |
| 38 | +//| :param bytes data: representation. |
| 39 | + |
| 40 | +STATIC mp_obj_t mod_msgpack_exttype_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 41 | + mod_msgpack_extype_obj_t *self = m_new_obj(mod_msgpack_extype_obj_t); |
| 42 | + self->base.type = &mod_msgpack_exttype_type; |
| 43 | + enum { ARG_code, ARG_data }; |
| 44 | + static const mp_arg_t allowed_args[] = { |
| 45 | + { MP_QSTR_code, MP_ARG_INT | MP_ARG_REQUIRED }, |
| 46 | + { MP_QSTR_data, MP_ARG_OBJ | MP_ARG_REQUIRED }, |
| 47 | + }; |
| 48 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 49 | + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 50 | + |
| 51 | + int code = args[ARG_code].u_int; |
| 52 | + if (code < 0 || code > 127) { |
| 53 | + mp_raise_AttributeError(translate("code outside range 0~127")); |
| 54 | + } |
| 55 | + self->code = code; |
| 56 | + |
| 57 | + mp_obj_t data = args[ARG_data].u_obj; |
| 58 | + self->data = data; |
| 59 | + return MP_OBJ_FROM_PTR(self); |
| 60 | +} |
| 61 | + |
| 62 | + |
| 63 | +//| code: int |
| 64 | +//| """The type code, in range 0~127.""" |
| 65 | +//| |
| 66 | + |
| 67 | +STATIC mp_obj_t mod_msgpack_exttype_get_code(mp_obj_t self_in) { |
| 68 | + mod_msgpack_extype_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 69 | + return MP_OBJ_NEW_SMALL_INT(self->code); |
| 70 | +} |
| 71 | +MP_DEFINE_CONST_FUN_OBJ_1(mod_msgpack_exttype_get_code_obj, mod_msgpack_exttype_get_code); |
| 72 | + |
| 73 | +STATIC mp_obj_t mod_msgpack_exttype_set_code(mp_obj_t self_in, mp_obj_t code_in) { |
| 74 | + mod_msgpack_extype_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 75 | + int code = mp_obj_get_int(code_in); |
| 76 | + if (code < 0 || code > 127) { |
| 77 | + mp_raise_AttributeError(translate("code outside range 0~127")); |
| 78 | + } |
| 79 | + self->code = code; |
| 80 | + return mp_const_none; |
| 81 | +} |
| 82 | +MP_DEFINE_CONST_FUN_OBJ_2(mod_msgpack_exttype_set_code_obj, mod_msgpack_exttype_set_code); |
| 83 | + |
| 84 | +const mp_obj_property_t mod_msgpack_exttype_code_obj = { |
| 85 | + .base.type = &mp_type_property, |
| 86 | + .proxy = {(mp_obj_t)&mod_msgpack_exttype_get_code_obj, |
| 87 | + (mp_obj_t)&mod_msgpack_exttype_set_code_obj, |
| 88 | + (mp_obj_t)&mp_const_none_obj}, |
| 89 | +}; |
| 90 | + |
| 91 | +//| data: bytes |
| 92 | +//| """Data.""" |
| 93 | +//| |
| 94 | + |
| 95 | +STATIC mp_obj_t mod_msgpack_exttype_get_data(mp_obj_t self_in) { |
| 96 | + mod_msgpack_extype_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 97 | + return self->data; |
| 98 | +} |
| 99 | +MP_DEFINE_CONST_FUN_OBJ_1(mod_msgpack_exttype_get_data_obj, mod_msgpack_exttype_get_data); |
| 100 | + |
| 101 | +STATIC mp_obj_t mod_msgpack_exttype_set_data(mp_obj_t self_in, mp_obj_t data_in) { |
| 102 | + mod_msgpack_extype_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 103 | + self->data = data_in; |
| 104 | + return mp_const_none; |
| 105 | +} |
| 106 | +MP_DEFINE_CONST_FUN_OBJ_2(mod_msgpack_exttype_set_data_obj, mod_msgpack_exttype_set_data); |
| 107 | + |
| 108 | +const mp_obj_property_t mod_msgpack_exttype_data_obj = { |
| 109 | + .base.type = &mp_type_property, |
| 110 | + .proxy = {(mp_obj_t)&mod_msgpack_exttype_get_data_obj, |
| 111 | + (mp_obj_t)&mod_msgpack_exttype_set_data_obj, |
| 112 | + (mp_obj_t)&mp_const_none_obj}, |
| 113 | +}; |
| 114 | + |
| 115 | +STATIC mp_rom_map_elem_t mod_msgpack_exttype_locals_dict_table[] = { |
| 116 | + // Properties |
| 117 | + { MP_ROM_QSTR(MP_QSTR_code), MP_ROM_PTR(&mod_msgpack_exttype_code_obj) }, |
| 118 | + { MP_ROM_QSTR(MP_QSTR_data), MP_ROM_PTR(&mod_msgpack_exttype_data_obj) }, |
| 119 | +}; |
| 120 | +STATIC MP_DEFINE_CONST_DICT(mod_msgpack_exttype_locals_dict, mod_msgpack_exttype_locals_dict_table); |
| 121 | + |
| 122 | +const mp_obj_type_t mod_msgpack_exttype_type = { |
| 123 | + { &mp_type_type }, |
| 124 | + .name = MP_QSTR_ExtType, |
| 125 | + .make_new = mod_msgpack_exttype_make_new, |
| 126 | + .locals_dict = (mp_obj_dict_t*)&mod_msgpack_exttype_locals_dict, |
| 127 | +}; |
0 commit comments