mirror of
https://github.com/jerryscript-project/jerryscript.git
synced 2025-12-15 16:29:21 +00:00
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
94 lines
2.9 KiB
C
94 lines
2.9 KiB
C
/* 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"
|
|
|
|
static bool error_object_created_callback_is_running = false;
|
|
static int error_object_created_callback_count = 0;
|
|
|
|
static void
|
|
error_object_created_callback (const jerry_value_t error_object_t, /**< new error object */
|
|
void *user_p) /**< user pointer */
|
|
{
|
|
TEST_ASSERT (!error_object_created_callback_is_running);
|
|
TEST_ASSERT (user_p == (void *) &error_object_created_callback_count);
|
|
|
|
error_object_created_callback_is_running = true;
|
|
error_object_created_callback_count++;
|
|
|
|
jerry_value_t name = jerry_string_sz ("message");
|
|
jerry_value_t message = jerry_string_sz ("Replaced message!");
|
|
|
|
jerry_value_t result = jerry_object_set (error_object_t, name, message);
|
|
TEST_ASSERT (jerry_value_is_boolean (result) && jerry_value_is_true (result));
|
|
jerry_value_free (result);
|
|
|
|
/* This SyntaxError must not trigger a recusrsive call of the this callback. */
|
|
const char *source_p = "Syntax Error in JS!";
|
|
result = jerry_eval ((const jerry_char_t *) source_p, strlen (source_p), 0);
|
|
TEST_ASSERT (jerry_value_is_exception (result));
|
|
|
|
jerry_value_free (result);
|
|
jerry_value_free (message);
|
|
jerry_value_free (name);
|
|
|
|
error_object_created_callback_is_running = false;
|
|
} /* error_object_created_callback */
|
|
|
|
static void
|
|
run_test (const char *source_p)
|
|
{
|
|
/* Run the code 5 times. */
|
|
for (int i = 0; i < 5; i++)
|
|
{
|
|
jerry_value_t result = jerry_eval ((const jerry_char_t *) source_p, strlen (source_p), 0);
|
|
TEST_ASSERT (jerry_value_is_boolean (result) && jerry_value_is_true (result));
|
|
jerry_value_free (result);
|
|
}
|
|
} /* run_test */
|
|
|
|
/**
|
|
* Unit test's main function.
|
|
*/
|
|
int
|
|
main (void)
|
|
{
|
|
TEST_INIT ();
|
|
|
|
jerry_init (JERRY_INIT_EMPTY);
|
|
|
|
jerry_error_on_created (error_object_created_callback, (void *) &error_object_created_callback_count);
|
|
|
|
run_test ("var result = false\n"
|
|
"try {\n"
|
|
" ref_error;\n"
|
|
"} catch(e) {\n"
|
|
" result = (e.message === 'Replaced message!')\n"
|
|
"}\n"
|
|
"result\n");
|
|
|
|
run_test ("var error = new Error()\n"
|
|
"error.message === 'Replaced message!'\n");
|
|
|
|
jerry_value_free (jerry_error_sz (JERRY_ERROR_COMMON, "Message"));
|
|
|
|
TEST_ASSERT (error_object_created_callback_count == 11);
|
|
|
|
jerry_cleanup ();
|
|
return 0;
|
|
} /* main */
|