From: Brendan Hansen Date: Mon, 22 Mar 2021 02:44:03 +0000 (-0500) Subject: random little changes X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=b5ddcc731c6bcfd2637f46f3d649788315107f7f;p=onyx.git random little changes --- diff --git a/bin/onyx b/bin/onyx index 8aa4189d..cf48b2c3 100755 Binary files a/bin/onyx and b/bin/onyx differ diff --git a/core/env.onyx b/core/env.onyx new file mode 100644 index 00000000..7f91b55d --- /dev/null +++ b/core/env.onyx @@ -0,0 +1,56 @@ +package core.env + +use package build_opts as build_opts +#if build_opts.Runtime != build_opts.Runtime_Wasi { + #error "'core.env' is only available with the 'wasi' runtime."; +} + + +use package wasi { environ_get, environ_sizes_get, Size } +use package core.map as map +use package core.memory as memory +use package core.string as string + +Environment :: struct { + vars : map.Map(str, str); + + buffer : [] u8; + buffer_allocator : Allocator; +} + +get_env :: (allocator := context.allocator) -> Environment { + env_count, env_buf_size : Size; + environ_sizes_get(^env_count, ^env_buf_size); + + env_var := memory.make_slice(cstr, env_count, allocator=allocator); + env_buf := memory.make_slice(u8, env_buf_size, allocator=allocator); + + environ_get(env_var.data, env_buf.data); + + while i := cast(i32) (env_var.count - 1); i >= 0 { + defer i -= 1; + + env_var[i] = cast(cstr) (cast(^u32) env_var.data)[i]; + } + + env_map := map.make(str, str, ""); + for env: env_var { + s := string.make(env); + var := string.read_until(^s, #char "="); + map.put(^env_map, var, string.advance(s, 1)); + } + + raw_free(allocator, env_var.data); + + return .{ + vars = env_map, + buffer = env_buf, + buffer_allocator = allocator, + }; +} + +free_env :: (use env: ^Environment) { + map.free(^vars); + + raw_free(buffer_allocator, buffer.data); +} diff --git a/core/io/file.onyx b/core/io/file.onyx index 8d4f66d4..99c1759c 100644 --- a/core/io/file.onyx +++ b/core/io/file.onyx @@ -117,7 +117,7 @@ file_close :: (file: File) -> bool { file_write :: (file: File, data: str) { vec := IOVec.{ buf = cast(u32) data.data, len = data.count }; tmp : Size; - wasi.fd_write(file.fd, IOVecArray.{ cast(u32) ^vec, 1 }, ^tmp); + wasi.fd_write(file.fd, .{ cast(u32) ^vec, 1 }, ^tmp); wasi.fd_datasync(file.fd); } @@ -141,7 +141,7 @@ get_contents_from_file :: (file: File) -> str { dummy2: u32; buf := IOVec.{ cast(u32) data, size }; - wasi.fd_pread(file.fd, IOVecArray.{ cast(u32) ^buf, 1 }, 0, ^dummy2); + wasi.fd_pread(file.fd, .{ cast(u32) ^buf, 1 }, 0, ^dummy2); wasi.fd_seek(file.fd, prev_loc, Whence.Set, ^dummy); @@ -153,7 +153,7 @@ get_contents :: proc { proc (path: str) -> str { tmp_file, success := file_open(path, OpenMode.Read); - if !success do return str.{ null, 0 }; + if !success do return .{ null, 0 }; defer file_close(tmp_file); return get_contents(tmp_file); @@ -167,8 +167,8 @@ FileStream :: struct { open :: (path: str, mode := OpenMode.Read) -> (Error, FileStream) { fs := FileStream.{ - stream = Stream.{ vtable = null }, - file = File.{ fd = -1 }, + stream = .{ vtable = null }, + file = .{ fd = -1 }, }; file, success := file_open(path, mode); diff --git a/core/map.onyx b/core/map.onyx index 9c588706..cbdb47b9 100644 --- a/core/map.onyx +++ b/core/map.onyx @@ -111,10 +111,9 @@ empty :: (use map: ^Map($K, $V)) -> bool { hash_function :: proc { (key: rawptr) -> u32 { return 0xcbf29ce7 ^ cast(u32) key; }, - (key: i32) -> u32 { return 0xcbf29ce7 ^ cast(u32) key; }, - (key: i64) -> u32 { return cast(u32) (cast(u64) 0xcbf29ce7 ^ cast(u64) key); }, - - (key: str) -> u32 { + (key: i32) -> u32 { return 0xcbf29ce7 ^ cast(u32) key; }, + (key: i64) -> u32 { return cast(u32) (cast(u64) 0xcbf29ce7 ^ cast(u64) key); }, + (key: str) -> u32 { hash: u32 = 5381; for ch: key do hash += (hash << 5) + ~~ch; return hash; diff --git a/core/std.onyx b/core/std.onyx index ec6ad5c2..58428eb4 100644 --- a/core/std.onyx +++ b/core/std.onyx @@ -30,6 +30,7 @@ use package build_opts as build_opts #load "core/sys/wasi" #load "core/wasi" #load "core/io/file" + #load "core/env" } #if build_opts.Runtime == build_opts.Runtime_Js { diff --git a/docs/bugs b/docs/bugs index 94500beb..56a44438 100644 --- a/docs/bugs +++ b/docs/bugs @@ -36,6 +36,24 @@ List of known bugs: SN.foo() } +[ ] There is a segfault when passing an anonymous struct literal to an untyped vararg. + + vararg_proc :: (va: ...) { + ... + } + + vararg_proc(.{ 1, 2 }); + +[ ] This currently does not work but I think it should: + + baked_proc :: (x: $T, $func: (T) -> T) -> T { + return func(x); + } + + The problem is that 'T' is an unresolved symbol because the context of the function type + does not have the solved value of T in it. + + List of things to change: [X] Currently, there is no way to use the initialized members of a structure without using a struct literal. There should be a initialize intrinsic procedure that takes a pointer to anything and initializes it. diff --git a/src/onyxastnodes.c b/src/onyxastnodes.c index 52c7ecc7..fb654767 100644 --- a/src/onyxastnodes.c +++ b/src/onyxastnodes.c @@ -429,6 +429,7 @@ b32 type_check_or_auto_cast(AstTyped** pnode, Type* type) { if (node_is_type((AstNode *) node)) return 0; if (node->kind == Ast_Kind_Struct_Literal && node->type_node == NULL) { + if (type->kind == Type_Kind_VarArgs) type = type->VarArgs.ptr_to_data->Pointer.elem; node->type = type; add_entities_for_node(NULL, (AstNode *) node, NULL, NULL); diff --git a/src/onyxparser.c b/src/onyxparser.c index 7e53af02..ae264191 100644 --- a/src/onyxparser.c +++ b/src/onyxparser.c @@ -2389,6 +2389,8 @@ static AstPackage* parse_package_name(OnyxParser* parser) { token_toggle_end(symbol); symbol_subpackage_introduce(package->scope, symbol->text, pnode); token_toggle_end(symbol); + + package_reinsert_use_packages(package); } package = newpackage; diff --git a/src/onyxsymres.c b/src/onyxsymres.c index 43f7a2db..efb5ec6e 100644 --- a/src/onyxsymres.c +++ b/src/onyxsymres.c @@ -1018,6 +1018,7 @@ static SymresStatus symres_struct_defaults(AstType* t) { } static SymresStatus symres_polyproc(AstPolyProc* pp) { + pp->flags |= Ast_Flag_Comptime; pp->poly_scope = curr_scope; bh_arr_each(AstPolyParam, param, pp->poly_params) {