glendix/js/object

Builds plain, prototype-safe JavaScript data objects from typed entries.

This module owns data-only object construction. It converts typed Gleam scalars into opaque JavaScript values and assembles them into plain objects whose keys are always ordinary own data properties. Dynamic interop such as property reads and writes, method calls, and constructor invocation lives in the separate glendix/js/reflect module, so this data boundary never depends on arbitrary reflection.

Construction keeps a small bespoke FFI (Object.fromEntries) on purpose: no ecosystem package builds a live, prototype-pollution-safe plain object. The public gleam/javascript API only covers arrays, promises, and symbols, while Plinth has no general plain-object builder. A gleam/json round-trip would be indirect and lossy for live handles. The retained FFI guarantees that even a __proto__ entry is stored as ordinary own data instead of invoking the legacy prototype setter.

Types

Represents a JavaScript boolean value.

pub type JsBoolean {
  TrueValue
  FalseValue
}

Constructors

  • TrueValue

    JavaScript true.

  • FalseValue

    JavaScript false.

Represents a JavaScript object handle.

pub type JsObject

Represents a JavaScript value whose runtime shape is intentionally opaque.

pub type JsValue

Values

pub fn bool(value value: JsBoolean) -> JsValue

Converts a typed JavaScript boolean into a JavaScript value.

pub fn empty() -> JsObject

Creates an empty object.

pub fn float(value value: Float) -> JsValue

Converts a float into a JavaScript value.

pub fn from_entries(
  entries entries: List(#(String, JsValue)),
) -> JsObject

Creates an object from ordered key-value entries.

The typed input prevents malformed entry shapes. JavaScript property enumeration rules preserve the order of ordinary string keys, duplicate keys keep their last value, and special keys such as __proto__ are stored as own data properties.

pub fn from_object(object object: JsObject) -> JsValue

Converts an object handle into a JavaScript value.

pub fn int(value value: Int) -> JsValue

Converts an integer into a JavaScript value.

pub fn string(value value: String) -> JsValue

Converts a string into a JavaScript value.

Search Document