Cleanup code around JERRY_UNREACHABLEs (#2342)

`JERRY_UNREACHABLE`s often signal code structure that could be
improved: they can usually either be rewritten to `JERRY_ASSERT`s
or eliminated by restructuring loops, `if`s or `#if`s. Roughly,
the only valid occurences are in default cases of `switch`es. And
even they can often be merged into non-default cases.

Moreover, it is dangerous to write meaningful code after
`JERRY_UNREACHABLE` because it pretends as if there was a way to
recover from an impossible situation.

This patch rewrites/eliminates `JERRY_UNREACHABLE`s where possible
and removes misleading code from after them.

JerryScript-DCO-1.0-Signed-off-by: Akos Kiss akiss@inf.u-szeged.hu
This commit is contained in:
Akos Kiss
2018-05-25 07:27:30 +02:00
committed by GitHub
parent acb3e71436
commit 4779451284
24 changed files with 168 additions and 256 deletions
@@ -259,6 +259,7 @@ ecma_builtin_date_prototype_dispatch_get (uint16_t builtin_routine_id, /**< buil
default:
{
JERRY_ASSERT (builtin_routine_id == ECMA_DATE_PROTOTYPE_GET_UTC_TIMEZONE_OFFSET);
date_num = ecma_date_timezone_offset (date_num);
break;
}
@@ -342,6 +343,7 @@ ecma_builtin_date_prototype_dispatch_set (uint16_t builtin_routine_id, /**< buil
{
JERRY_ASSERT (builtin_routine_id == ECMA_DATE_PROTOTYPE_SET_HOURS
|| builtin_routine_id == ECMA_DATE_PROTOTYPE_SET_UTC_HOURS);
conversions = 4;
break;
}
@@ -429,6 +431,7 @@ ecma_builtin_date_prototype_dispatch_set (uint16_t builtin_routine_id, /**< buil
{
JERRY_ASSERT (builtin_routine_id == ECMA_DATE_PROTOTYPE_SET_DATE
|| builtin_routine_id == ECMA_DATE_PROTOTYPE_SET_UTC_DATE);
day = converted_number[0];
break;
}
@@ -511,6 +514,7 @@ ecma_builtin_date_prototype_dispatch_set (uint16_t builtin_routine_id, /**< buil
{
JERRY_ASSERT (builtin_routine_id == ECMA_DATE_PROTOTYPE_SET_UTC_MILLISECONDS
|| builtin_routine_id == ECMA_DATE_PROTOTYPE_SET_MILLISECONDS);
ms = converted_number[0];
break;
}
@@ -643,6 +647,7 @@ ecma_builtin_date_prototype_dispatch_routine (uint16_t builtin_routine_id, /**<
default:
{
JERRY_ASSERT (builtin_routine_id == ECMA_DATE_PROTOTYPE_TO_UTC_STRING);
return ecma_date_value_to_utc_string (*prim_value_p);
}
}
@@ -264,13 +264,10 @@ ecma_builtin_global_object_parse_int (ecma_value_t this_arg, /**< this argument
{
current_number = (ecma_number_t) current_char - LIT_CHAR_UPPERCASE_A + 10;
}
else if (lit_char_is_decimal_digit (current_char))
{
current_number = (ecma_number_t) current_char - LIT_CHAR_0;
}
else
{
JERRY_UNREACHABLE ();
JERRY_ASSERT (lit_char_is_decimal_digit (current_char));
current_number = (ecma_number_t) current_char - LIT_CHAR_0;
}
value += current_number * multiplier;
@@ -740,8 +740,10 @@ ecma_date_to_string_format (ecma_number_t datetime_number, /**< datetime */
number_length = 2;
break;
}
case LIT_CHAR_UPPERCASE_Z: /* Time zone seconds part. */
default:
{
JERRY_ASSERT (*format_p == LIT_CHAR_UPPERCASE_Z); /* Time zone seconds part. */
int32_t time_zone = (int32_t) ecma_date_local_time_zone (datetime_number);
if (time_zone < 0)
@@ -753,11 +755,6 @@ ecma_date_to_string_format (ecma_number_t datetime_number, /**< datetime */
number_length = 2;
break;
}
default:
{
JERRY_UNREACHABLE ();
break;
}
}
format_p++;
@@ -1266,6 +1266,7 @@ ecma_builtin_json_quote (ecma_string_t *string_p) /**< string that should be quo
default:
{
JERRY_ASSERT (current_char == LIT_CHAR_BACKSLASH || current_char == LIT_CHAR_DOUBLE_QUOTE);
break;
}
}
@@ -416,6 +416,7 @@ ecma_instantiate_builtin_helper (ecma_builtin_id_t builtin_id, /**< built-in id
static void
ecma_instantiate_builtin (ecma_builtin_id_t id) /**< built-in id */
{
JERRY_ASSERT (id < ECMA_BUILTIN_ID__COUNT);
switch (id)
{
#define BUILTIN(builtin_id, \
@@ -437,8 +438,6 @@ ecma_instantiate_builtin (ecma_builtin_id_t id) /**< built-in id */
#undef BUILTIN_ROUTINE
default:
{
JERRY_ASSERT (id < ECMA_BUILTIN_ID__COUNT);
JERRY_UNREACHABLE (); /* The built-in is not implemented. */
}
}
@@ -685,11 +684,6 @@ ecma_builtin_try_to_instantiate_property (ecma_object_t *object_p, /**< object *
{
switch (curr_property_p->value)
{
case ECMA_BUILTIN_NUMBER_NAN:
{
num = ecma_number_make_nan ();
break;
}
case ECMA_BUILTIN_NUMBER_POSITIVE_INFINITY:
{
num = ecma_number_make_infinity (false);
@@ -702,7 +696,9 @@ ecma_builtin_try_to_instantiate_property (ecma_object_t *object_p, /**< object *
}
default:
{
JERRY_UNREACHABLE ();
JERRY_ASSERT (curr_property_p->value == ECMA_BUILTIN_NUMBER_NAN);
num = ecma_number_make_nan ();
break;
}
}
@@ -739,18 +735,15 @@ ecma_builtin_try_to_instantiate_property (ecma_object_t *object_p, /**< object *
setter_p = ecma_builtin_make_function_object_for_setter_accessor (builtin_id, setter_id);
break;
}
case ECMA_BUILTIN_PROPERTY_ACCESSOR_READ_ONLY:
default:
{
JERRY_ASSERT (curr_property_p->type == ECMA_BUILTIN_PROPERTY_ACCESSOR_READ_ONLY);
is_accessor = true;
getter_p = ecma_builtin_make_function_object_for_getter_accessor (builtin_id,
curr_property_p->value);
break;
}
default:
{
JERRY_UNREACHABLE ();
return NULL;
}
}
ecma_property_t *prop_p;
@@ -90,8 +90,7 @@ ecma_typedarray_helper_get_shift_size (uint8_t builtin_id) /**< the builtin id o
#endif /* CONFIG_ECMA_NUMBER_TYPE == CONFIG_ECMA_NUMBER_FLOAT64 */
default:
{
JERRY_ASSERT (false);
return ECMA_BUILTIN_ID__COUNT;
JERRY_UNREACHABLE ();
}
}
} /* ecma_typedarray_helper_get_shift_size */
@@ -125,8 +124,7 @@ ecma_typedarray_helper_get_magic_string (uint8_t builtin_id) /**< the builtin id
#endif /* CONFIG_ECMA_NUMBER_TYPE == CONFIG_ECMA_NUMBER_FLOAT64 */
default:
{
JERRY_ASSERT (false);
return LIT_MAGIC_STRING__COUNT;
JERRY_UNREACHABLE ();
}
}
#undef TYPEDARRAY_ID_CASE
@@ -161,8 +159,7 @@ ecma_typedarray_helper_get_prototype_id (uint8_t builtin_id) /**< the builtin id
#endif /* CONFIG_ECMA_NUMBER_TYPE == CONFIG_ECMA_NUMBER_FLOAT64 */
default:
{
JERRY_ASSERT (false);
return ECMA_BUILTIN_ID__COUNT;
JERRY_UNREACHABLE ();
}
}
#undef TYPEDARRAY_ID_CASE