• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright JS Foundation and other contributors, http://js.foundation
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "ecma-alloc.h"
17 #include "ecma-builtins.h"
18 #include "ecma-conversion.h"
19 #include "ecma-exceptions.h"
20 #include "ecma-gc.h"
21 #include "ecma-globals.h"
22 #include "ecma-helpers.h"
23 #include "lit-char-helpers.h"
24 #include "ecma-objects.h"
25 #include "ecma-string-object.h"
26 #include "ecma-try-catch-macro.h"
27 #include "jrt.h"
28 #include "lit-magic-strings.h"
29 
30 #define ECMA_BUILTINS_INTERNAL
31 #include "ecma-builtins-internal.h"
32 
33 #define BUILTIN_INC_HEADER_NAME "ecma-builtin-error-prototype.inc.h"
34 #define BUILTIN_UNDERSCORED_ID error_prototype
35 #include "ecma-builtin-internal-routines-template.inc.h"
36 
37 /** \addtogroup ecma ECMA
38  * @{
39  *
40  * \addtogroup ecmabuiltins
41  * @{
42  *
43  * \addtogroup errorprototype ECMA Error.prototype object built-in
44  * @{
45  */
46 
47 /**
48  * Helper method to get a property value from an error object
49  *
50  * @return ecma_string_t
51  */
52 static ecma_string_t *
ecma_builtin_error_prototype_object_to_string_helper(ecma_object_t * obj_p,lit_magic_string_id_t property_id,lit_magic_string_id_t default_value)53 ecma_builtin_error_prototype_object_to_string_helper (ecma_object_t *obj_p, /**< error object */
54                                                       lit_magic_string_id_t property_id, /**< property id */
55                                                       lit_magic_string_id_t default_value) /**< default prop value */
56 {
57   ecma_value_t prop_value = ecma_op_object_get_by_magic_id (obj_p, property_id);
58 
59   if (ECMA_IS_VALUE_ERROR (prop_value))
60   {
61     return NULL;
62   }
63 
64   if (ecma_is_value_undefined (prop_value))
65   {
66     return ecma_get_magic_string (default_value);
67   }
68 
69   ecma_string_t *ret_str_p = ecma_op_to_string (prop_value);
70   ecma_free_value (prop_value);
71 
72   return ret_str_p;
73 } /* ecma_builtin_error_prototype_object_to_string_helper */
74 
75 /**
76  * The Error.prototype object's 'toString' routine
77  *
78  * See also:
79  *          ECMA-262 v5, 15.11.4.4
80  *
81  * @return ecma value
82  *         Returned value must be freed with ecma_free_value.
83  */
84 static ecma_value_t
ecma_builtin_error_prototype_object_to_string(ecma_value_t this_arg)85 ecma_builtin_error_prototype_object_to_string (ecma_value_t this_arg) /**< this argument */
86 {
87   /* 2. */
88   if (!ecma_is_value_object (this_arg))
89   {
90     return ecma_raise_type_error (ECMA_ERR_MSG ("Argument 'this' is not an object."));
91   }
92 
93   ecma_object_t *obj_p = ecma_get_object_from_value (this_arg);
94 
95   ecma_string_t *name_string_p = ecma_builtin_error_prototype_object_to_string_helper (obj_p,
96                                                                                        LIT_MAGIC_STRING_NAME,
97                                                                                        LIT_MAGIC_STRING_ERROR_UL);
98 
99   if (JERRY_UNLIKELY (name_string_p == NULL))
100   {
101     return ECMA_VALUE_ERROR;
102   }
103 
104   ecma_string_t *msg_string_p = ecma_builtin_error_prototype_object_to_string_helper (obj_p,
105                                                                                       LIT_MAGIC_STRING_MESSAGE,
106                                                                                       LIT_MAGIC_STRING__EMPTY);
107 
108   if (JERRY_UNLIKELY (msg_string_p == NULL))
109   {
110     ecma_deref_ecma_string (name_string_p);
111     return ECMA_VALUE_ERROR;
112   }
113 
114   if (ecma_string_is_empty (name_string_p))
115   {
116     return ecma_make_string_value (msg_string_p);
117   }
118 
119   if (ecma_string_is_empty (msg_string_p))
120   {
121     return ecma_make_string_value (name_string_p);
122   }
123 
124   ecma_stringbuilder_t builder = ecma_stringbuilder_create_from (name_string_p);
125 
126   ecma_stringbuilder_append_raw (&builder, (const lit_utf8_byte_t *)": ", 2);
127   ecma_stringbuilder_append (&builder, msg_string_p);
128 
129   ecma_deref_ecma_string (name_string_p);
130   ecma_deref_ecma_string (msg_string_p);
131 
132   return ecma_make_string_value (ecma_stringbuilder_finalize (&builder));
133 } /* ecma_builtin_error_prototype_object_to_string */
134 
135 /**
136  * @}
137  * @}
138  * @}
139  */
140