Introduce C API to query the type of an Error object (#2177)

New api function:
* jerry_get_error_type

Additionally update a few places where this new function
can be used.

JerryScript-DCO-1.0-Signed-off-by: Peter Gal pgal.u-szeged@partner.samsung.com
This commit is contained in:
Péter Gál
2018-02-06 09:41:54 +01:00
committed by László Langó
parent 8041953a7a
commit 6f339eb05c
10 changed files with 217 additions and 183 deletions
+66
View File
@@ -0,0 +1,66 @@
/* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "jerryscript.h"
#include "test-common.h"
int
main (void)
{
TEST_INIT ();
jerry_init (JERRY_INIT_EMPTY);
jerry_error_t errors[] =
{
JERRY_ERROR_COMMON,
JERRY_ERROR_EVAL,
JERRY_ERROR_RANGE,
JERRY_ERROR_REFERENCE,
JERRY_ERROR_SYNTAX,
JERRY_ERROR_TYPE,
JERRY_ERROR_URI
};
for (size_t idx = 0; idx < sizeof (errors) / sizeof (errors[0]); idx++)
{
jerry_value_t error_obj = jerry_create_error (errors[idx], (const jerry_char_t *)"test");
TEST_ASSERT (jerry_value_has_error_flag (error_obj));
TEST_ASSERT (jerry_get_error_type (error_obj) == errors[idx]);
jerry_value_clear_error_flag (&error_obj);
TEST_ASSERT (jerry_get_error_type (error_obj) == errors[idx]);
jerry_release_value (error_obj);
}
jerry_value_t test_values[] =
{
jerry_create_number (11),
jerry_create_string ((const jerry_char_t *) "message"),
jerry_create_boolean (true),
jerry_create_object (),
};
for (size_t idx = 0; idx < sizeof (test_values) / sizeof (test_values[0]); idx++)
{
jerry_error_t error_type = jerry_get_error_type (test_values[idx]);
TEST_ASSERT (error_type == JERRY_ERROR_NONE);
jerry_release_value (test_values[idx]);
}
jerry_cleanup ();
} /* main */
+1 -18
View File
@@ -366,24 +366,7 @@ main (void)
{
jerry_value_t input_buffer = jerry_create_arraybuffer_external (0, NULL, NULL);
TEST_ASSERT (jerry_value_has_error_flag (input_buffer));
jerry_value_clear_error_flag (&input_buffer);
if (jerry_is_feature_enabled (JERRY_FEATURE_ERROR_MESSAGES))
{
jerry_char_t error_str[15];
jerry_char_t expected_error_str[15] = "RangeError";
jerry_char_t *name_str_p = (jerry_char_t *) "name";
jerry_value_t name_key = jerry_create_string (name_str_p);
jerry_value_t name_value = jerry_get_property (input_buffer, name_key);
jerry_size_t name_size = jerry_string_to_char_buffer (name_value, error_str, sizeof (error_str));
TEST_ASSERT (name_size == strlen ((char *) expected_error_str));
TEST_ASSERT (strncmp ((char *) error_str, (char *) expected_error_str, name_size) == 0);
jerry_release_value (name_value);
jerry_release_value (name_key);
}
TEST_ASSERT (jerry_get_error_type (input_buffer) == JERRY_ERROR_RANGE);
jerry_release_value (input_buffer);
}