Commit 97209d38 authored by Damien George's avatar Damien George
Browse files

Merge branch 'cplusplus' of https://github.com/ian-v/micropython into ian-v-cplusplus

Conflicts:
	py/objcomplex.c
parents d49420e7 a5a01df8
......@@ -23,14 +23,7 @@ void ellipsis_print(void (*print)(void *env, const char *fmt, ...), void *env, m
const mp_obj_type_t ellipsis_type = {
{ &mp_const_type },
"ellipsis",
ellipsis_print, // print
NULL, // make_new
NULL, // call_n
NULL, // unary_op
NULL, // binary_op
NULL, // getiter
NULL, // iternext
.methods = {{NULL, NULL},},
.print = ellipsis_print,
};
static const mp_obj_ellipsis_t ellipsis_obj = {{&ellipsis_type}};
......@@ -58,7 +51,6 @@ const mp_obj_type_t slice_type = {
{ &mp_const_type },
"slice",
.print = slice_print,
.methods = { { NULL, NULL }, },
};
// TODO: Make sure to handle "empty" values, which are signified by None in CPython
......
......@@ -184,17 +184,19 @@ mp_obj_t str_format(int n_args, const mp_obj_t *args) {
static MP_DEFINE_CONST_FUN_OBJ_2(str_join_obj, str_join);
static MP_DEFINE_CONST_FUN_OBJ_VAR(str_format_obj, 1, str_format);
static const mp_method_t str_type_methods[] = {
{ "join", &str_join_obj },
{ "format", &str_format_obj },
{ NULL, NULL }, // end-of-list sentinel
};
const mp_obj_type_t str_type = {
{ &mp_const_type },
"str",
.print = str_print,
.binary_op = str_binary_op,
.getiter = str_getiter,
.methods = {
{ "join", &str_join_obj },
{ "format", &str_format_obj },
{ NULL, NULL }, // end-of-list sentinel
},
.methods = str_type_methods,
};
mp_obj_t mp_obj_new_str(qstr qstr) {
......@@ -235,7 +237,6 @@ static const mp_obj_type_t str_it_type = {
{ &mp_const_type },
"str_iterator",
.iternext = str_it_iternext,
.methods = { { NULL, NULL }, },
};
mp_obj_t mp_obj_new_str_iterator(mp_obj_str_t *str, int cur) {
......
......@@ -103,7 +103,6 @@ const mp_obj_type_t tuple_type = {
.make_new = tuple_make_new,
.binary_op = tuple_binary_op,
.getiter = tuple_getiter,
.methods = {{NULL, NULL},},
};
// the zero-length tuple
......@@ -166,7 +165,6 @@ static const mp_obj_type_t tuple_it_type = {
{ &mp_const_type },
"tuple_iterator",
.iternext = tuple_it_iternext,
.methods = {{NULL, NULL},},
};
static mp_obj_t mp_obj_new_tuple_iterator(mp_obj_tuple_t *tuple, int cur) {
......
......@@ -27,5 +27,4 @@ const mp_obj_type_t mp_const_type = {
"type",
.print = type_print,
.call_n = type_call_n,
.methods = {{NULL, NULL},},
};
......@@ -775,10 +775,12 @@ mp_obj_t rt_load_attr(mp_obj_t base, qstr attr) {
} else if (MP_OBJ_IS_OBJ(base)) {
// generic method lookup
mp_obj_base_t *o = base;
const mp_method_t *meth = &o->type->methods[0];
for (; meth->name != NULL; meth++) {
if (strcmp(meth->name, qstr_str(attr)) == 0) {
return mp_obj_new_bound_meth(base, (mp_obj_t)meth->fun);
const mp_method_t *meth = o->type->methods;
if (meth != NULL) {
for (; meth->name != NULL; meth++) {
if (strcmp(meth->name, qstr_str(attr)) == 0) {
return mp_obj_new_bound_meth(base, (mp_obj_t)meth->fun);
}
}
}
}
......@@ -799,12 +801,14 @@ void rt_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest) {
} else if (MP_OBJ_IS_OBJ(base)) {
// generic method lookup
mp_obj_base_t *o = base;
const mp_method_t *meth = &o->type->methods[0];
for (; meth->name != NULL; meth++) {
if (strcmp(meth->name, qstr_str(attr)) == 0) {
dest[1] = (mp_obj_t)meth->fun;
dest[0] = base;
return;
const mp_method_t *meth = o->type->methods;
if (meth != NULL) {
for (; meth->name != NULL; meth++) {
if (strcmp(meth->name, qstr_str(attr)) == 0) {
dest[1] = (mp_obj_t)meth->fun;
dest[0] = base;
return;
}
}
}
}
......
......@@ -326,18 +326,20 @@ static MP_DEFINE_CONST_FUN_OBJ_1(i2c_obj_read_obj, i2c_obj_read);
static MP_DEFINE_CONST_FUN_OBJ_1(i2c_obj_readAndStop_obj, i2c_obj_readAndStop);
static MP_DEFINE_CONST_FUN_OBJ_1(i2c_obj_stop_obj, i2c_obj_stop);
static const mp_method_t i2c_methods[] = {
{ "start", &i2c_obj_start_obj },
{ "write", &i2c_obj_write_obj },
{ "read", &i2c_obj_read_obj },
{ "readAndStop", &i2c_obj_readAndStop_obj },
{ "stop", &i2c_obj_stop_obj },
{ NULL, NULL },
};
static const mp_obj_type_t i2c_obj_type = {
{ &mp_const_type },
"I2C",
.print = i2c_obj_print,
.methods = {
{ "start", &i2c_obj_start_obj },
{ "write", &i2c_obj_write_obj },
{ "read", &i2c_obj_read_obj },
{ "readAndStop", &i2c_obj_readAndStop_obj },
{ "stop", &i2c_obj_stop_obj },
{ NULL, NULL },
}
.methods = i2c_methods,
};
// create the I2C object
......
......@@ -176,15 +176,17 @@ mp_obj_t led_obj_off(mp_obj_t self_in) {
static MP_DEFINE_CONST_FUN_OBJ_1(led_obj_on_obj, led_obj_on);
static MP_DEFINE_CONST_FUN_OBJ_1(led_obj_off_obj, led_obj_off);
static const mp_method_t led_methods[] = {
{ "on", &led_obj_on_obj },
{ "off", &led_obj_off_obj },
{ NULL, NULL },
};
static const mp_obj_type_t led_obj_type = {
{ &mp_const_type },
"Led",
.print = led_obj_print,
.methods = {
{ "on", &led_obj_on_obj },
{ "off", &led_obj_off_obj },
{ NULL, NULL },
}
.methods = led_methods,
};
mp_obj_t pyb_Led(mp_obj_t led_id) {
......
......@@ -689,22 +689,18 @@ static MP_DEFINE_CONST_FUN_OBJ_1(file_obj_close_obj, file_obj_close);
// TODO gc hook to close the file if not already closed
static const mp_method_t file_methods[] = {
{ "read", &file_obj_read_obj },
{ "write", &file_obj_write_obj },
{ "close", &file_obj_close_obj },
{NULL, NULL},
};
static const mp_obj_type_t file_obj_type = {
{ &mp_const_type },
"File",
file_obj_print, // print
NULL, // make_new
NULL, // call_n
NULL, // unary_op
NULL, // binary_op
NULL, // getiter
NULL, // iternext
.methods = {
{ "read", &file_obj_read_obj },
{ "write", &file_obj_write_obj },
{ "close", &file_obj_close_obj },
{NULL, NULL},
}
.print = file_obj_print,
.methods = file_methods,
};
mp_obj_t pyb_io_open(mp_obj_t o_filename, mp_obj_t o_mode) {
......
......@@ -137,14 +137,16 @@ static mp_obj_t servo_obj_angle(mp_obj_t self_in, mp_obj_t angle) {
static MP_DEFINE_CONST_FUN_OBJ_2(servo_obj_angle_obj, servo_obj_angle);
static const mp_method_t servo_methods[] = {
{ "angle", &servo_obj_angle_obj },
{ NULL, NULL },
};
static const mp_obj_type_t servo_obj_type = {
{ &mp_const_type },
"Servo",
.print = servo_obj_print,
.methods = {
{ "angle", &servo_obj_angle_obj },
{ NULL, NULL },
}
.methods = servo_methods,
};
mp_obj_t pyb_Servo(mp_obj_t servo_id) {
......
......@@ -154,7 +154,7 @@ static void do_str(const char *str) {
typedef struct _test_obj_t {
mp_obj_base_t base;
bool value;
int value;
} test_obj_t;
static void test_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
......@@ -176,15 +176,17 @@ static mp_obj_t test_set(mp_obj_t self_in, mp_obj_t arg) {
static MP_DEFINE_CONST_FUN_OBJ_1(test_get_obj, test_get);
static MP_DEFINE_CONST_FUN_OBJ_2(test_set_obj, test_set);
static const mp_method_t test_methods[] = {
{ "get", &test_get_obj },
{ "set", &test_set_obj },
{ NULL, NULL },
};
static const mp_obj_type_t test_type = {
{ &mp_const_type },
"Test",
.print = test_print,
.methods = {
{ "get", &test_get_obj },
{ "set", &test_set_obj },
{ NULL, NULL },
}
.methods = test_methods,
};
mp_obj_t test_obj_new(int value) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment