Skip to main content

STD

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

Wrappers to the libc file stdin, stdout, stderr:

  • std.in
  • std.out
  • std.err

Enumeration

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

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

Methods

evalScript

Evaluate the string str as a script (global eval).

std.evalScript(str, options = undefined);

options is an optional object containing the following optional properties:

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

loadScript

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

std.loadScript(filename);

exists

Returns a bool that determines whether the file exists or not.

const hasfile = std.exists(filename);

loadFile

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

const fstr = std.loadFile(filename);

open

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.

const file = std.open(filename, flags, errorObj = undefined);

fdopen

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.

std.fdopen(fd, flags, errorObj = undefined);

tmpfile

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.

std.tmpfile(errorObj = undefined);

puts

Equivalent to std.out.puts(str).

std.puts(str);

printf

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

std.printf(fmt, ...args);

sprintf

Equivalent to the libc sprintf().

std.sprintf(fmt, ...args);

strerror

Return a string that describes the error errno.

std.strerror(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.

std.gc();

parseExtJSON

Parse str using a superset of JSON.parse.

std.parseExtJSON(str);

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

open

const file = std.open(filename, flags);
const file = std.open("test.txt", "w");
info
  • filename - Path to the file, E.g.: "samples/test.txt".
  • flags - File mode, E.g.: "w", "r", "wb", "rb", etc.

close

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

close();

puts

Outputs the string with the UTF-8 encoding.

puts(str);

printf

Formatted printf.

printf(fmt, ...args);
info

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.

flush();

seek

Seek to a give 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.

seek(offset, whence);

Constants:

  • std.SEEK_SET
  • std.SEEK_CUR
  • std.SEEK_END

tell

Return the current file position.

tell();

tello

Return the current file position as a bigint.

tello();

eof

Return true if end of file.

eof();

fileno

Return the associated OS handle.

fileno();

error

Return true if there was an error.

error();

clearerr

Clear the error indication.

clearerr();

read

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

read(buffer, position, length);

write

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

write(buffer, position, length);

getline

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

getline();

readAsString

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 its end.

readAsString(max_size = undefined);

getByte

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

getByte();

putByte

Write one byte to the file.

putByte(c);