obj.c 9.75 KB
Newer Older
1
2
3
4
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
5
#include <string.h>
6
7
8
9
#include <assert.h>

#include "nlr.h"
#include "misc.h"
10
#include "mpconfig.h"
11
#include "qstr.h"
12
13
#include "obj.h"
#include "runtime0.h"
14
15
16
#include "runtime.h"
#include "map.h"

Damien George's avatar
Damien George committed
17
18
19
mp_obj_t mp_obj_get_type(mp_obj_t o_in) {
    if (MP_OBJ_IS_SMALL_INT(o_in)) {
        return (mp_obj_t)&int_type;
20
21
    } else if (MP_OBJ_IS_QSTR(o_in)) {
        return (mp_obj_t)&str_type;
Damien George's avatar
Damien George committed
22
23
24
25
26
27
    } else {
        mp_obj_base_t *o = o_in;
        return (mp_obj_t)o->type;
    }
}

28
29
const char *mp_obj_get_type_str(mp_obj_t o_in) {
    if (MP_OBJ_IS_SMALL_INT(o_in)) {
30
        return "int";
31
32
    } else if (MP_OBJ_IS_QSTR(o_in)) {
        return "str";
33
    } else {
34
35
        mp_obj_base_t *o = o_in;
        return o->type->name;
36
37
38
39
40
41
42
43
44
45
    }
}

void printf_wrapper(void *env, const char *fmt, ...) {
    va_list args;
    va_start(args, fmt);
    vprintf(fmt, args);
    va_end(args);
}

46
void mp_obj_print_helper(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
47
48
    if (MP_OBJ_IS_SMALL_INT(o_in)) {
        print(env, "%d", (int)MP_OBJ_SMALL_INT_VALUE(o_in));
49
50
    } else if (MP_OBJ_IS_QSTR(o_in)) {
        mp_obj_str_print_qstr(print, env, MP_OBJ_QSTR_VALUE(o_in), kind);
51
    } else {
52
53
        mp_obj_base_t *o = o_in;
        if (o->type->print != NULL) {
54
            o->type->print(print, env, o_in, kind);
55
56
        } else {
            print(env, "<%s>", o->type->name);
57
58
59
60
        }
    }
}

61
62
void mp_obj_print(mp_obj_t o_in, mp_print_kind_t kind) {
    mp_obj_print_helper(printf_wrapper, NULL, o_in, kind);
63
64
}

65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// helper function to print an exception with traceback
void mp_obj_print_exception(mp_obj_t exc) {
    if (MP_OBJ_IS_TYPE(exc, &exception_type)) {
        machine_uint_t n, *values;
        mp_obj_exception_get_traceback(exc, &n, &values);
        if (n > 0) {
            printf("Traceback (most recent call last):\n");
            for (int i = n - 3; i >= 0; i -= 3) {
                printf("  File \"%s\", line %d, in %s\n", qstr_str(values[i]), (int)values[i + 1], qstr_str(values[i + 2]));
            }
        }
    }
    mp_obj_print(exc, PRINT_REPR);
    printf("\n");
}

81
82
bool mp_obj_is_callable(mp_obj_t o_in) {
    if (MP_OBJ_IS_SMALL_INT(o_in)) {
83
84
        return false;
    } else {
85
        mp_obj_base_t *o = o_in;
86
        return o->type->call != NULL;
87
88
89
    }
}

90
91
machine_int_t mp_obj_hash(mp_obj_t o_in) {
    if (o_in == mp_const_false) {
92
        return 0; // needs to hash to same as the integer 0, since False==0
93
    } else if (o_in == mp_const_true) {
94
        return 1; // needs to hash to same as the integer 1, since True==1
95
96
    } else if (MP_OBJ_IS_SMALL_INT(o_in)) {
        return MP_OBJ_SMALL_INT_VALUE(o_in);
97
98
    } else if (MP_OBJ_IS_QSTR(o_in)) {
        return MP_OBJ_QSTR_VALUE(o_in);
99
    } else if (MP_OBJ_IS_TYPE(o_in, &none_type)) {
100
        return (machine_int_t)o_in;
101
102
    } else if (MP_OBJ_IS_TYPE(o_in, &str_type)) {
        return mp_obj_str_get(o_in);
103
104
105
106
107
108
109
110
111
112
113
114
    } else {
        assert(0);
        return 0;
    }
}

