added: `alloc.copy_closure`
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 26 Apr 2023 18:23:42 +0000 (13:23 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 26 Apr 2023 18:23:42 +0000 (13:23 -0500)
compiler/src/onyx.c
core/alloc/alloc.onyx

index deb0143d441b5b8932e6d9a9b3d68f756df7777a..d0132e54c941a14d5bbb37cf3d5903d6ae3a9ad9 100644 (file)
@@ -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();
index e6147409acda56ed92fcbc8487e70ec9ad6adb31..6df2b6b4a913e66b8dcbbba40ab661508e996674 100644 (file)
@@ -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.