Skip to content

Commit f01c62a

Browse files
committed
more polyfills
1 parent 6b64d84 commit f01c62a

19 files changed

+212
-50
lines changed

jscomp/bin/whole_compiler.ml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92477,20 +92477,21 @@ let translate (prim_name : string)
9247792477
(** No cross compilation *)
9247892478
Js_of_lam_tuple.make [E.str Sys.os_type; E.small_int Sys.word_size;
9247992479
E.bool Sys.big_endian ]
92480-
| "caml_sys_get_argv" ->
92480+
| "caml_sys_get_argv"
9248192481
(** TODO: refine
9248292482
Inlined here is helpful for DCE
9248392483
{[ external get_argv: unit -> string * string array = "caml_sys_get_argv" ]}
9248492484
*)
92485-
Js_of_lam_tuple.make [E.str "cmd";
92486-
Js_of_lam_array.make_array NA Pgenarray []
92487-
]
92485+
(* Js_of_lam_tuple.make [E.str "cmd"; *)
92486+
(* Js_of_lam_array.make_array NA Pgenarray [] *)
92487+
(* ] *)
9248892488
| "caml_sys_time"
9248992489
| "caml_sys_random_seed"
9249092490
| "caml_sys_getenv"
9249192491
| "caml_sys_system_command"
9249292492
| "caml_sys_getcwd" (* check browser or nodejs *)
9249392493
| "caml_sys_is_directory"
92494+
| "caml_sys_exit"
9249492495
(* | "caml_sys_file_exists" *)
9249592496
->
9249692497
call Js_config.sys
@@ -92820,7 +92821,7 @@ let translate (prim_name : string)
9282092821
| "caml_ml_input_int"
9282192822
| "caml_ml_close_channel"
9282292823
| "caml_ml_output_int"
92823-
| "caml_sys_exit"
92824+
9282492825
| "caml_ml_channel_size_64"
9282592826
| "caml_ml_channel_size"
9282692827
| "caml_ml_pos_in_64"

jscomp/core/lam_dispatch_primitive.ml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -462,20 +462,21 @@ let translate (prim_name : string)
462462
(** No cross compilation *)
463463
Js_of_lam_tuple.make [E.str Sys.os_type; E.small_int Sys.word_size;
464464
E.bool Sys.big_endian ]
465-
| "caml_sys_get_argv" ->
465+
| "caml_sys_get_argv"
466466
(** TODO: refine
467467
Inlined here is helpful for DCE
468468
{[ external get_argv: unit -> string * string array = "caml_sys_get_argv" ]}
469469
*)
470-
Js_of_lam_tuple.make [E.str "cmd";
471-
Js_of_lam_array.make_array NA Pgenarray []
472-
]
470+
(* Js_of_lam_tuple.make [E.str "cmd"; *)
471+
(* Js_of_lam_array.make_array NA Pgenarray [] *)
472+
(* ] *)
473473
| "caml_sys_time"
474474
| "caml_sys_random_seed"
475475
| "caml_sys_getenv"
476476
| "caml_sys_system_command"
477477
| "caml_sys_getcwd" (* check browser or nodejs *)
478478
| "caml_sys_is_directory"
479+
| "caml_sys_exit"
479480
(* | "caml_sys_file_exists" *)
480481
->
481482
call Js_config.sys
@@ -805,7 +806,7 @@ let translate (prim_name : string)
805806
| "caml_ml_input_int"
806807
| "caml_ml_close_channel"
807808
| "caml_ml_output_int"
808-
| "caml_sys_exit"
809+
809810
| "caml_ml_channel_size_64"
810811
| "caml_ml_channel_size"
811812
| "caml_ml_pos_in_64"

jscomp/others/.depend

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ js_json.cmj : js_types.cmj js_string.cmj js_dict.cmj js_array.cmj \
1212
js_obj.cmj :
1313
bs_dyn.cmj : bs_dyn.cmi
1414
bs_dyn_lib.cmj : bs_dyn.cmj bs_dyn_lib.cmi
15-
node_child_process.cmj :
15+
node_child_process.cmj : node.cmj
1616
js_boolean.cmj : js_boolean.cmi
1717
js_math.cmj :
1818
js_dict.cmj : js_dict.cmi

