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 <stdarg.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 "ecma-objects.h" 24#include "ecma-symbol-object.h" 25#include "jcontext.h" 26#include "jrt.h" 27 28#if ENABLED (JERRY_LINE_INFO) 29#include "vm.h" 30#endif /* ENABLED (JERRY_LINE_INFO) */ 31 32/** \addtogroup ecma ECMA 33 * @{ 34 * 35 * \addtogroup exceptions Exceptions 36 * @{ 37 */ 38 39/** 40 * Map error type to error prototype. 41 */ 42typedef struct 43{ 44 ecma_standard_error_t error_type; /**< Native error type */ 45 ecma_builtin_id_t error_prototype_id; /**< ID of the error prototype */ 46} ecma_error_mapping_t; 47 48/** 49 * List of error type mappings 50 */ 51const ecma_error_mapping_t ecma_error_mappings[] = 52{ 53#define ERROR_ELEMENT(TYPE, ID) { TYPE, ID } 54 ERROR_ELEMENT (ECMA_ERROR_COMMON, ECMA_BUILTIN_ID_ERROR_PROTOTYPE), 55 56#if ENABLED (JERRY_BUILTIN_ERRORS) 57 ERROR_ELEMENT (ECMA_ERROR_EVAL, ECMA_BUILTIN_ID_EVAL_ERROR_PROTOTYPE), 58 ERROR_ELEMENT (ECMA_ERROR_RANGE, ECMA_BUILTIN_ID_RANGE_ERROR_PROTOTYPE), 59 ERROR_ELEMENT (ECMA_ERROR_REFERENCE, ECMA_BUILTIN_ID_REFERENCE_ERROR_PROTOTYPE), 60 ERROR_ELEMENT (ECMA_ERROR_TYPE, ECMA_BUILTIN_ID_TYPE_ERROR_PROTOTYPE), 61 ERROR_ELEMENT (ECMA_ERROR_URI, ECMA_BUILTIN_ID_URI_ERROR_PROTOTYPE), 62 ERROR_ELEMENT (ECMA_ERROR_SYNTAX, ECMA_BUILTIN_ID_SYNTAX_ERROR_PROTOTYPE), 63#endif /* ENABLED (JERRY_BUILTIN_ERRORS) */ 64 65#undef ERROR_ELEMENT 66}; 67 68/** 69 * Standard ecma-error object constructor. 70 * 71 * Note: 72 * calling with ECMA_ERROR_NONE does not make sense thus it will 73 * cause a fault in the system. 74 * 75 * @return pointer to ecma-object representing specified error 76 * with reference counter set to one. 77 */ 78ecma_object_t * 79ecma_new_standard_error (ecma_standard_error_t error_type) /**< native error type */ 80{ 81#if ENABLED (JERRY_BUILTIN_ERRORS) 82 ecma_builtin_id_t prototype_id = ECMA_BUILTIN_ID__COUNT; 83 84 switch (error_type) 85 { 86 case ECMA_ERROR_EVAL: 87 { 88 prototype_id = ECMA_BUILTIN_ID_EVAL_ERROR_PROTOTYPE; 89 break; 90 } 91 92 case ECMA_ERROR_RANGE: 93 { 94 prototype_id = ECMA_BUILTIN_ID_RANGE_ERROR_PROTOTYPE; 95 break; 96 } 97 98 case ECMA_ERROR_REFERENCE: 99 { 100 prototype_id = ECMA_BUILTIN_ID_REFERENCE_ERROR_PROTOTYPE; 101 break; 102 } 103 104 case ECMA_ERROR_TYPE: 105 { 106 prototype_id = ECMA_BUILTIN_ID_TYPE_ERROR_PROTOTYPE; 107 break; 108 } 109 110 case ECMA_ERROR_URI: 111 { 112 prototype_id = ECMA_BUILTIN_ID_URI_ERROR_PROTOTYPE; 113 break; 114 } 115 116 case ECMA_ERROR_SYNTAX: 117 { 118 prototype_id = ECMA_BUILTIN_ID_SYNTAX_ERROR_PROTOTYPE; 119 break; 120 } 121 122 default: 123 { 124 JERRY_ASSERT (error_type == ECMA_ERROR_COMMON); 125 126 prototype_id = ECMA_BUILTIN_ID_ERROR_PROTOTYPE; 127 break; 128 } 129 } 130#else 131 JERRY_UNUSED (error_type); 132 ecma_builtin_id_t prototype_id = ECMA_BUILTIN_ID_ERROR_PROTOTYPE; 133#endif /* ENABLED (JERRY_BUILTIN_ERRORS) */ 134 135 ecma_object_t *prototype_obj_p = ecma_builtin_get (prototype_id); 136 137 ecma_object_t *new_error_obj_p = ecma_create_object (prototype_obj_p, 138 sizeof (ecma_extended_object_t), 139 ECMA_OBJECT_TYPE_CLASS); 140 141 ((ecma_extended_object_t *) new_error_obj_p)->u.class_prop.class_id = LIT_MAGIC_STRING_ERROR_UL; 142 143#if ENABLED (JERRY_LINE_INFO) 144 /* The "stack" identifier is not a magic string. */ 145 const char * const stack_id_p = "stack"; 146 147 ecma_string_t *stack_str_p = ecma_new_ecma_string_from_utf8 ((const lit_utf8_byte_t *) stack_id_p, 5); 148 149 ecma_property_value_t *prop_value_p = ecma_create_named_data_property (new_error_obj_p, 150 stack_str_p, 151 ECMA_PROPERTY_CONFIGURABLE_WRITABLE, 152 NULL); 153 ecma_deref_ecma_string (stack_str_p); 154 155 ecma_value_t backtrace_value = vm_get_backtrace (0); 156 157 prop_value_p->value = backtrace_value; 158 ecma_deref_object (ecma_get_object_from_value (backtrace_value)); 159#endif /* ENABLED (JERRY_LINE_INFO) */ 160 161 return new_error_obj_p; 162} /* ecma_new_standard_error */ 163 164/** 165 * Return the error type for an Error object. 166 * 167 * @return one of the ecma_standard_error_t value 168 * if it is not an Error object then ECMA_ERROR_NONE will be returned 169 */ 170ecma_standard_error_t 171ecma_get_error_type (ecma_object_t *error_object) /**< possible error object */ 172{ 173 if (error_object->u2.prototype_cp == JMEM_CP_NULL) 174 { 175 return ECMA_ERROR_NONE; 176 } 177 178 ecma_object_t *prototype_p = ECMA_GET_NON_NULL_POINTER (ecma_object_t, error_object->u2.prototype_cp); 179 180 uint8_t builtin_id = ecma_get_object_builtin_id (prototype_p); 181 182 for (uint8_t idx = 0; idx < sizeof (ecma_error_mappings) / sizeof (ecma_error_mappings[0]); idx++) 183 { 184 if (ecma_error_mappings[idx].error_prototype_id == builtin_id) 185 { 186 return ecma_error_mappings[idx].error_type; 187 } 188 } 189 190 return ECMA_ERROR_NONE; 191} /* ecma_get_error_type */ 192 193/** 194 * Standard ecma-error object constructor. 195 * 196 * @return pointer to ecma-object representing specified error 197 * with reference counter set to one. 198 */ 199ecma_object_t * 200ecma_new_standard_error_with_message (ecma_standard_error_t error_type, /**< native error type */ 201 ecma_string_t *message_string_p) /**< message string */ 202{ 203 ecma_object_t *new_error_obj_p = ecma_new_standard_error (error_type); 204 205 ecma_property_value_t *prop_value_p; 206 prop_value_p = ecma_create_named_data_property (new_error_obj_p, 207 ecma_get_magic_string (LIT_MAGIC_STRING_MESSAGE), 208 ECMA_PROPERTY_CONFIGURABLE_WRITABLE, 209 NULL); 210 211 ecma_ref_ecma_string (message_string_p); 212 prop_value_p->value = ecma_make_string_value (message_string_p); 213 214 return new_error_obj_p; 215} /* ecma_new_standard_error_with_message */ 216 217/** 218 * Raise a standard ecma-error with the given type and message. 219 * 220 * @return ecma value 221 * Returned value must be freed with ecma_free_value 222 */ 223static ecma_value_t 224ecma_raise_standard_error (ecma_standard_error_t error_type, /**< error type */ 225 const lit_utf8_byte_t *msg_p) /**< error message */ 226{ 227 ecma_object_t *error_obj_p; 228 229 if (msg_p != NULL) 230 { 231 ecma_string_t *error_msg_p = ecma_new_ecma_string_from_utf8 (msg_p, 232 lit_zt_utf8_string_size (msg_p)); 233 error_obj_p = ecma_new_standard_error_with_message (error_type, error_msg_p); 234 ecma_deref_ecma_string (error_msg_p); 235 } 236 else 237 { 238 error_obj_p = ecma_new_standard_error (error_type); 239 } 240 241 jcontext_raise_exception (ecma_make_object_value (error_obj_p)); 242 return ECMA_VALUE_ERROR; 243} /* ecma_raise_standard_error */ 244 245#if ENABLED (JERRY_ERROR_MESSAGES) 246 247/** 248 * Raise a standard ecma-error with the given format string and arguments. 249 * 250 * @return ecma value 251 * Returned value must be freed with ecma_free_value 252 */ 253ecma_value_t 254ecma_raise_standard_error_with_format (ecma_standard_error_t error_type, /**< error type */ 255 const char *format, /**< format string */ 256 ...) /**< ecma-values */ 257{ 258 JERRY_ASSERT (format != NULL); 259 260 ecma_stringbuilder_t builder = ecma_stringbuilder_create (); 261 262 const char *start_p = format; 263 const char *end_p = format; 264 265 va_list args; 266 267 va_start (args, format); 268 269 while (*end_p) 270 { 271 if (*end_p == '%') 272 { 273 /* Concat template string. */ 274 if (end_p > start_p) 275 { 276 ecma_stringbuilder_append_raw (&builder, (lit_utf8_byte_t *) start_p, (lit_utf8_size_t) (end_p - start_p)); 277 } 278 279 /* Convert an argument to string without side effects. */ 280 ecma_string_t *arg_string_p; 281 const ecma_value_t arg_val = va_arg (args, ecma_value_t); 282 283 if (JERRY_UNLIKELY (ecma_is_value_object (arg_val))) 284 { 285 ecma_object_t *arg_object_p = ecma_get_object_from_value (arg_val); 286 lit_magic_string_id_t class_name = ecma_object_get_class_name (arg_object_p); 287 arg_string_p = ecma_get_magic_string (class_name); 288 } 289#if ENABLED (JERRY_ES2015) 290 else if (ecma_is_value_symbol (arg_val)) 291 { 292 ecma_value_t symbol_desc_value = ecma_get_symbol_descriptive_string (arg_val); 293 arg_string_p = ecma_get_string_from_value (symbol_desc_value); 294 } 295#endif /* ENABLED (JERRY_ES2015) */ 296 else 297 { 298 arg_string_p = ecma_op_to_string (arg_val); 299 JERRY_ASSERT (arg_string_p != NULL); 300 } 301 302 /* Concat argument. */ 303 ecma_stringbuilder_append (&builder, arg_string_p); 304 305 ecma_deref_ecma_string (arg_string_p); 306 307 start_p = end_p + 1; 308 } 309 310 end_p++; 311 } 312 313 va_end (args); 314 315 /* Concat reset of template string. */ 316 if (start_p < end_p) 317 { 318 ecma_stringbuilder_append_raw (&builder, (lit_utf8_byte_t *) start_p, (lit_utf8_size_t) (end_p - start_p)); 319 } 320 321 ecma_string_t *builder_str_p = ecma_stringbuilder_finalize (&builder); 322 323 ecma_object_t *error_obj_p = ecma_new_standard_error_with_message (error_type, builder_str_p); 324 325 ecma_deref_ecma_string (builder_str_p); 326 327 jcontext_raise_exception (ecma_make_object_value (error_obj_p)); 328 return ECMA_VALUE_ERROR; 329} /* ecma_raise_standard_error_with_format */ 330 331#endif /* ENABLED (JERRY_ERROR_MESSAGES) */ 332 333/** 334 * Raise a common error with the given message. 335 * 336 * @return ecma value 337 * Returned value must be freed with ecma_free_value 338 */ 339ecma_value_t 340ecma_raise_common_error (const char *msg_p) /**< error message */ 341{ 342 return ecma_raise_standard_error (ECMA_ERROR_COMMON, (const lit_utf8_byte_t *) msg_p); 343} /* ecma_raise_common_error */ 344 345/** 346 * Raise a RangeError with the given message. 347 * 348 * See also: ECMA-262 v5, 15.11.6.2 349 * 350 * @return ecma value 351 * Returned value must be freed with ecma_free_value 352 */ 353ecma_value_t 354ecma_raise_range_error (const char *msg_p) /**< error message */ 355{ 356 return ecma_raise_standard_error (ECMA_ERROR_RANGE, (const lit_utf8_byte_t *) msg_p); 357} /* ecma_raise_range_error */ 358 359/** 360 * Raise a ReferenceError with the given message. 361 * 362 * See also: ECMA-262 v5, 15.11.6.3 363 * 364 * @return ecma value 365 * Returned value must be freed with ecma_free_value 366 */ 367ecma_value_t 368ecma_raise_reference_error (const char *msg_p) /**< error message */ 369{ 370 return ecma_raise_standard_error (ECMA_ERROR_REFERENCE, (const lit_utf8_byte_t *) msg_p); 371} /* ecma_raise_reference_error */ 372 373/** 374 * Raise a SyntaxError with the given message. 375 * 376 * See also: ECMA-262 v5, 15.11.6.4 377 * 378 * @return ecma value 379 * Returned value must be freed with ecma_free_value 380 */ 381ecma_value_t 382ecma_raise_syntax_error (const char *msg_p) /**< error message */ 383{ 384 return ecma_raise_standard_error (ECMA_ERROR_SYNTAX, (const lit_utf8_byte_t *) msg_p); 385} /* ecma_raise_syntax_error */ 386 387/** 388 * Raise a TypeError with the given message. 389 * 390* See also: ECMA-262 v5, 15.11.6.5 391 * 392 * @return ecma value 393 * Returned value must be freed with ecma_free_value 394 */ 395ecma_value_t 396ecma_raise_type_error (const char *msg_p) /**< error message */ 397{ 398 return ecma_raise_standard_error (ECMA_ERROR_TYPE, (const lit_utf8_byte_t *) msg_p); 399} /* ecma_raise_type_error */ 400 401/** 402 * Raise a URIError with the given message. 403 * 404* See also: ECMA-262 v5, 15.11.6.6 405 * 406 * @return ecma value 407 * Returned value must be freed with ecma_free_value 408 */ 409ecma_value_t 410ecma_raise_uri_error (const char *msg_p) /**< error message */ 411{ 412 return ecma_raise_standard_error (ECMA_ERROR_URI, (const lit_utf8_byte_t *) msg_p); 413} /* ecma_raise_uri_error */ 414 415/** 416 * @} 417 * @} 418 */ 419