Skip to content

Commit d6be8f4

Browse files
committed
reduce an indirection for unwrap
1 parent 3afe16c commit d6be8f4

9 files changed

+26
-25
lines changed

jscomp/core/js_of_lam_option.ml

+2-7
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ let get_default_undefined_from_optional
9393
else
9494
(E.runtime_call Js_runtime_modules.option "option_get" [arg])
9595

96-
let get_default_undefined (arg : J.expression) : J.expression =
96+
let option_unwrap (arg : J.expression) : J.expression =
9797
let desc = arg.expression_desc in
9898
if is_none_static desc then E.undefined else
9999
match desc with
@@ -102,12 +102,7 @@ let get_default_undefined (arg : J.expression) : J.expression =
102102
Js_of_lam_polyvar.get_field x
103103
(* invariant: option encoding *)
104104
| _ ->
105-
(* FIXME: no need do such inlining*)
106-
if Js_analyzer.is_okay_to_duplicate arg then
107-
E.econd (is_not_none arg)
108-
(Js_of_lam_polyvar.get_field (val_from_option arg)) E.undefined
109-
else
110-
E.runtime_call Js_runtime_modules.option "option_get_unwrap" [arg]
105+
E.runtime_call Js_runtime_modules.option "option_unwrap" [arg]
111106

112107
let destruct_optional
113108
~for_sure_none

jscomp/core/js_of_lam_option.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ val get_default_undefined_from_optional:
4444

4545
(** Given [Some (`a x)] or [None],
4646
return [x] *)
47-
val get_default_undefined :
47+
val option_unwrap :
4848
J.expression ->
4949
J.expression
5050

jscomp/core/lam_compile_external_call.ml

+3-1
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,10 @@ let ocaml_to_js_eff
139139
- if ocaml arg is `None`, let js arg be `undefined` (no unwrapping)
140140
- if ocaml arg is `Some x`, unwrap the arg to get the `x`, then
141141
unwrap the `x` itself
142+
- Here `Some x` is `x` due to the current encoding
143+
Lets inline here since it depends on the runtime encoding
142144
*)
143-
Js_of_lam_option.get_default_undefined raw_arg
145+
Js_of_lam_option.option_unwrap raw_arg
144146
| _ ->
145147
Js_of_lam_variant.eval_as_unwrap raw_arg
146148
in

jscomp/runtime/caml_option.ml

+3-3
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ let option_get (x : 'a option) =
7474

7575

7676
(** [input] is optional polymorphic variant *)
77-
let option_get_unwrap (x : 'a option) =
77+
let option_unwrap (x : 'a option) =
7878
if x = None then Caml_undefined_extern.empty
79-
else Obj.magic (Obj.field (Obj.repr (valFromOption (Obj.repr x))) 1 )
80-
79+
else Obj.magic (Obj.field (Obj.repr ((Obj.repr x))) 1 )
80+
(* INVARIANT: polyvar encoding*)

jscomp/runtime/caml_option.mli

+4-1
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,7 @@ val some : Obj.t -> Obj.t
3535

3636
val option_get : Obj.t option -> Obj.t Caml_undefined_extern.t
3737

38-
val option_get_unwrap : 'a option -> Obj.t Caml_undefined_extern.t
38+
(** When it is None, return none
39+
When it is (Some (`a 3)) return 3
40+
*)
41+
val option_unwrap : 'a option -> Obj.t Caml_undefined_extern.t

jscomp/syntax/ast_external_process.ml

+2-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ let spec_of_ptyp
6767
begin match ptyp_desc with
6868
| Ptyp_variant (row_fields, Closed, _)
6969
when variant_unwrap row_fields ->
70-
Unwrap
70+
Unwrap
71+
(* Unwrap attribute can only be attached to things like `[a of a0 | b of b0]` *)
7172
| _ ->
7273
Bs_syntaxerr.err ptyp.ptyp_loc Invalid_bs_unwrap_type
7374
end

jscomp/test/bs_unwrap_test.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ var some_arg = /* `Bool */[
4444
true
4545
];
4646

47-
console.log(5, some_arg !== undefined ? Caml_option.valFromOption(some_arg)[1] : undefined);
47+
console.log(5, Caml_option.option_unwrap(some_arg));
4848

4949
console.log(6, undefined);
5050

51-
console.log(7, Caml_option.option_get_unwrap((console.log("trace"), undefined)));
51+
console.log(7, Caml_option.option_unwrap((console.log("trace"), undefined)));
5252

5353
function dyn_log3(prim, prim$1, prim$2) {
54-
console.log(prim[1], prim$1 !== undefined ? Caml_option.valFromOption(prim$1)[1] : undefined);
54+
console.log(prim[1], Caml_option.option_unwrap(prim$1));
5555

5656
}
5757

@@ -84,12 +84,12 @@ function f(x) {
8484
}
8585

8686
function ff0(x, p) {
87-
console.log(x !== undefined ? Caml_option.valFromOption(x)[1] : undefined, p);
87+
console.log(Caml_option.option_unwrap(x), p);
8888

8989
}
9090

9191
function ff1(x, p) {
92-
console.log(Caml_option.option_get_unwrap(Curry._1(x, undefined)), p);
92+
console.log(Caml_option.option_unwrap(Curry._1(x, undefined)), p);
9393

9494
}
9595

lib/es6/caml_option.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ function option_get(x) {
7171
}
7272
}
7373

74-
function option_get_unwrap(x) {
74+
function option_unwrap(x) {
7575
if (x === undefined) {
7676
return ;
7777
} else {
78-
return valFromOption(x)[1];
78+
return x[1];
7979
}
8080
}
8181

@@ -86,7 +86,7 @@ export {
8686
valFromOption ,
8787
some ,
8888
option_get ,
89-
option_get_unwrap ,
89+
option_unwrap ,
9090

9191
}
9292
/* No side effect */

lib/js/caml_option.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ function option_get(x) {
7171
}
7272
}
7373

74-
function option_get_unwrap(x) {
74+
function option_unwrap(x) {
7575
if (x === undefined) {
7676
return ;
7777
} else {
78-
return valFromOption(x)[1];
78+
return x[1];
7979
}
8080
}
8181

@@ -85,5 +85,5 @@ exports.null_to_opt = null_to_opt;
8585
exports.valFromOption = valFromOption;
8686
exports.some = some;
8787
exports.option_get = option_get;
88-
exports.option_get_unwrap = option_get_unwrap;
88+
exports.option_unwrap = option_unwrap;
8989
/* No side effect */

0 commit comments

Comments
 (0)