Skip to content

Commit 9601c08

Browse files
committed
re-org code and prepare to land
1 parent cb489c4 commit 9601c08

17 files changed

+1044
-441
lines changed

jscomp/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ SYNTAX_SRCS= \
247247
ast_derive ast_comb ast_attributes\
248248
ast_core_type ast_derive_dyn\
249249
ast_derive_projector \
250+
ast_derive_js_mapper\
250251
external_ffi_types\
251252
external_process\
252253
ast_util\

jscomp/all.depend

+8-4
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,11 @@ syntax/ast_derive_dyn.cmx : ext/ext_list.cmx syntax/bs_syntaxerr.cmx \
211211
syntax/ast_structure.cmx syntax/ast_derive_util.cmx syntax/ast_derive.cmx \
212212
syntax/ast_attributes.cmx syntax/ast_derive_dyn.cmi
213213
syntax/ast_derive_projector.cmx : ext/ext_list.cmx \
214-
syntax/ast_derive_util.cmx syntax/ast_derive.cmx \
214+
syntax/ast_derive_util.cmx syntax/ast_derive.cmx syntax/ast_comb.cmx \
215215
syntax/ast_derive_projector.cmi
216+
syntax/ast_derive_js_mapper.cmx : ext/ext_list.cmx \
217+
syntax/ast_derive_util.cmx syntax/ast_derive.cmx syntax/ast_comb.cmx \
218+
syntax/ast_derive_js_mapper.cmi
216219
syntax/external_ffi_types.cmx : syntax/external_arg_spec.cmx \
217220
ext/ext_string.cmx ext/ext_pervasives.cmx common/bs_version.cmx \
218221
syntax/external_ffi_types.cmi
@@ -232,9 +235,9 @@ syntax/ppx_entry.cmx : ext/string_map.cmx ext/literals.cmx \
232235
ext/ext_list.cmx syntax/ast_util.cmx syntax/ast_utf8_string_interp.cmx \
233236
syntax/ast_utf8_string.cmx syntax/ast_structure.cmx \
234237
syntax/ast_signature.cmx syntax/ast_payload.cmx syntax/ast_literal.cmx \
235-
syntax/ast_derive_projector.cmx syntax/ast_derive_dyn.cmx \
236-
syntax/ast_derive.cmx syntax/ast_core_type.cmx syntax/ast_comb.cmx \
237-
syntax/ast_attributes.cmx syntax/ppx_entry.cmi
238+
syntax/ast_derive_projector.cmx syntax/ast_derive_js_mapper.cmx \
239+
syntax/ast_derive_dyn.cmx syntax/ast_derive.cmx syntax/ast_core_type.cmx \
240+
syntax/ast_comb.cmx syntax/ast_attributes.cmx syntax/ppx_entry.cmi
238241
syntax/bs_ast_invariant.cmx : ext/literals.cmx ext/ext_string.cmx \
239242
common/bs_warnings.cmx syntax/bs_ast_iterator.cmx \
240243
syntax/ast_core_type.cmx syntax/bs_ast_invariant.cmi
@@ -259,6 +262,7 @@ syntax/ast_attributes.cmi : syntax/ast_payload.cmi
259262
syntax/ast_core_type.cmi :
260263
syntax/ast_derive_dyn.cmi :
261264
syntax/ast_derive_projector.cmi :
265+
syntax/ast_derive_js_mapper.cmi :
262266
syntax/external_ffi_types.cmi : syntax/external_arg_spec.cmi
263267
syntax/external_process.cmi : common/bs_loc.cmi syntax/ast_core_type.cmi \
264268
syntax/ast_attributes.cmi

jscomp/syntax/ast_comb.ml

+7
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,10 @@ let to_undefined_type loc x =
8282
{txt = Ast_literal.Lid.js_undefined ; loc}
8383
[x]
8484

85+
let single_non_rec_value name exp =
86+
Str.value Nonrecursive
87+
[Vb.mk (Pat.var name) exp]
88+
89+
let single_non_rec_val name ty =
90+
Sig.value
91+
(Val.mk name ty)

jscomp/syntax/ast_comb.mli

+10
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,13 @@ val to_undefined_type :
6262
Location.t -> Parsetree.core_type -> Parsetree.core_type
6363

6464
val to_js_re_type : Location.t -> Parsetree.core_type
65+
66+
val single_non_rec_value :
67+
Ast_helper.str ->
68+
Parsetree.expression ->
69+
Parsetree.structure_item
70+
71+
val single_non_rec_val :
72+
Ast_helper.str ->
73+
Parsetree.core_type ->
74+
Parsetree.signature_item

