more code restructing for isolating onyx_runtime
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 10 Dec 2021 16:28:39 +0000 (10:28 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 10 Dec 2021 16:28:39 +0000 (10:28 -0600)
include/bh.h
include/utils.h
src/onyx.c
src/utils.c
src/wasm_emit.c
src/wasm_runtime.c

index f223754e3b2de23b0be90ad8a7a87d234ae01ae2..7483ff9c95303972ce430aa3b9f3cb01e9430da0 100644 (file)
@@ -377,6 +377,10 @@ b32 bh_file_exists(char const* filename);
 char* bh_path_get_full_name(char const* filename, bh_allocator a);
 char* bh_path_get_parent(char const* filename, bh_allocator a);
 
+// This function returns a volatile pointer. Do not store it without copying!
+// `included_folders` is bh_arr(const char *).
+char* bh_lookup_file(char* filename, char* relative_to, char *suffix, b32 add_suffix, const char ** included_folders, b32 search_included_folders);
+
 #define bh_file_read_contents(allocator_, x) _Generic((x), \
     bh_file*: bh_file_read_contents_bh_file, \
     const char*: bh_file_read_contents_direct, \
@@ -1607,22 +1611,65 @@ char* bh_path_get_full_name(char const* filename, bh_allocator a) {
 }
 
 // NOTE: This assumes the filename is the full path, not relative to anything else.
-char* bh_path_get_parent(char const* filename, bh_allocator a) {
 #if defined(_BH_LINUX)
     #define DIR_SEPARATOR '/'
 #elif defined(_BH_WINDOWS)
     #define DIR_SEPARATOR '\\'
 #endif
+char* bh_path_get_parent(char const* filename, bh_allocator a) {
 
     char* result = bh_strdup(a, (char *) filename);
     char* end = result + strlen(result);
     while (*end != DIR_SEPARATOR && end != result) *end-- = '\0';
 
     return result;
+}
 
-#undef DIR_SEPARATOR
+// This function returns a volatile pointer. Do not store it without copying!
+char* bh_lookup_file(char* filename, char* relative_to, char *suffix, b32 add_suffix, bh_arr(const char *) included_folders, b32 search_included_folders) {
+    assert(relative_to != NULL);
+
+    static char path[512];
+    fori (i, 0, 512) path[i] = 0;
+
+    static char fn[256];
+    fori (i, 0, 256) fn[i] = 0;
+
+    if (!bh_str_ends_with(filename, suffix) && add_suffix) {
+        bh_snprintf(fn, 256, "%s%s", filename, suffix);
+    } else {
+        bh_snprintf(fn, 256, "%s", filename);
+    }
+
+    fori (i, 0, 256) if (fn[i] == '/') fn[i] = DIR_SEPARATOR;
+
+    if (bh_str_starts_with(filename, "./")) {
+        if (relative_to[strlen(relative_to) - 1] != DIR_SEPARATOR)
+            bh_snprintf(path, 512, "%s%c%s", relative_to, DIR_SEPARATOR, fn + 2);
+        else
+            bh_snprintf(path, 512, "%s%s", relative_to, fn + 2);
+
+        if (bh_file_exists(path)) return bh_path_get_full_name(path, bh_heap_allocator());
+
+        return fn;
+    }
+
+    if (search_included_folders) {
+        bh_arr_each(const char *, folder, included_folders) {
+            if ((*folder)[strlen(*folder) - 1] != DIR_SEPARATOR)
+                bh_snprintf(path, 512, "%s%c%s", *folder, DIR_SEPARATOR, fn);
+            else
+                bh_snprintf(path, 512, "%s%s", *folder, fn);
+
+            if (bh_file_exists(path)) return bh_path_get_full_name(path, bh_heap_allocator());
+        }
+    }
+
+    return fn;
 }
 
+#undef DIR_SEPARATOR
+
 #endif // ifndef BH_NO_FILE
 
 
index a53b983dc3531c5199729301fb4e39393c635f2d..626e8a2ecba0493fccc52f188e3dc1d500adcee7 100644 (file)
@@ -33,12 +33,6 @@ u32 char_to_base16_value(char x);
 // Returns the length after processing the string.
 i32 string_process_escape_seqs(char* dest, char* src, i32 len);
 
-// NOTE: This should not be called until immediately before using the return value.
-// This function can return a static variable which will change if this is called
-// another time.                                        -brendanfh 2020/10/09
-// :RelativeFiles This should lookup for the file relative to "relative_to"
-char* lookup_included_file(char* filename, char* relative_to, char *suffix, b32 add_suffix, bh_arr(const char *) included_folders, b32 search_included_folders);
-
 u32 levenshtein_distance(const char *str1, const char *str2);
 char *find_closest_symbol_in_node(AstNode *node, char *sym);
 
index 3331c08c2bd7ad993cb97b58ead10fafd9437e4f..32472b8011223d11bf874020ab0d227456ca4cce 100644 (file)
@@ -315,7 +315,7 @@ static void process_load_entity(Entity* ent) {
 
         char* parent_folder = bh_path_get_parent(parent_file, global_scratch_allocator);
         
-        char* filename = lookup_included_file(include->name, parent_folder, ".onyx", 1, context.options->included_folders, 1);
+        char* filename = bh_lookup_file(include->name, parent_folder, ".onyx", 1, context.options->included_folders, 1);
         char* formatted_name = bh_strdup(global_heap_allocator, filename);
 
         process_source_file(formatted_name, include->token->pos);
index 109bded28f9a0197bd47e983fdf779d2e3748cc4..d23ccb1c8277766148a40238824becb7353772b3 100644 (file)
@@ -955,56 +955,6 @@ i32 string_process_escape_seqs(char* dest, char* src, i32 len) {
     return total_len;
 }
 
-char* lookup_included_file(char* filename, char* relative_to, char *suffix, b32 add_suffix, bh_arr(const char *) included_folders, b32 search_included_folders) {
-    assert(relative_to != NULL);
-
-    static char path[256];
-    fori (i, 0, 256) path[i] = 0;
-
-    static char fn[128];
-    fori (i, 0, 128) fn[i] = 0;
-
-    if (!bh_str_ends_with(filename, suffix) && add_suffix) {
-        bh_snprintf(fn, 128, "%s%s", filename, suffix);
-    } else {
-        bh_snprintf(fn, 128, "%s", filename);
-    }
-
-#if defined(_BH_LINUX)
-    #define DIR_SEPARATOR '/'
-#elif defined(_BH_WINDOWS)
-    #define DIR_SEPARATOR '\\'
-#endif
-
-    fori (i, 0, 128) if (fn[i] == '/') fn[i] = DIR_SEPARATOR;
-
-    if (bh_str_starts_with(filename, "./")) {
-        if (relative_to[strlen(relative_to) - 1] != DIR_SEPARATOR)
-            bh_snprintf(path, 256, "%s%c%s", relative_to, DIR_SEPARATOR, fn + 2);
-        else
-            bh_snprintf(path, 256, "%s%s", relative_to, fn + 2);
-
-        if (bh_file_exists(path)) return bh_path_get_full_name(path, global_scratch_allocator);
-
-        return fn;
-    }
-
-    if (search_included_folders) {
-        bh_arr_each(const char *, folder, included_folders) {
-            if ((*folder)[strlen(*folder) - 1] != DIR_SEPARATOR)
-                bh_snprintf(path, 256, "%s%c%s", *folder, DIR_SEPARATOR, fn);
-            else
-                bh_snprintf(path, 256, "%s%s", *folder, fn);
-
-            if (bh_file_exists(path)) return bh_path_get_full_name(path, global_scratch_allocator);
-        }
-    }
-
-    return fn;
-
-#undef DIR_SEPARATOR
-}
-
 u32 levenshtein_distance(const char *str1, const char *str2) {
     i32 m = strlen(str1) + 1;
     i32 n = strlen(str2) + 1;
index e34de4b418839dcda32eea509eac21f9bf193a95..799a1ecd3a65f945ba38ab2eaf14bca03da27369 100644 (file)
@@ -3655,7 +3655,7 @@ static void emit_file_contents(OnyxWasmModule* mod, AstFileContents* fc) {
 
         char* temp_fn     = bh_alloc_array(global_scratch_allocator, char, fc->filename_token->length);
         i32   temp_fn_len = string_process_escape_seqs(temp_fn, fc->filename_token->text, fc->filename_token->length);
-        char* filename    = lookup_included_file(temp_fn, parent_folder, "", 0, NULL, 0);
+        char* filename    = bh_lookup_file(temp_fn, parent_folder, "", 0, NULL, 0);
         fc->filename      = bh_strdup(global_heap_allocator, filename);
     }
 
index bd52138c5d70b5957c39181278f0d44ebf17048d..e11332c8a56511ba56ce1d878306c9855f2d4860 100644 (file)
@@ -519,12 +519,12 @@ static void onyx_load_library(char *name) {
     }
 
     char *library_load_name_tmp = bh_bprintf("onyx_library_%s", library);
-    char *library_load_name = alloca(strlen(library_load_name_tmp));
+    char *library_load_name = alloca(strlen(library_load_name_tmp) + 1);
     strcpy(library_load_name, library_load_name_tmp);
     LibraryLinker library_load;
 
     #ifdef _BH_LINUX
-    char *library_name = lookup_included_file(name, ".", ".so", 1, (const char **) library_paths, 1);
+    char *library_name = bh_lookup_file(name, ".", ".so", 1, (const char **) library_paths, 1);
     void* handle = dlopen(library_name, RTLD_LAZY);
     if (handle == NULL) {
         printf("ERROR LOADING LIBRARY %s: %s\n", name, dlerror());
@@ -539,7 +539,7 @@ static void onyx_load_library(char *name) {
     #endif
 
     #ifdef _BH_WINDOWS
-    char *library_name = lookup_included_file(name, ".", ".dll", 1, (const char **) library_paths, 1);
+    char *library_name = bh_lookup_file(name, ".", ".dll", 1, (const char **) library_paths, 1);
     HMODULE handle = LoadLibraryA(library_name);
     if (handle == NULL) {
         printf("ERROR LOADING LIBRARY %s: %d\n", name, GetLastError());