@@ -84,6 +84,7 @@ module L = struct
8484 let json = " JSON"
8585 let stringify = " stringify"
8686 let console = " console"
87+ let define = " define"
8788end
8889let return_indent = (String. length L. return / Ext_pp. indent_length)
8990
@@ -284,8 +285,10 @@ let rec pp_function
284285 action return
285286 else
286287 let lexical = Ident_set. elements lexical in
287-
288- (if return then P. string f " return " else () );
288+ (if return then begin
289+ P. string f L. return ;
290+ P. space f
291+ end else () );
289292 P. string f " (function(" ;
290293 ignore @@ aux inner_cxt f lexical;
291294 P. string f " )" ;
@@ -1350,69 +1353,76 @@ let amd_program f ({modules; block = b ; exports = exp ; side_effect } : J.prog
13501353 list ~pp_sep pp_v ppf vs in
13511354
13521355 P. vgroup f 1 @@ fun _ ->
1353- P. string f " define([" ;
1354- list ~pp_sep: (fun f _ -> P. string f L. comma)
1355- (fun f (_ ,s ) ->
1356- pp_string f ~utf: true ~quote: (best_string_quote s) s; ) f modules;
1357- P. string f " ]" ;
1358- P. string f L. comma;
1359- P. newline f;
1360- P. string f L. function_;
1361- P. string f " (" ;
1362- let cxt = aux cxt f modules in
1363- P. string f " )" ;
1364-
1365- P. brace_vgroup f 1 @@ (fun _ ->
1366- let cxt = statement_list true cxt f b in
1367- (* FIXME AMD : use {[ function xx ]} or {[ var x = function ..]} *)
1368- P. newline f;
1369- P. string f L. return;
1370- P. space f;
1371- P. brace_vgroup f 1 @@ fun _ ->
1372- let rec aux cxt f (idents : Ident.t list ) =
1373- match idents with
1374- | [] -> cxt
1375- | [id] ->
1376- P. string f (Ext_ident. convert id.name);
1377- P. space f ;
1378- P. string f L. colon;
1379- P. space f ;
1380- ident cxt f id
1381- | id :: rest
1382- ->
1383- P. string f (Ext_ident. convert id.name);
1384- P. space f ;
1385- P. string f L. colon;
1386- P. space f;
1387- let cxt = ident cxt f id in
1388- P. string f L. comma;
1389- P. space f ;
1390- P. newline f ;
1391- aux cxt f rest
1356+ P. string f L. define;
1357+ P. string f " ([" ;
1358+ list ~pp_sep: (fun f _ -> P. string f L. comma)
1359+ (fun f (_ ,s ) ->
1360+ pp_string f ~utf: true ~quote: (best_string_quote s) s; ) f modules;
1361+ P. string f " ]" ;
1362+ P. string f L. comma;
1363+ P. newline f;
1364+ P. string f L. function_;
1365+ P. string f " (" ;
1366+ let cxt = aux cxt f modules in
1367+ P. string f " )" ;
1368+ P. brace_vgroup f 1 @@ (fun _ ->
1369+ let () = P. string f " 'use strict';" in
1370+ let () = P. newline f in
1371+ let cxt = statement_list true cxt f b in
1372+ (* FIXME AMD : use {[ function xx ]} or {[ var x = function ..]} *)
1373+ P. newline f;
1374+ P. string f L. return;
1375+ P. space f;
1376+ P. brace_vgroup f 1 @@ fun _ ->
1377+ let rec aux cxt f (idents : Ident.t list ) =
1378+ match idents with
1379+ | [] -> cxt
1380+ | [id] ->
1381+ P. string f (Ext_ident. convert id.name);
1382+ P. space f ;
1383+ P. string f L. colon;
1384+ P. space f ;
1385+ ident cxt f id
1386+ | id :: rest
1387+ ->
1388+ P. string f (Ext_ident. convert id.name);
1389+ P. space f ;
1390+ P. string f L. colon;
1391+ P. space f;
1392+ let cxt = ident cxt f id in
1393+ P. string f L. comma;
1394+ P. space f ;
1395+ P. newline f ;
1396+ aux cxt f rest
13921397
1393- in
1394- ignore @@ aux cxt f exp);
1395- P. string f " )" ;
1398+ in
1399+ ignore @@ aux cxt f exp);
1400+ P. string f " )" ;
13961401;;
13971402
13981403let pp_program (program : J.program ) (f : Ext_pp.t ) =
1399- let () =
1404+ begin
14001405 P. string f " // Generated CODE, PLEASE EDIT WITH CARE" ;
14011406 P. newline f;
1402- P. string f { | " use strict" ; | };
1407+ P. string f " \" use strict\" ; " ; (* TODO: use single quote in another commit *)
14031408 P. newline f ;
1404- in
1405- (match Sys. getenv " OCAML_AMD_MODULE" with
1406- | exception Not_found ->
1407- ignore (node_program f program)
1408- | _ -> amd_program f program ) ;
1409- P. string f (
1410- match program.side_effect with
1411- | None -> " /* No side effect */"
1412- | Some v -> Printf. sprintf " /* %s Not a pure module */" v );
1413- P. newline f;
1414- P. flush f ()
1415-
1409+ (match Js_config. get_env () with
1410+ | Browser ->
1411+ ignore (node_program f program)
1412+ | NodeJS ->
1413+ begin match Sys. getenv " OCAML_AMD_MODULE" with
1414+ | exception Not_found ->
1415+ ignore (node_program f program)
1416+ | _ -> amd_program f program
1417+ end ) ;
1418+ P. newline f ;
1419+ P. string f (
1420+ match program.side_effect with
1421+ | None -> " /* No side effect */"
1422+ | Some v -> Printf. sprintf " /* %s Not a pure module */" v );
1423+ P. newline f;
1424+ P. flush f ()
1425+ end
14161426let dump_program
14171427 (program : J.program )
14181428 (oc : out_channel ) =
0 commit comments