From b16ca5a986c129500285c238bbe1d3e0dd44e75e Mon Sep 17 00:00:00 2001 From: Jeroen van Rijn Date: Sun, 8 Feb 2026 13:51:49 +0100 Subject: fix libc-shim --- vendor/libc-shim/stdio_js.odin | 60 ++++++++++++++++++++++++ vendor/libc-shim/stdio_os.odin | 104 +++++++++++++++++++++++++++++++++++++++++ vendor/libc/stdio_js.odin | 60 ------------------------ vendor/libc/stdio_os.odin | 104 ----------------------------------------- 4 files changed, 164 insertions(+), 164 deletions(-) create mode 100644 vendor/libc-shim/stdio_js.odin create mode 100644 vendor/libc-shim/stdio_os.odin delete mode 100644 vendor/libc/stdio_js.odin delete mode 100644 vendor/libc/stdio_os.odin diff --git a/vendor/libc-shim/stdio_js.odin b/vendor/libc-shim/stdio_js.odin new file mode 100644 index 000000000..2382ed449 --- /dev/null +++ b/vendor/libc-shim/stdio_js.odin @@ -0,0 +1,60 @@ +#+private +package odin_libc + +import "core:c" + +foreign import "odin_env" + +_fopen :: proc(path, mode: cstring) -> FILE { + unimplemented("vendor/libc: fopen in JS") +} + +_fseek :: proc(file: FILE, offset: c.long, whence: i32) -> i32 { + unimplemented("vendor/libc: fseek in JS") +} + +_ftell :: proc(file: FILE) -> c.long { + unimplemented("vendor/libc: ftell in JS") +} + +_fclose :: proc(file: FILE) -> i32 { + unimplemented("vendor/libc: fclose in JS") +} + +_fread :: proc(buffer: [^]byte, size: uint, count: uint, file: FILE) -> uint { + unimplemented("vendor/libc: fread in JS") +} + +_fwrite :: proc(buffer: [^]byte, size: uint, count: uint, file: FILE) -> uint { + fd, ok := __fd(file) + if !ok { + return 0 + } + + __write(fd, buffer[:size*count]) + return count +} + +_putchar :: proc(char: c.int) -> c.int { + __write(1, {byte(char)}) + return char +} + +_getchar :: proc() -> c.int { + return EOF +} + +@(private="file") +foreign odin_env { + @(link_name="write") + __write :: proc "contextless" (fd: u32, p: []byte) --- +} + +@(private="file") +__fd :: proc(file: FILE) -> (u32, bool) { + switch (uint(uintptr(file))) { + case 2: return 1, true // stdout + case 3: return 2, true // stderr + case: return 0, false + } +} diff --git a/vendor/libc-shim/stdio_os.odin b/vendor/libc-shim/stdio_os.odin new file mode 100644 index 000000000..db40fb250 --- /dev/null +++ b/vendor/libc-shim/stdio_os.odin @@ -0,0 +1,104 @@ +#+build !freestanding +#+build !js +package odin_libc + +import "core:io" +import "core:c" +import os "core:os/os2" + +_fopen :: proc(path, _mode: cstring) -> FILE { + flags: os.File_Flags + + mode := string(_mode) + if len(mode) > 1 { + switch mode[0] { + case 'r': + flags += {.Read} + case 'w': + flags += {.Write, .Create, .Trunc} + case 'a': + flags += {.Write, .Create, .Append} + case: + return nil + } + + if len(mode) > 1 && mode[1] == '+' { + flags += {.Write, .Read} + } else if len(mode) > 2 && mode[1] == 'b' && mode[2] == '+' { + flags += {.Write, .Read} + } + } + + file, err := os.open(string(path), flags, os.Permissions_Read_Write_All) + if err != nil { + return nil + } + + return FILE(file) +} + +_fseek :: proc(_file: FILE, offset: c.long, whence: i32) -> i32 { + file := __file(_file) + if _, err := os.seek(file, i64(offset), io.Seek_From(whence)); err != nil { + return -1 + } + + return 0 +} + +_ftell :: proc(_file: FILE) -> c.long { + file := __file(_file) + pos, err := os.seek(file, 0, .Current) + if err != nil { + return -1 + } + + return c.long(pos) +} + +_fclose :: proc(_file: FILE) -> i32 { + file := __file(_file) + if err := os.close(file); err != nil { + return EOF + } + + return 0 +} + +_fread :: proc(buffer: [^]byte, size: uint, count: uint, _file: FILE) -> uint { + file := __file(_file) + n, _ := os.read(file, buffer[:size*count]) + return uint(max(0, n)) / size +} + +_fwrite :: proc(buffer: [^]byte, size: uint, count: uint, _file: FILE) -> uint { + file := __file(_file) + n, _ := os.write(file, buffer[:size*count]) + return uint(max(0, n)) / size +} + +_putchar :: proc(char: c.int) -> c.int { + n, err := os.write_byte(os.stdout, byte(char)) + if n == 0 || err != nil { + return EOF + } + return char +} + +_getchar :: proc() -> c.int { + ret: [1]byte + n, err := os.read(os.stdin, ret[:]) + if n == 0 || err != nil { + return EOF + } + return c.int(ret[0]) +} + +@(private="file") +__file :: proc(file: FILE) -> ^os.File { + switch (uint(uintptr(file))) { + case 2: return os.stdout + case 3: return os.stderr + case: return (^os.File)(file) + } +} diff --git a/vendor/libc/stdio_js.odin b/vendor/libc/stdio_js.odin deleted file mode 100644 index 2382ed449..000000000 --- a/vendor/libc/stdio_js.odin +++ /dev/null @@ -1,60 +0,0 @@ -#+private -package odin_libc - -import "core:c" - -foreign import "odin_env" - -_fopen :: proc(path, mode: cstring) -> FILE { - unimplemented("vendor/libc: fopen in JS") -} - -_fseek :: proc(file: FILE, offset: c.long, whence: i32) -> i32 { - unimplemented("vendor/libc: fseek in JS") -} - -_ftell :: proc(file: FILE) -> c.long { - unimplemented("vendor/libc: ftell in JS") -} - -_fclose :: proc(file: FILE) -> i32 { - unimplemented("vendor/libc: fclose in JS") -} - -_fread :: proc(buffer: [^]byte, size: uint, count: uint, file: FILE) -> uint { - unimplemented("vendor/libc: fread in JS") -} - -_fwrite :: proc(buffer: [^]byte, size: uint, count: uint, file: FILE) -> uint { - fd, ok := __fd(file) - if !ok { - return 0 - } - - __write(fd, buffer[:size*count]) - return count -} - -_putchar :: proc(char: c.int) -> c.int { - __write(1, {byte(char)}) - return char -} - -_getchar :: proc() -> c.int { - return EOF -} - -@(private="file") -foreign odin_env { - @(link_name="write") - __write :: proc "contextless" (fd: u32, p: []byte) --- -} - -@(private="file") -__fd :: proc(file: FILE) -> (u32, bool) { - switch (uint(uintptr(file))) { - case 2: return 1, true // stdout - case 3: return 2, true // stderr - case: return 0, false - } -} diff --git a/vendor/libc/stdio_os.odin b/vendor/libc/stdio_os.odin deleted file mode 100644 index db40fb250..000000000 --- a/vendor/libc/stdio_os.odin +++ /dev/null @@ -1,104 +0,0 @@ -#+build !freestanding -#+build !js -package odin_libc - -import "core:io" -import "core:c" -import os "core:os/os2" - -_fopen :: proc(path, _mode: cstring) -> FILE { - flags: os.File_Flags - - mode := string(_mode) - if len(mode) > 1 { - switch mode[0] { - case 'r': - flags += {.Read} - case 'w': - flags += {.Write, .Create, .Trunc} - case 'a': - flags += {.Write, .Create, .Append} - case: - return nil - } - - if len(mode) > 1 && mode[1] == '+' { - flags += {.Write, .Read} - } else if len(mode) > 2 && mode[1] == 'b' && mode[2] == '+' { - flags += {.Write, .Read} - } - } - - file, err := os.open(string(path), flags, os.Permissions_Read_Write_All) - if err != nil { - return nil - } - - return FILE(file) -} - -_fseek :: proc(_file: FILE, offset: c.long, whence: i32) -> i32 { - file := __file(_file) - if _, err := os.seek(file, i64(offset), io.Seek_From(whence)); err != nil { - return -1 - } - - return 0 -} - -_ftell :: proc(_file: FILE) -> c.long { - file := __file(_file) - pos, err := os.seek(file, 0, .Current) - if err != nil { - return -1 - } - - return c.long(pos) -} - -_fclose :: proc(_file: FILE) -> i32 { - file := __file(_file) - if err := os.close(file); err != nil { - return EOF - } - - return 0 -} - -_fread :: proc(buffer: [^]byte, size: uint, count: uint, _file: FILE) -> uint { - file := __file(_file) - n, _ := os.read(file, buffer[:size*count]) - return uint(max(0, n)) / size -} - -_fwrite :: proc(buffer: [^]byte, size: uint, count: uint, _file: FILE) -> uint { - file := __file(_file) - n, _ := os.write(file, buffer[:size*count]) - return uint(max(0, n)) / size -} - -_putchar :: proc(char: c.int) -> c.int { - n, err := os.write_byte(os.stdout, byte(char)) - if n == 0 || err != nil { - return EOF - } - return char -} - -_getchar :: proc() -> c.int { - ret: [1]byte - n, err := os.read(os.stdin, ret[:]) - if n == 0 || err != nil { - return EOF - } - return c.int(ret[0]) -} - -@(private="file") -__file :: proc(file: FILE) -> ^os.File { - switch (uint(uintptr(file))) { - case 2: return os.stdout - case 3: return os.stderr - case: return (^os.File)(file) - } -} -- cgit v1.2.3