import { Result, SnapshotPreview1 } from "./snapshot-preview1.js"; import { WASIExecutionResult } from "../types.js"; import { WASIContext, WASIContextOptions } from "./wasi-context.js"; import { WASIDrive } from "./wasi-drive.js"; /** Injects a function between implementation and return for debugging */ export type DebugFn = (name: string, args: string[], ret: number, data: { [key: string]: any; }[]) => number | undefined; export declare class InvalidInstanceError extends Error { } export declare class InitializationError extends Error { } /** * Implementation of a WASI runner for the browser. * Explicitly designed for the browser context, where system resources * must all be emulated. This WASI implementation relies on configuration and * callbacks to decide on what system resources are available and defaults * to providing none. * * This implementation adapted from cloudflare/workers-wasi * https://github.com/cloudflare/workers-wasi/blob/main/src/index.ts * */ export declare class WASI implements SnapshotPreview1 { instance: WebAssembly.Instance; module: WebAssembly.Module; memory: WebAssembly.Memory; context: WASIContext; drive: WASIDrive; hasBeenInitialized: boolean; /** * Start a WASI command. * */ static start(wasmSource: Response | PromiseLike, context?: Partial): Promise; /** * Initialize a WASI reactor. * * Returns the WebAssembly instance exports. */ static initialize(wasmSource: Response | PromiseLike, context?: Partial): Promise; constructor(context: Partial); getImportObject(): { wasi_snapshot_preview1: WebAssembly.ModuleImports & SnapshotPreview1; wasi_unstable: WebAssembly.ModuleImports & SnapshotPreview1; }; /** * Start a WASI command. * * See: https://github.com/WebAssembly/WASI/blob/main/legacy/application-abi.md */ start(wasm: WebAssembly.WebAssemblyInstantiatedSource, options?: { memory?: WebAssembly.Memory; }): WASIExecutionResult; /** * Initialize a WASI Reactor. * * See: https://github.com/WebAssembly/WASI/blob/main/legacy/application-abi.md */ initialize(wasm: WebAssembly.WebAssemblyInstantiatedSource, options?: { memory?: WebAssembly.Memory; }): void; getImports(version: "unstable" | "preview1", debug?: DebugFn): WebAssembly.ModuleImports & SnapshotPreview1; get envArray(): Array; /** * Read 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. */ args_get(argv_ptr: number, argv_buf_ptr: number): number; /** * Return command-line argument data sizes. */ args_sizes_get(argc_ptr: number, argv_buf_size_ptr: number): number; /** * Return the resolution of a clock. Implementations are required to provide a * non-zero value for supported clocks. For unsupported clocks, return * errno::inval. Note: This is similar to clock_getres in POSIX. */ clock_res_get(id: number, retptr0: number): number; /** * Return the time value of a clock. * Note: This is similar to clock_gettime in POSIX. */ clock_time_get(id: number, _: bigint, retptr0: number): number; /** * Read environment variable data. 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. */ environ_get(env_ptr_ptr: number, env_buf_ptr: number): number; /** * Return environment variable data sizes. */ environ_sizes_get(env_ptr: number, env_buf_size_ptr: number): number; /** * Terminate the process normally. An exit code of 0 indicates successful * termination of the program. The meanings of other values is dependent on * the environment. */ proc_exit(code: number): void; /** * Write high-quality random data into a buffer. This function blocks when the * implementation is unable to immediately provide sufficient high-quality * random data. This function may execute slowly, so when large mounts of * random data are required, it's advisable to use this function to seed a * pseudo-random number generator, rather than to provide the random data * directly. */ random_get(buffer_ptr: number, buffer_len: number): number; /** * Temporarily yield execution of the calling thread. * Note: This is similar to sched_yield in POSIX. */ sched_yield(): number; /** * Read from a file descriptor. Note: This is similar to readv in POSIX. */ fd_read(fd: number, iovs_ptr: number, iovs_len: number, retptr0: number): number; /** * Write to a file descriptor. Note: This is similar to writev in POSIX. */ fd_write(fd: number, ciovs_ptr: number, ciovs_len: number, retptr0: number): number; /** * Provide file advisory information on a file descriptor. * Note: This is similar to posix_fadvise in POSIX. */ fd_advise(): number; /** * Force the allocation of space in a file. * Note: This is similar to posix_fallocate in POSIX. */ fd_allocate(fd: number, offset: bigint, length: bigint): number; /** * Close a file descriptor. * Note: This is similar to close in POSIX. * * @param fd */ fd_close(fd: number): Result; /** * Synchronize the data of a file to disk. * Note: This is similar to fdatasync in POSIX. * * @param fd */ fd_datasync(fd: number): Result; /** * Get the attributes of a file descriptor. * Note: This returns similar flags to fsync(fd, F_GETFL) in POSIX, * as well as additional fields. * * Returns fdstat - the buffer where the file descriptor's attributes * are stored. * * @returns Result */ fd_fdstat_get(fd: number, retptr0: number): number; /** * Adjust the flags associated with a file descriptor. * Note: This is similar to fcntl(fd, F_SETFL, flags) in POSIX. */ fd_fdstat_set_flags(fd: number, flags: number): number; /** * Adjust the rights associated with a file descriptor. This can only be used * to remove rights, and returns errno::notcapable if called in a way that * would attempt to add rights */ fd_fdstat_set_rights(): Result; /** * Return the attributes of an open file. */ fd_filestat_get(fd: number, retptr0: number): number; /** * Return the attributes of an open file. * This version is used */ unstable_fd_filestat_get(fd: number, retptr0: number): number; /** * Return the attributes of an open file. */ shared_fd_filestat_get(fd: number, retptr0: number, version: "unstable" | "preview1"): number; /** * Adjust 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. */ fd_filestat_set_size(fd: number, size: bigint): number; /** * Adjust the timestamps of an open file or directory. * Note: This is similar to futimens in POSIX. */ fd_filestat_set_times(fd: number, atim: bigint, mtim: bigint, fst_flags: number): number; /** * Read from a file descriptor, without using and updating the file * descriptor's offset. Note: This is similar to preadv in POSIX. */ fd_pread(fd: number, iovs_ptr: number, iovs_len: number, offset: bigint, retptr0: number): number; /** * Return a description of the given preopened file descriptor. */ fd_prestat_dir_name(fd: number, path_ptr: number, path_len: number): number; /** * Return a description of the given preopened file descriptor. */ fd_prestat_get(fd: number, retptr0: number): number; /** * Write to a file descriptor, without using and updating the file * descriptor's offset. Note: This is similar to pwritev in POSIX. */ fd_pwrite(fd: number, ciovs_ptr: number, ciovs_len: number, offset: bigint, retptr0: number): number; /** * Read 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. */ fd_readdir(fd: number, buf: number, buf_len: number, cookie: bigint, retptr0: number): number; /** * Atomically replace 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. */ fd_renumber(old_fd: number, new_fd: number): number; /** * Move the offset of a file descriptor. * * The offset is specified as a bigint here * Note: This is similar to lseek in POSIX. * * The offset, and return type are FileSize (u64) which is represented by * bigint in JavaScript. */ fd_seek(fd: number, offset: bigint, whence: number, retptr0: number): Result; unstable_fd_seek(fd: number, offset: bigint, whence: number, retptr0: number): Result; /** * Synchronize the data and metadata of a file to disk. * Note: This is similar to fsync in POSIX. */ fd_sync(fd: number): Result; /** * Return the current offset of a file descriptor. * Note: This is similar to lseek(fd, 0, SEEK_CUR) in POSIX. * * The return type is FileSize (u64) which is represented by bigint in JS. * */ fd_tell(fd: number, retptr0: number): number; path_filestat_get(fd: number, flags: number, path_ptr: number, path_len: number, retptr0: number): number; unstable_path_filestat_get(fd: number, flags: number, path_ptr: number, path_len: number, retptr0: number): number; /** * Return the attributes of a file or directory. * Note: This is similar to stat in POSIX. */ shared_path_filestat_get(fd: number, _: number, path_ptr: number, path_len: number, retptr0: number, version: "unstable" | "preview1"): number; /** * Adjust the timestamps of a file or directory. * Note: This is similar to utimensat in POSIX. */ path_filestat_set_times(fd: number, _: number, // Runno doesn't support links path_ptr: number, path_len: number, atim: bigint, mtim: bigint, fst_flags: number): number; /** * Open 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. * @param fd: fd * @param dirflags: lookupflags Flags determining the method of how the path * is resolved. Not supported by Runno (symlinks) * @param path: string The relative path of the file or directory to open, * relative to the path_open::fd directory. * @param oflags: oflags The method by which to open the file. * @param fs_rights_base: rights 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 rights * that will apply to operations using the file * descriptor itself, while the inheriting rights are * rights that apply to file descriptors derived from * it. * @param fs_rights_inheriting: rights * @param fdflags: fdflags * */ path_open(fd: number, _: number, path_ptr: number, path_len: number, oflags: number, rights_base: bigint, rights_inheriting: bigint, fdflags: number, retptr0: number): number; /** * Rename a file or directory. Note: This is similar to renameat in POSIX. */ path_rename(old_fd_dir: number, old_path_ptr: number, old_path_len: number, new_fd_dir: number, new_path_ptr: number, new_path_len: number): number; /** * Unlink a file. Return errno::isdir if the path refers to a directory. * Note: This is similar to unlinkat(fd, path, 0) in POSIX. */ path_unlink_file(fd: number, path_ptr: number, path_len: number): number; /** * Concurrently poll for the occurrence of a set of events. */ poll_oneoff(in_ptr: number, out_ptr: number, nsubscriptions: number, retptr0: number): number; /** * Create a directory. Note: This is similar to mkdirat in POSIX. */ path_create_directory(fd: number, path_ptr: number, path_len: number): number; /** * Create a hard link. Note: This is similar to linkat in POSIX. */ path_link(): Result; /** * Read the contents of a symbolic link. * Note: This is similar to readlinkat in POSIX. */ path_readlink(): Result; /** * Remove a directory. Return errno::notempty if the directory is not empty. * Note: This is similar to unlinkat(fd, path, AT_REMOVEDIR) in POSIX. */ path_remove_directory(): Result; /** * Create a symbolic link. Note: This is similar to symlinkat in POSIX. */ path_symlink(): Result; /** * Send a signal to the process of the calling thread. * Note: This is similar to raise in POSIX. */ proc_raise(): number; /** * Accept a new incoming connection. Note: This is similar to accept in POSIX. */ sock_accept(): number; /** * Receive 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. */ sock_recv(): number; /** * Send 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. */ sock_send(): number; /** * Shut down socket send and receive channels. Note: This is similar to * shutdown in POSIX. */ sock_shutdown(): number; sock_open(): number; sock_listen(): number; sock_connect(): number; sock_setsockopt(): number; sock_bind(): number; sock_getlocaladdr(): number; sock_getpeeraddr(): number; sock_getaddrinfo(): number; }