From 66cb7e316dd22727233e508513566b658e55f822 Mon Sep 17 00:00:00 2001 From: Ruben Ayrapetyan Date: Wed, 29 Oct 2014 21:28:56 +0300 Subject: [PATCH] Implementing ReferenceError and ReferenceError.prototype built-ins. --- src/libecmabuiltins/ecma-builtin-global.inc.h | 10 +- .../ecma-builtin-referenceerror-prototype.c | 33 +++++++ ...cma-builtin-referenceerror-prototype.inc.h | 65 +++++++++++++ .../ecma-builtin-referenceerror.c | 97 +++++++++++++++++++ .../ecma-builtin-referenceerror.inc.h | 65 +++++++++++++ src/libecmabuiltins/ecma-builtins-internal.h | 10 ++ tests/jerry/error.js | 38 ++++++++ 7 files changed, 313 insertions(+), 5 deletions(-) create mode 100644 src/libecmabuiltins/ecma-builtin-referenceerror-prototype.c create mode 100644 src/libecmabuiltins/ecma-builtin-referenceerror-prototype.inc.h create mode 100644 src/libecmabuiltins/ecma-builtin-referenceerror.c create mode 100644 src/libecmabuiltins/ecma-builtin-referenceerror.inc.h diff --git a/src/libecmabuiltins/ecma-builtin-global.inc.h b/src/libecmabuiltins/ecma-builtin-global.inc.h index 6f13e8f26..65e37b960 100644 --- a/src/libecmabuiltins/ecma-builtin-global.inc.h +++ b/src/libecmabuiltins/ecma-builtin-global.inc.h @@ -152,11 +152,11 @@ CP_UNIMPLEMENTED_VALUE (ECMA_MAGIC_STRING_RANGE_ERROR_UL, ECMA_PROPERTY_CONFIGURABLE) // ECMA-262 v5, 15.1.4.12 -CP_UNIMPLEMENTED_VALUE (ECMA_MAGIC_STRING_REFERENCE_ERROR_UL, - ecma_builtin_get (ECMA_BUILTIN_ID_REFERENCE_ERROR), - ECMA_PROPERTY_WRITABLE, - ECMA_PROPERTY_NOT_ENUMERABLE, - ECMA_PROPERTY_CONFIGURABLE) +OBJECT_VALUE (ECMA_MAGIC_STRING_REFERENCE_ERROR_UL, + ecma_builtin_get (ECMA_BUILTIN_ID_REFERENCE_ERROR), + ECMA_PROPERTY_WRITABLE, + ECMA_PROPERTY_NOT_ENUMERABLE, + ECMA_PROPERTY_CONFIGURABLE) // ECMA-262 v5, 15.1.4.13 CP_UNIMPLEMENTED_VALUE (ECMA_MAGIC_STRING_SYNTAX_ERROR_UL, diff --git a/src/libecmabuiltins/ecma-builtin-referenceerror-prototype.c b/src/libecmabuiltins/ecma-builtin-referenceerror-prototype.c new file mode 100644 index 000000000..0bfc03d10 --- /dev/null +++ b/src/libecmabuiltins/ecma-builtin-referenceerror-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-referenceerror-prototype.inc.h" +#define BUILTIN_UNDERSCORED_ID reference_error_prototype +#include "ecma-builtin-internal-routines-template.inc.h" diff --git a/src/libecmabuiltins/ecma-builtin-referenceerror-prototype.inc.h b/src/libecmabuiltins/ecma-builtin-referenceerror-prototype.inc.h new file mode 100644 index 000000000..e640a0284 --- /dev/null +++ b/src/libecmabuiltins/ecma-builtin-referenceerror-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. + */ + +/* + * ReferenceError.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_REFERENCE_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_REFERENCE_ERROR), + ECMA_PROPERTY_WRITABLE, + ECMA_PROPERTY_NOT_ENUMERABLE, + ECMA_PROPERTY_CONFIGURABLE) + +// 15.11.7.9 +STRING_VALUE (ECMA_MAGIC_STRING_NAME, + ECMA_MAGIC_STRING_REFERENCE_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-referenceerror.c b/src/libecmabuiltins/ecma-builtin-referenceerror.c new file mode 100644 index 000000000..4d4e84450 --- /dev/null +++ b/src/libecmabuiltins/ecma-builtin-referenceerror.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-referenceerror.inc.h" +#define BUILTIN_UNDERSCORED_ID reference_error +#include "ecma-builtin-internal-routines-template.inc.h" + +/** \addtogroup ecma ECMA + * @{ + * + * \addtogroup ecmabuiltins + * @{ + * + * \addtogroup referenceerror ECMA ReferenceError object built-in + * @{ + */ + +/** + * Handle calling [[Call]] of built-in ReferenceError object + * + * @return completion-value + */ +ecma_completion_value_t +ecma_builtin_reference_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_REFERENCE, + 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_REFERENCE); + + return ecma_make_normal_completion_value (ecma_make_object_value (new_error_object_p)); + } +} /* ecma_builtin_reference_error_dispatch_call */ + +/** + * Handle calling [[Construct]] of built-in ReferenceError object + * + * @return completion-value + */ +ecma_completion_value_t +ecma_builtin_reference_error_dispatch_construct (ecma_value_t *arguments_list_p, /**< arguments list */ + ecma_length_t arguments_list_len) /**< number of arguments */ +{ + return ecma_builtin_reference_error_dispatch_call (arguments_list_p, arguments_list_len); +} /* ecma_builtin_reference_error_dispatch_construct */ + +/** + * @} + * @} + * @} + */ diff --git a/src/libecmabuiltins/ecma-builtin-referenceerror.inc.h b/src/libecmabuiltins/ecma-builtin-referenceerror.inc.h new file mode 100644 index 000000000..c6f53e97e --- /dev/null +++ b/src/libecmabuiltins/ecma-builtin-referenceerror.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. + */ + +/* + * ReferenceError 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_REFERENCE_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_REFERENCE_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 be3380bda..c7b35e5e4 100644 --- a/src/libecmabuiltins/ecma-builtins-internal.h +++ b/src/libecmabuiltins/ecma-builtins-internal.h @@ -140,6 +140,16 @@ ecma_builtin_bin_search_for_magic_string_id_in_array (const ecma_magic_string_id ERROR_UL, \ ECMA_BUILTIN_ID_ERROR_PROTOTYPE, \ type_error_prototype) \ + macro (REFERENCE_ERROR, \ + TYPE_FUNCTION, \ + ERROR_UL, \ + ECMA_BUILTIN_ID_REFERENCE_ERROR_PROTOTYPE, \ + reference_error) \ + macro (REFERENCE_ERROR_PROTOTYPE, \ + TYPE_GENERAL, \ + ERROR_UL, \ + ECMA_BUILTIN_ID_ERROR_PROTOTYPE, \ + reference_error_prototype) \ macro (TYPE_ERROR, \ TYPE_FUNCTION, \ ERROR_UL, \ diff --git a/tests/jerry/error.js b/tests/jerry/error.js index c7ac72474..c6d4a2cef 100644 --- a/tests/jerry/error.js +++ b/tests/jerry/error.js @@ -72,3 +72,41 @@ catch (e) assert(!(e instanceof Function)); } + + +/* ReferenceError */ +e = new ReferenceError (); +assert (e.name === "ReferenceError"); +assert (e.message === ""); +assert (e.toString() === "ReferenceError"); + +e = new ReferenceError("some message"); +assert (e.name === "ReferenceError"); +assert (e.message === "some message"); +assert (e.toString() === "ReferenceError: some message"); + +e.name = ""; +assert (e.toString() === "some message"); +e.message = ""; +assert (e.toString() === ""); + +assert (ReferenceError.prototype.toString === Error.prototype.toString); +assert (ReferenceError.prototype.constructor === ReferenceError); +assert (ReferenceError.prototype.name === "ReferenceError"); +assert (ReferenceError.prototype.message === ""); +assert (ReferenceError.prototype.toString() === "ReferenceError"); + +try +{ + var a = non_existing_variable; + + assert (false); +} +catch (e) +{ + assert(e instanceof ReferenceError); + assert(e instanceof Error); + assert(e instanceof Object); + + assert(!(e instanceof Function)); +}