Skip to content

Commit 9c1cc43

Browse files
committed
Switch to jsoo_refmt_main build target
This will restore the original `jsoo_main.ml` and introduce an enhanced `jsoo_refmt_main` with the `Refmt_Api` dependency built in. Also adds a rule to `jscomp/snapshot.ninja` to build bytecode for the new `jsoo_refmt_main.ml` file, so we can `repl.js` it into a bundle file. `repl.js` will build the new jsoo_refmt_main bytecode target instead.
1 parent 46f65ce commit 9c1cc43

File tree

7 files changed

+316
-51
lines changed

7 files changed

+316
-51
lines changed

jscomp/main/jsoo_main.ml

+34-49
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,6 @@ end
5656
* type: "error" // or "warning" or "info"
5757
* }
5858
*)
59-
let mk_js_error loc msg =
60-
let (file,line,startchar) = Location.get_pos_info loc.Location.loc_start in
61-
let (file,endline,endchar) = Location.get_pos_info loc.Location.loc_end in
62-
Js.Unsafe.(obj
63-
[|
64-
"js_error_msg",
65-
inject @@ Js.string (Printf.sprintf "Line %d, %d:\n %s" line startchar msg);
66-
"row" , inject (line - 1);
67-
"column" , inject startchar;
68-
"endRow" , inject (endline - 1);
69-
"endColumn" , inject endchar;
70-
"text" , inject @@ Js.string msg;
71-
"type" , inject @@ Js.string "error"
72-
|]
73-
)
74-
7559
let () =
7660
Bs_conditional_initial.setup_env ();
7761
Clflags.binary_annotations := false
@@ -88,7 +72,7 @@ let error_of_exn e =
8872

8973
type react_ppx_version = V2 | V3
9074

91-
let implementation ~use_super_errors ?(react_ppx_version=V3) ?prefix impl str : Js.Unsafe.obj =
75+
let implementation ~use_super_errors ?(react_ppx_version=V3) prefix impl str : Js.Unsafe.obj =
9276
let modulename = "Test" in
9377
(* let env = !Toploop.toplevel_env in *)
9478
(* Compmisc.init_path false; *)
@@ -107,10 +91,9 @@ let implementation ~use_super_errors ?(react_ppx_version=V3) ?prefix impl str :
10791
Warnings.parse_options false Bsb_warning.default_warning;
10892

10993
try
110-
let code = match prefix with
111-
| None -> str
112-
| Some(prefix) -> prefix ^ str in
113-
let ast = impl (Lexing.from_string code) in
94+
let ast = impl
95+
(Lexing.from_string
96+
(if prefix then "[@@@bs.config{no_export}]\n#1 \"repl.ml\"\n" ^ str else str )) in
11497
let ast = match react_ppx_version with
11598
| V2 -> Reactjs_jsx_ppx_v2.rewrite_implementation ast
11699
| V3 -> Reactjs_jsx_ppx_v3.rewrite_implementation ast in
@@ -141,29 +124,38 @@ let implementation ~use_super_errors ?(react_ppx_version=V3) ?prefix impl str :
141124
(* Format.fprintf output_ppf {| { "js_code" : %S }|} v ) *)
142125
with
143126
| e ->
144-
begin match error_of_exn e with
127+
begin match error_of_exn e with
145128
| Some error ->
146-
Location.report_error Format.err_formatter error;
147-
mk_js_error error.loc error.msg
129+
Location.report_error Format.err_formatter error;
130+
let (file,line,startchar) = Location.get_pos_info error.loc.loc_start in
131+
let (file,endline,endchar) = Location.get_pos_info error.loc.loc_end in
132+
Js.Unsafe.(obj
133+
[|
134+
"js_error_msg",
135+
inject @@ Js.string (Printf.sprintf "Line %d, %d:\n %s" line startchar error.msg);
136+
"row" , inject (line - 1);
137+
"column" , inject startchar;
138+
"endRow" , inject (endline - 1);
139+
"endColumn" , inject endchar;
140+
"text" , inject @@ Js.string error.msg;
141+
"type" , inject @@ Js.string "error"
142+
|]
143+
);
144+
148145
| None ->
149-
let msg = Printexc.to_string e in
150-
match e with
151-
| Refmt_api.Migrate_parsetree.Def.Migration_error (_,loc)
152-
| Refmt_api.Reason_errors.Reason_error (_,loc) ->
153-
mk_js_error loc msg
154-
| _ ->
155-
Js.Unsafe.(obj [|
156-
"js_error_msg" , inject @@ Js.string msg;
157-
"type" , inject @@ Js.string "error"
158-
|])
146+
Js.Unsafe.(obj [|
147+
"js_error_msg" , inject @@ Js.string (Printexc.to_string e)
148+
|])
149+
159150
end
160151

