objgenerator.c 6.29 KB
Newer Older
1
2
3
4
5
6
#include <stdlib.h>
#include <assert.h>

#include "nlr.h"
#include "misc.h"
#include "mpconfig.h"
7
#include "qstr.h"
8
9
10
11
12
13
14
15
16
17
18
19
#include "obj.h"
#include "runtime.h"
#include "bc.h"

/******************************************************************************/
/* generator wrapper                                                          */

typedef struct _mp_obj_gen_wrap_t {
    mp_obj_base_t base;
    mp_obj_t *fun;
} mp_obj_gen_wrap_t;

20
STATIC mp_obj_t gen_wrap_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args) {
21
22
23
24
25
26
27
28
    mp_obj_gen_wrap_t *self = self_in;
    mp_obj_t self_fun = self->fun;
    assert(MP_OBJ_IS_TYPE(self_fun, &fun_bc_type));
    int bc_n_args;
    uint bc_n_state;
    const byte *bc_code;
    mp_obj_fun_bc_get(self_fun, &bc_n_args, &bc_n_state, &bc_code);
    if (n_args != bc_n_args) {
29
        nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "function takes %d positional arguments but %d were given", bc_n_args, n_args));
30
    }
31
    if (n_kw != 0) {
32
        nlr_jump(mp_obj_new_exception_msg(&mp_type_TypeError, "function does not take keyword arguments"));
33
    }
Damien George's avatar
Damien George committed
34

35
    return mp_obj_new_gen_instance(bc_code, bc_n_state, n_args, args);
36
37
38
}

const mp_obj_type_t gen_wrap_type = {
39
    { &mp_type_type },
40
    .name = MP_QSTR_generator,
41
    .call = gen_wrap_call,
42
43
};

44
mp_obj_t mp_obj_new_gen_wrap(mp_obj_t fun) {
45
46
47
48
49
50
51
52
53
54
55
    mp_obj_gen_wrap_t *o = m_new_obj(mp_obj_gen_wrap_t);
    o->base.type = &gen_wrap_type;
    o->fun = fun;
    return o;
}

/******************************************************************************/
/* generator instance                                                         */

typedef struct _mp_obj_gen_instance_t {
    mp_obj_base_t base;
56
    const byte *code_info;
57
58
    const byte *ip;
    mp_obj_t *sp;
59
    // bit 0 is saved currently_in_except_block value
60
    mp_exc_stack *exc_sp;
61
    uint n_state;
62
63
64
65
    // Variable-length
    mp_obj_t state[0];
    // Variable-length, never accessed by name, only as (void*)(state + n_state)
    mp_exc_stack exc_state[0];
66
67
} mp_obj_gen_instance_t;

68
void gen_instance_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
69
70
71
72
73
74
75
    print(env, "<generator object 'fun-name' at %p>", self_in);
}

mp_obj_t gen_instance_getiter(mp_obj_t self_in) {
    return self_in;
}

76
STATIC mp_obj_t gen_resume(mp_obj_t self_in, mp_obj_t send_value, mp_obj_t throw_value) {
77
    mp_obj_gen_instance_t *self = self_in;
78
79
80
    if (self->ip == 0) {
        return mp_const_stop_iteration;
    }
81
82
    if (self->sp == self->state - 1) {
        if (send_value != mp_const_none) {
83
            nlr_jump(mp_obj_new_exception_msg(&mp_type_TypeError, "can't send non-None value to a just-started generator"));
84
85
86
87
        }
    } else {
        *self->sp = send_value;
    }
88
    mp_vm_return_kind_t vm_return_kind = mp_execute_byte_code_2(self->code_info, &self->ip,
89
90
        &self->state[self->n_state - 1], &self->sp, (mp_exc_stack*)(self->state + self->n_state),
        &self->exc_sp, throw_value);
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
    switch (vm_return_kind) {
        case MP_VM_RETURN_NORMAL:
            // Explicitly mark generator as completed. If we don't do this,
            // subsequent next() may re-execute statements after last yield
            // again and again, leading to side effects.
            // TODO: check how return with value behaves under such conditions
            // in CPython.
            self->ip = 0;
            if (*self->sp == mp_const_none) {
                return mp_const_stop_iteration;
            } else {
                // TODO return StopIteration with value *self->sp
                return mp_const_stop_iteration;
            }

        case MP_VM_RETURN_YIELD:
            return *self->sp;

        case MP_VM_RETURN_EXCEPTION:
110
111
112
            self->ip = 0;
            nlr_jump(self->state[self->n_state - 1]);

113
114
115
        default:
            assert(0);
            return mp_const_none;
116
117
    }
}
118
119

