Skip to content

Commit e80b833

Browse files
committed
Remove Primitive_option.toUndefined; use valFromOption for optional ffi args
1 parent 4d93364 commit e80b833

6 files changed

Lines changed: 4 additions & 33 deletions

File tree

compiler/core/js_of_lam_option.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ let get_default_undefined_from_optional (arg : J.expression) : J.expression =
7979
if Js_analyzer.is_okay_to_duplicate arg then
8080
(* FIXME: no need do such inlining*)
8181
E.econd (is_not_none arg) (val_from_option arg) E.undefined
82-
else E.runtime_call Primitive_modules.option "toUndefined" [arg]
82+
else E.runtime_call Primitive_modules.option "valFromOption" [arg]
8383

8484
let option_unwrap (arg : J.expression) : J.expression =
8585
let desc = arg.expression_desc in

packages/@rescript/runtime/Primitive_option.res

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ let fromNull = (type t, x: Js.null<t>): option<t> =>
4848
/* external valFromOption : 'a option -> 'a =
4949
"#val_from_option" */
5050

51-
/** The input is already of [Some] form, [x] is not None,
52-
make sure [x[0]] will not throw */
51+
/** Preserves `None`/`undefined` and unwraps one outer option layer.
52+
Nested `Some` values stay encoded one level deeper. */
5353
let valFromOption = (x: Obj.t): Obj.t =>
5454
if x !== Obj.repr(Js.null) && isNested(x) {
5555
let {depth}: nested = Obj.magic(x)
@@ -62,13 +62,6 @@ let valFromOption = (x: Obj.t): Obj.t =>
6262
Obj.magic(x)
6363
}
6464

65-
let toUndefined = (x: option<'a>) =>
66-
if x == None {
67-
Js.undefined
68-
} else {
69-
Obj.magic(valFromOption(Obj.repr(x)))
70-
}
71-
7265
type poly = {@as("VAL") value: Obj.t}
7366

7467
/** [input] is optional polymorphic variant */

packages/@rescript/runtime/Primitive_option.resi

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ let some: Primitive_object_extern.t => Primitive_object_extern.t
1818

1919
let isNested: Primitive_object_extern.t => bool
2020

21-
let toUndefined: option<Primitive_object_extern.t> => Primitive_js_extern.undefined<
22-
Primitive_object_extern.t,
23-
>
24-
2521
type poly
2622

2723
/** When it is None, return None

packages/@rescript/runtime/lib/es6/Primitive_option.mjs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,6 @@ function valFromOption(x) {
5757
}
5858
}
5959

60-
function toUndefined(x) {
61-
if (x === undefined) {
62-
return;
63-
} else {
64-
return valFromOption(x);
65-
}
66-
}
67-
6860
function unwrapPolyVar(x) {
6961
if (x !== undefined) {
7062
return x.VAL;
@@ -80,7 +72,6 @@ export {
8072
valFromOption,
8173
some,
8274
isNested,
83-
toUndefined,
8475
unwrapPolyVar,
8576
}
8677
/* No side effect */

packages/@rescript/runtime/lib/js/Primitive_option.cjs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,6 @@ function valFromOption(x) {
5757
}
5858
}
5959

60-
function toUndefined(x) {
61-
if (x === undefined) {
62-
return;
63-
} else {
64-
return valFromOption(x);
65-
}
66-
}
67-
6860
function unwrapPolyVar(x) {
6961
if (x !== undefined) {
7062
return x.VAL;
@@ -79,6 +71,5 @@ exports.fromNull = fromNull;
7971
exports.valFromOption = valFromOption;
8072
exports.some = some;
8173
exports.isNested = isNested;
82-
exports.toUndefined = toUndefined;
8374
exports.unwrapPolyVar = unwrapPolyVar;
8475
/* No side effect */

tests/tests/src/optional_ffi_test.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function bug_to_fix(f, x) {
2424
}
2525

2626
function bug_to_fix2(f, x) {
27-
return hey(Primitive_option.toUndefined(f(x)), 3);
27+
return hey(Primitive_option.valFromOption(f(x)), 3);
2828
}
2929

3030
let counter2 = {

0 commit comments

Comments
 (0)