Skip to content

Check error values in API functions #1167

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions jerry-core/jerry-api.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ bool jerry_value_to_boolean (const jerry_value_t);
jerry_value_t jerry_value_to_number (const jerry_value_t);
jerry_value_t jerry_value_to_object (const jerry_value_t);
jerry_value_t jerry_value_to_string (const jerry_value_t);
jerry_value_t jerry_value_remove_error_flag (const jerry_value_t);

/**
* Create functions of 'jerry_value_t'
Expand Down
67 changes: 66 additions & 1 deletion jerry-core/jerry.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ static jerry_flag_t jerry_flags;
*/
static bool jerry_api_available;

static const jerry_char_t *error_value_msg_p = (const jerry_char_t *) "argument cannot have an error flag";

/** \addtogroup jerry_extension Jerry engine extension interface
* @{
*/
Expand Down Expand Up @@ -542,13 +544,18 @@ jerry_create_array_object (jerry_size_t size) /**< size of array */
* Set value of field in the specified array object
*
* @return true, if field value was set successfully
* throw exception, otherwise
* false, otherwise
*/
bool
jerry_set_array_index_value (jerry_object_t *array_obj_p, /**< array object */
jerry_length_t index, /**< index to be written */
jerry_value_t value) /**< value to set */
{
if (ECMA_IS_VALUE_ERROR (value))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the "throw exception, otherwise" comment above is incorrect

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

{
return false;
}

ecma_string_t *str_idx_p = ecma_new_ecma_string_from_uint32 ((uint32_t) index);
ecma_value_t set_completion = ecma_op_object_put (array_obj_p, str_idx_p, value, false);
JERRY_ASSERT (!ECMA_IS_VALUE_ERROR (set_completion));
Expand Down Expand Up @@ -857,6 +864,11 @@ jerry_add_object_field (jerry_object_t *object_p, /**< object to add field at */
{
jerry_assert_api_available ();

if (ECMA_IS_VALUE_ERROR (field_value))
{
return false;
}

bool is_successful = false;

if (ecma_get_object_extensible (object_p))
Expand Down Expand Up @@ -1053,6 +1065,11 @@ jerry_set_object_field_value_sz (jerry_object_t *object_p, /**< object */
{
jerry_assert_api_available ();

if (ECMA_IS_VALUE_ERROR (field_value))
{
return false;
}

bool is_successful = true;

ecma_string_t *field_name_str_p = ecma_new_ecma_string_from_utf8 ((lit_utf8_byte_t *) field_name_p,
Expand Down Expand Up @@ -1235,6 +1252,14 @@ jerry_call_function (jerry_object_t *function_object_p, /**< function object to
{
jerry_assert_api_available ();

for (uint16_t i = 0; i < args_count; i++)
{
if (ECMA_IS_VALUE_ERROR (args_p[i]))
{
return jerry_create_object_value (jerry_create_error (JERRY_ERROR_TYPE, error_value_msg_p));
}
}

if (jerry_is_function (function_object_p))
{
return jerry_invoke_function (false, function_object_p, this_arg_p, args_p, args_count);
Expand All @@ -1260,6 +1285,14 @@ jerry_construct_object (jerry_object_t *function_object_p, /**< function object
{
jerry_assert_api_available ();

for (uint16_t i = 0; i < args_count; i++)
{
if (ECMA_IS_VALUE_ERROR (args_p[i]))
{
return jerry_create_object_value (jerry_create_error (JERRY_ERROR_TYPE, error_value_msg_p));
}
}

if (jerry_is_constructor (function_object_p))
{
return jerry_invoke_function (true, function_object_p, NULL, args_p, args_count);
Expand Down Expand Up @@ -2115,6 +2148,11 @@ jerry_value_to_boolean (const jerry_value_t value) /**< input value */
{
jerry_assert_api_available ();

if (ECMA_IS_VALUE_ERROR (value))
{
return false;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a value with error flag has a logical value. Or you do this for consistency?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Come to think of it, this is not a conversion, so even the internal api has a wrong name. Hence the value is not converted to boolen (e.g. a Boolean object is always true) so perhaps we need another conversion routine.

var a = new Boolean(false); if (a) print("TRUE"); // yields true, although a to_boolean should yield false.

I would change this function to jerry_get_logical_value

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO, this should be ToBoolean function of the standard: http://www.ecma-international.org/ecma-262/5.1/index.html#sec-9.2
I think the name is good as is.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

return ecma_op_to_boolean (value);
} /* jerry_value_to_boolean */

Expand All @@ -2132,6 +2170,11 @@ jerry_value_to_number (const jerry_value_t value) /**< input value */
{
jerry_assert_api_available ();

if (ECMA_IS_VALUE_ERROR (value))
{
return jerry_create_object_value (jerry_create_error (JERRY_ERROR_TYPE, error_value_msg_p));
}

return ecma_op_to_number (value);
} /* jerry_value_to_number */

Expand All @@ -2149,6 +2192,11 @@ jerry_value_to_object (const jerry_value_t value) /**< input value */
{
jerry_assert_api_available ();

if (ECMA_IS_VALUE_ERROR (value))
{
return jerry_create_object_value (jerry_create_error (JERRY_ERROR_TYPE, error_value_msg_p));
}

return ecma_op_to_object (value);
} /* jerry_value_to_object */

Expand All @@ -2166,9 +2214,26 @@ jerry_value_to_string (const jerry_value_t value) /**< input value */
{
jerry_assert_api_available ();

if (ECMA_IS_VALUE_ERROR (value))
{
return jerry_create_object_value (jerry_create_error (JERRY_ERROR_TYPE, error_value_msg_p));
}

return ecma_op_to_string (value);
} /* jerry_value_to_string */

/**
* Remove the error flag
*
* @return converted normal value, if value is error
* unchanged value otherwise
*/
jerry_value_t
jerry_value_remove_error_flag (const jerry_value_t value)
{
return (jerry_value_t) (value & ~ECMA_VALUE_ERROR_FLAG);
} /* jerry_value_remove_error_flag */

/**
* Get size of Jerry string
*
Expand Down
1 change: 1 addition & 0 deletions main-unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@ main (int argc,
}
else if (!jerry_value_is_undefined (err_value))
{
err_value = jerry_value_remove_error_flag (err_value);
err_str_p = jerry_get_string_value (jerry_value_to_string (err_value));
jerry_release_value (err_value);
}
Expand Down