obj.c 8.64 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"

17
mp_obj_type_t *mp_obj_get_type(mp_obj_t o_in) {
Damien George's avatar
Damien George committed
18
19
    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
const char *mp_obj_get_type_str(mp_obj_t o_in) {
29
    return qstr_str(mp_obj_get_type(o_in)->name);
30
31
32
33
34
35
36
37
38
}

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

39
void mp_obj_print_helper(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
40
41
42
    mp_obj_type_t *type = mp_obj_get_type(o_in);
    if (type->print != NULL) {
        type->print(print, env, o_in, kind);
43
    } else {
44
        print(env, "<%s>", qstr_str(type->name));
45
46
47
    }
}

48
49
void mp_obj_print(mp_obj_t o_in, mp_print_kind_t kind) {
    mp_obj_print_helper(printf_wrapper, NULL, o_in, kind);
50
51
}

52
53
// helper function to print an exception with traceback
void mp_obj_print_exception(mp_obj_t exc) {
54
    if (mp_obj_is_exception_instance(exc)) {
55
56
57
58
59
        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) {
60
#if MICROPY_ENABLE_SOURCE_LINE
61
                printf("  File \"%s\", line %d, in %s\n", qstr_str(values[i]), (int)values[i + 1], qstr_str(values[i + 2]));
62
63
64
#else
                printf("  File \"%s\", in %s\n", qstr_str(values[i]), qstr_str(values[i + 2]));
#endif
65
66
67
68
69
70
71
            }
        }
    }
    mp_obj_print(exc, PRINT_REPR);
    printf("\n");
}

72
bool mp_obj_is_callable(mp_obj_t o_in) {
73
    return mp_obj_get_type(o_in)->call != NULL;
74
75
}

76
77
machine_int_t mp_obj_hash(mp_obj_t o_in) {
    if (o_in == mp_const_false) {
78
        return 0; // needs to hash to same as the integer 0, since False==0
79
    } else if (o_in == mp_const_true) {
80
        return 1; // needs to hash to same as the integer 1, since True==1
81
82
    } else if (MP_OBJ_IS_SMALL_INT(o_in)) {
        return MP_OBJ_SMALL_INT_VALUE(o_in);
83
84
    } else if (MP_OBJ_IS_STR(o_in)) {
        return mp_obj_str_get_hash(o_in);
85
    } else if (MP_OBJ_IS_TYPE(o_in, &none_type)) {
86
87
88
89
90
91
92
93
94
95
96
97
98
        return (machine_int_t)o_in;
    } 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
99
bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2) {
100
101
    if (o1 == o2) {
        return true;
102
103
    } 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)) {
104
105
            return false;
        } else {
106
107
            if (MP_OBJ_IS_SMALL_INT(o2)) {
                mp_obj_t temp = o1; o1 = o2; o2 = temp;
108
109
            }
            // o1 is the SMALL_INT, o2 is not
110
111
            mp_small_int_t val = MP_OBJ_SMALL_INT_VALUE(o1);
            if (o2 == mp_const_false) {
112
                return val == 0;
113
            } else if (o2 == mp_const_true) {
114
                return val == 1;
115
116
117
118
            } 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) {
119
                    mp_obj_t r = o->type->binary_op(RT_BINARY_OP_EQUAL, o2, o1);
120
121
                    return r == mp_const_true ? true : false;
                }
122
            }
123
            return false;
124
        }
125
126
    } else if (MP_OBJ_IS_STR(o1) && MP_OBJ_IS_STR(o2)) {
        return mp_obj_str_equal(o1, o2);
127
    } else {
128
129
        mp_obj_base_t *o = o1;
        if (o->type->binary_op != NULL) {
130
            mp_obj_t r = o->type->binary_op(RT_BINARY_OP_EQUAL, o1, o2);
131
132
133
134
            if (r != MP_OBJ_NULL) {
                return r == mp_const_true ? true : false;
            }
        }
135

136
        nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_NotImplementedError,
137
            "Equality for '%s' and '%s' types not yet implemented", mp_obj_get_type_str(o1), mp_obj_get_type_str(o2)));
138
139
140
141
        return false;
    }
}

142
143
144
145
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);
146
147
148
149
150
151
152
        return i1 < i2;
    } else {
        assert(0);
        return false;
    }
}

