Module Quickjs
OCaml bindings to QuickJS.
type
type runtime
runtime
represents a Javascript runtime corresponding to an object heap. Several runtimes can exist at the same time but they cannot exchange objects. Inside a given runtime, no multi-threading is supported.
type context
context
represents a Javascript context (or Realm). Each context has its own global objects and system objects. There can be several contexts per runtime and they can share objects, similar to frames of the same origin sharing Javascript objects in a web browser.
type bytecode
bytecode
represents the bytecode which is generated bycompile
and can be executed withexecute
.
type js_exn
= value
JS can throw any
value
type 'a or_js_exn
= ('a, js_exn) Stdlib.result
raw represent
val get_raw_runtime : runtime -> Quickjs_raw.js_runtime_ptr
val get_raw_context : context -> Quickjs_raw.js_context_ptr
val get_raw_value : value -> Quickjs_raw.js_value
val get_raw_bytecode : bytecode -> Quickjs_raw.js_value
runtime
val new_runtime : unit -> runtime
val set_max_stack_size : runtime -> Unsigned.size_t -> unit
val set_memory_limit : runtime -> Unsigned.size_t -> unit
val set_gc_threshold : runtime -> Unsigned.size_t -> unit
val run_gc : runtime -> unit
val compute_memory_usage : runtime -> Quickjs_raw.MemoryUsage.t
val memory_usage_to_string : Quickjs_raw.MemoryUsage.t -> string
type interrupt_handler
= runtime -> bool
interrupt_handler runtime
, return false will interrupt runtime
val set_interrupt_handler : runtime -> interrupt_handler -> unit
context
val new_context : runtime -> context
val get_runtime : context -> runtime
val enable_bignum_ext : context -> unit
enable math mode support.
val disable_bignum_ext : context -> unit
value
module Value : sig ... end
convert
value
to ocaml data
val check_exception : value -> value or_js_exn
check_exception value
get exception obj from context. Cannot be called twice with the samevalue
.let _ = let open Quickjs in let r = eval_unsafe script in match check_exception r with | Ok obj -> "it is safe to use " ^ (Value.to_string obj) | Error js_exn -> "eval_unsafe throw " ^ (Value.to_string js_exn)
eval
type eval_type
=[
|
`GLOBAL
global code (default)
|
`MODULE
module code
]
type eval_flag
=[
|
`STRICT
force 'strict' mode
|
`STRIP
force 'strip' mode
|
`BACKTRACE_BARRIER
don't include the stack frames before this eval in the Error() backtraces
]
val eval : ?typ:eval_type -> ?flags:eval_flag list -> ?ctx:context -> string -> value or_js_exn
eval ~typ ~flags ~ctx script
val eval_unsafe : ?typ:eval_type -> ?flags:eval_flag list -> ?ctx:context -> string -> value
eval_unsafe ~typ ~flags ~ctx script
, you must check exception by yourself.check_exception