Skip to content

Commit 129d5cb

Browse files
author
Hongbo Zhang
committed
add bindings to node.process, make unit tests work with both mocha and node
1 parent f655b26 commit 129d5cb

16 files changed

+223
-136
lines changed

jscomp/others/.depend

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
bs_node.cmi : bs_node_path.cmj bs_node_fs.cmj
1+
bs_node.cmi : bs_node_process.cmj bs_node_path.cmj bs_node_fs.cmj
22
bs_node_fs.cmj :
33
bs_node_fs.cmx :
44
bs_node_path.cmj :
55
bs_node_path.cmx :
6+
bs_node_process.cmj :
7+
bs_node_process.cmx :
68
bs_node_fs.cmo :
79
bs_node_fs.cmj :
810
bs_node_path.cmo :
911
bs_node_path.cmj :
12+
bs_node_process.cmo :
13+
bs_node_process.cmj :

jscomp/others/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ include ../Makefile.shared
22

33
COMPILER=../bin/bsc
44

5-
SOURCE_LIST= bs_node bs_node_path bs_node_fs
5+
SOURCE_LIST= bs_node bs_node_path bs_node_fs bs_node_process
66

77

88
RUNTIME := $(addsuffix .cmj, $(SOURCE_LIST))

jscomp/others/bs_node.mli

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ module Path = Bs_node_path
2828

2929
module Fs = Bs_node_fs
3030

31+
module Process = Bs_node_process
32+
3133
type node_exports
3234
type node_module = <
3335
id : string ;

jscomp/others/bs_node_process.ml

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
2+
*
3+
* This program is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU Lesser General Public License as published by
5+
* the Free Software Foundation, either version 3 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* In addition to the permissions granted to you by the LGPL, you may combine
9+
* or link a "work that uses the Library" with a publicly distributed version
10+
* of this file to produce a combined library or application, then distribute
11+
* that combined work under the terms of your choosing, with no requirement
12+
* to comply with the obligations normally placed on you by section 4 of the
13+
* LGPL version 3 (or the corresponding section of a later version of the LGPL
14+
* should you choose to use a later version).
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Lesser General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Lesser General Public License
22+
* along with this program; if not, write to the Free Software
23+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
24+
25+
type t =
26+
< argv : string array;
27+
arch : string ;
28+
abort : unit -> unit [@bs.meth];
29+
chdir : string -> unit [@bs.meth];
30+
(** @raise *)
31+
cwd : unit -> string [@bs.meth];
32+
disconnect : unit -> unit [@bs.meth];
33+
> Js.t
34+
35+
external process : t = "" [@@bs.module]

jscomp/syntax/ast_comb.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ let to_js_type loc x =
7777
let to_js_re_type loc =
7878
Typ.constr ~loc { txt = re_id ; loc} []
7979

80-
let to_js_undefined_type loc x =
80+
let to_undefined_type loc x =
8181
Typ.constr ~loc
8282
{txt = Ast_literal.Lid.js_undefined ; loc}
8383
[x]

jscomp/syntax/ast_comb.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ val to_js_type :
5858

5959

6060
(** TODO: make it work for browser too *)
61-
val to_js_undefined_type :
61+
val to_undefined_type :
6262
Location.t -> Parsetree.core_type -> Parsetree.core_type
6363

6464
val to_js_re_type : Location.t -> Parsetree.core_type

jscomp/syntax/ast_external_attributes.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ let handle_attributes
446446
Ldot (Lident "*predef*", "option") },
447447
[ty])}
448448
->
449-
(s, [], Ast_comb.to_js_undefined_type loc ty) :: acc
449+
(s, [], Ast_comb.to_undefined_type loc ty) :: acc
450450
| _ -> assert false
451451
end
452452
| (_, _), Ast_core_type.Empty -> acc

jscomp/syntax/ppx_entry.ml

