Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file ast_uncurry_apply.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162(* 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. *)openAst_helpertypeexp=Parsetree.expression(* TODO: have a final checking for property arities
[#=],
*)letjsInternal=Ast_literal.Lid.js_internalletignored_extra_argument:Parsetree.attribute={attr_name=Location.mknoloc"ocaml.warning";attr_payload=PStr[Str.eval(Exp.constant(Pconst_string("-ignored-extra-argument",Location.none,None)));];attr_loc=Location.none;}(* 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~loc(e:exp):Parsetree.expression_desc=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. *)ignored_extra_argument;](Exp.ident{txt=Ast_literal.Lid.js_internal_full_apply;loc})[(Nolabel,e)],Typ.any~loc())letgeneric_applyloc(self:Ast_mapper.mapper)(obj:Parsetree.expression)(args:Ast_compatible.args)(cb:loc->exp->exp)=letobj=self.exprselfobjinletargs=Ext_list.mapargs(fun(lbl,e)->Bs_syntaxerr.optional_errloclbl;(lbl,self.exprselfe))inlet fn=cblocobjinletargs=matchargswith|[(Nolabel,{pexp_desc=Pexp_construct({txt=Lident"()"},None)});]->[]|_->argsinletarity=List.lengthargsinif arity=0thenParsetree.Pexp_apply(Exp.ident{txt=Ldot(jsInternal,"run");loc},[(Nolabel,fn)])elseletarity_s=string_of_intarityinopaque_full_apply~loc(Exp.apply ~loc(Exp.apply~loc(Exp.ident~loc{txt=Ast_literal.Lid.opaque;loc})[(Nolabel,Exp.field~loc(Exp.constraint_~locfn(Typ.constr~loc{txt=Ldot(Ast_literal.Lid.js_fn,"arity"^arity_s);loc;}[Typ.any~loc()])){txt=Ast_literal.Lid.hidden_fieldarity_s;loc});])args)letmethod_apply loc(self:Ast_mapper.mapper)(obj:Parsetree.expression)name(args:Ast_compatible.args)=letobj=self.exprselfobjinletargs=Ext_list.mapargs(fun(lbl,e)->Bs_syntaxerr.optional_errloclbl;(lbl,self.exprselfe))inlet fn=Exp.mk~loc(Ast_util.js_propertylocobjname)inletargs=matchargs with|[(Nolabel,{pexp_desc=Pexp_construct({txt=Lident"()"},None)});]->[]|_->argsinletarity=List.lengthargsinif arity=0thenParsetree.Pexp_apply(Exp.ident{txt=Ldot(Ldot(Ast_literal.Lid.js_oo,"Internal"),"run");loc},[(Nolabel,fn)])elseletarity_s=string_of_intarityinopaque_full_apply~loc(Exp.apply ~loc(Exp.apply~loc(Exp.ident~loc{txt=Ast_literal.Lid.opaque;loc})[(Nolabel,Exp.field~loc(Exp.constraint_~locfn(Typ.constr~loc{txt=Ldot(Ast_literal.Lid.js_meth,"arity"^arity_s);loc;}[Typ.any~loc()])){loc;txt=Ast_literal.Lid.hidden_fieldarity_s});])args)letuncurry_fn_apply locselffnargs=generic_applylocselffnargs(fun _obj->obj)letproperty_applylocselfobjnameargs=generic_applylocselfobjargs(funlocobj->Exp.mk~loc(Ast_util.js_propertylocobjname))