From b1bd7b53123e3e3b8da2fc30bda8dc22c2cdb170 Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Fri, 18 Oct 2024 14:50:58 +0200 Subject: types: add ucv_resource_create() helper Introduce a new inline convenience function ucv_resource_create() which simplifies creating resource values by resource type name by combining resource type lookup and resource value creation in one call. This function will be used in subsequent refactoring to eliminate global static variables. Suggested-by: Isaac de Wolff [separated from original commit, move ucv_resource_create() into types.h] Signed-off-by: Jo-Philipp Wich --- include/ucode/types.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/include/ucode/types.h b/include/ucode/types.h index 2334029..bf8b9f2 100644 --- a/include/ucode/types.h +++ b/include/ucode/types.h @@ -422,6 +422,17 @@ uc_value_t *ucv_resource_new(uc_resource_type_t *, void *); void *ucv_resource_data(uc_value_t *uv, const char *); void **ucv_resource_dataptr(uc_value_t *, const char *); +static inline uc_value_t * +ucv_resource_create(uc_vm_t *vm, const char *typename, void *value) +{ + uc_resource_type_t *t = NULL; + + if (typename && (t = ucv_resource_type_lookup(vm, typename)) == NULL) + return NULL; + + return ucv_resource_new(t, value); +} + uc_value_t *ucv_regexp_new(const char *, bool, bool, bool, char **); uc_value_t *ucv_upvalref_new(size_t); -- cgit v1.2.3 From 63e18eae374e1fc6dfa162e8f9890f526eb16666 Mon Sep 17 00:00:00 2001 From: Isaac de Wolff Date: Tue, 8 Oct 2024 19:05:05 +0200 Subject: fs: eliminate the usage of global variables Move last error code into a VM registry value and lookup resource types within the current VM context at call time, allowing the proper use of the fs module within multiple threads of a multithreaded application. Suggested-by: Isaac de Wolff [turn err_return() back into a local macro, simplify uc_fs_error(), align whitespace style] Signed-off-by: Jo-Philipp Wich --- lib/fs.c | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/lib/fs.c b/lib/fs.c index e3f8a4a..3572ff2 100644 --- a/lib/fs.c +++ b/lib/fs.c @@ -68,12 +68,11 @@ #include "ucode/module.h" #include "ucode/platform.h" -#define err_return(err) do { last_error = err; return NULL; } while(0) +#define err_return(err) do { \ + uc_vm_registry_set(vm, "fs.last_error", ucv_int64_new(err)); \ + return NULL; \ +} while(0) -//static const uc_ops *ops; -static uc_resource_type_t *file_type, *proc_type, *dir_type; - -static int last_error = 0; /** * Query error information. @@ -96,15 +95,14 @@ static int last_error = 0; static uc_value_t * uc_fs_error(uc_vm_t *vm, size_t nargs) { - uc_value_t *errmsg; + int last_error = ucv_int64_get(uc_vm_registry_get(vm, "fs.last_error")); if (last_error == 0) return NULL; - errmsg = ucv_string_new(strerror(last_error)); - last_error = 0; + uc_vm_registry_set(vm, "fs.last_error", ucv_int64_new(0)); - return errmsg; + return ucv_string_new(strerror(last_error)); } static uc_value_t * @@ -511,7 +509,7 @@ uc_fs_popen(uc_vm_t *vm, size_t nargs) if (!fp) err_return(errno); - return uc_resource_new(proc_type, fp); + return ucv_resource_create(vm, "fs.proc", fp); } @@ -1178,7 +1176,7 @@ uc_fs_open(uc_vm_t *vm, size_t nargs) err_return(i); } - return uc_resource_new(file_type, fp); + return ucv_resource_create(vm, "fs.file", fp); } /** @@ -1237,7 +1235,7 @@ uc_fs_fdopen(uc_vm_t *vm, size_t nargs) if (!fp) err_return(errno); - return uc_resource_new(file_type, fp); + return ucv_resource_create(vm, "fs.file", fp); } @@ -1438,7 +1436,7 @@ uc_fs_opendir(uc_vm_t *vm, size_t nargs) if (!dp) err_return(errno); - return uc_resource_new(dir_type, dp); + return ucv_resource_create(vm, "fs.dir", dp); } /** @@ -2400,7 +2398,7 @@ uc_fs_mkstemp(uc_vm_t *vm, size_t nargs) err_return(errno); } - return uc_resource_new(file_type, fp); + return ucv_resource_create(vm, "fs.file", fp); } /** @@ -2770,8 +2768,8 @@ uc_fs_pipe(uc_vm_t *vm, size_t nargs) rv = ucv_array_new_length(vm, 2); - ucv_array_push(rv, uc_resource_new(file_type, rfp)); - ucv_array_push(rv, uc_resource_new(file_type, wfp)); + ucv_array_push(rv, ucv_resource_create(vm, "fs.file", rfp)); + ucv_array_push(rv, ucv_resource_create(vm, "fs.file", wfp)); return rv; } @@ -2873,9 +2871,10 @@ void uc_module_init(uc_vm_t *vm, uc_value_t *scope) { uc_function_list_register(scope, global_fns); - proc_type = uc_type_declare(vm, "fs.proc", proc_fns, close_proc); - file_type = uc_type_declare(vm, "fs.file", file_fns, close_file); - dir_type = uc_type_declare(vm, "fs.dir", dir_fns, close_dir); + uc_type_declare(vm, "fs.proc", proc_fns, close_proc); + uc_type_declare(vm, "fs.dir", dir_fns, close_dir); + + uc_resource_type_t *file_type = uc_type_declare(vm, "fs.file", file_fns, close_file); ucv_object_add(scope, "stdin", uc_resource_new(file_type, stdin)); ucv_object_add(scope, "stdout", uc_resource_new(file_type, stdout)); -- cgit v1.2.3