@@ -311,40 +311,6 @@ let jsxMapper () =
311
311
#end
312
312
313
313
#if BS_COMPILER_IN_BROWSER then
314
- let rewrite code =
315
- let mapper = jsxMapper () in
316
- Location. input_name := " //toplevel//" ;
317
- try
318
- let lexer = Lexing. from_string code in
319
- let pstr = Parse. implementation lexer in
320
- let pstr = mapper.structure mapper pstr in
321
- Pprintast. structure Format. str_formatter pstr;
322
- let ocaml_code = Format. flush_str_formatter () in
323
- Printf. sprintf " {\" ocaml_code\" : %S}" ocaml_code
324
- with e ->
325
- match Location. error_of_exn e with
326
- | Some error ->
327
- let (file, line, startchar) =
328
- Location. get_pos_info error.loc.loc_start in
329
- let (file, endline, endchar) =
330
- Location. get_pos_info error.loc.loc_end in
331
- let result =
332
- Printf. sprintf
333
- " { \
334
- \" ppx_error_msg\" : %S, \
335
- \" row\" : %d, \
336
- \" column\" : %d, \
337
- \" endRow\" : %d, \
338
- \" endColumn\" : %d, \
339
- \" text\" : %S, \
340
- \" type\" : \" error\" \
341
- }"
342
- (Printf. sprintf " Line %d, %d: %s" line startchar error.msg)
343
- (line - 1 ) startchar (endline - 1 ) endchar error.msg in
344
- Location. report_error Format. std_formatter error;
345
- result
346
- | None ->
347
- Printf. sprintf " {\" js_error_msg\" : %S}" (Printexc. to_string e)
348
314
349
315
module Js = struct
350
316
module Unsafe = struct
@@ -365,6 +331,40 @@ module Js = struct
365
331
external to_string : js_string t -> string = "caml_js_to_string"
366
332
end
367
333
334
+ (* keep in sync with jscomp/core/jsoo_main.ml `let implementation` *)
335
+ let rewrite code =
336
+ let mapper = jsxMapper () in
337
+ Location. input_name := " //toplevel//" ;
338
+ try
339
+ let lexer = Lexing. from_string code in
340
+ let pstr = Parse. implementation lexer in
341
+ let pstr = mapper.structure mapper pstr in
342
+ let buffer = Buffer. create 1000 in
343
+ Pprintast. structure Format. str_formatter pstr;
344
+ let ocaml_code = Format. flush_str_formatter () in
345
+ Js.Unsafe. (obj [| " ocaml_code" , inject @@ Js. string ocaml_code |])
346
+ with e ->
347
+ match Location. error_of_exn e with
348
+ | Some error ->
349
+ Location. report_error Format. std_formatter error;
350
+ let (file, line, startchar) = Location. get_pos_info error.loc.loc_start in
351
+ let (file, endline, endchar) = Location. get_pos_info error.loc.loc_end in
352
+ Js.Unsafe. (obj
353
+ [|
354
+ " ppx_error_msg" , inject @@ Js. string (Printf. sprintf " Line %d, %d: %s" line startchar error.msg);
355
+ " row" , inject (line - 1 );
356
+ " column" , inject startchar;
357
+ " endRow" , inject (endline - 1 );
358
+ " endColumn" , inject endchar;
359
+ " text" , inject @@ Js. string error.msg;
360
+ " type" , inject @@ Js. string " error" ;
361
+ |]
362
+ )
363
+ | None ->
364
+ Js.Unsafe. (obj [|
365
+ " js_error_msg" , inject @@ Js. string (Printexc. to_string e)
366
+ |])
367
+
368
368
let export (field : string ) v =
369
369
Js.Unsafe. set (Js.Unsafe. global) field v
370
370
@@ -374,9 +374,9 @@ let make_ppx name =
374
374
[|" rewrite" ,
375
375
inject @@
376
376
Js. wrap_meth_callback
377
- (fun _ code ->
378
- Js. string (rewrite (Js. to_string code)));
377
+ (fun _ code -> rewrite (Js. to_string code));
379
378
|]))
379
+
380
380
let () = make_ppx " jsxv2"
381
381
#elif defined BS_NO_COMPILER_PATCH then
382
382
let () = Compiler_libs.Ast_mapper. register " JSX" (fun _argv -> jsxMapper () )
0 commit comments