From: Brendan Hansen Date: Tue, 9 Feb 2021 21:34:18 +0000 (-0600) Subject: added runtime configuration at compile-time; removed redundant files X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=15079463cc1aefb87d395cbf12a388e9887c02d9;p=onyx.git added runtime configuration at compile-time; removed redundant files --- diff --git a/bin/onyx b/bin/onyx index 9ef4ddb8..1406205b 100755 Binary files a/bin/onyx and b/bin/onyx differ diff --git a/bin/test b/bin/test index 59bef915..98aae825 100755 --- a/bin/test +++ b/bin/test @@ -16,7 +16,7 @@ for test_file in $(find tests/ -name '*.onyx'); do print_check "$name" - if ! ./bin/onyx --use-post-mvp-features "$test_file" -o "./tests/$name.wasm" >/dev/null; then + if ! ./bin/onyx -r js --use-post-mvp-features "$test_file" -o "./tests/$name.wasm" >/dev/null; then print "\n❌ Failed to compile $name.onyx.\n" failed=1 continue diff --git a/core/file.onyx b/core/file.onyx deleted file mode 100644 index 7515dc95..00000000 --- a/core/file.onyx +++ /dev/null @@ -1,276 +0,0 @@ -package core.io - -use package core -use package wasi as wasi -use package wasi { - FileDescriptor, - FDFlags, OFlags, Rights, - LookupFlags, Errno, - IOVec, IOVecArray, Size, - FileStat, Whence -} - -OpenMode :: enum { - Invalid; - Read; - Write; - Append; -} - -File :: struct { - fd : FileDescriptor; - - mode : OpenMode = OpenMode.Invalid; - rights : Rights = ~~ 0; - flags : FDFlags = ~~ 0; -} - -file_open :: (path: str, mode := OpenMode.Read, flags := FDFlags.Sync) -> (File, bool) { - // Currently the directory's file descriptor appears to always be 3 - DIR_FD :: 3; - - // Requesting all of the rights because why not. - rights := - Rights.DataSync - | Rights.Read - | Rights.Seek - | Rights.FdStatSetFlags - | Rights.Sync - | Rights.Tell - | Rights.Write - | Rights.Advise - | Rights.Allocate - | Rights.PathCreateDirectory - | Rights.PathCreateFile - | Rights.PathLinkSource - | Rights.PathLinkTarget - | Rights.PathOpen - | Rights.ReadDir - | Rights.PathReadlink - | Rights.PathRenameSource - | Rights.PathRenameTarget - | Rights.PathFilestatGet - | Rights.PathFilestateSetSize - | Rights.PathFilestateSetTimes - | Rights.FilestatGet - | Rights.FilestatSetSize - | Rights.FilestatSetTimes - | Rights.PathSymlink - | Rights.PathRemoveDirectory - | Rights.PathUnlinkFile - | Rights.PollFDReadWrite; - - open_flags := cast(OFlags) 0; - fd_flags := flags; - - switch mode { - case OpenMode.Write { - open_flags |= OFlags.Creat | OFlags.Trunc; - rights |= Rights.Write; - } - - case OpenMode.Append { - open_flags |= OFlags.Creat; - rights |= Rights.Write; - fd_flags |= FDFlags.Append; - } - - case OpenMode.Read { - rights |= Rights.Read | Rights.Seek | Rights.Tell; - } - } - - file := File.{ fd = -1 }; - - if err := wasi.path_open( - DIR_FD, - LookupFlags.SymLinkFollow, - path, - open_flags, - rights, - rights, - fd_flags, - ^file.fd); - err != Errno.Success { - return file, false; - } - - file.mode = mode; - file.rights = rights; - file.flags = fd_flags; - return file, true; -} - -file_close :: (file: File) -> bool { - if wasi.fd_close(file.fd) != Errno.Success { - return false; - } - - return true; -} - -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_datasync(file.fd); -} - -get_size :: (file: File) -> u64 { - fs: FileStat; - if wasi.fd_filestat_get(file.fd, ^fs) != Errno.Success do return 0; - - return fs.size; -} - -get_contents_from_file :: (file: File) -> str { - size := cast(u32) get_size(file); - - data := cast(^u8) raw_alloc(context.allocator, size); - - prev_loc: i64; - wasi.fd_tell(file.fd, ^prev_loc); - - dummy: i64; - wasi.fd_seek(file.fd, 0, Whence.Set, ^dummy); - - dummy2: u32; - buf := IOVec.{ cast(u32) data, size }; - wasi.fd_pread(file.fd, IOVecArray.{ cast(u32) ^buf, 1 }, 0, ^dummy2); - - wasi.fd_seek(file.fd, prev_loc, Whence.Set, ^dummy); - - return data[0 .. size]; -} - -get_contents :: proc { - get_contents_from_file, - - proc (path: str) -> str { - tmp_file, success := file_open(path, OpenMode.Read); - if !success do return str.{ null, 0 }; - defer file_close(tmp_file); - - return get_contents(tmp_file); - } -} - -FileStream :: struct { - use stream : Stream; - use file : File; -} - -open :: (path: str, mode := OpenMode.Read) -> (Error, FileStream) { - fs := FileStream.{ - stream = Stream.{ vtable = null }, - file = File.{ fd = -1 }, - }; - - file, success := file_open(path, mode); - if !success do return Error.NotFound, fs; - - fs.file = file; - fs.vtable = ^file_stream_vtable; - return Error.None, fs; -} - -#private -file_stream_vtable := Stream_Vtable.{ - seek = (use fs: ^FileStream, to: i32, whence: SeekFrom) -> Error { - // Currently, the new offset is just ignored. - newoffset : wasi.Filesize; - error := wasi.fd_seek(file.fd, ~~ to, ~~ whence, ^newoffset); - if error != Errno.Success do return Error.BadFile; - - return Error.None; - }, - - tell = (use fs: ^FileStream) -> (Error, u32) { - location : wasi.Filesize; - error := wasi.fd_tell(file.fd, ^location); - if error != Errno.Success do return Error.BadFile, 0; - - return Error.None, ~~location; - }, - - read = (use fs: ^FileStream, buffer: [] u8) -> (Error, u32) { - bytes_read : wasi.Size; - vec := IOVec.{ buf = cast(u32) buffer.data, len = buffer.count }; - error := wasi.fd_read(file.fd, IOVecArray.{ cast(u32) ^vec, 1 }, ^bytes_read); - if error != Errno.Success do return Error.BadFile, 0; - - return Error.None, bytes_read; - }, - - read_at = (use fs: ^FileStream, at: u32, buffer: [] u8) -> (Error, u32) { - bytes_read : wasi.Size; - vec := IOVec.{ buf = cast(u32) buffer.data, len = buffer.count }; - error := wasi.fd_pread(file.fd, IOVecArray.{ cast(u32) ^vec, 1 }, ~~at, ^bytes_read); - - // FIX: Maybe report Error.OutOfBounds if the 'at' was out of bounds? - if error != Errno.Success do return Error.BadFile, 0; - - return Error.None, bytes_read; - }, - - read_byte = (use fs: ^FileStream) -> (Error, u8) { - bytes_read : wasi.Size; - byte : u8; - vec := IOVec.{ buf = cast(u32) ^byte, len = 1}; - error := wasi.fd_read(file.fd, IOVecArray.{ cast(u32) ^vec, 1 }, ^bytes_read); - if error != Errno.Success do return Error.BadFile, 0; - - return Error.None, byte; - }, - - unread_byte = (use fs: ^FileStream) -> Error { - return Error.NotImplemented; - }, - - write = (use fs: ^FileStream, buffer: [] u8) -> (Error, u32) { - bytes_written : wasi.Size; - vec := IOVec.{ buf = cast(u32) buffer.data, len = buffer.count }; - error := wasi.fd_write(file.fd, IOVecArray.{ cast(u32) ^vec, 1 }, ^bytes_written); - if error != Errno.Success do return Error.BadFile, 0; - - return Error.None, bytes_written; - }, - - write_at = (use fs: ^FileStream, at: u32, buffer: [] u8) -> (Error, u32) { - bytes_written : wasi.Size; - vec := IOVec.{ buf = cast(u32) buffer.data, len = buffer.count }; - error := wasi.fd_pwrite(file.fd, IOVecArray.{ cast(u32) ^vec, 1 }, ~~at, ^bytes_written); - - // FIX: Maybe report Error.OutOfBounds if the 'at' was out of bounds? - if error != Errno.Success do return Error.BadFile, 0; - - return Error.None, bytes_written; - }, - - write_byte = (use fs: ^FileStream, byte: u8) -> Error { - bytes_written : wasi.Size; - byte_to_write := byte; - vec := IOVec.{ buf = cast(u32) ^byte_to_write, len = 1 }; - error := wasi.fd_write(file.fd, IOVecArray.{ cast(u32) ^vec, 1 }, ^bytes_written); - if error != Errno.Success do return Error.BadFile; - - return Error.None; - }, - - close = (use fs: ^FileStream) -> Error { - file_close(file); - return Error.None; - }, - - flush = (use fs: ^FileStream) -> Error { - wasi.fd_datasync(file.fd); - return Error.None; - }, - - size = (use fs: ^FileStream) -> i32 { - file_stat: FileStat; - if wasi.fd_filestat_get(file.fd, ^file_stat) != Errno.Success do return 0; - - return ~~ file_stat.size; - }, -} diff --git a/core/io/file.onyx b/core/io/file.onyx new file mode 100644 index 00000000..3451d8f8 --- /dev/null +++ b/core/io/file.onyx @@ -0,0 +1,281 @@ +package core.io + +use package build_opts as build_opts +#if build_opts.Runtime != build_opts.Runtime_Wasi { + #error "The file system library is currently only available on the WASI runtime, and should only be included if that is the chosen runtime." +} + +use package core +use package wasi as wasi +use package wasi { + FileDescriptor, + FDFlags, OFlags, Rights, + LookupFlags, Errno, + IOVec, IOVecArray, Size, + FileStat, Whence +} + +OpenMode :: enum { + Invalid; + Read; + Write; + Append; +} + +File :: struct { + fd : FileDescriptor; + + mode : OpenMode = OpenMode.Invalid; + rights : Rights = ~~ 0; + flags : FDFlags = ~~ 0; +} + +file_open :: (path: str, mode := OpenMode.Read, flags := FDFlags.Sync) -> (File, bool) { + // Currently the directory's file descriptor appears to always be 3 + DIR_FD :: 3; + + // Requesting all of the rights because why not. + rights := + Rights.DataSync + | Rights.Read + | Rights.Seek + | Rights.FdStatSetFlags + | Rights.Sync + | Rights.Tell + | Rights.Write + | Rights.Advise + | Rights.Allocate + | Rights.PathCreateDirectory + | Rights.PathCreateFile + | Rights.PathLinkSource + | Rights.PathLinkTarget + | Rights.PathOpen + | Rights.ReadDir + | Rights.PathReadlink + | Rights.PathRenameSource + | Rights.PathRenameTarget + | Rights.PathFilestatGet + | Rights.PathFilestateSetSize + | Rights.PathFilestateSetTimes + | Rights.FilestatGet + | Rights.FilestatSetSize + | Rights.FilestatSetTimes + | Rights.PathSymlink + | Rights.PathRemoveDirectory + | Rights.PathUnlinkFile + | Rights.PollFDReadWrite; + + open_flags := cast(OFlags) 0; + fd_flags := flags; + + switch mode { + case OpenMode.Write { + open_flags |= OFlags.Creat | OFlags.Trunc; + rights |= Rights.Write; + } + + case OpenMode.Append { + open_flags |= OFlags.Creat; + rights |= Rights.Write; + fd_flags |= FDFlags.Append; + } + + case OpenMode.Read { + rights |= Rights.Read | Rights.Seek | Rights.Tell; + } + } + + file := File.{ fd = -1 }; + + if err := wasi.path_open( + DIR_FD, + LookupFlags.SymLinkFollow, + path, + open_flags, + rights, + rights, + fd_flags, + ^file.fd); + err != Errno.Success { + return file, false; + } + + file.mode = mode; + file.rights = rights; + file.flags = fd_flags; + return file, true; +} + +file_close :: (file: File) -> bool { + if wasi.fd_close(file.fd) != Errno.Success { + return false; + } + + return true; +} + +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_datasync(file.fd); +} + +get_size :: (file: File) -> u64 { + fs: FileStat; + if wasi.fd_filestat_get(file.fd, ^fs) != Errno.Success do return 0; + + return fs.size; +} + +get_contents_from_file :: (file: File) -> str { + size := cast(u32) get_size(file); + + data := cast(^u8) raw_alloc(context.allocator, size); + + prev_loc: i64; + wasi.fd_tell(file.fd, ^prev_loc); + + dummy: i64; + wasi.fd_seek(file.fd, 0, Whence.Set, ^dummy); + + dummy2: u32; + buf := IOVec.{ cast(u32) data, size }; + wasi.fd_pread(file.fd, IOVecArray.{ cast(u32) ^buf, 1 }, 0, ^dummy2); + + wasi.fd_seek(file.fd, prev_loc, Whence.Set, ^dummy); + + return data[0 .. size]; +} + +get_contents :: proc { + get_contents_from_file, + + proc (path: str) -> str { + tmp_file, success := file_open(path, OpenMode.Read); + if !success do return str.{ null, 0 }; + defer file_close(tmp_file); + + return get_contents(tmp_file); + } +} + +FileStream :: struct { + use stream : Stream; + use file : File; +} + +open :: (path: str, mode := OpenMode.Read) -> (Error, FileStream) { + fs := FileStream.{ + stream = Stream.{ vtable = null }, + file = File.{ fd = -1 }, + }; + + file, success := file_open(path, mode); + if !success do return Error.NotFound, fs; + + fs.file = file; + fs.vtable = ^file_stream_vtable; + return Error.None, fs; +} + +#private +file_stream_vtable := Stream_Vtable.{ + seek = (use fs: ^FileStream, to: i32, whence: SeekFrom) -> Error { + // Currently, the new offset is just ignored. + newoffset : wasi.Filesize; + error := wasi.fd_seek(file.fd, ~~ to, ~~ whence, ^newoffset); + if error != Errno.Success do return Error.BadFile; + + return Error.None; + }, + + tell = (use fs: ^FileStream) -> (Error, u32) { + location : wasi.Filesize; + error := wasi.fd_tell(file.fd, ^location); + if error != Errno.Success do return Error.BadFile, 0; + + return Error.None, ~~location; + }, + + read = (use fs: ^FileStream, buffer: [] u8) -> (Error, u32) { + bytes_read : wasi.Size; + vec := IOVec.{ buf = cast(u32) buffer.data, len = buffer.count }; + error := wasi.fd_read(file.fd, IOVecArray.{ cast(u32) ^vec, 1 }, ^bytes_read); + if error != Errno.Success do return Error.BadFile, 0; + + return Error.None, bytes_read; + }, + + read_at = (use fs: ^FileStream, at: u32, buffer: [] u8) -> (Error, u32) { + bytes_read : wasi.Size; + vec := IOVec.{ buf = cast(u32) buffer.data, len = buffer.count }; + error := wasi.fd_pread(file.fd, IOVecArray.{ cast(u32) ^vec, 1 }, ~~at, ^bytes_read); + + // FIX: Maybe report Error.OutOfBounds if the 'at' was out of bounds? + if error != Errno.Success do return Error.BadFile, 0; + + return Error.None, bytes_read; + }, + + read_byte = (use fs: ^FileStream) -> (Error, u8) { + bytes_read : wasi.Size; + byte : u8; + vec := IOVec.{ buf = cast(u32) ^byte, len = 1}; + error := wasi.fd_read(file.fd, IOVecArray.{ cast(u32) ^vec, 1 }, ^bytes_read); + if error != Errno.Success do return Error.BadFile, 0; + + return Error.None, byte; + }, + + unread_byte = (use fs: ^FileStream) -> Error { + return Error.NotImplemented; + }, + + write = (use fs: ^FileStream, buffer: [] u8) -> (Error, u32) { + bytes_written : wasi.Size; + vec := IOVec.{ buf = cast(u32) buffer.data, len = buffer.count }; + error := wasi.fd_write(file.fd, IOVecArray.{ cast(u32) ^vec, 1 }, ^bytes_written); + if error != Errno.Success do return Error.BadFile, 0; + + return Error.None, bytes_written; + }, + + write_at = (use fs: ^FileStream, at: u32, buffer: [] u8) -> (Error, u32) { + bytes_written : wasi.Size; + vec := IOVec.{ buf = cast(u32) buffer.data, len = buffer.count }; + error := wasi.fd_pwrite(file.fd, IOVecArray.{ cast(u32) ^vec, 1 }, ~~at, ^bytes_written); + + // FIX: Maybe report Error.OutOfBounds if the 'at' was out of bounds? + if error != Errno.Success do return Error.BadFile, 0; + + return Error.None, bytes_written; + }, + + write_byte = (use fs: ^FileStream, byte: u8) -> Error { + bytes_written : wasi.Size; + byte_to_write := byte; + vec := IOVec.{ buf = cast(u32) ^byte_to_write, len = 1 }; + error := wasi.fd_write(file.fd, IOVecArray.{ cast(u32) ^vec, 1 }, ^bytes_written); + if error != Errno.Success do return Error.BadFile; + + return Error.None; + }, + + close = (use fs: ^FileStream) -> Error { + file_close(file); + return Error.None; + }, + + flush = (use fs: ^FileStream) -> Error { + wasi.fd_datasync(file.fd); + return Error.None; + }, + + size = (use fs: ^FileStream) -> i32 { + file_stat: FileStat; + if wasi.fd_filestat_get(file.fd, ^file_stat) != Errno.Success do return 0; + + return ~~ file_stat.size; + }, +} diff --git a/core/std.onyx b/core/std.onyx new file mode 100644 index 00000000..ec6ad5c2 --- /dev/null +++ b/core/std.onyx @@ -0,0 +1,41 @@ +package core + + +#load "core/build_opts" + +#load "core/alloc" +#load "core/memory" + +#load "core/array" +#load "core/map" + +#load "core/conv" +#load "core/math" +#load "core/random" + +#load "core/string" +#load "core/string/builder" +#load "core/string/reader" + +#load "core/intrinsics/wasm" + +#load "core/io/io" +#load "core/io/stream" +#load "core/io/reader" +#load "core/io/writer" +#load "core/io/binary" + +use package build_opts as build_opts +#if build_opts.Runtime == build_opts.Runtime_Wasi { + #load "core/sys/wasi" + #load "core/wasi" + #load "core/io/file" +} + +#if build_opts.Runtime == build_opts.Runtime_Js { + #load "core/sys/js" +} + +#if build_opts.Runtime != build_opts.Runtime_Custom { + #load "core/stdio" +} diff --git a/core/std/js.onyx b/core/std/js.onyx deleted file mode 100644 index 71ef69e5..00000000 --- a/core/std/js.onyx +++ /dev/null @@ -1,25 +0,0 @@ -package core - - - -#load "core/alloc" -#load "core/array" -#load "core/conv" -#load "core/intrinsics/wasm" -#load "core/map" -#load "core/math" -#load "core/memory" -#load "core/random" -#load "core/stdio" -#load "core/string" -#load "core/string/builder" -#load "core/string/reader" - -#load "core/io/io" -#load "core/io/stream" -#load "core/io/reader" -#load "core/io/writer" - -#load "core/sys/js" - - diff --git a/core/std/wasi.onyx b/core/std/wasi.onyx deleted file mode 100644 index 5b0db8ba..00000000 --- a/core/std/wasi.onyx +++ /dev/null @@ -1,28 +0,0 @@ -package core - - - -#load "core/alloc" -#load "core/array" -#load "core/conv" -#load "core/file" -#load "core/intrinsics/wasm" -#load "core/map" -#load "core/math" -#load "core/memory" -#load "core/random" -#load "core/stdio" -#load "core/string" -#load "core/string/builder" -#load "core/string/reader" -#load "core/wasi" - -#load "core/io/io" -#load "core/io/stream" -#load "core/io/reader" -#load "core/io/writer" -#load "core/io/binary" - -#load "core/sys/wasi" - - diff --git a/core/stdio.onyx b/core/stdio.onyx index 39f0c431..47ac620b 100644 --- a/core/stdio.onyx +++ b/core/stdio.onyx @@ -9,7 +9,13 @@ package core // It is expected that a file will be included that will be part // of the system package -use package system as system + +use package build_opts as build_opts +#if build_opts.Runtime != build_opts.Runtime_Custom { + use package system as system +} else { + #error "'stdio' can only be included in the 'wasi' or 'js' runtime." +} #private_file print_stream : io.DynamicStringStream; #private_file print_writer : io.Writer; diff --git a/examples/01_hello_world.onyx b/examples/01_hello_world.onyx index accfe9dd..44691db8 100644 --- a/examples/01_hello_world.onyx +++ b/examples/01_hello_world.onyx @@ -15,7 +15,7 @@ package main // added to the queue of files to load. When all files in the queue have been parsed, // the compiler can continue with the compilation process. You can also include the // same file as many times as you want. The redudant copies will be discarded. -#load "core/std/wasi" +#load "core/std" // All of the functionality we need is in the 'core' package. Unlike other package systems, // there is no way to reference symbols in a package without specifically 'using' it. This diff --git a/examples/02_variables.onyx b/examples/02_variables.onyx index e0a83a75..98d2606a 100644 --- a/examples/02_variables.onyx +++ b/examples/02_variables.onyx @@ -1,7 +1,7 @@ // This time, we are not adding, 'package main' to the top of the file, since // every file is automatically part of the main package unless specified otherwise. -#load "core/std/wasi" +#load "core/std" use package core diff --git a/examples/03_basics.onyx b/examples/03_basics.onyx index fa45ac56..c3ca6b57 100644 --- a/examples/03_basics.onyx +++ b/examples/03_basics.onyx @@ -1,6 +1,6 @@ // Now, lets go over the basic types and control flow mechanisms in Onyx. -#load "core/std/wasi" +#load "core/std" use package core diff --git a/examples/04_fixed_arrays.onyx b/examples/04_fixed_arrays.onyx index 91bcace7..6f005bf1 100644 --- a/examples/04_fixed_arrays.onyx +++ b/examples/04_fixed_arrays.onyx @@ -15,7 +15,7 @@ // This file will give examples of all of these things, as well as some of the gotchas // you need to be aware of. -#load "core/std/wasi" +#load "core/std" use package core diff --git a/examples/05_slices.onyx b/examples/05_slices.onyx index ac9ef020..bc2649cd 100644 --- a/examples/05_slices.onyx +++ b/examples/05_slices.onyx @@ -5,7 +5,7 @@ // is a powerful construct to have. In fact, strings in Onyx, i.e. // the 'str' type, is actually just a slice of u8. -#load "core/std/wasi" +#load "core/std" use package core diff --git a/examples/06_dynamic_arrays.onyx b/examples/06_dynamic_arrays.onyx index 16b95386..d41c7609 100644 --- a/examples/06_dynamic_arrays.onyx +++ b/examples/06_dynamic_arrays.onyx @@ -7,7 +7,7 @@ // Dynamic arrays in Onyx are easy to use on purpose, because I // know how useful they are in almost every program I write. -#load "core/std/wasi" +#load "core/std" use package core diff --git a/examples/07_structs.onyx b/examples/07_structs.onyx index 9e9d59a6..03d56bd1 100644 --- a/examples/07_structs.onyx +++ b/examples/07_structs.onyx @@ -5,7 +5,7 @@ // 'structs' in Onyx are very similar to structs in C and C++, with a couple // of additional capabilities to make using them even easier. -#load "core/std/wasi" +#load "core/std" use package core diff --git a/examples/08_enums.onyx b/examples/08_enums.onyx index fcee0f50..7d0044b8 100644 --- a/examples/08_enums.onyx +++ b/examples/08_enums.onyx @@ -1,7 +1,7 @@ // Enumerations (or enums for short) in Onyx work very similar to how they do // in C and C++. -#load "core/std/wasi" +#load "core/std" use package core diff --git a/examples/09_for_loops.onyx b/examples/09_for_loops.onyx index a8911705..28560ac6 100644 --- a/examples/09_for_loops.onyx +++ b/examples/09_for_loops.onyx @@ -4,7 +4,7 @@ // in Onyx are extremely simple to use by design and should make programming very // enjoyable. But I digress, let's look at some examples. -#load "core/std/wasi" +#load "core/std" use package core diff --git a/examples/10_switch_statements.onyx b/examples/10_switch_statements.onyx index 3336f44d..0f8bc03e 100644 --- a/examples/10_switch_statements.onyx +++ b/examples/10_switch_statements.onyx @@ -1,4 +1,4 @@ -#load "core/std/wasi" +#load "core/std" use package core diff --git a/examples/11_map.onyx b/examples/11_map.onyx index 064edf5a..86154af6 100644 --- a/examples/11_map.onyx +++ b/examples/11_map.onyx @@ -1,4 +1,4 @@ -#load "core/std/wasi" +#load "core/std" use package core diff --git a/examples/12_varargs.onyx b/examples/12_varargs.onyx index a2e4dea1..ddf56f41 100644 --- a/examples/12_varargs.onyx +++ b/examples/12_varargs.onyx @@ -1,4 +1,4 @@ -#load "core/std/wasi" +#load "core/std" use package core diff --git a/examples/13_use_keyword.onyx b/examples/13_use_keyword.onyx index a2e4dea1..ddf56f41 100644 --- a/examples/13_use_keyword.onyx +++ b/examples/13_use_keyword.onyx @@ -1,4 +1,4 @@ -#load "core/std/wasi" +#load "core/std" use package core diff --git a/examples/14_overloaded_procs.onyx b/examples/14_overloaded_procs.onyx index a2e4dea1..ddf56f41 100644 --- a/examples/14_overloaded_procs.onyx +++ b/examples/14_overloaded_procs.onyx @@ -1,4 +1,4 @@ -#load "core/std/wasi" +#load "core/std" use package core diff --git a/examples/15_polymorphic_procs.onyx b/examples/15_polymorphic_procs.onyx index a2e4dea1..ddf56f41 100644 --- a/examples/15_polymorphic_procs.onyx +++ b/examples/15_polymorphic_procs.onyx @@ -1,4 +1,4 @@ -#load "core/std/wasi" +#load "core/std" use package core diff --git a/examples/16_pipe_operator.onyx b/examples/16_pipe_operator.onyx index 5662fda6..107da554 100644 --- a/examples/16_pipe_operator.onyx +++ b/examples/16_pipe_operator.onyx @@ -1,4 +1,4 @@ -#load "core/std/wasi" +#load "core/std" use package core { println } diff --git a/include/onyxastnodes.h b/include/onyxastnodes.h index 7ca8f47a..40150f2c 100644 --- a/include/onyxastnodes.h +++ b/include/onyxastnodes.h @@ -1007,6 +1007,17 @@ enum CompileAction { ONYX_COMPILE_ACTION_PRINT_HELP, }; + +// ROBUSTNESS: The Runtime definitions here must match those in build_opts.onyx!! +typedef enum Runtime Runtime; +enum Runtime { + Runtime_Unknown = 0, + Runtime_Wasi = 1, + Runtime_Js = 2, + Runtime_Custom = 3, +}; + + typedef struct CompileOptions CompileOptions; struct CompileOptions { bh_allocator allocator; @@ -1019,6 +1030,8 @@ struct CompileOptions { b32 use_post_mvp_features : 1; + Runtime runtime; + bh_arr(const char *) included_folders; bh_arr(const char *) files; const char* target_file; diff --git a/onyx.exe b/onyx.exe index faabecef..1e601f20 100644 Binary files a/onyx.exe and b/onyx.exe differ diff --git a/progs/odin_example.onyx b/progs/odin_example.onyx index 42cfc561..309f06c4 100644 --- a/progs/odin_example.onyx +++ b/progs/odin_example.onyx @@ -1,4 +1,4 @@ -#load "core/std/wasi" +#load "core/std" #load "progs/foo_test" use package core diff --git a/progs/particle_sym.onyx b/progs/particle_sym.onyx index 53768b2e..b52eec17 100644 --- a/progs/particle_sym.onyx +++ b/progs/particle_sym.onyx @@ -1,6 +1,6 @@ package main -#load "core/std/wasi" +#load "core/std" #load "core/intrinsics/simd" use package core diff --git a/progs/poly_solidify.onyx b/progs/poly_solidify.onyx index 021834b4..29c6219c 100644 --- a/progs/poly_solidify.onyx +++ b/progs/poly_solidify.onyx @@ -1,4 +1,4 @@ -#load "core/std/wasi" +#load "core/std" use package core diff --git a/progs/simd_test.onyx b/progs/simd_test.onyx index 7ec1563f..c3ea8149 100644 --- a/progs/simd_test.onyx +++ b/progs/simd_test.onyx @@ -1,6 +1,6 @@ package main -#load "core/std/wasi" +#load "core/std" #load "core/intrinsics/simd" use package core diff --git a/progs/vararg_test.onyx b/progs/vararg_test.onyx index 0aeefed2..14759e38 100644 --- a/progs/vararg_test.onyx +++ b/progs/vararg_test.onyx @@ -1,6 +1,6 @@ package main -#load "core/std/wasi" +#load "core/std" use package core; diff --git a/progs/wasi_test.onyx b/progs/wasi_test.onyx index 70f6380f..40f48b0f 100644 --- a/progs/wasi_test.onyx +++ b/progs/wasi_test.onyx @@ -1,6 +1,6 @@ package main -#load "core/std/wasi" +#load "core/std" // NOTE: Didn't realize this would work so easily use package core { string_builder_append as sba } diff --git a/src/onyx.c b/src/onyx.c index 549b0f22..8b26168f 100644 --- a/src/onyx.c +++ b/src/onyx.c @@ -38,6 +38,7 @@ static const char* docstring = "Onyx compiler version " VERSION "\n" "Flags:\n" "\t List of initial files\n" "\t-o Specify the target file (default: out.wasm)\n" + "\t-r Specifies a runtime. Can be: wasi, js, custom.\n" "\t--verbose Verbose output\n"; static CompileOptions compile_opts_parse(bh_allocator alloc, int argc, char *argv[]) { @@ -50,6 +51,8 @@ static CompileOptions compile_opts_parse(bh_allocator alloc, int argc, char *arg .print_function_mappings = 0, .use_post_mvp_features = 0, + .runtime = Runtime_Wasi, + .files = NULL, .target_file = "out.wasm", }; @@ -95,6 +98,16 @@ static CompileOptions compile_opts_parse(bh_allocator alloc, int argc, char *arg else if (!strcmp(argv[i], "-I")) { bh_arr_push(options.included_folders, argv[++i]); } + else if (!strcmp(argv[i], "-r")) { + i += 1; + if (!strcmp(argv[i], "wasi")) options.runtime = Runtime_Wasi; + else if (!strcmp(argv[i], "js")) options.runtime = Runtime_Js; + else if (!strcmp(argv[i], "custom")) options.runtime = Runtime_Custom; + else { + bh_printf("WARNING: '%s' is not a valid runtime. Defaulting to 'wasi'.\n", argv[i]); + options.runtime = Runtime_Wasi; + } + } #if defined(_BH_LINUX) // NOTE: Fun output is only enabled for Linux because Windows command line // is not ANSI compatible and for a silly feature, I don't want to learn diff --git a/src/onyxastnodes.c b/src/onyxastnodes.c index 0e630b3e..6f25c6be 100644 --- a/src/onyxastnodes.c +++ b/src/onyxastnodes.c @@ -237,7 +237,7 @@ AstNumLit* ast_reduce_binop(bh_allocator a, AstBinaryOp* node) { #define REDUCE_UNOP_INT(op) \ if (type_is_small_integer(unop->type) || type_is_bool(unop->type)) { \ res->value.i = op ((AstNumLit *) unop->expr)->value.i; \ - } else if (type_is_integer(unop->type)) { \ + } else if (type_is_integer(unop->type) || res->type->Basic.kind == Basic_Kind_Int_Unsized) { \ res->value.l = op ((AstNumLit *) unop->expr)->value.l; \ } diff --git a/src/onyxbuiltins.c b/src/onyxbuiltins.c index ca4ddb01..a0936dc6 100644 --- a/src/onyxbuiltins.c +++ b/src/onyxbuiltins.c @@ -383,6 +383,6 @@ void initialize_builtins(bh_allocator a) { void introduce_build_options(bh_allocator a) { Package* p = package_lookup_or_create("build_opts", context.global_scope, a); - AstNumLit* runtime_type = make_int_literal(a, 1); + AstNumLit* runtime_type = make_int_literal(a, context.options->runtime); symbol_builtin_introduce(p->scope, "Runtime", (AstNode *) runtime_type); } \ No newline at end of file diff --git a/tests/aoc-2020/day1.onyx b/tests/aoc-2020/day1.onyx index b8a0b1a0..161401cd 100644 --- a/tests/aoc-2020/day1.onyx +++ b/tests/aoc-2020/day1.onyx @@ -1,4 +1,4 @@ -#load "core/std/js" +#load "core/std" use package core diff --git a/tests/aoc-2020/day10.onyx b/tests/aoc-2020/day10.onyx index 4477bf84..20aec872 100644 --- a/tests/aoc-2020/day10.onyx +++ b/tests/aoc-2020/day10.onyx @@ -1,4 +1,4 @@ -#load "core/std/js" +#load "core/std" use package core use package core.string.reader as reader diff --git a/tests/aoc-2020/day11.onyx b/tests/aoc-2020/day11.onyx index e038e0f7..b8a8b327 100644 --- a/tests/aoc-2020/day11.onyx +++ b/tests/aoc-2020/day11.onyx @@ -1,4 +1,4 @@ -#load "core/std/js" +#load "core/std" use package core use package core.string.reader as reader diff --git a/tests/aoc-2020/day12.onyx b/tests/aoc-2020/day12.onyx index 0b7c70d3..bd632b98 100644 --- a/tests/aoc-2020/day12.onyx +++ b/tests/aoc-2020/day12.onyx @@ -1,4 +1,4 @@ -#load "core/std/js" +#load "core/std" use package core use package core.string.reader as reader diff --git a/tests/aoc-2020/day13.onyx b/tests/aoc-2020/day13.onyx index adbf691b..5ebc6287 100644 --- a/tests/aoc-2020/day13.onyx +++ b/tests/aoc-2020/day13.onyx @@ -1,4 +1,4 @@ -#load "core/std/js" +#load "core/std" use package core use package core.string.reader as reader diff --git a/tests/aoc-2020/day14.onyx b/tests/aoc-2020/day14.onyx index 380dc1cc..55e77d3c 100644 --- a/tests/aoc-2020/day14.onyx +++ b/tests/aoc-2020/day14.onyx @@ -1,4 +1,4 @@ -#load "core/std/js" +#load "core/std" use package core use package core.string.reader as reader diff --git a/tests/aoc-2020/day15.onyx b/tests/aoc-2020/day15.onyx index 804e1e9f..cfb09564 100644 --- a/tests/aoc-2020/day15.onyx +++ b/tests/aoc-2020/day15.onyx @@ -1,4 +1,4 @@ -#load "core/std/js" +#load "core/std" use package core use package core.string.reader as reader diff --git a/tests/aoc-2020/day16.onyx b/tests/aoc-2020/day16.onyx index 578847f9..3e385ded 100644 --- a/tests/aoc-2020/day16.onyx +++ b/tests/aoc-2020/day16.onyx @@ -1,4 +1,4 @@ -#load "core/std/js" +#load "core/std" use package core use package core.string.reader as reader diff --git a/tests/aoc-2020/day17.onyx b/tests/aoc-2020/day17.onyx index 4cc8f304..b6580591 100644 --- a/tests/aoc-2020/day17.onyx +++ b/tests/aoc-2020/day17.onyx @@ -1,4 +1,4 @@ -#load "core/std/js" +#load "core/std" use package core use package core.string.reader as reader diff --git a/tests/aoc-2020/day18.onyx b/tests/aoc-2020/day18.onyx index 93ec4a27..e48da308 100644 --- a/tests/aoc-2020/day18.onyx +++ b/tests/aoc-2020/day18.onyx @@ -1,4 +1,4 @@ -#load "core/std/js" +#load "core/std" use package core use package core.string.reader as reader diff --git a/tests/aoc-2020/day19.onyx b/tests/aoc-2020/day19.onyx index 2dd97c4e..6183a30b 100644 --- a/tests/aoc-2020/day19.onyx +++ b/tests/aoc-2020/day19.onyx @@ -1,4 +1,4 @@ -#load "core/std/js" +#load "core/std" use package core use package core.string.reader as reader diff --git a/tests/aoc-2020/day2.onyx b/tests/aoc-2020/day2.onyx index 2685e065..7f4044e8 100644 --- a/tests/aoc-2020/day2.onyx +++ b/tests/aoc-2020/day2.onyx @@ -1,6 +1,6 @@ package main -#load "core/std/js" +#load "core/std" use package core diff --git a/tests/aoc-2020/day20.onyx b/tests/aoc-2020/day20.onyx index a7fe3278..c91593e5 100644 --- a/tests/aoc-2020/day20.onyx +++ b/tests/aoc-2020/day20.onyx @@ -1,4 +1,4 @@ -#load "core/std/js" +#load "core/std" use package core use package core.string.reader as reader diff --git a/tests/aoc-2020/day21.onyx b/tests/aoc-2020/day21.onyx index dfe862c8..82f01c17 100644 --- a/tests/aoc-2020/day21.onyx +++ b/tests/aoc-2020/day21.onyx @@ -1,4 +1,4 @@ -#load "core/std/js" +#load "core/std" use package core use package core.string.reader as reader diff --git a/tests/aoc-2020/day22.onyx b/tests/aoc-2020/day22.onyx index 393b0b8a..29912ed5 100644 --- a/tests/aoc-2020/day22.onyx +++ b/tests/aoc-2020/day22.onyx @@ -1,4 +1,4 @@ -#load "core/std/js" +#load "core/std" use package core use package core.string.reader as reader diff --git a/tests/aoc-2020/day23.onyx b/tests/aoc-2020/day23.onyx index 4d28fcbf..55c634de 100644 --- a/tests/aoc-2020/day23.onyx +++ b/tests/aoc-2020/day23.onyx @@ -1,4 +1,4 @@ -#load "core/std/js" +#load "core/std" use package core diff --git a/tests/aoc-2020/day24.onyx b/tests/aoc-2020/day24.onyx index 7d9cb5cd..2fca89f6 100644 --- a/tests/aoc-2020/day24.onyx +++ b/tests/aoc-2020/day24.onyx @@ -1,4 +1,4 @@ -#load "core/std/js" +#load "core/std" use package core use package core.string.reader as reader diff --git a/tests/aoc-2020/day25.onyx b/tests/aoc-2020/day25.onyx index 2f20fc22..eda4d688 100644 --- a/tests/aoc-2020/day25.onyx +++ b/tests/aoc-2020/day25.onyx @@ -1,4 +1,4 @@ -#load "core/std/js" +#load "core/std" use package core use package core.string.reader as reader diff --git a/tests/aoc-2020/day3.onyx b/tests/aoc-2020/day3.onyx index 95eb223d..3e378ea2 100644 --- a/tests/aoc-2020/day3.onyx +++ b/tests/aoc-2020/day3.onyx @@ -1,4 +1,4 @@ -#load "core/std/js" +#load "core/std" use package core diff --git a/tests/aoc-2020/day4.onyx b/tests/aoc-2020/day4.onyx index 51387b42..7f68201a 100644 --- a/tests/aoc-2020/day4.onyx +++ b/tests/aoc-2020/day4.onyx @@ -1,4 +1,4 @@ -#load "core/std/js" +#load "core/std" use package core diff --git a/tests/aoc-2020/day5.onyx b/tests/aoc-2020/day5.onyx index c2ac2358..9fb1e42f 100644 --- a/tests/aoc-2020/day5.onyx +++ b/tests/aoc-2020/day5.onyx @@ -1,4 +1,4 @@ -#load "core/std/js" +#load "core/std" use package core diff --git a/tests/aoc-2020/day6.onyx b/tests/aoc-2020/day6.onyx index adf23213..b2d6d976 100644 --- a/tests/aoc-2020/day6.onyx +++ b/tests/aoc-2020/day6.onyx @@ -1,4 +1,4 @@ -#load "core/std/js" +#load "core/std" use package core diff --git a/tests/aoc-2020/day7.onyx b/tests/aoc-2020/day7.onyx index 3282b976..3839db3f 100644 --- a/tests/aoc-2020/day7.onyx +++ b/tests/aoc-2020/day7.onyx @@ -1,4 +1,4 @@ -#load "core/std/js" +#load "core/std" use package core use package core.string.reader as reader diff --git a/tests/aoc-2020/day8.onyx b/tests/aoc-2020/day8.onyx index 6a0a3f93..8acf6f04 100644 --- a/tests/aoc-2020/day8.onyx +++ b/tests/aoc-2020/day8.onyx @@ -1,4 +1,4 @@ -#load "core/std/js" +#load "core/std" use package core use package core.string.reader as reader diff --git a/tests/aoc-2020/day9.onyx b/tests/aoc-2020/day9.onyx index 92f387a9..c6278208 100644 --- a/tests/aoc-2020/day9.onyx +++ b/tests/aoc-2020/day9.onyx @@ -1,4 +1,4 @@ -#load "core/std/js" +#load "core/std" use package core use package core.string.reader as reader diff --git a/tests/array_struct_robustness.onyx b/tests/array_struct_robustness.onyx index 17dfeb46..e001f9d5 100644 --- a/tests/array_struct_robustness.onyx +++ b/tests/array_struct_robustness.onyx @@ -1,4 +1,4 @@ -#load "core/std/js" +#load "core/std" use package core diff --git a/tests/baked_parameters.onyx b/tests/baked_parameters.onyx index 7e707c5c..01feb48d 100644 --- a/tests/baked_parameters.onyx +++ b/tests/baked_parameters.onyx @@ -1,4 +1,4 @@ -#load "core/std/js" +#load "core/std" use package core diff --git a/tests/compile_time_procedures.onyx b/tests/compile_time_procedures.onyx index a8503261..eca82416 100644 --- a/tests/compile_time_procedures.onyx +++ b/tests/compile_time_procedures.onyx @@ -1,4 +1,4 @@ -#load "core/std/js" +#load "core/std" use package core diff --git a/tests/defer_with_continue.onyx b/tests/defer_with_continue.onyx index 45190817..7e390ce7 100644 --- a/tests/defer_with_continue.onyx +++ b/tests/defer_with_continue.onyx @@ -1,4 +1,4 @@ -#load "core/std/js" +#load "core/std" use package core diff --git a/tests/general1.onyx.notest b/tests/general1.onyx.notest index 8e25d1f8..38695cb5 100644 --- a/tests/general1.onyx.notest +++ b/tests/general1.onyx.notest @@ -1,6 +1,6 @@ package main -#load "core/std/js" +#load "core/std" use package core diff --git a/tests/hello_world.onyx b/tests/hello_world.onyx index 3a309801..aa793d3b 100644 --- a/tests/hello_world.onyx +++ b/tests/hello_world.onyx @@ -1,6 +1,6 @@ package main -#load "core/std/js" +#load "core/std" use package core diff --git a/tests/i32map.onyx b/tests/i32map.onyx index d43a198f..c83cb05f 100644 --- a/tests/i32map.onyx +++ b/tests/i32map.onyx @@ -1,6 +1,6 @@ package main -#load "core/std/js" +#load "core/std" use package core diff --git a/tests/multiple_returns_robustness.onyx b/tests/multiple_returns_robustness.onyx index cbda0b4e..27790819 100644 --- a/tests/multiple_returns_robustness.onyx +++ b/tests/multiple_returns_robustness.onyx @@ -1,4 +1,4 @@ -#load "core/std/js" +#load "core/std" use package core diff --git a/tests/named_arguments_test.onyx b/tests/named_arguments_test.onyx index 643393d6..a43ae642 100644 --- a/tests/named_arguments_test.onyx +++ b/tests/named_arguments_test.onyx @@ -1,4 +1,4 @@ -#load "core/std/js" +#load "core/std" use package core diff --git a/tests/new_struct_behaviour.onyx b/tests/new_struct_behaviour.onyx index 9bb6887d..4f114fb1 100644 --- a/tests/new_struct_behaviour.onyx +++ b/tests/new_struct_behaviour.onyx @@ -1,6 +1,6 @@ // This is a needlessly complicated test of some of the newer features with structs. -#load "core/std/js" +#load "core/std" use package core diff --git a/tests/operator_overload.onyx b/tests/operator_overload.onyx index bcc7ed0d..f3aa5a90 100644 --- a/tests/operator_overload.onyx +++ b/tests/operator_overload.onyx @@ -1,4 +1,4 @@ -#load "core/std/js" +#load "core/std" use package core diff --git a/tests/overload_with_autocast.onyx b/tests/overload_with_autocast.onyx index 9048be22..e790ac62 100644 --- a/tests/overload_with_autocast.onyx +++ b/tests/overload_with_autocast.onyx @@ -1,4 +1,4 @@ -#load "core/std/js" +#load "core/std" use package core diff --git a/tests/poly_structs_with_values.onyx b/tests/poly_structs_with_values.onyx index 40394fa9..d49957f9 100644 --- a/tests/poly_structs_with_values.onyx +++ b/tests/poly_structs_with_values.onyx @@ -1,4 +1,4 @@ -#load "core/std/js" +#load "core/std" use package core diff --git a/tests/polymorphic_array_lengths.onyx b/tests/polymorphic_array_lengths.onyx index 787693c7..822b2faf 100644 --- a/tests/polymorphic_array_lengths.onyx +++ b/tests/polymorphic_array_lengths.onyx @@ -1,4 +1,4 @@ -#load "core/std/js" +#load "core/std" use package core diff --git a/tests/string_stream_test.onyx b/tests/string_stream_test.onyx index 0da25100..55a10a63 100644 --- a/tests/string_stream_test.onyx +++ b/tests/string_stream_test.onyx @@ -1,4 +1,4 @@ -#load "core/std/js" +#load "core/std" use package core diff --git a/tests/struct_robustness.onyx b/tests/struct_robustness.onyx index 70df0f08..896d038c 100644 --- a/tests/struct_robustness.onyx +++ b/tests/struct_robustness.onyx @@ -1,3 +1,5 @@ +#load "core/std" + use package core main :: proc (args: [] cstr) { @@ -191,5 +193,3 @@ main :: proc (args: [] cstr) { printf("PolyUnion(%i)\n", pu.r_data); } } - -#load "core/std/js" \ No newline at end of file diff --git a/tests/vararg_test.onyx b/tests/vararg_test.onyx index 988f355e..9bc287e8 100644 --- a/tests/vararg_test.onyx +++ b/tests/vararg_test.onyx @@ -1,6 +1,6 @@ package main -#load "core/std/js" +#load "core/std" use package core;