+27-23
Original file line numberDiff line numberDiff line change
@@ -294,31 +294,35 @@ let rec unsafe_mapper : Ast_mapper.mapper =
294294
({txt = "bs.node"; loc},
295295
payload)
296296
->
297+
let strip s =
298+
let len = String.length s in
299+
if s.[len - 1] = '_' then
300+
String.sub s 0 (len - 1)
301+
else s in
297302
begin match Ast_payload.as_ident payload with
298-
| Some {txt = Lident ("__filename" | "__dirname" as name); loc}
303+
| Some {txt = Lident
304+
("__filename"
305+
| "__dirname"
306+
| "module_"
307+
| "require" as name); loc}
299308
->
300-
301-
Exp.constraint_ ~loc
302-
(Ast_util.handle_raw loc
303-
(Ast_payload.raw_string_payload loc
304-
name ))
305-
(Ast_literal.type_string ~loc ())
306-
| Some {txt = Lident "__module"}
307-
->
308-
Exp.constraint_ ~loc
309-
(Ast_util.handle_raw loc
310-
(Ast_payload.raw_string_payload loc "module"))
311-
(Typ.constr ~loc
312-
{ txt = Ldot (Lident "Bs_node", "node_module") ;
313-
loc} [] )
314-
| Some {txt = Lident "__require"}
315-
->
316-
Exp.constraint_ ~loc
317-
(Ast_util.handle_raw loc
318-
(Ast_payload.raw_string_payload loc "require"))
319-
(Typ.constr ~loc
320-
{ txt = Ldot (Lident "Bs_node", "node_require") ;
321-
loc} [] )
309+
let exp =
310+
Ast_util.handle_raw loc
311+
(Ast_payload.raw_string_payload loc
312+
(strip name) ) in
313+
let typ =
314+
Ast_comb.to_undefined_type loc @@
315+
if name = "module_" then
316+
Typ.constr ~loc
317+
{ txt = Ldot (Lident "Bs_node", "node_module") ;
318+
loc} []
319+
else if name = "require" then
320+
(Typ.constr ~loc
321+
{ txt = Ldot (Lident "Bs_node", "node_require") ;
322+
loc} [] )
323+
else
324+
Ast_literal.type_string ~loc () in
325+
Exp.constraint_ ~loc exp typ
322326
| Some _ | None -> Location.raise_errorf ~loc "Ilegal payload"
323327
end
324328

jscomp/test/.depend

+4-4
Original file line numberDiff line numberDiff line change
@@ -392,8 +392,8 @@ module_missing_conversion.cmx : ../stdlib/string.cmx \
392392
../stdlib/moreLabels.cmx ../stdlib/hashtbl.cmx ../stdlib/array.cmx
393393
module_parameter_test.cmj : ../stdlib/string.cmi mt.cmi
394394
module_parameter_test.cmx : ../stdlib/string.cmx mt.cmx
395-
mt.cmj : ../stdlib/list.cmi mt.cmi
396-
mt.cmx : ../stdlib/list.cmx mt.cmi
395+
mt.cmj : ../stdlib/list.cmi ../others/bs_node.cmi ../stdlib/array.cmi mt.cmi
396+
mt.cmx : ../stdlib/list.cmx ../others/bs_node.cmi ../stdlib/array.cmx mt.cmi
397397
mt_global.cmj : mt.cmi mt_global.cmi
398398
mt_global.cmx : mt.cmx mt_global.cmi
399399
mutual_non_recursive_type.cmj :
@@ -1152,8 +1152,8 @@ module_missing_conversion.cmj : ../stdlib/string.cmj \
11521152
../stdlib/moreLabels.cmj ../stdlib/hashtbl.cmj ../stdlib/array.cmj
11531153
module_parameter_test.cmo : ../stdlib/string.cmi mt.cmi
11541154
module_parameter_test.cmj : ../stdlib/string.cmj mt.cmj
1155-
mt.cmo : ../stdlib/list.cmi mt.cmi
1156-
mt.cmj : ../stdlib/list.cmj mt.cmi
1155+
mt.cmo : ../stdlib/list.cmi ../others/bs_node.cmi ../stdlib/array.cmi mt.cmi
1156+
mt.cmj : ../stdlib/list.cmj ../others/bs_node.cmi ../stdlib/array.cmj mt.cmi
11571157
mt_global.cmo : mt.cmi mt_global.cmi
11581158
mt_global.cmj : mt.cmj mt_global.cmi
11591159
mutual_non_recursive_type.cmo :

jscomp/test/fs_test.js

+20-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

3-
var Fs = require("fs");
43
var Mt = require("./mt");
4+
var Fs = require("fs");
55
var Block = require("../../lib/js/block");
66
var Path = require("path");
77

@@ -28,31 +28,36 @@ function eq(loc, param) {
2828
return /* () */0;
2929
}
3030

31-
var current_file = (__filename);
31+
var match = (__filename);
32+
33+
var current_file = match !== undefined ? match : "<Not Node JS>";
34+
35+
var match$1 = (__dirname);
3236

33-
var current_dir_name = (__dirname);
37+
var current_dir_name = match$1 !== undefined ? match$1 : "<Not Node Js>";
3438

3539
Fs.readFileSync(current_file, "utf8");
3640

3741
Fs.readdirSync(current_dir_name);
3842

3943
var pathobj = Path.parse(current_dir_name);
4044

41-
var module_ = (module);
42-
43-
console.log(/* tuple */[
44-
module_.id,
45-
module_.paths
46-
]);
47-
48-
eq('File "fs_test.ml", line 38, characters 5-12', /* tuple */[
49-
pathobj.name,
50-
"test"
51-
]);
45+
var match$2 = (module);
46+
47+
if (match$2 !== undefined) {
48+
console.log(/* tuple */[
49+
match$2.id,
50+
match$2.paths
51+
]);
52+
eq('File "fs_test.ml", line 45, characters 7-14', /* tuple */[
53+
pathobj.name,
54+
"test"
55+
]);
56+
}
5257

5358
Mt.from_pair_suites("fs_test.ml", suites[0]);
5459