161152

162153
let compile impl ~use_super_errors ?react_ppx_version =
163-
implementation ~use_super_errors ?react_ppx_version impl
154+
implementation ~use_super_errors ?react_ppx_version false impl
164155

165-
let shake_compile impl ~use_super_errors ?react_ppx_version prefix =
166-
implementation ~use_super_errors ?react_ppx_version ~prefix impl
156+
(** TODO: add `[@@bs.config{no_export}]\n# 1 "repl.ml"`*)
157+
let shake_compile impl ~use_super_errors ?react_ppx_version =
158+
implementation ~use_super_errors ?react_ppx_version true impl
167159

168160

169161

@@ -188,12 +180,7 @@ let dir_directory d =
188180
let () =
189181
dir_directory "/static/cmis"
190182

191-
module Converter = Refmt_api.Migrate_parsetree.Convert(Refmt_api.Migrate_parsetree.OCaml_404)(Refmt_api.Migrate_parsetree.OCaml_406)
192-
193-
let reason_parse lexbuf =
194-
Refmt_api.Reason_toolchain.RE.implementation lexbuf |> Converter.copy_structure;;
195-
196-
let make_compiler name impl prefix =
183+
let make_compiler name impl =
197184
export name
198185
(Js.Unsafe.(obj
199186
[|"compile",
@@ -205,7 +192,7 @@ let make_compiler name impl prefix =
205192
inject @@
206193
Js.wrap_meth_callback
207194
(fun _ code ->
208-
(shake_compile impl ~use_super_errors:false (Js.to_string code) prefix));
195+
(shake_compile impl ~use_super_errors:false (Js.to_string code)));
209196
"compile_super_errors",
210197
inject @@
211198
Js.wrap_meth_callback
@@ -224,8 +211,8 @@ let make_compiler name impl prefix =
224211
"shake_compile_super_errors",
225212
inject @@
226213
Js.wrap_meth_callback
227-
(fun _ code -> (shake_compile impl ~use_super_errors:true (Js.to_string code) prefix));
228-
"version", Js.Unsafe.inject (Js.string (match name with | "reason" -> Refmt_api.version | _ -> Bs_version.version));
214+
(fun _ code -> (shake_compile impl ~use_super_errors:true (Js.to_string code)));
215+
"version", Js.Unsafe.inject (Js.string (Bs_version.version));
229216
"load_module",
230217
inject @@
231218
Js.wrap_meth_callback
@@ -236,9 +223,7 @@ let make_compiler name impl prefix =
236223
Js.Unsafe.set cmj_bytestring "t" 9;
237224
load_module cmi_path cmi_content (Js.to_string cmj_name) cmj_bytestring);
238225
|]))
239-
240-
let () = make_compiler "ocaml" Parse.implementation "[@@@bs.config{no_export}]\n#1 \"repl.ml\"\n"
241-
let () = make_compiler "reason" reason_parse "[@bs.config {no_export: no_export}];\n#1 \"repl.re\";\n"
226+
let () = make_compiler "ocaml" Parse.implementation
242227

243228
(* local variables: *)
244229
(* compile-command: "ocamlbuild -use-ocamlfind -pkg compiler-libs -no-hygiene driver.cmo" *)

jscomp/main/jsoo_main.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

25-
val make_compiler : string -> (Lexing.lexbuf -> Parsetree.structure) -> string -> unit
25+
val make_compiler : string -> (Lexing.lexbuf -> Parsetree.structure) -> unit

0 commit comments

Comments
 (0)