From 178288dcf87030a06133f35bfaba7f866b236466 Mon Sep 17 00:00:00 2001 From: Young-Flash Date: Wed, 22 Jan 2025 15:59:32 +0800 Subject: [PATCH] internal: use native api in c_runtime --- sync_io/moon.pkg.json | 8 +--- sync_io/native_stub.c | 77 -------------------------------------- sync_io/sync_io.mbt | 2 +- sync_io/sync_io_native.mbt | 6 +-- 4 files changed, 6 insertions(+), 87 deletions(-) delete mode 100644 sync_io/native_stub.c diff --git a/sync_io/moon.pkg.json b/sync_io/moon.pkg.json index 3cbc9dfd3..43ac59fb7 100644 --- a/sync_io/moon.pkg.json +++ b/sync_io/moon.pkg.json @@ -8,11 +8,7 @@ "targets": { "sync_io_wasm.mbt": ["wasm", "wasm-gc"], "sync_io_js.mbt": ["js"], - "sync_io_native.mbt": ["native"] - }, - "link": { - "native": { - "cc-flags": "./sync_io/native_stub.c" - } + "sync_io_native.mbt": ["native"], + "util.mbt": ["wasm", "wasm-gc"] } } diff --git a/sync_io/native_stub.c b/sync_io/native_stub.c deleted file mode 100644 index 74ca9b494..000000000 --- a/sync_io/native_stub.c +++ /dev/null @@ -1,77 +0,0 @@ -#include -#include -#include -#include -#include "moonbit.h" - -#ifdef _WIN32 -#include -#include -#else -#include -#include -#endif - -int path_exists(struct moonbit_bytes *path) { - struct stat buffer; - int status = stat((const char *)(path->data), &buffer); - if (status == 0) { - return 0; - } - return -1; -} - -struct moonbit_bytes* read_file_to_bytes(struct moonbit_bytes *filename) { - FILE *file = fopen((const char*)(filename->data), "rb"); - if (file == NULL) { - perror("fopen"); - return NULL; - } - - // move file pointer to the end of the file - if (fseek(file, 0, SEEK_END) != 0) { - perror("fseek"); - fclose(file); - return NULL; - } - - // get the current position of the file pointer, which is the file size - long size = ftell(file); - if (size == -1L) { - perror("ftell"); - fclose(file); - return NULL; - } - - if (fseek(file, 0, SEEK_SET) != 0) { - perror("fseek"); - fclose(file); - return NULL; - } - - struct moonbit_bytes* bytes = moonbit_make_bytes(size, 0); - - // read the file content into the bytes->data - size_t bytes_read = fread(bytes->data, 1, (size_t)size, file); - if (bytes_read != (size_t)size) { - perror("fread"); - fclose(file); - return NULL; - } - - // close the file - if (fclose(file) != 0) { - perror("fclose"); - return NULL; - } - - return bytes; -} - -void write_bytes_to_file(struct moonbit_bytes* path, struct moonbit_bytes* content) { - FILE *file = fopen((const char *)(path->data), "wb"); - size_t content_size = Moonbit_array_length(content); - fwrite(content->data, 1, content_size, file); - fflush(file); - fclose(file); -} diff --git a/sync_io/sync_io.mbt b/sync_io/sync_io.mbt index 87808dd78..48ce3867c 100644 --- a/sync_io/sync_io.mbt +++ b/sync_io/sync_io.mbt @@ -13,7 +13,7 @@ // limitations under the License. ///| -type! IOError { +pub type! IOError { NotFound(String) } diff --git a/sync_io/sync_io_native.mbt b/sync_io/sync_io_native.mbt index 865530303..e50258be6 100644 --- a/sync_io/sync_io_native.mbt +++ b/sync_io/sync_io_native.mbt @@ -20,7 +20,7 @@ fn read_file_to_bytes_internal(path : String) -> Bytes! { } ///| -extern "C" fn read_file_to_bytes_ffi(path : Bytes) -> Bytes = "read_file_to_bytes" +fn read_file_to_bytes_ffi(path : Bytes) -> Bytes = "$moonbit.read_file_to_bytes" ///| fn write_bytes_to_file_internal(path : String, content : Bytes) -> Unit { @@ -29,7 +29,7 @@ fn write_bytes_to_file_internal(path : String, content : Bytes) -> Unit { } ///| -extern "C" fn write_bytes_to_file_ffi(path : Bytes, content : Bytes) = "write_bytes_to_file" +fn write_bytes_to_file_ffi(path : Bytes, content : Bytes) = "$moonbit.write_bytes_to_file" ///| fn path_exists_internal(path : String) -> Bool { @@ -37,7 +37,7 @@ fn path_exists_internal(path : String) -> Bool { } ///| -extern "C" fn path_exists_ffi(path : Bytes) -> Int = "path_exists" +fn path_exists_ffi(path : Bytes) -> Int = "$moonbit.path_exists" ///| fn mbt_string_to_utf8_bytes(str : String, is_c_str : Bool) -> Bytes {