jscomp/others/node_child_process.ml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,20 @@ external option : ?cwd:string -> ?encoding:string -> unit -> option = "" [@@bs.o
3030

3131
(* TODO: when no option it would return buffer *)
3232
external execSync : string -> option -> string = "" [@@bs.module "child_process"]
33+
34+
(* Note we have to make it abstract type, since if you declare it as
35+
[ < pid : float > Js.t ], then you will create other external
36+
functions which will work with this type too, it is not what you want
37+
*)
38+
type spawnResult
39+
40+
41+
external spawnSync : string -> spawnResult = "" [@@bs.module "child_process"]
42+
43+
external readAs : spawnResult ->
44+
< pid : int ;
45+
status : int Js.null;
46+
signal : string Js.null ;
47+
stdout : Node.string_buffer Js.null ;
48+
stderr : Node.string_buffer Js.null > Js.t =
49+
"%identity"

jscomp/others/node_process.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ external process : t = "" [@@bs.module]
3939

4040
external exit : int -> unit = "" [@@bs.module "process"]
4141

42-
42+
(** The process.uptime() method returns the number of seconds
43+
the current Node.js process has been running.) *)
44+
external uptime : t -> unit -> float = "" [@@bs.send]
4345
let putEnvVar key (var : string) =
4446
Js_dict.set process##env key var
4547
(** Note that

jscomp/others/node_process.mli

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,9 @@ external process : t = "" [@@bs.module]
3737

3838
external exit : int -> unit = "" [@@bs.module "process"]
3939

40+
(** The process.uptime() method returns the number of seconds
41+
the current Node.js process has been running.) *)
42+
external uptime : t -> unit -> float = "" [@@bs.send]
43+
4044
val putEnvVar : string -> string -> unit
4145
val deleteEnvVar : string -> unit

jscomp/runtime/.depend

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ caml_int64.cmj : js_typed_array.cmj js_float.cmj caml_utils.cmj \
66
caml_int32.cmj bs_string.cmj caml_int64.cmi
77
caml_exceptions.cmj : caml_builtin_exceptions.cmj caml_exceptions.cmi
88
caml_utils.cmj : caml_utils.cmi
9-
caml_sys.cmj : caml_sys.cmi
9+
caml_sys.cmj : js.cmj caml_sys.cmi
1010
caml_io.cmj : js_undefined.cmj js.cmj bs_string.cmj
1111
caml_float.cmj : js_typed_array.cmj js_float.cmj caml_float.cmi
1212
caml_lexer.cmj : caml_lexer.cmi
@@ -32,7 +32,7 @@ caml_module.cmj :
3232
caml_missing_polyfill.cmj : caml_missing_polyfill.cmi
3333
bs_string.cmj :
3434
js_float.cmj :
35-
js_exn.cmj : js.cmj caml_exceptions.cmj js_exn.cmi
35+
js_exn.cmj : caml_exceptions.cmj js_exn.cmi
3636
bs_obj.cmj : js.cmj
3737
js_nativeint.cmj :
3838
js_int.cmj :
@@ -62,6 +62,6 @@ js_primitive.cmi : js_undefined.cmi js.cmi
6262
caml_basic.cmi : js_undefined.cmi js.cmi
6363
caml_oo.cmi :
6464
caml_missing_polyfill.cmi :
65-
js_exn.cmi : js.cmi
65+
js_exn.cmi :
6666
js_null.cmi : js.cmi
6767
js_undefined.cmi : js.cmi

jscomp/runtime/caml_sys.ml

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,18 @@ let caml_sys_getenv s =
4242
external now : unit -> float = "" [@@bs.val "Date.now"]
4343

4444

45-
let caml_initial_time = now () *. 0.001
45+
(* let caml_initial_time = now () *. 0.001 *)
4646

