objgenerator.c 9.95 KB
Newer Older
1
2
3
#include <stdlib.h>
#include <assert.h>

4
#include "mpconfig.h"
5
6
#include "nlr.h"
#include "misc.h"
7
#include "qstr.h"
8
9
10
#include "obj.h"
#include "runtime.h"
#include "bc.h"
11
#include "objgenerator.h"
12
#include "objfun.h"
13
14
15
16
17
18
19
20
21

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

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

22
23
mp_obj_t mp_obj_new_gen_instance(mp_obj_dict_t *globals, const byte *bytecode, uint n_args, const mp_obj_t *args,
    uint n_args2, const mp_obj_t *args2);
24

25
STATIC mp_obj_t gen_wrap_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args) {
26
    mp_obj_gen_wrap_t *self = self_in;
27
    mp_obj_fun_bc_t *self_fun = (mp_obj_fun_bc_t*)self->fun;
28
    assert(MP_OBJ_IS_TYPE(self_fun, &mp_type_fun_bc));
Damien George's avatar
Damien George committed
29

30
31
    const mp_obj_t *args1, *args2;
    uint len1, len2;
32
33
34
    if (!mp_obj_fun_prepare_simple_args(self_fun, n_args, n_kw, args, &len1, &args1, &len2, &args2)) {
        assert(0);
    }
35

36
    return mp_obj_new_gen_instance(self_fun->globals, self_fun->bytecode, len1, args1, len2, args2);
37
38
}

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

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

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

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

70
void gen_instance_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
71
72
    mp_obj_gen_instance_t *self = self_in;
    print(env, "<generator object '%s' at %p>", mp_obj_code_get_name(self->code_info), self_in);
73
74
75
76
77
78
}

mp_obj_t gen_instance_getiter(mp_obj_t self_in) {
    return self_in;
}

79
mp_vm_return_kind_t mp_obj_gen_resume(mp_obj_t self_in, mp_obj_t send_value, mp_obj_t throw_value, mp_obj_t *ret_val) {
80
    assert(MP_OBJ_IS_TYPE(self_in, &mp_type_gen_instance));
81
    mp_obj_gen_instance_t *self = self_in;
82
    if (self->ip == 0) {
83
        *ret_val = MP_OBJ_STOP_ITERATION;
84
        return MP_VM_RETURN_NORMAL;
85
    }
86
87
    if (self->sp == self->state - 1) {
        if (send_value != mp_const_none) {
88
            nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "can't send non-None value to a just-started generator"));
89
90
91
92
        }
    } else {
        *self->sp = send_value;
    }
93
94
    mp_obj_dict_t *old_globals = mp_globals_get();
    mp_globals_set(self->globals);
95
    mp_vm_return_kind_t ret_kind = mp_execute_byte_code_2(self->code_info, &self->ip,
96
        &self->state[self->n_state - 1], &self->sp, (mp_exc_stack_t*)(self->state + self->n_state),
97
        &self->exc_sp, throw_value);
98
    mp_globals_set(old_globals);
99

100
    switch (ret_kind) {
101
102
103
104
105
106
107
        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;
108
109
            *ret_val = *self->sp;
            break;
110
111

        case MP_VM_RETURN_YIELD:
112
113
            *ret_val = *self->sp;
            break;
114
115
116

        case MP_VM_RETURN_EXCEPTION:
            self->ip = 0;
117
118
            *ret_val = self->state[self->n_state - 1];
            break;
119
120
121

        default:
            assert(0);
122
123
            *ret_val = mp_const_none;
            break;
124
    }
125
126

    return ret_kind;
127
128
129
}

STATIC mp_obj_t gen_resume_and_raise(mp_obj_t self_in, mp_obj_t send_value, mp_obj_t throw_value) {
130
131
    mp_obj_t ret;
    switch (mp_obj_gen_resume(self_in, send_value, throw_value, &ret)) {
132
133
        case MP_VM_RETURN_NORMAL:
            // Optimize return w/o value in case generator is used in for loop
134
135
            if (ret == mp_const_none || ret == MP_OBJ_STOP_ITERATION) {
                return MP_OBJ_STOP_ITERATION;
136
            } else {
137
                nlr_raise(mp_obj_new_exception_args(&mp_type_StopIteration, 1, &ret));
138
139
140
            }

        case MP_VM_RETURN_YIELD:
141
            if (throw_value != MP_OBJ_NULL && mp_obj_is_subclass_fast(mp_obj_get_type(throw_value), &mp_type_GeneratorExit)) {
142
                nlr_raise(mp_obj_new_exception_msg(&mp_type_RuntimeError, "generator ignored GeneratorExit"));
143
            }
144
            return ret;
145
146

        case MP_VM_RETURN_EXCEPTION:
147
            // TODO: Optimization of returning MP_OBJ_STOP_ITERATION is really part
148
            // of mp_iternext() protocol, but this function is called by other methods
149
            // too, which may not handled MP_OBJ_STOP_ITERATION.
150
            if (mp_obj_is_subclass_fast(mp_obj_get_type(ret), &mp_type_StopIteration)) {
151
                return MP_OBJ_STOP_ITERATION;
152
            } else {
153
                nlr_raise(ret);
154
            }
155

156
157
158
        default:
            assert(0);
            return mp_const_none;
159
160
    }
}
161
162

mp_obj_t gen_instance_iternext(mp_obj_t self_in) {
163
    return gen_resume_and_raise(self_in, mp_const_none, MP_OBJ_NULL);
164
165
}

166
STATIC mp_obj_t gen_instance_send(mp_obj_t self_in, mp_obj_t send_value) {
167
    mp_obj_t ret = gen_resume_and_raise(self_in, send_value, MP_OBJ_NULL);
168
    if (ret == MP_OBJ_STOP_ITERATION) {
169
        nlr_raise(mp_obj_new_exception(&mp_type_StopIteration));
170
171
    } else {
        return ret;
172
    }
173
}
174

