Skip to content

Commit 6144a88

Browse files
author
Hongbo Zhang
committed
refactoring
1 parent 3758f13 commit 6144a88

File tree

1 file changed

+31
-56
lines changed

1 file changed

+31
-56
lines changed

jscomp/ppx_entry.ml

Lines changed: 31 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ let curry_type_id () =
8686

8787
let ignore_id = Longident.Ldot (Lident "Pervasives", "ignore")
8888

89-
89+
let arrow = Ast_helper.Typ.arrow
9090

9191
(* note we first declare its type is [unit],
9292
then [ignore] it, [ignore] is necessary since
@@ -166,11 +166,6 @@ let handle_record_as_js_object
166166
let pval_attributes = [attr] in
167167
let local_module_name = "Tmp" in
168168
let local_fun_name = "run" in
169-
let arrow label a b =
170-
{Parsetree.ptyp_desc = Ptyp_arrow (label, a, b);
171-
ptyp_attributes = [];
172-
ptyp_loc = loc} in
173-
174169
let pval_type =
175170
let arity = List.length labels in
176171
let tyvars = (Ext_list.init arity (fun i ->
@@ -190,7 +185,7 @@ let handle_record_as_js_object
190185
ptyp_attributes = []
191186
} in
192187
List.fold_right2
193-
(fun label tyvar acc -> arrow label tyvar acc) labels tyvars result_type
188+
(fun label tyvar acc -> arrow ~loc label tyvar acc) labels tyvars result_type
194189
in
195190
create_local_external loc
196191
~pval_prim
@@ -225,14 +220,9 @@ let gen_fn_run loc arity args : Parsetree.expression_desc =
225220
ptyp_loc = loc }]);
226221
ptyp_attributes;
227222
ptyp_loc = loc} in
228-
let arrow a b =
229-
{ptyp_desc =
230-
Ptyp_arrow ("", a, b);
231-
ptyp_attributes ;
232-
ptyp_loc = loc} in
233223
(** could be optimized *)
234224
let pval_type =
235-
Ext_list.reduce_from_right arrow (uncurry_fn :: tyvars) in
225+
Ext_list.reduce_from_right (fun a b -> arrow ~loc "" a b) (uncurry_fn :: tyvars) in
236226
create_local_external loc ~pval_prim ~pval_type ~pval_attributes:[]
237227
local_module_name local_fun_name args
238228

@@ -262,15 +252,11 @@ let gen_fn_mk loc arity args : Parsetree.expression_desc =
262252
ptyp_loc = loc }]);
263253
ptyp_attributes;
264254
ptyp_loc = loc} in
265-
let arrow a b =
266-
{ptyp_desc =
267-
Ptyp_arrow ("", a, b);
268-
ptyp_attributes ;
269-
ptyp_loc = loc} in
255+
let arrow = arrow ~loc "" in
270256
(** could be optimized *)
271257
let pval_type =
272258
if arity = 0 then
273-
arrow (arrow predef_unit_type (List.hd tyvars) ) uncurry_fn
259+
arrow (arrow predef_unit_type (List.hd tyvars) ) uncurry_fn
274260
else
275261
arrow (Ext_list.reduce_from_right arrow tyvars) uncurry_fn in
276262
create_local_external loc ~pval_prim ~pval_type ~pval_attributes:[]
@@ -280,16 +266,13 @@ let gen_fn_mk loc arity args : Parsetree.expression_desc =
280266

281267

282268
let handle_raw loc e =
283-
Ast_helper.Exp.letmodule
284-
{txt = tmp_module_name; loc }
285-
(Ast_helper.Mod.structure [
286-
Ast_helper.Str.primitive
287-
(Ast_helper.Val.mk {loc ; txt = tmp_fn}
288-
~prim:[prim]
289-
(Ast_helper.Typ.arrow "" predef_string_type predef_any_type))])
290-
(Ast_helper.Exp.apply
291-
(Ast_helper.Exp.ident {txt= Ldot(Lident tmp_module_name, tmp_fn) ; loc})
292-
[("",e)])
269+
create_local_external loc
270+
~pval_prim:prim
271+
~pval_type:(arrow "" predef_string_type predef_any_type)
272+
~pval_attributes:[]
273+
tmp_module_name
274+
tmp_fn
275+
[("",e)]
293276

