Skip to content

Commit

Permalink
Merge pull request #234 from IdWV/fs
Browse files Browse the repository at this point in the history
lib: Removed global variables from module fs for thread safety
  • Loading branch information
jow- authored Dec 1, 2024
2 parents 60e7a88 + 63e18ea commit b0b5d93
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
11 changes: 11 additions & 0 deletions include/ucode/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
37 changes: 18 additions & 19 deletions lib/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 *
Expand Down Expand Up @@ -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);
}


Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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);
}


Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit b0b5d93

Please sign in to comment.