objclosure.c 3.64 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*
 * This file is part of the Micro Python project, http://micropython.org/
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2013, 2014 Damien P. George
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

Damien George's avatar
Damien George committed
27
#include <string.h>
28

29
#include "mpconfig.h"
30
31
#include "nlr.h"
#include "misc.h"
32
#include "qstr.h"
33
#include "obj.h"
34
#include "objtuple.h"
35
36
37
38
39
#include "runtime.h"

typedef struct _mp_obj_closure_t {
    mp_obj_base_t base;
    mp_obj_t fun;
40
    mp_uint_t n_closed;
41
    mp_obj_t closed[];
42
43
} mp_obj_closure_t;

44
mp_obj_t closure_call(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
Damien George's avatar
Damien George committed
45
46
    mp_obj_closure_t *self = self_in;

47
    // need to concatenate closed-over-vars and args
Damien George's avatar
Damien George committed
48

49
    int n_total = self->n_closed + n_args + 2 * n_kw;
50
51
52
    if (n_total <= 5) {
        // use stack to allocate temporary args array
        mp_obj_t args2[5];
53
54
55
        memcpy(args2, self->closed, self->n_closed * sizeof(mp_obj_t));
        memcpy(args2 + self->n_closed, args, (n_args + 2 * n_kw) * sizeof(mp_obj_t));
        return mp_call_function_n_kw(self->fun, self->n_closed + n_args, n_kw, args2);
56
57
58
    } else {
        // use heap to allocate temporary args array
        mp_obj_t *args2 = m_new(mp_obj_t, n_total);
59
60
61
        memcpy(args2, self->closed, self->n_closed * sizeof(mp_obj_t));
        memcpy(args2 + self->n_closed, args, (n_args + 2 * n_kw) * sizeof(mp_obj_t));
        mp_obj_t res = mp_call_function_n_kw(self->fun, self->n_closed + n_args, n_kw, args2);
62
63
64
        m_del(mp_obj_t, args2, n_total);
        return res;
    }
Damien George's avatar
Damien George committed
65
66
}

67
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED
68
69
STATIC void closure_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
    mp_obj_closure_t *o = o_in;
70
    print(env, "<closure %s at %p, n_closed=%u ", mp_obj_fun_get_name(o->fun), o, o->n_closed);
71
72
    for (int i = 0; i < o->n_closed; i++) {
        if (o->closed[i] == MP_OBJ_NULL) {
73
74
            print(env, "(nil)");
        } else {
75
            mp_obj_print_helper(print, env, o->closed[i], PRINT_REPR);
76
77
78
79
80
81
82
        }
        print(env, " ");
    }
    print(env, ">");
}
#endif

83
const mp_obj_type_t closure_type = {
84
    { &mp_type_type },
85
    .name = MP_QSTR_closure,
86
87
88
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED
    .print = closure_print,
#endif
89
    .call = closure_call,
90
91
};

92
93
mp_obj_t mp_obj_new_closure(mp_obj_t fun, uint n_closed_over, const mp_obj_t *closed) {
    mp_obj_closure_t *o = m_new_obj_var(mp_obj_closure_t, mp_obj_t, n_closed_over);
94
95
    o->base.type = &closure_type;
    o->fun = fun;
96
97
    o->n_closed = n_closed_over;
    memcpy(o->closed, closed, n_closed_over * sizeof(mp_obj_t));
98
99
    return o;
}