47-
let caml_sys_time () = (now () *. 0.001) -. caml_initial_time
47+
type process
48+
external uptime : process -> unit -> float = "" [@@bs.send]
49+
external exit : process -> int -> 'a = "" [@@bs.send]
50+
51+
let caml_sys_time () =
52+
match [%external process] with
53+
| None -> -1.
54+
| Some x -> uptime x ()
55+
56+
(* (now () *. 0.001) -. caml_initial_time *)
4857

4958
external random : unit -> float = "Math.random" [@@bs.val]
5059

@@ -54,12 +63,47 @@ let caml_sys_random_seed () : nativeint array =
5463
((Nativeint.to_float (Nativeint.logxor (Nativeint.of_float (now ()))
5564
0xffffffffn)) *. random ()) |]
5665

57-
let caml_sys_system_command () = 127
66+
type spawnResult
67+
68+
external spawnSync : string -> spawnResult = "" [@@bs.module "child_process"]
69+
70+
external readAs : spawnResult ->
71+
<
72+
status : int Js.null;
73+
> Js.t =
74+
"%identity"
75+
76+
(** This will pull in 'child_process', we should investigate more*)
77+
(* let caml_sys_system_command cmd = *)
78+
(* match Js_null.to_opt (readAs (spawnSync cmd)) ##status with *)
79+
(* | None -> 127 (\* command not found *\) *)
80+
(* | Some i -> i *)
81+
82+
let caml_sys_system_command _cmd = 127
83+
84+
let caml_sys_getcwd () =
85+
match [%external process] with
86+
| None -> "/"
87+
| Some x -> x##cwd ()
88+
89+
(* Called by {!Sys} in the toplevel, should never fail*)
90+
let caml_sys_get_argv () : string * string array =
91+
match [%external process] with
92+
| None -> ("",[|""|])
93+
| Some process
94+
-> Array.unsafe_get process##argv 0, process##argv
5895

59-
let caml_sys_getcwd () = "/"
96+
(** {!Pervasives.sys_exit} *)
97+
let caml_sys_exit exit_code =
98+
match [%external process] with
99+
| None -> ()
100+
| Some x -> exit x exit_code
60101

61102
let caml_sys_is_directory _s =
62103
raise @@ Failure "caml_sys_is_directory not implemented"
63104

105+
(** Need polyfill to make cmdliner work
106+
{!Sys.is_directory} or {!Sys.file_exists} {!Sys.command}
107+
*)
64108
let caml_sys_file_exists _s =
65109
raise @@ Failure "caml_sys_file_exists not implemented"

jscomp/runtime/caml_sys.mli

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,19 @@
2424

2525

2626

27-
28-
29-
30-
31-
3227
val caml_sys_getenv : string -> string
3328

3429
val caml_sys_time : unit -> float
3530

3631
val caml_sys_random_seed : unit -> nativeint array
3732

38-
val caml_sys_system_command : unit -> int
33+
val caml_sys_system_command : string -> int
3934

4035
val caml_sys_getcwd : unit -> string
4136

37+
val caml_sys_get_argv : unit -> string * string array
38+
39+
val caml_sys_exit : int -> unit
40+
4241
val caml_sys_is_directory : string -> bool
4342
val caml_sys_file_exists : string -> bool

jscomp/runtime/js_exn.ml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

2525

26-
type t =
27-
< stack : string Js.undefined ;
28-
message : string Js.undefined ;
29-
name : string Js.undefined;
30-
fileName : string Js.undefined
31-
> Js.t
26+
type t (* = *)
27+
(* < stack : string Js.undefined ; *)
28+
(* message : string Js.undefined ; *)
29+
(* name : string Js.undefined; *)
30+
(* fileName : string Js.undefined *)
31+
(* > Js.t *)
3232

3333
exception Error of t
3434

jscomp/runtime/js_exn.mli

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,7 @@
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

25-
type t =
26-
< stack : string Js.undefined ;
27-
message : string Js.undefined ;
28-
name : string Js.undefined;
29-
fileName : string Js.undefined
30-
> Js.t
25+
type t
3126

3227

3328
exception Error of t

jscomp/test/.depend

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ abstract_type.cmj : abstract_type.cmi
5050
alias_test.cmj : ../stdlib/string.cmj alias_test.cmi
5151
and_or_tailcall_test.cmj : mt.cmj
5252
app_root_finder.cmj : ../others/node.cmj ../runtime/js.cmj
53+
argv_test.cmj : ../stdlib/arg.cmj
5354
ari_regress_test.cmj : mt.cmj ari_regress_test.cmi
5455
arith_lexer.cmj : ../stdlib/lexing.cmj arith_syntax.cmj arith_parser.cmj
5556
arith_parser.cmj : ../stdlib/parsing.cmj ../stdlib/obj.cmj \

jscomp/test/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ OTHERS := literals a test_ari test_export2 test_internalOO test_obj_simple_ffi t
140140
gpr_1501_test\
141141
exception_repr_test\
142142
nested_pattern_match_test\
143-
caml_sys_poly_fill_test
143+
caml_sys_poly_fill_test\
144+
argv_test
144145

145146

146147
# bs_uncurry_test

jscomp/test/argv_test.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict';
2+
3+
var Arg = require("../../lib/js/arg.js");
4+
var Block = require("../../lib/js/block.js");
5+
6+
function anno_fun() {
7+
return /* () */0;
8+
}
9+
10+
var usage_msg = "Usage:\n";
11+
12+
var compile = [/* false */0];
13+
14+
var test = [/* true */1];
15+
16+
var arg_spec_000 = /* tuple */[
17+
"-c",
18+
/* Set */Block.__(2, [compile]),
19+
" Compile"
20+
];
21+
22+
var arg_spec_001 = /* :: */[
23+
/* tuple */[
24+
"-d",
25+
/* Clear */Block.__(3, [test]),
26+
" Test"
27+
],
28+
/* [] */0
29+
];
30+
31+
var arg_spec = /* :: */[
32+
arg_spec_000,
33+
arg_spec_001
34+
];
35+
36+
Arg.parse(arg_spec, anno_fun, usage_msg);
37+
38+
exports.anno_fun = anno_fun;
39+
exports.usage_msg = usage_msg;
40+
exports.compile = compile;
41+
exports.test = test;
42+
exports.arg_spec = arg_spec;
43+
/* Not a pure module */

jscomp/test/argv_test.ml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
3+
let anno_fun arg =
4+
()
5+
(* Js.log arg *)
6+
let usage_msg = "Usage:\n";;
7+
let compile = ref false
8+
let test = ref true
9+
let arg_spec = Arg.[
10+
"-c" , Set compile ,
11+
" Compile";
12+
"-d", Clear test,
13+
" Test"
14+
]
15+
16+
;;
17+
Arg.parse arg_spec anno_fun usage_msg

jscomp/test/test_per.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
var Curry = require("../../lib/js/curry.js");
44
var Caml_io = require("../../lib/js/caml_io.js");
55
var Caml_obj = require("../../lib/js/caml_obj.js");
6+
var Caml_sys = require("../../lib/js/caml_sys.js");
67
var Caml_format = require("../../lib/js/caml_format.js");
78
var Caml_string = require("../../lib/js/caml_string.js");
89
var Caml_exceptions = require("../../lib/js/caml_exceptions.js");
@@ -502,9 +503,9 @@ function do_at_exit() {
502503
return Curry._1(exit_function[0], /* () */0);
503504
}
504505

505-
function exit() {
506+
function exit(retcode) {
506507
Curry._1(exit_function[0], /* () */0);
507-
return Caml_missing_polyfill.not_implemented("caml_sys_exit not implemented by bucklescript yet\n");
508+
return Caml_sys.caml_sys_exit(retcode);
508509
}
509510

510511
var max_int = 2147483647;

0 commit comments

Comments
 (0)