Skip to content

Commit 7dc4938

Browse files
authored
Merge pull request #3155 from BuckleScript/gpr_3154
fix #3154
2 parents 542c2d2 + f18a1b7 commit 7dc4938

14 files changed

+146
-34
lines changed

jscomp/cleanlib.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env node
22
var cp = require('child_process')
33
var fs = require('fs')
4-
exports.run = function () {
4+
function run() {
55
cp.execSync(`make clean`)
66
if (fs.existsSync(`stdlib-406`)) {
77
cp.execSync(`git clean -dfx stdlib-406`)
@@ -13,3 +13,5 @@ exports.run = function () {
1313
if(require.main === module){
1414
run()
1515
}
16+
17+
exports.run = run

jscomp/others/js_dict.ml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,19 @@ type 'a t
3030
(** The key type, an alias of string *)
3131
type key = string
3232

33-
(** [get dict key] returns the value associated with [key] in [dict] *)
34-
external get : 'a t -> key -> 'a option = "" [@@bs.get_index] [@@bs.return {undefined_to_opt}]
35-
3633
(** [unsafeGet dict key] returns the value associated with [key] in [dict]
3734
3835
This function will return an invalid value ([undefined]) if [key] does not exist in [dict]. It
3936
will not throw an error.
4037
*)
4138
external unsafeGet : 'a t -> key -> 'a = "" [@@bs.get_index]
4239

40+
(** [get dict key] returns the value associated with [key] in [dict] *)
41+
let get (type u) (dict : u t) (k : key) : u option =
42+
if [%raw {|k in dict|}] then
43+
Some (unsafeGet dict k)
44+
else None
45+
4346
(** [set dict key value] sets the value of [key] in [dict] to [value] *)
4447
external set : 'a t -> key -> 'a -> unit = "" [@@bs.set_index]
4548

jscomp/others/js_dict.mli

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ type 'a t
3232
type key = string
3333
(** Key type *)
3434

35-
external get :
35+
val get :
3636
'a t ->
3737
key ->
38-
'a option = "" [@@bs.get_index] [@@bs.return {undefined_to_opt}]
38+
'a option
3939
(** [get dict key] returns [None] if the [key] is not found in the
4040
dictionary, [Some value] otherwise *)
4141

jscomp/test/.depend

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ gpr_2789_test.cmj : mt.cmj
291291
gpr_2863_test.cmj : ../others/belt.cmj
292292
gpr_2931_test.cmj : mt.cmj
293293
gpr_3142_test.cmj : mt.cmj
294+
gpr_3154_test.cmj : mt.cmj ../runtime/js.cmj
294295
gpr_405_test.cmj : gpr_405_test.cmi
295296
gpr_441.cmj :
296297
gpr_459_test.cmj : mt.cmj
@@ -467,7 +468,7 @@ rec_module_test.cmj : mt.cmj
467468
rec_value_test.cmj : mt.cmj ../runtime/js.cmj
468469
record_debug_test.cmj : ../runtime/js.cmj ../others/belt.cmj
469470
record_with_test.cmj : mt.cmj
470-
recursive_module.cmj :
471+
recursive_module.cmj : mt.cmj
471472
recursive_module_test.cmj : mt.cmj
472473
regression_print.cmj :
473474
return_check.cmj : ../runtime/js.cmj

jscomp/test/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ OTHERS := test_literals a test_ari test_export2 test_internalOO test_obj_simple_
268268
406_primitive_test\
269269
fun_pattern_match\
270270
gpr_3142_test\
271+
gpr_3154_test\
271272
ocaml_typedtree_test
272273
# ocaml_typedtree_test is not cross version due to camlinternalFormat
273274
# bs_uncurry_test

jscomp/test/adt_optimize_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ function f11(x) {
160160
Caml_builtin_exceptions.assert_failure,
161161
/* tuple */[
162162
"adt_optimize_test.ml",
163-
155,
163+
191,
164164
9
165165
]
166166
];

jscomp/test/g

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/sh
2+
BS_DEBUG_FILE=$1 ../../lib/bsc.exe -I ../others/ -I ../stdlib-402 -I ../runtime/ -c $1

jscomp/test/gpr_3154_test.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'use strict';
2+
3+
var Mt = require("./mt.js");
4+
var Js_dict = require("../../lib/js/js_dict.js");
5+
var Js_primitive = require("../../lib/js/js_primitive.js");
6+
7+
var suites = /* record */[/* contents : [] */0];
8+
9+
var test_id = /* record */[/* contents */0];
10+
11+
function eq(loc, x, y) {
12+
return Mt.eq_suites(test_id, suites, loc, x, y);
13+
}
14+
15+
function b(loc, x) {
16+
return Mt.bool_suites(test_id, suites, loc, x);
17+
}
18+
19+
var d = { };
20+
21+
d["foo"] = undefined;
22+
23+
var match = Js_dict.get(d, "foo");
24+
25+
if (match !== undefined && Js_primitive.valFromOption(match) === undefined) {
26+
b("File \"gpr_3154_test.ml\", line 12, characters 19-26", true);
27+
} else {
28+
b("File \"gpr_3154_test.ml\", line 13, characters 11-18", false);
29+
}
30+
31+
var d0 = { };
32+
33+
d0["foo"] = undefined;
34+
35+
eq("File \"gpr_3154_test.ml\", line 18, characters 5-12", Js_dict.get(d0, "foo"), Js_primitive.some(undefined));
36+
37+
Mt.from_pair_suites("gpr_3154_test.ml", suites[0]);
38+
39+
var J = 0;
40+
41+
exports.suites = suites;
42+
exports.test_id = test_id;
43+
exports.eq = eq;
44+
exports.b = b;
45+
exports.J = J;
46+
/* Not a pure module */

jscomp/test/gpr_3154_test.ml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
let suites : Mt.pair_suites ref = ref []
2+
let test_id = ref 0
3+
let eq loc x y = Mt.eq_suites ~test_id ~suites loc x y
4+
let b loc x = Mt.bool_suites ~test_id ~suites loc x
5+
6+
module J = Js.Dict
7+
8+
let () =
9+
let d = Js.Dict.empty () in
10+
J.set d "foo" None;
11+
match J.get d "foo" with
12+
Some None -> b __LOC__ true
13+
| _ -> b __LOC__ false
14+
15+
let () =
16+
let d0 = Js.Dict.empty () in
17+
J.set d0 "foo" None ;
18+
eq __LOC__ (J.get d0 "foo") (Some None)
19+
20+
let () =
21+
Mt.from_pair_suites __FILE__ !suites
22+

jscomp/test/js_dict_test.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
var Mt = require("./mt.js");
44
var Block = require("../../lib/js/block.js");
55
var Js_dict = require("../../lib/js/js_dict.js");
6-
var Js_primitive = require("../../lib/js/js_primitive.js");
76

87
function obj(param) {
98
return {
@@ -28,10 +27,10 @@ var suites_001 = /* :: */[
2827
(function (param) {
2928
return /* Eq */Block.__(0, [
3029
43,
31-
Js_primitive.undefined_to_opt(({
32-
foo: 43,
33-
bar: 86
34-
})["foo"])
30+
Js_dict.get({
31+
foo: 43,
32+
bar: 86
33+
}, "foo")
3534
]);
3635
})
3736
],
@@ -41,10 +40,10 @@ var suites_001 = /* :: */[
4140
(function (param) {
4241
return /* Eq */Block.__(0, [
4342
undefined,
44-
Js_primitive.undefined_to_opt(({
45-
foo: 43,
46-
bar: 86
47-
})["baz"])
43+
Js_dict.get({
44+
foo: 43,
45+
bar: 86
46+
}, "baz")
4847
]);
4948
})
5049
],
@@ -72,7 +71,7 @@ var suites_001 = /* :: */[
7271
o["foo"] = 36;
7372
return /* Eq */Block.__(0, [
7473
36,
75-
Js_primitive.undefined_to_opt(o["foo"])
74+
Js_dict.get(o, "foo")
7675
]);
7776
})
7877
],

jscomp/test/js_json_test.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
var Mt = require("./mt.js");
44
var $$Array = require("../../lib/js/array.js");
55
var Block = require("../../lib/js/block.js");
6+
var Js_dict = require("../../lib/js/js_dict.js");
67
var Js_json = require("../../lib/js/js_json.js");
78
var Caml_array = require("../../lib/js/caml_array.js");
89
var Js_primitive = require("../../lib/js/js_primitive.js");
@@ -53,9 +54,9 @@ add_test("File \"js_json_test.ml\", line 23, characters 11-18", (function (param
5354
if (typeof ty === "number" || ty.tag !== 2) {
5455
return /* Ok */Block.__(4, [false]);
5556
} else {
56-
var match = ty[0]["x"];
57+
var match = Js_dict.get(ty[0], "x");
5758
if (match !== undefined) {
58-
var ty2 = Js_json.classify(match);
59+
var ty2 = Js_json.classify(Js_primitive.valFromOption(match));
5960
if (typeof ty2 === "number" || ty2.tag !== 3) {
6061
return /* Ok */Block.__(4, [false]);
6162
} else {
@@ -224,7 +225,7 @@ if (typeof ty$4 === "number") {
224225
}));
225226
} else if (ty$4.tag === 2) {
226227
var x = ty$4[0];
227-
var ta = Js_json.classify(option_get(Js_primitive.undefined_to_opt(x["a"])));
228+
var ta = Js_json.classify(option_get(Js_dict.get(x, "a")));
228229
if (typeof ta === "number") {
229230
add_test("File \"js_json_test.ml\", line 132, characters 18-25", (function (param) {
230231
return /* Ok */Block.__(4, [false]);
@@ -238,7 +239,7 @@ if (typeof ty$4 === "number") {
238239
return /* Ok */Block.__(4, [false]);
239240
}));
240241
} else {
241-
var ty$5 = Js_json.classify(option_get(Js_primitive.undefined_to_opt(x["b"])));
242+
var ty$5 = Js_json.classify(option_get(Js_dict.get(x, "b")));
242243
if (typeof ty$5 === "number") {
243244
add_test("File \"js_json_test.ml\", line 130, characters 22-29", (function (param) {
244245
return /* Ok */Block.__(4, [false]);
@@ -460,7 +461,7 @@ if (typeof ty$6 === "number") {
460461
return /* Ok */Block.__(4, [false]);
461462
}));
462463
} else if (ty$7.tag === 2) {
463-
var ty$8 = Js_json.classify(option_get(Js_primitive.undefined_to_opt(ty$7[0]["a"])));
464+
var ty$8 = Js_json.classify(option_get(Js_dict.get(ty$7[0], "a")));
464465
if (typeof ty$8 === "number") {
465466
add_test("File \"js_json_test.ml\", line 278, characters 20-27", (function (param) {
466467
return /* Ok */Block.__(4, [false]);

jscomp/test/local_exception_test.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,12 @@ var d = [
1919
3
2020
];
2121

22-
var A$1 = Caml_exceptions.create("Local_exception_test.A");
23-
24-
var x = [
25-
A$1,
26-
3
27-
];
28-
2922
var u = B;
3023

24+
exports.A = A;
3125
exports.v = v;
3226
exports.B = B;
3327
exports.u = u;
3428
exports.D = D;
3529
exports.d = d;
36-
exports.A = A$1;
37-
exports.x = x;
3830
/* No side effect */

jscomp/test/recursive_module.js

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
'use strict';
22

3+
var Mt = require("./mt.js");
4+
var Curry = require("../../lib/js/curry.js");
5+
var Js_exn = require("../../lib/js/js_exn.js");
36
var Caml_module = require("../../lib/js/caml_module.js");
7+
var Caml_builtin_exceptions = require("../../lib/js/caml_builtin_exceptions.js");
8+
9+
var suites = /* record */[/* contents : [] */0];
10+
11+
var test_id = /* record */[/* contents */0];
12+
13+
function eq(loc, x, y) {
14+
return Mt.eq_suites(test_id, suites, loc, x, y);
15+
}
416

517
var Int32 = Caml_module.init_mod([
618
"recursive_module.ml",
7-
9,
19+
14,
820
6
921
], [[
1022
0,
@@ -24,12 +36,34 @@ Caml_module.update_mod([[
2436

2537
var Int3 = Caml_module.init_mod([
2638
"recursive_module.ml",
27-
14,
39+
19,
2840
6
2941
], [[0]]);
3042

3143
Caml_module.update_mod([[0]], Int3, Int3);
3244

45+
var tmp;
46+
47+
try {
48+
Curry._1(Int3[/* u */0], 3);
49+
tmp = 3;
50+
}
51+
catch (raw_exn){
52+
var exn = Js_exn.internalToOCamlException(raw_exn);
53+
if (exn[0] === Caml_builtin_exceptions.undefined_recursive_module) {
54+
tmp = 4;
55+
} else {
56+
throw exn;
57+
}
58+
}
59+
60+
eq("File \"recursive_module.ml\", line 24, characters 6-13", 4, tmp);
61+
62+
Mt.from_pair_suites("recursive_module.ml", suites[0]);
63+
64+
exports.suites = suites;
65+
exports.test_id = test_id;
66+
exports.eq = eq;
3367
exports.Int32 = Int32;
3468
exports.Int3 = Int3;
3569
/* Int32 Not a pure module */

lib/js/js_dict.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
'use strict';
22

3+
var Js_primitive = require("./js_primitive.js");
4+
5+
function get(dict, k) {
6+
if ((k in dict)) {
7+
return Js_primitive.some(dict[k]);
8+
}
9+
10+
}
311

412
var unsafeDeleteKey = (
513
function(dict,key){
@@ -69,6 +77,7 @@ function map(f, source) {
6977
return target;
7078
}
7179

80+
exports.get = get;
7281
exports.unsafeDeleteKey = unsafeDeleteKey;
7382
exports.entries = entries;
7483
exports.values = values;

0 commit comments

Comments
 (0)