// this function implements the '==' operator (and so the inverse of '!=')
// from the python language reference:
// "The objects need not have the same type. If both are numbers, they are converted
// to a common type. Otherwise, the == and != operators always consider objects of
// different types to be unequal."
// note also that False==0 and True==1 are true expressions
115
bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2) {
116
117
    if (o1 == o2) {
        return true;
118
119
    } else if (MP_OBJ_IS_SMALL_INT(o1) || MP_OBJ_IS_SMALL_INT(o2)) {
        if (MP_OBJ_IS_SMALL_INT(o1) && MP_OBJ_IS_SMALL_INT(o2)) {
120
121
            return false;
        } else {
122
123
            if (MP_OBJ_IS_SMALL_INT(o2)) {
                mp_obj_t temp = o1; o1 = o2; o2 = temp;
124
125
            }
            // o1 is the SMALL_INT, o2 is not
126
127
            mp_small_int_t val = MP_OBJ_SMALL_INT_VALUE(o1);
            if (o2 == mp_const_false) {
128
                return val == 0;
129
            } else if (o2 == mp_const_true) {
130
                return val == 1;
131
132
133
134
135
136
137
            } else if (MP_OBJ_IS_TYPE(o2, &int_type)) {
                // If o2 is long int, dispatch to its virtual methods
                mp_obj_base_t *o = o2;
                if (o->type->binary_op != NULL) {
                    mp_obj_t r = o->type->binary_op(RT_COMPARE_OP_EQUAL, o2, o1);
                    return r == mp_const_true ? true : false;
                }
138
            }
139
            return false;
140
        }
141
142
    } else if (MP_OBJ_IS_QSTR(o1) || MP_OBJ_IS_QSTR(o2)) {
        return false;
143
144
    } else if (MP_OBJ_IS_TYPE(o1, &str_type) && MP_OBJ_IS_TYPE(o2, &str_type)) {
        return mp_obj_str_get(o1) == mp_obj_str_get(o2);
145
    } else {
146
147
148
149
150
151
152
        mp_obj_base_t *o = o1;
        if (o->type->binary_op != NULL) {
            mp_obj_t r = o->type->binary_op(RT_COMPARE_OP_EQUAL, o1, o2);
            if (r != MP_OBJ_NULL) {
                return r == mp_const_true ? true : false;
            }
        }
153
154
        // TODO: Debugging helper
        printf("Equality for '%s' and '%s' types not yet implemented\n", mp_obj_get_type_str(o1), mp_obj_get_type_str(o2));
155
156
157
158
159
        assert(0);
        return false;
    }
}

160
161
162
163
bool mp_obj_less(mp_obj_t o1, mp_obj_t o2) {
    if (MP_OBJ_IS_SMALL_INT(o1) && MP_OBJ_IS_SMALL_INT(o2)) {
        mp_small_int_t i1 = MP_OBJ_SMALL_INT_VALUE(o1);
        mp_small_int_t i2 = MP_OBJ_SMALL_INT_VALUE(o2);
164
165
166
167
168
169
170
        return i1 < i2;
    } else {
        assert(0);
        return false;
    }
}

171
172
machine_int_t mp_obj_get_int(mp_obj_t arg) {
    if (arg == mp_const_false) {
173
        return 0;
174
    } else if (arg == mp_const_true) {
175
        return 1;
176
177
    } else if (MP_OBJ_IS_SMALL_INT(arg)) {
        return MP_OBJ_SMALL_INT_VALUE(arg);
178
179
    } else if (MP_OBJ_IS_TYPE(arg, &int_type)) {
        return mp_obj_int_get_checked(arg);
180
181
182
183
184
#if MICROPY_ENABLE_FLOAT
    } else if (MP_OBJ_IS_TYPE(arg, &float_type)) {
        // TODO work out if this should be floor, ceil or trunc
        return (machine_int_t)mp_obj_float_get(arg);
#endif
185
    } else {
186
        nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "can't convert %s to int", mp_obj_get_type_str(arg)));
187
188
189
190
    }
}

#if MICROPY_ENABLE_FLOAT
191
192
machine_float_t mp_obj_get_float(mp_obj_t arg) {
    if (arg == mp_const_false) {
193
        return 0;
194
    } else if (arg == mp_const_true) {
195
        return 1;
196
197
198
199
    } else if (MP_OBJ_IS_SMALL_INT(arg)) {
        return MP_OBJ_SMALL_INT_VALUE(arg);
    } else if (MP_OBJ_IS_TYPE(arg, &float_type)) {
        return mp_obj_float_get(arg);
200
    } else {
201
        nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "can't convert %s to float", mp_obj_get_type_str(arg)));
