Implement __bool__ and __len__ via unary_op virtual method for all types.

__bool__() and __len__() are just the same as __neg__() or __invert__(),
and require efficient dispatching implementation (not requiring search/lookup).
type->unary_op() is just the right choice for this short of adding
standalone virtual method(s) to already big mp_obj_type_t structure.
diff --git a/py/obj.c b/py/obj.c
index ce555ef..9837180 100644
--- a/py/obj.c
+++ b/py/obj.c
@@ -251,9 +251,15 @@
         len = seq_len;
     } else if (MP_OBJ_IS_TYPE(o_in, &dict_type)) {
         len = mp_obj_dict_len(o_in);
-    } else if (MP_OBJ_IS_TYPE(o_in, &array_type)) {
-        len = mp_obj_array_len(o_in);
     } else {
+        mp_obj_type_t *type = mp_obj_get_type(o_in);
+        if (type->unary_op != NULL) {
+            mp_obj_t result = type->unary_op(RT_UNARY_OP_LEN, o_in);
+            if (result != MP_OBJ_NULL) {
+                return result;
+            }
+        }
+
         return MP_OBJ_NULL;
     }
     return MP_OBJ_NEW_SMALL_INT(len);
diff --git a/py/objarray.c b/py/objarray.c
index 42dbfcd..c595d21 100644
--- a/py/objarray.c
+++ b/py/objarray.c
@@ -189,6 +189,15 @@
 }
 MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_bytearray_obj, mp_builtin_bytearray);
 