mp_obj_t gen_instance_iternext(mp_obj_t self_in) {
120
    return gen_resume(self_in, mp_const_none, MP_OBJ_NULL);
121
122
}

123
STATIC mp_obj_t gen_instance_send(mp_obj_t self_in, mp_obj_t send_value) {
124
    mp_obj_t ret = gen_resume(self_in, send_value, MP_OBJ_NULL);
125
    if (ret == mp_const_stop_iteration) {
126
127
128
        nlr_jump(mp_obj_new_exception(&mp_type_StopIteration));
    } else {
        return ret;
129
    }
130
}
131

132
STATIC MP_DEFINE_CONST_FUN_OBJ_2(gen_instance_send_obj, gen_instance_send);
133

134
135
136
137
138
139
140
141
142
143
144
145
STATIC mp_obj_t gen_instance_throw(uint n_args, const mp_obj_t *args) {
    mp_obj_t ret = gen_resume(args[0], mp_const_none, n_args == 2 ? args[1] : args[2]);
    if (ret == mp_const_stop_iteration) {
        nlr_jump(mp_obj_new_exception(&mp_type_StopIteration));
    } else {
        return ret;
    }
}

STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(gen_instance_throw_obj, 2, 4, gen_instance_throw);


146
STATIC const mp_method_t gen_type_methods[] = {
147
    { "send", &gen_instance_send_obj },
148
    { "throw", &gen_instance_throw_obj },
149
150
    { NULL, NULL }, // end-of-list sentinel
};
151
152

const mp_obj_type_t gen_instance_type = {
153
    { &mp_type_type },
154
    .name = MP_QSTR_generator,
155
156
157
    .print = gen_instance_print,
    .getiter = gen_instance_getiter,
    .iternext = gen_instance_iternext,
158
    .methods = gen_type_methods,
159
160
};

161
mp_obj_t mp_obj_new_gen_instance(const byte *bytecode, uint n_state, int n_args, const mp_obj_t *args) {
162
163
    // TODO: 4 is hardcoded number from vm.c, calc exc stack size instead.
    mp_obj_gen_instance_t *o = m_new_obj_var(mp_obj_gen_instance_t, byte, n_state * sizeof(mp_obj_t) + 4 * sizeof(mp_exc_stack));
164
    o->base.type = &gen_instance_type;
165
    o->code_info = bytecode;
166
    o->ip = bytecode;
167
    o->sp = &o->state[0] - 1; // sp points to top of stack, which starts off 1 below the state
168
    o->exc_sp = (mp_exc_stack*)(o->state + n_state) - 1;
169
    o->n_state = n_state;
170

171
    // copy args to end of state array, in reverse (that's how mp_execute_byte_code_2 needs it)
172
    for (int i = 0; i < n_args; i++) {
173
        o->state[n_state - 1 - i] = args[i];
174
175
176
177
178
179
    }

    // TODO
    // prelude for making cells (closed over variables)
    // for now we just make sure there are no cells variables
    // need to work out how to implement closed over variables in generators
180
181
182
183

    // get code info size
    machine_uint_t code_info_size = bytecode[0] | (bytecode[1] << 8) | (bytecode[2] << 16) | (bytecode[3] << 24);
    o->ip += code_info_size;
184
185
186
    assert(o->ip[0] == 0);
    o->ip += 1;

187
188
    return o;
}