]> git.lizzy.rs Git - uwu-lang.git/commitdiff
common/ refactoring
authorElias Fleckenstein <eliasfleckenstein@web.de>
Thu, 30 Dec 2021 20:09:41 +0000 (21:09 +0100)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Thu, 30 Dec 2021 20:09:41 +0000 (21:09 +0100)
common/dl.h [new file with mode: 0644]
common/err.h
common/file.h [new file with mode: 0644]
common/str.h
src/load.c
src/run.c

diff --git a/common/dl.h b/common/dl.h
new file mode 100644 (file)
index 0000000..143b422
--- /dev/null
@@ -0,0 +1,14 @@
+#ifndef _COMMON_DL_H_
+#define _COMMON_DL_H_
+
+#include <dlfcn.h>
+#include "err.h"
+
+inline static void check_dlerror()
+{
+       char *err = dlerror();
+       if (err)
+               error("library error: %s\n", err);
+}
+
+#endif
index 1dccbf9b943c0e1e1125985f6875b75652a89c60..34620bb8835d13325fa912acea1c183d1555c8d8 100644 (file)
@@ -1,5 +1,5 @@
-#ifndef _ERR_H_
-#define _ERR_H_
+#ifndef _COMMON_ERR_H_
+#define _COMMON_ERR_H_
 
 #include <stdio.h>
 #include <stdlib.h>
diff --git a/common/file.h b/common/file.h
new file mode 100644 (file)
index 0000000..c5e8036
--- /dev/null
@@ -0,0 +1,19 @@
+#ifndef _COMMON_FILE_H_
+#define _COMMON_FILE_H_
+
+#include <stdio.h>
+#include <stdbool.h>
+
+inline static bool file_exists(const char *filename)
+{
+       FILE *f = fopen(filename, "r");
+
+       if (f) {
+               fclose(f);
+               return true;
+       }
+
+       return false;
+}
+
+#endif
index b0baa37e606fac4b59ad29e04186dab100f18ef6..fc5f587d202ba1e6f13a5d5634afad85b0e6f4fa 100644 (file)
@@ -1,5 +1,5 @@
-#ifndef _UTIL_H_
-#define _UTIL_H_
+#ifndef _COMMON_STR_H_
+#define _COMMON_STR_H_
 
 #include <stdio.h>
 #include <stdarg.h>
index edeba9759814b3850d0155b0bb5316b8b2eb71ec..aeee1c2427cbdd45e8daadc358d0f1adee279178 100644 (file)
@@ -6,6 +6,8 @@
 #include <dlfcn.h>
 #include "common/err.h"
 #include "common/str.h"
+#include "common/file.h"
+#include "common/dl.h"
 #include "load.h"
 #include "parse.h"
 
@@ -33,18 +35,6 @@ static char *dirname_wrapper(const char *name)
        return wrap_name_func(name, &dirname);
 }
 
-static bool file_exists(const char *filename)
-{
-       FILE *f = fopen(filename, "r");
-
-       if (f) {
-               fclose(f);
-               return true;
-       }
-
-       return false;
-}
-
 // type definitions
 
 typedef struct
@@ -148,9 +138,7 @@ static Module *require_module(LoadState *state, char *module_path)
                state->program.libraries = realloc(state->program.libraries, sizeof(void *) * ++state->program.num_libraries);
                state->program.libraries[state->program.num_libraries - 1] = module->handle.lib = dlopen(filename, RTLD_LAZY);
 
-               char *err = dlerror();
-               if (err)
-                       error("library error: %s\n", err);
+               check_dlerror();
        }
 
        return module;
@@ -293,10 +281,7 @@ static void load_functions(LoadState *state, Module *module)
                        char *symbol = asprintf_wrapper("uwu_%s", link->name);
                        link->ref->value.native = dlsym(module->handle.lib, symbol);
 
-                       char *err = dlerror();
-                       if (err)
-                               error("library error: %s\n", err);
-
+                       check_dlerror();
                        free(symbol);
                }
        }
index 7733c8da5a5db1bf8adaf29606fd3a725fa4c42d..36133fce7cf4ae6837681e0db7b063eb17596bb1 100644 (file)
--- a/src/run.c
+++ b/src/run.c
@@ -2,6 +2,7 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include "common/err.h"
+#include "common/dl.h"
 #include "load.h"
 #include "run.h"
 
@@ -29,9 +30,7 @@ void run_module(const char *progname, const char *modname, size_t num_args, char
        char      *(*uwuvm_print_value  )(UwUVMValue                                             ) = dlsym(program.api_library, "uwuvm_print_value"  );
        void       (*uwuvm_delet_value  )(UwUVMValue                                             ) = dlsym(program.api_library, "uwuvm_delet_value"  );
 
-       char *err = dlerror();
-       if (err)
-               error("library error: %s\n", err);
+       check_dlerror();
 
        UwUVMExpression arg_expressions[num_args];