objgenerator.c 7.99 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
#include "obj.h"
#include "runtime.h"
#include "bc.h"
11
#include "objgenerator.h"
12
13
14
15
16
17
18
19
20

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

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

21
STATIC mp_obj_t gen_wrap_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args) {
22
23
24
25
26
27
28
29
    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) {
30
        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));
31
    }
32
    if (n_kw != 0) {
33
        nlr_jump(mp_obj_new_exception_msg(&mp_type_TypeError, "function does not take keyword arguments"));
34
    }
Damien George's avatar
Damien George committed
35

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

const mp_obj_type_t gen_wrap_type = {
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
47
48
49
50
51
52
53
54
55
56
    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;
57
    const byte *code_info;
58
59
    const byte *ip;
    mp_obj_t *sp;
60
    // bit 0 is saved currently_in_except_block value
61
    mp_exc_stack *exc_sp;
62
    uint n_state;
63
64
65
66
    // 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];
67
68
} mp_obj_gen_instance_t;

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

mp_obj_t gen_instance_getiter(mp_obj_t self_in) {
    return self_in;
}

77
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) {
78
    mp_obj_gen_instance_t *self = self_in;
79
    if (self->ip == 0) {
80
81
        *ret_val = MP_OBJ_NULL;
        return MP_VM_RETURN_NORMAL;
82
    }
83
84
    if (self->sp == self->state - 1) {
        if (send_value != mp_const_none) {
85
            nlr_jump(mp_obj_new_exception_msg(&mp_type_TypeError, "can't send non-None value to a just-started generator"));
86
87
88
89
        }
    } else {
        *self->sp = send_value;
    }
90
    mp_vm_return_kind_t ret_kind = mp_execute_byte_code_2(self->code_info, &self->ip,
91
92
        &self->state[self->n_state - 1], &self->sp, (mp_exc_stack*)(self->state + self->n_state),
        &self->exc_sp, throw_value);
93

94
    switch (ret_kind) {
95
96
97
98
99
100
101
        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;
102
103
            *ret_val = *self->sp;
            break;
104
105

        case MP_VM_RETURN_YIELD:
106
107
            *ret_val = *self->sp;
            break;
108
109
110

        case MP_VM_RETURN_EXCEPTION:
            self->ip = 0;
111
112
            *ret_val = self->state[self->n_state - 1];
            break;
113
114
115

        default:
            assert(0);
116
117
            *ret_val = mp_const_none;
            break;
118
    }
119
120

    return ret_kind;
121
122
123
}

STATIC mp_obj_t gen_resume_and_raise(mp_obj_t self_in, mp_obj_t send_value, mp_obj_t throw_value) {
124
125
    mp_obj_t ret;
    switch (mp_obj_gen_resume(self_in, send_value, throw_value, &ret)) {
126
127
128
        case MP_VM_RETURN_NORMAL:
            // Optimize return w/o value in case generator is used in for loop
            if (ret == mp_const_none) {
129
                return MP_OBJ_NULL;
130
            } else {
131
                nlr_jump(mp_obj_new_exception_args(&mp_type_StopIteration, 1, &ret));
132
133
134
            }

        case MP_VM_RETURN_YIELD:
135
            return ret;
136
137

        case MP_VM_RETURN_EXCEPTION:
138
            nlr_jump(ret);
139

140
141
142
        default:
            assert(0);
            return mp_const_none;
143
144
    }
}
145
146

mp_obj_t gen_instance_iternext(mp_obj_t self_in) {
147
    return gen_resume_and_raise(self_in, mp_const_none, MP_OBJ_NULL);
148
149
}

150
STATIC mp_obj_t gen_instance_send(mp_obj_t self_in, mp_obj_t send_value) {
151
    mp_obj_t ret = gen_resume_and_raise(self_in, send_value, MP_OBJ_NULL);
152
    if (ret == MP_OBJ_NULL) {
153
154
155
        nlr_jump(mp_obj_new_exception(&mp_type_StopIteration));
    } else {
        return ret;
156
    }
157
}
158

159
STATIC MP_DEFINE_CONST_FUN_OBJ_2(gen_instance_send_obj, gen_instance_send);
160

161
STATIC mp_obj_t gen_instance_throw(uint n_args, const mp_obj_t *args) {
162
    mp_obj_t ret = gen_resume_and_raise(args[0], mp_const_none, n_args == 2 ? args[1] : args[2]);
163
    if (ret == MP_OBJ_NULL) {
164
165
166
167
168
169
170
171
        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);

172
STATIC mp_obj_t gen_instance_close(mp_obj_t self_in) {
173
174
175
176
    mp_obj_t ret;
    switch (mp_obj_gen_resume(self_in, mp_const_none, (mp_obj_t)&mp_type_GeneratorExit, &ret)) {
        case MP_VM_RETURN_YIELD:
            nlr_jump(mp_obj_new_exception_msg(&mp_type_RuntimeError, "generator ignored GeneratorExit"));
177

178
179
180
181
182
183
184
185
186
187
188
        // 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;
            }
            nlr_jump(ret);

        default:
            // The only choice left is MP_VM_RETURN_NORMAL which is successful close
189
190
191
192
193
            return mp_const_none;
    }
}

STATIC MP_DEFINE_CONST_FUN_OBJ_1(gen_instance_close_obj, gen_instance_close);
194

195
STATIC const mp_method_t gen_type_methods[] = {
196
197
198
199
    { MP_QSTR_close, &gen_instance_close_obj },
    { MP_QSTR_send, &gen_instance_send_obj },
    { MP_QSTR_throw, &gen_instance_throw_obj },
    { MP_QSTR_NULL, NULL }, // end-of-list sentinel
200
};
201
202

const mp_obj_type_t gen_instance_type = {
203
    { &mp_type_type },
204
    .name = MP_QSTR_generator,
205
206
207
    .print = gen_instance_print,
    .getiter = gen_instance_getiter,
    .iternext = gen_instance_iternext,
208
    .methods = gen_type_methods,
209
210
};

211
mp_obj_t mp_obj_new_gen_instance(const byte *bytecode, uint n_state, int n_args, const mp_obj_t *args) {
212
213
    // 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));
214
    o->base.type = &gen_instance_type;
215
    o->code_info = bytecode;
216
    o->ip = bytecode;
217
    o->sp = &o->state[0] - 1; // sp points to top of stack, which starts off 1 below the state
218
    o->exc_sp = (mp_exc_stack*)(o->state + n_state) - 1;
219
    o->n_state = n_state;
220

221
    // copy args to end of state array, in reverse (that's how mp_execute_byte_code_2 needs it)
222
    for (int i = 0; i < n_args; i++) {
223
        o->state[n_state - 1 - i] = args[i];
224
225
226
227
228
229
    }

    // 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
230
231
232
233

    // 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;
234
235
236
    assert(o->ip[0] == 0);
    o->ip += 1;

237
238
    return o;
}