jscomp/syntax/ast_derive_js_mapper.ml

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
(* Copyright (C) 2017 Authors of BuckleScript
2+
*
3+
* This program is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU Lesser General Public License as published by
5+
* the Free Software Foundation, either version 3 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* In addition to the permissions granted to you by the LGPL, you may combine
9+
* or link a "work that uses the Library" with a publicly distributed version
10+
* of this file to produce a combined library or application, then distribute
11+
* that combined work under the terms of your choosing, with no requirement
12+
* to comply with the obligations normally placed on you by section 4 of the
13+
* LGPL version 3 (or the corresponding section of a later version of the LGPL
14+
* should you choose to use a later version).
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Lesser General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Lesser General Public License
22+
* along with this program; if not, write to the Free Software
23+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
24+
25+
open Ast_helper
26+
27+
type tdcls = Parsetree.type_declaration list
28+
29+
let js_field (o : Parsetree.expression) m =
30+
Exp.apply
31+
(Exp.ident {txt = Lident "##"; loc = o.pexp_loc})
32+
[
33+
"",o;
34+
"", Exp.ident m
35+
]
36+
37+
let invalid_config (config : Parsetree.expression) =
38+
Location.raise_errorf ~loc:config.pexp_loc "such configuration is not supported"
39+
40+
let init () =
41+
Ast_derive.register
42+
"jsMapper"
43+
(fun ( x : Parsetree.expression option) ->
44+
(match x with
45+
| Some config -> invalid_config config
46+
| None -> ());
47+
{
48+
structure_gen = (fun (tdcls : tdcls) _ ->
49+
let handle_tdcl tdcl =
50+
let core_type = Ast_derive_util.core_type_of_type_declaration tdcl
51+
in
52+
let name = tdcl.ptype_name.txt in
53+
let toJs = name ^ "ToJs" in
54+
let fromJs = name ^ "FromJs" in
55+
match tdcl.ptype_kind with
56+
| Ptype_record label_declarations ->
57+
let record_arg = "record" in
58+
let exp =
59+
Exp.record
60+
(List.map
61+
(fun ({pld_name = {loc; txt } } : Parsetree.label_declaration) ->
62+
{Asttypes.loc; txt = Longident.Lident txt },
63+
Exp.field (Exp.ident {txt = Lident record_arg ; loc })
64+
{Asttypes.loc; txt = Longident.Lident txt }
65+
) label_declarations) None in
66+
let loc = tdcl.ptype_loc in
67+
let toJs = Ast_comb.single_non_rec_value {loc; txt = toJs}
68+
(Exp.fun_ "" None (Pat.constraint_ (Pat.var {loc; txt = record_arg}) core_type)
69+
(Exp.extension ({Asttypes.loc; txt = "bs.obj"}, (PStr [Str.eval exp ]))))
70+
in
71+
let obj_arg = "obj" in
72+
let obj_exp =
73+
Exp.record
74+
(List.map
75+
(fun ({pld_name = {loc; txt } } : Parsetree.label_declaration) ->
76+
{Asttypes.loc; txt = Longident.Lident txt },
77+
js_field (Exp.ident {txt = Lident obj_arg ; loc })
78+
{Asttypes.loc; txt = Longident.Lident txt }
79+
) label_declarations) None in
80+
let fromJs =
81+
Ast_comb.single_non_rec_value {loc; txt = fromJs}
82+
(Exp.fun_ "" None (Pat.var {loc; txt = obj_arg})
83+
(Exp.constraint_ obj_exp core_type) )
84+
in
85+
[
86+
toJs;
87+
fromJs
88+
]
89+
| Ptype_variant _
90+
| Ptype_abstract | Ptype_open -> [] in
91+
Ext_list.flat_map handle_tdcl tdcls
92+
);
93+
signature_gen =
94+
(fun (tdcls : tdcls) _ ->
95+
let handle_tdcl tdcl =
96+
let core_type = Ast_derive_util.core_type_of_type_declaration tdcl
97+
in
98+
let name = tdcl.ptype_name.txt in
99+
let toJs = name ^ "ToJs" in
100+
let fromJs = name ^ "FromJs" in
101+
match tdcl.ptype_kind with
102+
| Ptype_record label_declarations ->
103+
let loc = tdcl.ptype_loc in
104+
let ty =
105+
Ast_comb.to_js_type loc @@
106+
Typ.object_
107+
(List.map
108+
(fun ({pld_name = {loc; txt }; pld_type } : Parsetree.label_declaration) ->
109+
txt, [], pld_type
110+
) label_declarations)
111+
Closed in
112+
let loc = tdcl.ptype_loc in
113+
let toJs =
114+
Ast_comb.single_non_rec_val {loc; txt = toJs}
115+
(Typ.arrow "" core_type ty) in
116+
let fromJs =
117+
Ast_comb.single_non_rec_val {loc; txt = fromJs}
118+
(Typ.arrow "" ty core_type) in
119+
[
120+
toJs;
121+
fromJs
122+
]
123+
| Ptype_variant _
124+
| Ptype_abstract | Ptype_open -> [] in
125+
Ext_list.flat_map handle_tdcl tdcls
126+
127+
);
128+
expression_gen = None
129+
}
130+
)
131+
;
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
(* Copyright (C) 2017 Authors of BuckleScript
2+
*
3+
* This program is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU Lesser General Public License as published by
5+
* the Free Software Foundation, either version 3 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* In addition to the permissions granted to you by the LGPL, you may combine
9+
* or link a "work that uses the Library" with a publicly distributed version
10+
* of this file to produce a combined library or application, then distribute
11+
* that combined work under the terms of your choosing, with no requirement
12+
* to comply with the obligations normally placed on you by section 4 of the
13+
* LGPL version 3 (or the corresponding section of a later version of the LGPL
14+
* should you choose to use a later version).
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Lesser General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Lesser General Public License
22+
* along with this program; if not, write to the Free Software
23+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
24+
25+
26+
27+
val init : unit -> unit

0 commit comments

Comments
 (0)