123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175(* 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="caml_js_to_float"externalfloat_to_js:float->t="caml_js_from_float"externalobj:(string*t)array->t="caml_js_object"externalvariable:string->t="caml_js_var"externalget:t->string->t="caml_js_get"externalset:t->string->t->unit="caml_js_set"externaldelete:t->string->unit="caml_js_delete"externalget_prop:t->t->t="caml_js_get"externalset_prop:t->t->t->unit="caml_js_set"externaldelete_prop:t->t->unit="caml_js_delete"externalget_prop_ascii:t->string->t="caml_js_get"externalset_prop_ascii:t->string->t->unit="caml_js_set"externaldelete_prop_ascii:t->string->unit="caml_js_delete"externalinternal_type_of:t->t="caml_js_typeof"lettype_ofx=string_of_js(internal_type_ofx)externalinternal_instance_of:t->t->t="caml_js_instanceof"letinstance_ofx~constr=bool_of_js(internal_instance_ofxconstr)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"globalThis"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(get_prop_asciiglobal"Array")[|int_to_jsn|]letarray_getti=get_propt(int_to_jsi)letarray_settix=set_propt(int_to_jsi)xletarray_of_js_fromfobjsstart=letn=int_of_js(get_prop_asciiobjs"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=ifequalsxnullthenNoneelseSome(fx)letoption_to_jsf=function|Somex->fx|None->nullletunit_to_js()=undefinedletunit_of_js_=()classobj(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=type_ofo="object"&&o!=null&&get_propo(string_to_jsx)!=undefinedexternalnew_obj_arr:t->t->t="caml_ojs_new_arr"letempty_obj()=new_obj(get_prop_asciiglobal"Object")[||]externaliter_properties_untyped:t->t->unit="caml_ojs_iterate_properties"letiter_propertiesxf=iter_properties_untypedx(fun_to_js1(funx->f(string_of_jsx)))letapply_arroarr=callo"apply"[|null;arr|]letcall_arrosarr=call(get_propo(string_to_jss))"apply"[|o;arr|]letis_nullx=equalsxnullletobj_typex=string_of_js(call(pure_js_expr"Object.prototype.toString")"call"[|x|])moduletypeT=sigtypejs:=ttypetvalt_to_js:t->jsvalt_of_js:js->tend(* Ojs.T instances for built-in types *)moduleInt=structtypet=intlett_to_js=int_to_jslett_of_js=int_of_jsendmoduleString=structtypet=stringlett_to_js=string_to_jslett_of_js=string_of_jsendmoduleBool=structtypet=boollett_to_js=bool_to_jslett_of_js=bool_of_jsendmoduleFloat=structtypet=floatlett_to_js=float_to_jslett_of_js=float_of_jsendmoduleArray(A:T)=structtypet=A.tarraylett_to_js=array_to_jsA.t_to_jslett_of_js=array_of_jsA.t_of_jsendmoduleList(A:T)=structtypet=A.tlistlett_to_js=list_to_jsA.t_to_jslett_of_js=list_of_jsA.t_of_jsendmoduleOption(A:T)=structtypet=A.toptionlett_to_js=option_to_jsA.t_to_jslett_of_js=option_of_jsA.t_of_jsend