Skip to content

Commit

Permalink
Fix MinGW compilation errors in jerry-ext (#4510)
Browse files Browse the repository at this point in the history
MinGW also needs linking to ws2_32.
Defining socket types for unix/win32 for compatibility.
Also fixes #4512

JerryScript-DCO-1.0-Signed-off-by: Yonggang Luo [email protected]
  • Loading branch information
lygstate authored Jan 22, 2021
1 parent 9900cac commit 1db0905
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 25 deletions.
2 changes: 1 addition & 1 deletion jerry-ext/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ target_link_libraries(${JERRY_EXT_NAME} jerry-core)

set(JERRY_EXT_PKGCONFIG_LIBS)

if(USING_MSVC AND JERRY_DEBUGGER)
if("${PLATFORM}" STREQUAL "WINDOWS" AND JERRY_DEBUGGER)
target_link_libraries(${JERRY_EXT_NAME} ws2_32)
set(JERRY_EXT_PKGCONFIG_LIBS -lws2_32)
endif()
Expand Down
66 changes: 44 additions & 22 deletions jerry-ext/debugger/debugger-tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

#ifdef _WIN32
#include <BaseTsd.h>
typedef SSIZE_T ssize_t;
#include <WS2tcpip.h>
#include <winsock2.h>

Expand All @@ -33,11 +32,17 @@ typedef SSIZE_T ssize_t;
/* On Windows the invalid socket's value of INVALID_SOCKET */
#define JERRYX_SOCKET_INVALID INVALID_SOCKET

/* On Windows sockets have a SOCKET typedef */
typedef SOCKET jerryx_socket;

/*
* On Windows, socket functions have the following signatures:
* int send(SOCKET s, const char *buf, int len, int flags);
* int recv(SOCKET s, char *buf, int len, int flags);
* int setsockopt(SOCKET s, int level, int optname, const char *optval, int optlen);
*/
typedef int jerryx_socket_ssize_t;
typedef SOCKET jerryx_socket_t;
typedef char jerryx_socket_void_t;
typedef int jerryx_socket_size_t;
#else /* !_WIN32 */

#include <arpa/inet.h>
#include <fcntl.h>
#include <sys/socket.h>
Expand All @@ -49,8 +54,16 @@ typedef SOCKET jerryx_socket;
/* On *nix the invalid socket has a value of -1 */
#define JERRYX_SOCKET_INVALID (-1)

/* On *nix the sockets are integer identifiers */
typedef int jerryx_socket;
/*
* On *nix, socket functions have the following signatures:
* ssize_t send(int sockfd, const void *buf, size_t len, int flags);
* ssize_t recv(int sockfd, void *buf, size_t len, int flags);
* int setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen);
*/
typedef ssize_t jerryx_socket_ssize_t;
typedef int jerryx_socket_t;
typedef void jerryx_socket_void_t;
typedef size_t jerryx_socket_size_t;
#endif /* _WIN32 */

/**
Expand All @@ -59,7 +72,7 @@ typedef int jerryx_socket;
typedef struct
{
jerry_debugger_transport_header_t header; /**< transport header */
jerryx_socket tcp_socket; /**< tcp socket */
jerryx_socket_t tcp_socket; /**< tcp socket */
} jerryx_debugger_transport_tcp_t;

