README

#@global - JavaScript Global Functions

This package provides JavaScript global functions and utilities that are available in all JavaScript environments.

#Exported Functions

#Global Objects

// Access globalThis
let global = @global.globalThis()

// Get undefined
let undef = @global.undefined()

#Type Checking

// Check if value is NaN
let nan_val = @core.any(0.0 / 0.0)
@global.isNaN(nan_val) // true

// Check if value is finite
@global.isFinite(42.0) // true
@global.isFinite(1.0 / 0.0) // false (Infinity)

#Number Parsing

// Parse integer with optional radix
@global.parseInt("42") // Some(42)
@global.parseInt("ff", radix=16) // Some(255)
@global.parseInt("invalid") // None

// Parse floating point
@global.parseFloat("3.14") // Some(3.14)
@global.parseFloat("1.5e2") // Some(150.0)
@global.parseFloat("invalid") // None

#String Encoding (Base64)

// Encode to Base64
let encoded = @global.btoa("Hello World")
// "SGVsbG8gV29ybGQ="

// Decode from Base64
let decoded = @global.atob("SGVsbG8gV29ybGQ=")
// "Hello World"

#URI Encoding

// Encode complete URI
let uri = @global.encodeURI("https://example.com/path?name=John Doe")
// "https://example.com/path?name=John%20Doe"

// Decode URI
let decoded = @global.decodeURI("https://example.com/path?name=John%20Doe")
// "https://example.com/path?name=John Doe"

// Encode URI component
let encoded = @global.encodeURIComponent("Hello World & Friends")
// "Hello%20World%20%26%20Friends"

// Decode URI component
let decoded = @global.decodeURIComponent("Hello%20World%20%26%20Friends")
// "Hello World & Friends"

#Object Cloning

// Deep clone using structured clone algorithm
let obj = @core.new_object()
obj["name"] = @core.any("Alice")
obj["age"] = @core.any(30)

let cloned = @global.structuredClone(obj)
// Creates an independent deep copy

Note: structuredClone can clone:
  • Primitive values
  • Objects and arrays (nested)
  • Dates, RegExp, Maps, Sets
  • TypedArrays and ArrayBuffers
  • Many built-in types

Cannot clone:
  • Functions
  • DOM nodes
  • Symbols (as property keys)

#Timers

// Set timeout
let timer = @global.setTimeout(fn() {
@console.log("Delayed!")
}, 1000)

// Clear timeout
@global.clearTimeout(timer)

// Set interval
let interval = @global.setInterval(fn() {
@console.log("Repeating!")
}, 1000)

// Clear interval
@global.clearInterval(interval)

#Microtasks

// Queue a microtask
@global.queueMicrotask(fn() {
@console.log("Microtask executed")
})

#Module Loading

// Dynamic import (returns Promise)
let module_promise = @global.dynamic_import("./module.js")

#Error Handling

Most encoding/decoding functions can throw errors:

// Functions that can raise errors
try {
let decoded = @global.atob("invalid base64!@#")
} catch {
err => @console.log("Decoding failed")
}

#Type Aliases

This package exports the Timer type for use with timer functions:

pub type Timer // Represents a timer ID

#See Also

#
Timer

#external
pub type Timer

Timer handle returned by setTimeout/setInterval

#
Timer::as_any

fn Timer::as_any(self : Timer) ->
Any

#
atob

fn atob(encoded_data : String) -> String raise
ThrowError

JS: atob(encodedData)

Decodes a string of data which has been encoded using Base64 encoding.

#
btoa

fn btoa(data : String) -> String raise
ThrowError

JS: btoa(data)

Creates a Base64-encoded ASCII string from a binary string.

#
clearInterval

fn clearInterval(timer : Timer) -> Unit

JS: clearInterval(timer)

Cancels an interval previously established by calling setInterval().

#
clearTimeout

fn clearTimeout(timer : Timer) -> Unit

JS: clearTimeout(timer)

Cancels a timeout previously established by calling setTimeout().

#
clear_interval

fn clear_interval(timer : Timer) -> Unit

#
clear_timeout

fn clear_timeout(timer : Timer) -> Unit

#
decodeURI

#alias(docode_uri)
fn decodeURI(encoded_uri : String) -> String

JS: decodeURI(encodedURI)

#
decodeURIComponent

#alias(decode_uri_component)
fn decodeURIComponent(encoded_str : String) -> String

JS: decodeURIComponent(encodedStr)

#
dynamic_import

fn dynamic_import(module_name : String) ->
Any

Dynamic import (ES modules) Note: Returns a Promise

#
encodeURI

#alias(encode_uri)
fn encodeURI(uri : String) -> String

JS: encodeURI(uri)

#
encodeURIComponent

#alias(encode_uri_component)
fn encodeURIComponent(str : String) -> String

JS: encodeURIComponent(str)

#
globalThis

fn globalThis() ->
Any

JS: globalThis

#
global_this

fn global_this() ->
Any

#
isFinite

#alias(is_finite)
fn isFinite(v : Double) -> Bool

JS: isFinite(v)

#
isNaN

#alias(is_nan)
fn isNaN(v :
Any
) -> Bool

JS: isNaN(v)

#
parseFloat

#alias(parse_float)
fn parseFloat(string : String) -> Double?

JS: parseFloat(string)

Parse a string and return a floating point number. Returns None if the string cannot be parsed as a number.

#
parseInt

#alias(parse_int)
fn parseInt(string : String, radix? : Int) -> Int?

JS: parseInt(string, radix)

Parse a string and return an integer. If radix is not provided, it defaults to 10 (or 16 if string starts with "0x").

#
queueMicrotask

fn queueMicrotask(callback : () -> Unit) -> Unit

JS: queueMicrotask(callback)

Queue a microtask to be executed after the current task finishes. Microtasks are executed before the next task in the event loop.

#
queue_microtask

fn queue_microtask(callback : () -> Unit) -> Unit

#
setInterval

fn setInterval(f : () -> Unit, duration : Int) -> Timer

JS: setInterval(f, duration)

Repeatedly calls a function with a fixed time delay between each call. Returns a Timer that can be used to cancel the interval.

#
setTimeout

fn setTimeout(f : () -> Unit, duration : Int) -> Timer

JS: setTimeout(f, duration)

Schedules a function to be called after a specified delay (in milliseconds). Returns a Timer that can be used to cancel the scheduled execution.

#
set_interval

fn set_interval(f : () -> Unit, duration : Int) -> Timer

#
set_timeout

fn set_timeout(f : () -> Unit, duration : Int) -> Timer

#
structuredClone

#alias(structured_clone)
fn structuredClone(value :
Any
) ->
Any

JS: structuredClone(value)

Creates a deep clone of a value using the structured clone algorithm. This can clone complex objects including nested objects, arrays, dates, etc.

#
undefined

fn undefined() ->
Any

JS: undefined