LCOV - code coverage report
Current view: top level - py - objboundmeth.c (source / functions) Hit Total Coverage
Test: unix_coverage_v1.22.0-344-ge60e8079a.info Lines: 46 46 100.0 %
Date: 2024-04-26 14:58:11 Functions: 7 7 100.0 %
Branches: 12 16 75.0 %

           Branch data     Line data    Source code
       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 <string.h>
      28                 :            : 
      29                 :            : #include "py/obj.h"
      30                 :            : #include "py/runtime.h"
      31                 :            : 
      32                 :            : typedef struct _mp_obj_bound_meth_t {
      33                 :            :     mp_obj_base_t base;
      34                 :            :     mp_obj_t meth;
      35                 :            :     mp_obj_t self;
      36                 :            : } mp_obj_bound_meth_t;
      37                 :            : 
      38                 :            : #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED
      39                 :          4 : static void bound_meth_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
      40                 :          4 :     (void)kind;
      41                 :          4 :     mp_obj_bound_meth_t *o = MP_OBJ_TO_PTR(o_in);
      42                 :          4 :     mp_printf(print, "<bound_method %p ", o);
      43                 :          4 :     mp_obj_print_helper(print, o->self, PRINT_REPR);
      44                 :          4 :     mp_print_str(print, ".");
      45                 :          4 :     mp_obj_print_helper(print, o->meth, PRINT_REPR);
      46                 :          4 :     mp_print_str(print, ">");
      47                 :          4 : }
      48                 :            : #endif
      49                 :            : 
      50                 :        112 : mp_obj_t mp_call_method_self_n_kw(mp_obj_t meth, mp_obj_t self, size_t n_args, size_t n_kw, const mp_obj_t *args) {
      51                 :            :     // need to insert self before all other args and then call meth
      52                 :        112 :     size_t n_total = n_args + 2 * n_kw;
      53                 :        112 :     mp_obj_t *args2 = NULL;
      54                 :            :     #if MICROPY_ENABLE_PYSTACK
      55                 :            :     args2 = mp_pystack_alloc(sizeof(mp_obj_t) * (1 + n_total));
      56                 :            :     #else
      57                 :        112 :     mp_obj_t *free_args2 = NULL;
      58         [ +  + ]:        112 :     if (n_total > 4) {
      59                 :            :         // try to use heap to allocate temporary args array
      60                 :          4 :         args2 = m_new_maybe(mp_obj_t, 1 + n_total);
      61                 :          4 :         free_args2 = args2;
      62                 :            :     }
      63         [ -  + ]:          4 :     if (args2 == NULL) {
      64                 :            :         // (fallback to) use stack to allocate temporary args array
      65                 :        108 :         args2 = alloca(sizeof(mp_obj_t) * (1 + n_total));
      66                 :            :     }
      67                 :            :     #endif
      68                 :        112 :     args2[0] = self;
      69                 :        112 :     memcpy(args2 + 1, args, n_total * sizeof(mp_obj_t));
      70                 :        112 :     mp_obj_t res = mp_call_function_n_kw(meth, n_args + 1, n_kw, args2);
      71                 :            :     #if MICROPY_ENABLE_PYSTACK
      72                 :            :     mp_pystack_free(args2);
      73                 :            :     #else
      74         [ +  + ]:        102 :     if (free_args2 != NULL) {
      75                 :          4 :         m_del(mp_obj_t, free_args2, 1 + n_total);
      76                 :            :     }
      77                 :            :     #endif
      78                 :        102 :     return res;
      79                 :            : }
      80                 :            : 
      81                 :         84 : static mp_obj_t bound_meth_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
      82                 :         84 :     mp_obj_bound_meth_t *self = MP_OBJ_TO_PTR(self_in);
      83                 :         84 :     return mp_call_method_self_n_kw(self->meth, self->self, n_args, n_kw, args);
      84                 :            : }
      85                 :            : 
      86                 :         44 : static mp_obj_t bound_meth_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
      87                 :         44 :     mp_obj_bound_meth_t *self = MP_OBJ_TO_PTR(self_in);
      88         [ +  - ]:         44 :     switch (op) {
      89                 :         44 :         case MP_UNARY_OP_HASH:
      90                 :         44 :             return MP_OBJ_NEW_SMALL_INT((mp_uint_t)self->self ^ (mp_uint_t)self->meth);
      91                 :            :         default:
      92                 :            :             return MP_OBJ_NULL; // op not supported
      93                 :            :     }
      94                 :            : }
      95                 :            : 
      96                 :         28 : static mp_obj_t bound_meth_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
      97                 :            :     // The MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE flag is clear for this type, so if this
      98                 :            :     // function is called with MP_BINARY_OP_EQUAL then lhs_in and rhs_in must have the
      99                 :            :     // same type, which is mp_type_bound_meth.
     100         [ +  - ]:         28 :     if (op != MP_BINARY_OP_EQUAL) {
     101                 :            :         return MP_OBJ_NULL; // op not supported
     102                 :            :     }
     103                 :         28 :     mp_obj_bound_meth_t *lhs = MP_OBJ_TO_PTR(lhs_in);
     104                 :         28 :     mp_obj_bound_meth_t *rhs = MP_OBJ_TO_PTR(rhs_in);
     105   [ +  +  -  + ]:         28 :     return mp_obj_new_bool(lhs->self == rhs->self && lhs->meth == rhs->meth);
     106                 :            : }
     107                 :            : 
     108                 :            : #if MICROPY_PY_FUNCTION_ATTRS
     109                 :         28 : static void bound_meth_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
     110         [ +  + ]:         28 :     if (dest[0] != MP_OBJ_NULL) {
     111                 :            :         // not load attribute
     112                 :            :         return;
     113                 :            :     }
     114                 :            :     // Delegate the load to the method object
     115                 :         24 :     mp_obj_bound_meth_t *self = MP_OBJ_TO_PTR(self_in);
     116                 :         24 :     mp_load_method_maybe(self->meth, attr, dest);
     117                 :            : }
     118                 :            : #endif
     119                 :            : 
     120                 :            : #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED
     121                 :            : #define BOUND_METH_TYPE_PRINT print, bound_meth_print,
     122                 :            : #else
     123                 :            : #define BOUND_METH_TYPE_PRINT
     124                 :            : #endif
     125                 :            : 
     126                 :            : #if MICROPY_PY_FUNCTION_ATTRS
     127                 :            : #define BOUND_METH_TYPE_ATTR attr, bound_meth_attr,
     128                 :            : #else
     129                 :            : #define BOUND_METH_TYPE_ATTR
     130                 :            : #endif
     131                 :            : 
     132                 :            : MP_DEFINE_CONST_OBJ_TYPE(
     133                 :            :     mp_type_bound_meth,
     134                 :            :     MP_QSTR_bound_method,
     135                 :            :     MP_TYPE_FLAG_NONE,
     136                 :            :     BOUND_METH_TYPE_PRINT
     137                 :            :     BOUND_METH_TYPE_ATTR
     138                 :            :     call, bound_meth_call,
     139                 :            :     unary_op, bound_meth_unary_op,
     140                 :            :     binary_op, bound_meth_binary_op
     141                 :            :     );
     142                 :            : 
     143                 :        210 : mp_obj_t mp_obj_new_bound_meth(mp_obj_t meth, mp_obj_t self) {
     144                 :        210 :     mp_obj_bound_meth_t *o = mp_obj_malloc(mp_obj_bound_meth_t, &mp_type_bound_meth);
     145                 :        210 :     o->meth = meth;
     146                 :        210 :     o->self = self;
     147                 :        210 :     return MP_OBJ_FROM_PTR(o);
     148                 :            : }

Generated by: LCOV version 1.15-5-g462f71d