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

Lines changed: 5 additions & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 35 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 27 additions & 23 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 20 additions & 15 deletions
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 */

0 commit comments

Comments
 (0)