Skip to content

Commit e2d4395

Browse files
committed
prototype of optimization on external obj
1 parent b1da5a5 commit e2d4395

11 files changed

+116
-68
lines changed

jscomp/core/lam_compile_external_obj.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ let assemble_obj_args (labels : External_arg_spec.obj_params) (args : J.express
104104
(Ext_list.flat_map assignment (fun
105105
((xlabel : External_arg_spec.obj_param), (arg : J.expression )) ->
106106
match xlabel with
107-
| {obj_arg_label = Obj_optional {name = label} } ->
107+
| {obj_arg_label = Obj_optional {name = label;for_sure_no_nested_option} } ->
108108
(* Need make sure whether assignment is effectful or not
109109
to avoid code duplication
110110
*)
@@ -114,7 +114,7 @@ let assemble_obj_args (labels : External_arg_spec.obj_params) (args : J.express
114114
Lam_compile_external_call.ocaml_to_js_eff
115115
~arg_label:
116116
Arg_empty ~arg_type:xlabel.obj_arg_type
117-
(Js_of_lam_option.val_from_option arg) in
117+
(if for_sure_no_nested_option then arg else Js_of_lam_option.val_from_option arg) in
118118
begin match acc with
119119
| Splice1 v ->
120120
[S.if_ (Js_of_lam_option.is_not_none arg )

jscomp/frontend/ast_external_process.ml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -492,17 +492,21 @@ let process_obj
492492
param_type::arg_types, result_types
493493
| Nothing ->
494494
let s = (Lam_methname.translate name) in
495-
{obj_arg_label = External_arg_spec.optional s; obj_arg_type},
495+
let not_nested =
496+
match ty.ptyp_desc with
497+
| Ptyp_constr({txt = Lident ("unit" | "string");_}, []) -> true
498+
| _ -> false in
499+
{obj_arg_label = External_arg_spec.optional not_nested s; obj_arg_type},
496500
param_type :: arg_types,
497501
( Parsetree.Otag ({Asttypes.txt = name; loc}, [], Ast_comb.to_undefined_type loc ty) :: result_types)
498502
| Int _ ->
499503
let s = Lam_methname.translate name in
500-
{obj_arg_label = External_arg_spec.optional s ; obj_arg_type },
504+
{obj_arg_label = External_arg_spec.optional true s ; obj_arg_type },
501505
param_type :: arg_types,
502506
(Otag ({Asttypes.txt = name; loc}, [], Ast_comb.to_undefined_type loc @@ Ast_literal.type_int ~loc ()) :: result_types)
503507
| Poly_var_string _ ->
504508
let s = Lam_methname.translate name in
505-
{obj_arg_label = External_arg_spec.optional s ; obj_arg_type },
509+
{obj_arg_label = External_arg_spec.optional true s ; obj_arg_type },
506510
param_type::arg_types,
507511
(Otag ({Asttypes.txt = name; loc}, [], Ast_comb.to_undefined_type loc @@ Ast_literal.type_string ~loc ()) :: result_types)
508512
| Arg_cst _
@@ -1020,7 +1024,7 @@ let pval_prim_of_option_labels
10201024
let label_name = Lam_methname.translate p.txt in
10211025
let obj_arg_label =
10221026
if is_option then
1023-
External_arg_spec.optional label_name
1027+
External_arg_spec.optional false label_name
10241028
else External_arg_spec.obj_label label_name
10251029
in
10261030
{obj_arg_type = Nothing ;

jscomp/frontend/external_arg_spec.ml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ type label_noname =
3535
| Arg_optional
3636

3737
type label =
38-
| Obj_label of {name : string }
38+
| Obj_label of {name : string ; }
3939
| Obj_empty
40-
| Obj_optional of {name : string }
40+
| Obj_optional of {name : string; for_sure_no_nested_option : bool }
41+
4142
(* it will be ignored , side effect will be recorded *)
4243

4344

@@ -90,7 +91,8 @@ let empty_label = Obj_empty
9091
let obj_label name =
9192
Obj_label {name }
9293

93-
let optional name = Obj_optional {name}
94+
let optional for_sure_no_nested_option name =
95+
Obj_optional {name; for_sure_no_nested_option}
9496

9597
let empty_kind obj_arg_type = { obj_arg_label = empty_label ; obj_arg_type }
9698
let dummy =

jscomp/frontend/external_arg_spec.mli

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ type label = private
3232
| Obj_label of {name : string}
3333
| Obj_empty
3434

35-
| Obj_optional of {name : string}
35+
| Obj_optional of {name : string;
36+
for_sure_no_nested_option : bool}
3637
(* it will be ignored , side effect will be recorded *)
3738

3839

@@ -83,6 +84,8 @@ val cst_string : string -> cst
8384
val empty_label : label
8485
(* val empty_lit : cst -> label *)
8586
val obj_label : string -> label
86-
val optional : string -> label
87+
val optional :
88+
bool ->
89+
string -> label
8790
val empty_kind : attr -> obj_param
8891
val dummy : param

jscomp/main/builtin_cmi_datasets.ml

Lines changed: 5 additions & 5 deletions
Large diffs are not rendered by default.

jscomp/test/debug_tmp.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,15 @@
1-
/* This output is empty. Its source's type definitions, externals and/or unused code got optimized away. */
1+
'use strict';
2+
3+
4+
function u(lo, hi) {
5+
var tmp = {
6+
lo: lo
7+
};
8+
if (hi !== undefined) {
9+
tmp.hi = hi;
10+
}
11+
return tmp;
12+
}
13+
14+
exports.u = u;
15+
/* No side effect */

jscomp/test/debug_tmp.ml

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

22
[@@@config {
33
flags = [|
4+
(* "-w";
5+
"@A" *)
46
(* "-drawlambda"; *)
57
(* "-dtypedtree"; *)
68
(* "-bs-diagnose"; *)
@@ -9,3 +11,7 @@
911
|]
1012
}]
1113

14+
external f : lo:int -> ?hi:string -> unit -> _ = "" [@@obj]
15+
16+
17+
let u lo hi = f ~lo ?hi ()

jscomp/test/gpr_1409_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ function make(foo) {
5252
return function (param) {
5353
var tmp = {};
5454
if (partial_arg !== undefined) {
55-
tmp.foo = Caml_option.valFromOption(partial_arg);
55+
tmp.foo = partial_arg;
5656
}
5757
return tmp;
5858
};

jscomp/test/test_obj_simple_ffi.js

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

3-
var Caml_option = require("../../lib/js/caml_option.js");
43

54
function v(displayName, param) {
65
var tmp = {
@@ -9,7 +8,7 @@ function v(displayName, param) {
98
hi: "ghos"
109
};
1110
if (displayName !== undefined) {
12-
tmp.displayName = Caml_option.valFromOption(displayName);
11+
tmp.displayName = displayName;
1312
}
1413
return tmp;
1514
}

lib/4.06.1/unstable/js_compiler.ml

Lines changed: 34 additions & 24 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)