blob: 0c91e8f6d8aa0dd59fe98af7321abb2ccae40a1b [file] [log] [blame]
Damien660365e2013-12-17 18:27:24 +00001#include <stdio.h>
2#include <stdarg.h>
3#include <assert.h>
4
5#include "nlr.h"
6#include "misc.h"
Damiend99b0522013-12-21 18:17:45 +00007#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +00008#include "qstr.h"
Damien660365e2013-12-17 18:27:24 +00009#include "obj.h"
Damiend99b0522013-12-21 18:17:45 +000010#include "runtime0.h"
11#include "runtime.h"
12#include "map.h"
Damien660365e2013-12-17 18:27:24 +000013
Damien George5fa93b62014-01-22 14:35:10 +000014mp_obj_type_t *mp_obj_get_type(mp_obj_t o_in) {
Damien Georgeb97669a2014-01-08 11:47:55 +000015 if (MP_OBJ_IS_SMALL_INT(o_in)) {
16 return (mp_obj_t)&int_type;
Damien George38a2da62014-01-08 17:33:12 +000017 } else if (MP_OBJ_IS_QSTR(o_in)) {
18 return (mp_obj_t)&str_type;
Damien Georgeb97669a2014-01-08 11:47:55 +000019 } else {
20 mp_obj_base_t *o = o_in;
21 return (mp_obj_t)o->type;
22 }
23}
24
Damiend99b0522013-12-21 18:17:45 +000025const char *mp_obj_get_type_str(mp_obj_t o_in) {
Damien Georgea71c83a2014-02-15 11:34:50 +000026 return qstr_str(mp_obj_get_type(o_in)->name);
Damien660365e2013-12-17 18:27:24 +000027}
28
29void printf_wrapper(void *env, const char *fmt, ...) {
30 va_list args;
31 va_start(args, fmt);
32 vprintf(fmt, args);
33 va_end(args);
34}
35
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020036void mp_obj_print_helper(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
Damien George5fa93b62014-01-22 14:35:10 +000037 mp_obj_type_t *type = mp_obj_get_type(o_in);
38 if (type->print != NULL) {
39 type->print(print, env, o_in, kind);
Damien660365e2013-12-17 18:27:24 +000040 } else {
Damien Georgea71c83a2014-02-15 11:34:50 +000041 print(env, "<%s>", qstr_str(type->name));
Damien660365e2013-12-17 18:27:24 +000042 }
43}
44
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020045void mp_obj_print(mp_obj_t o_in, mp_print_kind_t kind) {
46 mp_obj_print_helper(printf_wrapper, NULL, o_in, kind);
Damien660365e2013-12-17 18:27:24 +000047}
48
Damien George136b1492014-01-19 12:38:49 +000049// helper function to print an exception with traceback
50void mp_obj_print_exception(mp_obj_t exc) {
Damien Georgec5966122014-02-15 16:10:44 +000051 if (mp_obj_is_exception_instance(exc)) {
Damien George136b1492014-01-19 12:38:49 +000052 machine_uint_t n, *values;
53 mp_obj_exception_get_traceback(exc, &n, &values);
54 if (n > 0) {
55 printf("Traceback (most recent call last):\n");
56 for (int i = n - 3; i >= 0; i -= 3) {
Damien George62ad1892014-01-29 21:51:51 +000057#if MICROPY_ENABLE_SOURCE_LINE
Damien George136b1492014-01-19 12:38:49 +000058 printf(" File \"%s\", line %d, in %s\n", qstr_str(values[i]), (int)values[i + 1], qstr_str(values[i + 2]));
Damien George62ad1892014-01-29 21:51:51 +000059#else
60 printf(" File \"%s\", in %s\n", qstr_str(values[i]), qstr_str(values[i + 2]));
61#endif
Damien George136b1492014-01-19 12:38:49 +000062 }
63 }
64 }
65 mp_obj_print(exc, PRINT_REPR);
66 printf("\n");
67}
68
Damiend99b0522013-12-21 18:17:45 +000069bool mp_obj_is_callable(mp_obj_t o_in) {
Damien Georgeb051e7d2014-01-23 18:13:53 +000070 return mp_obj_get_type(o_in)->call != NULL;
Damien660365e2013-12-17 18:27:24 +000071}
72
Damiend99b0522013-12-21 18:17:45 +000073machine_int_t mp_obj_hash(mp_obj_t o_in) {
74 if (o_in == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +000075 return 0; // needs to hash to same as the integer 0, since False==0
Damiend99b0522013-12-21 18:17:45 +000076 } else if (o_in == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +000077 return 1; // needs to hash to same as the integer 1, since True==1
Damiend99b0522013-12-21 18:17:45 +000078 } else if (MP_OBJ_IS_SMALL_INT(o_in)) {
79 return MP_OBJ_SMALL_INT_VALUE(o_in);
Damien George5fa93b62014-01-22 14:35:10 +000080 } else if (MP_OBJ_IS_STR(o_in)) {
81 return mp_obj_str_get_hash(o_in);
Damiend99b0522013-12-21 18:17:45 +000082 } else if (MP_OBJ_IS_TYPE(o_in, &none_type)) {
Damien660365e2013-12-17 18:27:24 +000083 return (machine_int_t)o_in;
Damien George7f8be592014-03-20 19:20:59 +000084 } else if (MP_OBJ_IS_TYPE(o_in, &fun_native_type) || MP_OBJ_IS_TYPE(o_in, &fun_bc_type)) {
85 return (machine_int_t)o_in;
86 } else if (MP_OBJ_IS_TYPE(o_in, &tuple_type)) {
87 return mp_obj_tuple_hash(o_in);
88
89 // TODO hash class and instances
90 // TODO delegate to __hash__ method if it exists
91
Damien660365e2013-12-17 18:27:24 +000092 } else {
Damien George7f8be592014-03-20 19:20:59 +000093 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "unhashable type: '%s'", mp_obj_get_type_str(o_in)));
Damien660365e2013-12-17 18:27:24 +000094 }
95}
96
97// this function implements the '==' operator (and so the inverse of '!=')
98// from the python language reference:
99// "The objects need not have the same type. If both are numbers, they are converted
100// to a common type. Otherwise, the == and != operators always consider objects of
101// different types to be unequal."
102// note also that False==0 and True==1 are true expressions
Damiend99b0522013-12-21 18:17:45 +0000103bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2) {
Damien660365e2013-12-17 18:27:24 +0000104 if (o1 == o2) {
105 return true;
Damiend99b0522013-12-21 18:17:45 +0000106 } else if (MP_OBJ_IS_SMALL_INT(o1) || MP_OBJ_IS_SMALL_INT(o2)) {
107 if (MP_OBJ_IS_SMALL_INT(o1) && MP_OBJ_IS_SMALL_INT(o2)) {
Damien660365e2013-12-17 18:27:24 +0000108 return false;
109 } else {
Damiend99b0522013-12-21 18:17:45 +0000110 if (MP_OBJ_IS_SMALL_INT(o2)) {
111 mp_obj_t temp = o1; o1 = o2; o2 = temp;
Damien660365e2013-12-17 18:27:24 +0000112 }
113 // o1 is the SMALL_INT, o2 is not
Damiend99b0522013-12-21 18:17:45 +0000114 mp_small_int_t val = MP_OBJ_SMALL_INT_VALUE(o1);
115 if (o2 == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000116 return val == 0;
Damiend99b0522013-12-21 18:17:45 +0000117 } else if (o2 == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000118 return val == 1;
Paul Sokolovskyca318bb2014-01-13 16:29:14 +0200119 } else if (MP_OBJ_IS_TYPE(o2, &int_type)) {
120 // If o2 is long int, dispatch to its virtual methods
121 mp_obj_base_t *o = o2;
122 if (o->type->binary_op != NULL) {
Damien George9aa2a522014-02-01 23:04:09 +0000123 mp_obj_t r = o->type->binary_op(RT_BINARY_OP_EQUAL, o2, o1);
Paul Sokolovskyca318bb2014-01-13 16:29:14 +0200124 return r == mp_const_true ? true : false;
125 }
Damien660365e2013-12-17 18:27:24 +0000126 }
Paul Sokolovskyca318bb2014-01-13 16:29:14 +0200127 return false;
Damien660365e2013-12-17 18:27:24 +0000128 }
Damien George5fa93b62014-01-22 14:35:10 +0000129 } else if (MP_OBJ_IS_STR(o1) && MP_OBJ_IS_STR(o2)) {
130 return mp_obj_str_equal(o1, o2);
Damien660365e2013-12-17 18:27:24 +0000131 } else {
Paul Sokolovskycc57bd22014-01-12 01:55:50 +0200132 mp_obj_base_t *o = o1;
133 if (o->type->binary_op != NULL) {
Damien George9aa2a522014-02-01 23:04:09 +0000134 mp_obj_t r = o->type->binary_op(RT_BINARY_OP_EQUAL, o1, o2);
Paul Sokolovskycc57bd22014-01-12 01:55:50 +0200135 if (r != MP_OBJ_NULL) {
136 return r == mp_const_true ? true : false;
137 }
138 }
Paul Sokolovsky4e836fb2014-02-10 19:43:41 +0200139
Damien Georgec5966122014-02-15 16:10:44 +0000140 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_NotImplementedError,
Paul Sokolovsky4e836fb2014-02-10 19:43:41 +0200141 "Equality for '%s' and '%s' types not yet implemented", mp_obj_get_type_str(o1), mp_obj_get_type_str(o2)));
Damien660365e2013-12-17 18:27:24 +0000142 return false;
143 }
144}
145
Damiend99b0522013-12-21 18:17:45 +0000146bool mp_obj_less(mp_obj_t o1, mp_obj_t o2) {
147 if (MP_OBJ_IS_SMALL_INT(o1) && MP_OBJ_IS_SMALL_INT(o2)) {
148 mp_small_int_t i1 = MP_OBJ_SMALL_INT_VALUE(o1);
149 mp_small_int_t i2 = MP_OBJ_SMALL_INT_VALUE(o2);
Damiena3dcd9e2013-12-17 21:35:38 +0000150 return i1 < i2;
151 } else {
152 assert(0);
153 return false;
154 }
155}
156
Damiend99b0522013-12-21 18:17:45 +0000157machine_int_t mp_obj_get_int(mp_obj_t arg) {
158 if (arg == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000159 return 0;
Damiend99b0522013-12-21 18:17:45 +0000160 } else if (arg == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000161 return 1;
Damiend99b0522013-12-21 18:17:45 +0000162 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
163 return MP_OBJ_SMALL_INT_VALUE(arg);
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200164 } else if (MP_OBJ_IS_TYPE(arg, &int_type)) {
165 return mp_obj_int_get_checked(arg);
Damien660365e2013-12-17 18:27:24 +0000166 } else {
Damien Georgec5966122014-02-15 16:10:44 +0000167 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "can't convert %s to int", mp_obj_get_type_str(arg)));
Damien660365e2013-12-17 18:27:24 +0000168 }
169}
170
171#if MICROPY_ENABLE_FLOAT
Damien George0c36da02014-03-08 15:24:39 +0000172mp_float_t mp_obj_get_float(mp_obj_t arg) {
Damiend99b0522013-12-21 18:17:45 +0000173 if (arg == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000174 return 0;
Damiend99b0522013-12-21 18:17:45 +0000175 } else if (arg == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000176 return 1;
Damiend99b0522013-12-21 18:17:45 +0000177 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
178 return MP_OBJ_SMALL_INT_VALUE(arg);
Damien George0c36da02014-03-08 15:24:39 +0000179 } else if (MP_OBJ_IS_TYPE(arg, &mp_type_float)) {
Damiend99b0522013-12-21 18:17:45 +0000180 return mp_obj_float_get(arg);
Damien660365e2013-12-17 18:27:24 +0000181 } else {
Damien Georgec5966122014-02-15 16:10:44 +0000182 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "can't convert %s to float", mp_obj_get_type_str(arg)));
Damien660365e2013-12-17 18:27:24 +0000183 }
184}
185
Damiend99b0522013-12-21 18:17:45 +0000186void mp_obj_get_complex(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) {
187 if (arg == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000188 *real = 0;
189 *imag = 0;
Damiend99b0522013-12-21 18:17:45 +0000190 } else if (arg == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000191 *real = 1;
192 *imag = 0;
Damiend99b0522013-12-21 18:17:45 +0000193 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
194 *real = MP_OBJ_SMALL_INT_VALUE(arg);
Damien660365e2013-12-17 18:27:24 +0000195 *imag = 0;
Damien George0c36da02014-03-08 15:24:39 +0000196 } else if (MP_OBJ_IS_TYPE(arg, &mp_type_float)) {
Damiend99b0522013-12-21 18:17:45 +0000197 *real = mp_obj_float_get(arg);
Damien660365e2013-12-17 18:27:24 +0000198 *imag = 0;
Damien George0c36da02014-03-08 15:24:39 +0000199 } else if (MP_OBJ_IS_TYPE(arg, &mp_type_complex)) {
Damiend99b0522013-12-21 18:17:45 +0000200 mp_obj_complex_get(arg, real, imag);
Damien660365e2013-12-17 18:27:24 +0000201 } else {
Damien Georgec5966122014-02-15 16:10:44 +0000202 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "can't convert %s to complex", mp_obj_get_type_str(arg)));
Damien660365e2013-12-17 18:27:24 +0000203 }
204}
205#endif
206
Damiend99b0522013-12-21 18:17:45 +0000207mp_obj_t *mp_obj_get_array_fixed_n(mp_obj_t o_in, machine_int_t n) {
208 if (MP_OBJ_IS_TYPE(o_in, &tuple_type) || MP_OBJ_IS_TYPE(o_in, &list_type)) {
209 uint seq_len;
210 mp_obj_t *seq_items;
211 if (MP_OBJ_IS_TYPE(o_in, &tuple_type)) {
212 mp_obj_tuple_get(o_in, &seq_len, &seq_items);
213 } else {
214 mp_obj_list_get(o_in, &seq_len, &seq_items);
Damien660365e2013-12-17 18:27:24 +0000215 }
Damiend99b0522013-12-21 18:17:45 +0000216 if (seq_len != n) {
Damien Georgec5966122014-02-15 16:10:44 +0000217 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_IndexError, "requested length %d but object has length %d", n, seq_len));
Damiend99b0522013-12-21 18:17:45 +0000218 }
219 return seq_items;
Damien660365e2013-12-17 18:27:24 +0000220 } else {
Damien Georgec5966122014-02-15 16:10:44 +0000221 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)));
Damien660365e2013-12-17 18:27:24 +0000222 }
223}
224
xbe9e1e8cd2014-03-12 22:57:16 -0700225// is_slice determines whether the index is a slice index
226uint mp_get_index(const mp_obj_type_t *type, machine_uint_t len, mp_obj_t index, bool is_slice) {
227 int i;
Damiend99b0522013-12-21 18:17:45 +0000228 if (MP_OBJ_IS_SMALL_INT(index)) {
xbe9e1e8cd2014-03-12 22:57:16 -0700229 i = MP_OBJ_SMALL_INT_VALUE(index);
230 } else if (MP_OBJ_IS_TYPE(index, &bool_type)) {
xbec5d70ba2014-03-13 00:29:15 -0700231 i = (index == mp_const_true ? 1 : 0);
Damiend99b0522013-12-21 18:17:45 +0000232 } else {
Damien Georgec5966122014-02-15 16:10:44 +0000233 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)));
Damien660365e2013-12-17 18:27:24 +0000234 }
xbe9e1e8cd2014-03-12 22:57:16 -0700235
236 if (i < 0) {
xbec5d70ba2014-03-13 00:29:15 -0700237 i += len;
xbe9e1e8cd2014-03-12 22:57:16 -0700238 }
239 if (is_slice) {
xbec5d70ba2014-03-13 00:29:15 -0700240 if (i < 0) {
241 i = 0;
242 } else if (i > len) {
243 i = len;
244 }
xbe9e1e8cd2014-03-12 22:57:16 -0700245 } else {
xbec5d70ba2014-03-13 00:29:15 -0700246 if (i < 0 || i >= len) {
247 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_IndexError, "%s index out of range", qstr_str(type->name)));
248 }
xbe9e1e8cd2014-03-12 22:57:16 -0700249 }
250 return i;
Damien660365e2013-12-17 18:27:24 +0000251}
John R. Lenton4bee76e2014-01-10 11:25:03 +0000252
Damien Georgeeae16442014-01-11 19:22:29 +0000253// may return MP_OBJ_NULL
John R. Lenton4bee76e2014-01-10 11:25:03 +0000254mp_obj_t mp_obj_len_maybe(mp_obj_t o_in) {
Damien George5fa93b62014-01-22 14:35:10 +0000255 if (MP_OBJ_IS_STR(o_in)) {
Damien George09a0c642014-01-30 10:05:33 +0000256 return MP_OBJ_NEW_SMALL_INT((machine_int_t)mp_obj_str_get_len(o_in));
John R. Lenton4bee76e2014-01-10 11:25:03 +0000257 } else {
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200258 mp_obj_type_t *type = mp_obj_get_type(o_in);
259 if (type->unary_op != NULL) {
Damien George09a0c642014-01-30 10:05:33 +0000260 return type->unary_op(RT_UNARY_OP_LEN, o_in);
261 } else {
262 return MP_OBJ_NULL;
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200263 }
John R. Lenton4bee76e2014-01-10 11:25:03 +0000264 }
John R. Lenton4bee76e2014-01-10 11:25:03 +0000265}
Paul Sokolovskydff3f892014-01-20 18:37:30 +0200266
267// Return input argument. Useful as .getiter for objects which are
268// their own iterators, etc.
269mp_obj_t mp_identity(mp_obj_t self) {
270 return self;
271}
Paul Sokolovsky557c9d52014-02-08 21:57:19 +0200272MP_DEFINE_CONST_FUN_OBJ_1(mp_identity_obj, mp_identity);