294277

295278

@@ -413,18 +396,13 @@ let handle_debugger loc payload =
413396
match payload with
414397
| Parsetree.PStr ( [])
415398
->
416-
Ast_helper.Exp.letmodule
417-
{txt = tmp_module_name; loc }
418-
(Ast_helper.Mod.structure [
419-
Ast_helper.Str.primitive
420-
(Ast_helper.Val.mk {loc ; txt = tmp_fn}
421-
~prim:[prim_debugger]
422-
(Ast_helper.Typ.arrow "" predef_unit_type predef_unit_type)
423-
)])
424-
(Ast_helper.Exp.apply
425-
(Ast_helper.Exp.ident
426-
{txt= Ldot(Lident tmp_module_name, tmp_fn) ; loc})
427-
[("", predef_val_unit)])
399+
create_local_external loc
400+
~pval_prim:prim_debugger
401+
~pval_type:(arrow "" predef_unit_type predef_unit_type)
402+
~pval_attributes:[]
403+
tmp_module_name
404+
tmp_fn
405+
[("", predef_val_unit)]
428406
| Parsetree.PTyp _
429407
| Parsetree.PPat (_,_)
430408
| Parsetree.PStr _
@@ -644,7 +622,7 @@ let rec unsafe_mapper : Ast_mapper.mapper =
644622
} as e ,
645623
_); pstr_loc = _ }]))
646624
->
647-
handle_raw loc e
625+
{e with pexp_desc = handle_raw loc e }
648626
| Pexp_extension({txt = "bs.raw"; loc}, (PTyp _ | PPat _ | PStr _))
649627
->
650628
Location.raise_errorf ~loc "bs.raw can only be applied to a string"
@@ -653,7 +631,7 @@ let rec unsafe_mapper : Ast_mapper.mapper =
653631

654632
(** Begin rewriting [bs.debugger], its output should not be rewritten any more*)
655633
| Pexp_extension ({txt = "bs.debugger"; loc} , payload)
656-
-> handle_debugger loc payload
634+
-> {e with pexp_desc = handle_debugger loc payload}
657635
(** End rewriting *)
658636
| Pexp_fun ("", None, pat , body)
659637
->
@@ -751,19 +729,16 @@ let rec unsafe_mapper : Ast_mapper.mapper =
751729
pexp_desc = Pexp_constant (Const_string (cont, opt_label)) ;
752730
pexp_loc; pexp_attributes } as e ,_); pstr_loc }])
753731
->
754-
Ast_helper.Str.eval @@
755-
Ast_helper.Exp.letmodule
756-
{txt = tmp_module_name; loc }
757-
(Ast_helper.Mod.structure [
758-
Ast_helper.Str.primitive
759-
(Ast_helper.Val.mk {loc ; txt = tmp_fn}
760-
~prim:[prim_stmt]
761-
(Ast_helper.Typ.arrow ""
762-
predef_string_type predef_any_type))])
763-
(Ast_helper.Exp.apply
764-
(Ast_helper.Exp.ident
765-
{txt= Ldot(Lident tmp_module_name, tmp_fn) ; loc})
766-
[("",e)])
732+
Ast_helper.Str.eval
733+
{ e with pexp_desc =
734+
create_local_external loc
735+
~pval_prim:prim_stmt
736+
~pval_type:(arrow ""
737+
predef_string_type predef_any_type)
738+
~pval_attributes:[]
739+
tmp_module_name
740+
tmp_fn
741+
[("",e)]}
767742
| Parsetree.PTyp _
768743
| Parsetree.PPat (_,_)
769744
| Parsetree.PStr _

0 commit comments

Comments
 (0)