From 81385d5c0b35d20f33c78e3dcbcdd9c9beb928f6 Mon Sep 17 00:00:00 2001 From: Nanaloveyuki Date: Sun, 14 Jun 2026 09:00:00 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20consolidate=20async=20and=20native?= =?UTF-8?q?=20runtime=20updates?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- moon.mod.json | 2 +- src-async/async_logger_shared.mbt | 11 +++++-- src/utils/file_backend_native.mbt | 25 +++++++++------ src/utils/moon.pkg | 1 + src/utils/stub.c | 51 +++++++++++++++++++++++++++++++ 5 files changed, 77 insertions(+), 13 deletions(-) create mode 100644 src/utils/stub.c diff --git a/moon.mod.json b/moon.mod.json index ee24499..a6a60a6 100644 --- a/moon.mod.json +++ b/moon.mod.json @@ -1,6 +1,6 @@ { "name": "Nanaloveyuki/BitLogger", - "version": "0.5.2", + "version": "0.5.3", "deps": { "maria/json_parser": "0.1.1", "moonbitlang/async": "0.19.0" diff --git a/src-async/async_logger_shared.mbt b/src-async/async_logger_shared.mbt index e1ff32f..d76be25 100644 --- a/src-async/async_logger_shared.mbt +++ b/src-async/async_logger_shared.mbt @@ -254,11 +254,16 @@ pub async fn[S] AsyncLogger::log( } else { match self.overflow { AsyncOverflowPolicy::Blocking => { - self.queue.put(rec) catch { - err if err is AsyncLoggerClosed => () + let accepted = (async fn() -> Bool raise { + self.queue.put(rec) + true + })() catch { + err if err is AsyncLoggerClosed => false err => raise err } - self.pending_count.val += 1 + if accepted { + self.pending_count.val += 1 + } } AsyncOverflowPolicy::DropOldest | AsyncOverflowPolicy::DropNewest => { self.dropped_count.val += 1 diff --git a/src/utils/file_backend_native.mbt b/src/utils/file_backend_native.mbt index dde4854..7589a80 100644 --- a/src/utils/file_backend_native.mbt +++ b/src/utils/file_backend_native.mbt @@ -35,9 +35,9 @@ fn string_to_c_bytes(str : String) -> Bytes { type NativeFileHandle #borrow(path, mode) -extern "C" fn file_open_ffi(path : Bytes, mode : Bytes) -> NativeFileHandle = "fopen" +extern "C" fn file_open_ffi(path : Bytes, mode : Bytes) -> NativeFileHandle = "bitlogger_file_open" -extern "C" fn file_is_null_ffi(handle : NativeFileHandle) -> Bool = "moonbitlang_async_pointer_is_null" +extern "C" fn file_is_null_ffi(handle : NativeFileHandle) -> Bool = "bitlogger_pointer_is_null" #borrow(buffer) extern "C" fn file_write_ffi( @@ -45,21 +45,28 @@ extern "C" fn file_write_ffi( size : Int, count : Int, handle : NativeFileHandle, -) -> Int = "fwrite" +) -> Int = "bitlogger_file_write" -extern "C" fn file_flush_ffi(handle : NativeFileHandle) -> Int = "fflush" +extern "C" fn file_flush_ffi(handle : NativeFileHandle) -> Int = "bitlogger_file_flush" -extern "C" fn file_close_ffi(handle : NativeFileHandle) -> Int = "fclose" +extern "C" fn file_close_ffi(handle : NativeFileHandle) -> Int = "bitlogger_file_close" -extern "C" fn file_seek_ffi(handle : NativeFileHandle, offset : Int, origin : Int) -> Int = "fseek" +extern "C" fn file_seek_ffi( + handle : NativeFileHandle, + offset : Int, + origin : Int, +) -> Int = "bitlogger_file_seek" -extern "C" fn file_tell_ffi(handle : NativeFileHandle) -> Int = "ftell" +extern "C" fn file_tell_ffi(handle : NativeFileHandle) -> Int = "bitlogger_file_tell" #borrow(from_path, to_path) -extern "C" fn file_rename_ffi(from_path : Bytes, to_path : Bytes) -> Int = "rename" +extern "C" fn file_rename_ffi( + from_path : Bytes, + to_path : Bytes, +) -> Int = "bitlogger_file_rename" #borrow(path) -extern "C" fn file_remove_ffi(path : Bytes) -> Int = "remove" +extern "C" fn file_remove_ffi(path : Bytes) -> Int = "bitlogger_file_remove" pub struct FileHandle { path : String diff --git a/src/utils/moon.pkg b/src/utils/moon.pkg index 6cda0a0..0ebc5b8 100644 --- a/src/utils/moon.pkg +++ b/src/utils/moon.pkg @@ -7,6 +7,7 @@ import { } options( + "native-stub": [ "stub.c" ], targets: { "file_backend_native.mbt": [ "native", "llvm" ], "file_backend_stub.mbt": [ "js", "wasm", "wasm-gc" ], diff --git a/src/utils/stub.c b/src/utils/stub.c new file mode 100644 index 0000000..c794bfd --- /dev/null +++ b/src/utils/stub.c @@ -0,0 +1,51 @@ +#include + +#ifndef _CRT_SECURE_NO_WARNINGS +#define _CRT_SECURE_NO_WARNINGS +#endif + +#include + +int32_t bitlogger_pointer_is_null(void *ptr) { + return ptr == 0; +} + +void *bitlogger_file_open(const char *path, const char *mode) { +#if defined(__clang__) +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" +#endif + return fopen(path, mode); +#if defined(__clang__) +#pragma clang diagnostic pop +#endif +} + +int32_t bitlogger_file_write(const char *buffer, int32_t size, int32_t count, void *handle) { + return (int32_t)fwrite(buffer, (size_t)size, (size_t)count, (FILE *)handle); +} + +int32_t bitlogger_file_flush(void *handle) { + return fflush((FILE *)handle); +} + +int32_t bitlogger_file_close(void *handle) { + return fclose((FILE *)handle); +} + +int32_t bitlogger_file_seek(void *handle, int32_t offset, int32_t origin) { + return fseek((FILE *)handle, offset, origin); +} + +int32_t bitlogger_file_tell(void *handle) { + long position = ftell((FILE *)handle); + return position < 0 ? -1 : (int32_t)position; +} + +int32_t bitlogger_file_rename(const char *from_path, const char *to_path) { + return rename(from_path, to_path); +} + +int32_t bitlogger_file_remove(const char *path) { + return remove(path); +}