From eb15233a76c938da8889dd8ba35a0aef6e43a8bc Mon Sep 17 00:00:00 2001 From: cozis Date: Sun, 5 Dec 2021 17:26:04 +0100 Subject: [PATCH] bug fix in map select/insert routines --- samples/server2.noja | 91 ++++++++++++++++++++++++++++++++++++++++++++ samples/socket.noja | 28 -------------- src/debug.c | 19 ++++++++- src/o_net_builtins.c | 24 +++++++++++- src/objects/o_map.c | 12 +++--- 5 files changed, 138 insertions(+), 36 deletions(-) create mode 100644 samples/server2.noja delete mode 100644 samples/socket.noja diff --git a/samples/server2.noja b/samples/server2.noja new file mode 100644 index 0000000..9456c46 --- /dev/null +++ b/samples/server2.noja @@ -0,0 +1,91 @@ + +fun startServer(callbacks, port, backlog) +{ + # Handle default arguments. + { + if callbacks == none: + callbacks = {}; + + if port == none: + port = 8000; + + if backlog == none: + backlog = 5; + } + + fd = net.socket(net.AF_INET, net.SOCK_STREAM, 0); + + if fd < 0: + { + print('Failed to create socket.\n'); + return none; + } + + if net.bind(fd, {sin_family: net.AF_INET, sin_port: net.htons(port), sin_addr: {s_addr: net.htonl(net.INADDR_ANY)}}) != 0: + { + print('Failed to bind.\n'); + return none; + } + + if net.listen(fd, backlog) != 0: + { + print('Failed to listen.\n'); + return none; + } + + on_datain = callbacks.on_datain; + on_connect = callbacks.on_connect; + on_disconnect = callbacks.on_disconnect; + + if on_datain == none: fun on_datain() none; + if on_connect == none: fun on_connect() none; + if on_disconnect == none: fun on_disconnect() none; + + buffer = newBuffer(16); + + while true: + { + client_addr = {}; + client_fd = net.accept(fd, client_addr); + + if client_fd < 0: + { + print('Warning! Failed to accept!\n'); + continue; + } + + addr_str = net.inet_ntoa(client_addr.sin_addr); + + on_connect(addr_str); + + do + { + n = net.recv(client_fd, buffer, 0); + + if n < 0: + print('Warning! Failed to read from socket!\n'); + else if n == 0: + on_disconnect(addr_str); + else + on_datain(addr_str, buffer, n); + } + while n > 0; + } +} + +fun on_connect(addr) +{ + print(addr, ' connected.\n'); +} + +fun on_datain(addr, buf, len) +{ + print('Received ', len, ' bytes from ', addr); +} + +fun on_disconnect(addr) +{ + print(addr, ' disconnected.\n'); +} + +startServer({on_connect: on_connect, on_datain: on_datain, on_disconnect: on_disconnect}, 8080); \ No newline at end of file diff --git a/samples/socket.noja b/samples/socket.noja deleted file mode 100644 index 36d9295..0000000 --- a/samples/socket.noja +++ /dev/null @@ -1,28 +0,0 @@ - -addr = "216.58.208.142"; -port = 80; - -fd = net.socket(net.AF_INET, net.SOCK_STREAM, 0); - -if fd < 0: - { - print('Failed to create socket\n'); - return none; - } - -in_addr = {}; -if net.inet_aton(addr, in_addr) == 0: - { - print('Bad address string format\n'); - return none; - } - -r = net.connect(fd, {sin_family: net.AF_INET, sin_port: net.htons(port), sin_addr: in_addr}); - -if r < 0: - { - print('Failed to connect\n'); - return none; - } - -print('Connected!\n'); \ No newline at end of file diff --git a/src/debug.c b/src/debug.c index 8239246..25ea157 100644 --- a/src/debug.c +++ b/src/debug.c @@ -268,7 +268,8 @@ _Bool Debug_Callback(Runtime *runtime, void *userp) "quit .............. Stop execution\n" "continue .......... Run until a breakpoint or the end of the code is reached\n" "breakpoint ........ Add a breakpoint\n" - "stack ............. Show the contents of the stack\n"); + "stack ............. Show the contents of the stack\n" + "disassembly ....... Show the current file's bytecode\n"); } else if(!strcmp(argv[1], "help")) { @@ -316,6 +317,17 @@ _Bool Debug_Callback(Runtime *runtime, void *userp) " | reached.\n" "\n"); } + else if(!strcmp(argv[1], "disassembly")) + { + fprintf(stderr, + "\n" + " Command | disassembly\n" + " | \n" + " Usage | > disassembly\n" + " | \n" + " Description | Show the current file's bytecode.\n" + "\n"); + } else if(!strcmp(argv[1], "breakpoint")) { fprintf(stderr, @@ -444,6 +456,11 @@ _Bool Debug_Callback(Runtime *runtime, void *userp) dbg->continuing = 1; return 1; } + else if(!strcmp(argv[0], "disassembly")) + { + Executable_Dump(Runtime_GetCurrentExecutable(runtime)); + return 1; + } else if(!strcmp(argv[0], "step")) { return 1; diff --git a/src/o_net_builtins.c b/src/o_net_builtins.c index cb4cb66..ef4cbda 100644 --- a/src/o_net_builtins.c +++ b/src/o_net_builtins.c @@ -18,6 +18,7 @@ static Object *bin_socket(Runtime *runtime, Object **argv, unsigned int argc, Er static Object *bin_accept(Runtime *runtime, Object **argv, unsigned int argc, Error *error); static Object *bin_connect(Runtime *runtime, Object **argv, unsigned int argc, Error *error); static Object *bin_inet_aton(Runtime *runtime, Object **argv, unsigned int argc, Error *error); +static Object *bin_inet_ntoa(Runtime *runtime, Object **argv, unsigned int argc, Error *error); static Object *bin_htonl(Runtime *runtime, Object **argv, unsigned int argc, Error *error); static Object *bin_ntohl(Runtime *runtime, Object **argv, unsigned int argc, Error *error); static Object *bin_htons(Runtime *runtime, Object **argv, unsigned int argc, Error *error); @@ -135,6 +136,10 @@ static Object *select_(Object *self, Object *key, Heap *heap, Error *err) { if(!strcmp(s, "inet_aton")) return Object_FromNativeFunction(bm->runtime, bin_inet_aton, 2, heap, err); + + if(!strcmp(s, "inet_ntoa")) + return Object_FromNativeFunction(bm->runtime, bin_inet_ntoa, 1, heap, err); + return NULL; } @@ -513,6 +518,23 @@ static Object *bin_inet_aton(Runtime *runtime, Object **argv, unsigned int argc, return Object_FromInt(r, heap, error); } +static Object *bin_inet_ntoa(Runtime *runtime, Object **argv, unsigned int argc, Error *error) +{ + assert(argc == 1); + + Heap *heap = Runtime_GetHeap(runtime); + + struct in_addr in_addr; + + if(!get_in_addr_from_map(argv[0], &in_addr, heap, error)) + return NULL; + + const char *addr = inet_ntoa(in_addr); + assert(addr != NULL); + + return Object_FromString(addr, -1, heap, error); +} + static Object *bin_htonl(Runtime *runtime, Object **argv, unsigned int argc, Error *error) { assert(argc == 1); @@ -632,7 +654,7 @@ static Object *bin_accept(Runtime *runtime, Object **argv, unsigned int argc, Er Heap *heap = Runtime_GetHeap(runtime); - int sockfd = Object_ToInt(argv[0], error); + long long int sockfd = Object_ToInt(argv[0], error); if(error->occurred) return NULL; struct sockaddr_in addr; diff --git a/src/objects/o_map.c b/src/objects/o_map.c index 03a6d9c..86755c4 100644 --- a/src/objects/o_map.c +++ b/src/objects/o_map.c @@ -78,9 +78,9 @@ static Object *select(Object *self, Object *key, Heap *heap, Error *error) MapObject *map = (MapObject*) self; - int mask = map->mapper_size - 1; - int hash = Object_Hash(key, error); - int pert = hash; + unsigned int mask = map->mapper_size - 1; + unsigned int hash = Object_Hash(key, error); + unsigned int pert = hash; if(error->occurred) // No hash function. @@ -200,9 +200,9 @@ static _Bool insert(Object *self, Object *key, Object *val, Heap *heap, Error *e if(!grow(map, heap, error)) return 0; - int mask = map->mapper_size - 1; - int hash = Object_Hash(key, error); - int pert = hash; + unsigned int mask = map->mapper_size - 1; + unsigned int hash = Object_Hash(key, error); + unsigned int pert = hash; if(error->occurred) // No hash function.