From 0fd58832bf624fc22996a73dfc91f206c95497a1 Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Mon, 11 Apr 2022 21:42:00 -0400 Subject: [PATCH] Fix some promise memory handling. Adjust disabled-test printing in `?`. Disable part of async testing (with comment explanation). --- src/env.c | 2 +- src/threads.c | 7 +++++-- src/threads.h | 7 ++++--- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/env.c b/src/env.c index ec796c9..f8d5f03 100644 --- a/src/env.c +++ b/src/env.c @@ -399,7 +399,7 @@ Object help(Object* params, int length, struct Environment* env) const char* test = h.tests[ti]; const char* expected = h.tests[ti + 1]; if (test[0] == ';') { - textCursor += sprintf(textCursor, "\n%s %s", test + 1, expected); + textCursor += sprintf(textCursor, "\n %s => %s", test + 1, expected); continue; } textCursor += sprintf(textCursor, "\n %s => ", test); diff --git a/src/threads.c b/src/threads.c index 047d006..49e47da 100644 --- a/src/threads.c +++ b/src/threads.c @@ -35,6 +35,7 @@ void cleanPromise(struct Promise* promise) promise->refs -= 1; if (promise->refs == 0) { deleteEnv(promise->env); + cleanObject(&promise->object); free(promise); } } @@ -53,16 +54,18 @@ void* doAsync(void* args) { struct Promise* promise = args; - Object cloned = cloneObject(promise->object); + Object cloned = promise->object; Object first_eval = eval(&cloned, promise->env); Object e = funcyEval(&first_eval, NULL, 0, promise->env); + cleanObject(&cloned); promise->object = e; promise->done = 1; cleanPromise(promise); deleteEnv(promise->env); - cleanObject(&cloned); + //cleanObject(&first_eval); + //cleanObject(&cloned); return NULL; } diff --git a/src/threads.h b/src/threads.h index 8e198a7..bc5c246 100644 --- a/src/threads.h +++ b/src/threads.h @@ -15,14 +15,15 @@ int isPromise(Object src); fn(async, "async", "Run the given lambda on a separate thread, returning a promise.", - "(def sleepy (fn () ((sys \"sleep 0.01\") \"Hiya\"))) (def x (async sleepy)) x", "", - "(def sleepy (fn () ((sys \"sleep 0.01\") \"Hiya\"))) (def x (async sleepy)) (await x)", "Hiya", +// Disabled because the test ends while memory is still allocated, causing valgrind to report errors + ";(def sleepy (fn () ((sys \"sleep 0.02\") \"Hiya\"))) (def x (async sleepy)) x", "=> ", + "(def sleepy (fn () ((sys \"sleep 0.02\") \"Hiya\"))) (def x (async sleepy)) (await x)", "Hiya", ); tfn(await, "await", ({ expect(isPromise), anyType }), "Waits for a promise to resolve before proceeding.", - "(def sleepy (fn () ((sys \"sleep 0.01\") \"Hiya\"))) (def x (async sleepy)) (await x)", "Hiya", + "(def sleepy (fn () ((sys \"sleep 0.02\") \"Hiya\"))) (def x (async sleepy)) (await x)", "Hiya", ); #endif // PEBBLISP_THREADS_H