Package ej.wasi

Class Wasi.WasiSnapshotPreview1

    • Constructor Detail

      • WasiSnapshotPreview1

        public WasiSnapshotPreview1()
    • Method Detail

      • args_sizes_get

        public static int args_sizes_get​(WasmModule module,
                                         int argsCountPtr,
                                         int argsDataSizePtr)
        Returns command-line argument data sizes, that is the number of arguments and the size of the argument string data.
        Parameters:
        module - the Wasm module instance
        argsCountPtr - pointer in WASM memory where the number of arguments is written
        argsDataSizePtr - pointer in WASM memory where the size of the argument string data is written
        Returns:
        an ERRNO
      • args_get

        public static int args_get​(WasmModule module,
                                   int argNamePtr,
                                   int argValuePtr)
        Reads command-line argument data. The size of the array should match that returned by args_sizes_get. Each argument is expected to be \0 terminated.
        Parameters:
        module - the Wasm module instance
        argNamePtr - pointer in WASM memory to the array of argument pointers
        argValuePtr - pointer in WASM memory to the buffer holding the argument data
        Returns:
        an ERRNO
      • environ_sizes_get

        public static int environ_sizes_get​(WasmModule module,
                                            int envCountPtr,
                                            int envDataSizePtr)
        Returns environment variable data sizes, that is the number of environment variable arguments and the size of the environment variable data.
        Parameters:
        module - the Wasm module instance
        envCountPtr - pointer in WASM memory where the number of environment variables is written
        envDataSizePtr - pointer in WASM memory where the size of the environment variable data is written
        Returns:
        an ERRNO
      • environ_get

        public static int environ_get​(WasmModule module,
                                      int envPtrsPtr,
                                      int envBufPtr)
        Reads environment variable data and writes the environment variable pointers and data into WASM linear memory. The sizes of the buffers should match that returned by environ_sizes_get. Key/value pairs are expected to be joined with =s, and terminated with \0s. The WASI convention: envPtrsPtr points to an array of int32 pointers, envBufPtr points to a contiguous buffer of null-terminated "KEY=VALUE" strings.
        Parameters:
        module - the Wasm module instance
        envPtrsPtr - pointer in WASM memory to the array of environment variable pointers
        envBufPtr - pointer in WASM memory to the buffer holding the environment variable data
        Returns:
        an ERRNO
      • proc_exit

        public static void proc_exit​(int exitcode)
        Terminates the process normally. An exit code of 0 indicates successful termination of the program. The meanings of other values is dependent on the environment.
        Parameters:
        exitcode - the exit code returned by the process
      • clock_time_get

        public static int clock_time_get​(WasmModule module,
                                         int clockid,
                                         long precision,
                                         int timePtr)
        Returns the time value of a clock. Note: This is similar to clock_gettime in POSIX.
        Parameters:
        module - the Wasm module instance
        clockid - the clock for which to return the time
        precision - the maximum lag (exclusive) that the returned time value may have, compared to its actual value
        timePtr - pointer in WASM memory where the time value of the clock is written, in nanoseconds
        Returns:
        an ERRNO
      • clock_res_get

        public static int clock_res_get​(WasmModule module,
                                        int clockid,
                                        int timeResolutionPtr)
        Returns the resolution of a clock. Implementations are required to provide a non-zero value for supported clocks. For unsupported clocks, errno::inval is returned. Note: This is similar to clock_getres in POSIX.
        Parameters:
        module - the Wasm module instance
        clockid - the clock for which to return the resolution
        timeResolutionPtr - pointer in WASM memory where the resolution of the clock is written, in nanoseconds
        Returns:
        an ERRNO
      • fd_close

        public static int fd_close​(WasmModule module,
                                   int fd)
        Closes a file descriptor. Note: This is similar to close in POSIX.
        Parameters:
        module - the Wasm module instance
        fd - the file descriptor to close
        Returns:
        an ERRNO
      • fd_read

        public static int fd_read​(WasmModule module,
                                  int fd,
                                  int iovecListPtr,
                                  int iovecLen,
                                  int nbReadPtr)
        Reads from a file descriptor. Note: This is similar to readv in POSIX.
        Parameters:
        module - the Wasm module instance
        fd - the file descriptor to read from
        iovecListPtr - pointer in WASM memory to the list of scatter/gather vectors to which to store data
        iovecLen - the length of the array pointed to by iovecListPtr
        nbReadPtr - pointer in WASM memory where the number of bytes read is written
        Returns:
        an ERRNO
      • fd_pread

        public static int fd_pread​(WasmModule module,
                                   int fd,
                                   int iovecListPtr,
                                   int iovecLen,
                                   long offset,
                                   int nbReadPtr)
        Reads from a file descriptor, without using and updating the file descriptor's offset. Note: This is similar to preadv in Linux (and other Unix-es).
        Parameters:
        module - the Wasm module instance
        fd - the file descriptor to read from
        iovecListPtr - pointer in WASM memory to the list of scatter/gather vectors in which to store data
        iovecLen - the length of the array pointed to by iovecListPtr
        offset - the offset within the file at which to read
        nbReadPtr - pointer in WASM memory where the number of bytes read is written
        Returns:
        an ERRNO
      • fd_readdir

        public static int fd_readdir​(WasmModule module,
                                     int dirfd,
                                     int bufPtr,
                                     int bufLen,
                                     long cookie,
                                     int resPtr)
        Reads directory entries from a directory. When successful, the contents of the output buffer consist of a sequence of directory entries. Each directory entry consists of a dirent object, followed by dirent::d_namlen bytes holding the name of the directory entry. This function fills the output buffer as much as possible, potentially truncating the last directory entry. This allows the caller to grow its read buffer size in case it's too small to fit a single large directory entry, or skip the oversized directory entry.
        Parameters:
        module - the Wasm module instance
        dirfd - the file descriptor of the directory to read
        bufPtr - pointer in WASM memory to the buffer where directory entries are stored
        bufLen - the length of the buffer at bufPtr
        cookie - the location within the directory to start reading
        resPtr - pointer in WASM memory where the number of bytes stored in the read buffer is written; if less than the size of the read buffer, the end of the directory has been reached
        Returns:
        an ERRNO
      • fd_write

        public static int fd_write​(WasmModule module,
                                   int fd,
                                   int iovecListPtr,
                                   int iovecLen,
                                   int nbWritePtr)
        Writes to a file descriptor. Note: This is similar to writev in POSIX. Like POSIX, any calls of write (and other functions to read or write) for a regular file by other threads in the WASI process should not be interleaved while write is executed.
        Parameters:
        module - the Wasm module instance
        fd - the file descriptor to write to
        iovecListPtr - pointer in WASM memory to the list of scatter/gather vectors from which to retrieve data
        iovecLen - the length of the array pointed to by iovecListPtr
        nbWritePtr - pointer in WASM memory where the number of bytes written is written
        Returns:
        an ERRNO
      • fd_pwrite

        public static int fd_pwrite​(WasmModule module,
                                    int fd,
                                    int iovecListPtr,
                                    int iovecLen,
                                    long offset,
                                    int nbWritePtr)
        Writes to a file descriptor, without using and updating the file descriptor's offset. Note: This is similar to pwritev in Linux (and other Unix-es). Like Linux (and other Unix-es), any calls of pwrite (and other functions to read or write) for a regular file by other threads in the WASI process should not be interleaved while pwrite is executed.
        Parameters:
        module - the Wasm module instance
        fd - the file descriptor to write to
        iovecListPtr - pointer in WASM memory to the list of scatter/gather vectors from which to retrieve data
        iovecLen - the length of the array pointed to by iovecListPtr
        offset - the offset within the file at which to write
        nbWritePtr - pointer in WASM memory where the number of bytes written is written
        Returns:
        an ERRNO
      • fd_datasync

        public static int fd_datasync​(int fd)
        Synchronizes the data of a file to disk. Note: This is similar to fdatasync in POSIX.
        Parameters:
        fd - the file descriptor to synchronize
        Returns:
        an ERRNO
      • fd_sync

        public static int fd_sync​(int fd)
        Synchronizes the data and metadata of a file to disk. Note: This is similar to fsync in POSIX.
        Parameters:
        fd - the file descriptor to synchronize
        Returns:
        an ERRNO
      • path_create_directory

        public static int path_create_directory​(WasmModule module,
                                                int fd,
                                                int path,
                                                int pathLen)
        Creates a directory. Note: This is similar to mkdirat in POSIX.
        Parameters:
        module - the Wasm module instance
        fd - the file descriptor of the directory the path is relative to
        path - pointer in WASM memory to the path at which to create the directory
        pathLen - the length of the path
        Returns:
        an ERRNO
      • fd_filestat_set_size

        public static int fd_filestat_set_size​(WasmModule module,
                                               int fd,
                                               long size)
        Adjusts the size of an open file. If this increases the file's size, the extra bytes are filled with zeros. Note: This is similar to ftruncate in POSIX.
        Parameters:
        module - the Wasm module instance
        fd - the file descriptor of the file to resize
        size - the desired file size
        Returns:
        an ERRNO
      • path_filestat_set_times

        public static int path_filestat_set_times​(WasmModule module,
                                                  int fd,
                                                  int flags,
                                                  int path,
                                                  int pathLen,
                                                  long accessedTime,
                                                  long modifiedTime,
                                                  int fstFlags)
        Adjusts the timestamps of a file or directory. Note: This is similar to utimensat in POSIX.
        Parameters:
        module - the Wasm module instance
        fd - the file descriptor of the directory the path is relative to
        flags - flags determining the method of how the path is resolved
        path - pointer in WASM memory to the path of the file or directory to operate on
        pathLen - the length of the path
        accessedTime - the desired values of the data access timestamp
        modifiedTime - the desired values of the data modification timestamp
        fstFlags - a bitmask indicating which timestamps to adjust
        Returns:
        an ERRNO
      • fd_seek

        public static int fd_seek​(WasmModule module,
                                  int fd,
                                  long offset,
                                  int whence,
                                  int newOffsetPtr)
        Moves the offset of a file descriptor. Note: This is similar to lseek in POSIX.
        Parameters:
        module - the Wasm module instance
        fd - the file descriptor to seek
        offset - the number of bytes to move
        whence - the base from which the offset is relative
        newOffsetPtr - pointer in WASM memory where the new offset of the file descriptor, relative to the start of the file, is written
        Returns:
        an ERRNO
      • fd_fdstat_get

        public static int fd_fdstat_get​(WasmModule module,
                                        int fd,
                                        int fdstatPtr)
        Gets the attributes of a file descriptor. Note: This returns similar flags to fcntl(fd, F_GETFL) in POSIX, as well as additional fields.
        Parameters:
        module - the Wasm module instance
        fd - the file descriptor to query
        fdstatPtr - pointer in WASM memory to the buffer where the file descriptor's attributes are stored
        Returns:
        an ERRNO
      • fd_fdstat_set_flags

        public static int fd_fdstat_set_flags​(WasmModule module,
                                              int fd,
                                              int fdflags)
        Adjusts the flags associated with a file descriptor. Note: This is similar to fcntl(fd, F_SETFL, flags) in POSIX.
        Parameters:
        module - the Wasm module instance
        fd - the file descriptor to configure
        fdflags - the desired values of the file descriptor flags
        Returns:
        an ERRNO
      • fd_prestat_get

        public static int fd_prestat_get​(WasmModule module,
                                         int fd,
                                         int prestatPtr)
        Returns a description of the given preopened file descriptor.
        Parameters:
        module - the Wasm module instance
        fd - the file descriptor to query
        prestatPtr - pointer in WASM memory to the buffer where the description is stored
        Returns:
        an ERRNO
      • fd_prestat_dir_name

        public static int fd_prestat_dir_name​(WasmModule module,
                                              int fd,
                                              int pathPtr,
                                              int pathLen)
        Returns a description of the given preopened file descriptor.
        Parameters:
        module - the Wasm module instance
        fd - the file descriptor to query
        pathPtr - pointer in WASM memory to a buffer into which to write the preopened directory name
        pathLen - the length of the buffer at pathPtr
        Returns:
        an ERRNO
      • sock_accept

        public static int sock_accept​(WasmModule module,
                                      int fd,
                                      int fdflags,
                                      int fdPtr)
        Accepts a new incoming connection. Note: This is similar to accept in POSIX.
        Parameters:
        module - the Wasm module instance
        fd - the listening socket
        fdflags - the desired values of the file descriptor flags
        fdPtr - pointer in WASM memory where the file descriptor of the new socket connection is written
        Returns:
        an ERRNO
      • sock_shutdown

        public static int sock_shutdown​(WasmModule module,
                                        int sockfd,
                                        int how)
        Shuts down socket send and receive channels. Note: This is similar to shutdown in POSIX.
        Parameters:
        module - the Wasm module instance
        sockfd - the file descriptor of the socket to shut down
        how - which channels on the socket to shut down
        Returns:
        an ERRNO
      • fd_tell

        public static int fd_tell​(WasmModule module,
                                  int fd,
                                  int offsetPtr)
        Returns the current offset of a file descriptor. Note: This is similar to lseek(fd, 0, SEEK_CUR) in POSIX.
        Parameters:
        module - the Wasm module instance
        fd - the file descriptor to query
        offsetPtr - pointer in WASM memory where the current offset of the file descriptor, relative to the start of the file, is written
        Returns:
        an ERRNO
      • fd_filestat_get

        public static int fd_filestat_get​(WasmModule module,
                                          int fd,
                                          int filestatPtr)
        Returns the attributes of an open file.
        Parameters:
        module - the Wasm module instance
        fd - the file descriptor to query
        filestatPtr - pointer in WASM memory to the buffer where the file's attributes are stored
        Returns:
        an ERRNO
      • path_unlink_file

        public static int path_unlink_file​(WasmModule module,
                                           int dirfd,
                                           int pathPtr,
                                           int pathLen)
        Unlinks a file. errno::isdir is returned if the path refers to a directory. Note: This is similar to unlinkat(fd, path, 0) in POSIX.
        Parameters:
        module - the Wasm module instance
        dirfd - the file descriptor of the directory the path is relative to
        pathPtr - pointer in WASM memory to the path to a file to unlink
        pathLen - the length of the path
        Returns:
        an ERRNO
      • path_remove_directory

        public static int path_remove_directory​(WasmModule module,
                                                int dirfd,
                                                int pathPtr,
                                                int pathLen)
        Removes a directory. errno::notempty is returned if the directory is not empty. Note: This is similar to unlinkat(fd, path, AT_REMOVEDIR) in POSIX.
        Parameters:
        module - the Wasm module instance
        dirfd - the file descriptor of the directory the path is relative to
        pathPtr - pointer in WASM memory to the path to a directory to remove
        pathLen - the length of the path
        Returns:
        an ERRNO
      • path_filestat_get

        public static int path_filestat_get​(WasmModule module,
                                            int dirfd,
                                            int flags,
                                            int pathPtr,
                                            int pathLen,
                                            int filestatPtr)
        Returns the attributes of a file or directory. Note: This is similar to stat in POSIX.
        Parameters:
        module - the Wasm module instance
        dirfd - the file descriptor of the directory the path is relative to
        flags - flags determining the method of how the path is resolved
        pathPtr - pointer in WASM memory to the path of the file or directory to inspect
        pathLen - the length of the path
        filestatPtr - pointer in WASM memory to the buffer where the file's attributes are stored
        Returns:
        an ERRNO
      • path_open

        public static int path_open​(WasmModule module,
                                    int dirfd,
                                    int dirflags,
                                    int pathPtr,
                                    int pathLen,
                                    int oflags,
                                    long fs_rights_base,
                                    long fs_rights_inheriting,
                                    int fdflags,
                                    int fdPtr)
        Opens a file or directory. The returned file descriptor is not guaranteed to be the lowest-numbered file descriptor not currently open; it is randomized to prevent applications from depending on making assumptions about indexes, since this is error-prone in multi-threaded contexts. The returned file descriptor is guaranteed to be less than 2**31. Note: This is similar to openat in POSIX.
        Parameters:
        module - the Wasm module instance
        dirfd - the file descriptor of the directory the path is relative to
        dirflags - flags determining the method of how the path is resolved
        pathPtr - pointer in WASM memory to the relative path of the file or directory to open, relative to the dirfd directory
        pathLen - the length of the path
        oflags - the method by which to open the file
        fs_rights_base - the initial rights of the newly created file descriptor; the implementation is allowed to return a file descriptor with fewer rights than specified, if and only if those rights do not apply to the type of file being opened; the base rights are the rights that will apply to operations using the file descriptor itself
        fs_rights_inheriting - the inheriting rights, that is the rights that apply to file descriptors derived from the new one
        fdflags - the desired values of the file descriptor flags
        fdPtr - pointer in WASM memory where the file descriptor of the file that has been opened is written
        Returns:
        an ERRNO
      • path_rename

        public static int path_rename​(WasmModule module,
                                      int oldFd,
                                      int oldPathPtr,
                                      int oldPathLen,
                                      int newFd,
                                      int newPathPtr,
                                      int newPathLen)
        Renames a file or directory. Note: This is similar to renameat in POSIX.
        Parameters:
        module - the Wasm module instance
        oldFd - the file descriptor of the directory the source path is relative to
        oldPathPtr - pointer in WASM memory to the source path of the file or directory to rename
        oldPathLen - the length of the source path
        newFd - the working directory at which the resolution of the new path starts
        newPathPtr - pointer in WASM memory to the destination path to which to rename the file or directory
        newPathLen - the length of the destination path
        Returns:
        WasiConstants.ERRNO_SUCCESS on success, or the appropriate errno value on error
      • sock_recv

        public static int sock_recv​(WasmModule module,
                                    int fd,
                                    int iovecListPtr,
                                    int iovecLen,
                                    int riFlags,
                                    int nbRecvPtr,
                                    int roFlagsPtr)
        Receives a message from a socket. Note: This is similar to recv in POSIX, though it also supports reading the data into multiple buffers in the manner of readv.
        Parameters:
        module - the Wasm module instance
        fd - the file descriptor of the socket to receive from
        iovecListPtr - pointer in WASM memory to the list of scatter/gather vectors to which to store data
        iovecLen - the length of the array pointed to by iovecListPtr
        riFlags - message flags
        nbRecvPtr - pointer in WASM memory where the number of bytes stored in the vectors is written
        roFlagsPtr - pointer in WASM memory where the message flags of the received message are written
        Returns:
        an ERRNO
      • sock_send

        public static int sock_send​(WasmModule module,
                                    int fd,
                                    int iovecListPtr,
                                    int iovecLen,
                                    int siFlags,
                                    int nbSendPtr)
        Sends a message on a socket. Note: This is similar to send in POSIX, though it also supports writing the data from multiple buffers in the manner of writev.
        Parameters:
        module - the Wasm module instance
        fd - the file descriptor of the socket to send on
        iovecListPtr - pointer in WASM memory to the list of scatter/gather vectors from which to retrieve data
        iovecLen - the length of the array pointed to by iovecListPtr
        siFlags - message flags
        nbSendPtr - pointer in WASM memory where the number of bytes transmitted is written
        Returns:
        an ERRNO
      • poll_oneoff

        public static int poll_oneoff​(WasmModule module,
                                      int subscriptionEventPtr,
                                      int outEventPtr,
                                      int nbSubscription,
                                      int nbOccuredEventPtr)
        Concurrently polls for the occurrence of a set of events. If nbSubscription is 0, errno::inval is returned.
        Parameters:
        module - the Wasm module instance
        subscriptionEventPtr - pointer in WASM memory to the events to which to subscribe
        outEventPtr - pointer in WASM memory where the events that have occurred are written
        nbSubscription - both the number of subscriptions and events
        nbOccuredEventPtr - pointer in WASM memory where the number of events stored is written
        Returns:
        an ERRNO
      • recvfrom_ext

        public static int recvfrom_ext​(WasmModule module,
                                       int fd,
                                       int buffPtr,
                                       int buffLen,
                                       int srcAddrPtr,
                                       int srcAddrLenPtr,
                                       int srcPortPtr,
                                       int nonblocking,
                                       int nbReadPtr)
        Receives data from a socket, optionally returning the sender's address.

        Uses the status-returning convention: returns WasiConstants.ERRNO_SUCCESS on success, or a positive WASI errno on error.

        Supports both TCP (WasiSocket) and UDP (WasiDatagramSocket). For UDP, the sender's IP and port are written back to WASM memory so the C side can reconstruct struct sockaddr.

        Parameters:
        module - the Wasm module instance
        fd - the socket file descriptor
        buffPtr - pointer to the receive buffer in WASM memory
        buffLen - the size of the receive buffer
        srcAddrPtr - pointer to write the sender IP bytes (UDP)
        srcAddrLenPtr - pointer to write the sender address length (UDP)
        srcPortPtr - pointer to write the sender port (UDP)
        nonblocking - 1 for non-blocking receive, 0 for blocking
        nbReadPtr - pointer to write the number of bytes read
        Returns:
        ERRNO_SUCCESS, ERRNO_BADF, ERRNO_NOTSOCK, ERRNO_AGAIN, ERRNO_FAULT, or ERRNO_IO
      • sendto_ext

        public static int sendto_ext​(WasmModule module,
                                     int fd,
                                     int buffPtr,
                                     int buffLen,
                                     int dstAddrPtr,
                                     int dstAddrLen,
                                     int dstPort,
                                     int nonblocking,
                                     int nbsentPtr)
        Sends data through a socket, optionally to a specific destination.

        Uses the status-returning convention: returns WasiConstants.ERRNO_SUCCESS on success, or a positive WASI errno on error.

        Supports both TCP (WasiSocket) and UDP (WasiDatagramSocket). For UDP, the destination address is specified via raw IP bytes and port, already parsed by the C side from struct sockaddr.

        Parameters:
        module - the Wasm module instance
        fd - the socket file descriptor
        buffPtr - pointer to the send buffer in WASM memory
        buffLen - the number of bytes to send
        dstAddrPtr - pointer to the destination IP bytes (UDP)
        dstAddrLen - the length of the destination address (UDP)
        dstPort - the destination port (UDP)
        nonblocking - 1 for non-blocking send, 0 for blocking
        nbsentPtr - pointer to write the number of bytes sent
        Returns:
        ERRNO_SUCCESS, ERRNO_BADF, ERRNO_NOTSOCK, ERRNO_FAULT, ERRNO_MSGSIZE, or ERRNO_IO
      • random_get

        public static int random_get​(WasmModule module,
                                     int ptr,
                                     int len)
        Writes high-quality random data into a buffer. This function blocks when the implementation is unable to immediately provide sufficient high-quality random data.
        Parameters:
        module - the Wasm module instance
        ptr - pointer in WASM memory to the buffer to fill with random data
        len - the length of the buffer at ptr
        Returns:
        an ERRNO
      • socket

        public static int socket​(WasmModule module,
                                 int domain,
                                 int type,
                                 int protocol)
        Creates a new socket of the specified domain, type, and protocol.

        Uses the fd-returning convention: returns the new file descriptor on success (>= 0), or a negative WASI errno on error. The C wrapper negates the return value to extract errno and returns -1. This convention is shared with dup(ej.lang.WasmModule, int) and dup2(ej.lang.WasmModule, int, int), which also return file descriptors.

        Linux-style flags (SOCK_NONBLOCK, SOCK_CLOEXEC) OR'd into type are stripped and handled here. SOCK_CLOEXEC is silently ignored since WASI has no fork/exec.

        Parameters:
        module - the Wasm module instance
        domain - the address family (AF_INET or AF_INET6)
        type - the socket type (SOCK_STREAM or SOCK_DGRAM), optionally OR'd with SOCK_NONBLOCK or SOCK_CLOEXEC
        protocol - the protocol number (must be 0)
        Returns:
        the new file descriptor, or:
        • -ERRNO_AFNOSUPPORT if the address family is not supported
        • -ERRNO_NOTSUP if the protocol is not supported
        • -ERRNO_INVAL if the socket type is unknown
        • -ERRNO_IO on I/O error setting non-blocking mode
      • getpeername_ext

        public static int getpeername_ext​(WasmModule module,
                                          int sockfd,
                                          int addrPtr,
                                          int addrLenPtr,
                                          int portPtr)
        Retrieves the remote address of a connected socket.

        Uses the status-returning convention: returns WasiConstants.ERRNO_SUCCESS on success, or a positive WASI errno on error.

        Parameters:
        module - the Wasm module instance
        sockfd - the socket file descriptor
        addrPtr - pointer in WASM memory to write the IP address bytes
        addrLenPtr - pointer in WASM memory to write the address length
        portPtr - pointer in WASM memory to write the port number
        Returns:
        ERRNO_SUCCESS, ERRNO_BADF, ERRNO_NOTSOCK, ERRNO_NOTCONN, or ERRNO_NOTSUP
      • setsockopt

        public static int setsockopt​(WasmModule module,
                                     int sockfd,
                                     int level,
                                     int optname,
                                     int optvalPtr,
                                     int optlen)
        Sets a socket option.

        Uses the status-returning convention: returns WasiConstants.ERRNO_SUCCESS (0) on success, or a positive WASI errno on error. The C wrapper translates non-zero returns into errno and returns -1.

        Currently SO_REUSEADDR, SO_RCVBUF, SO_SNDBUF and SO_BROADCAST at SOL_SOCKET level are implemented, as well as the multicast options at IPPROTO_IP and IPPROTO_IPV6 level. Unsupported options and levels return ERRNO_NOSYS, except at IPPROTO_IP and IPPROTO_IPV6 level on a non-datagram socket, where the call is a no-op reporting ERRNO_SUCCESS. That no-op is there because C libraries commonly use the pattern if (setsockopt(...)) return -errno;, which would surface errors for options we simply do not implement yet.

        Parameters:
        module - the Wasm module instance
        sockfd - the socket file descriptor
        level - the protocol level (e.g. SOL_SOCKET)
        optname - the option name (e.g. SO_REUSEADDR)
        optvalPtr - pointer to the option value in WASM linear memory
        optlen - the size of the option value in bytes
        Returns:
        ERRNO_SUCCESS, or:
        • ERRNO_BADF if the file descriptor is invalid
        • ERRNO_NOTSOCK if the file descriptor is not a socket
        • ERRNO_INVAL if the option length is invalid
        • ERRNO_NOSYS if the option or the level is not supported
        • ERRNO_IO on I/O error
      • getsockopt

        public static int getsockopt​(WasmModule module,
                                     int sockfd,
                                     int level,
                                     int optname,
                                     int optvalPtr,
                                     int optlenPtr)
        Gets a socket option.

        Uses the status-returning convention: returns WasiConstants.ERRNO_SUCCESS (0) on success, or a positive WASI errno on error.

        Currently supports SO_BROADCAST, SO_RCVBUF and SO_SNDBUF; any other option returns ERRNO_NOSYS. The level argument is ignored by this implementation. wasi-libc's own getsockopt() only handles SO_TYPE via fd_fdstat_get; this import covers the remaining queries, including the buffer-size ones, from C code.

        Parameters:
        module - the Wasm module instance
        sockfd - the socket file descriptor
        level - the protocol level (e.g. SOL_SOCKET), ignored by this implementation
        optname - the option name (e.g. SO_RCVBUF)
        optvalPtr - pointer to write the option value in WASM memory
        optlenPtr - pointer to write the option length in WASM memory
        Returns:
        ERRNO_SUCCESS, or a positive WASI errno
      • bind_ext

        public static int bind_ext​(WasmModule module,
                                   int sockfd,
                                   int addrPtr,
                                   int addrlen,
                                   int port)
        Binds a socket to a local address.

        Uses the status-returning convention: returns WasiConstants.ERRNO_SUCCESS on success, or a positive WASI errno on error.

        For TCP sockets (WasiSocket), binding promotes the socket to a WasiServerSocket so it can later accept connections.

        Parameters:
        module - the Wasm module instance
        sockfd - the socket file descriptor
        addrPtr - pointer to the raw IP address bytes in WASM memory
        addrlen - the length of the address (4 for IPv4, 16 for IPv6)
        port - the port number to bind to
        Returns:
        ERRNO_SUCCESS, ERRNO_BADF, ERRNO_NOTSOCK, ERRNO_INVAL, ERRNO_FAULT, ERRNO_ADDRINUSE, or ERRNO_IO
      • listen

        public static int listen​(WasmModule module,
                                 int sockfd,
                                 int backlog)
        Marks a socket as listening for incoming connections.

        Uses the status-returning convention: returns WasiConstants.ERRNO_SUCCESS on success, or a positive WASI errno on error.

        If the socket is not yet bound, it is auto-promoted to a WasiServerSocket bound to 0.0.0.0:0 (ephemeral port), matching POSIX behavior for listen on an unbound socket.

        Parameters:
        module - the Wasm module instance
        sockfd - the socket file descriptor
        backlog - the maximum length of the pending connection queue
        Returns:
        ERRNO_SUCCESS, ERRNO_INVAL, or ERRNO_IO
      • connect_ext

        public static int connect_ext​(WasmModule module,
                                      int sockfd,
                                      int addrPtr,
                                      int addrlen,
                                      int port)
        Connects a socket to a remote address.

        Uses the status-returning convention: returns WasiConstants.ERRNO_SUCCESS on success, or a positive WASI errno on error.

        Parameters:
        module - the Wasm module instance
        sockfd - the socket file descriptor
        addrPtr - pointer to the raw IP address bytes in WASM memory
        addrlen - the length of the address (4 for IPv4, 16 for IPv6)
        port - the remote port number
        Returns:
        ERRNO_SUCCESS, ERRNO_BADF, ERRNO_NOTSOCK, ERRNO_INVAL, ERRNO_FAULT, ERRNO_CONNREFUSED, or ERRNO_IO
      • pipe

        public static int pipe​(WasmModule module,
                               int readFdPtr,
                               int writeFdPtr)
        Creates a unidirectional pipe and writes the two file descriptors into WASM memory.

        Uses the status-returning convention: returns WasiConstants.ERRNO_SUCCESS (0) on success, or a positive WASI errno on error. The C wrapper takes a standard int fds[2] array and passes the address of each element as separate pointers.

        Parameters:
        module - the Wasm module instance
        readFdPtr - pointer in WASM memory where the read-end fd is written
        writeFdPtr - pointer in WASM memory where the write-end fd is written
        Returns:
        ERRNO_SUCCESS or ERRNO_IO
      • dup

        public static int dup​(WasmModule module,
                              int fd)
        Duplicates a file descriptor.

        Uses the fd-returning convention: returns the new file descriptor on success (>= 0), or a negative WASI errno on error. The C wrapper negates the return value to extract errno and returns -1. This convention is necessary because the return value must distinguish a valid fd (>= 0) from an error.

        Parameters:
        module - the Wasm module instance
        fd - the file descriptor to duplicate
        Returns:
        the new file descriptor, or -ERRNO_BADF
      • dup2

        public static int dup2​(WasmModule module,
                               int oldFd,
                               int newFd)
        Duplicates a file descriptor to a specific target descriptor.

        Uses the fd-returning convention: returns newFd on success (>= 0), or a negative WASI errno on error. The C wrapper negates the return value to extract errno and returns -1. This convention is necessary because the return value must distinguish a valid fd (>= 0) from an error.

        If newFd is already open, it is closed first. If oldFd == newFd or both refer to the same I/O object, newFd is returned as-is.

        Parameters:
        module - the Wasm module instance
        oldFd - the source file descriptor
        newFd - the target file descriptor
        Returns:
        newFd on success, or:
        • -ERRNO_BADF if oldFd or newFd is invalid
        • -ERRNO_IO if closing newFd fails
      • fd_renumber

        public static int fd_renumber​(WasmModule module,
                                      int fd,
                                      int to)
        Atomically replaces a file descriptor by renumbering another file descriptor. Due to the strong focus on thread safety, this environment does not provide a mechanism to duplicate or renumber a file descriptor to an arbitrary number, like dup2(). This would be prone to race conditions, as an actual file descriptor with the same number could be allocated by a different thread at the same time. This function provides a way to atomically renumber file descriptors, which would disappear if dup2() were to be removed entirely. After the call, fd is invalidated and to refers to what fd previously referenced. If to was open, it is closed first.
        Parameters:
        module - the Wasm module instance
        fd - the source file descriptor to renumber (will be invalidated)
        to - the target file descriptor to overwrite (will be replaced)
        Returns:
        WasiConstants.ERRNO_SUCCESS on success, or the appropriate errno value on error
      • getfilepath

        public static int getfilepath​(WasmModule module,
                                      int fd,
                                      int buf,
                                      int bufLen)
        Gets the path of the file or directory identified by the given file descriptor fd.
        Parameters:
        module - the Wasm module instance
        fd - the file descriptor to query
        buf - the pointer in WASM memory to the buffer where the path is written
        bufLen - the length of the buffer at buf
        Returns:
        WasiConstants.ERRNO_SUCCESS on success, or the appropriate errno value on error, one of ERRNO_BADF, ERRNO_NOENT, ERRNO_NAMETOOLONG or ERRNO_IO
      • sched_yield

        public static int sched_yield()
        Temporarily yields execution of the calling thread. Note: This is similar to sched_yield in POSIX.
        Returns:
        an ERRNO
      • path_readlink

        public static int path_readlink​(int dir_fd,
                                        int path,
                                        int path_len,
                                        int buf,
                                        int buf_len,
                                        int buf_used)
        Reads the contents of a symbolic link. Note: This is similar to readlinkat in POSIX.
        Parameters:
        dir_fd - the file descriptor of the directory the path is relative to
        path - pointer in WASM memory to the path of the symbolic link from which to read
        path_len - the length of the path
        buf - pointer in WASM memory to the buffer to which to write the contents of the symbolic link
        buf_len - the length of the buffer at buf
        buf_used - pointer in WASM memory where the number of bytes placed in the buffer is written
        Returns:
        an ERRNO
      • path_link

        public static int path_link​(int oldFd,
                                    int oldFlags,
                                    int oldPathPtr,
                                    int oldPathLen,
                                    int newFd,
                                    int newPathPtr,
                                    int newPathLen)
        Creates a hard link. Note: This is similar to linkat in POSIX.
        Parameters:
        oldFd - the file descriptor of the directory containing the source path, to which the source path is relative
        oldFlags - flags determining the method of how the path is resolved
        oldPathPtr - pointer in WASM linear memory to the source path string from which to link
        oldPathLen - the length of the source path string
        newFd - the file descriptor of the directory containing the destination path, the working directory at which the resolution of the new path starts
        newPathPtr - pointer in WASM linear memory to the destination path string at which to create the hard link
        newPathLen - the length of the destination path string
        Returns:
        an ERRNO
      • path_symlink

        public static int path_symlink​(int oldPathPtr,
                                       int oldPathLen,
                                       int dirFd,
                                       int newPathPtr,
                                       int newPathLen)
        Creates a symbolic link. Note: This is similar to symlinkat in POSIX.
        Parameters:
        oldPathPtr - pointer in WASM memory to the content of the symbolic link (the target path)
        oldPathLen - the length of the contents of the symbolic link, the target path string
        dirFd - the file descriptor of the directory containing the new link, the working directory at which the resolution of the new path starts
        newPathPtr - pointer in WASM memory to the destination path at which to create the symbolic link
        newPathLen - the length of the destination path string
        Returns:
        an ERRNO
      • gethostname_ext

        public static int gethostname_ext​(WasmModule module,
                                          int nameOffset,
                                          int namelen)
        Retrieves the local hostname.

        Uses the status-returning convention: returns WasiConstants.ERRNO_SUCCESS on success, or a positive WASI errno on error.

        Parameters:
        module - the Wasm module instance
        nameOffset - pointer in WASM memory to write the hostname
        namelen - the size of the buffer
        Returns:
        ERRNO_SUCCESS, ERRNO_NAMETOOLONG, ERRNO_FAULT, or ERRNO_IO
      • getsockname_ext

        public static int getsockname_ext​(WasmModule module,
                                          int sockfd,
                                          int addrPtr,
                                          int addrLenPtr,
                                          int portPtr)
        Retrieves the local address of a socket.

        Uses the status-returning convention: returns WasiConstants.ERRNO_SUCCESS on success, or a positive WASI errno on error.

        Parameters:
        module - the Wasm module instance
        sockfd - the socket file descriptor
        addrPtr - pointer in WASM memory to write the IP address bytes
        addrLenPtr - pointer in WASM memory to write the address length
        portPtr - pointer in WASM memory to write the port number
        Returns:
        ERRNO_SUCCESS, ERRNO_BADF, ERRNO_NOTSOCK, ERRNO_NOTCONN, or ERRNO_NOTSUP
      • getaddrinfo_nb_ext

        public static int getaddrinfo_nb_ext​(WasmModule module,
                                             int nodeNamePtr,
                                             int nodeNameLen,
                                             int ipv4Family,
                                             int ipv6Family,
                                             int passive,
                                             int nbAddrPtr)
        Returns the number of addresses matching a DNS query.

        Uses the status-returning convention: returns WasiConstants.ERRNO_SUCCESS on success, or a positive WASI errno on error. Called first to determine how many results to iterate with getaddrinfo_ext(ej.lang.WasmModule, int, int, int, int, int, int, int, int, int, int, int).

        Parameters:
        module - the Wasm module instance
        nodeNamePtr - pointer to the hostname string in WASM memory
        nodeNameLen - the length of the hostname string
        ipv4Family - 1 to include IPv4 results, 0 to exclude
        ipv6Family - 1 to include IPv6 results, 0 to exclude
        passive - 1 for passive (bind) addresses, 0 for active (connect)
        nbAddrPtr - pointer to write the result count
        Returns:
        ERRNO_SUCCESS, ERRNO_INVAL, or ERRNO_HOSTUNREACH
      • getaddrinfo_ext

        public static int getaddrinfo_ext​(WasmModule module,
                                          int index,
                                          int nodeNamePtr,
                                          int nodeNameLen,
                                          int ipv4Family,
                                          int ipv6Family,
                                          int passive,
                                          int canonical,
                                          int canonicalNnamePtr,
                                          int canonicalNameLenPtr,
                                          int addrPtr,
                                          int addrLenPtr)
        Retrieves a single resolved address by index.

        Uses the status-returning convention: returns WasiConstants.ERRNO_SUCCESS on success, or a positive WASI errno on error. Call getaddrinfo_nb_ext(ej.lang.WasmModule, int, int, int, int, int, int) first to determine the number of results, then iterate with this function.

        Parameters:
        module - the Wasm module instance
        index - the 0-based index of the address to retrieve
        nodeNamePtr - pointer to the hostname string in WASM memory
        nodeNameLen - the length of the hostname string
        ipv4Family - 1 to include IPv4 results
        ipv6Family - 1 to include IPv6 results
        passive - 1 for passive addresses
        canonical - 1 to request the canonical name
        canonicalNnamePtr - pointer to write the canonical name bytes
        canonicalNameLenPtr - pointer to read and write the canonical name buffer length
        addrPtr - pointer to write the IP address bytes
        addrLenPtr - pointer to write the address length
        Returns:
        ERRNO_SUCCESS, ERRNO_INVAL, ERRNO_HOSTUNREACH, or ERRNO_NAMETOOLONG
      • getifaddrs_count_ext

        public static int getifaddrs_count_ext​(WasmModule module,
                                               int countPtr)
        Returns the number of (interface, address) pairs available. Status-returning: 0 on success, positive WASI errno on error.
        Parameters:
        module - the Wasm module instance
        countPtr - pointer in WASM memory where the number of pairs is written
        Returns:
        an ERRNO
      • getifaddrs_get_ext

        public static int getifaddrs_get_ext​(WasmModule module,
                                             int index,
                                             int namePtr,
                                             int nameLen,
                                             int flagsPtr,
                                             int familyPtr,
                                             int addrPtr,
                                             int addrLenPtr,
                                             int prefixLenPtr)
        Retrieves the (interface, address) pair at the given index. Status-returning: 0 on success, positive WASI errno on error.

        Writes to WASM memory:

        • namePtr: interface name bytes + null terminator
        • flagsPtr: IFF_UP|IFF_RUNNING (always), |IFF_LOOPBACK if loopback
        • familyPtr: AF_INET (1) or AF_INET6 (2), WASI values matching WasiConstants
        • addrPtr: IP address bytes (4 for v4, 16 for v6)
        • addrLenPtr: address byte length
        • prefixLenPtr: CIDR prefix length
        Parameters:
        module - the Wasm module instance
        index - the 0-based index of the pair to retrieve
        namePtr - pointer in WASM memory where the interface name is written
        nameLen - the length of the buffer at namePtr
        flagsPtr - pointer in WASM memory where the interface flags are written
        familyPtr - pointer in WASM memory where the address family is written
        addrPtr - pointer in WASM memory where the IP address bytes are written
        addrLenPtr - pointer in WASM memory where the length of the IP address is written
        prefixLenPtr - pointer in WASM memory where the CIDR prefix length is written
        Returns:
        an ERRNO