1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465(* Js_of_ocaml compiler
* http://www.ocsigen.org/js_of_ocaml/
* Copyright (C) 2019 Hugo Heuzard
*
* 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.
*)open!StdlibmoduleJ=Javascriptletzero=J.ENum(J.Num.of_int320l)letone=J.ENum(J.Num.of_int321l)(* JavaScript engines recognize the pattern 'typeof x==="number"'; if the string is
shared, less efficient code is generated. *)lettype_of_is_numberbinope=J.EBin(binop,J.EUn(J.Typeof,e),J.EStr("number",`Bytes))letis_blocke=type_of_is_numberJ.NotEqEqeletis_immediatee=type_of_is_numberJ.EqEqEqemoduleBlock=structletmake~tag~args=J.EArr(List.map~f:(funx->Somex)(J.ENum(J.Num.of_int32(Int32.of_inttag))::args))lettage=J.EAccess(e,zero)letfieldeidx=letadjusted=J.ENum(J.Num.of_int32(Int32.of_int(idx+1)))inJ.EAccess(e,adjusted)endmoduleArray=structletmake=Block.makeletlengthe=letunderlying=J.EDot(e,"length")inJ.EBin(J.Minus,underlying,one)letfieldei=matchiwith|J.ENumn->letidx=J.Num.to_int32ninletadjusted=J.ENum(J.Num.of_int32(Int32.addidx1l))inJ.EAccess(e,adjusted)|J.EUn(J.Neg,_)->failwith"Negative field indexes are not allowed"|_->letadjusted=J.EBin(J.Plus,one,i)inJ.EAccess(e,adjusted)end