|
20 | 20 | #include "ecma-gc.h" |
21 | 21 | #include "ecma-globals.h" |
22 | 22 | #include "ecma-helpers.h" |
| 23 | +#include "lit-char-helpers.h" |
23 | 24 | #include "ecma-objects.h" |
24 | 25 | #include "ecma-string-object.h" |
25 | 26 | #include "ecma-try-catch-macro.h" |
|
43 | 44 | * @{ |
44 | 45 | */ |
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 * |
| 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 | + |
46 | 75 | /** |
47 | 76 | * The Error.prototype object's 'toString' routine |
48 | 77 | * |
|
55 | 84 | static ecma_value_t |
56 | 85 | ecma_builtin_error_prototype_object_to_string (ecma_value_t this_arg) /**< this argument */ |
57 | 86 | { |
58 | | - ecma_value_t ret_value = ECMA_VALUE_EMPTY; |
59 | | - |
60 | 87 | /* 2. */ |
61 | 88 | if (!ecma_is_value_object (this_arg)) |
62 | 89 | { |
63 | 90 | return ecma_raise_type_error (ECMA_ERR_MSG ("Argument 'this' is not an object.")); |
64 | 91 | } |
65 | | - else |
| 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)) |
66 | 100 | { |
67 | | - ecma_object_t *obj_p = ecma_get_object_from_value (this_arg); |
68 | | - |
69 | | - ecma_value_t name_get_ret_value = ecma_op_object_get_by_magic_id (obj_p, LIT_MAGIC_STRING_NAME); |
70 | | - if (ECMA_IS_VALUE_ERROR (name_get_ret_value)) |
71 | | - { |
72 | | - return name_get_ret_value; |
73 | | - } |
74 | | - |
75 | | - ecma_string_t *name_string_p; |
76 | | - |
77 | | - if (ecma_is_value_undefined (name_get_ret_value)) |
78 | | - { |
79 | | - name_string_p = ecma_get_magic_string (LIT_MAGIC_STRING_ERROR_UL); |
80 | | - } |
81 | | - else |
82 | | - { |
83 | | - name_string_p = ecma_op_to_string (name_get_ret_value); |
84 | | - } |
85 | | - |
86 | | - ecma_free_value (name_get_ret_value); |
87 | | - |
88 | | - if (JERRY_UNLIKELY (name_string_p == NULL)) |
89 | | - { |
90 | | - return ECMA_VALUE_ERROR; |
91 | | - } |
92 | | - else |
93 | | - { |
94 | | - ecma_value_t msg_get_ret_value = ecma_op_object_get_by_magic_id (obj_p, LIT_MAGIC_STRING_MESSAGE); |
95 | | - if (ECMA_IS_VALUE_ERROR (msg_get_ret_value)) |
96 | | - { |
97 | | - ecma_deref_ecma_string (name_string_p); |
98 | | - return msg_get_ret_value; |
99 | | - } |
100 | | - |
101 | | - ecma_string_t *msg_string_p; |
102 | | - |
103 | | - if (ecma_is_value_undefined (msg_get_ret_value)) |
104 | | - { |
105 | | - msg_string_p = ecma_get_magic_string (LIT_MAGIC_STRING__EMPTY); |
106 | | - } |
107 | | - else |
108 | | - { |
109 | | - msg_string_p = ecma_op_to_string (msg_get_ret_value); |
110 | | - } |
111 | | - |
112 | | - ecma_free_value (msg_get_ret_value); |
113 | | - |
114 | | - if (JERRY_UNLIKELY (msg_string_p == NULL)) |
115 | | - { |
116 | | - ecma_deref_ecma_string (name_string_p); |
117 | | - return ECMA_VALUE_ERROR; |
118 | | - } |
119 | | - else |
120 | | - { |
121 | | - ecma_string_t *ret_str_p; |
122 | | - |
123 | | - if (ecma_string_is_empty (name_string_p)) |
124 | | - { |
125 | | - ret_str_p = msg_string_p; |
126 | | - ecma_ref_ecma_string (ret_str_p); |
127 | | - } |
128 | | - else if (ecma_string_is_empty (msg_string_p)) |
129 | | - { |
130 | | - ret_str_p = name_string_p; |
131 | | - ecma_ref_ecma_string (ret_str_p); |
132 | | - } |
133 | | - else |
134 | | - { |
135 | | - const lit_utf8_size_t name_size = ecma_string_get_size (name_string_p); |
136 | | - const lit_utf8_size_t msg_size = ecma_string_get_size (msg_string_p); |
137 | | - const lit_utf8_size_t colon_size = lit_get_magic_string_size (LIT_MAGIC_STRING_COLON_CHAR); |
138 | | - const lit_utf8_size_t space_size = lit_get_magic_string_size (LIT_MAGIC_STRING_SPACE_CHAR); |
139 | | - const lit_utf8_size_t size = name_size + msg_size + colon_size + space_size; |
140 | | - |
141 | | - JMEM_DEFINE_LOCAL_ARRAY (ret_str_buffer, size, lit_utf8_byte_t); |
142 | | - lit_utf8_byte_t *ret_str_buffer_p = ret_str_buffer; |
143 | | - |
144 | | - lit_utf8_size_t bytes = ecma_string_copy_to_cesu8_buffer (name_string_p, ret_str_buffer_p, name_size); |
145 | | - JERRY_ASSERT (bytes == name_size); |
146 | | - ret_str_buffer_p = ret_str_buffer_p + bytes; |
147 | | - JERRY_ASSERT (ret_str_buffer_p <= ret_str_buffer + size); |
148 | | - |
149 | | - ret_str_buffer_p = lit_copy_magic_string_to_buffer (LIT_MAGIC_STRING_COLON_CHAR, |
150 | | - ret_str_buffer_p, |
151 | | - colon_size); |
152 | | - JERRY_ASSERT (ret_str_buffer_p <= ret_str_buffer + size); |
153 | | - |
154 | | - ret_str_buffer_p = lit_copy_magic_string_to_buffer (LIT_MAGIC_STRING_SPACE_CHAR, |
155 | | - ret_str_buffer_p, |
156 | | - space_size); |
157 | | - JERRY_ASSERT (ret_str_buffer_p <= ret_str_buffer + size); |
158 | | - |
159 | | - bytes = ecma_string_copy_to_cesu8_buffer (msg_string_p, ret_str_buffer_p, msg_size); |
160 | | - JERRY_ASSERT (bytes == msg_size); |
161 | | - ret_str_buffer_p = ret_str_buffer_p + bytes; |
162 | | - JERRY_ASSERT (ret_str_buffer_p == ret_str_buffer + size); |
163 | | - |
164 | | - ret_str_p = ecma_new_ecma_string_from_utf8 (ret_str_buffer, |
165 | | - size); |
166 | | - |
167 | | - JMEM_FINALIZE_LOCAL_ARRAY (ret_str_buffer); |
168 | | - } |
169 | | - |
170 | | - ret_value = ecma_make_string_value (ret_str_p); |
171 | | - } |
172 | | - |
173 | | - ecma_deref_ecma_string (msg_string_p); |
174 | | - } |
| 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); |
175 | 107 |
|
| 108 | + if (JERRY_UNLIKELY (msg_string_p == NULL)) |
| 109 | + { |
176 | 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); |
177 | 122 | } |
178 | 123 |
|
179 | | - return ret_value; |
| 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)); |
180 | 133 | } /* ecma_builtin_error_prototype_object_to_string */ |
181 | 134 |
|
182 | 135 | /** |
|
0 commit comments