JavaScript Runtime
Globals available in the Gnome JavaScript runtime on top of standard ECMAScript.
This page might be out of date. Upstream documentation can be found on gitlab
Globals
console
GJS implements the WHATWG Console specification, with log severity mapped to GLib.LogLevelFlags.
const console: ConsoleExample:
console.log("Hello", { world: true })console.profile(), console.profileEnd() and console.timeStamp() are not implemented. Messages are logged with the Gjs-Console log domain by default, see setConsoleLogDomain.
WARNING
Stack traces of errors printed with console methods are not source mapped. Prefer logError for logging errors.
logError
Logs a stack trace for error, with an optional prefix, with severity GLib.LogLevelFlags.LEVEL_WARNING.
function logError(error: Error | GLib.Error, prefix?: string): voidExample:
try {
file.load_contents(null)
} catch (error) {
logError(error, "failed to load file")
}Unlike console.error, logError prints source mapped stack traces, so prefer it when logging errors.
log
Logs a message with severity GLib.LogLevelFlags.LEVEL_MESSAGE.
function log(message: any): voidExample:
log("Hello World")Prefer console.log.
print
Joins its arguments with a space, appends a newline and prints the result to stdout with g_print().
function print(...messages: any[]): voidExample:
print("Hello", "World") // Hello WorldUnlike console.log, the output is not a log message and has no log domain or severity.
printerr
Joins its arguments with a space, appends a newline and prints the result to stderr with g_printerr().
function printerr(...messages: any[]): voidExample:
printerr("usage: my-app [FILE]")setTimeout
Schedules handler to run once after timeout milliseconds. Any extra arguments are passed to handler.
function setTimeout<TArgs extends any[]>(
handler: (...args: TArgs) => void,
timeout?: number,
...args: TArgs
): GLib.SourceExample:
const source = setTimeout(() => {
console.log("one second has passed")
}, 1000)clearTimeout
Cancels a timeout created with setTimeout or setInterval.
function clearTimeout(id: GLib.Source): voidExample:
const source = setTimeout(callback, 1000)
clearTimeout(source)setInterval
Schedules handler to run every timeout milliseconds. Any extra arguments are passed to handler.
function setInterval<TArgs extends any[]>(
handler: (...args: TArgs) => void,
timeout?: number,
...args: TArgs
): GLib.SourceExample:
const source = setInterval(() => {
console.log("tick")
}, 1000)clearInterval
Cancels an interval created with setInterval or setTimeout.
function clearInterval(id: GLib.Source): voidExample:
const source = setInterval(callback, 1000)
clearInterval(source)TextEncoder
Encodes strings into UTF-8 bytes, as specified by the Encoding Standard.
class TextEncoder {
encode(input: string): Uint8Array
}Example:
const bytes = new TextEncoder().encode("Hello")Useful for passing strings to APIs that take GLib.Bytes or Uint8Array, such as Gio.File.replace_contents.
TextDecoder
Decodes bytes into a string, as specified by the Encoding Standard.
class TextDecoder {
constructor(label?: TextDecoderEncoding, options?: { fatal?: boolean })
decode(
buffer?: ArrayBuffer | ArrayBufferView | DataView | null,
options?: { stream?: boolean },
): string
}Example:
const [, contents] = file.load_contents(null)
const text = new TextDecoder().decode(contents)label defaults to "utf-8". When fatal is true, invalid input throws a TypeError instead of being replaced with U+FFFD.
import.meta.url
The absolute file: or resource: URL of the current module.
interface ImportMeta {
url: string
}Example:
import GLib from "gi://GLib?version=2.0"
const [filename] = GLib.filename_from_uri(import.meta.url)
const dirname = GLib.path_get_dirname(filename)IMPORTANT
It points to the transpiled JavaScript file, not the source TypeScript file.
system
Low-level facilities such as process arguments, exit() and debugging helpers.
NOTE
Only commonly used members are documented on this page. Documentation for the rest of the symbols can be found on gitlab. Most functions in this module should not be used in application code.
namespace System {
/** Corresponds to `argv[0]` in C. */
const programInvocationName: string
/** Command line arguments without `argv[0]`. */
const programArgs: string[]
/** GJS version */
const version: number
/** Exit the program */
function exit(code: number): never
}Example:
import System from "system"
// or
import { exit, programInvocationName, programArgs } from "system"
exit(0)
System.exit(0)
let app: Gtk.Application
app.runAsync([programInvocationName, ...programArgs])gettext
A convenience layer over the gettext family of functions, implemented by GLib.
NOTE
Gnim provides a higher level API in gnim/intl and so gettext namespace documentation on this page is partial.
namespace Gettext {
enum LocaleCategory {
CTYPE = 0,
NUMERIC = 1,
TIME = 2,
COLLATE = 3,
MONETARY = 4,
MESSAGES = 5,
ALL = 6,
}
function setlocale(
category: LocaleCategory,
locale: string | null,
): string | null
}Example:
import Gettext from "gettext"
// or
import { LocaleCategory, setlocale } from "gettext"
const current = setlocale(LocaleCategory.ALL, null)Pass null as locale to query the current locale. It is rarely necessary to call this, GJS sets the locale from the environment on startup.
console
Configures the global console object.
namespace Console {
const DEFAULT_LOG_DOMAIN: "Gjs-Console"
function setConsoleLogDomain(domain: string): void
}Example:
import Console from "console"
// or
import { setConsoleLogDomain } from "console"
// Sets the GLib log domain of the global console.
setConsoleLogDomain("com.example.MyApp")