From 5f3e005b6791634b104fa6385c8a9bf5ed1af164 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 2 Feb 2016 23:16:05 +0000 Subject: py: Extend native type-sig to use 4 bits, so uint is separate to ptr. Before this patch, the native types for uint and ptr/ptr8/ptr16/ptr32 all overlapped and it was possible to make a mistake in casting. Now, these types are all separate and any coding mistakes will be raised as runtime errors. --- py/nativeglue.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) (limited to 'py/nativeglue.c') diff --git a/py/nativeglue.c b/py/nativeglue.c index cc0d61ce96..e27d69e14e 100644 --- a/py/nativeglue.c +++ b/py/nativeglue.c @@ -45,19 +45,16 @@ // convert a Micro Python object to a valid native value based on type mp_uint_t mp_convert_obj_to_native(mp_obj_t obj, mp_uint_t type) { DEBUG_printf("mp_convert_obj_to_native(%p, " UINT_FMT ")\n", obj, type); - switch (type & 3) { + switch (type & 0xf) { case MP_NATIVE_TYPE_OBJ: return (mp_uint_t)obj; case MP_NATIVE_TYPE_BOOL: - case MP_NATIVE_TYPE_INT: return mp_obj_get_int_truncated(obj); - case MP_NATIVE_TYPE_UINT: { + case MP_NATIVE_TYPE_INT: + case MP_NATIVE_TYPE_UINT: return mp_obj_get_int_truncated(obj); + default: { // a pointer mp_buffer_info_t bufinfo; - if (mp_get_buffer(obj, &bufinfo, MP_BUFFER_RW)) { - return (mp_uint_t)bufinfo.buf; - } else { - return mp_obj_get_int_truncated(obj); - } + mp_get_buffer_raise(obj, &bufinfo, MP_BUFFER_RW); + return (mp_uint_t)bufinfo.buf; } - default: assert(0); return 0; } } @@ -68,12 +65,14 @@ mp_uint_t mp_convert_obj_to_native(mp_obj_t obj, mp_uint_t type) { // convert a native value to a Micro Python object based on type mp_obj_t mp_convert_native_to_obj(mp_uint_t val, mp_uint_t type) { DEBUG_printf("mp_convert_native_to_obj(" UINT_FMT ", " UINT_FMT ")\n", val, type); - switch (type & 3) { + switch (type & 0xf) { case MP_NATIVE_TYPE_OBJ: return (mp_obj_t)val; case MP_NATIVE_TYPE_BOOL: return mp_obj_new_bool(val); case MP_NATIVE_TYPE_INT: return mp_obj_new_int(val); case MP_NATIVE_TYPE_UINT: return mp_obj_new_int_from_uint(val); - default: assert(0); return mp_const_none; + default: // a pointer + // we return just the value of the pointer as an integer + return mp_obj_new_int_from_uint(val); } } -- cgit v1.2.3