Skip to content

Commit 96d12d0

Browse files
cristianoccknitt
authored andcommitted
Respect conversion semantics from undefined to option.
1 parent b11b327 commit 96d12d0

13 files changed

+77
-43
lines changed

lib/es6/Js.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22

3+
import * as Primitive_option from "./Primitive_option.js";
34

45
let Null;
56

@@ -67,6 +68,8 @@ let $$Map;
6768

6869
let $$WeakMap;
6970

71+
let undefinedToOption = Primitive_option.fromUndefined;
72+
7073
export {
7174
Null,
7275
Undefined,
@@ -101,5 +104,6 @@ export {
101104
$$WeakSet,
102105
$$Map,
103106
$$WeakMap,
107+
undefinedToOption,
104108
}
105109
/* No side effect */

lib/es6/Js_undefined.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,25 @@ function testAny(x) {
1111
}
1212

1313
function getExn(f) {
14-
if (f !== undefined) {
15-
return Primitive_option.valFromOption(f);
14+
let x = Primitive_option.fromUndefined(f);
15+
if (x !== undefined) {
16+
return Primitive_option.valFromOption(x);
1617
}
1718
throw new Error("Js.Undefined.getExn");
1819
}
1920

2021
function bind(x, f) {
21-
if (x !== undefined) {
22-
return f(Primitive_option.valFromOption(x));
22+
let x$1 = Primitive_option.fromUndefined(x);
23+
if (x$1 !== undefined) {
24+
return f(Primitive_option.valFromOption(x$1));
2325
}
2426

2527
}
2628

2729
function iter(x, f) {
28-
if (x !== undefined) {
29-
return f(Primitive_option.valFromOption(x));
30+
let x$1 = Primitive_option.fromUndefined(x);
31+
if (x$1 !== undefined) {
32+
return f(Primitive_option.valFromOption(x$1));
3033
}
3134

3235
}
@@ -40,6 +43,10 @@ function fromOption(x) {
4043

4144
let from_opt = fromOption;
4245

46+
let toOption = Primitive_option.fromUndefined;
47+
48+
let to_opt = Primitive_option.fromUndefined;
49+
4350
export {
4451
test,
4552
testAny,
@@ -48,5 +55,7 @@ export {
4855
iter,
4956
fromOption,
5057
from_opt,
58+
toOption,
59+
to_opt,
5160
}
5261
/* No side effect */

lib/js/Js.js

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

3+
let Primitive_option = require("./Primitive_option.js");
34

45
let Null;
56

@@ -67,6 +68,8 @@ let $$Map;
6768

6869
let $$WeakMap;
6970

71+
let undefinedToOption = Primitive_option.fromUndefined;
72+
7073
exports.Null = Null;
7174
exports.Undefined = Undefined;
7275
exports.Nullable = Nullable;
@@ -100,4 +103,5 @@ exports.$$Set = $$Set;
100103
exports.$$WeakSet = $$WeakSet;
101104
exports.$$Map = $$Map;
102105
exports.$$WeakMap = $$WeakMap;
106+
exports.undefinedToOption = undefinedToOption;
103107
/* No side effect */

lib/js/Js_undefined.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,25 @@ function testAny(x) {
1111
}
1212

1313
function getExn(f) {
14-
if (f !== undefined) {
15-
return Primitive_option.valFromOption(f);
14+
let x = Primitive_option.fromUndefined(f);
15+
if (x !== undefined) {
16+
return Primitive_option.valFromOption(x);
1617
}
1718
throw new Error("Js.Undefined.getExn");
1819
}
1920

2021
function bind(x, f) {
21-
if (x !== undefined) {
22-
return f(Primitive_option.valFromOption(x));
22+
let x$1 = Primitive_option.fromUndefined(x);
23+
if (x$1 !== undefined) {
24+
return f(Primitive_option.valFromOption(x$1));
2325
}
2426

2527
}
2628

2729
function iter(x, f) {
28-
if (x !== undefined) {
29-
return f(Primitive_option.valFromOption(x));
30+
let x$1 = Primitive_option.fromUndefined(x);
31+
if (x$1 !== undefined) {
32+
return f(Primitive_option.valFromOption(x$1));
3033
}
3134

3235
}
@@ -40,11 +43,17 @@ function fromOption(x) {
4043

4144
let from_opt = fromOption;
4245

46+
let toOption = Primitive_option.fromUndefined;
47+
48+
let to_opt = Primitive_option.fromUndefined;
49+
4350
exports.test = test;
4451
exports.testAny = testAny;
4552
exports.getExn = getExn;
4653
exports.bind = bind;
4754
exports.iter = iter;
4855
exports.fromOption = fromOption;
4956
exports.from_opt = from_opt;
57+
exports.toOption = toOption;
58+
exports.to_opt = to_opt;
5059
/* No side effect */

runtime/Js.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ type nullable<+'a> = Js_null_undefined.t<'a> = Value('a) | @as(null) Null | @as(
190190
type null_undefined<+'a> = nullable<'a>
191191

192192
external toOption: nullable<'a> => option<'a> = "%nullable_to_opt"
193-
external undefinedToOption: undefined<'a> => option<'a> = "%identity"
193+
let undefinedToOption: undefined<'a> => option<'a> = Primitive_option.fromUndefined
194194
external nullToOption: null<'a> => option<'a> = "%null_to_opt"
195195
external isNullable: nullable<'a> => bool = "%is_nullable"
196196
external import: 'a => promise<'a> = "%import"

runtime/Js_undefined.res

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@
2626

2727
type t<+'a> = Primitive_js_extern.undefined<'a>
2828

29-
external to_opt: t<'a> => option<'a> = "%identity"
30-
external toOption: t<'a> => option<'a> = "%identity"
29+
let to_opt: t<'a> => option<'a> = Primitive_option.fromUndefined
30+
let toOption: t<'a> => option<'a> = Primitive_option.fromUndefined
31+
3132
external return: 'a => t<'a> = "%identity"
3233

3334
external empty: t<'a> = "%undefined"

runtime/Js_undefined.resi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,6 @@ Maps `Js.undefined<'a>` to `option<'a>`
9090
`a` => `Some(a)`
9191
`empty` => `None`
9292
*/
93-
external toOption: t<'a> => option<'a> = "%identity"
93+
let toOption: t<'a> => option<'a>
9494

95-
@deprecated("use toOption instead") external to_opt: t<'a> => option<'a> = "%identity"
95+
@deprecated("use toOption instead") let to_opt: t<'a> => option<'a>

tests/tests/src/ExternalExtension.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// Generated by ReScript, PLEASE EDIT WITH CARE
22

3+
import * as Js from "rescript/lib/es6/Js.js";
34

4-
let match = typeof __DEV__ === "undefined" ? undefined : __DEV__;
5+
let match = Js.undefinedToOption(typeof __DEV__ === "undefined" ? undefined : __DEV__);
56

67
if (match !== undefined) {
78
console.log("dev mode");

tests/tests/src/functor_ffi.mjs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
// Generated by ReScript, PLEASE EDIT WITH CARE
22

3+
import * as Js_undefined from "rescript/lib/es6/Js_undefined.js";
34

45
function Make(S) {
5-
let opt_get = (f, i) => f[i];
6+
let opt_get = (f, i) => Js_undefined.toOption(f[i]);
67
return {
78
opt_get: opt_get
89
};
910
}
1011

1112
function opt_get(f, i) {
12-
return f[i];
13+
return Js_undefined.toOption(f[i]);
1314
}
1415

1516
let Int_arr = {
@@ -19,7 +20,7 @@ let Int_arr = {
1920
function f(v) {
2021
return [
2122
v[0],
22-
v[1]
23+
Js_undefined.toOption(v[1])
2324
];
2425
}
2526

tests/tests/src/gpr_974_test.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Generated by ReScript, PLEASE EDIT WITH CARE
22

3+
import * as Js_undefined from "rescript/lib/es6/Js_undefined.js";
34
import * as Primitive_object from "rescript/lib/es6/Primitive_object.js";
45
import * as Primitive_option from "rescript/lib/es6/Primitive_option.js";
56

@@ -15,7 +16,7 @@ if (!Primitive_object.equal(Primitive_option.fromNullable(""), "")) {
1516
};
1617
}
1718

18-
if (!Primitive_object.equal("", "")) {
19+
if (!Primitive_object.equal(Js_undefined.toOption(""), "")) {
1920
throw {
2021
RE_EXN_ID: "Assert_failure",
2122
_1: [

tests/tests/src/js_undefined_test.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ let suites_0 = [
88
param => ({
99
TAG: "Eq",
1010
_0: undefined,
11-
_1: undefined
11+
_1: Js_undefined.toOption(undefined)
1212
})
1313
];
1414

@@ -18,7 +18,7 @@ let suites_1 = {
1818
param => ({
1919
TAG: "Eq",
2020
_0: undefined,
21-
_1: undefined
21+
_1: Js_undefined.toOption()
2222
})
2323
],
2424
tl: {
@@ -27,7 +27,7 @@ let suites_1 = {
2727
param => ({
2828
TAG: "Eq",
2929
_0: "something",
30-
_1: "something"
30+
_1: Js_undefined.toOption("something")
3131
})
3232
],
3333
tl: {

tests/tests/src/test_zero_nullable.mjs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Generated by ReScript, PLEASE EDIT WITH CARE
22

33
import * as Mt from "./mt.mjs";
4+
import * as Js_undefined from "rescript/lib/es6/Js_undefined.js";
45
import * as Primitive_option from "rescript/lib/es6/Primitive_option.js";
56

67
let suites = {
@@ -112,23 +113,25 @@ let Test_null = {
112113
};
113114

114115
function f1$1(x) {
115-
if (x !== undefined) {
116-
return x + 1 | 0;
116+
let x$1 = Js_undefined.toOption(x);
117+
if (x$1 !== undefined) {
118+
return x$1 + 1 | 0;
117119
} else {
118120
return 3;
119121
}
120122
}
121123

122124
function f2$1(x) {
123-
if (x !== undefined) {
124-
return x + 1 | 0;
125+
let u = Js_undefined.toOption(x);
126+
if (u !== undefined) {
127+
return u + 1 | 0;
125128
} else {
126129
return 3;
127130
}
128131
}
129132

130133
function f5$1(h, x) {
131-
let u = h(32);
134+
let u = Js_undefined.toOption(h(32));
132135
if (u !== undefined) {
133136
return u + 1 | 0;
134137
} else {
@@ -137,7 +140,7 @@ function f5$1(h, x) {
137140
}
138141

139142
function f4$1(h, x) {
140-
let u = h(32);
143+
let u = Js_undefined.toOption(h(32));
141144
let v = 32 + x | 0;
142145
if (u !== undefined) {
143146
return u + 1 | 0;
@@ -155,22 +158,21 @@ function f7$1(x) {
155158
}
156159

157160
function f8$1(x) {
158-
if (x !== undefined) {
159-
if (Primitive_option.valFromOption(x) !== undefined) {
160-
return 0;
161-
} else {
162-
return 1;
163-
}
164-
} else {
161+
let x$1 = Js_undefined.toOption(x);
162+
if (x$1 === undefined) {
165163
return 2;
166164
}
165+
let match = Js_undefined.toOption(Primitive_option.valFromOption(x$1));
166+
if (match !== undefined) {
167+
return 0;
168+
} else {
169+
return 1;
170+
}
167171
}
168172

169173
let u$1 = f8$1(undefined);
170174

171-
function f9$1(x) {
172-
return x;
173-
}
175+
let f9$1 = Js_undefined.toOption;
174176

175177
function f10$1(x) {
176178
return x === undefined;

tests/tests/src/undef_regression_test.mjs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
// Generated by ReScript, PLEASE EDIT WITH CARE
22

3+
import * as Js_undefined from "rescript/lib/es6/Js_undefined.js";
34
import * as Primitive_option from "rescript/lib/es6/Primitive_option.js";
45

56
function f(obj) {
67
if (typeof obj === "function") {
78
return;
89
}
910
let size = obj.length;
10-
if (size !== undefined) {
11-
console.log(Primitive_option.valFromOption(size));
11+
let s = Js_undefined.toOption(size);
12+
if (s !== undefined) {
13+
console.log(Primitive_option.valFromOption(s));
1214
return;
1315
}
1416

0 commit comments

Comments
 (0)