123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135(* Copyright (C) 2020- Hongbo Zhang, Authors of ReScript
*
* 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, either version 3 of the License, or
* (at your option) any later version.
*
* In addition to the permissions granted to you by the LGPL, you may combine
* or link a "work that uses the Library" with a publicly distributed version
* of this file to produce a combined library or application, then distribute
* that combined work under the terms of your choosing, with no requirement
* to comply with the obligations normally placed on you by section 4 of the
* LGPL version 3 (or the corresponding section of a later version of the LGPL
* should you choose to use a 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. *)openImportopenAst_helperletjs_propertylocobj(name:string)=Pexp_send([%expr[%eExp.ident{txt=Ast_literal.unsafe_downgrade;loc}][%eobj]],{loc;txt=name})letgeneric_apply=(* we use the trick
[( opaque e : _) ] to avoid it being inspected, the type constraint is
avoid some syntactic transformation, e.g ` e |. (f g [@bs])` `opaque` is
to avoid it being inspected in the type level *)letopaque_full_apply~loce=Pexp_constraint(Exp.apply~loc~attrs:[(* Ignore warning 20 (`ignored-extra-argument`) on expressions such
* as:
*
* external x : < .. > Js.t = ""
* let () = x##fn "hello"
*
* OCaml thinks the extra argument is unused because we're
* producing * an uncurried call to a JS function whose arity isn't
* known at compile time. *)Ast_attributes.ignored_extra_argument;](Exp.ident{txt=Ast_literal.js_internal_full_apply;loc})[(Nolabel,e)],Typ.any~loc())infun~loc~kind(self:Ast_traverse.map)objargs~(f:loc:loc->expression->expression)->letfn=letobj=self#expressionobjinf~locobjinletargs=letargs=List.map~f:(fun(lbl,e)->Error.optional_err~loclbl;(lbl,self#expressione))argsinmatchargswith|[(Nolabel,{pexp_desc=Pexp_construct({txt=Lident"()";_},None);_});]->[]|_->argsinmatchargswith|[]->Pexp_apply(Exp.ident{txt=(matchkindwith|`oo->Ldot(Ldot(Ast_literal.js_oo,"Internal"),"run")|`generic->Ldot(Ast_literal.js_internal,"run"));loc;},[(Nolabel,fn)])|args->letarity_s=string_of_int(List.lengthargs)inopaque_full_apply~loc(Exp.apply~loc(Exp.apply~loc(Exp.ident~loc{txt=Ast_literal.opaque;loc})[(Nolabel,Exp.field~loc(Exp.constraint_~locfn(Typ.constr~loc{txt=Ldot((matchkindwith|`oo->Ast_literal.js_meth|`generic->Ast_literal.js_fn),Format.sprintf"arity%s"arity_s);loc;}[Typ.any~loc()])){txt=Ast_literal.hidden_fieldarity_s;loc});])args)letmethod_apply~loc(self:Ast_traverse.map)objnameargs=letf~locobj=Exp.mk~loc(js_propertylocobjname)ingeneric_apply~loc~kind:`ooselfobjargs~fletgeneric_apply~loc(self:Ast_traverse.map)objargs~(f:loc:loc->expression->expression)=generic_apply~loc~kind:`genericselfobjargs~fletuncurry_fn_apply~locselffnargs=generic_apply~locselffnargs~f:(fun~loc:_obj->obj)letproperty_apply~locselfobjnameargs=generic_apply~locselfobjargs~f:(fun~locobj->Exp.mk~loc(js_propertylocobjname))