Repository: jerryscript-project/jerryscript
File: jerry-debugger/jerry_client_main.py (Python)

Code before:
        source_code = ""
        source_code_name = ""
        function_name = ""
        stack = [{"line": 1,
                  "column": 1,
                  "name": "",
                  "lines": [],
                  "offsets": []}]
        new_function_list = {}
        result = ""

        while True:
            if data is None:
                return "Error: connection lost during source code receiving"

            buffer_type = data[0]
            buffer_size = len(data) - 1

            logging.debug("Parser buffer type: %d, message size: %d", buffer_type, buffer_size)

            if buffer_type == JERRY_DEBUGGER_PARSE_ERROR:
                logging.error("Syntax error found")
                return ""

            elif buffer_type in [JERRY_DEBUGGER_SOURCE_CODE, JERRY_DEBUGGER_SOURCE_CODE_END]:
                source_code += data[1:].decode('latin1')

            elif buffer_type in [JERRY_DEBUGGER_SOURCE_CODE_NAME, JERRY_DEBUGGER_SOURCE_CODE_NAME_END]:
                source_code_name += data[1:].decode('latin1')

            elif buffer_type in [JERRY_DEBUGGER_FUNCTION_NAME, JERRY_DEBUGGER_FUNCTION_NAME_END]:
                function_name += data[1:].decode('latin1')

            elif buffer_type == JERRY_DEBUGGER_PARSE_FUNCTION:
                logging.debug("Source name: %s, function name: %s", source_code_name, function_name)

                position = struct.unpack(self.byte_order + self.idx_format + self.idx_format,
                                         data[1: 1 + 4 + 4])

                stack.append({"source": source_code,
                              "source_name": source_code_name,
                              "line": position[0],
                              "column": position[1],
                              "name": function_name,
                              "lines": [],
                              "offsets": []})
                function_name = ""

            elif buffer_type in [JERRY_DEBUGGER_BREAKPOINT_LIST, JERRY_DEBUGGER_BREAKPOINT_OFFSET_LIST]:
                name = "lines"
                if buffer_type == JERRY_DEBUGGER_BREAKPOINT_OFFSET_LIST:

Review comment (bug):
Is the source code Latin-1 encoded? Isn't it UTF-8 by default? It seems that at other places, e.g., when decoding `JERRY_DEBUGGER_EVAL_RESULT_END` or `JERRY_DEBUGGER_OUTPUT_RESULT_END`, UTF-8 encoding is used.

Code after:
        stack = [{"line": 1,
                  "column": 1,
                  "name": "",
                  "lines": [],
                  "offsets": []}]
        new_function_list = {}
        result = ""

        while True:
            if data is None:
                return "Error: connection lost during source code receiving"

            buffer_type = data[0]
            buffer_size = len(data) - 1

            logging.debug("Parser buffer type: %d, message size: %d", buffer_type, buffer_size)

            if buffer_type == JERRY_DEBUGGER_PARSE_ERROR:
                logging.error("Syntax error found")
                return ""

            elif buffer_type in [JERRY_DEBUGGER_SOURCE_CODE, JERRY_DEBUGGER_SOURCE_CODE_END]:
                source_code += data[1:]

            elif buffer_type in [JERRY_DEBUGGER_SOURCE_CODE_NAME, JERRY_DEBUGGER_SOURCE_CODE_NAME_END]:
                source_code_name += data[1:]

            elif buffer_type in [JERRY_DEBUGGER_FUNCTION_NAME, JERRY_DEBUGGER_FUNCTION_NAME_END]:
                function_name += data[1:]

            elif buffer_type == JERRY_DEBUGGER_PARSE_FUNCTION:
                logging.debug("Source name: %s, function name: %s", source_code_name.decode('utf8'),
                              function_name.decode('utf8'))

                position = struct.unpack(self.byte_order + self.idx_format + self.idx_format,
                                         data[1: 1 + 4 + 4])

                stack.append({"source": source_code.decode('utf8'),
                              "source_name": source_code_name.decode('utf8'),
                              "line": position[0],
                              "column": position[1],
                              "name": function_name.decode('utf8'),
                              "lines": [],
                              "offsets": []})
                function_name = b""

            elif buffer_type in [JERRY_DEBUGGER_BREAKPOINT_LIST, JERRY_DEBUGGER_BREAKPOINT_OFFSET_LIST]:
                name = "lines"
                if buffer_type == JERRY_DEBUGGER_BREAKPOINT_OFFSET_LIST:
                    name = "offsets"