Module Jv
JavaScript values.
Values
type t
The type for JavaScript values. A value of this type represents a value of any JavaScript primitive type.
val equal : t -> t -> bool
equal v0 v1
is JavaScript==
equality.
val strict_equal : t -> t -> bool
strict_equal v0 v1
is JavaScript's strict equality. OCaml's(==)
is mapped on that equality.
val repr : 'a -> t
repr v
is the OCaml valuev
as its JavaScript value representation.
Null and undefined
val is_null : t -> bool
is_null v
istrue
iffv
is strictly equal tonull
.
val is_undefined : t -> bool
is_undefined v
istrue
iffv
is strictly equal toundefined
.
val is_none : t -> bool
is_none v
isis_null v || is_undefined v
.
val is_some : t -> bool
is_some v
isnot (is_none v)
.
Objects
val global : t
global
refers to the global object.
Properties
val get : t -> prop -> t
get o p
is the propertyp
ofo
. Warning, the result can benull
orundefined
. See alsofind
.
val delete : t -> prop -> unit
delete o p
deletes propertyp
ofo
. The propertyp
oro
becomesundefined
.
val find : t -> prop -> t option
find p o
is propertyp
ofo
. If the property isnull
orundefined
, the result isNone
. See alsoget
.
Creating
val new' : t -> t array -> t
new_obj c args
creates an object with constructor functionc
and argumentsargs
.constructor
can be used to lookup a constructor function by name in the global object.
Methods
Booleans
val true' : t
true
is JavaScripttrue
.
val false' : t
false'
is JavaScriptfalse
.
val to_bool : t -> bool
to_bool v
is the JavaScriptBoolean
valuev
as abool
value. This is unsafe, only use ifv
is guaranted to be a JavaScript boolean.
val of_bool : bool -> t
of_bool b
is thebool
valueb
as a JavaScriptBoolean
value.
module Bool : sig ... end
bool
properties accessors.
Integers
val to_int : t -> int
to_int v
is the JavaScriptNumber
valuev
as anint
value. The conversion is lossless providedv
is integral. This is unsafe, only use ifv
is guaranteed to be a JavaScript number.
val of_int : int -> t
of_int i
is theint
valuei
as a JavaScriptNumber
value. The conversion is lossess.
module Int : sig ... end
int
properties accessors.
Floating points
val to_float : t -> float
to_float v
is the JavaScriptNumber
valuev
as afloat
value. The conversion is lossless.
val of_float : float -> t
of_float f
is thefloat
valuef
as a JavaScriptNumber
value. The conversion is lossless.
module Float : sig ... end
float
object properties.
JavaScript strings
module Jstr : sig ... end
Jstr
object properties.
OCaml strings
val of_string : string -> t
of_string v
is a JavaScript string from the UTF-8 encoded OCaml stringv
. Shortcut forof_jstr (Jstr.v v)
.
val to_string : t -> string
to_string v
is an UTF-8 encoded OCaml string from the JavaScript stringv
. Shortcut forJstr.to_string (to_jstr v)
.
Arrays
val to_array : (t -> 'a) -> t -> 'a array
to_array conv a
is anarray
value made of the JavaScript arraya
whose elements are converted withconv
.
val of_array : ('a -> t) -> 'a array -> t
of_array conv a
is a JavaScriptArray
value made of thearray
valuea
whose element are converted to JavaScript values withconv
.
val to_list : (t -> 'a) -> t -> 'a list
to_list conv a
is alist
value made of the JavaScript arraya
whose elements are converted withconv
.
val of_list : ('a -> t) -> 'a list -> t
of_list conv l
is the JavaScriptArray
value made of thelist
valuel
whose element are converted to JavaScript values withconv
.
Specialized conversions
Can be faster.
JavaScript array manipulation
module Jarray : sig ... end
JavaScript arrays.
Functions
val apply : t -> t array -> t
apply f args
calls functionf
with argumentsargs
.func
can be used to lookup a function by name in the global object.
Errors and exceptions
module Error : sig ... end
Error objects.
exception
Error of Error.t
This OCaml exception represents any exception thrown by JavaScript code that is an instance of the Error exception. You should match on this exception in OCaml code to catch JavaScript exceptions.
val throw : ?name:Jstr.t -> Jstr.t -> 'a
throw ?name msg
throws a JavaScript exception with error objectJv.Error.v
?name msg
.
Iterator protocol
module It : sig ... end
JavaScript iterator protocol.
Promises
module Promise : sig ... end
JavaScript promise support.
JavaScript Unicode identifiers
The functions above only work with US-ASCII OCaml string literals. If you hit general Unicode identifiers create JavaScript strings representing them with Jstr
.v and use the following functions.
type prop'
= Jstr.t
The type for full Unicode JavaScript object property names.
val get' : t -> prop' -> t
get' o p
is the propertyp
ofo
. Warning, the result can benull
orundefined
. See alsofind
.
val delete' : t -> prop' -> unit
delete' o p
deletes propertyp
ofo
. The propertyp
oro
becomesundefined
.
Entering the debugger
Feature detection
Conversion interface
module type CONV = sig ... end
Abstract type conversion iterface.