Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
koush committed Jul 23, 2021
2 parents d85b25c + 1ecbdda commit ef0c897
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ repl-bn.c
run-test262
run-test262-bn
test_fib.c
*.exe
3 changes: 3 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
"request": "launch",
"name": "Launch QuickJS",
"runtimeExecutable": "${workspaceFolder}/qjs-debug",
"windows": {
"runtimeExecutable": "${workspaceFolder}/qjs-debug.exe"
},
// "trace": true,
"program": "${workspaceFolder}/test.js"
},
Expand Down
24 changes: 24 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"files.associations": {
"__bit_reference": "c",
"__functional_base": "c",
"__node_handle": "c",
"algorithm": "c",
"atomic": "c",
"bitset": "c",
"chrono": "c",
"cstddef": "c",
"__memory": "c",
"functional": "c",
"iterator": "c",
"limits": "c",
"locale": "c",
"memory": "c",
"optional": "c",
"ratio": "c",
"system_error": "c",
"tuple": "c",
"type_traits": "c",
"vector": "c"
}
}
3 changes: 3 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
"label": "make qjs-debug",
"type": "shell",
"command": "make -j32 qjs-debug",
"windows": {
"command": "make -j32 qjs-debug.exe",
},
"group": {
"kind": "build",
"isDefault": true
Expand Down
8 changes: 8 additions & 0 deletions config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef __QUICKJS_CONFIG_H
#define __QUICKJS_CONFIG_H

#ifdef _WIN32
#define _WIN32_WINNT 0x0600
#endif

#endif // __QUICKJS_CONFIG_H
130 changes: 130 additions & 0 deletions quickjs-debugger-transport-win.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#include "quickjs-debugger.h"

#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#include <winsock2.h>

struct js_transport_data {
int handle;
} js_transport_data;

static size_t js_transport_read(void *udata, char *buffer, size_t length) {
struct js_transport_data* data = (struct js_transport_data *)udata;
if (data->handle <= 0)
return -1;

if (length == 0)
return -2;

if (buffer == NULL)
return -3;

//ssize_t ret = read(data->handle, (void *)buffer, length);
ssize_t ret = recv( data->handle, (void*)buffer, length, 0);

if (ret == SOCKET_ERROR )
return -4;

if (ret == 0)
return -5;

if (ret > length)
return -6;

return ret;
}

static size_t js_transport_write(void *udata, const char *buffer, size_t length) {
struct js_transport_data* data = (struct js_transport_data *)udata;
if (data->handle <= 0)
return -1;

if (length == 0)
return -2;

if (buffer == NULL) {
return -3;
}

//size_t ret = write(data->handle, (const void *) buffer, length);
size_t ret = send( data->handle, (const void *) buffer, length, 0);
if (ret <= 0 || ret > (ssize_t) length)
return -4;

return ret;
}

static size_t js_transport_peek(void *udata) {
WSAPOLLFD fds[1];
int poll_rc;

struct js_transport_data* data = (struct js_transport_data *)udata;
if (data->handle <= 0)
return -1;

fds[0].fd = data->handle;
fds[0].events = POLLIN;
fds[0].revents = 0;

poll_rc = WSAPoll(fds, 1, 0);
if (poll_rc < 0)
return -2;
if (poll_rc > 1)
return -3;
// no data
if (poll_rc == 0)
return 0;
// has data
return 1;
}

static void js_transport_close(JSContext* ctx, void *udata) {
struct js_transport_data* data = (struct js_transport_data *)udata;
if (data->handle <= 0)
return;

close(data->handle);
data->handle = 0;

free(udata);

WSACleanup();
}

void js_debugger_connect(JSContext *ctx, char *address) {

WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);

char* port_string = strstr(address, ":");
assert(port_string);

int port = atoi(port_string + 1);
assert(port);

int client = socket(AF_INET, SOCK_STREAM, 0);
assert(client > 0);
char host_string[256];
strcpy(host_string, address);
host_string[port_string - address] = 0;

struct hostent *host = gethostbyname(host_string);
assert(host);
struct sockaddr_in addr;

memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
memcpy((char *)&addr.sin_addr.s_addr, (char *)host->h_addr, host->h_length);
addr.sin_port = htons(port);

//__asm__ volatile("int $0x03");
assert(!connect(client, (const struct sockaddr *)&addr, sizeof(addr)));

struct js_transport_data *data = (struct js_transport_data *)malloc(sizeof(struct js_transport_data));
data->handle = client;
js_debugger_attach(ctx, js_transport_read, js_transport_write, js_transport_peek, js_transport_close, data);
}
4 changes: 0 additions & 4 deletions quickjs-debugger.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
#include "quickjs-debugger.h"
#include <time.h>
#include <math.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
Expand Down
1 change: 1 addition & 0 deletions quickjs-debugger.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef QUICKJS_DEBUGGER_H
#define QUICKJS_DEBUGGER_H

#include "config.h"
#include "quickjs.h"
#include <time.h>

Expand Down

0 comments on commit ef0c897

Please sign in to comment.