/**
Expand All @@ -85,7 +98,7 @@ jerryx_debugger_tcp_get_errno (void)
* Correctly close a single socket.
*/
static inline void
jerryx_debugger_tcp_close_socket (jerryx_socket socket_id) /**< socket to close */
jerryx_debugger_tcp_close_socket (jerryx_socket_t socket_id) /**< socket to close */
{
#ifdef _WIN32
closesocket (socket_id);
Expand All @@ -109,7 +122,7 @@ jerryx_debugger_tcp_log_error (int errno_value) /**< error value to log */
char *error_message = NULL;
FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
errno_value,
(DWORD) errno_value,
MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &error_message,
0,
Expand Down Expand Up @@ -152,6 +165,7 @@ jerryx_debugger_tcp_send (jerry_debugger_transport_header_t *header_p, /**< tcp
JERRYX_ASSERT (jerry_debugger_transport_is_connected ());

jerryx_debugger_transport_tcp_t *tcp_p = (jerryx_debugger_transport_tcp_t *) header_p;
jerryx_socket_size_t remaining_bytes = (jerryx_socket_size_t) message_length;

do
{
Expand All @@ -167,7 +181,10 @@ jerryx_debugger_tcp_send (jerry_debugger_transport_header_t *header_p, /**< tcp
}
#endif /* __linux__ */

ssize_t sent_bytes = send (tcp_p->tcp_socket, message_p, message_length, 0);
jerryx_socket_ssize_t sent_bytes = send (tcp_p->tcp_socket,
(jerryx_socket_void_t *) message_p,
remaining_bytes,
0);

if (sent_bytes < 0)
{
Expand All @@ -184,9 +201,9 @@ jerryx_debugger_tcp_send (jerry_debugger_transport_header_t *header_p, /**< tcp
}

message_p += sent_bytes;
message_length -= (size_t) sent_bytes;
remaining_bytes -= (jerryx_socket_size_t) sent_bytes;
}
while (message_length > 0);
while (remaining_bytes > 0);

return true;
} /* jerryx_debugger_tcp_send */
Expand All @@ -200,10 +217,12 @@ jerryx_debugger_tcp_receive (jerry_debugger_transport_header_t *header_p, /**< t
{
jerryx_debugger_transport_tcp_t *tcp_p = (jerryx_debugger_transport_tcp_t *) header_p;

uint8_t *buffer_p = receive_context_p->buffer_p + receive_context_p->received_length;
size_t buffer_size = JERRY_DEBUGGER_TRANSPORT_MAX_BUFFER_SIZE - receive_context_p->received_length;
jerryx_socket_void_t *buffer_p = (jerryx_socket_void_t *) (receive_context_p->buffer_p
+ receive_context_p->received_length);
jerryx_socket_size_t buffer_size = (jerryx_socket_size_t) (JERRY_DEBUGGER_TRANSPORT_MAX_BUFFER_SIZE
- receive_context_p->received_length);

ssize_t length = recv (tcp_p->tcp_socket, buffer_p, buffer_size, 0);
jerryx_socket_ssize_t length = recv (tcp_p->tcp_socket, buffer_p, buffer_size, 0);

if (length <= 0)
{
Expand Down Expand Up @@ -241,7 +260,7 @@ jerryx_debugger_tcp_receive (jerry_debugger_transport_header_t *header_p, /**< t
* false if there was an error
*/
static bool
jerryx_debugger_tcp_configure_socket (jerryx_socket server_socket, /** < socket to configure */
jerryx_debugger_tcp_configure_socket (jerryx_socket_t server_socket, /** < socket to configure */
uint16_t port) /** < port number to be used for the socket */
{
struct sockaddr_in addr;
Expand All @@ -250,9 +269,12 @@ jerryx_debugger_tcp_configure_socket (jerryx_socket server_socket, /** < socket
addr.sin_port = htons (port);
addr.sin_addr.s_addr = INADDR_ANY;

int opt_value = 1;
const int opt_value = 1;

if (setsockopt (server_socket, SOL_SOCKET, SO_REUSEADDR, &opt_value, sizeof (int)) != 0)
if (setsockopt (server_socket,
SOL_SOCKET, SO_REUSEADDR,
(const jerryx_socket_void_t *) &opt_value,
sizeof (int)) != 0)
{
return false;
}
Expand Down Expand Up @@ -289,7 +311,7 @@ jerryx_debugger_tcp_create (uint16_t port) /**< listening port */
}
#endif /* _WIN32*/

jerryx_socket server_socket = socket (AF_INET, SOCK_STREAM, 0);
jerryx_socket_t server_socket = socket (AF_INET, SOCK_STREAM, 0);
if (server_socket == JERRYX_SOCKET_INVALID)
{
jerryx_debugger_tcp_log_error (jerryx_debugger_tcp_get_errno ());
Expand All @@ -309,7 +331,7 @@ jerryx_debugger_tcp_create (uint16_t port) /**< listening port */
struct sockaddr_in addr;
socklen_t sin_size = sizeof (struct sockaddr_in);

jerryx_socket tcp_socket = accept (server_socket, (struct sockaddr *) &addr, &sin_size);
jerryx_socket_t tcp_socket = accept (server_socket, (struct sockaddr *) &addr, &sin_size);

jerryx_debugger_tcp_close_socket (server_socket);

Expand All @@ -322,7 +344,7 @@ jerryx_debugger_tcp_create (uint16_t port) /**< listening port */
/* Set non-blocking mode. */
#ifdef _WIN32
u_long nonblocking_enabled = 1;
if (ioctlsocket (tcp_socket, FIONBIO, &nonblocking_enabled) != NO_ERROR)
if (ioctlsocket (tcp_socket, (long) FIONBIO, &nonblocking_enabled) != NO_ERROR)
{
jerryx_debugger_tcp_close_socket (tcp_socket);
return false;
Expand Down
2 changes: 1 addition & 1 deletion jerry-ext/handle-scope/handle-scope-allocator.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ jerryx_handle_scope_get_child (jerryx_handle_scope_t *scope)
{
return (jerryx_handle_scope_t *) jerryx_handle_scope_pool.start;
}
long idx = JERRYX_HANDLE_SCOPE_PRELIST_IDX (scope);
ptrdiff_t idx = JERRYX_HANDLE_SCOPE_PRELIST_IDX (scope);
if (idx < 0)
{
return NULL;
Expand Down
2 changes: 1 addition & 1 deletion jerry-port/default/default-module.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ jerry_port_normalize_path (const char *in_path_p, /**< input file path */
char *dir_p = (char *) malloc (_MAX_DIR);

_splitpath_s (base_file_p, drive, _MAX_DRIVE, dir_p, _MAX_DIR, NULL, 0, NULL, 0);
const size_t drive_len = strnlen (&drive, _MAX_DRIVE);
const size_t drive_len = strnlen (drive, _MAX_DRIVE);
const size_t dir_len = strnlen (dir_p, _MAX_DIR);
base_drive_dir_len = drive_len + dir_len;
path_p = (char *) malloc (base_drive_dir_len + in_path_len + 1);
Expand Down

0 comments on commit 1db0905

Please sign in to comment.