Skip to content

Commit be3fc3a

Browse files
authored
Merge pull request rescript-lang#982 from bloomberg/gpr_974
fix grr rescript-lang#974
2 parents 7be7e19 + a8e136b commit be3fc3a

13 files changed

+109
-16
lines changed

Diff for: .travis.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ node_js:
99
# This delays notification
1010

1111
# Not a very reliable service..
12-
script: npm run coveralls
12+
# script: npm run coveralls
13+
# FIXME: instrumentation takes too much time
1314

Diff for: jscomp/all.depend

+1-2
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,7 @@ core/lam_dispatch_primitive.cmx : core/js_stmt_make.cmx \
353353
core/js_of_lam_tuple.cmx core/js_of_lam_option.cmx \
354354
core/js_of_lam_exception.cmx core/js_of_lam_array.cmx core/js_long.cmx \
355355
core/js_exp_make.cmx common/js_config.cmx core/js_analyzer.cmx core/j.cmx \
356-
common/ext_log.cmx ext/ext_list.cmx ext/ext_ident.cmx \
357-
core/lam_dispatch_primitive.cmi
356+
common/ext_log.cmx ext/ext_list.cmx core/lam_dispatch_primitive.cmi
358357
core/lam_exit_code.cmx : core/lam.cmx core/lam_exit_code.cmi
359358
core/lam_group.cmx : core/lam_util.cmx core/lam_print.cmx core/lam.cmx \
360359
core/js_number.cmx ext/ident_set.cmx core/lam_group.cmi

Diff for: jscomp/bin/whole_compiler.ml

+10-1
Original file line numberDiff line numberDiff line change
@@ -79063,6 +79063,9 @@ There are two things we need consider:
7906379063
2. For some standard library functions, we prefer to replace with javascript primitives
7906479064
For example [Pervasives["^"] -> ^]
7906579065
We can collect all mli files in OCaml and replace it with an efficient javascript runtime
79066+
79067+
TODO: return type to be expression is ugly,
79068+
we should allow return block
7906679069
*)
7906779070
let translate (prim_name : string)
7906879071
(args : J.expression list) : J.expression =
@@ -80027,10 +80030,13 @@ let translate (prim_name : string)
8002780030
| Var _ ->
8002880031
E.econd (E.is_undef e) Js_of_lam_option.none (Js_of_lam_option.some e)
8002980032
| _ ->
80033+
call Js_config.js_primitive
80034+
(* # GPR 974
8003080035
let id = Ext_ident.create "v" in
8003180036
let tmp = E.var id in
8003280037
E.(seq (assign tmp e )
8003380038
(econd (is_undef tmp) Js_of_lam_option.none (Js_of_lam_option.some tmp)) )
80039+
*)
8003480040
end
8003580041

8003680042
| _ -> assert false
@@ -80042,11 +80048,14 @@ let translate (prim_name : string)
8004280048
begin match e.expression_desc with
8004380049
| Var _ ->
8004480050
E.econd (E.is_nil e) Js_of_lam_option.none (Js_of_lam_option.some e)
80045-
| _ ->
80051+
| _ ->
80052+
call Js_config.js_primitive
80053+
(* GPR #974
8004680054
let id = Ext_ident.create "v" in
8004780055
let tmp = E.var id in
8004880056
E.(seq (assign tmp e )
8004980057
(econd (is_nil tmp) Js_of_lam_option.none (Js_of_lam_option.some tmp)) )
80058+
*)
8005080059
end
8005180060

8005280061
| _ -> assert false

Diff for: jscomp/core/lam_dispatch_primitive.ml

+10-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ There are two things we need consider:
4040
2. For some standard library functions, we prefer to replace with javascript primitives
4141
For example [Pervasives["^"] -> ^]
4242
We can collect all mli files in OCaml and replace it with an efficient javascript runtime
43+
44+
TODO: return type to be expression is ugly,
45+
we should allow return block
4346
*)
4447
let translate (prim_name : string)
4548
(args : J.expression list) : J.expression =
@@ -1004,10 +1007,13 @@ let translate (prim_name : string)
10041007
| Var _ ->
10051008
E.econd (E.is_undef e) Js_of_lam_option.none (Js_of_lam_option.some e)
10061009
| _ ->
1010+
call Js_config.js_primitive
1011+
(* # GPR 974
10071012
let id = Ext_ident.create "v" in
10081013
let tmp = E.var id in
10091014
E.(seq (assign tmp e )
10101015
(econd (is_undef tmp) Js_of_lam_option.none (Js_of_lam_option.some tmp)) )
1016+
*)
10111017
end
10121018

10131019
| _ -> assert false
@@ -1019,11 +1025,14 @@ let translate (prim_name : string)
10191025
begin match e.expression_desc with
10201026
| Var _ ->
10211027
E.econd (E.is_nil e) Js_of_lam_option.none (Js_of_lam_option.some e)
1022-
| _ ->
1028+
| _ ->
1029+
call Js_config.js_primitive
1030+
(* GPR #974
10231031
let id = Ext_ident.create "v" in
10241032
let tmp = E.var id in
10251033
E.(seq (assign tmp e )
10261034
(econd (is_nil tmp) Js_of_lam_option.none (Js_of_lam_option.some tmp)) )
1035+
*)
10271036
end
10281037

10291038
| _ -> assert false

Diff for: jscomp/runtime/js_primitive.ml