202
203
204
    }
}

205
206
void mp_obj_get_complex(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) {
    if (arg == mp_const_false) {
207
208
        *real = 0;
        *imag = 0;
209
    } else if (arg == mp_const_true) {
210
211
        *real = 1;
        *imag = 0;
212
213
    } else if (MP_OBJ_IS_SMALL_INT(arg)) {
        *real = MP_OBJ_SMALL_INT_VALUE(arg);
214
        *imag = 0;
215
216
    } else if (MP_OBJ_IS_TYPE(arg, &float_type)) {
        *real = mp_obj_float_get(arg);
217
        *imag = 0;
218
219
    } else if (MP_OBJ_IS_TYPE(arg, &complex_type)) {
        mp_obj_complex_get(arg, real, imag);
220
    } else {
221
        nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "can't convert %s to complex", mp_obj_get_type_str(arg)));
222
223
224
225
    }
}
#endif

226
qstr mp_obj_get_qstr(mp_obj_t arg) {
227
228
229
    if (MP_OBJ_IS_QSTR(arg)) {
        return MP_OBJ_QSTR_VALUE(arg);
    } else if (MP_OBJ_IS_TYPE(arg, &str_type)) {
230
        return mp_obj_str_get(arg);
231
232
233
234
235
236
    } else {
        assert(0);
        return 0;
    }
}

237
238
239
240
241
242
243
244
245
246
mp_obj_t *mp_obj_get_array_fixed_n(mp_obj_t o_in, machine_int_t n) {
    if (MP_OBJ_IS_TYPE(o_in, &tuple_type) || MP_OBJ_IS_TYPE(o_in, &list_type)) {
        uint seq_len;
        mp_obj_t *seq_items;
        if (MP_OBJ_IS_TYPE(o_in, &tuple_type)) {
            mp_obj_tuple_get(o_in, &seq_len, &seq_items);
        } else {
            mp_obj_list_get(o_in, &seq_len, &seq_items);
        }
        if (seq_len != n) {
247
            nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_IndexError, "requested length %d but object has length %d", n, seq_len));
248
        }
249
        return seq_items;
250
    } else {
251
        nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "object '%s' is not a tuple or list", mp_obj_get_type_str(o_in)));
252
253
254
    }
}

255
256
257
258
259
260
261
262
uint mp_get_index(const mp_obj_type_t *type, machine_uint_t len, mp_obj_t index) {
    // TODO False and True are considered 0 and 1 for indexing purposes
    if (MP_OBJ_IS_SMALL_INT(index)) {
        int i = MP_OBJ_SMALL_INT_VALUE(index);
        if (i < 0) {
            i += len;
        }
        if (i < 0 || i >= len) {
263
            nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_IndexError, "%s index out of range", type->name));
264
265
266
        }
        return i;
    } else {
267
        nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "%s indices must be integers, not %s", type->name, mp_obj_get_type_str(index)));
268
269
    }
}
270

271
// may return MP_OBJ_NULL
272
273
274
mp_obj_t mp_obj_len_maybe(mp_obj_t o_in) {
    mp_small_int_t len = 0;
    if (MP_OBJ_IS_TYPE(o_in, &str_type)) {
275
        len = qstr_len(mp_obj_str_get(o_in));
276
277
278
279
280
281
282
283
284
285
286
287
    } else if (MP_OBJ_IS_TYPE(o_in, &tuple_type)) {
        uint seq_len;
        mp_obj_t *seq_items;
        mp_obj_tuple_get(o_in, &seq_len, &seq_items);
        len = seq_len;
    } else if (MP_OBJ_IS_TYPE(o_in, &list_type)) {
        uint seq_len;
        mp_obj_t *seq_items;
        mp_obj_list_get(o_in, &seq_len, &seq_items);
        len = seq_len;
    } else if (MP_OBJ_IS_TYPE(o_in, &dict_type)) {
        len = mp_obj_dict_len(o_in);
288
289
    } else if (MP_OBJ_IS_TYPE(o_in, &array_type)) {
        len = mp_obj_array_len(o_in);
290
    } else {
291
        return MP_OBJ_NULL;
292
293
294
    }
    return MP_OBJ_NEW_SMALL_INT(len);
}
295
296
297
298
299
300

// Return input argument. Useful as .getiter for objects which are
// their own iterators, etc.
mp_obj_t mp_identity(mp_obj_t self) {
    return self;
}