mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-05 15:18:49 +00:00
Clean up of the core library to make the stream vtables not be pointers directly.
This commit is contained in:
@@ -15,12 +15,12 @@ read_writer_init :: proc(rw: ^Read_Writer, r: ^Reader, w: ^Writer) {
|
|||||||
|
|
||||||
read_writer_to_stream :: proc(rw: ^Read_Writer) -> (s: io.Stream) {
|
read_writer_to_stream :: proc(rw: ^Read_Writer) -> (s: io.Stream) {
|
||||||
s.stream_data = rw
|
s.stream_data = rw
|
||||||
s.stream_vtable = _read_writer_vtable
|
s.stream_vtable = &_read_writer_vtable
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@(private)
|
@(private)
|
||||||
_read_writer_vtable := &io.Stream_VTable{
|
_read_writer_vtable := io.Stream_VTable{
|
||||||
impl_read = proc(s: io.Stream, p: []byte) -> (n: int, err: io.Error) {
|
impl_read = proc(s: io.Stream, p: []byte) -> (n: int, err: io.Error) {
|
||||||
b := (^Read_Writer)(s.stream_data).r
|
b := (^Read_Writer)(s.stream_data).r
|
||||||
return reader_read(b, p)
|
return reader_read(b, p)
|
||||||
|
|||||||
@@ -353,14 +353,14 @@ reader_write_to :: proc(b: ^Reader, w: io.Writer) -> (n: i64, err: io.Error) {
|
|||||||
// reader_to_stream converts a Reader into an io.Stream
|
// reader_to_stream converts a Reader into an io.Stream
|
||||||
reader_to_stream :: proc(b: ^Reader) -> (s: io.Stream) {
|
reader_to_stream :: proc(b: ^Reader) -> (s: io.Stream) {
|
||||||
s.stream_data = b
|
s.stream_data = b
|
||||||
s.stream_vtable = _reader_vtable
|
s.stream_vtable = &_reader_vtable
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@(private)
|
@(private)
|
||||||
_reader_vtable := &io.Stream_VTable{
|
_reader_vtable := io.Stream_VTable{
|
||||||
impl_destroy = proc(s: io.Stream) -> io.Error {
|
impl_destroy = proc(s: io.Stream) -> io.Error {
|
||||||
b := (^Reader)(s.stream_data)
|
b := (^Reader)(s.stream_data)
|
||||||
reader_destroy(b)
|
reader_destroy(b)
|
||||||
|
|||||||
@@ -223,14 +223,14 @@ writer_read_from :: proc(b: ^Writer, r: io.Reader) -> (n: i64, err: io.Error) {
|
|||||||
// writer_to_stream converts a Writer into an io.Stream
|
// writer_to_stream converts a Writer into an io.Stream
|
||||||
writer_to_stream :: proc(b: ^Writer) -> (s: io.Stream) {
|
writer_to_stream :: proc(b: ^Writer) -> (s: io.Stream) {
|
||||||
s.stream_data = b
|
s.stream_data = b
|
||||||
s.stream_vtable = _writer_vtable
|
s.stream_vtable = &_writer_vtable
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@(private)
|
@(private)
|
||||||
_writer_vtable := &io.Stream_VTable{
|
_writer_vtable := io.Stream_VTable{
|
||||||
impl_destroy = proc(s: io.Stream) -> io.Error {
|
impl_destroy = proc(s: io.Stream) -> io.Error {
|
||||||
b := (^Writer)(s.stream_data)
|
b := (^Writer)(s.stream_data)
|
||||||
writer_destroy(b)
|
writer_destroy(b)
|
||||||
|
|||||||
@@ -374,12 +374,12 @@ buffer_read_from :: proc(b: ^Buffer, r: io.Reader) -> (n: i64, err: io.Error) #n
|
|||||||
|
|
||||||
buffer_to_stream :: proc(b: ^Buffer) -> (s: io.Stream) {
|
buffer_to_stream :: proc(b: ^Buffer) -> (s: io.Stream) {
|
||||||
s.stream_data = b
|
s.stream_data = b
|
||||||
s.stream_vtable = _buffer_vtable
|
s.stream_vtable = &_buffer_vtable
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@(private)
|
@(private)
|
||||||
_buffer_vtable := &io.Stream_VTable{
|
_buffer_vtable := io.Stream_VTable{
|
||||||
impl_size = proc(s: io.Stream) -> i64 {
|
impl_size = proc(s: io.Stream) -> i64 {
|
||||||
b := (^Buffer)(s.stream_data)
|
b := (^Buffer)(s.stream_data)
|
||||||
return i64(buffer_capacity(b))
|
return i64(buffer_capacity(b))
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ reader_init :: proc(r: ^Reader, s: []byte) {
|
|||||||
|
|
||||||
reader_to_stream :: proc(r: ^Reader) -> (s: io.Stream) {
|
reader_to_stream :: proc(r: ^Reader) -> (s: io.Stream) {
|
||||||
s.stream_data = r
|
s.stream_data = r
|
||||||
s.stream_vtable = _reader_vtable
|
s.stream_vtable = &_reader_vtable
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -137,7 +137,7 @@ reader_write_to :: proc(r: ^Reader, w: io.Writer) -> (n: i64, err: io.Error) {
|
|||||||
|
|
||||||
|
|
||||||
@(private)
|
@(private)
|
||||||
_reader_vtable := &io.Stream_VTable{
|
_reader_vtable := io.Stream_VTable{
|
||||||
impl_size = proc(s: io.Stream) -> i64 {
|
impl_size = proc(s: io.Stream) -> i64 {
|
||||||
r := (^Reader)(s.stream_data)
|
r := (^Reader)(s.stream_data)
|
||||||
return reader_size(r)
|
return reader_size(r)
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ foreign odin_env {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@(private="file")
|
@(private="file")
|
||||||
write_vtable := &io.Stream_VTable{
|
write_vtable := io.Stream_VTable{
|
||||||
impl_write = proc(s: io.Stream, p: []byte) -> (n: int, err: io.Error) {
|
impl_write = proc(s: io.Stream, p: []byte) -> (n: int, err: io.Error) {
|
||||||
fd := u32(uintptr(s.stream_data))
|
fd := u32(uintptr(s.stream_data))
|
||||||
write(fd, p)
|
write(fd, p)
|
||||||
@@ -22,14 +22,14 @@ write_vtable := &io.Stream_VTable{
|
|||||||
@(private="file")
|
@(private="file")
|
||||||
stdout := io.Writer{
|
stdout := io.Writer{
|
||||||
stream = {
|
stream = {
|
||||||
stream_vtable = write_vtable,
|
stream_vtable = &write_vtable,
|
||||||
stream_data = rawptr(uintptr(1)),
|
stream_data = rawptr(uintptr(1)),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@(private="file")
|
@(private="file")
|
||||||
stderr := io.Writer{
|
stderr := io.Writer{
|
||||||
stream = {
|
stream = {
|
||||||
stream_vtable = write_vtable,
|
stream_vtable = &write_vtable,
|
||||||
stream_data = rawptr(uintptr(2)),
|
stream_data = rawptr(uintptr(2)),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import "core:io"
|
|||||||
|
|
||||||
to_stream :: proc(f: ^File) -> (s: io.Stream) {
|
to_stream :: proc(f: ^File) -> (s: io.Stream) {
|
||||||
s.stream_data = f
|
s.stream_data = f
|
||||||
s.stream_vtable = _file_stream_vtable
|
s.stream_vtable = &_file_stream_vtable
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,7 +26,7 @@ error_to_io_error :: proc(ferr: Error) -> io.Error {
|
|||||||
|
|
||||||
|
|
||||||
@(private)
|
@(private)
|
||||||
_file_stream_vtable := &io.Stream_VTable{
|
_file_stream_vtable := io.Stream_VTable{
|
||||||
impl_read = proc(s: io.Stream, p: []byte) -> (n: int, err: io.Error) {
|
impl_read = proc(s: io.Stream, p: []byte) -> (n: int, err: io.Error) {
|
||||||
f := (^File)(s.stream_data)
|
f := (^File)(s.stream_data)
|
||||||
ferr: Error
|
ferr: Error
|
||||||
|
|||||||
+2
-2
@@ -5,13 +5,13 @@ import "core:io"
|
|||||||
stream_from_handle :: proc(fd: Handle) -> io.Stream {
|
stream_from_handle :: proc(fd: Handle) -> io.Stream {
|
||||||
s: io.Stream
|
s: io.Stream
|
||||||
s.stream_data = rawptr(uintptr(fd))
|
s.stream_data = rawptr(uintptr(fd))
|
||||||
s.stream_vtable = _file_stream_vtable
|
s.stream_vtable = &_file_stream_vtable
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@(private)
|
@(private)
|
||||||
_file_stream_vtable := &io.Stream_VTable{
|
_file_stream_vtable := io.Stream_VTable{
|
||||||
impl_read = proc(s: io.Stream, p: []byte) -> (n: int, err: io.Error) {
|
impl_read = proc(s: io.Stream, p: []byte) -> (n: int, err: io.Error) {
|
||||||
fd := Handle(uintptr(s.stream_data))
|
fd := Handle(uintptr(s.stream_data))
|
||||||
os_err: Errno
|
os_err: Errno
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ builder_init :: proc{
|
|||||||
}
|
}
|
||||||
|
|
||||||
@(private)
|
@(private)
|
||||||
_builder_stream_vtable := &io.Stream_VTable{
|
_builder_stream_vtable := io.Stream_VTable{
|
||||||
impl_write = proc(s: io.Stream, p: []byte) -> (n: int, err: io.Error) {
|
impl_write = proc(s: io.Stream, p: []byte) -> (n: int, err: io.Error) {
|
||||||
b := (^Builder)(s.stream_data)
|
b := (^Builder)(s.stream_data)
|
||||||
n = write_bytes(b, p)
|
n = write_bytes(b, p)
|
||||||
@@ -97,7 +97,7 @@ _builder_stream_vtable := &io.Stream_VTable{
|
|||||||
|
|
||||||
// return an `io.Stream` from a builder
|
// return an `io.Stream` from a builder
|
||||||
to_stream :: proc(b: ^Builder) -> io.Stream {
|
to_stream :: proc(b: ^Builder) -> io.Stream {
|
||||||
return io.Stream{stream_vtable=_builder_stream_vtable, stream_data=b}
|
return io.Stream{stream_vtable=&_builder_stream_vtable, stream_data=b}
|
||||||
}
|
}
|
||||||
|
|
||||||
// return an `io.Writer` from a builder
|
// return an `io.Writer` from a builder
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ reader_init :: proc(r: ^Reader, s: string) {
|
|||||||
// returns a stream from the reader data
|
// returns a stream from the reader data
|
||||||
reader_to_stream :: proc(r: ^Reader) -> (s: io.Stream) {
|
reader_to_stream :: proc(r: ^Reader) -> (s: io.Stream) {
|
||||||
s.stream_data = r
|
s.stream_data = r
|
||||||
s.stream_vtable = _reader_vtable
|
s.stream_vtable = &_reader_vtable
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -177,7 +177,7 @@ reader_write_to :: proc(r: ^Reader, w: io.Writer) -> (n: i64, err: io.Error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@(private)
|
@(private)
|
||||||
_reader_vtable := &io.Stream_VTable{
|
_reader_vtable := io.Stream_VTable{
|
||||||
impl_size = proc(s: io.Stream) -> i64 {
|
impl_size = proc(s: io.Stream) -> i64 {
|
||||||
r := (^Reader)(s.stream_data)
|
r := (^Reader)(s.stream_data)
|
||||||
return reader_size(r)
|
return reader_size(r)
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
package darwin
|
package darwin
|
||||||
|
|
||||||
import "core:strings"
|
|
||||||
import "core:c"
|
import "core:c"
|
||||||
|
import "core:runtime"
|
||||||
|
|
||||||
// this package uses the sys prefix for the proc names to indicate that these aren't native syscalls but directly call such
|
// this package uses the sys prefix for the proc names to indicate that these aren't native syscalls but directly call such
|
||||||
sys_write_string :: proc (fd: c.int, message: string) -> bool {
|
sys_write_string :: proc (fd: c.int, message: string) -> bool {
|
||||||
return syscall_write(fd, strings.ptr_from_string(message), cast(u64)len(message))
|
return syscall_write(fd, raw_data(message), cast(u64)len(message))
|
||||||
}
|
}
|
||||||
|
|
||||||
Offset_From :: enum c.int {
|
Offset_From :: enum c.int {
|
||||||
@@ -87,11 +87,20 @@ _sys_permission_mode :: #force_inline proc (mode: Permission) -> u32 {
|
|||||||
return cflags
|
return cflags
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@(private)
|
||||||
|
clone_to_cstring :: proc(s: string, allocator: runtime.Allocator, loc := #caller_location) -> cstring {
|
||||||
|
c := make([]byte, len(s)+1, allocator, loc)
|
||||||
|
copy(c, s)
|
||||||
|
c[len(s)] = 0
|
||||||
|
return cstring(&c[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
sys_open :: proc(path: string, oflag: Open_Flags, mode: Permission) -> (c.int, bool) {
|
sys_open :: proc(path: string, oflag: Open_Flags, mode: Permission) -> (c.int, bool) {
|
||||||
|
|
||||||
cmode: u32 = 0
|
cmode: u32 = 0
|
||||||
cflags: u32 = 0
|
cflags: u32 = 0
|
||||||
cpath: cstring = strings.clone_to_cstring(path, context.temp_allocator)
|
cpath: cstring = clone_to_cstring(path, context.temp_allocator)
|
||||||
|
|
||||||
cflags = _sys_permission_mode(mode)
|
cflags = _sys_permission_mode(mode)
|
||||||
|
|
||||||
@@ -123,32 +132,32 @@ sys_open :: proc(path: string, oflag: Open_Flags, mode: Permission) -> (c.int, b
|
|||||||
}
|
}
|
||||||
|
|
||||||
sys_mkdir :: proc(path: string, mode: Permission) -> bool {
|
sys_mkdir :: proc(path: string, mode: Permission) -> bool {
|
||||||
cpath: cstring = strings.clone_to_cstring(path, context.temp_allocator)
|
cpath: cstring = clone_to_cstring(path, context.temp_allocator)
|
||||||
cflags := _sys_permission_mode(mode)
|
cflags := _sys_permission_mode(mode)
|
||||||
return syscall_mkdir(cpath, cflags) != -1
|
return syscall_mkdir(cpath, cflags) != -1
|
||||||
}
|
}
|
||||||
|
|
||||||
sys_mkdir_at :: proc(fd: c.int, path: string, mode: Permission) -> bool {
|
sys_mkdir_at :: proc(fd: c.int, path: string, mode: Permission) -> bool {
|
||||||
cpath: cstring = strings.clone_to_cstring(path, context.temp_allocator)
|
cpath: cstring = clone_to_cstring(path, context.temp_allocator)
|
||||||
cflags := _sys_permission_mode(mode)
|
cflags := _sys_permission_mode(mode)
|
||||||
return syscall_mkdir_at(fd, cpath, cflags) != -1
|
return syscall_mkdir_at(fd, cpath, cflags) != -1
|
||||||
}
|
}
|
||||||
|
|
||||||
sys_rmdir :: proc(path: string, mode: Permission) -> bool {
|
sys_rmdir :: proc(path: string, mode: Permission) -> bool {
|
||||||
cpath: cstring = strings.clone_to_cstring(path, context.temp_allocator)
|
cpath: cstring = clone_to_cstring(path, context.temp_allocator)
|
||||||
cflags := _sys_permission_mode(mode)
|
cflags := _sys_permission_mode(mode)
|
||||||
return syscall_rmdir(cpath, cflags) != -1
|
return syscall_rmdir(cpath, cflags) != -1
|
||||||
}
|
}
|
||||||
|
|
||||||
sys_rename :: proc(path: string, new_path: string) -> bool {
|
sys_rename :: proc(path: string, new_path: string) -> bool {
|
||||||
cpath: cstring = strings.clone_to_cstring(path, context.temp_allocator)
|
cpath: cstring = clone_to_cstring(path, context.temp_allocator)
|
||||||
cnpath: cstring = strings.clone_to_cstring(new_path, context.temp_allocator)
|
cnpath: cstring = clone_to_cstring(new_path, context.temp_allocator)
|
||||||
return syscall_rename(cpath, cnpath) != -1
|
return syscall_rename(cpath, cnpath) != -1
|
||||||
}
|
}
|
||||||
|
|
||||||
sys_rename_at :: proc(fd: c.int, path: string, to_fd: c.int, new_path: string) -> bool {
|
sys_rename_at :: proc(fd: c.int, path: string, to_fd: c.int, new_path: string) -> bool {
|
||||||
cpath: cstring = strings.clone_to_cstring(path, context.temp_allocator)
|
cpath: cstring = clone_to_cstring(path, context.temp_allocator)
|
||||||
cnpath: cstring = strings.clone_to_cstring(new_path, context.temp_allocator)
|
cnpath: cstring = clone_to_cstring(new_path, context.temp_allocator)
|
||||||
return syscall_rename_at(fd, cpath, to_fd, cnpath) != -1
|
return syscall_rename_at(fd, cpath, to_fd, cnpath) != -1
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -157,12 +166,12 @@ sys_lseek :: proc(fd: c.int, offset: i64, whence: Offset_From) -> i64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sys_chmod :: proc(path: string, mode: Permission) -> bool {
|
sys_chmod :: proc(path: string, mode: Permission) -> bool {
|
||||||
cpath: cstring = strings.clone_to_cstring(path, context.temp_allocator)
|
cpath: cstring = clone_to_cstring(path, context.temp_allocator)
|
||||||
cmode := _sys_permission_mode(mode)
|
cmode := _sys_permission_mode(mode)
|
||||||
return syscall_chmod(cpath, cmode) != -1
|
return syscall_chmod(cpath, cmode) != -1
|
||||||
}
|
}
|
||||||
|
|
||||||
sys_lstat :: proc(path: string, status: ^stat) -> bool {
|
sys_lstat :: proc(path: string, status: ^stat) -> bool {
|
||||||
cpath: cstring = strings.clone_to_cstring(path, context.temp_allocator)
|
cpath: cstring = clone_to_cstring(path, context.temp_allocator)
|
||||||
return syscall_lstat(cpath, status) != -1
|
return syscall_lstat(cpath, status) != -1
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -390,6 +390,9 @@ lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bool allow_loc
|
|||||||
Entity *e = entity_from_expr(expr);
|
Entity *e = entity_from_expr(expr);
|
||||||
res = lb_find_procedure_value_from_entity(m, e);
|
res = lb_find_procedure_value_from_entity(m, e);
|
||||||
}
|
}
|
||||||
|
GB_ASSERT(res.value != nullptr);
|
||||||
|
GB_ASSERT(LLVMGetValueKind(res.value) == LLVMFunctionValueKind);
|
||||||
|
|
||||||
res.value = LLVMConstPointerCast(res.value, lb_type(m, res.type));
|
res.value = LLVMConstPointerCast(res.value, lb_type(m, res.type));
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user