Clean up of the core library to make the stream vtables not be pointers directly.

This commit is contained in:
gingerBill
2022-09-15 10:00:50 +01:00
parent 1e595f2e26
commit f50fc33749
12 changed files with 45 additions and 33 deletions

View File

@@ -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) {
s.stream_data = rw
s.stream_vtable = _read_writer_vtable
s.stream_vtable = &_read_writer_vtable
return
}
@(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) {
b := (^Read_Writer)(s.stream_data).r
return reader_read(b, p)

View File

@@ -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 :: proc(b: ^Reader) -> (s: io.Stream) {
s.stream_data = b
s.stream_vtable = _reader_vtable
s.stream_vtable = &_reader_vtable
return
}
@(private)
_reader_vtable := &io.Stream_VTable{
_reader_vtable := io.Stream_VTable{
impl_destroy = proc(s: io.Stream) -> io.Error {
b := (^Reader)(s.stream_data)
reader_destroy(b)

View File

@@ -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 :: proc(b: ^Writer) -> (s: io.Stream) {
s.stream_data = b
s.stream_vtable = _writer_vtable
s.stream_vtable = &_writer_vtable
return
}
@(private)
_writer_vtable := &io.Stream_VTable{
_writer_vtable := io.Stream_VTable{
impl_destroy = proc(s: io.Stream) -> io.Error {
b := (^Writer)(s.stream_data)
writer_destroy(b)

View File

@@ -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) {
s.stream_data = b
s.stream_vtable = _buffer_vtable
s.stream_vtable = &_buffer_vtable
return
}
@(private)
_buffer_vtable := &io.Stream_VTable{
_buffer_vtable := io.Stream_VTable{
impl_size = proc(s: io.Stream) -> i64 {
b := (^Buffer)(s.stream_data)
return i64(buffer_capacity(b))

View File

@@ -17,7 +17,7 @@ reader_init :: proc(r: ^Reader, s: []byte) {
reader_to_stream :: proc(r: ^Reader) -> (s: io.Stream) {
s.stream_data = r
s.stream_vtable = _reader_vtable
s.stream_vtable = &_reader_vtable
return
}
@@ -137,7 +137,7 @@ reader_write_to :: proc(r: ^Reader, w: io.Writer) -> (n: i64, err: io.Error) {
@(private)
_reader_vtable := &io.Stream_VTable{
_reader_vtable := io.Stream_VTable{
impl_size = proc(s: io.Stream) -> i64 {
r := (^Reader)(s.stream_data)
return reader_size(r)

View File

@@ -11,7 +11,7 @@ foreign odin_env {
}
@(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) {
fd := u32(uintptr(s.stream_data))
write(fd, p)
@@ -22,14 +22,14 @@ write_vtable := &io.Stream_VTable{
@(private="file")
stdout := io.Writer{
stream = {
stream_vtable = write_vtable,
stream_vtable = &write_vtable,
stream_data = rawptr(uintptr(1)),
},
}
@(private="file")
stderr := io.Writer{
stream = {
stream_vtable = write_vtable,
stream_vtable = &write_vtable,
stream_data = rawptr(uintptr(2)),
},
}

View File

@@ -4,7 +4,7 @@ import "core:io"
to_stream :: proc(f: ^File) -> (s: io.Stream) {
s.stream_data = f
s.stream_vtable = _file_stream_vtable
s.stream_vtable = &_file_stream_vtable
return
}
@@ -26,7 +26,7 @@ error_to_io_error :: proc(ferr: Error) -> io.Error {
@(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) {
f := (^File)(s.stream_data)
ferr: Error

View File

@@ -5,13 +5,13 @@ import "core:io"
stream_from_handle :: proc(fd: Handle) -> io.Stream {
s: io.Stream
s.stream_data = rawptr(uintptr(fd))
s.stream_vtable = _file_stream_vtable
s.stream_vtable = &_file_stream_vtable
return s
}
@(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) {
fd := Handle(uintptr(s.stream_data))
os_err: Errno

View File

@@ -67,7 +67,7 @@ builder_init :: proc{
}
@(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) {
b := (^Builder)(s.stream_data)
n = write_bytes(b, p)
@@ -97,7 +97,7 @@ _builder_stream_vtable := &io.Stream_VTable{
// return an `io.Stream` from a builder
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

View File

@@ -24,7 +24,7 @@ reader_init :: proc(r: ^Reader, s: string) {
// returns a stream from the reader data
reader_to_stream :: proc(r: ^Reader) -> (s: io.Stream) {
s.stream_data = r
s.stream_vtable = _reader_vtable
s.stream_vtable = &_reader_vtable
return
}
@@ -177,7 +177,7 @@ reader_write_to :: proc(r: ^Reader, w: io.Writer) -> (n: i64, err: io.Error) {
}
@(private)
_reader_vtable := &io.Stream_VTable{
_reader_vtable := io.Stream_VTable{
impl_size = proc(s: io.Stream) -> i64 {
r := (^Reader)(s.stream_data)
return reader_size(r)

View File

@@ -1,11 +1,11 @@
package darwin
import "core:strings"
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
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 {
@@ -87,11 +87,20 @@ _sys_permission_mode :: #force_inline proc (mode: Permission) -> u32 {
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) {
cmode: 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)
@@ -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 {
cpath: cstring = strings.clone_to_cstring(path, context.temp_allocator)
cpath: cstring = clone_to_cstring(path, context.temp_allocator)
cflags := _sys_permission_mode(mode)
return syscall_mkdir(cpath, cflags) != -1
}
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)
return syscall_mkdir_at(fd, cpath, cflags) != -1
}
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)
return syscall_rmdir(cpath, cflags) != -1
}
sys_rename :: proc(path: string, new_path: string) -> bool {
cpath: cstring = strings.clone_to_cstring(path, context.temp_allocator)
cnpath: cstring = strings.clone_to_cstring(new_path, context.temp_allocator)
cpath: cstring = clone_to_cstring(path, context.temp_allocator)
cnpath: cstring = clone_to_cstring(new_path, context.temp_allocator)
return syscall_rename(cpath, cnpath) != -1
}
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)
cnpath: cstring = strings.clone_to_cstring(new_path, context.temp_allocator)
cpath: cstring = clone_to_cstring(path, context.temp_allocator)
cnpath: cstring = clone_to_cstring(new_path, context.temp_allocator)
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 {
cpath: cstring = strings.clone_to_cstring(path, context.temp_allocator)
cpath: cstring = clone_to_cstring(path, context.temp_allocator)
cmode := _sys_permission_mode(mode)
return syscall_chmod(cpath, cmode) != -1
}
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
}

View File

@@ -390,6 +390,9 @@ lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bool allow_loc
Entity *e = entity_from_expr(expr);
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));
return res;
}