Rework the public API (#4829)
Related to #4186. Some notable changes: - The term 'Error' now strictly refers to native Error objects defined in the ECMA standard, which are ordinary objects. All other uses of 'error' or 'error reference' where the term refers to a thrown value is now called 'exception'. - Simplified the naming scheme of many String API functions. These functions will now also take an 'encoding' argument to specify the desired encoding in which to operate. - Removed the substring-copy-to-buffer functions. These functions behaved awkwardly, as they use character index to specify the start/end positions, and were mostly used incorrectly with byte offsets instead. The functionality can still be replicated with other functions if necessary. - String-to-buffer functions will no longer fail if the buffer is not sufficiently large, the string will instead be cropped. - Fixed the usage of the '_sz' prefix in many API functions. The term 'sz' means zero-terminated string in hungarian notation, this was used incorrectly in many cases. - Renamed most of the public API functions to have shorter, more on-point names, rather than the often too long descriptive names. Functions are now also grouped by the type of value they operate on, where this makes sense. JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai dbatyai@inf.u-szeged.hu
This commit is contained in:
@@ -33,7 +33,7 @@ vm_throw_callback (const jerry_value_t error_value, /**< captured error */
|
||||
case 0:
|
||||
{
|
||||
TEST_ASSERT (counter == 1);
|
||||
TEST_ASSERT (jerry_value_is_number (error_value) && jerry_get_number_value (error_value) == -5.6);
|
||||
TEST_ASSERT (jerry_value_is_number (error_value) && jerry_value_as_number (error_value) == -5.6);
|
||||
break;
|
||||
}
|
||||
case 1:
|
||||
@@ -52,15 +52,15 @@ vm_throw_callback (const jerry_value_t error_value, /**< captured error */
|
||||
|
||||
TEST_ASSERT (counter >= 1 && counter <= 3);
|
||||
TEST_ASSERT (jerry_value_is_string (error_value));
|
||||
TEST_ASSERT (jerry_get_string_size (error_value) == size);
|
||||
TEST_ASSERT (jerry_string_to_char_buffer (error_value, string_buf, size) == size);
|
||||
TEST_ASSERT (jerry_string_size (error_value, JERRY_ENCODING_CESU8) == size);
|
||||
TEST_ASSERT (jerry_string_to_buffer (error_value, JERRY_ENCODING_CESU8, string_buf, size) == size);
|
||||
TEST_ASSERT (string_buf[0] == 'e' && string_buf[1] == (char) ('0' + counter));
|
||||
break;
|
||||
}
|
||||
case 3:
|
||||
{
|
||||
TEST_ASSERT (counter == 1);
|
||||
TEST_ASSERT (jerry_get_error_type (error_value) == JERRY_ERROR_RANGE);
|
||||
TEST_ASSERT (jerry_error_type (error_value) == JERRY_ERROR_RANGE);
|
||||
break;
|
||||
}
|
||||
case 4:
|
||||
@@ -69,7 +69,7 @@ vm_throw_callback (const jerry_value_t error_value, /**< captured error */
|
||||
TEST_ASSERT (counter >= 1 && counter <= 2);
|
||||
|
||||
jerry_error_t error = (counter == 1) ? JERRY_ERROR_REFERENCE : JERRY_ERROR_TYPE;
|
||||
TEST_ASSERT (jerry_get_error_type (error_value) == error);
|
||||
TEST_ASSERT (jerry_error_type (error_value) == error);
|
||||
break;
|
||||
}
|
||||
case 5:
|
||||
@@ -100,23 +100,23 @@ native_handler (const jerry_call_info_t *call_info_p, /**< call info */
|
||||
|
||||
if (mode == 7)
|
||||
{
|
||||
jerry_value_t result = jerry_create_error (JERRY_ERROR_COMMON, (const jerry_char_t *) "Error!");
|
||||
jerry_value_t result = jerry_throw_sz (JERRY_ERROR_COMMON, "Error!");
|
||||
|
||||
TEST_ASSERT (!jerry_error_is_throw_captured (result));
|
||||
jerry_error_set_throw_capture (result, false);
|
||||
TEST_ASSERT (jerry_error_is_throw_captured (result));
|
||||
TEST_ASSERT (!jerry_exception_is_captured (result));
|
||||
jerry_exception_allow_capture (result, false);
|
||||
TEST_ASSERT (jerry_exception_is_captured (result));
|
||||
return result;
|
||||
}
|
||||
|
||||
jerry_char_t source[] = TEST_STRING_LITERAL ("throw false");
|
||||
jerry_value_t result = jerry_eval (source, sizeof (source) - 1, JERRY_PARSE_NO_OPTS);
|
||||
|
||||
TEST_ASSERT (jerry_error_is_throw_captured (result));
|
||||
TEST_ASSERT (jerry_exception_is_captured (result));
|
||||
|
||||
if (mode == 6)
|
||||
{
|
||||
jerry_error_set_throw_capture (result, true);
|
||||
TEST_ASSERT (!jerry_error_is_throw_captured (result));
|
||||
jerry_exception_allow_capture (result, true);
|
||||
TEST_ASSERT (!jerry_exception_is_captured (result));
|
||||
}
|
||||
return result;
|
||||
} /* native_handler */
|
||||
@@ -126,8 +126,8 @@ do_eval (const char *script_p, /**< script to evaluate */
|
||||
bool should_throw) /**< script throws an error */
|
||||
{
|
||||
jerry_value_t result = jerry_eval ((const jerry_char_t *) script_p, strlen (script_p), JERRY_PARSE_NO_OPTS);
|
||||
TEST_ASSERT (jerry_value_is_error (result) == should_throw);
|
||||
jerry_release_value (result);
|
||||
TEST_ASSERT (jerry_value_is_exception (result) == should_throw);
|
||||
jerry_value_free (result);
|
||||
} /* do_eval */
|
||||
|
||||
int
|
||||
@@ -136,14 +136,14 @@ main (void)
|
||||
TEST_INIT ();
|
||||
|
||||
/* Test stopping an infinite loop. */
|
||||
if (!jerry_is_feature_enabled (JERRY_FEATURE_VM_THROW))
|
||||
if (!jerry_feature_enabled (JERRY_FEATURE_VM_THROW))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
jerry_init (JERRY_INIT_EMPTY);
|
||||
|
||||
jerry_set_vm_throw_callback (vm_throw_callback, (void *) &mode);
|
||||
jerry_on_throw (vm_throw_callback, (void *) &mode);
|
||||
|
||||
mode = 0;
|
||||
counter = 0;
|
||||
@@ -183,14 +183,14 @@ main (void)
|
||||
TEST_ASSERT (counter == 2);
|
||||
|
||||
/* Native functions may trigger the call twice: */
|
||||
jerry_value_t global_object_value = jerry_get_global_object ();
|
||||
jerry_value_t function_value = jerry_create_external_function (native_handler);
|
||||
jerry_value_t function_name_value = jerry_create_string ((const jerry_char_t *) "native");
|
||||
jerry_value_t global_object_value = jerry_current_realm ();
|
||||
jerry_value_t function_value = jerry_function_external (native_handler);
|
||||
jerry_value_t function_name_value = jerry_string_sz ("native");
|
||||
|
||||
jerry_release_value (jerry_set_property (global_object_value, function_name_value, function_value));
|
||||
jerry_release_value (function_name_value);
|
||||
jerry_release_value (function_value);
|
||||
jerry_release_value (global_object_value);
|
||||
jerry_value_free (jerry_object_set (global_object_value, function_name_value, function_value));
|
||||
jerry_value_free (function_name_value);
|
||||
jerry_value_free (function_value);
|
||||
jerry_value_free (global_object_value);
|
||||
|
||||
mode = 5;
|
||||
counter = 0;
|
||||
@@ -222,11 +222,11 @@ main (void)
|
||||
true);
|
||||
TEST_ASSERT (counter == 1);
|
||||
|
||||
jerry_value_t value = jerry_create_object ();
|
||||
TEST_ASSERT (!jerry_error_is_throw_captured (value));
|
||||
jerry_error_set_throw_capture (value, false);
|
||||
TEST_ASSERT (!jerry_error_is_throw_captured (value));
|
||||
jerry_release_value (value);
|
||||
jerry_value_t value = jerry_object ();
|
||||
TEST_ASSERT (!jerry_exception_is_captured (value));
|
||||
jerry_exception_allow_capture (value, false);
|
||||
TEST_ASSERT (!jerry_exception_is_captured (value));
|
||||
jerry_value_free (value);
|
||||
|
||||
jerry_cleanup ();
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user