From ea9ed6af45febd5a6ef935f940a1b39496b9195b Mon Sep 17 00:00:00 2001 From: Akuli Date: Mon, 27 Jan 2025 03:14:01 +0200 Subject: [PATCH 1/3] use it in errno --- stdlib/errno.jou | 43 +++++++++++++++++++------------------------ 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/stdlib/errno.jou b/stdlib/errno.jou index 30f2d2b6..e04ac967 100644 --- a/stdlib/errno.jou +++ b/stdlib/errno.jou @@ -9,36 +9,31 @@ elif NETBSD: else: declare __errno_location() -> int* -# TODO: Ideally we would be able to place the if statements inside the functions. -if WINDOWS: - @public - def set_errno(value: int) -> None: + +@public +def set_errno(value: int) -> None: + if WINDOWS: *_errno() = value - @public - def get_errno() -> int: - return *_errno() -elif MACOS: - @public - def set_errno(value: int) -> None: + elif MACOS: *__error() = value - @public - def get_errno() -> int: - return *__error() -elif NETBSD: - @public - def set_errno(value: int) -> None: + elif NETBSD: *__errno() = value - @public - def get_errno() -> int: - return *__errno() -else: - @public - def set_errno(value: int) -> None: + else: *__errno_location() = value - @public - def get_errno() -> int: + + +@public +def get_errno() -> int: + if WINDOWS: + return *_errno() + elif MACOS: + return *__error() + elif NETBSD: + return *__errno() + else: return *__errno_location() + # Convert an error code into a string. Do not modify or free() the returned string. @public declare strerror(errno_value: int) -> byte* From 381530756114d798dc4833d1729b10136a41a53d Mon Sep 17 00:00:00 2001 From: Akuli Date: Mon, 27 Jan 2025 03:19:41 +0200 Subject: [PATCH 2/3] use in compiler --- compiler/paths.jou | 66 ++++++++++++++++------------------------------ 1 file changed, 23 insertions(+), 43 deletions(-) diff --git a/compiler/paths.jou b/compiler/paths.jou index bb60be45..341e7f14 100644 --- a/compiler/paths.jou +++ b/compiler/paths.jou @@ -23,53 +23,40 @@ declare dirname(path: byte*) -> byte* declare stat(path: byte*, buf: byte[1000]*) -> int # lol -def fail_finding_exe() -> noreturn: - # TODO: include os error message (GetLastError / errno) - fprintf(stderr, "error: cannot locate currently running executable, needed for finding the Jou standard library\n") - exit(1) - - -if WINDOWS: - @public - def find_current_executable() -> byte*: - buf = NULL - for size = 2L; True; size *= 2: - buf = realloc(buf, size) - memset(buf, 0, size) - ret = GetModuleFileNameA(NULL, buf, size as int) - if ret <= 0: - fail_finding_exe() - if ret < size: - # buffer is big enough, it fits - return buf - -elif MACOS: - @public - def find_current_executable() -> byte*: +@public +def find_current_executable() -> byte*: + if MACOS: n = 1 result: byte* = malloc(n) ret = _NSGetExecutablePath(result, &n) # sets n to desired size assert ret < 0 # didn't fit result = realloc(result, n) ret = _NSGetExecutablePath(result, &n) - if ret != 0: - fail_finding_exe() - return result + if ret == 0: + # success + return result -else: - @public - def find_current_executable() -> byte*: + else: buf = NULL for size = 2L; True; size *= 2: buf = realloc(buf, size) memset(buf, 0, size) - ret = readlink("/proc/self/exe", buf, size) + + if WINDOWS: + ret = GetModuleFileNameA(NULL, buf, size as int) + else: + ret = readlink("/proc/self/exe", buf, size) + if ret <= 0: - fail_finding_exe() + break # error if ret < size: # buffer is big enough, it fits return buf + # TODO: show os error message? (GetLastError / errno) + fprintf(stderr, "error: cannot locate currently running executable, needed for finding the Jou standard library\n") + exit(1) + @public def find_stdlib() -> byte*: @@ -110,16 +97,6 @@ def find_stdlib() -> byte*: fprintf(stderr, " %s\n", checked[i]) exit(1) -# Ignoring return values, because there's currently no way to check errno. -# We need to ignore the error when directory exists already (EEXIST). -# Ideally we wouldn't ignore any other errors. -if WINDOWS: - def my_mkdir(path: byte*) -> None: - _mkdir(path) -else: - def my_mkdir(path: byte*) -> None: - mkdir(path, 0o777) # this is what mkdir in bash does according to strace - def write_gitignore(p: byte*) -> None: filename: byte* = malloc(strlen(p) + 100) @@ -139,10 +116,13 @@ def write_gitignore(p: byte*) -> None: free(filename) -def mkdir_exist_ok(p: byte*) -> None: +def mkdir_exist_ok(path: byte*) -> None: # TODO: check if errno == EEXIST # Currently no good way to access EEXIST constant - my_mkdir(p) + if WINDOWS: + _mkdir(path) + else: + mkdir(path, 0o777) # this is what mkdir in bash does according to strace @public From bb4382ee8dca21e3264ecc0d1bc71af7586d9149 Mon Sep 17 00:00:00 2001 From: Akuli Date: Mon, 27 Jan 2025 23:09:07 +0200 Subject: [PATCH 3/3] add to bootstrap --- bootstrap.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/bootstrap.sh b/bootstrap.sh index f3ea266d..6dd12233 100755 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -20,6 +20,7 @@ commits=( 1c7ce74933aea8a8862fd1d4409735b9fb7a1d7e # last commit on main that contains the compiler written in C b339b1b300ba98a2245b493a58dd7fab4c465020 # "match ... with ..." syntax 874d1978044a080173fcdcc4e92736136c97dd61 # "match some_integer:" support + 8a0bb4b50ef77932243f4f65ae90be84e763ec45 # evaluating "if WINDOWS:" and such inside functions ) for commit in ${commits[@]}; do