added core.clock package for wasi runtime
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Sat, 29 May 2021 21:39:39 +0000 (16:39 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Sat, 29 May 2021 21:39:39 +0000 (16:39 -0500)
core/env.onyx [deleted file]
core/runtime/wasi.onyx
core/std.onyx
core/wasi.onyx [deleted file]
core/wasi/README.md [new file with mode: 0644]
core/wasi/clock.onyx [new file with mode: 0644]
core/wasi/env.onyx [new file with mode: 0644]
core/wasi/wasi.onyx [new file with mode: 0644]
tests/sets.onyx

diff --git a/core/env.onyx b/core/env.onyx
deleted file mode 100644 (file)
index 568dea4..0000000
+++ /dev/null
@@ -1,55 +0,0 @@
-package core.env
-
-#if (package runtime).Runtime != (package runtime).Runtime_Wasi {
-    #error "'core.env' is only available with the 'wasi' runtime.";
-}
-
-
-use package wasi { environ_get, environ_sizes_get, Size }
-#private map    :: package core.map
-#private memory :: package core.memory
-#private string :: package core.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.from_cstr(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);
-}
index a90ffabf0c6ae40e18c52e7263f572e27079ab14..be8f7fad57fedd562b421a48ea3935a4a0d3f516 100644 (file)
@@ -1,6 +1,6 @@
 package runtime
 
-#load "core/wasi"
+#load "core/wasi/wasi"
 #load "core/runtime/common"
 
 use package wasi
index 15d5a7bee9cabf98d20301c1a25ff9e6aae18966..776d715d7ff0c3ce1f363795553b344db2726b65 100644 (file)
@@ -35,9 +35,10 @@ package core
 #private_file runtime :: package runtime
 #if runtime.Runtime == runtime.Runtime_Wasi {
     #load "./runtime/wasi"
-    #load "./wasi"
+    #load "./wasi/wasi"
+    #load "./wasi/env"
+    #load "./wasi/clock"
     #load "./io/file"
-    #load "./env"
 }
 
 #if runtime.Runtime == runtime.Runtime_Js {
