blob: 75b9d1c568e8d65851b1b7b9b66865b849cf26bd [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python 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
Damien George66eaf842014-03-26 19:27:58 +000027#include <stdlib.h>
Damien George7c9c6672014-01-25 00:17:36 +000028
Paul Sokolovskyf54bcbf2014-05-02 17:47:01 +030029#include "mpconfig.h"
Damien George7c9c6672014-01-25 00:17:36 +000030#include "nlr.h"
31#include "misc.h"
Damien George7c9c6672014-01-25 00:17:36 +000032#include "qstr.h"
33#include "obj.h"
34#include "runtime.h"
35
36// this is a wrapper object that is turns something that has a __getitem__ method into an iterator
37
38typedef struct _mp_obj_getitem_iter_t {
39 mp_obj_base_t base;
40 mp_obj_t args[3];
41} mp_obj_getitem_iter_t;
42
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020043STATIC mp_obj_t it_iternext(mp_obj_t self_in) {
Damien George7c9c6672014-01-25 00:17:36 +000044 mp_obj_getitem_iter_t *self = self_in;
45 nlr_buf_t nlr;
46 if (nlr_push(&nlr) == 0) {
47 // try to get next item
Damien Georged17926d2014-03-30 13:35:08 +010048 mp_obj_t value = mp_call_method_n_kw(1, 0, self->args);
Damien George7c9c6672014-01-25 00:17:36 +000049 self->args[2] = MP_OBJ_NEW_SMALL_INT(MP_OBJ_SMALL_INT_VALUE(self->args[2]) + 1);
50 nlr_pop();
51 return value;
52 } else {
53 // an exception was raised
Damien Georgec5966122014-02-15 16:10:44 +000054 if (mp_obj_get_type(nlr.ret_val) == &mp_type_StopIteration) {
Damien Georgeea8d06c2014-04-17 23:19:36 +010055 // return MP_OBJ_STOP_ITERATION instead of raising StopIteration
56 return MP_OBJ_STOP_ITERATION;
Damien George7c9c6672014-01-25 00:17:36 +000057 } else {
58 // re-raise exception
Damien Georgeea13f402014-04-05 18:32:08 +010059 nlr_raise(nlr.ret_val);
Damien George7c9c6672014-01-25 00:17:36 +000060 }
61 }
62}
63
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020064STATIC const mp_obj_type_t it_type = {
Damien Georgec5966122014-02-15 16:10:44 +000065 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +000066 .name = MP_QSTR_iterator,
Paul Sokolovskyf7eaf602014-03-30 22:00:12 +030067 .getiter = mp_identity,
Damien George7c9c6672014-01-25 00:17:36 +000068 .iternext = it_iternext
69};
70
Damien Georged17926d2014-03-30 13:35:08 +010071// args are those returned from mp_load_method_maybe (ie either an attribute or a method)
Damien George7c9c6672014-01-25 00:17:36 +000072mp_obj_t mp_obj_new_getitem_iter(mp_obj_t *args) {
73 mp_obj_getitem_iter_t *o = m_new_obj(mp_obj_getitem_iter_t);
74 o->base.type = &it_type;
75 o->args[0] = args[0];
76 o->args[1] = args[1];
77 o->args[2] = MP_OBJ_NEW_SMALL_INT(0);
78 return o;
79}