Skip to main content
The 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.
number
required
Exit code (0 for success, non-zero for error)

gc()

Manually invokes the garbage collector cycle removal algorithm.
undefined
No return value

Script Evaluation

evalScript(str, options)

Evaluates a string as JavaScript code in the global scope.
string
required
The JavaScript code to evaluate
object
Optional configuration object
boolean
default:false
If true, error backtraces do not list stack frames below the evalScript call
boolean
default:false
If true, await is accepted in the script and a promise is returned
any
The result of the evaluated code, or a Promise if async is true

loadScript(filename)

Loads and evaluates a JavaScript file in the global scope.
string
required
Path to the JavaScript file to load

File Operations

loadFile(filename, options)

Loads a file and returns its contents.
string
required
Path to the file to load
object
Optional configuration
boolean
default:false
If true, returns a Uint8Array instead of a string
string | Uint8Array | null
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.
string
required
Path to the file to write
string | TypedArray | ArrayBuffer
Data to write. Strings are written in text mode (UTF-8), binary data in binary mode. undefined creates an empty file.
undefined
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.
string
required
Path to the file
string
required
File mode: "r", "w", "a", "r+", "w+", "a+", with optional "b" for binary
object
If provided, its errno property is set to the error code (0 on success)
FILE | null
FILE object or null on error

popen(command, flags, errorObj)

Opens a process by creating a pipe (Unix/Linux/macOS only).
string
required
Command to execute
string
required
Either "r" (read) or "w" (write)
object
Optional error object
Not available on Windows and WASI platforms.

fdopen(fd, flags, errorObj)

Opens a file from a file descriptor.
number
required
File descriptor
string
required
File mode flags

tmpfile(errorObj)

Creates a temporary file that is automatically deleted when closed.
object
Optional error object
FILE | null
FILE object or null on error

FILE Methods

All FILE objects have the following methods:

close()

Closes the file.
number
Returns 0 on success or -errno on error

puts(str)

Writes a string to the file with UTF-8 encoding.
string
required
String to write

printf(fmt, ...args)

Writes formatted output to the file.
string
required
Format string (C printf syntax)
any
Values to format

flush()

Flushes the file’s write buffer.

seek(offset, whence)

Seeks to a position in the file.
number | BigInt
required
Byte offset
number
required
std.SEEK_SET, std.SEEK_CUR, or std.SEEK_END
number
Returns 0 on success or -errno on error

tell()

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.
ArrayBuffer
required
Destination buffer
number
default:0
Byte position in buffer to start writing
number
default:"buffer.length"
Number of bytes to read
number
Number of bytes actually read

write(buffer, position, length)

Writes bytes from an ArrayBuffer or string.
ArrayBuffer | string
required
Source buffer or string
number
default:0
Byte position to start reading from
number
default:"buffer.length"
Number of bytes to write

getline()

Reads the next line from the file, excluding the newline character.
string | null
The line as a string, or null at EOF

readAsString(max_size)

Reads the file contents as a UTF-8 string.
number
Maximum bytes to read (reads entire file if omitted)

readAsArrayBuffer(max_size)

Reads the file contents as an ArrayBuffer.
number
Maximum bytes to read (reads entire file if omitted)

getByte()

Reads one byte.
number
The byte value (0-255) or -1 at EOF

putByte(c)

Writes one byte.
number
required
Byte value to write (0-255)

Standard Streams

FILE
Standard input
FILE
Standard output
FILE
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().
string
Formatted string

Environment Variables

getenv(name)

Returns the value of an environment variable.
string
required
Environment variable name
string | undefined
Variable value or undefined if not set

setenv(name, value)

Sets an environment variable.
string
required
Variable name
string
required
Variable value

unsetenv(name)

Deletes an environment variable.
string
required
Variable name to delete

getenviron()

Returns all environment variables as an object.
object
Object with environment variable names as keys and values as strings

Error Handling

std.Error

Enumeration object containing common error codes:
  • EINVAL - Invalid argument
  • EIO - I/O error
  • EACCES - Permission denied
  • EEXIST - File exists
  • ENOSPC - No space left
  • ENOSYS - Function not implemented
  • EBUSY - Device or resource busy
  • ENOENT - No such file or directory
  • EPERM - Operation not permitted
  • EPIPE - Broken pipe

strerror(errno)

Returns a human-readable error message.
number
required
Error code
string
Error description

Seek Constants

  • std.SEEK_SET - Seek from beginning of file
  • std.SEEK_CUR - Seek from current position
  • std.SEEK_END - Seek from end of file

Network Utilities

urlGet(url, options)

Downloads a URL using the curl command (requires curl to be installed).
string
required
URL to download
object
Optional configuration
boolean
default:false
If true, returns ArrayBuffer instead of string
boolean
default:false
If true, returns object with response, responseHeaders, and status
string | ArrayBuffer | object | null
Response data, or null on error. If full is true, returns object with response, responseHeaders, and status properties.
Not available on WASI. Requires curl to be installed on the system.