diff --git a/src/libecmabuiltins/ecma-builtin-error-prototype.c b/src/libecmabuiltins/ecma-builtin-error-prototype.c index 627286309..02cec8fc8 100644 --- a/src/libecmabuiltins/ecma-builtin-error-prototype.c +++ b/src/libecmabuiltins/ecma-builtin-error-prototype.c @@ -120,48 +120,61 @@ ecma_builtin_error_prototype_object_to_string (ecma_value_t this) /**< this argu ecma_string_t *name_string_p = ECMA_GET_POINTER (name_to_str_completion.u.value.value); ecma_string_t *msg_string_p = ECMA_GET_POINTER (msg_to_str_completion.u.value.value); - const ecma_char_t *colon_zt_magic_string_p = ecma_get_magic_string_zt (ECMA_MAGIC_STRING_COLON_CHAR); - const ecma_char_t *space_zt_magic_string_p = ecma_get_magic_string_zt (ECMA_MAGIC_STRING_SPACE_CHAR); + ecma_string_t *ret_str_p; + + if (ecma_string_get_length (name_string_p) == 0) + { + ret_str_p = ecma_copy_or_ref_ecma_string (msg_string_p); + } + else if (ecma_string_get_length (msg_string_p) == 0) + { + ret_str_p = ecma_copy_or_ref_ecma_string (name_string_p); + } + else + { + const ecma_char_t *colon_zt_magic_string_p = ecma_get_magic_string_zt (ECMA_MAGIC_STRING_COLON_CHAR); + const ecma_char_t *space_zt_magic_string_p = ecma_get_magic_string_zt (ECMA_MAGIC_STRING_SPACE_CHAR); - const int32_t len = (ecma_string_get_length (name_string_p) + - ecma_string_get_length (msg_string_p) + - ecma_zt_string_length (colon_zt_magic_string_p) + - ecma_zt_string_length (space_zt_magic_string_p)); + const int32_t len = (ecma_string_get_length (name_string_p) + + ecma_string_get_length (msg_string_p) + + ecma_zt_string_length (colon_zt_magic_string_p) + + ecma_zt_string_length (space_zt_magic_string_p)); - const ssize_t buffer_size = (len + 1) * (ssize_t) sizeof (ecma_char_t); - ssize_t bytes, buffer_size_left = buffer_size; + const ssize_t buffer_size = (len + 1) * (ssize_t) sizeof (ecma_char_t); + ssize_t bytes, buffer_size_left = buffer_size; - ecma_char_t ret_str_buffer [buffer_size]; - ecma_char_t *ret_str_buffer_p = ret_str_buffer; + ecma_char_t ret_str_buffer [buffer_size]; + ecma_char_t *ret_str_buffer_p = ret_str_buffer; - bytes = ecma_string_to_zt_string (name_string_p, ret_str_buffer_p, buffer_size_left); - JERRY_ASSERT (bytes >= 1 && buffer_size_left - bytes >= 0); + bytes = ecma_string_to_zt_string (name_string_p, ret_str_buffer_p, buffer_size_left); + JERRY_ASSERT (bytes >= 1 && buffer_size_left - bytes >= 0); - buffer_size_left -= bytes - 1 /* null character */; - ret_str_buffer_p = (ecma_char_t*) ((uint8_t*) ret_str_buffer + (buffer_size - buffer_size_left)); + buffer_size_left -= bytes - 1 /* null character */; + ret_str_buffer_p = (ecma_char_t*) ((uint8_t*) ret_str_buffer + (buffer_size - buffer_size_left)); - ret_str_buffer_p = ecma_copy_zt_string_to_buffer (colon_zt_magic_string_p, - ret_str_buffer_p, - buffer_size_left); - buffer_size_left = buffer_size - (ret_str_buffer_p - ret_str_buffer) * (ssize_t) sizeof (ecma_char_t); - JERRY_ASSERT (buffer_size_left >= 0); + ret_str_buffer_p = ecma_copy_zt_string_to_buffer (colon_zt_magic_string_p, + ret_str_buffer_p, + buffer_size_left); + buffer_size_left = buffer_size - (ret_str_buffer_p - ret_str_buffer) * (ssize_t) sizeof (ecma_char_t); + JERRY_ASSERT (buffer_size_left >= 0); - ret_str_buffer_p = ecma_copy_zt_string_to_buffer (space_zt_magic_string_p, - ret_str_buffer_p, - buffer_size_left); - buffer_size_left = buffer_size - (ret_str_buffer_p - ret_str_buffer) * (ssize_t) sizeof (ecma_char_t); - JERRY_ASSERT (buffer_size_left >= 0); + ret_str_buffer_p = ecma_copy_zt_string_to_buffer (space_zt_magic_string_p, + ret_str_buffer_p, + buffer_size_left); + buffer_size_left = buffer_size - (ret_str_buffer_p - ret_str_buffer) * (ssize_t) sizeof (ecma_char_t); + JERRY_ASSERT (buffer_size_left >= 0); - bytes = ecma_string_to_zt_string (msg_string_p, ret_str_buffer_p, buffer_size_left); - JERRY_ASSERT (bytes >= 1 && buffer_size_left - bytes >= 0); + bytes = ecma_string_to_zt_string (msg_string_p, ret_str_buffer_p, buffer_size_left); + JERRY_ASSERT (bytes >= 1 && buffer_size_left - bytes >= 0); - buffer_size_left -= bytes - 1 /* null character */; - ret_str_buffer_p = (ecma_char_t*) ((uint8_t*) ret_str_buffer + (buffer_size - buffer_size_left)); + buffer_size_left -= bytes - 1 /* null character */; + ret_str_buffer_p = (ecma_char_t*) ((uint8_t*) ret_str_buffer + (buffer_size - buffer_size_left)); - JERRY_ASSERT (buffer_size_left >= (ssize_t) sizeof (ecma_char_t)); - *ret_str_buffer_p = ECMA_CHAR_NULL; + JERRY_ASSERT (buffer_size_left >= (ssize_t) sizeof (ecma_char_t)); + *ret_str_buffer_p = ECMA_CHAR_NULL; - ecma_string_t *ret_str_p = ecma_new_ecma_string (ret_str_buffer); + ret_str_p = ecma_new_ecma_string (ret_str_buffer); + } ret_value = ecma_make_normal_completion_value (ecma_make_string_value (ret_str_p)); } diff --git a/src/libecmabuiltins/ecma-builtin-error.c b/src/libecmabuiltins/ecma-builtin-error.c index 88e6de577..7a10413ed 100644 --- a/src/libecmabuiltins/ecma-builtin-error.c +++ b/src/libecmabuiltins/ecma-builtin-error.c @@ -52,49 +52,30 @@ ecma_builtin_error_dispatch_call (ecma_value_t *arguments_list_p, /**< arguments { JERRY_ASSERT (arguments_list_len == 0 || arguments_list_p != NULL); - ecma_completion_value_t msg_to_str_completion = ecma_make_empty_completion_value ();; - if (arguments_list_len != 0 && !ecma_is_value_undefined (arguments_list_p [0])) { - msg_to_str_completion = ecma_op_to_string (arguments_list_p[0]); + ecma_completion_value_t ret_value; - if (!ecma_is_completion_value_normal (msg_to_str_completion)) - { - return msg_to_str_completion; - } + ECMA_TRY_CATCH (msg_to_str_completion, + ecma_op_to_string (arguments_list_p[0]), + ret_value); + + ecma_string_t *message_string_p = ECMA_GET_POINTER (msg_to_str_completion.u.value.value); + ecma_object_t *new_error_object_p = ecma_new_standard_error_with_message (ECMA_ERROR_COMMON, + message_string_p); + ret_value = ecma_make_normal_completion_value (ecma_make_object_value (new_error_object_p)); + + ECMA_FINALIZE (msg_to_str_completion); + + return ret_value; } - - ecma_object_t *error_prototype_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_ERROR_PROTOTYPE); - - ecma_object_t *new_error_object_p = ecma_create_object (error_prototype_obj_p, - true, - ECMA_OBJECT_TYPE_GENERAL); - - ecma_deref_object (error_prototype_obj_p); - - ecma_property_t *class_prop_p = ecma_create_internal_property (new_error_object_p, - ECMA_INTERNAL_PROPERTY_CLASS); - class_prop_p->u.internal_property.value = ECMA_MAGIC_STRING_ERROR_UL; - - if (!ecma_is_completion_value_empty (msg_to_str_completion)) + else { - ecma_string_t *message_magic_string_p = ecma_get_magic_string (ECMA_MAGIC_STRING_MESSAGE); + ecma_object_t *new_error_object_p = ecma_new_standard_error (ECMA_ERROR_COMMON); - ecma_property_t *prop_p = ecma_create_named_data_property (new_error_object_p, - message_magic_string_p, - ECMA_PROPERTY_WRITABLE, - ECMA_PROPERTY_NOT_ENUMERABLE, - ECMA_PROPERTY_CONFIGURABLE); - prop_p->u.named_data_property.value = ecma_copy_value (msg_to_str_completion.u.value, true); - ecma_gc_update_may_ref_younger_object_flag_by_value (new_error_object_p, prop_p->u.named_data_property.value); - - ecma_deref_ecma_string (message_magic_string_p); - - ecma_free_completion_value (msg_to_str_completion); + return ecma_make_normal_completion_value (ecma_make_object_value (new_error_object_p)); } - - return ecma_make_normal_completion_value (ecma_make_object_value (new_error_object_p)); } /* ecma_builtin_error_dispatch_call */ /** diff --git a/src/libecmabuiltins/ecma-builtin-global.inc.h b/src/libecmabuiltins/ecma-builtin-global.inc.h index 22238a2ed..6f13e8f26 100644 --- a/src/libecmabuiltins/ecma-builtin-global.inc.h +++ b/src/libecmabuiltins/ecma-builtin-global.inc.h @@ -166,11 +166,11 @@ CP_UNIMPLEMENTED_VALUE (ECMA_MAGIC_STRING_SYNTAX_ERROR_UL, ECMA_PROPERTY_CONFIGURABLE) // ECMA-262 v5, 15.1.4.14 -CP_UNIMPLEMENTED_VALUE (ECMA_MAGIC_STRING_TYPE_ERROR_UL, - ecma_builtin_get (ECMA_BUILTIN_ID_TYPE_ERROR), - ECMA_PROPERTY_WRITABLE, - ECMA_PROPERTY_NOT_ENUMERABLE, - ECMA_PROPERTY_CONFIGURABLE) +OBJECT_VALUE (ECMA_MAGIC_STRING_TYPE_ERROR_UL, + ecma_builtin_get (ECMA_BUILTIN_ID_TYPE_ERROR), + ECMA_PROPERTY_WRITABLE, + ECMA_PROPERTY_NOT_ENUMERABLE, + ECMA_PROPERTY_CONFIGURABLE) // ECMA-262 v5, 15.1.4.15 CP_UNIMPLEMENTED_VALUE (ECMA_MAGIC_STRING_URI_ERROR_UL, diff --git a/src/libecmabuiltins/ecma-builtin-typeerror-prototype.c b/src/libecmabuiltins/ecma-builtin-typeerror-prototype.c new file mode 100644 index 000000000..efda0b33c --- /dev/null +++ b/src/libecmabuiltins/ecma-builtin-typeerror-prototype.c @@ -0,0 +1,33 @@ +/* Copyright 2014 Samsung Electronics Co., Ltd. + * + * 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 "ecma-alloc.h" +#include "ecma-builtins.h" +#include "ecma-conversion.h" +#include "ecma-exceptions.h" +#include "ecma-gc.h" +#include "ecma-globals.h" +#include "ecma-helpers.h" +#include "ecma-objects.h" +#include "ecma-string-object.h" +#include "ecma-try-catch-macro.h" +#include "globals.h" + +#define ECMA_BUILTINS_INTERNAL +#include "ecma-builtins-internal.h" + +#define BUILTIN_INC_HEADER_NAME "ecma-builtin-typeerror-prototype.inc.h" +#define BUILTIN_UNDERSCORED_ID type_error_prototype +#include "ecma-builtin-internal-routines-template.inc.h" diff --git a/src/libecmabuiltins/ecma-builtin-typeerror-prototype.inc.h b/src/libecmabuiltins/ecma-builtin-typeerror-prototype.inc.h new file mode 100644 index 000000000..f93635684 --- /dev/null +++ b/src/libecmabuiltins/ecma-builtin-typeerror-prototype.inc.h @@ -0,0 +1,65 @@ +/* Copyright 2014 Samsung Electronics Co., Ltd. + * + * 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. + */ + +/* + * TypeError.prototype built-in description + */ + +#ifndef OBJECT_ID +# define OBJECT_ID(builtin_object_id) +#endif /* !OBJECT_ID */ + +#ifndef STRING_VALUE +# define STRING_VALUE(name, magic_string_id, prop_writable, prop_enumerable, prop_configurable) +#endif /* !STRING_VALUE */ + +#ifndef OBJECT_VALUE +# define OBJECT_VALUE(name, obj_getter, prop_writable, prop_enumerable, prop_configurable) +#endif /* !OBJECT_VALUE */ + +/* Object identifier */ +OBJECT_ID (ECMA_BUILTIN_ID_TYPE_ERROR_PROTOTYPE) + +/* Object properties: + * (property name, object pointer getter) */ + +// 15.11.7.8 +OBJECT_VALUE (ECMA_MAGIC_STRING_CONSTRUCTOR, + ecma_builtin_get (ECMA_BUILTIN_ID_TYPE_ERROR), + ECMA_PROPERTY_WRITABLE, + ECMA_PROPERTY_NOT_ENUMERABLE, + ECMA_PROPERTY_CONFIGURABLE) + +// 15.11.7.9 +STRING_VALUE (ECMA_MAGIC_STRING_NAME, + ECMA_MAGIC_STRING_TYPE_ERROR_UL, + ECMA_PROPERTY_WRITABLE, + ECMA_PROPERTY_NOT_ENUMERABLE, + ECMA_PROPERTY_CONFIGURABLE) + +// 15.11.7.10 +STRING_VALUE (ECMA_MAGIC_STRING_MESSAGE, + ECMA_MAGIC_STRING__EMPTY, + ECMA_PROPERTY_WRITABLE, + ECMA_PROPERTY_NOT_ENUMERABLE, + ECMA_PROPERTY_CONFIGURABLE) + +#undef OBJECT_ID +#undef SIMPLE_VALUE +#undef NUMBER_VALUE +#undef STRING_VALUE +#undef OBJECT_VALUE +#undef CP_UNIMPLEMENTED_VALUE +#undef ROUTINE diff --git a/src/libecmabuiltins/ecma-builtin-typeerror.c b/src/libecmabuiltins/ecma-builtin-typeerror.c new file mode 100644 index 000000000..89ad0a13f --- /dev/null +++ b/src/libecmabuiltins/ecma-builtin-typeerror.c @@ -0,0 +1,97 @@ +/* Copyright 2014 Samsung Electronics Co., Ltd. + * + * 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 "ecma-alloc.h" +#include "ecma-builtins.h" +#include "ecma-conversion.h" +#include "ecma-exceptions.h" +#include "ecma-gc.h" +#include "ecma-globals.h" +#include "ecma-helpers.h" +#include "ecma-objects.h" +#include "ecma-try-catch-macro.h" +#include "globals.h" + +#define ECMA_BUILTINS_INTERNAL +#include "ecma-builtins-internal.h" + +#define BUILTIN_INC_HEADER_NAME "ecma-builtin-typeerror.inc.h" +#define BUILTIN_UNDERSCORED_ID type_error +#include "ecma-builtin-internal-routines-template.inc.h" + +/** \addtogroup ecma ECMA + * @{ + * + * \addtogroup ecmabuiltins + * @{ + * + * \addtogroup typeerror ECMA TypeError object built-in + * @{ + */ + +/** + * Handle calling [[Call]] of built-in TypeError object + * + * @return completion-value + */ +ecma_completion_value_t +ecma_builtin_type_error_dispatch_call (ecma_value_t *arguments_list_p, /**< arguments list */ + ecma_length_t arguments_list_len) /**< number of arguments */ +{ + JERRY_ASSERT (arguments_list_len == 0 || arguments_list_p != NULL); + + if (arguments_list_len != 0 + && !ecma_is_value_undefined (arguments_list_p [0])) + { + ecma_completion_value_t ret_value; + + ECMA_TRY_CATCH (msg_to_str_completion, + ecma_op_to_string (arguments_list_p[0]), + ret_value); + + ecma_string_t *message_string_p = ECMA_GET_POINTER (msg_to_str_completion.u.value.value); + ecma_object_t *new_error_object_p = ecma_new_standard_error_with_message (ECMA_ERROR_TYPE, + message_string_p); + ret_value = ecma_make_normal_completion_value (ecma_make_object_value (new_error_object_p)); + + ECMA_FINALIZE (msg_to_str_completion); + + return ret_value; + } + else + { + ecma_object_t *new_error_object_p = ecma_new_standard_error (ECMA_ERROR_TYPE); + + return ecma_make_normal_completion_value (ecma_make_object_value (new_error_object_p)); + } +} /* ecma_builtin_type_error_dispatch_call */ + +/** + * Handle calling [[Construct]] of built-in TypeError object + * + * @return completion-value + */ +ecma_completion_value_t +ecma_builtin_type_error_dispatch_construct (ecma_value_t *arguments_list_p, /**< arguments list */ + ecma_length_t arguments_list_len) /**< number of arguments */ +{ + return ecma_builtin_type_error_dispatch_call (arguments_list_p, arguments_list_len); +} /* ecma_builtin_type_error_dispatch_construct */ + +/** + * @} + * @} + * @} + */ diff --git a/src/libecmabuiltins/ecma-builtin-typeerror.inc.h b/src/libecmabuiltins/ecma-builtin-typeerror.inc.h new file mode 100644 index 000000000..3326c0de5 --- /dev/null +++ b/src/libecmabuiltins/ecma-builtin-typeerror.inc.h @@ -0,0 +1,65 @@ +/* Copyright 2014 Samsung Electronics Co., Ltd. + * + * 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. + */ + +/* + * TypeError built-in description + */ + +#ifndef OBJECT_ID +# define OBJECT_ID(builtin_object_id) +#endif /* !OBJECT_ID */ + +#ifndef NUMBER_VALUE +# define NUMBER_VALUE(name, number_value, prop_writable, prop_enumerable, prop_configurable) +#endif /* !NUMBER_VALUE */ + +#ifndef STRING_VALUE +# define STRING_VALUE(name, magic_string_id, prop_writable, prop_enumerable, prop_configurable) +#endif /* !STRING_VALUE */ + +#ifndef OBJECT_VALUE +# define OBJECT_VALUE(name, obj_getter, prop_writable, prop_enumerable, prop_configurable) +#endif /* !OBJECT_VALUE */ + +/* Object identifier */ +OBJECT_ID (ECMA_BUILTIN_ID_TYPE_ERROR) + +/* Number properties: + * (property name, number value, writable, enumerable, configurable) */ + +// 15.11.3 +NUMBER_VALUE (ECMA_MAGIC_STRING_LENGTH, + 1, + ECMA_PROPERTY_NOT_WRITABLE, + ECMA_PROPERTY_NOT_ENUMERABLE, + ECMA_PROPERTY_NOT_CONFIGURABLE) + +/* Object properties: + * (property name, object pointer getter) */ + +// 15.7.3.1 +OBJECT_VALUE (ECMA_MAGIC_STRING_PROTOTYPE, + ecma_builtin_get (ECMA_BUILTIN_ID_TYPE_ERROR_PROTOTYPE), + ECMA_PROPERTY_NOT_WRITABLE, + ECMA_PROPERTY_NOT_ENUMERABLE, + ECMA_PROPERTY_NOT_CONFIGURABLE) + +#undef OBJECT_ID +#undef SIMPLE_VALUE +#undef NUMBER_VALUE +#undef STRING_VALUE +#undef OBJECT_VALUE +#undef CP_UNIMPLEMENTED_VALUE +#undef ROUTINE diff --git a/src/libecmabuiltins/ecma-builtins-internal.h b/src/libecmabuiltins/ecma-builtins-internal.h index ed861a144..be3380bda 100644 --- a/src/libecmabuiltins/ecma-builtins-internal.h +++ b/src/libecmabuiltins/ecma-builtins-internal.h @@ -135,6 +135,16 @@ ecma_builtin_bin_search_for_magic_string_id_in_array (const ecma_magic_string_id ERROR_UL, \ ECMA_BUILTIN_ID_ERROR_PROTOTYPE, \ error) \ + macro (TYPE_ERROR_PROTOTYPE, \ + TYPE_GENERAL, \ + ERROR_UL, \ + ECMA_BUILTIN_ID_ERROR_PROTOTYPE, \ + type_error_prototype) \ + macro (TYPE_ERROR, \ + TYPE_FUNCTION, \ + ERROR_UL, \ + ECMA_BUILTIN_ID_TYPE_ERROR_PROTOTYPE, \ + type_error) \ macro (COMPACT_PROFILE_ERROR, \ TYPE_FUNCTION, \ COMPACT_PROFILE_ERROR_UL, \ diff --git a/src/libecmaoperations/ecma-exceptions.c b/src/libecmaoperations/ecma-exceptions.c index 54adae93f..9d7699cc1 100644 --- a/src/libecmaoperations/ecma-exceptions.c +++ b/src/libecmaoperations/ecma-exceptions.c @@ -13,7 +13,9 @@ * limitations under the License. */ +#include "ecma-builtins.h" #include "ecma-exceptions.h" +#include "ecma-gc.h" #include "ecma-globals.h" #include "ecma-helpers.h" #include "globals.h" @@ -36,12 +38,94 @@ ecma_object_t* ecma_new_standard_error (ecma_standard_error_t error_type) /**< native error type */ { - /* SyntaxError should be treated as an early error */ - JERRY_ASSERT (error_type != ECMA_ERROR_SYNTAX); + ecma_builtin_id_t prototype_id = ECMA_BUILTIN_ID__COUNT; - JERRY_UNIMPLEMENTED_REF_UNUSED_VARS("Built-in error objects are not implemented.", error_type); + switch (error_type) + { + case ECMA_ERROR_COMMON: + { + prototype_id = ECMA_BUILTIN_ID_ERROR_PROTOTYPE; + break; + } + + case ECMA_ERROR_EVAL: + { + prototype_id = ECMA_BUILTIN_ID_EVAL_ERROR_PROTOTYPE; + break; + } + + case ECMA_ERROR_RANGE: + { + prototype_id = ECMA_BUILTIN_ID_RANGE_ERROR_PROTOTYPE; + break; + } + + case ECMA_ERROR_REFERENCE: + { + prototype_id = ECMA_BUILTIN_ID_REFERENCE_ERROR_PROTOTYPE; + break; + } + + case ECMA_ERROR_TYPE: + { + prototype_id = ECMA_BUILTIN_ID_TYPE_ERROR_PROTOTYPE; + break; + } + + case ECMA_ERROR_URI: + { + prototype_id = ECMA_BUILTIN_ID_URI_ERROR_PROTOTYPE; + break; + } + + case ECMA_ERROR_SYNTAX: + { + /* SyntaxError should be treated as an early error */ + JERRY_UNREACHABLE (); + } + } + + ecma_object_t *prototype_obj_p = ecma_builtin_get (prototype_id); + + ecma_object_t *new_error_obj_p = ecma_create_object (prototype_obj_p, + true, + ECMA_OBJECT_TYPE_GENERAL); + + ecma_deref_object (prototype_obj_p); + + ecma_property_t *class_prop_p = ecma_create_internal_property (new_error_obj_p, + ECMA_INTERNAL_PROPERTY_CLASS); + class_prop_p->u.internal_property.value = ECMA_MAGIC_STRING_ERROR_UL; + + return new_error_obj_p; } /* ecma_new_standard_error */ +/** + * Standard ecma-error object constructor. + * + * @return pointer to ecma-object representing specified error + * with reference counter set to one. + */ +ecma_object_t* +ecma_new_standard_error_with_message (ecma_standard_error_t error_type, /**< native error type */ + ecma_string_t* message_string_p) /**< message string */ +{ + ecma_object_t *new_error_obj_p = ecma_new_standard_error (error_type); + + ecma_string_t *message_magic_string_p = ecma_get_magic_string (ECMA_MAGIC_STRING_MESSAGE); + ecma_property_t *prop_p = ecma_create_named_data_property (new_error_obj_p, + message_magic_string_p, + ECMA_PROPERTY_WRITABLE, + ECMA_PROPERTY_NOT_ENUMERABLE, + ECMA_PROPERTY_CONFIGURABLE); + + ecma_string_t *msg_string_p = ecma_copy_or_ref_ecma_string (message_string_p); + prop_p->u.named_data_property.value = ecma_make_string_value (msg_string_p); + ecma_deref_ecma_string (message_magic_string_p); + + return new_error_obj_p; +} /* ecma_new_standard_error_with_message */ + /** * @} * @} diff --git a/src/libecmaoperations/ecma-exceptions.h b/src/libecmaoperations/ecma-exceptions.h index ad234b4b5..70de15389 100644 --- a/src/libecmaoperations/ecma-exceptions.h +++ b/src/libecmaoperations/ecma-exceptions.h @@ -31,10 +31,11 @@ /** * Native errors. * - * See also: 15.11.6 + * See also: 15.11.1, 15.11.6 */ typedef enum { + ECMA_ERROR_COMMON, /**< Error */ ECMA_ERROR_EVAL, /**< EvalError */ ECMA_ERROR_RANGE, /**< RangeError */ ECMA_ERROR_REFERENCE, /**< ReferenceError */ @@ -44,6 +45,8 @@ typedef enum } ecma_standard_error_t; extern ecma_object_t *ecma_new_standard_error (ecma_standard_error_t error_type); +extern ecma_object_t* ecma_new_standard_error_with_message (ecma_standard_error_t error_type, + ecma_string_t *message_string_p); /** * @} diff --git a/tests/jerry/error.js b/tests/jerry/error.js index bf03d4ee8..c7ac72474 100644 --- a/tests/jerry/error.js +++ b/tests/jerry/error.js @@ -14,19 +14,61 @@ var e; +/* Error */ e = new Error (); assert (e.name === "Error"); assert (e.message === ""); -assert (e.toString() === "Error: "); +assert (e.toString() === "Error"); e = new Error("some message"); assert (e.name === "Error"); assert (e.message === "some message"); assert (e.toString() === "Error: some message"); +e.name = ""; +assert (e.toString() === "some message"); +e.message = ""; +assert (e.toString() === ""); assert (Error.prototype.toString !== Object.prototype.toString); assert (Error.prototype.constructor === Error); assert (Error.prototype.name === "Error"); assert (Error.prototype.message === ""); -assert (Error.prototype.toString() === "Error: "); +assert (Error.prototype.toString() === "Error"); + +/* TypeError */ +e = new TypeError (); +assert (e.name === "TypeError"); +assert (e.message === ""); +assert (e.toString() === "TypeError"); + +e = new TypeError("some message"); +assert (e.name === "TypeError"); +assert (e.message === "some message"); +assert (e.toString() === "TypeError: some message"); + +e.name = ""; +assert (e.toString() === "some message"); +e.message = ""; +assert (e.toString() === ""); + +assert (TypeError.prototype.toString === Error.prototype.toString); +assert (TypeError.prototype.constructor === TypeError); +assert (TypeError.prototype.name === "TypeError"); +assert (TypeError.prototype.message === ""); +assert (TypeError.prototype.toString() === "TypeError"); + +try +{ + null[1] = 'abcd'; + + assert (false); +} +catch (e) +{ + assert(e instanceof TypeError); + assert(e instanceof Error); + assert(e instanceof Object); + + assert(!(e instanceof Function)); +}