123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
*
* 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_helperletffi_of_labelslabels=Melange_ffi.External_ffi_types.ffi_obj_create(List.fold_rightlabels~init:[]~f:(fun(p:stringwith_loc)arg_kinds->{Melange_ffi.External_arg_spec.arg_type=Nothing;arg_label=Melange_ffi.External_arg_spec.Obj_label.obj(Melange_ffi.Lam_methname.translatep.txt);}::arg_kinds))letocaml_object_as_js_object=letlocal_extern_cont_to_objloc~ffi~pval_type?(local_module_name="J")?(local_fun_name="unsafe_expr")(cb:expression->'a):expression_desc=Pexp_letmodule({txt=Somelocal_module_name;loc},{pmod_desc=Pmod_structure[{pstr_desc=Pstr_primitive{pval_name={txt=local_fun_name;loc};pval_type;pval_loc=loc;pval_prim=Ast_external.pval_prim_default;pval_attributes=[Ast_attributes.mel_ffiffi];};pstr_loc=loc;};];pmod_loc=loc;pmod_attributes=[];},cb{pexp_desc=Pexp_ident{txt=Ldot(Lidentlocal_module_name,local_fun_name);loc};pexp_attributes=[];pexp_loc=loc;pexp_loc_stack=[loc];})infunloc(mapper:Ast_traverse.map)(self_pat:pattern)(clfs:class_fieldlist)->(* Attention: we should avoid type variable conflict for each method
Since the method name is unique, there would be no conflict
OCaml does not allow duplicate instance variable and duplicate methods,
but it does allow duplicates between instance variable and method name,
we should enforce such rules
{[
object [@u]
val x = 3
method x = 3
end
]} should not compile with a meaningful error message
*)letgenerate_val_method_pairloc(mapper:Ast_traverse.map)(val_name:stringAsttypes.loc)is_mutable=letresult=Typ.var~locval_name.txtin(result,Of.tagval_nameresult::(ifis_mutablethen[Of.tag{val_namewithtxt=val_name.txt^Melange_ffi.External_ffi_types.Literals.setter_suffix;}(Ast_typ_uncurry.to_method_typelocmapperNolabelresult[%type:unit]);]else[]))in(* Note mapper is only for API compatible
* TODO: we should check label name to avoid conflict
*)(* we need calculate the real object type
and exposed object type, in some cases there are equivalent
for public object type its [@meth] it does not depend on itself
while for label argument it is [@this] which depends internal object
*)let((internal_label_attr_types:object_fieldlist),(public_label_attr_types:object_fieldlist))=List.fold_right~f:(fun({pcf_loc=loc;_}asx:class_field)(label_attr_types,public_label_attr_types)->matchx.pcf_descwith|Pcf_method(label,public_flag,Cfk_concrete(Fresh,e))->(matche.pexp_descwith|Pexp_poly({pexp_desc=Pexp_fun(lbl,_,pat,e);_},None)->letmethod_type=Ast_typ_uncurry.generate_arg_typex.pcf_locmapperlabel.txtlblpatein(Of.taglabelmethod_type::label_attr_types,ifpublic_flag=PublicthenOf.taglabelmethod_type::public_label_attr_typeselsepublic_label_attr_types)|Pexp_poly(_,Some_)->Location.raise_errorf~loc"polymorphic type annotation not supported yet"|Pexp_poly(_,None)->Location.raise_errorf~loc"Unsupported JS Object syntax. Methods must take at least \
one argument"|_->Location.raise_errorf~loc"Unsupported syntax in js object")|Pcf_val(label,mutable_flag,Cfk_concrete(Fresh,_))->let_,label_attr=generate_val_method_pairx.pcf_locmapperlabel(mutable_flag=Mutable)in(List.appendlabel_attrlabel_attr_types,public_label_attr_types)|Pcf_val(_,_,Cfk_concrete(Override,_))->Location.raise_errorf~loc"override flag not support currently"|Pcf_val(_,_,Cfk_virtual_)->Location.raise_errorf~loc"virtual flag not support currently"|Pcf_method(_,_,Cfk_concrete(Override,_))->Location.raise_errorf~loc"override flag not supported"|Pcf_method(_,_,Cfk_virtual_)->Location.raise_errorf~loc"virtural method not supported"|Pcf_inherit_|Pcf_initializer_|Pcf_attribute_|Pcf_extension_|Pcf_constraint_->Location.raise_errorf~loc"Only method support currently")clfs~init:([],[])inletinternal_obj_type=Ast_core_type.make_obj~locinternal_label_attr_typesinletpublic_obj_type=Ast_core_type.to_js_type~loc(Typ.object_~locpublic_label_attr_typesClosed)inletlabels,label_types,exprs,_=List.fold_right~f:(fun(x:class_field)(labels,label_types,exprs,aliased)->matchx.pcf_descwith|Pcf_method(label,_public_flag,Cfk_concrete(Fresh,e))->(matche.pexp_descwith|Pexp_poly(({pexp_desc=Pexp_fun(ll,None,pat,e);_}asf),None)->letalias_type=ifaliasedthenNoneelseSomeinternal_obj_typeinletlabel_type=Ast_typ_uncurry.generate_method_type?alias_typex.pcf_locmapperlabel.txtllpatein(label::labels,label_type::label_types,{fwithpexp_desc=(letf=Ast_pat.is_unit_contpat~yes:e~no:finAst_uncurry_gen.to_method_callbacklocmapperNolabelself_patf)(* the first argument is this*);}::exprs,true)|Pexp_poly(_,Some_)->Location.raise_errorf~loc"polymorphic type annotation not supported yet"|Pexp_poly(_,None)->Location.raise_errorf~loc"Unsupported syntax, expect syntax like `method x () = x ` "|_->Location.raise_errorf~loc"Unsupported syntax in js object")|Pcf_val(label,mutable_flag,Cfk_concrete(Fresh,val_exp))->letlabel_type,_=generate_val_method_pairx.pcf_locmapperlabel(mutable_flag=Mutable)in(label::labels,label_type::label_types,mapper#expressionval_exp::exprs,aliased)|Pcf_val(_,_,Cfk_concrete(Override,_))->Location.raise_errorf~loc"override flag not support currently"|Pcf_val(_,_,Cfk_virtual_)->Location.raise_errorf~loc"virtual flag not support currently"|Pcf_method(_,_,Cfk_concrete(Override,_))->Location.raise_errorf~loc"override flag not supported"|Pcf_method(_,_,Cfk_virtual_)->Location.raise_errorf~loc"virtural method not supported"|Pcf_inherit_|Pcf_initializer_|Pcf_attribute_|Pcf_extension_|Pcf_constraint_->Location.raise_errorf~loc"Only method support currently")clfs~init:([],[],[],false)inletpval_type=List.fold_right2~f:(funlabellabel_typeacc->Typ.arrow~loc:label.Asttypes.loc(Labelledlabel.Asttypes.txt)label_typeacc)labelslabel_types~init:public_obj_typeinlocal_extern_cont_to_objloc~ffi:(ffi_of_labelslabels)(fune->Exp.apply~loce(List.map2~f:(funlexpr->(Labelledl.txt,expr))labelsexprs))~pval_typeletrecord_as_js_object=letlocal_external_objloc~ffi~pval_type?(local_module_name="J")?(local_fun_name="unsafe_expr")args:expression_desc=Pexp_letmodule({txt=Somelocal_module_name;loc},{pmod_desc=Pmod_structure[{pstr_desc=Pstr_primitive{pval_name={txt=local_fun_name;loc};pval_type;pval_loc=loc;pval_prim=["";""];pval_attributes=[Ast_attributes.mel_ffiffi];};pstr_loc=loc;};];pmod_loc=loc;pmod_attributes=[];},Exp.apply({pexp_desc=Pexp_ident{txt=Ldot(Lidentlocal_module_name,local_fun_name);loc};pexp_attributes=[];pexp_loc=loc;pexp_loc_stack=[loc];}:expression)(List.map~f:(fun(l,a)->(Asttypes.Labelledl,a))args)~loc)in(* Note that OCaml type checker will not allow arbitrary
name as type variables, for example:
{[
'_x'_
]}
will be recognized as a invalid program
*)letfrom_labels~locaritylabels:core_type=lettyvars=List.init~len:arity~f:(funi->Typ.var~loc("a"^string_of_inti))inletresult_type=Ast_core_type.to_js_type~loc(Typ.object_~loc(List.map2~f:(funxy->Of.tagxy)labelstyvars)Closed)inList.fold_right2~f:(funlabel(* {loc ; txt = label } *)tyvaracc->Typ.arrow~loc:label.loc(Labelledlabel.txt)tyvaracc)labelstyvars~init:result_typeinfun~loc(label_exprs:(Longident.tAsttypes.loc*expression)list):expression_desc->letlabels,args,arity=List.fold_right~f:(fun({txt;loc},e)(labels,args,i)->matchtxtwith|Lidentobj_label->letobj_label=Ast_attributes.iter_process_mel_string_ase.pexp_attributes|>Option.value~default:obj_labelin({Asttypes.loc;txt=obj_label}::labels,(obj_label,e)::args,i+1)|Ldot_|Lapply_->Location.raise_errorf~loc"`%%mel.obj' literals only support simple labels")label_exprs~init:([],[],0)inlocal_external_objloc~ffi:(ffi_of_labelslabels)~pval_type:(from_labels~locaritylabels)args