From: Brendan Hansen Date: Fri, 10 Dec 2021 16:28:39 +0000 (-0600) Subject: more code restructing for isolating onyx_runtime X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=0fcd77f70e391f29b5ca4a125193b5ed0a72be31;p=onyx.git more code restructing for isolating onyx_runtime --- diff --git a/include/bh.h b/include/bh.h index f223754e..7483ff9c 100644 --- a/include/bh.h +++ b/include/bh.h @@ -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 diff --git a/include/utils.h b/include/utils.h index a53b983d..626e8a2e 100644 --- a/include/utils.h +++ b/include/utils.h @@ -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); diff --git a/src/onyx.c b/src/onyx.c index 3331c08c..32472b80 100644 --- a/src/onyx.c +++ b/src/onyx.c @@ -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); diff --git a/src/utils.c b/src/utils.c index 109bded2..d23ccb1c 100644 --- a/src/utils.c +++ b/src/utils.c @@ -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; diff --git a/src/wasm_emit.c b/src/wasm_emit.c index e34de4b4..799a1ecd 100644 --- a/src/wasm_emit.c +++ b/src/wasm_emit.c @@ -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); } diff --git a/src/wasm_runtime.c b/src/wasm_runtime.c index bd52138c..e11332c8 100644 --- a/src/wasm_runtime.c +++ b/src/wasm_runtime.c @@ -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());