@@ -84,6 +84,7 @@ module L = struct
84
84
let json = " JSON"
85
85
let stringify = " stringify"
86
86
let console = " console"
87
+ let define = " define"
87
88
end
88
89
let return_indent = (String. length L. return / Ext_pp. indent_length)
89
90
@@ -284,8 +285,10 @@ let rec pp_function
284
285
action return
285
286
else
286
287
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 () );
289
292
P. string f " (function(" ;
290
293
ignore @@ aux inner_cxt f lexical;
291
294
P. string f " )" ;
@@ -1350,69 +1353,76 @@ let amd_program f ({modules; block = b ; exports = exp ; side_effect } : J.prog
1350
1353
list ~pp_sep pp_v ppf vs in
1351
1354
1352
1355
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
1392
1397
1393
- in
1394
- ignore @@ aux cxt f exp);
1395
- P. string f " )" ;
1398
+ in
1399
+ ignore @@ aux cxt f exp);
1400
+ P. string f " )" ;
1396
1401
;;
1397
1402
1398
1403
let pp_program (program : J.program ) (f : Ext_pp.t ) =
1399
- let () =
1404
+ begin
1400
1405
P. string f " // Generated CODE, PLEASE EDIT WITH CARE" ;
1401
1406
P. newline f;
1402
- P. string f { | " use strict" ; | };
1407
+ P. string f " \" use strict\" ; " ; (* TODO: use single quote in another commit *)
1403
1408
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
1416
1426
let dump_program
1417
1427
(program : J.program )
1418
1428
(oc : out_channel ) =
0 commit comments