153
154
machine_int_t mp_obj_get_int(mp_obj_t arg) {
    if (arg == mp_const_false) {
155
        return 0;
156
    } else if (arg == mp_const_true) {
157
        return 1;
158
159
    } else if (MP_OBJ_IS_SMALL_INT(arg)) {
        return MP_OBJ_SMALL_INT_VALUE(arg);
160
161
    } else if (MP_OBJ_IS_TYPE(arg, &int_type)) {
        return mp_obj_int_get_checked(arg);
162
    } else {
163
        nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "can't convert %s to int", mp_obj_get_type_str(arg)));
164
165
166
167
    }
}

#if MICROPY_ENABLE_FLOAT
168
mp_float_t mp_obj_get_float(mp_obj_t arg) {
169
    if (arg == mp_const_false) {
170
        return 0;
171
    } else if (arg == mp_const_true) {
172
        return 1;
173
174
    } else if (MP_OBJ_IS_SMALL_INT(arg)) {
        return MP_OBJ_SMALL_INT_VALUE(arg);
175
    } else if (MP_OBJ_IS_TYPE(arg, &mp_type_float)) {
176
        return mp_obj_float_get(arg);
177
    } else {
178
        nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "can't convert %s to float", mp_obj_get_type_str(arg)));
179
180
181
    }
}

182
183
void mp_obj_get_complex(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) {
    if (arg == mp_const_false) {
184
185
        *real = 0;
        *imag = 0;
186
    } else if (arg == mp_const_true) {
187
188
        *real = 1;
        *imag = 0;
189
190
    } else if (MP_OBJ_IS_SMALL_INT(arg)) {
        *real = MP_OBJ_SMALL_INT_VALUE(arg);
191
        *imag = 0;
192
    } else if (MP_OBJ_IS_TYPE(arg, &mp_type_float)) {
193
        *real = mp_obj_float_get(arg);
194
        *imag = 0;
195
    } else if (MP_OBJ_IS_TYPE(arg, &mp_type_complex)) {
196
        mp_obj_complex_get(arg, real, imag);
197
    } else {
198
        nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "can't convert %s to complex", mp_obj_get_type_str(arg)));
199
200
201
202
    }
}
#endif

203
204
205
206
207
208
209
210
211
212
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) {
213
            nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_IndexError, "requested length %d but object has length %d", n, seq_len));
214
        }
215
        return seq_items;
216
    } else {
217
        nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "object '%s' is not a tuple or list", mp_obj_get_type_str(o_in)));
218
219
220
    }
}

221
222
223
// is_slice determines whether the index is a slice index
uint mp_get_index(const mp_obj_type_t *type, machine_uint_t len, mp_obj_t index, bool is_slice) {
    int i;
224
    if (MP_OBJ_IS_SMALL_INT(index)) {
225
226
227
        i = MP_OBJ_SMALL_INT_VALUE(index);
    } else if (MP_OBJ_IS_TYPE(index, &bool_type)) {
	i = index == mp_const_true ? 1 : 0;
228
    } else {
229
        nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "%s indices must be integers, not %s", qstr_str(type->name), mp_obj_get_type_str(index)));
230
    }
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246

    if (i < 0) {
	i += len;
    }
    if (is_slice) {
	if (i < 0) {
	    i = 0;
	} else if (i > len) {
	    i = len;
	}
    } else {
	if (i < 0 || i >= len) {
	    nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_IndexError, "%s index out of range", qstr_str(type->name)));
	}
    }
    return i;
247
}
248

249
// may return MP_OBJ_NULL
250
mp_obj_t mp_obj_len_maybe(mp_obj_t o_in) {
251
    if (MP_OBJ_IS_STR(o_in)) {
252
        return MP_OBJ_NEW_SMALL_INT((machine_int_t)mp_obj_str_get_len(o_in));
253
    } else {
254
255
        mp_obj_type_t *type = mp_obj_get_type(o_in);
        if (type->unary_op != NULL) {
256
257
258
            return type->unary_op(RT_UNARY_OP_LEN, o_in);
        } else {
            return MP_OBJ_NULL;
259
        }
260
261
    }
}
262
263
264
265
266
267

// 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;
}
268
MP_DEFINE_CONST_FUN_OBJ_1(mp_identity_obj, mp_identity);