Numworks Epsilon  1.4.1
Graphing Calculator Operating System
objboundmeth.c
Go to the documentation of this file.
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 {
35  mp_obj_t self;
37 
38 #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED
39 STATIC void bound_meth_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
40  (void)kind;
42  mp_printf(print, "<bound_method %p ", o);
44  mp_print_str(print, ".");
46  mp_print_str(print, ">");
47 }
48 #endif
49 
50 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  size_t n_total = n_args + 2 * n_kw;
53  mp_obj_t *args2 = NULL;
54  mp_obj_t *free_args2 = NULL;
55  if (n_total > 4) {
56  // try to use heap to allocate temporary args array
57  args2 = m_new_maybe(mp_obj_t, 1 + n_total);
58  free_args2 = args2;
59  }
60  if (args2 == NULL) {
61  // (fallback to) use stack to allocate temporary args array
62  args2 = alloca(sizeof(mp_obj_t) * (1 + n_total));
63  }
64  args2[0] = self;
65  memcpy(args2 + 1, args, n_total * sizeof(mp_obj_t));
66  mp_obj_t res = mp_call_function_n_kw(meth, n_args + 1, n_kw, args2);
67  if (free_args2 != NULL) {
68  m_del(mp_obj_t, free_args2, 1 + n_total);
69  }
70  return res;
71 }
72 
73 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) {
74  mp_obj_bound_meth_t *self = MP_OBJ_TO_PTR(self_in);
75  return mp_call_method_self_n_kw(self->meth, self->self, n_args, n_kw, args);
76 }
77 
78 #if MICROPY_PY_FUNCTION_ATTRS
79 STATIC void bound_meth_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
80  if (dest[0] != MP_OBJ_NULL) {
81  // not load attribute
82  return;
83  }
84  if (attr == MP_QSTR___name__) {
85  mp_obj_bound_meth_t *o = MP_OBJ_TO_PTR(self_in);
87  }
88 }
89 #endif
90 
92  { &mp_type_type },
93  .name = MP_QSTR_bound_method,
94 #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED
95  .print = bound_meth_print,
96 #endif
97  .call = bound_meth_call,
98 #if MICROPY_PY_FUNCTION_ATTRS
99  .attr = bound_meth_attr,
100 #endif
101 };
102 
105  o->base.type = &mp_type_bound_meth;
106  o->meth = meth;
107  o->self = self;
108  return MP_OBJ_FROM_PTR(o);
109 }
STATIC const uint8_t attr[]
Definition: unicode.c:51
#define m_del(type, ptr, num)
Definition: misc.h:77
int mp_print_str(const mp_print_t *print, const char *str)
Definition: mpprint.c:53
STATIC void bound_meth_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind)
Definition: objboundmeth.c:39
#define MP_OBJ_FROM_PTR(p)
Definition: obj.h:233
#define MP_OBJ_NEW_QSTR(qst)
Definition: obj.h:92
struct _mp_obj_bound_meth_t mp_obj_bound_meth_t
#define STATIC
Definition: mpconfig.h:1178
mp_obj_base_t base
Definition: objboundmeth.c:33
mp_print_kind_t
Definition: obj.h:412
#define m_new_maybe(type, num)
Definition: misc.h:58
qstr mp_obj_fun_get_name(mp_const_obj_t fun)
Definition: objfun.c:154
#define NULL
Definition: stddef.h:4
#define MP_OBJ_NULL
Definition: obj.h:73
void mp_obj_print_helper(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind)
Definition: obj.c:59
size_t qstr
Definition: qstr.h:48
args
Definition: i18n.py:175
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)
Definition: objboundmeth.c:73
const mp_obj_type_t mp_type_type
Definition: objtype.c:969
#define MP_OBJ_TO_PTR(o)
Definition: obj.h:228
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)
Definition: objboundmeth.c:50
#define alloca(size)
Definition: alloca.h:4
qstr name
Definition: obj.h:478
STATIC const mp_obj_type_t mp_type_bound_meth
Definition: objboundmeth.c:91
uint64_t mp_obj_t
Definition: obj.h:39
int mp_printf(const mp_print_t *print, const char *fmt,...)
Definition: mpprint.c:380
mp_obj_t mp_call_function_n_kw(mp_obj_t fun_in, size_t n_args, size_t n_kw, const mp_obj_t *args)
Definition: runtime.c:615
#define m_new_obj(type)
Definition: misc.h:60
void * memcpy(void *dst, const void *src, size_t n)
Definition: memcpy.c:3
mp_obj_t mp_obj_new_bound_meth(mp_obj_t meth, mp_obj_t self)
Definition: objboundmeth.c:103