+static mp_obj_t array_unary_op(int op, mp_obj_t o_in) {
+    mp_obj_array_t *o = o_in;
+    switch (op) {
+        case RT_UNARY_OP_BOOL: return MP_BOOL(o->len != 0);
+        case RT_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(o->len);
+        default: return MP_OBJ_NULL; // op not supported
+    }
+}
+
 static mp_obj_t array_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
     mp_obj_array_t *o = lhs;
     switch (op) {
@@ -245,6 +254,7 @@
     .print = array_print,
     .make_new = array_make_new,
     .getiter = array_iterator_new,
+    .unary_op = array_unary_op,
     .binary_op = array_binary_op,
     .store_item = array_store_item,
     .methods = array_type_methods,
diff --git a/py/objbool.c b/py/objbool.c
index 53b2bf8..3a3ac3e 100644
--- a/py/objbool.c
+++ b/py/objbool.c
@@ -36,7 +36,7 @@
 static mp_obj_t bool_unary_op(int op, mp_obj_t o_in) {
     machine_int_t value = ((mp_obj_bool_t*)o_in)->value;
     switch (op) {
-        case RT_UNARY_OP_NOT: if (value) { return mp_const_false; } else { return mp_const_true; }
+        case RT_UNARY_OP_BOOL: return o_in;
         case RT_UNARY_OP_POSITIVE: return MP_OBJ_NEW_SMALL_INT(value);
         case RT_UNARY_OP_NEGATIVE: return MP_OBJ_NEW_SMALL_INT(-value);
         case RT_UNARY_OP_INVERT:
diff --git a/py/objcomplex.c b/py/objcomplex.c
index 24762e8..5b4df0d 100644
--- a/py/objcomplex.c
+++ b/py/objcomplex.c
@@ -73,7 +73,7 @@
 static mp_obj_t complex_unary_op(int op, mp_obj_t o_in) {
     mp_obj_complex_t *o = o_in;
     switch (op) {
-        case RT_UNARY_OP_NOT: if (o->real != 0 || o->imag != 0) { return mp_const_true;} else { return mp_const_false; }
+        case RT_UNARY_OP_BOOL: return MP_BOOL(o->real != 0 || o->imag != 0);
         case RT_UNARY_OP_POSITIVE: return o_in;
         case RT_UNARY_OP_NEGATIVE: return mp_obj_new_complex(-o->real, -o->imag);
         default: return MP_OBJ_NULL; // op not supported
diff --git a/py/objdict.c b/py/objdict.c
index 93ff1af..f78a735 100644
--- a/py/objdict.c
+++ b/py/objdict.c
@@ -46,7 +46,8 @@
 static mp_obj_t dict_unary_op(int op, mp_obj_t self_in) {
     mp_obj_dict_t *self = self_in;
     switch (op) {
-        case RT_UNARY_OP_NOT: if (self->map.used == 0) { return mp_const_true; } else { return mp_const_false; }
+        case RT_UNARY_OP_BOOL: return MP_BOOL(self->map.used != 0);
+        case RT_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(self->map.used);
         default: return MP_OBJ_NULL; // op not supported for None
     }
 }
diff --git a/py/objfloat.c b/py/objfloat.c
index 69fd65e..9caeaf7 100644
--- a/py/objfloat.c
+++ b/py/objfloat.c
@@ -47,7 +47,7 @@
 static mp_obj_t float_unary_op(int op, mp_obj_t o_in) {
     mp_obj_float_t *o = o_in;
     switch (op) {
-        case RT_UNARY_OP_NOT: if (o->value != 0) { return mp_const_true;} else { return mp_const_false; }
+        case RT_UNARY_OP_BOOL: return MP_BOOL(o->value != 0);
         case RT_UNARY_OP_POSITIVE: return o_in;
         case RT_UNARY_OP_NEGATIVE: return mp_obj_new_float(-o->value);
         default: return NULL; // op not supported
diff --git a/py/objint_longlong.c b/py/objint_longlong.c
index 38a0183..a59bcf0 100644
--- a/py/objint_longlong.c
+++ b/py/objint_longlong.c
@@ -35,7 +35,7 @@
 mp_obj_t int_unary_op(int op, mp_obj_t o_in) {
     mp_obj_int_t *o = o_in;
     switch (op) {
-        case RT_UNARY_OP_NOT: return MP_BOOL(o->val != 0); // TODO: implements RT_UNARY_OP_BOOL
+        case RT_UNARY_OP_BOOL: return MP_BOOL(o->val != 0);
         case RT_UNARY_OP_POSITIVE: return o_in;
         case RT_UNARY_OP_NEGATIVE: return mp_obj_new_int_from_ll(-o->val);
         case RT_UNARY_OP_INVERT: return mp_obj_new_int_from_ll(~o->val);
diff --git a/py/objlist.c b/py/objlist.c
index fb68e2c..81040e3 100644
--- a/py/objlist.c
+++ b/py/objlist.c
@@ -125,7 +125,8 @@
 static mp_obj_t list_unary_op(int op, mp_obj_t self_in) {
     mp_obj_list_t *self = self_in;
     switch (op) {
-        case RT_UNARY_OP_NOT: if (self->len == 0) { return mp_const_true; } else { return mp_const_false; }
+        case RT_UNARY_OP_BOOL: return MP_BOOL(self->len != 0);
+        case RT_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(self->len);
         default: return MP_OBJ_NULL; // op not supported for None
     }
 }
diff --git a/py/objnone.c b/py/objnone.c
index 4151403..7af0b29 100644
--- a/py/objnone.c
+++ b/py/objnone.c
@@ -18,7 +18,7 @@
 
 static mp_obj_t none_unary_op(int op, mp_obj_t o_in) {
     switch (op) {
-        case RT_UNARY_OP_NOT: return mp_const_true;
+        case RT_UNARY_OP_BOOL: return mp_const_false;
         default: return MP_OBJ_NULL; // op not supported for None
     }
 }
diff --git a/py/objtuple.c b/py/objtuple.c
index 754fe4b..5f1744e 100644
--- a/py/objtuple.c
+++ b/py/objtuple.c
@@ -76,7 +76,8 @@
 static mp_obj_t tuple_unary_op(int op, mp_obj_t self_in) {
     mp_obj_tuple_t *self = self_in;
     switch (op) {
-        case RT_UNARY_OP_NOT: if (self->len == 0) { return mp_const_true; } else { return mp_const_false; }
+        case RT_UNARY_OP_BOOL: return MP_BOOL(self->len != 0);
+        case RT_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(self->len);
         default: return MP_OBJ_NULL; // op not supported for None
     }
 }
diff --git a/py/runtime.c b/py/runtime.c
index 6dd6921..6b3c8dc 100644
--- a/py/runtime.c
+++ b/py/runtime.c
@@ -317,15 +317,20 @@
     } else if (arg == mp_const_true) {
         return 1;
     } else {
+        mp_obj_type_t *type = mp_obj_get_type(arg);
+        if (type->unary_op != NULL) {
+            mp_obj_t result = type->unary_op(RT_UNARY_OP_BOOL, arg);
+            if (result != NULL) {
+                return result == mp_const_true;
+            }
+        }
+
         mp_obj_t len = mp_obj_len_maybe(arg);
         if (len != MP_OBJ_NULL) {
             // obj has a length, truth determined if len != 0
             return len != MP_OBJ_NEW_SMALL_INT(0);
         } else {
-            // TODO check for __bool__ method
-            // TODO check floats and complex numbers
-
-            // any other obj is true (TODO is that correct?)
+            // any other obj is true per Python semantics
             return 1;
         }
     }
@@ -476,7 +481,7 @@
     if (MP_OBJ_IS_SMALL_INT(arg)) {
         mp_small_int_t val = MP_OBJ_SMALL_INT_VALUE(arg);
         switch (op) {
-            case RT_UNARY_OP_NOT: if (val == 0) { return mp_const_true;} else { return mp_const_false; }
+            case RT_UNARY_OP_BOOL: return MP_BOOL(val != 0);
             case RT_UNARY_OP_POSITIVE: break;
             case RT_UNARY_OP_NEGATIVE: val = -val; break;
             case RT_UNARY_OP_INVERT: val = ~val; break;
diff --git a/py/runtime0.h b/py/runtime0.h
index cd82b14..a0f553a 100644
--- a/py/runtime0.h
+++ b/py/runtime0.h
@@ -1,8 +1,11 @@
 typedef enum {
-    RT_UNARY_OP_NOT, // TODO remove this op since it's no longer needed
+    RT_UNARY_OP_BOOL, // __bool__
+    RT_UNARY_OP_LEN, // __len__
     RT_UNARY_OP_POSITIVE,
     RT_UNARY_OP_NEGATIVE,
     RT_UNARY_OP_INVERT,
+    // Used only for CPython-compatible codegeneration
+    RT_UNARY_OP_NOT,
 } rt_unary_op_t;
 
 typedef enum {
diff --git a/tests/basics/true-value.py b/tests/basics/true-value.py
index 6ba410d..1dd547f 100644
--- a/tests/basics/true-value.py
+++ b/tests/basics/true-value.py
@@ -3,9 +3,18 @@
 if not False:
     print("False")
 
+if not None:
+    print("None")
+
 if not 0:
     print("0")
 
+if not 0.0:
+    print("float 0")
+
+if not 0+0j:
+    print("complex 0")
+
 if not "":
     print("Empty string")
 if "foo":