123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115(* The gen_js_api is released under the terms of an MIT-like license. *)(* See the attached LICENSE file. *)(* Copyright 2015 by LexiFi. *)(* This module (mostly) abstracts away from js_of_ocaml encoding of
OCaml values. It serves as a support library for the code generated
by gen_js_api.
The module could mostly be implemented on top of js_of_ocaml's Js module
(and in particular Js.Unsafe), but we prefer to drop the dependency
to js_of_ocaml's library and to rely only on its compiler and JS
runtime code.
*)typetexternalt_of_js:t->t="%identity"externalt_to_js:t->t="%identity"externalstring_of_js:t->string="caml_js_to_string"externalstring_to_js:string->t="caml_js_from_string"externalint_of_js:t->int="%identity"externalint_to_js:int->t="%identity"externalbool_of_js:t->bool="caml_js_to_bool"externalbool_to_js:bool->t="caml_js_from_bool"externalfloat_of_js:t->float="%identity"externalfloat_to_js:float->t="%identity"externalobj:(string*t)array->t="caml_js_object"externalvariable:string->t="caml_js_var"externalinternal_get:t->t->t="caml_js_get"externalinternal_set:t->t->t->unit="caml_js_set"externalget:t->string->t="caml_js_get"externalset:t->string->t->unit="caml_js_set"externalinternal_type_of:t->t="caml_js_typeof"lettype_ofx=string_of_js(internal_type_ofx)externalpure_js_expr:string->t="caml_pure_js_expr"letnull=pure_js_expr"null"letundefined=pure_js_expr"undefined"externalequals:t->t->bool="caml_js_equals"letglobal=pure_js_expr"joo_global_object"externalnew_obj:t->tarray->t="caml_js_new"externalcall:t->string->tarray->t="caml_js_meth_call"externalapply:t->tarray->t="caml_js_fun_call"letarray_maken=new_obj(getglobal"Array")[|int_to_jsn|]letarray_getti=internal_gett(int_to_jsi)letarray_settix=internal_sett(int_to_jsi)xletarray_of_js_fromfobjsstart=letn=int_of_js(getobjs"length")inArray.init(n-start)(funi->f(array_getobjs(start+i)))letarray_of_jsfobjs=array_of_js_fromfobjs0letarray_to_jsfarr=letn=Array.lengtharrinleta=array_makeninfori=0ton-1doarray_setai(farr.(i))done;aletlist_of_js_fromfobjsstart=Array.to_list(array_of_js_fromfobjsstart)letlist_of_jsfobjs=list_of_js_fromfobjs0letlist_to_jsfl=array_to_jsf(Array.of_listl)letoption_of_jsfx=ifequalsxnull||x==undefinedthenNoneelseSome(fx)letoption_to_jsf=function|Somex->fx|None->nullclassobj(x:t)=objectmethodto_js=xendexternalfun_to_js:int->(t->'a)->t="caml_js_wrap_callback_strict"externalfun_to_js_args:(t->'a)->t="caml_ojs_wrap_fun_arguments"lethas_propertyox=not(getox==undefined)externaliter_properties:t->(string->unit)->unit="caml_ojs_iterate_properties"letempty_obj()=new_obj(getglobal"Object")[||]letapply_arroarr=callo"apply"[|null;arr|]letcall_arrosarr=call(getos)"apply"[|o;arr|]externalnew_obj_arr:t->t->t="caml_ojs_new_arr"externaldelete:t->string->unit="caml_js_delete"letis_nullx=equalsxnullletobj_typex=string_of_js(call(pure_js_expr"Object.prototype.toString")"call"[|x|])