From 1fd42153e3ef828ce4a85c411bc2f59e945921a1 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Wed, 26 Apr 2023 13:23:42 -0500 Subject: [PATCH] added: `alloc.copy_closure` --- compiler/src/onyx.c | 6 +++--- core/alloc/alloc.onyx | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/compiler/src/onyx.c b/compiler/src/onyx.c index deb0143d..d0132e54 100644 --- a/compiler/src/onyx.c +++ b/compiler/src/onyx.c @@ -970,13 +970,13 @@ static void onyx_watch(CompileOptions *compile_opts) { char time_buf[128] = {0}; time_t now = time(NULL); strftime(time_buf, 128, "%X", localtime(&now)); - bh_printf("\e[1;1H\e[30;104m [BUILT] %s \e[0m", time_buf); + bh_printf("\e[1;1H\e[30;105m Onyx " VERSION " \e[30;104m Built %s \e[0m", time_buf); i32 errors = bh_arr_length(context.errors.errors); if (errors == 0) { - bh_printf("\e[30;102m [ERRORS] 0 \e[0m"); + bh_printf("\e[30;102m Errors 0 \e[0m"); } else { - bh_printf("\e[30;101m [ERRORS] %d \e[0m", errors); + bh_printf("\e[30;101m Error%s %d \e[0m", bh_num_plural(errors), errors); } watches = bh_file_watch_new(); diff --git a/core/alloc/alloc.onyx b/core/alloc/alloc.onyx index e6147409..6df2b6b4 100644 --- a/core/alloc/alloc.onyx +++ b/core/alloc/alloc.onyx @@ -8,6 +8,9 @@ package core.alloc #load "./logging" #load "./gc" +use core.memory +use core.intrinsics.types {type_is_function} + as_allocator :: #match { macro (a: Allocator) => a } @@ -65,6 +68,19 @@ on_temp :: macro (v: $V) -> &V { return out; } +#doc """ + Copies the internal closure data of a function to the provided allocator, + and returns the new closed function. +""" +copy_closure :: (f: $F/type_is_function, a: Allocator) -> F { + if !f.closure do return f; + + new_closure := raw_alloc(a, f.closure_size); + memory.copy(new_closure, f.closure, f.closure_size); + + return F.{ f.__funcidx, new_closure, f.closure_size }; +} + TEMPORARY_ALLOCATOR_SIZE :: 1 << 16; // 16Kb // The global heap allocator, set up upon program intialization. -- 2.25.1