blob: 8340ad3045881e19fdb8eb41452a8434d9dcc0e8 [file] [log] [blame]
Damien660365e2013-12-17 18:27:24 +00001#include <stdint.h>
2#include <stdlib.h>
3#include <stdio.h>
4#include <stdarg.h>
5#include <string.h>
6#include <assert.h>
7
8#include "nlr.h"
9#include "misc.h"
Damiend99b0522013-12-21 18:17:45 +000010#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +000011#include "qstr.h"
Damien660365e2013-12-17 18:27:24 +000012#include "obj.h"
Damiend99b0522013-12-21 18:17:45 +000013#include "runtime0.h"
14#include "runtime.h"
Damiend99b0522013-12-21 18:17:45 +000015#include "map.h"
Damien660365e2013-12-17 18:27:24 +000016#include "builtin.h"
17
Damien Georgeb97669a2014-01-08 11:47:55 +000018// args[0] is function from class body
19// args[1] is class name
20// args[2:] are base objects
Damien Georgea11ceca2014-01-19 16:02:09 +000021static mp_obj_t mp_builtin___build_class__(uint n_args, const mp_obj_t *args) {
Damien Georgeb97669a2014-01-08 11:47:55 +000022 assert(2 <= n_args);
23
Damiena3dcd9e2013-12-17 21:35:38 +000024 // we differ from CPython: we set the new __locals__ object here
Damien George66028ab2014-01-03 14:03:48 +000025 mp_map_t *old_locals = rt_locals_get();
Damien George062478e2014-01-09 20:57:50 +000026 mp_obj_t class_locals = mp_obj_new_dict(0);
27 rt_locals_set(mp_obj_dict_get_map(class_locals));
Damiena3dcd9e2013-12-17 21:35:38 +000028
29 // call the class code
Damien Georgeb97669a2014-01-08 11:47:55 +000030 mp_obj_t cell = rt_call_function_1(args[0], (mp_obj_t)0xdeadbeef);
Damiena3dcd9e2013-12-17 21:35:38 +000031
32 // restore old __locals__ object
Damien George66028ab2014-01-03 14:03:48 +000033 rt_locals_set(old_locals);
Damiena3dcd9e2013-12-17 21:35:38 +000034
Damien Georgeb97669a2014-01-08 11:47:55 +000035 // get the class type (meta object) from the base objects
36 mp_obj_t meta;
37 if (n_args == 2) {
38 // no explicit bases, so use 'type'
39 meta = (mp_obj_t)&mp_const_type;
40 } else {
41 // use type of first base object
42 meta = mp_obj_get_type(args[2]);
43 }
Damien Georgeb97669a2014-01-08 11:47:55 +000044
45 // TODO do proper metaclass resolution for multiple base objects
46
Damien Georgeb97669a2014-01-08 11:47:55 +000047 // create the new class using a call to the meta object
Damien Georgeb97669a2014-01-08 11:47:55 +000048 mp_obj_t meta_args[3];
Damien George20006db2014-01-18 14:10:48 +000049 meta_args[0] = args[1]; // class name
Damien Georgeb97669a2014-01-08 11:47:55 +000050 meta_args[1] = mp_obj_new_tuple(n_args - 2, args + 2); // tuple of bases
Damien George20006db2014-01-18 14:10:48 +000051 meta_args[2] = class_locals; // dict of members
52 mp_obj_t new_class = rt_call_function_n_kw(meta, 3, 0, meta_args);
Damien Georgeb97669a2014-01-08 11:47:55 +000053
54 // store into cell if neede
55 if (cell != mp_const_none) {
56 mp_obj_cell_set(cell, new_class);
57 }
58
59 return new_class;
Damiena3dcd9e2013-12-17 21:35:38 +000060}
61
Damien Georgeb97669a2014-01-08 11:47:55 +000062MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin___build_class___obj, 2, mp_builtin___build_class__);
63
Damien George23005372014-01-13 19:39:01 +000064static mp_obj_t mp_builtin___repl_print__(mp_obj_t o) {
Damiend99b0522013-12-21 18:17:45 +000065 if (o != mp_const_none) {
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020066 mp_obj_print(o, PRINT_REPR);
Damien660365e2013-12-17 18:27:24 +000067 printf("\n");
68 }
Damiend99b0522013-12-21 18:17:45 +000069 return mp_const_none;
Damien660365e2013-12-17 18:27:24 +000070}
71
Damien George23005372014-01-13 19:39:01 +000072MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin___repl_print___obj, mp_builtin___repl_print__);
73
Damiend99b0522013-12-21 18:17:45 +000074mp_obj_t mp_builtin_abs(mp_obj_t o_in) {
75 if (MP_OBJ_IS_SMALL_INT(o_in)) {
76 mp_small_int_t val = MP_OBJ_SMALL_INT_VALUE(o_in);
Damien660365e2013-12-17 18:27:24 +000077 if (val < 0) {
78 val = -val;
79 }
Damiend99b0522013-12-21 18:17:45 +000080 return MP_OBJ_NEW_SMALL_INT(val);
Damien660365e2013-12-17 18:27:24 +000081#if MICROPY_ENABLE_FLOAT
Damiend99b0522013-12-21 18:17:45 +000082 } else if (MP_OBJ_IS_TYPE(o_in, &float_type)) {
83 mp_float_t value = mp_obj_float_get(o_in);
Damien660365e2013-12-17 18:27:24 +000084 // TODO check for NaN etc
Damiend99b0522013-12-21 18:17:45 +000085 if (value < 0) {
86 return mp_obj_new_float(-value);
Damien660365e2013-12-17 18:27:24 +000087 } else {
88 return o_in;
89 }
Damiend99b0522013-12-21 18:17:45 +000090 } else if (MP_OBJ_IS_TYPE(o_in, &complex_type)) {
91 mp_float_t real, imag;
92 mp_obj_complex_get(o_in, &real, &imag);
93 return mp_obj_new_float(machine_sqrt(real*real + imag*imag));
Damien660365e2013-12-17 18:27:24 +000094#endif
95 } else {
96 assert(0);
Damiend99b0522013-12-21 18:17:45 +000097 return mp_const_none;
Damien660365e2013-12-17 18:27:24 +000098 }
99}
100
Damien George23005372014-01-13 19:39:01 +0000101MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_abs_obj, mp_builtin_abs);
102
103static mp_obj_t mp_builtin_all(mp_obj_t o_in) {
Damiend99b0522013-12-21 18:17:45 +0000104 mp_obj_t iterable = rt_getiter(o_in);
105 mp_obj_t item;
106 while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) {
Damiena3dcd9e2013-12-17 21:35:38 +0000107 if (!rt_is_true(item)) {
Damiend99b0522013-12-21 18:17:45 +0000108 return mp_const_false;
Damiena3dcd9e2013-12-17 21:35:38 +0000109 }
110 }
Damiend99b0522013-12-21 18:17:45 +0000111 return mp_const_true;
Damien660365e2013-12-17 18:27:24 +0000112}
113
Damien George23005372014-01-13 19:39:01 +0000114MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_all_obj, mp_builtin_all);
115
116static mp_obj_t mp_builtin_any(mp_obj_t o_in) {
Damiend99b0522013-12-21 18:17:45 +0000117 mp_obj_t iterable = rt_getiter(o_in);
118 mp_obj_t item;
119 while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) {
Damiena3dcd9e2013-12-17 21:35:38 +0000120 if (rt_is_true(item)) {
Damiend99b0522013-12-21 18:17:45 +0000121 return mp_const_true;
Damiena3dcd9e2013-12-17 21:35:38 +0000122 }
123 }
Damiend99b0522013-12-21 18:17:45 +0000124 return mp_const_false;
Damiena3dcd9e2013-12-17 21:35:38 +0000125}
126
Damien George23005372014-01-13 19:39:01 +0000127MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_any_obj, mp_builtin_any);
128
129static mp_obj_t mp_builtin_callable(mp_obj_t o_in) {
Damiend99b0522013-12-21 18:17:45 +0000130 if (mp_obj_is_callable(o_in)) {
131 return mp_const_true;
Damiena3dcd9e2013-12-17 21:35:38 +0000132 } else {
Damiend99b0522013-12-21 18:17:45 +0000133 return mp_const_false;
Damiena3dcd9e2013-12-17 21:35:38 +0000134 }
135}
136
Damien George23005372014-01-13 19:39:01 +0000137MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_callable_obj, mp_builtin_callable);
138
139static mp_obj_t mp_builtin_chr(mp_obj_t o_in) {
Damiend99b0522013-12-21 18:17:45 +0000140 int ord = mp_obj_get_int(o_in);
Damiena3dcd9e2013-12-17 21:35:38 +0000141 if (0 <= ord && ord <= 0x10ffff) {
Damien George55baff42014-01-21 21:40:13 +0000142 char str[1] = {ord};
143 return mp_obj_new_str(qstr_from_strn(str, 1));
Damiena3dcd9e2013-12-17 21:35:38 +0000144 } else {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000145 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_ValueError, "chr() arg not in range(0x110000)"));
Damiena3dcd9e2013-12-17 21:35:38 +0000146 }
147}
148
Damien George23005372014-01-13 19:39:01 +0000149MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_chr_obj, mp_builtin_chr);
150
151static mp_obj_t mp_builtin_divmod(mp_obj_t o1_in, mp_obj_t o2_in) {
Damiend99b0522013-12-21 18:17:45 +0000152 if (MP_OBJ_IS_SMALL_INT(o1_in) && MP_OBJ_IS_SMALL_INT(o2_in)) {
153 mp_small_int_t i1 = MP_OBJ_SMALL_INT_VALUE(o1_in);
154 mp_small_int_t i2 = MP_OBJ_SMALL_INT_VALUE(o2_in);
Damien George20006db2014-01-18 14:10:48 +0000155 mp_obj_t args[2];
156 args[0] = MP_OBJ_NEW_SMALL_INT(i1 / i2);
157 args[1] = MP_OBJ_NEW_SMALL_INT(i1 % i2);
158 return rt_build_tuple(2, args);
Damiena3dcd9e2013-12-17 21:35:38 +0000159 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000160 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "unsupported operand type(s) for divmod(): '%s' and '%s'", mp_obj_get_type_str(o1_in), mp_obj_get_type_str(o2_in)));
Damiena3dcd9e2013-12-17 21:35:38 +0000161 }
162}
163
Damien George23005372014-01-13 19:39:01 +0000164MP_DEFINE_CONST_FUN_OBJ_2(mp_builtin_divmod_obj, mp_builtin_divmod);
165
Damiend99b0522013-12-21 18:17:45 +0000166static mp_obj_t mp_builtin_hash(mp_obj_t o_in) {
Damiena3dcd9e2013-12-17 21:35:38 +0000167 // TODO hash will generally overflow small integer; can we safely truncate it?
Damiend99b0522013-12-21 18:17:45 +0000168 return mp_obj_new_int(mp_obj_hash(o_in));
Damiena3dcd9e2013-12-17 21:35:38 +0000169}
170
Damiend99b0522013-12-21 18:17:45 +0000171MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_hash_obj, mp_builtin_hash);
172
173static mp_obj_t mp_builtin_iter(mp_obj_t o_in) {
Damiena3dcd9e2013-12-17 21:35:38 +0000174 return rt_getiter(o_in);
175}
176
Damiend99b0522013-12-21 18:17:45 +0000177MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_iter_obj, mp_builtin_iter);
Damiena3dcd9e2013-12-17 21:35:38 +0000178
Damien George23005372014-01-13 19:39:01 +0000179static mp_obj_t mp_builtin_len(mp_obj_t o_in) {
John R. Lenton4bee76e2014-01-10 11:25:03 +0000180 mp_obj_t len = mp_obj_len_maybe(o_in);
181 if (len == NULL) {
Damien George6c73ca12014-01-08 18:11:23 +0000182 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "object of type '%s' has no len()", mp_obj_get_type_str(o_in)));
John R. Lenton4bee76e2014-01-10 11:25:03 +0000183 } else {
184 return len;
Damiena3dcd9e2013-12-17 21:35:38 +0000185 }
Damiena3dcd9e2013-12-17 21:35:38 +0000186}
187
Damien George23005372014-01-13 19:39:01 +0000188MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_len_obj, mp_builtin_len);
189
Damien Georgea11ceca2014-01-19 16:02:09 +0000190static mp_obj_t mp_builtin_max(uint n_args, const mp_obj_t *args) {
Damiena3dcd9e2013-12-17 21:35:38 +0000191 if (n_args == 1) {
192 // given an iterable
Damiend99b0522013-12-21 18:17:45 +0000193 mp_obj_t iterable = rt_getiter(args[0]);
194 mp_obj_t max_obj = NULL;
195 mp_obj_t item;
196 while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) {
197 if (max_obj == NULL || mp_obj_less(max_obj, item)) {
Damiena3dcd9e2013-12-17 21:35:38 +0000198 max_obj = item;
199 }
200 }
201 if (max_obj == NULL) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000202 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_ValueError, "max() arg is an empty sequence"));
Damiena3dcd9e2013-12-17 21:35:38 +0000203 }
204 return max_obj;
205 } else {
206 // given many args
Damiend99b0522013-12-21 18:17:45 +0000207 mp_obj_t max_obj = args[0];
Damiena3dcd9e2013-12-17 21:35:38 +0000208 for (int i = 1; i < n_args; i++) {
Damiend99b0522013-12-21 18:17:45 +0000209 if (mp_obj_less(max_obj, args[i])) {
Damiena3dcd9e2013-12-17 21:35:38 +0000210 max_obj = args[i];
211 }
212 }
213 return max_obj;
214 }
215}
216
Damien George23005372014-01-13 19:39:01 +0000217MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin_max_obj, 1, mp_builtin_max);
218
Damien Georgea11ceca2014-01-19 16:02:09 +0000219static mp_obj_t mp_builtin_min(uint n_args, const mp_obj_t *args) {
Damiena3dcd9e2013-12-17 21:35:38 +0000220 if (n_args == 1) {
221 // given an iterable
Damiend99b0522013-12-21 18:17:45 +0000222 mp_obj_t iterable = rt_getiter(args[0]);
223 mp_obj_t min_obj = NULL;
224 mp_obj_t item;
225 while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) {
226 if (min_obj == NULL || mp_obj_less(item, min_obj)) {
Damiena3dcd9e2013-12-17 21:35:38 +0000227 min_obj = item;
228 }
229 }
230 if (min_obj == NULL) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000231 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_ValueError, "min() arg is an empty sequence"));
Damiena3dcd9e2013-12-17 21:35:38 +0000232 }
233 return min_obj;
234 } else {
235 // given many args
Damiend99b0522013-12-21 18:17:45 +0000236 mp_obj_t min_obj = args[0];
Damiena3dcd9e2013-12-17 21:35:38 +0000237 for (int i = 1; i < n_args; i++) {
Damiend99b0522013-12-21 18:17:45 +0000238 if (mp_obj_less(args[i], min_obj)) {
Damiena3dcd9e2013-12-17 21:35:38 +0000239 min_obj = args[i];
240 }
241 }
242 return min_obj;
243 }
244}
245
Damien George23005372014-01-13 19:39:01 +0000246MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin_min_obj, 1, mp_builtin_min);
247
Damiend9d62012013-12-21 18:38:03 +0000248static mp_obj_t mp_builtin_next(mp_obj_t o) {
249 mp_obj_t ret = rt_iternext(o);
250 if (ret == mp_const_stop_iteration) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000251 nlr_jump(mp_obj_new_exception(MP_QSTR_StopIteration));
Damiend9d62012013-12-21 18:38:03 +0000252 } else {
253 return ret;
254 }
Damiend99b0522013-12-21 18:17:45 +0000255}
256
257MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_next_obj, mp_builtin_next);
258
Damien George23005372014-01-13 19:39:01 +0000259static mp_obj_t mp_builtin_ord(mp_obj_t o_in) {
Damien George55baff42014-01-21 21:40:13 +0000260 uint len;
261 const byte *str = qstr_data(mp_obj_get_qstr(o_in), &len);
262 if (len == 1) {
Damiend99b0522013-12-21 18:17:45 +0000263 return mp_obj_new_int(str[0]);
Damiena3dcd9e2013-12-17 21:35:38 +0000264 } else {
Damien George55baff42014-01-21 21:40:13 +0000265 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "ord() expected a character, but string of length %d found", len));
Damiena3dcd9e2013-12-17 21:35:38 +0000266 }
267}
268
Damien George23005372014-01-13 19:39:01 +0000269MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_ord_obj, mp_builtin_ord);
270
Damien Georgea11ceca2014-01-19 16:02:09 +0000271static mp_obj_t mp_builtin_pow(uint n_args, const mp_obj_t *args) {
Damien George23005372014-01-13 19:39:01 +0000272 assert(2 <= n_args && n_args <= 3);
Damiena3dcd9e2013-12-17 21:35:38 +0000273 switch (n_args) {
274 case 2: return rt_binary_op(RT_BINARY_OP_POWER, args[0], args[1]);
Damien George23005372014-01-13 19:39:01 +0000275 default: return rt_binary_op(RT_BINARY_OP_MODULO, rt_binary_op(RT_BINARY_OP_POWER, args[0], args[1]), args[2]); // TODO optimise...
Damiena3dcd9e2013-12-17 21:35:38 +0000276 }
277}
278
Damien George23005372014-01-13 19:39:01 +0000279MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_pow_obj, 2, 3, mp_builtin_pow);
280
Damien Georgea11ceca2014-01-19 16:02:09 +0000281static mp_obj_t mp_builtin_print(uint n_args, const mp_obj_t *args) {
Damiena3dcd9e2013-12-17 21:35:38 +0000282 for (int i = 0; i < n_args; i++) {
283 if (i > 0) {
284 printf(" ");
285 }
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200286 mp_obj_print(args[i], PRINT_STR);
Damiena3dcd9e2013-12-17 21:35:38 +0000287 }
288 printf("\n");
Damiend99b0522013-12-21 18:17:45 +0000289 return mp_const_none;
Damiena3dcd9e2013-12-17 21:35:38 +0000290}
291
Damien George23005372014-01-13 19:39:01 +0000292MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin_print_obj, 0, mp_builtin_print);
293
Damien Georgea11ceca2014-01-19 16:02:09 +0000294static mp_obj_t mp_builtin_range(uint n_args, const mp_obj_t *args) {
Damien George23005372014-01-13 19:39:01 +0000295 assert(1 <= n_args && n_args <= 3);
Damien660365e2013-12-17 18:27:24 +0000296 switch (n_args) {
Damiend99b0522013-12-21 18:17:45 +0000297 case 1: return mp_obj_new_range(0, mp_obj_get_int(args[0]), 1);
298 case 2: return mp_obj_new_range(mp_obj_get_int(args[0]), mp_obj_get_int(args[1]), 1);
Damien George23005372014-01-13 19:39:01 +0000299 default: return mp_obj_new_range(mp_obj_get_int(args[0]), mp_obj_get_int(args[1]), mp_obj_get_int(args[2]));
Damien660365e2013-12-17 18:27:24 +0000300 }
301}
Damiena3dcd9e2013-12-17 21:35:38 +0000302
Damien George23005372014-01-13 19:39:01 +0000303MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_range_obj, 1, 3, mp_builtin_range);
304
Damien Georgee2fb2ba2014-01-15 21:40:48 +0000305static mp_obj_t mp_builtin_repr(mp_obj_t o_in) {
306 vstr_t *vstr = vstr_new();
Damien George4899ff92014-01-15 22:39:03 +0000307 mp_obj_print_helper((void (*)(void *env, const char *fmt, ...))vstr_printf, vstr, o_in, PRINT_REPR);
Damien George55baff42014-01-21 21:40:13 +0000308 // TODO don't intern this string
309 return mp_obj_new_str(qstr_from_strn_take(vstr->buf, vstr->alloc, vstr->len));
Damien Georgee2fb2ba2014-01-15 21:40:48 +0000310}
311
312MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_repr_obj, mp_builtin_repr);
313
Damien Georgea11ceca2014-01-19 16:02:09 +0000314static mp_obj_t mp_builtin_sum(uint n_args, const mp_obj_t *args) {
Damien George23005372014-01-13 19:39:01 +0000315 assert(1 <= n_args && n_args <= 2);
Damiend99b0522013-12-21 18:17:45 +0000316 mp_obj_t value;
Damiena3dcd9e2013-12-17 21:35:38 +0000317 switch (n_args) {
Damiend99b0522013-12-21 18:17:45 +0000318 case 1: value = mp_obj_new_int(0); break;
Damien George23005372014-01-13 19:39:01 +0000319 default: value = args[1]; break;
Damiena3dcd9e2013-12-17 21:35:38 +0000320 }
Damiend99b0522013-12-21 18:17:45 +0000321 mp_obj_t iterable = rt_getiter(args[0]);
322 mp_obj_t item;
323 while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) {
Damiena3dcd9e2013-12-17 21:35:38 +0000324 value = rt_binary_op(RT_BINARY_OP_ADD, value, item);
325 }
326 return value;
327}
Damien Georgee2fb2ba2014-01-15 21:40:48 +0000328
Damien George23005372014-01-13 19:39:01 +0000329MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_sum_obj, 1, 2, mp_builtin_sum);
John R. Lenton5c768392014-01-13 05:12:50 +0000330
Damien George20006db2014-01-18 14:10:48 +0000331static mp_obj_t mp_builtin_sorted(uint n_args, const mp_obj_t *args, mp_map_t *kwargs) {
332 assert(n_args >= 1);
333 if (n_args > 1) {
John R. Lenton5c768392014-01-13 05:12:50 +0000334 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError,
335 "must use keyword argument for key function"));
336 }
Damien George20006db2014-01-18 14:10:48 +0000337 mp_obj_t self = list_type.make_new((mp_obj_t)&list_type, 1, 0, args);
338 mp_obj_list_sort(1, &self, kwargs);
John R. Lenton5c768392014-01-13 05:12:50 +0000339
340 return self;
341}
Damien Georgee2fb2ba2014-01-15 21:40:48 +0000342
John R. Lenton88cb1e62014-01-13 19:55:18 +0000343MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_sorted_obj, 1, mp_builtin_sorted);
Paul Sokolovsky36c44992014-01-13 19:20:46 +0200344
Damien George4899ff92014-01-15 22:39:03 +0000345static mp_obj_t mp_builtin_str(mp_obj_t o_in) {
Paul Sokolovsky36c44992014-01-13 19:20:46 +0200346 vstr_t *vstr = vstr_new();
Damien George4899ff92014-01-15 22:39:03 +0000347 mp_obj_print_helper((void (*)(void*, const char*, ...))vstr_printf, vstr, o_in, PRINT_STR);
Damien George55baff42014-01-21 21:40:13 +0000348 // TODO don't intern this string
349 return mp_obj_new_str(qstr_from_strn_take(vstr->buf, vstr->alloc, vstr->len));
Paul Sokolovsky36c44992014-01-13 19:20:46 +0200350}
351
352MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_str_obj, mp_builtin_str);