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

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

Damien George's avatar
Damien George committed
16
17
18
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;
19
20
    } else if (MP_OBJ_IS_QSTR(o_in)) {
        return (mp_obj_t)&str_type;
Damien George's avatar
Damien George committed
21
22
23
24
25
26
    } else {
        mp_obj_base_t *o = o_in;
        return (mp_obj_t)o->type;
    }
}

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

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

43
44
45
void mp_obj_print_helper(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in) {
    if (MP_OBJ_IS_SMALL_INT(o_in)) {
        print(env, "%d", (int)MP_OBJ_SMALL_INT_VALUE(o_in));
46
    } else {
47
48
49
50
51
        mp_obj_base_t *o = o_in;
        if (o->type->print != NULL) {
            o->type->print(print, env, o_in);
        } else {
            print(env, "<%s>", o->type->name);
52
53
54
55
        }
    }
}

56
57
void mp_obj_print(mp_obj_t o_in) {
    mp_obj_print_helper(printf_wrapper, NULL, o_in);
58
59
}

60
61
bool mp_obj_is_callable(mp_obj_t o_in) {
    if (MP_OBJ_IS_SMALL_INT(o_in)) {
62
63
        return false;
    } else {
64
65
        mp_obj_base_t *o = o_in;
        return o->type->call_n != NULL;
66
67
68
    }
}

69
70
machine_int_t mp_obj_hash(mp_obj_t o_in) {
    if (o_in == mp_const_false) {
71
        return 0; // needs to hash to same as the integer 0, since False==0
72
    } else if (o_in == mp_const_true) {
73
        return 1; // needs to hash to same as the integer 1, since True==1
74
75
    } else if (MP_OBJ_IS_SMALL_INT(o_in)) {
        return MP_OBJ_SMALL_INT_VALUE(o_in);
76
77
    } else if (MP_OBJ_IS_QSTR(o_in)) {
        return MP_OBJ_QSTR_VALUE(o_in);
78
    } else if (MP_OBJ_IS_TYPE(o_in, &none_type)) {
79
        return (machine_int_t)o_in;
80
81
    } else if (MP_OBJ_IS_TYPE(o_in, &str_type)) {
        return mp_obj_str_get(o_in);
82
83
84
85
86
87
88
89
90
91
92
93
    } 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
94
bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2) {
95
96
    if (o1 == o2) {
        return true;
97
98
    } 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)) {
99
100
            return false;
        } else {
101
102
            if (MP_OBJ_IS_SMALL_INT(o2)) {
                mp_obj_t temp = o1; o1 = o2; o2 = temp;
103
104
            }
            // o1 is the SMALL_INT, o2 is not
105
106
            mp_small_int_t val = MP_OBJ_SMALL_INT_VALUE(o1);
            if (o2 == mp_const_false) {
107
                return val == 0;
108
            } else if (o2 == mp_const_true) {
109
110
111
112
113
                return val == 1;
            } else {
                return false;
            }
        }
114
115
    } else if (MP_OBJ_IS_QSTR(o1) || MP_OBJ_IS_QSTR(o2)) {
        return false;
116
117
    } 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);
118
119
120
121
122
123
    } else {
        assert(0);
        return false;
    }
}

124
125
126
127
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);
128
129
130
131
132
133
134
        return i1 < i2;
    } else {
        assert(0);
        return false;
    }
}

135
136
machine_int_t mp_obj_get_int(mp_obj_t arg) {
    if (arg == mp_const_false) {
137
        return 0;
138
    } else if (arg == mp_const_true) {
139
        return 1;
140
141
    } else if (MP_OBJ_IS_SMALL_INT(arg)) {
        return MP_OBJ_SMALL_INT_VALUE(arg);
142
143
144
145
146
#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
147
    } else {
148
        nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "can't convert %s to int", mp_obj_get_type_str(arg)));
149
150
151
152
    }
}

#if MICROPY_ENABLE_FLOAT
153
154
machine_float_t mp_obj_get_float(mp_obj_t arg) {
    if (arg == mp_const_false) {
155
        return 0;
156
    } else if (arg == mp_const_true) {
157
        return 1;
158
159
160
161
    } 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);
162
    } else {
163
        nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "can't convert %s to float", mp_obj_get_type_str(arg)));
164
165
166
    }
}

167
168
void mp_obj_get_complex(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) {
    if (arg == mp_const_false) {
169
170
        *real = 0;
        *imag = 0;
171
    } else if (arg == mp_const_true) {
172
173
        *real = 1;
        *imag = 0;
174
175
    } else if (MP_OBJ_IS_SMALL_INT(arg)) {
        *real = MP_OBJ_SMALL_INT_VALUE(arg);
176
        *imag = 0;
177
178
    } else if (MP_OBJ_IS_TYPE(arg, &float_type)) {
        *real = mp_obj_float_get(arg);
179
        *imag = 0;
180
181
    } else if (MP_OBJ_IS_TYPE(arg, &complex_type)) {
        mp_obj_complex_get(arg, real, imag);
182
    } else {
183
        nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "can't convert %s to complex", mp_obj_get_type_str(arg)));
184
185
186
187
    }
}
#endif

188
189
190
qstr mp_obj_get_qstr(mp_obj_t arg) {
    if (MP_OBJ_IS_TYPE(arg, &str_type)) {
        return mp_obj_str_get(arg);
191
192
193
194
195
196
    } else {
        assert(0);
        return 0;
    }
}

197
198
199
200
201
202
203
204
205
206
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) {
207
            nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_IndexError, "requested length %d but object has length %d", n, seq_len));
208
        }
209
        return seq_items;
210
    } else {
211
        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)));
212
213
214
    }
}

215
216
217
218
219
220
221
222
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) {
223
            nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_IndexError, "%s index out of range", type->name));
224
225
226
        }
        return i;
    } else {
227
        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)));
228
229
    }
}