diff --git a/core/wasi.onyx b/core/wasi.onyx
deleted file mode 100644 (file)
index fa0e3a1..0000000
+++ /dev/null
@@ -1,434 +0,0 @@
-package wasi
-
-Size      :: #type u32;
-Filesize  :: #type u64;
-Timestamp :: #type u64;
-
-ClockID   :: enum (u32) {
-    Realtime         :: 0x00;
-    Monotonic        :: 0x01;
-    ProcessCPUTimeID :: 0x02;
-    ThreadCPUTimeID  :: 0x03;
-}
-
-Errno :: enum (u16) {
-    Success      :: 0x00;
-    TooBig       :: 0x01;
-    Access       :: 0x02;
-    AddrInUse    :: 0x03;
-    AddrNotAvail :: 0x04;
-    AFNoSupport  :: 0x05;
-    Again        :: 0x06;
-    Already      :: 0x07;
-    BadFile      :: 0x08;
-    BadMsg       :: 0x09;
-    Busy         :: 0x0a;
-    Canceled     :: 0x0b;
-    Child        :: 0x0c;
-    ConnAborted  :: 0x0d;
-    ConnRefused  :: 0x0e;
-    ConnReset    :: 0x0f;
-    DeadLock     :: 0x10;
-    DestAddrReq  :: 0x11;
-    Domain       :: 0x12;
-    DQUOT        :: 0x13;
-    Exist        :: 0x14;
-    Fault        :: 0x15;
-    FileTooBig   :: 0x16;
-    HostUnreach  :: 0x17;
-    IdentRemoved :: 0x18;
-    IllegalSeq   :: 0x19;
-    InProgress   :: 0x1a;
-    Interrupt    :: 0x1b;
-    Invalid      :: 0x1c;
-    IO           :: 0x1d;
-    IsConnection :: 0x1e;
-    IsDirectory  :: 0x1f;
-    Loop         :: 0x20;
-    MFile        :: 0x21;
-    MLink        :: 0x22;
-    MsgSize      :: 0x23;
-    MultiHop     :: 0x24;
-    NameTooLong  :: 0x25;
-    NetDown      :: 0x26;
-    NetReset     :: 0x27;
-    NetUnreach   :: 0x28;
-    NFile        :: 0x29;
-    NoBufs       :: 0x2a;   
-    NoDev        :: 0x2b;
-    NoEntry      :: 0x2c;
-    NoExec       :: 0x2d;
-    NoLock       :: 0x2e;
-    NoLink       :: 0x2f;
-    NoMemory     :: 0x30;
-    NoMsg        :: 0x31;
-    NoProtoOpt   :: 0x32;
-    NoSpace      :: 0x33;
-    NoSys        :: 0x34;
-    NotConn      :: 0x35;
-    NotDir       :: 0x36;
-    NotEmpty     :: 0x37;
-    NotRecover   :: 0x38;
-    NotSock      :: 0x39;
-    NotSupported :: 0x3a;
-    NoTTY        :: 0x3b;
-    NXIO         :: 0x3c;
-    Overflow     :: 0x3d;
-    OwnerDead    :: 0x3e;
-    Permission   :: 0x3f;
-    Pipe         :: 0x40;
-    Protocol     :: 0x41;
-    ProtoNoSup   :: 0x42;
-    Prototype    :: 0x43;
-    Range        :: 0x44;
-    ReadonlyFS   :: 0x45;
-    SeekPipe     :: 0x46;
-    Search       :: 0x47;
-    Stale        :: 0x48;
-    Timedout     :: 0x49;
-    TextBusy     :: 0x4a;
-    XDev         :: 0x4b;
-
-    NotCapable   :: 0x4c;
-}
-
-Rights :: enum #flags (u64) {
-    DataSync;
-    Read;
-    Seek;
-    FdStatSetFlags;
-    Sync;
-    Tell;
-    Write;
-    Advise;
-    Allocate;
-    PathCreateDirectory;
-    PathCreateFile;
-    PathLinkSource;
-    PathLinkTarget;
-    PathOpen;
-    ReadDir;
-    PathReadlink;
-    PathRenameSource;
-    PathRenameTarget;
-    PathFilestatGet;
-    PathFilestateSetSize;
-    PathFilestateSetTimes;
-    FilestatGet;
-    FilestatSetSize;
-    FilestatSetTimes;
-    PathSymlink;
-    PathRemoveDirectory;
-    PathUnlinkFile;
-    PollFDReadWrite;
-    SockShutdown;
-}
-
-FileDescriptor :: #type i32;
-
-IOVec :: struct {
-    buf : u32; // actually a ^u8, but WASM is 32-bit at the moment;
-    len : u32;
-}
-
-CIOVec :: #type IOVec; // Constant IOVec
-
-FileDelta :: #type i64;
-
-Whence :: enum (u8) {
-    Set :: 0x00;
-    Cur :: 0x01;
-    End :: 0x02;
-}
-
-DirCookie  :: #type u64;
-DirNameLen :: #type u32;
-INode      :: #type u64;
-
-Filetype :: enum (u8) {
-    Unknown      :: 0x00;
-    BlockDevice  :: 0x01;
-    CharDevice   :: 0x02;
-    Directory    :: 0x03;
-    RegularFile  :: 0x04;
-    SocketDgram  :: 0x05;
-    SocketStream :: 0x06;   
-    SymLink      :: 0x07;
-}
-
-DirEnt :: struct {
-    d_next   : DirCookie;
-    d_ino    : INode;
-    d_namlen : DirNameLen;
-    d_type   : Filetype;
-}
-
-Advice :: enum (u8) {
-    Normal     :: 0x00; 
-    Sequential :: 0x01;
-    Random     :: 0x02;
-    WillNeed   :: 0x03;
-    DontNeed   :: 0x04;
-    NoReuse    :: 0x05;
-}
-
-FDFlags :: enum #flags (u16) {
-    Append;
-    DSync;
-    NonBlock;
-    RSync;
-    Sync;
-}
-
-FDStat :: struct {
-    fs_filetype          : Filetype;
-    fs_flags             : FDFlags;
-    fs_rights_base       : Rights;
-    fs_rights_inheriting : Rights;
-}
-
-Device :: #type u64;
-
-FSTFlags :: enum #flags (u16) {
-    ATIM;
-    ATIM_NOW;
-    MTIM;
-    MTIM_NOW;
-}
-
-LookupFlags :: enum #flags (u32) {
-    SymLinkFollow;
-}
-
-OFlags :: enum #flags (u16) {
-    Creat;
-    Directory;
-    FailIfExists;
-    Trunc;
-}
-
-LinkCount :: #type u32;
-
-FileStat :: struct {
-    dev      : Device;
-    ino      : INode;
-    filetype : Filetype;
-
-    // ANGER(Brendan Hansen): I give the worst documentation in
-    // the entire world award to WASI, whose best documentation
-    // is a C header file, that is itself incorrect. nlink does
-    // not exist in the current version of wasmtime, and should
-    // not be here, but maybe in the future? Ugh.
-    //
-    // STILL ANGERY(Brendan Hansen): Actually, nlink does exist
-    // in the current version of wasmtime; however its actually
-    // a 32-bit value, not 64-bit, which means the offsets were
-    // incorrect. I fixed it for now, but when wasmtime updates
-    // the size of 'LinkCount' needs to be changed.
-    //                                   - brendanfh 2020/12/05
-    //                                   - brendanfh 2020/12/07
-    nlink    : LinkCount;
-
-    size     : Filesize;
-    atim     : Timestamp;
-    mtim     : Timestamp;
-    ctim     : Timestamp;
-}
-
-Userdata :: #type u64;
-
-EventType :: enum (u8) {
-    Clock;
-    FDRead;
-    FDWrite;
-}
-
-EventRWFlags :: enum #flags (u16) {
-    ReadWriteHangUp;
-}
-
-EventFDReadWrite :: struct {
-    nbytes : Filesize;
-    flags  : EventRWFlags;
-}
-
-Event :: struct {
-    userdata     : Userdata;
-    error        : Errno;
-    type         : EventType;
-    fd_readwrite : EventFDReadWrite;
-}
-
-SubClockFlags :: enum #flags (u16) {
-    ClockAbsTime;
-}
-
-SubscriptionClock :: struct {
-    id        : ClockID;
-    timeout   : Timestamp;
-    precision : Timestamp;
-    flags     : SubClockFlags;
-}
-
-SubscriptionFDReadWrite :: struct {
-    file_descriptor : FileDescriptor;
-}
-
-SubscriptionTagged :: struct {
-    tag : EventType;
-
-    u : struct #union {
-        clock    : SubscriptionClock;
-        fd_read  : SubscriptionFDReadWrite;
-        fd_write : SubscriptionFDReadWrite;
-    };
-}
-
-Subscription :: struct {
-    userdata : Userdata;
-
-    u : SubscriptionTagged;
-}
-
-ExitCode :: #type u32;
-Signal :: enum (u8) {
-    None;
-    Hup;
-    Int;
-    Quit;
-    Ill;
-    Trap;
-    Abrt;
-    Bus;
-    Fpe;
-    Kill;
-    USR1;
-    Segv;
-    USR2;
-    Pipe;
-    Alrm;
-    Term;
-    Chld;
-    Stop;
-    Tstp;
-    Ttin;
-    Urg;
-    Xcpu;
-    Xfsz;
-    Vtalrm;
-    Prof;
-    Winch;
-    Poll;
-    Pwr;
-    Sys;
-}
-
-RIFlags :: enum #flags (u16) {
-    RecvPeek;
-    RecvWaitAll;
-}
-
-ROFlags :: enum #flags (u16) {
-    RecvDataTruncated :: 1;
-}
-
-SIFlags :: enum #flags (u16) {
-    None;
-}
-
-SDFlags :: enum #flags (u16) {
-    RD;
-    WR;
-}
-
-PreopenType :: enum (u8) {
-    Dir :: 0x00;
-}
-
-PrestatDir :: struct {
-    pr_name_len : Size;
-}
-
-PrestatTagged :: struct {
-    tag : PreopenType;
-
-    u : struct #union {
-        dir : PrestatDir;
-    };
-}
-
-IOVecArray :: struct {
-    iovs     : u32; // actually ^IOVec; see comment above
-    iovs_len : Size;
-}
-
-
-// FUNCTIONS
-args_get       :: (argv: ^^u8, argv_buf: ^u8) -> Errno #foreign "wasi_snapshot_preview1" "args_get"---
-args_sizes_get :: (argc: ^Size, argv_buf_size: ^Size) -> Errno #foreign "wasi_snapshot_preview1" "args_sizes_get" ---
-
-environ_get       :: (environ: ^^u8, environ_buf: ^u8) -> Errno #foreign "wasi_snapshot_preview1" "environ_get" ---
-environ_sizes_get :: (environc: ^Size, environ_buf_size: ^Size) -> Errno #foreign "wasi_snapshot_preview1" "environ_sizes_get" ---
-
-clock_res_get  :: (id: ClockID, resolution: ^Timestamp) -> Errno #foreign "wasi_snapshot_preview1" "clock_res_get" ---
-clock_time_get :: (id: ClockID, precision: Timestamp, time: ^Timestamp) -> Errno #foreign "wasi_snapshot_preview1" "clock_time_get" ---
-
-fd_advise             :: (fd: FileDescriptor, offset: Filesize, len: Filesize, advice: Advice) -> Errno #foreign "wasi_snapshot_preview1" "fd_advise" ---
-fd_allocate           :: (fd: FileDescriptor, offset: Filesize, len: Filesize) -> Errno #foreign "wasi_snapshot_preview1" "fd_allocate" ---
-fd_close              :: (fd: FileDescriptor) -> Errno #foreign "wasi_snapshot_preview1" "fd_close" ---
-fd_datasync           :: (fd: FileDescriptor) -> Errno #foreign "wasi_snapshot_preview1" "fd_datasync" ---
-fd_fdstat_get         :: (fd: FileDescriptor, stat: ^FDStat) -> Errno #foreign "wasi_snapshot_preview1" "fd_fdstat_get" ---
-fd_fdstat_set_flags   :: (fd: FileDescriptor, flags: FDFlags) -> Errno #foreign "wasi_snapshot_preview1" "fd_fdstat_set_flags" ---
-fd_fdstat_set_rights  :: (fd: FileDescriptor, rights_base: Rights, rights_inheriting: Rights) -> Errno #foreign "wasi_snapshot_preview1" "fd_fdstat_set_rights" ---
-fd_filestat_get       :: (fd: FileDescriptor, buf: ^FileStat) -> Errno #foreign "wasi_snapshot_preview1" "fd_filestat_get" ---
-fd_filestat_set_size  :: (fd: FileDescriptor, size: Filesize) -> Errno #foreign "wasi_snapshot_preview1" "fd_filestat_set_size" ---
-fd_filestat_set_times :: (fd: FileDescriptor, atim: Timestamp, mtim: Timestamp, fst_flags: FSTFlags) -> Errno #foreign "wasi_snapshot_preview1" "fd_filestat_set_times" ---
-fd_pread              :: (fd: FileDescriptor, iovs: IOVecArray, offset: Filesize, nread: ^Size) -> Errno #foreign "wasi_snapshot_preview1" "fd_pread" ---
-fd_prestat_get        :: (fd: FileDescriptor, buf: ^PrestatTagged) -> Errno #foreign "wasi_snapshot_preview1" "fd_prestat_get" ---
-fd_prestat_dir_name   :: (fd: FileDescriptor, path: str) -> Errno #foreign "wasi_snapshot_preview1" "fd_prestat_dir_name" ---
-fd_pwrite             :: (fd: FileDescriptor, iovs: IOVecArray, offset: Filesize, nwritten: ^Size) -> Errno #foreign "wasi_snapshot_preview1" "fd_pwrite" ---
-fd_read               :: (fd: FileDescriptor, iovs: IOVecArray, nread: ^Size) -> Errno #foreign "wasi_snapshot_preview1" "fd_read" ---
-fd_readdir            :: (fd: FileDescriptor, buf: ^u8, buf_len: Size, cookie: DirCookie, bufused: ^Size) -> Errno #foreign "wasi_snapshot_preview1" "fd_readdir" ---
-fd_renumber           :: (fd: FileDescriptor, to: FileDescriptor) -> Errno #foreign "wasi_snapshot_preview1" "fd_renumber" ---
-fd_seek               :: (fd: FileDescriptor, offset: FileDelta, whence: Whence, newoffset: ^Filesize) -> Errno #foreign "wasi_snapshot_preview1" "fd_seek" ---
-fd_sync               :: (fd: FileDescriptor) -> Errno #foreign "wasi_snapshot_preview1" "fd_sync" ---
-fd_tell               :: (fd: FileDescriptor, offset: ^Filesize) -> Errno #foreign "wasi_snapshot_preview1" "fd_tell" ---
-fd_write              :: (fd: FileDescriptor, iovs: IOVecArray, nwritten: ^Size) -> Errno #foreign "wasi_snapshot_preview1" "fd_write" ---
-
-path_create_directory   :: (fd: FileDescriptor, path: str) -> Errno #foreign "wasi_snapshot_preview1" "path_create_directory" ---
-path_filestat_get       :: (fd: FileDescriptor, flags: LookupFlags, path: str, buf: ^FileStat) -> Errno #foreign "wasi_snapshot_preview1" "path_filestat_get" ---
-path_filestat_set_times :: (fd: FileDescriptor, flags: LookupFlags, path: str, atim: Timestamp, mtim: Timestamp, fst_flags: FSTFlags) -> Errno #foreign "wasi_snapshot_preview1" "path_filestat_set_times" ---
-
-path_link :: (fd: FileDescriptor, old_flags: LookupFlags, old_path: str, new_fd: FileDescriptor, new_path: str) -> Errno #foreign "wasi_snapshot_preview1" "path_link" ---
-path_open :: (fd: FileDescriptor
-    , dirflags: LookupFlags
-    , path: str
-    , oflags: OFlags
-    , fs_rights_base: Rights
-    , fs_rights_inherting: Rights
-    , fdflags: FDFlags
-    , opened_fd: ^FileDescriptor
-    ) -> Errno
-     #foreign "wasi_snapshot_preview1" "path_open" ---
-
-path_readlink :: (fd: FileDescriptor, path: str, buf: ^u8, buf_len: Size, bufused: ^Size) -> Errno #foreign "wasi_snapshot_preview1" "path_readlink" ---
-path_remove_directory :: (fd: FileDescriptor, path: str) -> Errno #foreign "wasi_snapshot_preview1" "path_remove_directory" ---
-path_rename  :: (fd: FileDescriptor, old_path: str, new_fd: FileDescriptor, new_path: str) -> Errno #foreign "wasi_snapshot_preview1" "path_rename" ---
-path_symlink :: (old_path: ^u8, old_path_len: Size, fd: FileDescriptor, new_path: str) -> Errno #foreign "wasi_snapshot_preview1" "path_symlink" ---
-path_unlink_file :: (fd: FileDescriptor, path: str) -> Errno #foreign "wasi_snapshot_preview1" "path_unlink_file" ---
-
-poll_oneoff :: (in: ^Subscription, out: ^Event, nsubscriptions: Size, nevents: ^Size) -> Errno #foreign "wasi_snapshot_preview1" "poll_oneoff" ---
-
-proc_exit  :: (rval: ExitCode) -> void #foreign "wasi_snapshot_preview1" "proc_exit" ---
-proc_raise :: (sig: Signal) -> Errno #foreign "wasi_snapshot_preview1" "proc_raise" ---
-
-sched_yield :: () -> Errno #foreign "wasi_snapshot_preview1" "sched_yield" ---
-
-random_get :: (buf: ^u8, buf_len: Size) -> Errno #foreign "wasi_snapshot_preview1" "random_get" ---
-
-sock_recv     :: (fd: FileDescriptor, ri_data: IOVecArray, ri_flags: RIFlags, ro_datalen: ^Size, ro_flags: ^ROFlags) -> Errno #foreign "wasi_snapshot_preview1" "sock_recv" ---
-sock_send     :: (fd: FileDescriptor, si_data: IOVecArray, si_flags: SIFlags, so_datalen: ^Size) -> Errno #foreign "wasi_snapshot_preview1" "sock_send" ---
-sock_shutdown :: (fd: FileDescriptor, how: SDFlags) -> Errno #foreign "wasi_snapshot_preview1" "sock_shutdown" ---
-
-
-
diff --git a/core/wasi/README.md b/core/wasi/README.md
new file mode 100644 (file)
index 0000000..3c3e6ee
--- /dev/null
@@ -0,0 +1 @@
+These files are only available on the 'wasi' runtime. In a C compilation, other files will be included, and the API between them will have to remain the same.
\ No newline at end of file
diff --git a/core/wasi/clock.onyx b/core/wasi/clock.onyx
new file mode 100644 (file)
index 0000000..bd04ad0
--- /dev/null
@@ -0,0 +1,40 @@
+package core.clock
+
+#if (package runtime).Runtime != (package runtime).Runtime_Wasi {
+    #error "'core.clock' is only available with the 'wasi' runtime.";
+}
+
+use package wasi
+
+time :: () -> u64 {
+    output_time: Timestamp;
+    clock_time_get(.Realtime, 500000, ^output_time);
+    return ~~(output_time / 1000000);
+}
+
+time_ns :: () -> u64 {
+    output_time: Timestamp;
+    clock_time_get(.Realtime, 1, ^output_time);
+    return ~~output_time;
+}
+
+sleep :: (milliseconds: u32) {
+    tagged: SubscriptionTagged;
+    tagged.tag = .Clock;
+    tagged.clock = .{
+        id = .Realtime,
+        timeout = cast(u64) milliseconds * 1000000,
+        precision = 1000,
+        flags = .ClockAbsTime,
+    };
+
+    subscription := Subscription.{
+        userdata = 0,
+        u = tagged,
+    };
+
+    event: Event;
+    number_of_events: u32;
+
+    error_code := poll_oneoff(^subscription, ^event, 1, ^number_of_events);
+}
\ No newline at end of file
diff --git a/core/wasi/env.onyx b/core/wasi/env.onyx
new file mode 100644 (file)
index 0000000..568dea4
--- /dev/null
@@ -0,0 +1,55 @@
+package core.env
+
+#if (package runtime).Runtime != (package runtime).Runtime_Wasi {
+    #error "'core.env' is only available with the 'wasi' runtime.";
+}
+
+
+use package wasi { environ_get, environ_sizes_get, Size }
+#private map    :: package core.map
+#private memory :: package core.memory
+#private string :: package core.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.from_cstr(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/wasi/wasi.onyx b/core/wasi/wasi.onyx
new file mode 100644 (file)
index 0000000..4a31ab8
--- /dev/null
@@ -0,0 +1,434 @@
+package wasi
+
+Size      :: #type u32;
+Filesize  :: #type u64;
+Timestamp :: #type u64;
+
+ClockID   :: enum (u32) {
+    Realtime         :: 0x00;
+    Monotonic        :: 0x01;
+    ProcessCPUTimeID :: 0x02;
+    ThreadCPUTimeID  :: 0x03;
+}
+
+Errno :: enum (u16) {
+    Success      :: 0x00;
+    TooBig       :: 0x01;
+    Access       :: 0x02;
+    AddrInUse    :: 0x03;
+    AddrNotAvail :: 0x04;
+    AFNoSupport  :: 0x05;
+    Again        :: 0x06;
+    Already      :: 0x07;
+    BadFile      :: 0x08;
+    BadMsg       :: 0x09;
+    Busy         :: 0x0a;
+    Canceled     :: 0x0b;
+    Child        :: 0x0c;
+    ConnAborted  :: 0x0d;
+    ConnRefused  :: 0x0e;
+    ConnReset    :: 0x0f;
+    DeadLock     :: 0x10;
+    DestAddrReq  :: 0x11;
+    Domain       :: 0x12;
+    DQUOT        :: 0x13;
+    Exist        :: 0x14;
+    Fault        :: 0x15;
+    FileTooBig   :: 0x16;
+    HostUnreach  :: 0x17;
+    IdentRemoved :: 0x18;
+    IllegalSeq   :: 0x19;
+    InProgress   :: 0x1a;
+    Interrupt    :: 0x1b;
+    Invalid      :: 0x1c;
+    IO           :: 0x1d;
+    IsConnection :: 0x1e;
+    IsDirectory  :: 0x1f;
+    Loop         :: 0x20;
+    MFile        :: 0x21;
+    MLink        :: 0x22;
+    MsgSize      :: 0x23;
+    MultiHop     :: 0x24;
+    NameTooLong  :: 0x25;
+    NetDown      :: 0x26;
+    NetReset     :: 0x27;
+    NetUnreach   :: 0x28;
+    NFile        :: 0x29;
+    NoBufs       :: 0x2a;   
+    NoDev        :: 0x2b;
+    NoEntry      :: 0x2c;
+    NoExec       :: 0x2d;
+    NoLock       :: 0x2e;
+    NoLink       :: 0x2f;
+    NoMemory     :: 0x30;
+    NoMsg        :: 0x31;
+    NoProtoOpt   :: 0x32;
+    NoSpace      :: 0x33;
+    NoSys        :: 0x34;
+    NotConn      :: 0x35;
+    NotDir       :: 0x36;
+    NotEmpty     :: 0x37;
+    NotRecover   :: 0x38;
+    NotSock      :: 0x39;
+    NotSupported :: 0x3a;
+    NoTTY        :: 0x3b;
+    NXIO         :: 0x3c;
+    Overflow     :: 0x3d;
+    OwnerDead    :: 0x3e;
+    Permission   :: 0x3f;
+    Pipe         :: 0x40;
+    Protocol     :: 0x41;
+    ProtoNoSup   :: 0x42;
+    Prototype    :: 0x43;
+    Range        :: 0x44;
+    ReadonlyFS   :: 0x45;
+    SeekPipe     :: 0x46;
+    Search       :: 0x47;
+    Stale        :: 0x48;
+    Timedout     :: 0x49;
+    TextBusy     :: 0x4a;
+    XDev         :: 0x4b;
+
+    NotCapable   :: 0x4c;
+}
+
+Rights :: enum #flags (u64) {
+    DataSync;
+    Read;
+    Seek;
+    FdStatSetFlags;
+    Sync;
+    Tell;
+    Write;
+    Advise;
+    Allocate;
+    PathCreateDirectory;
+    PathCreateFile;
+    PathLinkSource;
+    PathLinkTarget;
+    PathOpen;
+    ReadDir;
+    PathReadlink;
+    PathRenameSource;
+    PathRenameTarget;
+    PathFilestatGet;
+    PathFilestateSetSize;
+    PathFilestateSetTimes;
+    FilestatGet;
+    FilestatSetSize;
+    FilestatSetTimes;
+    PathSymlink;
+    PathRemoveDirectory;
+    PathUnlinkFile;
+    PollFDReadWrite;
+    SockShutdown;
+}
+
+FileDescriptor :: #type i32;
+
+IOVec :: struct {
+    buf : u32; // actually a ^u8, but WASM is 32-bit at the moment;
+    len : u32;
+}
+
+CIOVec :: #type IOVec; // Constant IOVec
+
+FileDelta :: #type i64;
+
+Whence :: enum (u8) {
+    Set :: 0x00;
+    Cur :: 0x01;
+    End :: 0x02;
+}
+
+DirCookie  :: #type u64;
+DirNameLen :: #type u32;
+INode      :: #type u64;
+
+Filetype :: enum (u8) {
+    Unknown      :: 0x00;
+    BlockDevice  :: 0x01;
+    CharDevice   :: 0x02;
+    Directory    :: 0x03;
+    RegularFile  :: 0x04;
+    SocketDgram  :: 0x05;
+    SocketStream :: 0x06;   
+    SymLink      :: 0x07;
+}
+
+DirEnt :: struct {
+    d_next   : DirCookie;
+    d_ino    : INode;
+    d_namlen : DirNameLen;
+    d_type   : Filetype;
+}
+
+Advice :: enum (u8) {
+    Normal     :: 0x00; 
+    Sequential :: 0x01;
+    Random     :: 0x02;
+    WillNeed   :: 0x03;
+    DontNeed   :: 0x04;
+    NoReuse    :: 0x05;
+}
+
+FDFlags :: enum #flags (u16) {
+    Append;
+    DSync;
+    NonBlock;
+    RSync;
+    Sync;
+}
+
+FDStat :: struct {
+    fs_filetype          : Filetype;
+    fs_flags             : FDFlags;
+    fs_rights_base       : Rights;
+    fs_rights_inheriting : Rights;
+}
+
+Device :: #type u64;
+
+FSTFlags :: enum #flags (u16) {
+    ATIM;
+    ATIM_NOW;
+    MTIM;
+    MTIM_NOW;
+}
+
+LookupFlags :: enum #flags (u32) {
+    SymLinkFollow;
+}
+
+OFlags :: enum #flags (u16) {
+    Creat;
+    Directory;
+    FailIfExists;
+    Trunc;
+}
+
+LinkCount :: #type u32;
+
+FileStat :: struct {
+    dev      : Device;
+    ino      : INode;
+    filetype : Filetype;
+
+    // ANGER(Brendan Hansen): I give the worst documentation in
+    // the entire world award to WASI, whose best documentation
+    // is a C header file, that is itself incorrect. nlink does
+    // not exist in the current version of wasmtime, and should
+    // not be here, but maybe in the future? Ugh.
+    //
+    // STILL ANGERY(Brendan Hansen): Actually, nlink does exist
+    // in the current version of wasmtime; however its actually
+    // a 32-bit value, not 64-bit, which means the offsets were
+    // incorrect. I fixed it for now, but when wasmtime updates
+    // the size of 'LinkCount' needs to be changed.
+    //                                   - brendanfh 2020/12/05
+    //                                   - brendanfh 2020/12/07
+    nlink    : LinkCount;
+
+    size     : Filesize;
+    atim     : Timestamp;
+    mtim     : Timestamp;
+    ctim     : Timestamp;
+}
+
+Userdata :: #type u64;
+
+EventType :: enum (u8) {
+    Clock;
+    FDRead;
+    FDWrite;
+}
+
+EventRWFlags :: enum #flags (u16) {
+    ReadWriteHangUp;
+}
+
+EventFDReadWrite :: struct {
+    nbytes : Filesize;
+    flags  : EventRWFlags;
+}
+
+Event :: struct {
+    userdata     : Userdata;
+    error        : Errno;
+    type         : EventType;
+    fd_readwrite : EventFDReadWrite;
+}
+
+SubClockFlags :: enum #flags (u16) {
+    ClockAbsTime;
+}
+
+SubscriptionClock :: struct {
+    id        : ClockID;
+    timeout   : Timestamp;
+    precision : Timestamp;
+    flags     : SubClockFlags;
+}
+
+SubscriptionFDReadWrite :: struct {
+    file_descriptor : FileDescriptor;
+}
+
+SubscriptionTagged :: struct {
+    tag : EventType;
+
+    use u : struct #union {
+        clock    : SubscriptionClock;
+        fd_read  : SubscriptionFDReadWrite;
+        fd_write : SubscriptionFDReadWrite;
+    };
+}
+
+Subscription :: struct {
+    userdata : Userdata;
+
+    u : SubscriptionTagged;
+}
+
+ExitCode :: #type u32;
+Signal :: enum (u8) {
+    None;
+    Hup;
+    Int;
+    Quit;
+    Ill;
+    Trap;
+    Abrt;
+    Bus;
+    Fpe;
+    Kill;
+    USR1;
+    Segv;
+    USR2;
+    Pipe;
+    Alrm;
+    Term;
+    Chld;
+    Stop;
+    Tstp;
+    Ttin;
+    Urg;
+    Xcpu;
+    Xfsz;
+    Vtalrm;
+    Prof;
+    Winch;
+    Poll;
+    Pwr;
+    Sys;
+}
+
+RIFlags :: enum #flags (u16) {
+    RecvPeek;
+    RecvWaitAll;
+}
+
+ROFlags :: enum #flags (u16) {
+    RecvDataTruncated :: 1;
+}
+
+SIFlags :: enum #flags (u16) {
+    None;
+}
+
+SDFlags :: enum #flags (u16) {
+    RD;
+    WR;
+}
+
+PreopenType :: enum (u8) {
+    Dir :: 0x00;
+}
+
+PrestatDir :: struct {
+    pr_name_len : Size;
+}
+
+PrestatTagged :: struct {
+    tag : PreopenType;
+
+    u : struct #union {
+        dir : PrestatDir;
+    };
+}
+
+IOVecArray :: struct {
+    iovs     : u32; // actually ^IOVec; see comment above
+    iovs_len : Size;
+}
+
+
+// FUNCTIONS
+args_get       :: (argv: ^^u8, argv_buf: ^u8) -> Errno #foreign "wasi_snapshot_preview1" "args_get"---
+args_sizes_get :: (argc: ^Size, argv_buf_size: ^Size) -> Errno #foreign "wasi_snapshot_preview1" "args_sizes_get" ---
+
+environ_get       :: (environ: ^^u8, environ_buf: ^u8) -> Errno #foreign "wasi_snapshot_preview1" "environ_get" ---
+environ_sizes_get :: (environc: ^Size, environ_buf_size: ^Size) -> Errno #foreign "wasi_snapshot_preview1" "environ_sizes_get" ---
+
+clock_res_get  :: (id: ClockID, resolution: ^Timestamp) -> Errno #foreign "wasi_snapshot_preview1" "clock_res_get" ---
+clock_time_get :: (id: ClockID, precision: Timestamp, time: ^Timestamp) -> Errno #foreign "wasi_snapshot_preview1" "clock_time_get" ---
+
+fd_advise             :: (fd: FileDescriptor, offset: Filesize, len: Filesize, advice: Advice) -> Errno #foreign "wasi_snapshot_preview1" "fd_advise" ---
+fd_allocate           :: (fd: FileDescriptor, offset: Filesize, len: Filesize) -> Errno #foreign "wasi_snapshot_preview1" "fd_allocate" ---
+fd_close              :: (fd: FileDescriptor) -> Errno #foreign "wasi_snapshot_preview1" "fd_close" ---
+fd_datasync           :: (fd: FileDescriptor) -> Errno #foreign "wasi_snapshot_preview1" "fd_datasync" ---
+fd_fdstat_get         :: (fd: FileDescriptor, stat: ^FDStat) -> Errno #foreign "wasi_snapshot_preview1" "fd_fdstat_get" ---
+fd_fdstat_set_flags   :: (fd: FileDescriptor, flags: FDFlags) -> Errno #foreign "wasi_snapshot_preview1" "fd_fdstat_set_flags" ---
+fd_fdstat_set_rights  :: (fd: FileDescriptor, rights_base: Rights, rights_inheriting: Rights) -> Errno #foreign "wasi_snapshot_preview1" "fd_fdstat_set_rights" ---
+fd_filestat_get       :: (fd: FileDescriptor, buf: ^FileStat) -> Errno #foreign "wasi_snapshot_preview1" "fd_filestat_get" ---
+fd_filestat_set_size  :: (fd: FileDescriptor, size: Filesize) -> Errno #foreign "wasi_snapshot_preview1" "fd_filestat_set_size" ---
+fd_filestat_set_times :: (fd: FileDescriptor, atim: Timestamp, mtim: Timestamp, fst_flags: FSTFlags) -> Errno #foreign "wasi_snapshot_preview1" "fd_filestat_set_times" ---
+fd_pread              :: (fd: FileDescriptor, iovs: IOVecArray, offset: Filesize, nread: ^Size) -> Errno #foreign "wasi_snapshot_preview1" "fd_pread" ---
+fd_prestat_get        :: (fd: FileDescriptor, buf: ^PrestatTagged) -> Errno #foreign "wasi_snapshot_preview1" "fd_prestat_get" ---
+fd_prestat_dir_name   :: (fd: FileDescriptor, path: str) -> Errno #foreign "wasi_snapshot_preview1" "fd_prestat_dir_name" ---
+fd_pwrite             :: (fd: FileDescriptor, iovs: IOVecArray, offset: Filesize, nwritten: ^Size) -> Errno #foreign "wasi_snapshot_preview1" "fd_pwrite" ---
+fd_read               :: (fd: FileDescriptor, iovs: IOVecArray, nread: ^Size) -> Errno #foreign "wasi_snapshot_preview1" "fd_read" ---
+fd_readdir            :: (fd: FileDescriptor, buf: ^u8, buf_len: Size, cookie: DirCookie, bufused: ^Size) -> Errno #foreign "wasi_snapshot_preview1" "fd_readdir" ---
+fd_renumber           :: (fd: FileDescriptor, to: FileDescriptor) -> Errno #foreign "wasi_snapshot_preview1" "fd_renumber" ---
+fd_seek               :: (fd: FileDescriptor, offset: FileDelta, whence: Whence, newoffset: ^Filesize) -> Errno #foreign "wasi_snapshot_preview1" "fd_seek" ---
+fd_sync               :: (fd: FileDescriptor) -> Errno #foreign "wasi_snapshot_preview1" "fd_sync" ---
+fd_tell               :: (fd: FileDescriptor, offset: ^Filesize) -> Errno #foreign "wasi_snapshot_preview1" "fd_tell" ---
+fd_write              :: (fd: FileDescriptor, iovs: IOVecArray, nwritten: ^Size) -> Errno #foreign "wasi_snapshot_preview1" "fd_write" ---
+
+path_create_directory   :: (fd: FileDescriptor, path: str) -> Errno #foreign "wasi_snapshot_preview1" "path_create_directory" ---
+path_filestat_get       :: (fd: FileDescriptor, flags: LookupFlags, path: str, buf: ^FileStat) -> Errno #foreign "wasi_snapshot_preview1" "path_filestat_get" ---
+path_filestat_set_times :: (fd: FileDescriptor, flags: LookupFlags, path: str, atim: Timestamp, mtim: Timestamp, fst_flags: FSTFlags) -> Errno #foreign "wasi_snapshot_preview1" "path_filestat_set_times" ---
+
+path_link :: (fd: FileDescriptor, old_flags: LookupFlags, old_path: str, new_fd: FileDescriptor, new_path: str) -> Errno #foreign "wasi_snapshot_preview1" "path_link" ---
+path_open :: (fd: FileDescriptor
+    , dirflags: LookupFlags
+    , path: str
+    , oflags: OFlags
+    , fs_rights_base: Rights
+    , fs_rights_inherting: Rights
+    , fdflags: FDFlags
+    , opened_fd: ^FileDescriptor
+    ) -> Errno
+     #foreign "wasi_snapshot_preview1" "path_open" ---
+
+path_readlink :: (fd: FileDescriptor, path: str, buf: ^u8, buf_len: Size, bufused: ^Size) -> Errno #foreign "wasi_snapshot_preview1" "path_readlink" ---
+path_remove_directory :: (fd: FileDescriptor, path: str) -> Errno #foreign "wasi_snapshot_preview1" "path_remove_directory" ---
+path_rename  :: (fd: FileDescriptor, old_path: str, new_fd: FileDescriptor, new_path: str) -> Errno #foreign "wasi_snapshot_preview1" "path_rename" ---
+path_symlink :: (old_path: ^u8, old_path_len: Size, fd: FileDescriptor, new_path: str) -> Errno #foreign "wasi_snapshot_preview1" "path_symlink" ---
+path_unlink_file :: (fd: FileDescriptor, path: str) -> Errno #foreign "wasi_snapshot_preview1" "path_unlink_file" ---
+
+poll_oneoff :: (in: ^Subscription, out: ^Event, nsubscriptions: Size, nevents: ^Size) -> Errno #foreign "wasi_snapshot_preview1" "poll_oneoff" ---
+
+proc_exit  :: (rval: ExitCode) -> void #foreign "wasi_snapshot_preview1" "proc_exit" ---
+proc_raise :: (sig: Signal) -> Errno #foreign "wasi_snapshot_preview1" "proc_raise" ---
+
+sched_yield :: () -> Errno #foreign "wasi_snapshot_preview1" "sched_yield" ---
+
+random_get :: (buf: ^u8, buf_len: Size) -> Errno #foreign "wasi_snapshot_preview1" "random_get" ---
+
+sock_recv     :: (fd: FileDescriptor, ri_data: IOVecArray, ri_flags: RIFlags, ro_datalen: ^Size, ro_flags: ^ROFlags) -> Errno #foreign "wasi_snapshot_preview1" "sock_recv" ---
+sock_send     :: (fd: FileDescriptor, si_data: IOVecArray, si_flags: SIFlags, so_datalen: ^Size) -> Errno #foreign "wasi_snapshot_preview1" "sock_send" ---
+sock_shutdown :: (fd: FileDescriptor, how: SDFlags) -> Errno #foreign "wasi_snapshot_preview1" "sock_shutdown" ---
+
+
+
index a1b42452cfec4c40aa49cde53b3fb4372a1213d1..aaf1875a34dd749217537844ce8cbf639d939bec 100644 (file)
@@ -17,5 +17,4 @@ main :: (args: [] cstr) {
     set.remove(^S, 5);
     for entry: S.entries do println(entry.value);
     println(set.has(^S, 5));
-
 }
\ No newline at end of file