Module Jx

This module provides utilities for interfacing with JavaScript values.

Example:

(* Bind the global document value. *)
let document = Jx.global "document"

let get_element_by_id id =
  (* Define decoder for the return type. *)
  let return = Jx.Decoder.(nullable js) in
  (* Bind the method call: document.getElementById(id). *)
  Jx.Obj.call1 document "getElementById" Jx.Encoder.string id ~return
type t

JavaScript values.

JavaScript values can be decoded using Decoder and encoded using Encoder.

type js = t

Alias for t.

val null : js

The JavaScript null value.

val undefined : js

The JavaScript undefined value.

exception Undefined_property of string

An exception raised when an unexpected undefined property is encountered.

val debugger : unit -> unit
val log : 'a -> unit
val debug : string list -> unit
val is_null : js -> bool

is_null js is js == null.

val is_undefined : js -> bool

is_undefined js is js == undefined.

val is_defined : js -> bool

is_defined js is js != undefined.

val is_number : js -> bool
val is_int : js -> bool
val is_boolean : js -> bool
val is_string : js -> bool
val is_object : js -> bool

Type helpers

val type_of : js -> string

See typeof.

val instance_of : js -> constr:js -> bool

Equality

val equal : js -> js -> bool

Global values

val global : string -> js

global name is globalThis[name]. If this evaluates to undefined, Undefined_property is raised.

val global_this : js

Value encoding

type 'a encoder = 'a -> js

The type for encoders of js values.

val encode : 'a encoder -> 'a -> js

Encode an OCaml value to a js value using an encoder.

module Encoder : sig ... end

Value decoding

type 'a decoder = js -> 'a

The type for decoders of js values.

val decode : 'a decoder -> js -> 'a

Decode an OCaml value from a js value using an decoder.

module Decoder : sig ... end

JavaScript objects

module Obj : sig ... end
module Array : sig ... end

Function helpers

module Fun : sig ... end