2014-05-03 18:27:38 -04:00
|
|
|
/*
|
2017-06-30 03:22:17 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-05-03 18:27:38 -04:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2020-06-03 18:40:05 -04:00
|
|
|
* SPDX-FileCopyrightText: Copyright (c) 2013, 2014 Damien P. George
|
2014-05-03 18:27:38 -04:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2014-04-13 13:59:45 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
2015-01-01 15:27:54 -05:00
|
|
|
#include "py/nlr.h"
|
2016-11-03 18:50:59 -04:00
|
|
|
#include "py/objproperty.h"
|
2015-01-01 15:27:54 -05:00
|
|
|
#include "py/runtime.h"
|
2014-04-13 13:59:45 -04:00
|
|
|
|
2014-06-01 08:32:54 -04:00
|
|
|
#if MICROPY_PY_BUILTINS_PROPERTY
|
2014-04-13 13:59:45 -04:00
|
|
|
|
2016-01-03 10:55:55 -05:00
|
|
|
STATIC mp_obj_t property_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
|
2016-01-13 06:29:17 -05:00
|
|
|
enum { ARG_fget, ARG_fset, ARG_fdel, ARG_doc };
|
|
|
|
static const mp_arg_t allowed_args[] = {
|
2019-12-15 23:40:05 -05:00
|
|
|
{ MP_QSTR_, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
|
|
|
|
{ MP_QSTR_, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
|
|
|
|
{ MP_QSTR_, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
|
|
|
|
{ MP_QSTR_doc, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
|
2016-01-13 06:29:17 -05:00
|
|
|
};
|
|
|
|
mp_arg_val_t vals[MP_ARRAY_SIZE(allowed_args)];
|
|
|
|
mp_arg_parse_all_kw_array(n_args, n_kw, args, MP_ARRAY_SIZE(allowed_args), allowed_args, vals);
|
2014-04-13 13:59:45 -04:00
|
|
|
|
|
|
|
mp_obj_property_t *o = m_new_obj(mp_obj_property_t);
|
2016-01-03 10:55:55 -05:00
|
|
|
o->base.type = type;
|
2016-01-13 06:29:17 -05:00
|
|
|
o->proxy[0] = vals[ARG_fget].u_obj;
|
|
|
|
o->proxy[1] = vals[ARG_fset].u_obj;
|
|
|
|
o->proxy[2] = vals[ARG_fdel].u_obj;
|
|
|
|
// vals[ARG_doc] is silently discarded
|
2015-11-27 12:01:44 -05:00
|
|
|
return MP_OBJ_FROM_PTR(o);
|
2014-04-13 13:59:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
STATIC mp_obj_t property_getter(mp_obj_t self_in, mp_obj_t getter) {
|
|
|
|
mp_obj_property_t *p2 = m_new_obj(mp_obj_property_t);
|
2021-03-15 09:57:36 -04:00
|
|
|
*p2 = *(mp_obj_property_t *)MP_OBJ_TO_PTR(self_in);
|
2014-04-13 13:59:45 -04:00
|
|
|
p2->proxy[0] = getter;
|
2015-11-27 12:01:44 -05:00
|
|
|
return MP_OBJ_FROM_PTR(p2);
|
2014-04-13 13:59:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(property_getter_obj, property_getter);
|
|
|
|
|
|
|
|
STATIC mp_obj_t property_setter(mp_obj_t self_in, mp_obj_t setter) {
|
|
|
|
mp_obj_property_t *p2 = m_new_obj(mp_obj_property_t);
|
2021-03-15 09:57:36 -04:00
|
|
|
*p2 = *(mp_obj_property_t *)MP_OBJ_TO_PTR(self_in);
|
2014-04-13 13:59:45 -04:00
|
|
|
p2->proxy[1] = setter;
|
2015-11-27 12:01:44 -05:00
|
|
|
return MP_OBJ_FROM_PTR(p2);
|
2014-04-13 13:59:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(property_setter_obj, property_setter);
|
|
|
|
|
|
|
|
STATIC mp_obj_t property_deleter(mp_obj_t self_in, mp_obj_t deleter) {
|
|
|
|
mp_obj_property_t *p2 = m_new_obj(mp_obj_property_t);
|
2021-03-15 09:57:36 -04:00
|
|
|
*p2 = *(mp_obj_property_t *)MP_OBJ_TO_PTR(self_in);
|
2014-04-13 13:59:45 -04:00
|
|
|
p2->proxy[2] = deleter;
|
2015-11-27 12:01:44 -05:00
|
|
|
return MP_OBJ_FROM_PTR(p2);
|
2014-04-13 13:59:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(property_deleter_obj, property_deleter);
|
|
|
|
|
2015-11-27 08:38:15 -05:00
|
|
|
STATIC const mp_rom_map_elem_t property_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_getter), MP_ROM_PTR(&property_getter_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_setter), MP_ROM_PTR(&property_setter_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_deleter), MP_ROM_PTR(&property_deleter_obj) },
|
2014-04-13 13:59:45 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(property_locals_dict, property_locals_dict_table);
|
|
|
|
|
|
|
|
const mp_obj_type_t mp_type_property = {
|
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_property,
|
|
|
|
.make_new = property_make_new,
|
2021-03-15 09:57:36 -04:00
|
|
|
.locals_dict = (mp_obj_dict_t *)&property_locals_dict,
|
2014-04-13 13:59:45 -04:00
|
|
|
};
|
|
|
|
|
2022-05-03 09:31:41 -04:00
|
|
|
#if MICROPY_PY_OPTIMIZE_PROPERTY_FLASH_SIZE
|
|
|
|
extern const mp_obj_property_t __property_getter_start, __property_getter_end, __property_getset_start, __property_getset_end;
|
|
|
|
#endif
|
|
|
|
|
2023-02-28 18:07:35 -05:00
|
|
|
const mp_obj_t *PLACE_IN_ITCM(mp_obj_property_get)(mp_obj_t self_in, size_t *n_proxy) {
|
2021-04-22 20:55:39 -04:00
|
|
|
mp_check_self(mp_obj_is_type(self_in, &mp_type_property));
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_property_t *self = MP_OBJ_TO_PTR(self_in);
|
2022-05-03 09:31:41 -04:00
|
|
|
#if MICROPY_PY_OPTIMIZE_PROPERTY_FLASH_SIZE
|
|
|
|
if (self >= &__property_getter_start && self < &__property_getter_end) {
|
|
|
|
*n_proxy = 1;
|
|
|
|
} else if (self >= &__property_getset_start && self < &__property_getset_end) {
|
|
|
|
*n_proxy = 2;
|
|
|
|
} else {
|
|
|
|
*n_proxy = 3;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
*n_proxy = 3;
|
|
|
|
#endif
|
2014-04-13 13:59:45 -04:00
|
|
|
return self->proxy;
|
|
|
|
}
|
|
|
|
|
2014-06-01 08:32:54 -04:00
|
|
|
#endif // MICROPY_PY_BUILTINS_PROPERTY
|