Skip to content

Commit f27bef4

Browse files
committed
1.several bug fixes:
a. fix cml_ml_out_channels_list b. fix `no_side_effect` 2. add debug output for each pass 3. add js_config to add support for browser
1 parent b1771ae commit f27bef4

27 files changed

+349
-122
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,5 @@ ocaml/man
4848
jscomp/bench/*.js
4949
*.bak
5050
.vscode
51-
osc
51+
osc
52+
jscomp/pre_load.js

jscomp/compiler.mllib

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ lam_group
6161
lam_current_unit
6262

6363
j
64+
js_config
6465
js_program_loader
6566
js_output
6667
js_dump

jscomp/js.sh

+7
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,10 @@ js_of_ocaml -I +compiler-libs --toplevel +dynlink.js +toplevel.js +weak.js _bui
99

1010
rm -rf $OCAMLSCRIPT_DOC/js-demo/exports.js && cp _build/exports.js $OCAMLSCRIPT_DOC/js-demo/exports.js
1111

12+
ocamlbuild -use-ocamlfind -no-hygiene -no-links js_generate_require.byte --
13+
rm -rf $OCAMLSCRIPT_DOC/js-demo/pre_load.js
14+
cp ./pre_load.js $OCAMLSCRIPT_DOC/js-demo/
15+
# TODO: build with amd first
16+
cp runtime/*.js $OCAMLSCRIPT_DOC/js-demo/runtime
17+
cp stdlib/*.js $OCAMLSCRIPT_DOC/js-demo/stdlib
18+

jscomp/js_config.ml

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
(* OCamlScript compiler
2+
* Copyright (C) 2015-2016 Bloomberg Finance L.P.
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published by
6+
* the Free Software Foundation, with linking exception;
7+
* either version 2.1 of the License, or (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17+
*)
18+
19+
(* Author: Hongbo Zhang *)
20+
21+
22+
type env =
23+
| Browser
24+
| NodeJS
25+
26+
27+
let default_env = ref NodeJS
28+
29+
let get_env () = !default_env
30+
31+
let set_env env = default_env := env
32+
33+
let stdlib_set = String_set.of_list [
34+
"arg.js";
35+
"gc.js";
36+
"printexc.js";
37+
"array.js";
38+
"genlex.js";
39+
"printf.js";
40+
"arrayLabels.js";
41+
"hashtbl.js";
42+
"queue.js";
43+
"buffer.js";
44+
"int32.js";
45+
"random.js";
46+
"bytes.js";
47+
"int64.js";
48+
"scanf.js";
49+
"bytesLabels.js";
50+
"lazy.js";
51+
"set.js";
52+
"callback.js";
53+
"lexing.js";
54+
"sort.js";
55+
"camlinternalFormat.js";
56+
"list.js";
57+
"stack.js";
58+
"camlinternalFormatBasics.js";
59+
"listLabels.js";
60+
"stdLabels.js";
61+
"camlinternalLazy.js";
62+
"map.js";
63+
"std_exit.js";
64+
"camlinternalMod.js";
65+
"marshal.js";
66+
"stream.js";
67+
"camlinternalOO.js";
68+
"moreLabels.js";
69+
"string.js";
70+
"char.js";
71+
"nativeint.js";
72+
"stringLabels.js";
73+
"complex.js";
74+
"obj.js";
75+
"sys.js";
76+
"digest.js";
77+
"oo.js";
78+
"weak.js";
79+
"filename.js";
80+
"parsing.js";
81+
"format.js";
82+
"pervasives.js"
83+
]
84+
85+
let runtime_set = String_set.of_list [
86+
"caml_array.js";
87+
"caml_float.js";
88+
"caml_obj_runtime.js";
89+
(* "caml_sys.js"; *)
90+
"caml_bigarray.js";
91+
"caml_format.js";
92+
"caml_oo.js";
93+
(* "caml_unix.js"; *)
94+
"caml_c_ffi.js";
95+
"caml_int64.js";
96+
"caml_primitive.js";
97+
"caml_utils.js";
98+
"caml_exceptions.js";
99+
(* "caml_io.js"; *)
100+
"curry.js";
101+
"caml_file.js";
102+
"caml_lexer.js";
103+
"caml_string.js"
104+
]

jscomp/js_config.mli

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
(* OCamlScript compiler
2+
* Copyright (C) 2015-2016 Bloomberg Finance L.P.
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published by
6+
* the Free Software Foundation, with linking exception;
7+
* either version 2.1 of the License, or (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17+
*)
18+
19+
(* Author: Hongbo Zhang *)
20+
21+
type env =
22+
| Browser
23+
| NodeJS
24+
25+
26+
val get_env : unit -> env
27+
28+
val set_env : env -> unit
29+
30+
val runtime_set : String_set.t
31+
val stdlib_set : String_set.t
32+
33+

jscomp/js_dump.ml

+68-58
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ module L = struct
8484
let json = "JSON"
8585
let stringify = "stringify"
8686
let console = "console"
87+
let define = "define"
8788
end
8889
let 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

13981403
let 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
14161426
let dump_program
14171427
(program : J.program)
14181428
(oc : out_channel) =

jscomp/js_generate_require.ml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
let std_files =
3+
String_set.elements Js_config.stdlib_set
4+
|> List.map (fun x -> "./stdlib/" ^ x )
5+
let runtime_files =
6+
String_set.elements Js_config.runtime_set
7+
|> List.map (fun x -> "./runtime/" ^ x)
8+
9+
let () =
10+
Ext_pervasives.with_file_as_chan "./pre_load.js" (fun chan ->
11+
output_string chan (Printf.sprintf "require([%s], function(){})"
12+
(String.concat ","
13+
(List.map (Printf.sprintf "%S" ) (std_files @ runtime_files))))
14+
)

0 commit comments

Comments
 (0)