From c9be701b199ff7d7ebb310683c522ba4a33df4a3 Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Sat, 19 Mar 2022 21:28:17 -0400 Subject: [PATCH] Fix no-param lambda environment bug. Put allocation cap behind an ifdef --- src/env.c | 18 +++++++++--------- src/object.c | 15 +++++++++------ src/web.c | 54 ++++++++++++++++++++++++++-------------------------- 3 files changed, 45 insertions(+), 42 deletions(-) diff --git a/src/env.c b/src/env.c index 6c4c76f..ecb3b2b 100644 --- a/src/env.c +++ b/src/env.c @@ -21,15 +21,13 @@ Object fetchFromEnvironment(const char* name, struct Environment* env) } for (int i = 0; i < env->size; i++) { - if (env->strings[i] == NULL) { - printd("Try %d (NULL)\n", i); + if (env->strings[i] == NULL) { printd("Try %d (NULL)\n", i); break; } printd("Try %d (%s)\n", i, env->strings[i]); debugObj(&env->objects[i]); - if (strcmp(name, env->strings[i]) == 0) { - printd("Returning!\n"); + if (strcmp(name, env->strings[i]) == 0) { printd("Returning!\n"); return cloneObject(env->objects[i]); } } @@ -47,6 +45,10 @@ struct Environment envForLambda(const Object* params, const Object* arg_forms, { int paramCount = listLength(params); + if (outer) { + outer->refs += 1; + } + struct Environment env = { .outer = outer, .strings = NULL, @@ -58,7 +60,7 @@ struct Environment envForLambda(const Object* params, const Object* arg_forms, }; if (paramCount == 0) { - return env; + return outer ? *outer : env; } env.strings = calloc(sizeof(char*), paramCount + 1); @@ -74,9 +76,6 @@ struct Environment envForLambda(const Object* params, const Object* arg_forms, march = march->forward; } - if(outer) { - outer->refs += 1; - } return env; } @@ -225,12 +224,13 @@ struct symFunc { }; struct Environment* _global; + struct Environment* global() { return _global; } -void setGlobal(struct Environment *env) +void setGlobal(struct Environment* env) { _global = env; } diff --git a/src/object.c b/src/object.c index 3296ad5..c00bcd3 100644 --- a/src/object.c +++ b/src/object.c @@ -5,6 +5,7 @@ #include size_t bytes = 0; + int getBytes() { return bytes; @@ -135,6 +136,7 @@ inline int isEmpty(const Object* obj) } int allocations = 0; + int getAllocations() { return allocations; @@ -148,11 +150,13 @@ int getAllocations() */ void allocObject(Object** spot, const Object src) { +#ifdef ALLOCATION_CAP if (allocations >= 10000) { printf("MAX ALLOCATIONS EXCEEDED\n"); *spot = NULL; return; } +#endif allocations++; *spot = malloc(sizeof(struct Object)); **spot = src; @@ -341,6 +345,7 @@ void stringStruct(char* dest, const Object* obj) #else #define stringf(_dest, _len, _format, args...) snprintf(_dest, _len, _format, ## args) #endif + char* stringNObj(char* dest, const Object* obj, const size_t len) { if (!dest || !obj) { @@ -375,7 +380,7 @@ char* stringNObj(char* dest, const Object* obj, const size_t len) #else if (obj->error->context && obj->error->context[0] != '\0') { stringf(dest, len, "'%s': %s", errorText[code], - obj->error->context); + obj->error->context); } else { stringf(dest, len, "%s", errorText[code]); } @@ -630,12 +635,10 @@ void deleteList(Object* dest) void _copyList(Object* dest, const Object* src, int delete) { - if (!dest || !src) { - printd("NULL\n"); + if (!dest || !src) { printd("NULL\n"); return; } - if (!isListy(*dest) || !isListy(*src)) { - printd("NOT A LIST\n"); + if (!isListy(*dest) || !isListy(*src)) { printd("NOT A LIST\n"); return; } @@ -873,7 +876,7 @@ inline Object constructLambda(const Object* params, const Object* body, struct E copyList(&o.lambda->body, body); if (env) { - Object *dest = &o.lambda->body; + Object* dest = &o.lambda->body; FOR_POINTER_IN_LIST(dest) { if (POINTER->type == TYPE_SYMBOL) { Object fetched = fetchFromEnvironment(POINTER->string, env); diff --git a/src/web.c b/src/web.c index 00cc872..655e046 100644 --- a/src/web.c +++ b/src/web.c @@ -26,27 +26,28 @@ int requestDefinition = -1; int routeCount = 0; struct Route routes[10]; -int addRoute(struct Route route) { - routes[routeCount] = route; - routeCount += 1; - return 0; +int addRoute(struct Route route) +{ + routes[routeCount] = route; + routeCount += 1; + return 0; } int methodMatches(const char* method, struct Route* route) { - switch (route->type) { - case GET: - return method[0] == 'G' || method[0] == 'g'; - case POST: - return method[0] == 'P' || method[0] == 'p'; - default: - return 0; - } + switch (route->type) { + case GET: + return method[0] == 'G' || method[0] == 'g'; + case POST: + return method[0] == 'P' || method[0] == 'p'; + default: + return 0; + } } static HttpResult -print_out_key(void *queryParamsV, enum MHD_ValueKind kind, const char *key, - const char *value) +print_out_key(void* queryParamsV, enum MHD_ValueKind kind, const char* key, + const char* value) { (void) kind; /* Unused. Silent compiler warning. */ Object* queryParams = queryParamsV; @@ -59,12 +60,12 @@ print_out_key(void *queryParamsV, enum MHD_ValueKind kind, const char *key, } static HttpResult -answer_to_connection(void *cls, struct MHD_Connection *connection, - const char *url, const char *method, - const char *version, const char *upload_data, - size_t *upload_data_size, void **con_cls) +answer_to_connection(void* cls, struct MHD_Connection* connection, + const char* url, const char* method, + const char* version, const char* upload_data, + size_t* upload_data_size, void** con_cls) { - char *page = NULL; + char* page = NULL; printf("%s URL: '%s' :: ", method, url); printf("version: %s\n", version); printf("upload_data: %s\n", upload_data); @@ -89,8 +90,6 @@ answer_to_connection(void *cls, struct MHD_Connection *connection, page = "

404, Dumbass.

"; } - struct MHD_Response *response; - HttpResult ret; (void) cls; /* Unused. Silent compiler warning. */ (void) url; /* Unused. Silent compiler warning. */ (void) method; /* Unused. Silent compiler warning. */ @@ -99,10 +98,9 @@ answer_to_connection(void *cls, struct MHD_Connection *connection, (void) upload_data_size; /* Unused. Silent compiler warning. */ (void) con_cls; /* Unused. Silent compiler warning. */ - response = - MHD_create_response_from_buffer(strlen(page), (void *) page, - MHD_RESPMEM_PERSISTENT); - ret = MHD_queue_response(connection, MHD_HTTP_OK, response); + struct MHD_Response* response = MHD_create_response_from_buffer( + strlen(page), (void*) page, MHD_RESPMEM_PERSISTENT); + HttpResult ret = MHD_queue_response(connection, MHD_HTTP_OK, response); MHD_destroy_response(response); // if (needsFree) { @@ -113,6 +111,7 @@ answer_to_connection(void *cls, struct MHD_Connection *connection, } int initialized = 0; + void initialize() { if (!initialized) { @@ -126,12 +125,13 @@ void initialize() int start(int port) { initialize(); - struct MHD_Daemon *daemon = MHD_start_daemon( + struct MHD_Daemon* daemon = MHD_start_daemon( MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD, port, NULL, NULL, &answer_to_connection, NULL, MHD_OPTION_END); - if (NULL == daemon) + if (NULL == daemon) { return 1; + } // (void) getchar();