Commit f5a0a7d2 authored by John R. Lenton's avatar John R. Lenton
Browse files

Merge remote-tracking branch 'upstream/master' into containment

parents 189c8e1c ca318bba
This diff is collapsed.
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
// args[0] is function from class body // args[0] is function from class body
// args[1] is class name // args[1] is class name
// args[2:] are base objects // args[2:] are base objects
mp_obj_t mp_builtin___build_class__(int n_args, const mp_obj_t *args) { static mp_obj_t mp_builtin___build_class__(int n_args, const mp_obj_t *args) {
assert(2 <= n_args); assert(2 <= n_args);
// we differ from CPython: we set the new __locals__ object here // we differ from CPython: we set the new __locals__ object here
...@@ -62,7 +62,7 @@ mp_obj_t mp_builtin___build_class__(int n_args, const mp_obj_t *args) { ...@@ -62,7 +62,7 @@ mp_obj_t mp_builtin___build_class__(int n_args, const mp_obj_t *args) {
MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin___build_class___obj, 2, mp_builtin___build_class__); MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin___build_class___obj, 2, mp_builtin___build_class__);
mp_obj_t mp_builtin___repl_print__(mp_obj_t o) { static mp_obj_t mp_builtin___repl_print__(mp_obj_t o) {
if (o != mp_const_none) { if (o != mp_const_none) {
mp_obj_print(o); mp_obj_print(o);
printf("\n"); printf("\n");
...@@ -70,6 +70,8 @@ mp_obj_t mp_builtin___repl_print__(mp_obj_t o) { ...@@ -70,6 +70,8 @@ mp_obj_t mp_builtin___repl_print__(mp_obj_t o) {
return mp_const_none; return mp_const_none;
} }
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin___repl_print___obj, mp_builtin___repl_print__);
mp_obj_t mp_builtin_abs(mp_obj_t o_in) { mp_obj_t mp_builtin_abs(mp_obj_t o_in) {
if (MP_OBJ_IS_SMALL_INT(o_in)) { if (MP_OBJ_IS_SMALL_INT(o_in)) {
mp_small_int_t val = MP_OBJ_SMALL_INT_VALUE(o_in); mp_small_int_t val = MP_OBJ_SMALL_INT_VALUE(o_in);
...@@ -97,7 +99,9 @@ mp_obj_t mp_builtin_abs(mp_obj_t o_in) { ...@@ -97,7 +99,9 @@ mp_obj_t mp_builtin_abs(mp_obj_t o_in) {
} }
} }
mp_obj_t mp_builtin_all(mp_obj_t o_in) { MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_abs_obj, mp_builtin_abs);
static mp_obj_t mp_builtin_all(mp_obj_t o_in) {
mp_obj_t iterable = rt_getiter(o_in); mp_obj_t iterable = rt_getiter(o_in);
mp_obj_t item; mp_obj_t item;
while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) { while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) {
...@@ -108,7 +112,9 @@ mp_obj_t mp_builtin_all(mp_obj_t o_in) { ...@@ -108,7 +112,9 @@ mp_obj_t mp_builtin_all(mp_obj_t o_in) {
return mp_const_true; return mp_const_true;
} }
mp_obj_t mp_builtin_any(mp_obj_t o_in) { MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_all_obj, mp_builtin_all);
static mp_obj_t mp_builtin_any(mp_obj_t o_in) {
mp_obj_t iterable = rt_getiter(o_in); mp_obj_t iterable = rt_getiter(o_in);
mp_obj_t item; mp_obj_t item;
while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) { while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) {
...@@ -119,7 +125,9 @@ mp_obj_t mp_builtin_any(mp_obj_t o_in) { ...@@ -119,7 +125,9 @@ mp_obj_t mp_builtin_any(mp_obj_t o_in) {
return mp_const_false; return mp_const_false;
} }
mp_obj_t mp_builtin_callable(mp_obj_t o_in) { MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_any_obj, mp_builtin_any);
static mp_obj_t mp_builtin_callable(mp_obj_t o_in) {
if (mp_obj_is_callable(o_in)) { if (mp_obj_is_callable(o_in)) {
return mp_const_true; return mp_const_true;
} else { } else {
...@@ -127,7 +135,9 @@ mp_obj_t mp_builtin_callable(mp_obj_t o_in) { ...@@ -127,7 +135,9 @@ mp_obj_t mp_builtin_callable(mp_obj_t o_in) {
} }
} }
mp_obj_t mp_builtin_chr(mp_obj_t o_in) { MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_callable_obj, mp_builtin_callable);
static mp_obj_t mp_builtin_chr(mp_obj_t o_in) {
int ord = mp_obj_get_int(o_in); int ord = mp_obj_get_int(o_in);
if (0 <= ord && ord <= 0x10ffff) { if (0 <= ord && ord <= 0x10ffff) {
char *str = m_new(char, 2); char *str = m_new(char, 2);
...@@ -139,7 +149,9 @@ mp_obj_t mp_builtin_chr(mp_obj_t o_in) { ...@@ -139,7 +149,9 @@ mp_obj_t mp_builtin_chr(mp_obj_t o_in) {
} }
} }
mp_obj_t mp_builtin_divmod(mp_obj_t o1_in, mp_obj_t o2_in) { MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_chr_obj, mp_builtin_chr);
static mp_obj_t mp_builtin_divmod(mp_obj_t o1_in, mp_obj_t o2_in) {
if (MP_OBJ_IS_SMALL_INT(o1_in) && MP_OBJ_IS_SMALL_INT(o2_in)) { if (MP_OBJ_IS_SMALL_INT(o1_in) && MP_OBJ_IS_SMALL_INT(o2_in)) {
mp_small_int_t i1 = MP_OBJ_SMALL_INT_VALUE(o1_in); mp_small_int_t i1 = MP_OBJ_SMALL_INT_VALUE(o1_in);
mp_small_int_t i2 = MP_OBJ_SMALL_INT_VALUE(o2_in); mp_small_int_t i2 = MP_OBJ_SMALL_INT_VALUE(o2_in);
...@@ -152,6 +164,8 @@ mp_obj_t mp_builtin_divmod(mp_obj_t o1_in, mp_obj_t o2_in) { ...@@ -152,6 +164,8 @@ mp_obj_t mp_builtin_divmod(mp_obj_t o1_in, mp_obj_t o2_in) {
} }
} }
MP_DEFINE_CONST_FUN_OBJ_2(mp_builtin_divmod_obj, mp_builtin_divmod);
static mp_obj_t mp_builtin_hash(mp_obj_t o_in) { static mp_obj_t mp_builtin_hash(mp_obj_t o_in) {
// TODO hash will generally overflow small integer; can we safely truncate it? // TODO hash will generally overflow small integer; can we safely truncate it?
return mp_obj_new_int(mp_obj_hash(o_in)); return mp_obj_new_int(mp_obj_hash(o_in));
...@@ -165,7 +179,7 @@ static mp_obj_t mp_builtin_iter(mp_obj_t o_in) { ...@@ -165,7 +179,7 @@ static mp_obj_t mp_builtin_iter(mp_obj_t o_in) {
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_iter_obj, mp_builtin_iter); MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_iter_obj, mp_builtin_iter);
mp_obj_t mp_builtin_len(mp_obj_t o_in) { static mp_obj_t mp_builtin_len(mp_obj_t o_in) {
mp_obj_t len = mp_obj_len_maybe(o_in); mp_obj_t len = mp_obj_len_maybe(o_in);
if (len == NULL) { if (len == NULL) {
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))); 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)));
...@@ -174,7 +188,9 @@ mp_obj_t mp_builtin_len(mp_obj_t o_in) { ...@@ -174,7 +188,9 @@ mp_obj_t mp_builtin_len(mp_obj_t o_in) {
} }
} }
mp_obj_t mp_builtin_max(int n_args, const mp_obj_t *args) { MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_len_obj, mp_builtin_len);
static mp_obj_t mp_builtin_max(int n_args, const mp_obj_t *args) {
if (n_args == 1) { if (n_args == 1) {
// given an iterable // given an iterable
mp_obj_t iterable = rt_getiter(args[0]); mp_obj_t iterable = rt_getiter(args[0]);
...@@ -201,7 +217,9 @@ mp_obj_t mp_builtin_max(int n_args, const mp_obj_t *args) { ...@@ -201,7 +217,9 @@ mp_obj_t mp_builtin_max(int n_args, const mp_obj_t *args) {
} }
} }
mp_obj_t mp_builtin_min(int n_args, const mp_obj_t *args) { MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin_max_obj, 1, mp_builtin_max);
static mp_obj_t mp_builtin_min(int n_args, const mp_obj_t *args) {
if (n_args == 1) { if (n_args == 1) {
// given an iterable // given an iterable
mp_obj_t iterable = rt_getiter(args[0]); mp_obj_t iterable = rt_getiter(args[0]);
...@@ -228,6 +246,8 @@ mp_obj_t mp_builtin_min(int n_args, const mp_obj_t *args) { ...@@ -228,6 +246,8 @@ mp_obj_t mp_builtin_min(int n_args, const mp_obj_t *args) {
} }
} }
MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin_min_obj, 1, mp_builtin_min);
static mp_obj_t mp_builtin_next(mp_obj_t o) { static mp_obj_t mp_builtin_next(mp_obj_t o) {
mp_obj_t ret = rt_iternext(o); mp_obj_t ret = rt_iternext(o);
if (ret == mp_const_stop_iteration) { if (ret == mp_const_stop_iteration) {
...@@ -239,7 +259,7 @@ static mp_obj_t mp_builtin_next(mp_obj_t o) { ...@@ -239,7 +259,7 @@ static mp_obj_t mp_builtin_next(mp_obj_t o) {
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_next_obj, mp_builtin_next); MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_next_obj, mp_builtin_next);
mp_obj_t mp_builtin_ord(mp_obj_t o_in) { static mp_obj_t mp_builtin_ord(mp_obj_t o_in) {
const char *str = qstr_str(mp_obj_get_qstr(o_in)); const char *str = qstr_str(mp_obj_get_qstr(o_in));
if (strlen(str) == 1) { if (strlen(str) == 1) {
return mp_obj_new_int(str[0]); return mp_obj_new_int(str[0]);
...@@ -248,15 +268,19 @@ mp_obj_t mp_builtin_ord(mp_obj_t o_in) { ...@@ -248,15 +268,19 @@ mp_obj_t mp_builtin_ord(mp_obj_t o_in) {
} }
} }
mp_obj_t mp_builtin_pow(int n_args, const mp_obj_t *args) { MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_ord_obj, mp_builtin_ord);
static mp_obj_t mp_builtin_pow(int n_args, const mp_obj_t *args) {
assert(2 <= n_args && n_args <= 3);
switch (n_args) { switch (n_args) {
case 2: return rt_binary_op(RT_BINARY_OP_POWER, args[0], args[1]); case 2: return rt_binary_op(RT_BINARY_OP_POWER, args[0], args[1]);
case 3: return rt_binary_op(RT_BINARY_OP_MODULO, rt_binary_op(RT_BINARY_OP_POWER, args[0], args[1]), args[2]); // TODO optimise... default: return rt_binary_op(RT_BINARY_OP_MODULO, rt_binary_op(RT_BINARY_OP_POWER, args[0], args[1]), args[2]); // TODO optimise...
default: nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "pow expected at most 3 arguments, got %d", n_args));
} }
} }
mp_obj_t mp_builtin_print(int n_args, const mp_obj_t *args) { MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_pow_obj, 2, 3, mp_builtin_pow);
static mp_obj_t mp_builtin_print(int n_args, const mp_obj_t *args) {
for (int i = 0; i < n_args; i++) { for (int i = 0; i < n_args; i++) {
if (i > 0) { if (i > 0) {
printf(" "); printf(" ");
...@@ -273,21 +297,25 @@ mp_obj_t mp_builtin_print(int n_args, const mp_obj_t *args) { ...@@ -273,21 +297,25 @@ mp_obj_t mp_builtin_print(int n_args, const mp_obj_t *args) {
return mp_const_none; return mp_const_none;
} }
mp_obj_t mp_builtin_range(int n_args, const mp_obj_t *args) { MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin_print_obj, 0, mp_builtin_print);
static mp_obj_t mp_builtin_range(int n_args, const mp_obj_t *args) {
assert(1 <= n_args && n_args <= 3);
switch (n_args) { switch (n_args) {
case 1: return mp_obj_new_range(0, mp_obj_get_int(args[0]), 1); case 1: return mp_obj_new_range(0, mp_obj_get_int(args[0]), 1);
case 2: return mp_obj_new_range(mp_obj_get_int(args[0]), mp_obj_get_int(args[1]), 1); case 2: return mp_obj_new_range(mp_obj_get_int(args[0]), mp_obj_get_int(args[1]), 1);
case 3: return mp_obj_new_range(mp_obj_get_int(args[0]), mp_obj_get_int(args[1]), mp_obj_get_int(args[2])); default: return mp_obj_new_range(mp_obj_get_int(args[0]), mp_obj_get_int(args[1]), mp_obj_get_int(args[2]));
default: nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "range expected at most 3 arguments, got %d", n_args));
} }
} }
mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args) { MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_range_obj, 1, 3, mp_builtin_range);
static mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args) {
assert(1 <= n_args && n_args <= 2);
mp_obj_t value; mp_obj_t value;
switch (n_args) { switch (n_args) {
case 1: value = mp_obj_new_int(0); break; case 1: value = mp_obj_new_int(0); break;
case 2: value = args[1]; break; default: value = args[1]; break;
default: nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "sum expected at most 2 arguments, got %d", n_args));
} }
mp_obj_t iterable = rt_getiter(args[0]); mp_obj_t iterable = rt_getiter(args[0]);
mp_obj_t item; mp_obj_t item;
...@@ -296,3 +324,5 @@ mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args) { ...@@ -296,3 +324,5 @@ mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args) {
} }
return value; return value;
} }
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_sum_obj, 1, 2, mp_builtin_sum);
// TODO convert all these to objects using MP_DECLARE and MP_DEFINE mp_obj_t mp_builtin___import__(int n_args, mp_obj_t *args);
MP_DECLARE_CONST_FUN_OBJ(mp_builtin___build_class___obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin___build_class___obj);
mp_obj_t mp_builtin___import__(int n, mp_obj_t *args); MP_DECLARE_CONST_FUN_OBJ(mp_builtin___repl_print___obj);
mp_obj_t mp_builtin___repl_print__(mp_obj_t o); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_abs_obj);
mp_obj_t mp_builtin_abs(mp_obj_t o_in); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_all_obj);
mp_obj_t mp_builtin_all(mp_obj_t o_in); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_any_obj);
mp_obj_t mp_builtin_any(mp_obj_t o_in); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_callable_obj);
mp_obj_t mp_builtin_callable(mp_obj_t o_in); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_chr_obj);
mp_obj_t mp_builtin_chr(mp_obj_t o_in); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_divmod_obj);
mp_obj_t mp_builtin_divmod(mp_obj_t o1_in, mp_obj_t o2_in);
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_hash_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_hash_obj);
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_isinstance_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_isinstance_obj);
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_issubclass_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_issubclass_obj);
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_iter_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_iter_obj);
mp_obj_t mp_builtin_len(mp_obj_t o_in); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_len_obj);
mp_obj_t mp_builtin_list(int n_args, const mp_obj_t *args); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_list_obj);
mp_obj_t mp_builtin_max(int n_args, const mp_obj_t *args); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_max_obj);
mp_obj_t mp_builtin_min(int n_args, const mp_obj_t *args); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_min_obj);
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_next_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_next_obj);
mp_obj_t mp_builtin_ord(mp_obj_t o_in); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_ord_obj);
mp_obj_t mp_builtin_pow(int n_args, const mp_obj_t *args); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_pow_obj);
mp_obj_t mp_builtin_print(int n_args, const mp_obj_t *args); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_print_obj);
mp_obj_t mp_builtin_range(int n_args, const mp_obj_t *args); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_range_obj);
mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_sum_obj);
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
#include "map.h" #include "map.h"
#include "builtin.h" #include "builtin.h"
mp_obj_t mp_builtin___import__(int n, mp_obj_t *args) { mp_obj_t mp_builtin___import__(int n_args, mp_obj_t *args) {
/* /*
printf("import:\n"); printf("import:\n");
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
......
...@@ -91,7 +91,7 @@ typedef long long mp_longint_impl_t; ...@@ -91,7 +91,7 @@ typedef long long mp_longint_impl_t;
#define BITS_PER_BYTE (8) #define BITS_PER_BYTE (8)
#define BITS_PER_WORD (BITS_PER_BYTE * BYTES_PER_WORD) #define BITS_PER_WORD (BITS_PER_BYTE * BYTES_PER_WORD)
// machine_int_t value with most significant bit set // machine_int_t value with most significant bit set
#define WORD_MSBIT_HIGH (1 << (BYTES_PER_WORD * 8 - 1)) #define WORD_MSBIT_HIGH (((machine_uint_t)1) << (BYTES_PER_WORD * 8 - 1))
// printf format spec to use for machine_int_t and friends // printf format spec to use for machine_int_t and friends
#ifndef INT_FMT #ifndef INT_FMT
......
...@@ -108,9 +108,15 @@ bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2) { ...@@ -108,9 +108,15 @@ bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2) {
return val == 0; return val == 0;
} else if (o2 == mp_const_true) { } else if (o2 == mp_const_true) {
return val == 1; return val == 1;
} else { } else if (MP_OBJ_IS_TYPE(o2, &int_type)) {
return false; // 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;
}
} }
return false;
} }
} else if (MP_OBJ_IS_QSTR(o1) || MP_OBJ_IS_QSTR(o2)) { } else if (MP_OBJ_IS_QSTR(o1) || MP_OBJ_IS_QSTR(o2)) {
return false; return false;
......
...@@ -98,38 +98,13 @@ const mp_obj_type_t fun_native_type = { ...@@ -98,38 +98,13 @@ const mp_obj_type_t fun_native_type = {
.call_n_kw = fun_native_call_n_kw, .call_n_kw = fun_native_call_n_kw,
}; };
mp_obj_t rt_make_function_0(mp_fun_0_t fun) { // fun must have the correct signature for n_args fixed arguments
mp_obj_t rt_make_function_n(int n_args, void *fun) {
mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t); mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
o->base.type = &fun_native_type; o->base.type = &fun_native_type;
o->n_args_min = 0; o->is_kw = false;
o->n_args_max = 0; o->n_args_min = n_args;
o->fun = fun; o->n_args_max = n_args;
return o;
}
mp_obj_t rt_make_function_1(mp_fun_1_t fun) {
mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
o->base.type = &fun_native_type;
o->n_args_min = 1;
o->n_args_max = 1;
o->fun = fun;
return o;
}
mp_obj_t rt_make_function_2(mp_fun_2_t fun) {
mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
o->base.type = &fun_native_type;
o->n_args_min = 2;
o->n_args_max = 2;
o->fun = fun;
return o;
}
mp_obj_t rt_make_function_3(mp_fun_3_t fun) {
mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
o->base.type = &fun_native_type;
o->n_args_min = 3;
o->n_args_max = 3;
o->fun = fun; o->fun = fun;
return o; return o;
} }
...@@ -137,6 +112,7 @@ mp_obj_t rt_make_function_3(mp_fun_3_t fun) { ...@@ -137,6 +112,7 @@ mp_obj_t rt_make_function_3(mp_fun_3_t fun) {
mp_obj_t rt_make_function_var(int n_args_min, mp_fun_var_t fun) { mp_obj_t rt_make_function_var(int n_args_min, mp_fun_var_t fun) {
mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t); mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
o->base.type = &fun_native_type; o->base.type = &fun_native_type;
o->is_kw = false;
o->n_args_min = n_args_min; o->n_args_min = n_args_min;
o->n_args_max = ~((machine_uint_t)0); o->n_args_max = ~((machine_uint_t)0);
o->fun = fun; o->fun = fun;
...@@ -147,6 +123,7 @@ mp_obj_t rt_make_function_var(int n_args_min, mp_fun_var_t fun) { ...@@ -147,6 +123,7 @@ mp_obj_t rt_make_function_var(int n_args_min, mp_fun_var_t fun) {
mp_obj_t rt_make_function_var_between(int n_args_min, int n_args_max, mp_fun_var_t fun) { mp_obj_t rt_make_function_var_between(int n_args_min, int n_args_max, mp_fun_var_t fun) {
mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t); mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
o->base.type = &fun_native_type; o->base.type = &fun_native_type;
o->is_kw = false;
o->n_args_min = n_args_min; o->n_args_min = n_args_min;
o->n_args_max = n_args_max; o->n_args_max = n_args_max;
o->fun = fun; o->fun = fun;
......
...@@ -8,16 +8,7 @@ ...@@ -8,16 +8,7 @@
#include "mpconfig.h" #include "mpconfig.h"
#include "mpqstr.h" #include "mpqstr.h"
#include "obj.h" #include "obj.h"
#include "objint.h"
typedef struct _mp_obj_int_t {
mp_obj_base_t base;
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
mp_longint_impl_t val;
#endif
} mp_obj_int_t;
void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in);
mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in);
// This dispatcher function is expected to be independent of the implementation // This dispatcher function is expected to be independent of the implementation
// of long int // of long int
...@@ -54,11 +45,13 @@ void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj ...@@ -54,11 +45,13 @@ void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj
// This is called only for non-SMALL_INT // This is called only for non-SMALL_INT
mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
assert(0); assert(0);
return mp_const_none;
} }
// This is called only with strings whose value doesn't fit in SMALL_INT // This is called only with strings whose value doesn't fit in SMALL_INT
mp_obj_t mp_obj_new_int_from_long_str(const char *s) { mp_obj_t mp_obj_new_int_from_long_str(const char *s) {
assert(0); assert(0);
return mp_const_none;
} }
mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) { mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) {
...@@ -69,6 +62,7 @@ mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) { ...@@ -69,6 +62,7 @@ mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) {
} }
// TODO: Raise exception // TODO: Raise exception
assert(0); assert(0);
return mp_const_none;
} }
mp_obj_t mp_obj_new_int(machine_int_t value) { mp_obj_t mp_obj_new_int(machine_int_t value) {
...@@ -77,5 +71,6 @@ mp_obj_t mp_obj_new_int(machine_int_t value) { ...@@ -77,5 +71,6 @@ mp_obj_t mp_obj_new_int(machine_int_t value) {
} }
// TODO: Raise exception // TODO: Raise exception
assert(0); assert(0);
return mp_const_none;
} }
#endif #endif
typedef struct _mp_obj_int_t {
mp_obj_base_t base;
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
mp_longint_impl_t val;
#endif
} mp_obj_int_t;
void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in);
mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in);
...@@ -178,8 +178,8 @@ static mp_obj_t str_find(int n_args, const mp_obj_t *args) { ...@@ -178,8 +178,8 @@ static mp_obj_t str_find(int n_args, const mp_obj_t *args) {
const char* haystack = qstr_str(((mp_obj_str_t*)args[0])->qstr); const char* haystack = qstr_str(((mp_obj_str_t*)args[0])->qstr);
const char* needle = qstr_str(((mp_obj_str_t*)args[1])->qstr); const char* needle = qstr_str(((mp_obj_str_t*)args[1])->qstr);
ssize_t haystack_len = strlen(haystack); size_t haystack_len = strlen(haystack);
ssize_t needle_len = strlen(needle); size_t needle_len = strlen(needle);
size_t start = 0; size_t start = 0;
size_t end = haystack_len; size_t end = haystack_len;
...@@ -192,14 +192,17 @@ static mp_obj_t str_find(int n_args, const mp_obj_t *args) { ...@@ -192,14 +192,17 @@ static mp_obj_t str_find(int n_args, const mp_obj_t *args) {
} }
char *p = strstr(haystack + start, needle); char *p = strstr(haystack + start, needle);
ssize_t pos = -1; if (p == NULL) {
if (p) { // not found
pos = p - haystack; return MP_OBJ_NEW_SMALL_INT(-1);
} else {
// found
machine_int_t pos = p - haystack;
if (pos + needle_len > end) { if (pos + needle_len > end) {
pos = -1; pos = -1;
} }
return MP_OBJ_NEW_SMALL_INT(pos);
} }
return MP_OBJ_NEW_SMALL_INT(pos);
} }
mp_obj_t str_strip(int n_args, const mp_obj_t *args) { mp_obj_t str_strip(int n_args, const mp_obj_t *args) {
......
...@@ -97,7 +97,7 @@ void rt_init(void) { ...@@ -97,7 +97,7 @@ void rt_init(void) {
// built-in core functions // built-in core functions
mp_map_add_qstr(&map_builtins, MP_QSTR___build_class__, (mp_obj_t)&mp_builtin___build_class___obj); mp_map_add_qstr(&map_builtins, MP_QSTR___build_class__, (mp_obj_t)&mp_builtin___build_class___obj);
mp_map_add_qstr(&map_builtins, MP_QSTR___repl_print__, rt_make_function_1(mp_builtin___repl_print__)); mp_map_add_qstr(&map_builtins, MP_QSTR___repl_print__, (mp_obj_t)&mp_builtin___repl_print___obj);
// built-in types // built-in types
mp_map_add_qstr(&map_builtins, MP_QSTR_bool, (mp_obj_t)&bool_type); mp_map_add_qstr(&map_builtins, MP_QSTR_bool, (mp_obj_t)&bool_type);
...@@ -114,26 +114,26 @@ void rt_init(void) { ...@@ -114,26 +114,26 @@ void rt_init(void) {
mp_map_add_qstr(&map_builtins, MP_QSTR_tuple, (mp_obj_t)&tuple_type); mp_map_add_qstr(&map_builtins, MP_QSTR_tuple, (mp_obj_t)&tuple_type);
mp_map_add_qstr(&map_builtins, MP_QSTR_type, (mp_obj_t)&mp_const_type); mp_map_add_qstr(&map_builtins, MP_QSTR_type, (mp_obj_t)&mp_const_type);
// built-in user functions; TODO covert all to &mp_builtin_xxx's // built-in user functions
mp_map_add_qstr(&map_builtins, MP_QSTR_abs, rt_make_function_1(mp_builtin_abs)); mp_map_add_qstr(&map_builtins, MP_QSTR_abs, (mp_obj_t)&mp_builtin_abs_obj);
mp_map_add_qstr(&map_builtins, MP_QSTR_all, rt_make_function_1(mp_builtin_all)); mp_map_add_qstr(&map_builtins, MP_QSTR_all, (mp_obj_t)&mp_builtin_all_obj);
mp_map_add_qstr(&map_builtins, MP_QSTR_any, rt_make_function_1(mp_builtin_any)); mp_map_add_qstr(&map_builtins, MP_QSTR_any, (mp_obj_t)&mp_builtin_any_obj);
mp_map_add_qstr(&map_builtins, MP_QSTR_callable, rt_make_function_1(mp_builtin_callable));