1 /*
2  * This file is part of the MicroPython project, http://micropython.org/
3  *
4  * The MIT License (MIT)
5  *
6  * Copyright (c) 2013, 2014 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 #include <stdlib.h>
28 #include <assert.h>
29 
30 #include "py/runtime.h"
31 
32 #if MICROPY_PY_BUILTINS_PROPERTY
33 
34 typedef struct _mp_obj_property_t {
35     mp_obj_base_t base;
36     mp_obj_t proxy[3]; // getter, setter, deleter
37 } mp_obj_property_t;
38 
property_make_new(const mp_obj_type_t * type,size_t n_args,size_t n_kw,const mp_obj_t * args)39 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) {
40     enum { ARG_fget, ARG_fset, ARG_fdel, ARG_doc };
41     static const mp_arg_t allowed_args[] = {
42         { MP_QSTR_, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
43         { MP_QSTR_, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
44         { MP_QSTR_, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
45         { MP_QSTR_doc, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
46     };
47     mp_arg_val_t vals[MP_ARRAY_SIZE(allowed_args)];
48     mp_arg_parse_all_kw_array(n_args, n_kw, args, MP_ARRAY_SIZE(allowed_args), allowed_args, vals);
49 
50     mp_obj_property_t *o = m_new_obj(mp_obj_property_t);
51     o->base.type = type;
52     o->proxy[0] = vals[ARG_fget].u_obj;
53     o->proxy[1] = vals[ARG_fset].u_obj;
54     o->proxy[2] = vals[ARG_fdel].u_obj;
55     // vals[ARG_doc] is silently discarded
56     return MP_OBJ_FROM_PTR(o);
57 }
58 
property_getter(mp_obj_t self_in,mp_obj_t getter)59 STATIC mp_obj_t property_getter(mp_obj_t self_in, mp_obj_t getter) {
60     mp_obj_property_t *p2 = m_new_obj(mp_obj_property_t);
61     *p2 = *(mp_obj_property_t *)MP_OBJ_TO_PTR(self_in);
62     p2->proxy[0] = getter;
63     return MP_OBJ_FROM_PTR(p2);
64 }
65 
66 STATIC MP_DEFINE_CONST_FUN_OBJ_2(property_getter_obj, property_getter);
67 
property_setter(mp_obj_t self_in,mp_obj_t setter)68 STATIC mp_obj_t property_setter(mp_obj_t self_in, mp_obj_t setter) {
69     mp_obj_property_t *p2 = m_new_obj(mp_obj_property_t);
70     *p2 = *(mp_obj_property_t *)MP_OBJ_TO_PTR(self_in);
71     p2->proxy[1] = setter;
72     return MP_OBJ_FROM_PTR(p2);
73 }
74 
75 STATIC MP_DEFINE_CONST_FUN_OBJ_2(property_setter_obj, property_setter);
76 
property_deleter(mp_obj_t self_in,mp_obj_t deleter)77 STATIC mp_obj_t property_deleter(mp_obj_t self_in, mp_obj_t deleter) {
78     mp_obj_property_t *p2 = m_new_obj(mp_obj_property_t);
79     *p2 = *(mp_obj_property_t *)MP_OBJ_TO_PTR(self_in);
80     p2->proxy[2] = deleter;
81     return MP_OBJ_FROM_PTR(p2);
82 }
83 
84 STATIC MP_DEFINE_CONST_FUN_OBJ_2(property_deleter_obj, property_deleter);
85 
86 STATIC const mp_rom_map_elem_t property_locals_dict_table[] = {
87     { MP_ROM_QSTR(MP_QSTR_getter), MP_ROM_PTR(&property_getter_obj) },
88     { MP_ROM_QSTR(MP_QSTR_setter), MP_ROM_PTR(&property_setter_obj) },
89     { MP_ROM_QSTR(MP_QSTR_deleter), MP_ROM_PTR(&property_deleter_obj) },
90 };
91 
92 STATIC MP_DEFINE_CONST_DICT(property_locals_dict, property_locals_dict_table);
93 
94 const mp_obj_type_t mp_type_property = {
95     { &mp_type_type },
96     .name = MP_QSTR_property,
97     .make_new = property_make_new,
98     .locals_dict = (mp_obj_dict_t *)&property_locals_dict,
99 };
100 
mp_obj_property_get(mp_obj_t self_in)101 const mp_obj_t *mp_obj_property_get(mp_obj_t self_in) {
102     mp_check_self(mp_obj_is_type(self_in, &mp_type_property));
103     mp_obj_property_t *self = MP_OBJ_TO_PTR(self_in);
104     return self->proxy;
105 }
106 
107 #endif // MICROPY_PY_BUILTINS_PROPERTY
108