std module

The std module provides wrappers to the libc stdlib.h and stdio.h and a few other utilities.

Available exports:

exit(n)

Exit the process.

evalScript(str, options = undefined)

Evaluate the string str as a script (global eval). options is an optional object containing the following optional properties:

  • backtrace_barrier: Boolean (default = false). If true, error backtraces do not list the stack frames below the evalScript.

loadScript(filename)

Evaluate the file filename as a script (global eval).

loadFile(filename)

Load the file filename and return it as a string assuming UTF-8 encoding. Return null in case of I/O error.

open(filename, flags, errorObj = undefined)

Open a file (wrapper to the libc fopen()). Return the FILE object or null in case of I/O error. If errorObj is not undefined, set its errno property to the error code or to 0 if no error occured.

popen(command, flags, errorObj = undefined)

Open a process by creating a pipe (wrapper to the libc popen()). Return the FILE object or null in case of I/O error. If errorObj is not undefined, set its errno property to the error code or to 0 if no error occured.

fdopen(fd, flags, errorObj = undefined)

Open a file from a file handle (wrapper to the libc fdopen()). Return the FILE object or null in case of I/O error. If errorObj is not undefined, set its errno property to the error code or to 0 if no error occured.

tmpfile(errorObj = undefined)

Open a temporary file. Return the FILE object or null in case of I/O error. If errorObj is not undefined, set its errno property to the error code or to 0 if no error occured.

puts(str)

Equivalent to std.out.puts(str).

printf(fmt, ...args)

Equivalent to std.out.printf(fmt, ...args).

sprintf(fmt, ...args)

Equivalent to the libc sprintf().

in, out, err

Wrappers to the libc file stdin, stdout, stderr.

  • SEEK_SET
  • SEEK_CUR
  • SEEK_END

Constants for seek().

Error

Enumeration object containing the integer value of common errors (additional error codes may be defined):

  • EINVAL
  • EIO
  • EACCES
  • EEXIST
  • ENOSPC
  • ENOSYS
  • EBUSY
  • ENOENT
  • EPERM
  • EPIPE

strerror(errno)

Return a string that describes the error errno.

gc()

Manually invoke the cycle removal algorithm. The cycle removal algorithm is automatically started when needed, so this function is useful in case of specific memory constraints or for testing.

getenv(name)

Return the value of the environment variable name or undefined if it is not defined.

setenv(name, value)

Set the value of the environment variable name to the string value.

unsetenv(name)

Delete the environment variable name.

getenviron()

Return an object containing the environment variables as key-value pairs.

urlGet(url, options = undefined)

Download url using the curl command line utility. options is an optional object containing the following optional properties:

  • binary: Boolean (default = false). If true, the response is an ArrayBuffer instead of a string. When a string is returned, the data is assumed to be UTF-8 encoded.
  • full: Boolean (default = false). If true, return an object contains the properties response (response content), responseHeaders (headers separated by CRLF), status (status code). response is null in case of protocol or network error. If full is false, only the response is returned if the status is between 200 and 299. Otherwise null is returned.

parseExtJSON(str)

Parse str using a superset of JSON.parse. The following extensions are accepted:

  • Single line and multiline comments
  • Unquoted properties (ASCII-only Javascript identifiers)
  • Trailing comma in array and object definitions
  • Single quoted strings
  • \f and \v are accepted as space characters
  • Leading plus in numbers
  • Octal (0o prefix) and hexadecimal (0x prefix) numbers

FILE Prototype

close()

Close the file. Return 0 if OK or -errno in case of I/O error.

puts(str)

Outputs the string with the UTF-8 encoding.

printf(fmt, ...args)

Formatted printf. The same formats as the standard C library printf are supported. Integer format types (e.g. %d) truncate the Numbers or BigInts to 32 bits. Use the l modifier (e.g. %ld) to truncate to 64 bits.

flush()

Flush the buffered file.

seek(offset, whence)

Seek to a given file position (whence is std.SEEK_*). offset can be a number or a bigint. Return 0 if OK or -errno in case of I/O error.

tell()

Return the current file position.

tello()

Return the current file position as a bigint.

eof()

Return true if end of file.

fileno()

Return the associated OS handle.

error()

Return true if there was an error.

clearerr()

Clear the error indication.

read(buffer, position, length)

Read length bytes from the file to the ArrayBuffer buffer at byte position position (wrapper to the libc fread).

write(buffer, position, length)

Write length bytes to the file from the ArrayBuffer buffer at byte position position (wrapper to the libc fwrite).

getline()

Return the next line from the file, assuming UTF-8 encoding, excluding the trailing line feed.

readAsString(max_size = undefined)

Read max_size bytes from the file and return them as a string assuming UTF-8 encoding. If max_size is not present, the file is read up to its end.

getByte()

Return the next byte from the file. Return -1 if the end of file is reached.

putByte(c)

Write one byte to the file.