|
16 | 16 | #ifdef JERRY_DEBUGGER
|
17 | 17 |
|
18 | 18 | #include "byte-code.h"
|
| 19 | +#include "ecma-builtin-helpers.h" |
19 | 20 | #include "ecma-conversion.h"
|
20 | 21 | #include "ecma-eval.h"
|
21 | 22 | #include "ecma-objects.h"
|
22 | 23 | #include "jcontext.h"
|
23 | 24 | #include "jerry-debugger.h"
|
24 | 25 | #include "jerryscript-port.h"
|
| 26 | +#include "lit-char-helpers.h" |
25 | 27 |
|
26 | 28 | /**
|
27 | 29 | * Type cast the debugger send buffer into a specific type.
|
@@ -715,4 +717,151 @@ jerry_debugger_send_memstats (void)
|
715 | 717 | jerry_debugger_send (sizeof (jerry_debugger_send_memstats_t));
|
716 | 718 | } /* jerry_debugger_send_memstats */
|
717 | 719 |
|
| 720 | +/* |
| 721 | + * Converts an standard error into a string. |
| 722 | + * |
| 723 | + * @return standard error string |
| 724 | + */ |
| 725 | +static ecma_string_t * |
| 726 | +jerry_debugger_exception_object_to_string (ecma_value_t exception_obj_value) /**< exception object */ |
| 727 | +{ |
| 728 | + ecma_object_t *object_p = ecma_get_object_from_value (exception_obj_value); |
| 729 | + |
| 730 | + ecma_object_t *prototype_p = ecma_get_object_prototype (object_p); |
| 731 | + |
| 732 | + if (prototype_p == NULL |
| 733 | + || ecma_get_object_type (prototype_p) != ECMA_OBJECT_TYPE_GENERAL |
| 734 | + || !ecma_get_object_is_builtin (prototype_p)) |
| 735 | + { |
| 736 | + return NULL; |
| 737 | + } |
| 738 | + |
| 739 | + lit_magic_string_id_t string_id; |
| 740 | + |
| 741 | + switch (((ecma_extended_object_t *) prototype_p)->u.built_in.id) |
| 742 | + { |
| 743 | +#ifndef CONFIG_DISABLE_ERROR_BUILTINS |
| 744 | + case ECMA_BUILTIN_ID_EVAL_ERROR_PROTOTYPE: |
| 745 | + { |
| 746 | + string_id = LIT_MAGIC_STRING_EVAL_ERROR_UL; |
| 747 | + break; |
| 748 | + } |
| 749 | + case ECMA_BUILTIN_ID_RANGE_ERROR_PROTOTYPE: |
| 750 | + { |
| 751 | + string_id = LIT_MAGIC_STRING_RANGE_ERROR_UL; |
| 752 | + break; |
| 753 | + } |
| 754 | + case ECMA_BUILTIN_ID_REFERENCE_ERROR_PROTOTYPE: |
| 755 | + { |
| 756 | + string_id = LIT_MAGIC_STRING_REFERENCE_ERROR_UL; |
| 757 | + break; |
| 758 | + } |
| 759 | + case ECMA_BUILTIN_ID_SYNTAX_ERROR_PROTOTYPE: |
| 760 | + { |
| 761 | + string_id = LIT_MAGIC_STRING_SYNTAX_ERROR_UL; |
| 762 | + break; |
| 763 | + } |
| 764 | + case ECMA_BUILTIN_ID_TYPE_ERROR_PROTOTYPE: |
| 765 | + { |
| 766 | + string_id = LIT_MAGIC_STRING_TYPE_ERROR_UL; |
| 767 | + break; |
| 768 | + } |
| 769 | + case ECMA_BUILTIN_ID_URI_ERROR_PROTOTYPE: |
| 770 | + { |
| 771 | + string_id = LIT_MAGIC_STRING_URI_ERROR_UL; |
| 772 | + break; |
| 773 | + } |
| 774 | +#endif /* !CONFIG_DISABLE_ERROR_BUILTINS */ |
| 775 | + case ECMA_BUILTIN_ID_ERROR_PROTOTYPE: |
| 776 | + { |
| 777 | + string_id = LIT_MAGIC_STRING_ERROR_UL; |
| 778 | + break; |
| 779 | + } |
| 780 | + default: |
| 781 | + { |
| 782 | + return NULL; |
| 783 | + } |
| 784 | + } |
| 785 | + |
| 786 | + lit_utf8_size_t size = lit_get_magic_string_size (string_id); |
| 787 | + JERRY_ASSERT (size <= 14); |
| 788 | + |
| 789 | + lit_utf8_byte_t data[16]; |
| 790 | + memcpy (data, lit_get_magic_string_utf8 (string_id), size); |
| 791 | + |
| 792 | + ecma_string_t message_string; |
| 793 | + ecma_init_ecma_magic_string (&message_string, LIT_MAGIC_STRING_MESSAGE); |
| 794 | + |
| 795 | + ecma_property_t *property_p; |
| 796 | + property_p = ecma_find_named_property (ecma_get_object_from_value (exception_obj_value), |
| 797 | + &message_string); |
| 798 | + |
| 799 | + if (property_p == NULL |
| 800 | + || ECMA_PROPERTY_GET_TYPE (*property_p) != ECMA_PROPERTY_TYPE_NAMEDDATA) |
| 801 | + { |
| 802 | + return ecma_new_ecma_string_from_utf8 (data, size); |
| 803 | + } |
| 804 | + |
| 805 | + ecma_property_value_t *prop_value_p = ECMA_PROPERTY_VALUE_PTR (property_p); |
| 806 | + |
| 807 | + if (!ecma_is_value_string (prop_value_p->value)) |
| 808 | + { |
| 809 | + return ecma_new_ecma_string_from_utf8 (data, size); |
| 810 | + } |
| 811 | + |
| 812 | + data[size] = LIT_CHAR_COLON; |
| 813 | + data[size + 1] = LIT_CHAR_SP; |
| 814 | + |
| 815 | + ecma_string_t *type_string_p = ecma_new_ecma_string_from_utf8 (data, size + 2); |
| 816 | + |
| 817 | + ecma_string_t *string_p = ecma_concat_ecma_strings (type_string_p, |
| 818 | + ecma_get_string_from_value (prop_value_p->value)); |
| 819 | + ecma_deref_ecma_string (type_string_p); |
| 820 | + return string_p; |
| 821 | +} /* jerry_debugger_exception_object_to_string */ |
| 822 | + |
| 823 | +/** |
| 824 | + * Send string representation of exception to the client. |
| 825 | + * |
| 826 | + * @return true - if the data sent successfully to the debugger client, |
| 827 | + * false - otherwise |
| 828 | + */ |
| 829 | +bool |
| 830 | +jerry_debugger_send_exception_string (ecma_value_t exception_value) /**< error value */ |
| 831 | +{ |
| 832 | + ecma_string_t *string_p = NULL; |
| 833 | + |
| 834 | + if (ecma_is_value_object (exception_value)) |
| 835 | + { |
| 836 | + ecma_value_t object_value = ecma_get_value_from_error_value (exception_value); |
| 837 | + |
| 838 | + string_p = jerry_debugger_exception_object_to_string (object_value); |
| 839 | + if (string_p == NULL) |
| 840 | + { |
| 841 | + string_p = ecma_get_string_from_value (ecma_builtin_helper_object_to_string (object_value)); |
| 842 | + } |
| 843 | + } |
| 844 | + else if (ecma_is_value_string (exception_value)) |
| 845 | + { |
| 846 | + string_p = ecma_get_string_from_value (exception_value); |
| 847 | + ecma_ref_ecma_string (string_p); |
| 848 | + } |
| 849 | + else |
| 850 | + { |
| 851 | + exception_value = ecma_op_to_string (exception_value); |
| 852 | + string_p = ecma_get_string_from_value (exception_value); |
| 853 | + } |
| 854 | + |
| 855 | + ECMA_STRING_TO_UTF8_STRING (string_p, string_data_p, string_size); |
| 856 | + |
| 857 | + bool result = jerry_debugger_send_string (JERRY_DEBUGGER_EXCEPTION_STR, |
| 858 | + string_data_p, |
| 859 | + string_size); |
| 860 | + |
| 861 | + ECMA_FINALIZE_UTF8_STRING (string_data_p, string_size); |
| 862 | + |
| 863 | + ecma_deref_ecma_string (string_p); |
| 864 | + return result; |
| 865 | +} /* jerry_debugger_send_exception_string */ |
| 866 | + |
718 | 867 | #endif /* JERRY_DEBUGGER */
|
0 commit comments