5560
exports.suites = suites;
5661
exports.test_id = test_id;
5762
exports.eq = eq;
58-
/* current_file Not a pure module */
63+
/* match Not a pure module */

jscomp/test/fs_test.ml

+15-6
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,24 @@ external on :
2828

2929
open Bs_node
3030
let () =
31-
let current_file : string = [%bs.node __filename] in
32-
let current_dir_name : string = [%bs.node __dirname] in
31+
let current_file : string =
32+
match Js.Undefined.to_opt [%bs.node __filename] with
33+
| Some x -> x
34+
| None -> "<Not Node JS>" in
35+
let current_dir_name : string =
36+
match Js.Undefined.to_opt [%bs.node __dirname] with
37+
| Some x -> x
38+
| None -> "<Not Node Js>" in
3339
let _content = readFileSync current_file `utf8 in
3440
let _file_list = Fs.readdirSync current_dir_name in
3541
let pathobj = Path.parse current_dir_name in
36-
let module_ = [%bs.node __module] in
37-
Js.log (module_##id, module_##paths) ;
38-
eq __LOC__ (pathobj##name, "test" )
42+
match Js.Undefined.to_opt [%bs.node module_] with
43+
| Some module_ ->
44+
Js.log (module_##id, module_##paths) ;
45+
eq __LOC__ (pathobj##name, "test" )
46+
| None -> ()
3947

4048

4149

42-
let () = Mt.from_pair_suites __FILE__ !suites
50+
let () =
51+
Mt.from_pair_suites __FILE__ !suites

jscomp/test/mt.js

+60-45
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,77 @@
11
'use strict';
22

3+
var Assert = require("assert");
4+
var Path = require("path");
35
var Caml_builtin_exceptions = require("../../lib/js/caml_builtin_exceptions");
46
var Curry = require("../../lib/js/curry");
5-
var Assert = require("assert");
7+
var Process = require("process");
8+
var $$Array = require("../../lib/js/array");
69
var List = require("../../lib/js/list");
710

811
function from_suites(name, suite) {
9-
describe(name, function () {
10-
return List.iter(function (param) {
11-
it(param[0], param[1]);
12-
return /* () */0;
13-
}, suite);
14-
});
15-
return /* () */0;
12+
var match = $$Array.to_list(Process.argv);
13+
if (match && Path.basename(match[0]) === "mocha") {
14+
describe(name, function () {
15+
return List.iter(function (param) {
16+
it(param[0], param[1]);
17+
return /* () */0;
18+
}, suite);
19+
});
20+
return /* () */0;
21+
}
22+
else {
23+
return /* () */0;
24+
}
1625
}
1726

1827
function close_enough(x, y) {
1928
return +(Math.abs(x - y) < 0.0000001);
2029
}
2130

2231
function from_pair_suites(name, suites) {
23-
describe(name, function () {
24-
return List.iter(function (param) {
25-
var code = param[1];
26-
it(param[0], function () {
27-
var match = Curry._1(code, /* () */0);
28-
switch (match.tag | 0) {
29-
case 0 :
30-
Assert.deepEqual(match[0], match[1]);
31-
return /* () */0;
32-
case 1 :
33-
Assert.notDeepEqual(match[0], match[1]);
34-
return /* () */0;
35-
case 2 :
36-
if (close_enough(match[0], match[1])) {
37-
return 0;
38-
}
39-
else {
40-
throw [
41-
Caml_builtin_exceptions.assert_failure,
42-
[
43-
"mt.ml",
44-
56,
45-
16
46-
]
47-
];
48-
}
49-
break;
50-
case 3 :
51-
Assert.throws(match[0]);
52-
return /* () */0;
53-
54-
}
55-
});
56-
return /* () */0;
57-
}, suites);
58-
});
59-
return /* () */0;
32+
var match = $$Array.to_list(Process.argv);
33+
if (match && Path.basename(match[0]) === "mocha") {
34+
describe(name, function () {
35+
return List.iter(function (param) {
36+
var code = param[1];
37+
it(param[0], function () {
38+
var match = Curry._1(code, /* () */0);
39+
switch (match.tag | 0) {
40+
case 0 :
41+
Assert.deepEqual(match[0], match[1]);
42+
return /* () */0;
43+
case 1 :
44+
Assert.notDeepEqual(match[0], match[1]);
45+
return /* () */0;
46+
case 2 :
47+
if (close_enough(match[0], match[1])) {
48+
return 0;
49+
}
50+
else {
51+
throw [
52+
Caml_builtin_exceptions.assert_failure,
53+
[
54+
"mt.ml",
55+
64,
56+
20
57+
]
58+
];
59+
}
60+
break;
61+
case 3 :
62+
Assert.throws(match[0]);
63+
return /* () */0;
64+
65+
}
66+
});
67+
return /* () */0;
68+
}, suites);
69+
});
70+
return /* () */0;
71+
}
72+
else {
73+
return /* () */0;
74+
}
6075
}
6176

6277
exports.from_suites = from_suites;

0 commit comments

Comments
 (0)