bug fix in map select/insert routines
This commit is contained in:
@@ -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);
|
||||||
@@ -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');
|
|
||||||
+18
-1
@@ -268,7 +268,8 @@ _Bool Debug_Callback(Runtime *runtime, void *userp)
|
|||||||
"quit .............. Stop execution\n"
|
"quit .............. Stop execution\n"
|
||||||
"continue .......... Run until a breakpoint or the end of the code is reached\n"
|
"continue .......... Run until a breakpoint or the end of the code is reached\n"
|
||||||
"breakpoint ........ Add a breakpoint\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"))
|
else if(!strcmp(argv[1], "help"))
|
||||||
{
|
{
|
||||||
@@ -316,6 +317,17 @@ _Bool Debug_Callback(Runtime *runtime, void *userp)
|
|||||||
" | reached.\n"
|
" | reached.\n"
|
||||||
"\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"))
|
else if(!strcmp(argv[1], "breakpoint"))
|
||||||
{
|
{
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
@@ -444,6 +456,11 @@ _Bool Debug_Callback(Runtime *runtime, void *userp)
|
|||||||
dbg->continuing = 1;
|
dbg->continuing = 1;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
else if(!strcmp(argv[0], "disassembly"))
|
||||||
|
{
|
||||||
|
Executable_Dump(Runtime_GetCurrentExecutable(runtime));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
else if(!strcmp(argv[0], "step"))
|
else if(!strcmp(argv[0], "step"))
|
||||||
{
|
{
|
||||||
return 1;
|
return 1;
|
||||||
|
|||||||
+23
-1
@@ -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_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_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_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_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_ntohl(Runtime *runtime, Object **argv, unsigned int argc, Error *error);
|
||||||
static Object *bin_htons(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"))
|
if(!strcmp(s, "inet_aton"))
|
||||||
return Object_FromNativeFunction(bm->runtime, bin_inet_aton, 2, heap, err);
|
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;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -513,6 +518,23 @@ static Object *bin_inet_aton(Runtime *runtime, Object **argv, unsigned int argc,
|
|||||||
return Object_FromInt(r, heap, error);
|
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)
|
static Object *bin_htonl(Runtime *runtime, Object **argv, unsigned int argc, Error *error)
|
||||||
{
|
{
|
||||||
assert(argc == 1);
|
assert(argc == 1);
|
||||||
@@ -632,7 +654,7 @@ static Object *bin_accept(Runtime *runtime, Object **argv, unsigned int argc, Er
|
|||||||
|
|
||||||
Heap *heap = Runtime_GetHeap(runtime);
|
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;
|
if(error->occurred) return NULL;
|
||||||
|
|
||||||
struct sockaddr_in addr;
|
struct sockaddr_in addr;
|
||||||
|
|||||||
+6
-6
@@ -78,9 +78,9 @@ static Object *select(Object *self, Object *key, Heap *heap, Error *error)
|
|||||||
|
|
||||||
MapObject *map = (MapObject*) self;
|
MapObject *map = (MapObject*) self;
|
||||||
|
|
||||||
int mask = map->mapper_size - 1;
|
unsigned int mask = map->mapper_size - 1;
|
||||||
int hash = Object_Hash(key, error);
|
unsigned int hash = Object_Hash(key, error);
|
||||||
int pert = hash;
|
unsigned int pert = hash;
|
||||||
|
|
||||||
if(error->occurred)
|
if(error->occurred)
|
||||||
// No hash function.
|
// 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))
|
if(!grow(map, heap, error))
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
int mask = map->mapper_size - 1;
|
unsigned int mask = map->mapper_size - 1;
|
||||||
int hash = Object_Hash(key, error);
|
unsigned int hash = Object_Hash(key, error);
|
||||||
int pert = hash;
|
unsigned int pert = hash;
|
||||||
|
|
||||||
if(error->occurred)
|
if(error->occurred)
|
||||||
// No hash function.
|
// No hash function.
|
||||||
|
|||||||
Reference in New Issue
Block a user