175
STATIC MP_DEFINE_CONST_FUN_OBJ_2(gen_instance_send_obj, gen_instance_send);
176

177
STATIC mp_obj_t gen_instance_close(mp_obj_t self_in);
178
STATIC mp_obj_t gen_instance_throw(uint n_args, const mp_obj_t *args) {
179
    mp_obj_t exc = (n_args == 2) ? args[1] : args[2];
Damien George's avatar
Damien George committed
180
    exc = mp_make_raise_obj(exc);
181
182

    mp_obj_t ret = gen_resume_and_raise(args[0], mp_const_none, exc);
183
    if (ret == MP_OBJ_STOP_ITERATION) {
184
        nlr_raise(mp_obj_new_exception(&mp_type_StopIteration));
185
186
187
188
189
190
191
    } else {
        return ret;
    }
}

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

192
STATIC mp_obj_t gen_instance_close(mp_obj_t self_in) {
193
    mp_obj_t ret;
194
    switch (mp_obj_gen_resume(self_in, mp_const_none, (mp_obj_t)&mp_const_GeneratorExit_obj, &ret)) {
195
        case MP_VM_RETURN_YIELD:
196
            nlr_raise(mp_obj_new_exception_msg(&mp_type_RuntimeError, "generator ignored GeneratorExit"));
197

198
199
200
201
202
203
204
        // Swallow StopIteration & GeneratorExit (== successful close), and re-raise any other
        case MP_VM_RETURN_EXCEPTION:
            // ret should always be an instance of an exception class
            if (mp_obj_is_subclass_fast(mp_obj_get_type(ret), &mp_type_GeneratorExit) ||
                mp_obj_is_subclass_fast(mp_obj_get_type(ret), &mp_type_StopIteration)) {
                return mp_const_none;
            }
205
            nlr_raise(ret);
206
207
208

        default:
            // The only choice left is MP_VM_RETURN_NORMAL which is successful close
209
210
211
212
213
            return mp_const_none;
    }
}

STATIC MP_DEFINE_CONST_FUN_OBJ_1(gen_instance_close_obj, gen_instance_close);
214

215
216
217
218
STATIC const mp_map_elem_t gen_instance_locals_dict_table[] = {
    { MP_OBJ_NEW_QSTR(MP_QSTR_close), (mp_obj_t)&gen_instance_close_obj },
    { MP_OBJ_NEW_QSTR(MP_QSTR_send), (mp_obj_t)&gen_instance_send_obj },
    { MP_OBJ_NEW_QSTR(MP_QSTR_throw), (mp_obj_t)&gen_instance_throw_obj },
219
};
220

221
222
STATIC MP_DEFINE_CONST_DICT(gen_instance_locals_dict, gen_instance_locals_dict_table);

223
const mp_obj_type_t mp_type_gen_instance = {
224
    { &mp_type_type },
225
    .name = MP_QSTR_generator,
226
227
228
    .print = gen_instance_print,
    .getiter = gen_instance_getiter,
    .iternext = gen_instance_iternext,
229
    .locals_dict = (mp_obj_t)&gen_instance_locals_dict,
230
231
};

232
233
mp_obj_t mp_obj_new_gen_instance(mp_obj_dict_t *globals, const byte *bytecode, uint n_args, const mp_obj_t *args,
                                 uint n_args2, const mp_obj_t *args2) {
234
    const byte *code_info = bytecode;
235
236
237
238
    // get code info size, and skip the line number table
    machine_uint_t code_info_size = bytecode[0] | (bytecode[1] << 8) | (bytecode[2] << 16) | (bytecode[3] << 24);
    bytecode += code_info_size;

239
240
241
242
    // bytecode prelude: get state size and exception stack size
    machine_uint_t n_state = bytecode[0] | (bytecode[1] << 8);
    machine_uint_t n_exc_stack = bytecode[2] | (bytecode[3] << 8);
    bytecode += 4;
243

244
    // allocate the generator object, with room for local stack and exception stack
245
    mp_obj_gen_instance_t *o = m_new_obj_var(mp_obj_gen_instance_t, byte, n_state * sizeof(mp_obj_t) + n_exc_stack * sizeof(mp_exc_stack_t));
246
    o->base.type = &mp_type_gen_instance;
247
    o->globals = globals;
248
    o->code_info = code_info;
249
    o->sp = &o->state[0] - 1; // sp points to top of stack, which starts off 1 below the state
250
    o->exc_sp = (mp_exc_stack_t*)(o->state + n_state) - 1;
251
    o->n_state = n_state;
252

253
    // copy args to end of state array, in reverse (that's how mp_execute_byte_code_2 needs it)
254
    for (uint i = 0; i < n_args; i++) {
255
        o->state[n_state - 1 - i] = args[i];
256
    }
257
258
259
    for (uint i = 0; i < n_args2; i++) {
        o->state[n_state - 1 - n_args - i] = args2[i];
    }
260

261
262
263
264
265
    // set rest of state to MP_OBJ_NULL
    for (uint i = 0; i < n_state - n_args - n_args2; i++) {
        o->state[i] = MP_OBJ_NULL;
    }

266
267
268
    // bytecode prelude: initialise closed over variables
    for (uint n_local = *bytecode++; n_local > 0; n_local--) {
        uint local_num = *bytecode++;
269
        o->state[n_state - 1 - local_num] = mp_obj_new_cell(o->state[n_state - 1 - local_num]);
270
271
272
273
274
    }

    // set ip to start of actual byte code
    o->ip = bytecode;

275
276
    return o;
}