diff --git a/examples/json/test.noja b/examples/json/test.noja index 983cc71..ec6f3c4 100644 --- a/examples/json/test.noja +++ b/examples/json/test.noja @@ -1,5 +1,4 @@ - -json, err = import("json.noja"); +json = import("json.noja"); utils = import("utils.noja"); assert(json.parse("null") == none); @@ -9,4 +8,4 @@ assert(json.parse('"Hello, world!"') == "Hello, world!"); assert(utils.compareAny(json.parse('[]'), [])); assert(utils.compareAny(json.parse('[1, 2, 3]'), [1, 2, 3])); -print("No assertions fired!\n"); \ No newline at end of file +print("No assertions fired!\n"); diff --git a/src/lib/builtins/basic.c b/src/lib/builtins/basic.c index 5aaf4aa..5d3ad09 100644 --- a/src/lib/builtins/basic.c +++ b/src/lib/builtins/basic.c @@ -52,33 +52,20 @@ static int bin_print(Runtime *runtime, Object **argv, unsigned int argc, Object return 0; } -static const char *getCurrentScriptAbsolutePath(Runtime *runtime) -{ - Executable *exe = Runtime_GetCurrentExecutable(runtime); - assert(exe != NULL); - - Source *src = Executable_GetSource(exe); - if(src == NULL) - return NULL; - - const char *path = Source_GetAbsolutePath(src); - if(path == NULL) - return NULL; - - assert(path[0] != '\0'); - return path; -} - // Returns the length written in buff (not considering the zero byte) static size_t getCurrentScriptFolder(Runtime *runtime, char *buff, size_t buffsize) { - const char *path = getCurrentScriptAbsolutePath(runtime); + const char *path = Runtime_GetCurrentScriptAbsolutePath(runtime); if(path == NULL) { if(getcwd(buff, sizeof(buffsize)) == NULL) return 0; return strlen(buff); } + // This following block is a custom implementation + // of [dirname], which doesn't write into the input + // string and is way buggier. It will for sure give + // problems in the future!! size_t dir_len; { // This is buggy code!! diff --git a/src/lib/runtime/runtime.c b/src/lib/runtime/runtime.c index c133860..581d09f 100644 --- a/src/lib/runtime/runtime.c +++ b/src/lib/runtime/runtime.c @@ -56,6 +56,23 @@ struct xRuntime { Heap *heap; }; +const char *Runtime_GetCurrentScriptAbsolutePath(Runtime *runtime) +{ + Executable *exe = Runtime_GetCurrentExecutable(runtime); + assert(exe != NULL); + + Source *src = Executable_GetSource(exe); + if(src == NULL) + return NULL; + + const char *path = Source_GetAbsolutePath(src); + if(path == NULL) + return NULL; + + assert(path[0] != '\0'); + return path; +} + Stack *Runtime_GetStack(Runtime *runtime) { return Stack_Copy(runtime->stack, 1); diff --git a/src/lib/runtime/runtime.h b/src/lib/runtime/runtime.h index a8377c8..d1e345f 100644 --- a/src/lib/runtime/runtime.h +++ b/src/lib/runtime/runtime.h @@ -51,6 +51,7 @@ Object* Runtime_GetBuiltins(Runtime *runtime); void Runtime_SetBuiltins(Runtime *runtime, Object *builtins); int Runtime_GetCurrentIndex(Runtime *runtime); Executable *Runtime_GetCurrentExecutable(Runtime *runtime); +const char *Runtime_GetCurrentScriptAbsolutePath(Runtime *runtime); Snapshot *Snapshot_New(Runtime *runtime); void Snapshot_Free(Snapshot *snapshot); void Snapshot_Print(Snapshot *snapshot, FILE *fp); diff --git a/src/lib/utils/source.c b/src/lib/utils/source.c index 9ba93af..709723e 100644 --- a/src/lib/utils/source.c +++ b/src/lib/utils/source.c @@ -82,6 +82,32 @@ const char *Source_GetAbsolutePath(Source *src) return NULL; } +const char *makePathAbsolute(const char *path, char *buff, size_t buffsize) +{ + if(path[0] == '/') + // It's already absolute. + return path; + + if(getcwd(buff, buffsize) == NULL) + return NULL; + + size_t written = strlen(buff); + assert(buff[written-1] != '/'); + + if(written+1 >= buffsize) + return NULL; // No space for the / following the cwd. + + buff[written++] = '/'; + + size_t n = strlen(path); + if(written + n >= buffsize) + return NULL; + + memcpy(buff + written, path, n); + buff[written + n] = '\0'; + return buff; +} + Source *Source_FromFile(const char *file, Error *error) { assert(file != NULL); @@ -92,37 +118,10 @@ Source *Source_FromFile(const char *file, Error *error) } char maybe[1024]; - const char *abs_path; - - if(file[0] == '/') - abs_path = file; - else { - if(getcwd(maybe, sizeof(maybe)) == NULL) { - Error_Report(error, 0, "Internal buffer is too small"); - return NULL; - } - - size_t written = strlen(maybe); - assert(maybe[written-1] != '/'); - - if(written+1 >= sizeof(maybe)) { - Error_Report(error, 0, "Internal buffer is too small"); - return NULL; - } - - maybe[written] = '/'; - written += 1; - - size_t n = strlen(file); - if(written + n >= sizeof(maybe)) { - Error_Report(error, 0, "Internal buffer is too small"); - return NULL; - } - - memcpy(maybe + written, file, n); - maybe[written + n] = '\0'; - - abs_path = maybe; + const char *abs_path = makePathAbsolute(file, maybe, sizeof(maybe)); + if(abs_path == NULL) { + Error_Report(error, 0, "Internal buffer is too small"); + return NULL; } // Open the file and get it's size.