objboundmeth.c 5.1 KB

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