qjs:std module provides wrappers to C standard library functions (stdlib.h and stdio.h) along with additional utilities for file operations, script evaluation, and environment management.
Process Control
exit(n)
Terminates the process with the specified exit code.
Exit code (0 for success, non-zero for error)
gc()
Manually invokes the garbage collector cycle removal algorithm.
No return value
Script Evaluation
evalScript(str, options)
Evaluates a string as JavaScript code in the global scope.
The JavaScript code to evaluate
The result of the evaluated code, or a Promise if
async is trueloadScript(filename)
Loads and evaluates a JavaScript file in the global scope.
Path to the JavaScript file to load
File Operations
loadFile(filename, options)
Loads a file and returns its contents.
Path to the file to load
Optional configuration
If true, returns a Uint8Array instead of a string
File contents as a UTF-8 string or Uint8Array if binary mode. Returns
null on I/O error.writeFile(filename, data)
Writes data to a file, creating it if it doesn’t exist.
Path to the file to write
Data to write. Strings are written in text mode (UTF-8), binary data in binary mode.
undefined creates an empty file.Throws an exception on error
On Windows, files are opened in text mode for strings and binary mode for typed arrays. This affects line ending conversion.
FILE Object
open(filename, flags, errorObj)
Opens a file using C’s fopen() function.
Path to the file
File mode:
"r", "w", "a", "r+", "w+", "a+", with optional "b" for binaryIf provided, its
errno property is set to the error code (0 on success)FILE object or null on error
popen(command, flags, errorObj)
Opens a process by creating a pipe (Unix/Linux/macOS only).
Command to execute
Either
"r" (read) or "w" (write)Optional error object
fdopen(fd, flags, errorObj)
Opens a file from a file descriptor.
File descriptor
File mode flags
tmpfile(errorObj)
Creates a temporary file that is automatically deleted when closed.
Optional error object
FILE object or null on error
FILE Methods
All FILE objects have the following methods:close()
Closes the file.
Returns 0 on success or
-errno on errorputs(str)
Writes a string to the file with UTF-8 encoding.
String to write
printf(fmt, ...args)
Writes formatted output to the file.
Format string (C printf syntax)
Values to format
flush()
Flushes the file’s write buffer.
seek(offset, whence)
Seeks to a position in the file.
Byte offset
std.SEEK_SET, std.SEEK_CUR, or std.SEEK_ENDReturns 0 on success or
-errno on errortell()
Returns the current file position as a number.
tello()
Returns the current file position as a BigInt.
eof()
Returns true if at end of file.
fileno()
Returns the OS file handle (file descriptor).
error()
Returns true if an error occurred.
clearerr()
Clears the error indication.
read(buffer, position, length)
Reads bytes into an ArrayBuffer.
Destination buffer
Byte position in buffer to start writing
Number of bytes to read
Number of bytes actually read
write(buffer, position, length)
Writes bytes from an ArrayBuffer or string.
Source buffer or string
Byte position to start reading from
Number of bytes to write
getline()
Reads the next line from the file, excluding the newline character.
The line as a string, or null at EOF
readAsString(max_size)
Reads the file contents as a UTF-8 string.
Maximum bytes to read (reads entire file if omitted)
readAsArrayBuffer(max_size)
Reads the file contents as an ArrayBuffer.
Maximum bytes to read (reads entire file if omitted)
getByte()
Reads one byte.
The byte value (0-255) or -1 at EOF
putByte(c)
Writes one byte.
Byte value to write (0-255)
Standard Streams
Standard input
Standard output
Standard error
Output Functions
puts(str)
Equivalent to std.out.puts(str).
printf(fmt, ...args)
Equivalent to std.out.printf(fmt, ...args).
sprintf(fmt, ...args)
Formats a string like C’s sprintf().
Formatted string
Environment Variables
getenv(name)
Returns the value of an environment variable.
Environment variable name
Variable value or undefined if not set
setenv(name, value)
Sets an environment variable.
Variable name
Variable value
unsetenv(name)
Deletes an environment variable.
Variable name to delete
getenviron()
Returns all environment variables as an object.
Object with environment variable names as keys and values as strings
Error Handling
std.Error
Enumeration object containing common error codes:
EINVAL- Invalid argumentEIO- I/O errorEACCES- Permission deniedEEXIST- File existsENOSPC- No space leftENOSYS- Function not implementedEBUSY- Device or resource busyENOENT- No such file or directoryEPERM- Operation not permittedEPIPE- Broken pipe
strerror(errno)
Returns a human-readable error message.
Error code
Error description
Seek Constants
std.SEEK_SET- Seek from beginning of filestd.SEEK_CUR- Seek from current positionstd.SEEK_END- Seek from end of file
Network Utilities
urlGet(url, options)
Downloads a URL using the curl command (requires curl to be installed).
URL to download
Response data, or null on error. If
full is true, returns object with response, responseHeaders, and status properties.