objclosure.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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_closure_t {
  30. mp_obj_base_t base;
  31. mp_obj_t fun;
  32. size_t n_closed;
  33. mp_obj_t closed[];
  34. } mp_obj_closure_t;
  35. STATIC mp_obj_t closure_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  36. mp_obj_closure_t *self = MP_OBJ_TO_PTR(self_in);
  37. // need to concatenate closed-over-vars and args
  38. size_t n_total = self->n_closed + n_args + 2 * n_kw;
  39. if (n_total <= 5) {
  40. // use stack to allocate temporary args array
  41. mp_obj_t args2[5];
  42. memcpy(args2, self->closed, self->n_closed * sizeof(mp_obj_t));
  43. memcpy(args2 + self->n_closed, args, (n_args + 2 * n_kw) * sizeof(mp_obj_t));
  44. return mp_call_function_n_kw(self->fun, self->n_closed + n_args, n_kw, args2);
  45. } else {
  46. // use heap to allocate temporary args array
  47. mp_obj_t *args2 = m_new(mp_obj_t, n_total);
  48. memcpy(args2, self->closed, self->n_closed * sizeof(mp_obj_t));
  49. memcpy(args2 + self->n_closed, args, (n_args + 2 * n_kw) * sizeof(mp_obj_t));
  50. mp_obj_t res = mp_call_function_n_kw(self->fun, self->n_closed + n_args, n_kw, args2);
  51. m_del(mp_obj_t, args2, n_total);
  52. return res;
  53. }
  54. }
  55. #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED
  56. STATIC void closure_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
  57. (void)kind;
  58. mp_obj_closure_t *o = MP_OBJ_TO_PTR(o_in);
  59. mp_print_str(print, "<closure ");
  60. mp_obj_print_helper(print, o->fun, PRINT_REPR);
  61. mp_printf(print, " at %p, n_closed=%u ", o, (int)o->n_closed);
  62. for (size_t i = 0; i < o->n_closed; i++) {
  63. if (o->closed[i] == MP_OBJ_NULL) {
  64. mp_print_str(print, "(nil)");
  65. } else {
  66. mp_obj_print_helper(print, o->closed[i], PRINT_REPR);
  67. }
  68. mp_print_str(print, " ");
  69. }
  70. mp_print_str(print, ">");
  71. }
  72. #endif
  73. #if MICROPY_PY_FUNCTION_ATTRS
  74. STATIC void mp_obj_closure_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
  75. // forward to self_in->fun
  76. mp_obj_closure_t *o = MP_OBJ_TO_PTR(self_in);
  77. mp_load_method_maybe(o->fun, attr, dest);
  78. }
  79. #define CLOSURE_TYPE_ATTR attr, mp_obj_closure_attr,
  80. #else
  81. #define CLOSURE_TYPE_ATTR
  82. #endif
  83. #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED
  84. #define CLOSURE_TYPE_PRINT print, closure_print,
  85. #else
  86. #define CLOSURE_TYPE_PRINT
  87. #endif
  88. MP_DEFINE_CONST_OBJ_TYPE(
  89. mp_type_closure,
  90. MP_QSTR_closure,
  91. MP_TYPE_FLAG_BINDS_SELF,
  92. CLOSURE_TYPE_ATTR
  93. CLOSURE_TYPE_PRINT
  94. call, closure_call
  95. );
  96. mp_obj_t mp_obj_new_closure(mp_obj_t fun, size_t n_closed_over, const mp_obj_t *closed) {
  97. mp_obj_closure_t *o = mp_obj_malloc_var(mp_obj_closure_t, mp_obj_t, n_closed_over, &mp_type_closure);
  98. o->fun = fun;
  99. o->n_closed = n_closed_over;
  100. memcpy(o->closed, closed, n_closed_over * sizeof(mp_obj_t));
  101. return MP_OBJ_FROM_PTR(o);
  102. }