Dániel Bátyai 9860d66a56
Rework the public API (#4829)
Related to #4186.

Some notable changes:
  - The term 'Error' now strictly refers to native Error objects defined in
    the ECMA standard, which are ordinary objects. All other uses of
    'error' or 'error reference' where the term refers to a thrown value is
    now called 'exception'.

  - Simplified the naming scheme of many String API functions. These functions
    will now also take an 'encoding' argument to specify the desired
    encoding in which to operate.

  - Removed the substring-copy-to-buffer functions. These functions
    behaved awkwardly, as they use character index to specify the
    start/end positions, and were mostly used incorrectly with byte
    offsets instead. The functionality can still be replicated with
    other functions if necessary.

  - String-to-buffer functions will no longer fail if the buffer is not
    sufficiently large, the string will instead be cropped.

  - Fixed the usage of the '_sz' prefix in many API functions. The term
    'sz' means zero-terminated string in hungarian notation, this was
    used incorrectly in many cases.

  - Renamed most of the public API functions to have shorter, more on-point
    names, rather than the often too long descriptive names. Functions are now
    also grouped by the type of value they operate on, where this makes
    sense.

JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai dbatyai@inf.u-szeged.hu
2021-12-06 10:20:09 +01:00

179 lines
5.1 KiB
C

/* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "jerryscript-ext/debugger.h"
#include "jext-common.h"
#if defined(JERRY_DEBUGGER) && (JERRY_DEBUGGER == 1)
/* A simplified transmission layer. */
/**
* Size of the raw packet header.
*/
#define JERRYX_DEBUGGER_RAWPACKET_HEADER_SIZE 1
/**
* Maximum message size with 1 byte size field.
*/
#define JERRYX_DEBUGGER_RAWPACKET_ONE_BYTE_LEN_MAX 255
/**
* Header for incoming packets.
*/
typedef struct
{
uint8_t size; /**< size of the message */
} jerryx_rawpacket_receive_header_t;
/**
* Close a tcp connection.
*/
static void
jerryx_debugger_rp_close (jerry_debugger_transport_header_t *header_p) /**< header for the transport interface */
{
JERRYX_ASSERT (!jerry_debugger_transport_is_connected ());
jerry_heap_free ((void *) header_p, sizeof (jerry_debugger_transport_header_t));
} /* jerryx_debugger_rp_close */
/**
* Send data over a simple raw packet connection.
*
* @return true - if the data has been sent successfully
* false - otherwise
*/
static bool
jerryx_debugger_rp_send (jerry_debugger_transport_header_t *header_p, /**< header for the transport interface */
uint8_t *message_p, /**< message to be sent */
size_t message_length) /**< message length in bytes */
{
JERRYX_ASSERT (message_length <= JERRYX_DEBUGGER_RAWPACKET_ONE_BYTE_LEN_MAX);
message_p[-1] = (uint8_t) message_length;
return header_p->next_p->send (header_p->next_p, message_p - 1, message_length + 1);
} /* jerryx_debugger_rp_send */
/**
* Receive data from a rawpacket connection.
*
* @return true - if data has been received successfully
* false - otherwise
*/
static bool
jerryx_debugger_rp_receive (jerry_debugger_transport_header_t *header_p, /**< header for the transport interface */
jerry_debugger_transport_receive_context_t *receive_context_p) /**< receive context */
{
if (!header_p->next_p->receive (header_p->next_p, receive_context_p))
{
return false;
}
if (receive_context_p->message_p == NULL)
{
return true;
}
size_t message_total_length = receive_context_p->message_total_length;
if (message_total_length == 0)
{
/* Byte stream. */
if (receive_context_p->message_length < sizeof (jerryx_rawpacket_receive_header_t))
{
receive_context_p->message_p = NULL;
return true;
}
}
else
{
/* Datagram packet. */
JERRYX_ASSERT (receive_context_p->message_length >= sizeof (jerryx_rawpacket_receive_header_t));
}
uint8_t *message_p = receive_context_p->message_p;
size_t message_length = (size_t) (message_p[0]);
if (message_total_length == 0)
{
size_t new_total_length = message_length + sizeof (jerryx_rawpacket_receive_header_t);
/* Byte stream. */
if (receive_context_p->message_length < new_total_length)
{
receive_context_p->message_p = NULL;
return true;
}
receive_context_p->message_total_length = new_total_length;
}
else
{
/* Datagram packet. */
JERRYX_ASSERT (receive_context_p->message_length == (message_length + sizeof (jerryx_rawpacket_receive_header_t)));
}
receive_context_p->message_p = message_p + sizeof (jerryx_rawpacket_receive_header_t);
receive_context_p->message_length = message_length;
return true;
} /* jerryx_debugger_rp_receive */
/**
* Initialize a simple raw packet transmission layer.
*
* @return true - if the connection succeeded
* false - otherwise
*/
bool
jerryx_debugger_rp_create (void)
{
const jerry_size_t interface_size = sizeof (jerry_debugger_transport_header_t);
jerry_debugger_transport_header_t *header_p;
header_p = (jerry_debugger_transport_header_t *) jerry_heap_alloc (interface_size);
if (!header_p)
{
return false;
}
header_p->close = jerryx_debugger_rp_close;
header_p->send = jerryx_debugger_rp_send;
header_p->receive = jerryx_debugger_rp_receive;
jerry_debugger_transport_add (header_p,
JERRYX_DEBUGGER_RAWPACKET_HEADER_SIZE,
JERRYX_DEBUGGER_RAWPACKET_ONE_BYTE_LEN_MAX,
JERRYX_DEBUGGER_RAWPACKET_HEADER_SIZE,
JERRYX_DEBUGGER_RAWPACKET_ONE_BYTE_LEN_MAX);
return true;
} /* jerryx_debugger_rp_create */
#else /* !(defined (JERRY_DEBUGGER) && (JERRY_DEBUGGER == 1)) */
/**
* Dummy function when debugger is disabled.
*
* @return false
*/
bool
jerryx_debugger_rp_create (void)
{
return false;
} /* jerryx_debugger_rp_create */
#endif /* defined (JERRY_DEBUGGER) && (JERRY_DEBUGGER == 1) */