123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187(* Js_of_ocaml library
* http://www.ocsigen.org/js_of_ocaml/
* Copyright Grégoire Henry 2010.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, with linking exception;
* either version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*)openJsopen!Import(****)(* The writing logic for basic types is copied from [lib/deriving_json]. *)letwrite_stringbuffers=Buffer.add_charbuffer'"';fori=0toString.lengths-1domatchs.[i]with|'"'->Buffer.add_stringbuffer{|\"|}|'\\'->Buffer.add_stringbuffer{|\\|}|'\b'->Buffer.add_stringbuffer{|\b|}|'\x0C'->Buffer.add_stringbuffer{|\f|}|'\n'->Buffer.add_stringbuffer{|\n|}|'\r'->Buffer.add_stringbuffer{|\r|}|'\t'->Buffer.add_stringbuffer{|\t|}|cwhenPoly.(c<='\x1F')->(* Other control characters are escaped. *)Printf.bprintfbuffer{|\u%04X|}(int_of_charc)|cwhenPoly.(c<'\x80')->Buffer.add_charbuffers.[i]|_c(* >= '\x80' *)->(* Bytes greater than 127 are embedded in a UTF-8 sequence. *)Buffer.add_charbuffer(Char.chr(0xC2lor(Char.codes.[i]lsr6)));Buffer.add_charbuffer(Char.chr(0x80lor(Char.codes.[i]land0x3F)))done;Buffer.add_charbuffer'"'letwrite_floatbufferf=(* "%.15g" can be (much) shorter; "%.17g" is round-trippable *)lets=Printf.sprintf"%.15g"finifPoly.(float_of_strings=f)thenBuffer.add_stringbufferselsePrintf.bprintfbuffer"%.17g"fletwrite_int64bufferi=letmask16=Int64.of_int0xffffinletmask24=Int64.of_int0xffffffinPrintf.bprintfbuffer"[255,%Ld,%Ld,%Ld]"(Int64.logandimask24)(Int64.logand(Int64.shift_righti24)mask24)(Int64.logand(Int64.shift_righti48)mask16)externalcustom_identifier:Obj.t->string="caml_custom_identifier"letrecwritebv=ifObj.is_intvthenPrintf.bprintfb"%d"(Obj.objv:int)elselett=Obj.tagvinift<=Obj.last_non_constant_constructor_tagthen(Printf.bprintfb"[%d"t;fori=0toObj.sizev-1doBuffer.add_charb',';writeb(Obj.fieldvi)done;Buffer.add_charb']')elseift=Obj.string_tagthenwrite_stringb(Obj.objv:string)elseift=Obj.double_tagthenwrite_floatb(Obj.objv:float)elseift=Obj.double_array_tagthen(Printf.bprintfb"[%d"t;fori=0toObj.sizev-1doBuffer.add_charb',';write_floatb(Obj.double_fieldvi)done;Buffer.add_charb']')elseift=Obj.custom_tagthenmatchcustom_identifiervwith|"_i"->Printf.bprintfb"%ld"(Obj.objv:int32)|"_j"->leti:int64=Obj.objvinwrite_int64bi|id->failwith(Printf.sprintf"Json.output: unsupported custom value %s "id)elsefailwith(Printf.sprintf"Json.output: unsupported tag %d "t)letto_jsonv=letbuf=Buffer.create50inwritebufv;Buffer.contentsbuf(****)classtypejson=objectmethodparse:'a.js_stringt->'amethmethodparse_:'a'b'c'd.js_stringt->('bt,js_stringt->'c->'d)meth_callback->'amethmethodstringify:'a.'a->js_stringtmethmethodstringify_:'a'b'c'd.'a->('b,js_stringt->'c->'d)meth_callback->js_stringtmethendletjson:jsonJs.t=Unsafe.global##._JSONletinput_reviver=letreviver_this_key(value:Unsafe.any):Obj.t=ifJs.equals(typeofvalue)(string"string")thenObj.repr(to_bytestring(Unsafe.coercevalue))elseifinstanceofvalueJs.array_empty&&(Unsafe.coercevalue)##.length==4&&Unsafe.getvalue0==255thenObj.repr(Jsoo_runtime.Int64.create_int64_lo_mi_hi(Unsafe.getvalue1)(Unsafe.getvalue2)(Unsafe.getvalue3))elseObj.reprvalueinwrap_meth_callbackreviverletunsafe_inputs=matchSys.backend_typewith|Other"wasm_of_ocaml"->(* https://github.com/ocsigen/js_of_ocaml/pull/1660#discussion_r1731099372
The encoding of OCaml values is ambiguous since both integers and floats
are mapped to numbers *)failwith"Json.unsafe_input: not implemented in the Wasm backend"|_->json##parse_sinput_reviverclasstypeobj=objectmethodconstructor:'a.'aconstrJs.readonly_propendletmlInt64_constr=Js.Unsafe.pure_expr@@fun()->letdummy_int64=1Linletdummy_obj:objt=Obj.magicdummy_int64indummy_obj##.constructorletoutput_reviver_key(value:Unsafe.any):Obj.t=ifObj.tag(Obj.reprvalue)=Obj.string_tagthenObj.repr(bytestring(Obj.magicvalue:string))elseifinstanceofvaluemlInt64_constrthenletvalue=Unsafe.coercevalueinObj.repr(array[|255;value##.lo;value##.mi;value##.hi|])elseObj.reprvalueletuse_native_stringify_=ref(matchSys.backend_typewith|Other"js_of_ocaml"->true|Native|Bytecode|Other_->false)letuse_native_stringify()=!use_native_stringify_letset_use_native_stringifyb=use_native_stringify_:=bletoutput_x=to_json(Obj.reprx)letoutputobj=matchSys.backend_typewith|Other"js_of_ocaml"whenuse_native_stringify()->json##stringify_obj(Js.wrap_callbackoutput_reviver)|_->Js.string(output_obj)