+7
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ let js_from_nullable_def ( x : 'a Js.null_undefined) =
3535
None
3636
else Some (Obj.magic x : 'a)
3737

38+
let js_from_def ( x : 'a Js.undefined) =
39+
if (Obj.magic x) == Js.undefined then None
40+
else Some (Obj.magic x : 'a)
41+
42+
let js_from_nullable ( x : 'a Js.null) =
43+
if (Obj.magic x) == Js.null then None
44+
else Some (Obj.magic x : 'a)
3845

3946
let option_get (x : 'a option) : 'a Js_undefined.t =
4047
match x with

Diff for: jscomp/runtime/js_primitive.mli

+4
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,8 @@ val js_is_nil_undef : 'a Js.null_undefined -> bool
2929

3030
val js_from_nullable_def : 'a Js.null_undefined -> 'a option
3131

32+
val js_from_def : 'a Js.undefined -> 'a option
33+
34+
val js_from_nullable : 'a Js.null -> 'a option
35+
3236
val option_get : 'a option -> 'a Js_undefined.t

Diff for: jscomp/test/.depend

+1
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ gpr_658.cmj : ../runtime/js.cmj
183183
gpr_858_test.cmj : ../stdlib/list.cmj
184184
gpr_858_unit2_test.cmj :
185185
gpr_904_test.cmj : mt.cmj
186+
gpr_974_test.cmj : ../runtime/js.cmj
186187
guide_for_ext.cmj :
187188
hamming_test.cmj : ../stdlib/printf.cmj mt.cmj ../stdlib/lazy.cmj \
188189
../stdlib/int64.cmj ../stdlib/buffer.cmj

Diff for: jscomp/test/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ OTHERS := literals a test_ari test_export2 test_internalOO test_obj_simple_ffi t
7171
flow_parser_reg_test utf8_decode_test stream_parser_test condition_compilation_test semver_test update_record_test installation_test app_root_finder derive_projector_test\
7272
gpr_904_test gpr_858_unit2_test inner_unused \
7373
bal_set_common bal_tree string_set string_set_test \
74-
math_test bal_set_mini
74+
math_test bal_set_mini gpr_974_test
7575

7676

7777

Diff for: jscomp/test/gpr_974_test.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict';
2+
3+
var Caml_obj = require("../../lib/js/caml_obj");
4+
var Caml_builtin_exceptions = require("../../lib/js/caml_builtin_exceptions");
5+
var Js_primitive = require("../../lib/js/js_primitive");
6+
7+
if (!Caml_obj.caml_equal(Js_primitive.js_from_nullable_def(""), /* Some */[""])) {
8+
throw [
9+
Caml_builtin_exceptions.assert_failure,
10+
[
11+
"gpr_974_test.ml",
12+
5,
13+
4
14+
]
15+
];
16+
}
17+
18+
if (!Caml_obj.caml_equal(Js_primitive.js_from_def(""), /* Some */[""])) {
19+
throw [
20+
Caml_builtin_exceptions.assert_failure,
21+
[
22+
"gpr_974_test.ml",
23+
6,
24+
4
25+
]
26+
];
27+
}
28+
29+
if (!Caml_obj.caml_equal(Js_primitive.js_from_nullable(""), /* Some */[""])) {
30+
throw [
31+
Caml_builtin_exceptions.assert_failure,
32+
[
33+
"gpr_974_test.ml",
34+
7,
35+
4
36+
]
37+
];
38+
}
39+
40+
/* Not a pure module */

Diff for: jscomp/test/gpr_974_test.ml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
3+
let _ =
4+
begin
5+
assert (Js.Null_undefined.to_opt (Js.Null_undefined.return "" ) = Some "");
6+
assert (Js.Undefined.to_opt (Js.Undefined.return "" ) = Some "");
7+
assert (Js.Null.to_opt (Js.Null.return "") = Some "")
8+
end

Diff for: jscomp/test/map_test.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ let int_map_suites = let open Mt in Int_map.[
3030

3131
"iteration", (fun _ ->
3232
let m = ref String_map.empty in
33-
let count = 10000 in
33+
let count = 1_0000 in
3434
for i = 0 to count do
3535
m := String_map.add (string_of_int i) (string_of_int i) !m
3636
done;

Diff for: lib/js/caml_weak.js

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
'use strict';
22

3-
var Caml_obj = require("./caml_obj");
4-
var Caml_array = require("./caml_array");
3+
var Caml_obj = require("./caml_obj");
4+
var Caml_array = require("./caml_array");
5+
var Js_primitive = require("./js_primitive");
56

67
function caml_weak_create(n) {
78
return new Array(n);
@@ -18,13 +19,7 @@ function caml_weak_set(xs, i, v) {
1819
}
1920

2021
function caml_weak_get(xs, i) {
21-
v = xs[i];
22-
if (v === undefined) {
23-
return /* None */0;
24-
}
25-
else {
26-
return [v];
27-
}
22+
return Js_primitive.js_from_def(xs[i]);
2823
}
2924

3025
function caml_weak_get_copy(xs, i) {

Diff for: lib/js/js_primitive.js

+20
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,24 @@ function js_from_nullable_def(x) {
1919
}
2020
}
2121

22+
function js_from_def(x) {
23+
if (x === undefined) {
24+
return /* None */0;
25+
}
26+
else {
27+
return /* Some */[x];
28+
}
29+
}
30+
31+
function js_from_nullable(x) {
32+
if (x === null) {
33+
return /* None */0;
34+
}
35+
else {
36+
return /* Some */[x];
37+
}
38+
}
39+
2240
function option_get(x) {
2341
if (x) {
2442
return x[0];
@@ -30,5 +48,7 @@ function option_get(x) {
3048

3149
exports.js_is_nil_undef = js_is_nil_undef;
3250
exports.js_from_nullable_def = js_from_nullable_def;
51+
exports.js_from_def = js_from_def;
52+
exports.js_from_nullable = js_from_nullable;
3353
exports.option_get = option_get;
3454
/* No side effect */

0 commit comments

Comments
 (0)