Skip to content

Commit c8f71e5

Browse files
committed
tentative fix for #2633
1 parent 4d306ff commit c8f71e5

File tree

8 files changed

+148
-114
lines changed

8 files changed

+148
-114
lines changed

jscomp/all.depend

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ core/js_pass_tailcall_inline.cmx : core/js_stmt_make.cmx core/js_op_util.cmx \
537537
core/js_analyzer.cmx core/j.cmx ext/ident_set.cmx ext/ident_hashtbl.cmx \
538538
ext/ext_list.cmx core/js_pass_tailcall_inline.cmi
539539
core/js_of_lam_variant.cmx : core/js_stmt_make.cmx core/js_exp_make.cmx \
540-
core/j.cmx ext/ext_list.cmx ext/ext_ident.cmx core/js_of_lam_variant.cmi
540+
core/j.cmx ext/ext_list.cmx core/js_of_lam_variant.cmi
541541
core/js_pass_flatten_and_mark_dead.cmx : core/js_stmt_make.cmx \
542542
core/js_op_util.cmx core/js_map.cmx core/js_fold.cmx core/js_exp_make.cmx \
543543
common/js_config.cmx core/js_analyzer.cmx core/j.cmx ext/ident_set.cmx \

jscomp/core/js_of_lam_variant.ml

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
2-
*
2+
*
33
* This program is free software: you can redistribute it and/or modify
44
* it under the terms of the GNU Lesser General Public License as published by
55
* the Free Software Foundation, either version 3 of the License, or
@@ -17,76 +17,76 @@
1717
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1818
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1919
* GNU Lesser General Public License for more details.
20-
*
20+
*
2121
* You should have received a copy of the GNU Lesser General Public License
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

2525
module E = Js_exp_make
2626
module S = Js_stmt_make
2727

28-
type arg_expression =
28+
type arg_expression =
2929
| Splice0
30-
| Splice1 of E.t
30+
| Splice1 of E.t
3131
| Splice2 of E.t * E.t
32-
32+
3333
(* we need destruct [undefined] when input is optional *)
34-
let eval (arg : J.expression) (dispatches : (int * string) list ) : E.t =
34+
let eval (arg : J.expression) (dispatches : (int * string) list ) : E.t =
3535
if arg == E.undefined then E.undefined else
3636
match arg.expression_desc with
37-
| Number (Int {i} | Uint i) ->
38-
E.str (Ext_list.assoc_by_int None (Int32.to_int i) dispatches)
39-
| _ ->
37+
| Number (Int {i} | Uint i) ->
38+
E.str (Ext_list.assoc_by_int None (Int32.to_int i) dispatches)
39+
| _ ->
4040
E.of_block
4141
[(S.int_switch arg
42-
(Ext_list.map (fun (i,r) ->
43-
{J.case = i ;
42+
(Ext_list.map (fun (i,r) ->
43+
{J.case = i ;
4444
body = [S.return_stmt (E.str r)],
4545
false (* FIXME: if true, still print break*)
4646
}) dispatches))]
4747

4848
(** invariant: optional is not allowed in this case *)
49-
let eval_as_event (arg : J.expression) (dispatches : (int * string) list ) =
49+
let eval_as_event (arg : J.expression) (dispatches : (int * string) list ) =
5050
match arg.expression_desc with
5151
| Array ([{expression_desc = Number (Int {i} | Uint i)}; cb], _)
5252
| Caml_block([{expression_desc = Number (Int {i} | Uint i)}; cb], _, _, _)
53-
->
54-
let v = Ext_list.assoc_by_int None (Int32.to_int i) dispatches in
55-
Splice2(E.str v , cb )
56-
| _ ->
57-
let event = Ext_ident.create "action" in
53+
->
54+
let v = Ext_list.assoc_by_int None (Int32.to_int i) dispatches in
55+
Splice2(E.str v , cb )
56+
| _ ->
5857
Splice2
59-
(E.ocaml_fun [event]
60-
[(S.int_switch arg
61-
(Ext_list.map (fun (i,r) ->
62-
{J.case = i ;
63-
body = [S.return_stmt (E.index (E.var event) 0l)],
58+
(E.of_block
59+
[(S.int_switch (E.index arg 0l)
60+
(Ext_list.map (fun (i,r) ->
61+
{J.case = i ;
62+
body = [S.return_stmt (E.str r)],
6463
false (* FIXME: if true, still print break*)
6564
}) dispatches))]
66-
, (* TODO: improve, one dispatch later,
67-
the problem is that we can not create bindings
68-
due to the
65+
, (* TODO: improve, one dispatch later,
66+
the problem is that we can not create bindings
67+
due to the
6968
*)
70-
E.ocaml_fun [event]
71-
[(S.int_switch arg
72-
(Ext_list.map (fun (i,r) ->
73-
{J.case = i ;
74-
body = [S.return_stmt (E.index (E.var event) 1l)],
75-
false (* FIXME: if true, still print break*)
76-
}) dispatches))]
69+
E.index arg 1l
7770
)
71+
(** FIXME:
72+
1. duplicated evaluation of expressions arg
73+
Solution: calcuate the arg once in the beginning
74+
2. avoid block for branches < 3
75+
or always?
76+
a === 444? "a" : a==222? "b"
77+
*)
7878

7979
(* we need destruct [undefined] when input is optional *)
80-
let eval_as_int (arg : J.expression) (dispatches : (int * int) list ) : E.t =
81-
if arg == E.undefined then E.undefined else
80+
let eval_as_int (arg : J.expression) (dispatches : (int * int) list ) : E.t =
81+
if arg == E.undefined then E.undefined else
8282
match arg.expression_desc with
8383
| Number (Int {i} | Uint i) ->
84-
E.int (Int32.of_int (Ext_list.assoc_by_int None (Int32.to_int i) dispatches))
85-
| _ ->
84+
E.int (Int32.of_int (Ext_list.assoc_by_int None (Int32.to_int i) dispatches))
85+
| _ ->
8686
E.of_block
8787
[(S.int_switch arg
88-
(Ext_list.map (fun (i,r) ->
89-
{J.case = i ;
88+
(Ext_list.map (fun (i,r) ->
89+
{J.case = i ;
9090
body = [S.return_stmt (E.int (Int32.of_int r))],
9191
false (* FIXME: if true, still print break*)
9292
}) dispatches))]

jscomp/test/.depend

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ gpr_2474.cmj :
306306
gpr_2487.cmj : ../others/belt.cmj
307307
gpr_2503_test.cmj : mt.cmj ../runtime/js.cmj
308308
gpr_2608_test.cmj : mt.cmj ../stdlib/list.cmj
309+
gpr_2633_test.cmj :
309310
gpr_405_test.cmj : ../stdlib/hashtbl.cmj gpr_405_test.cmi
310311
gpr_441.cmj :
311312
gpr_459_test.cmj : mt.cmj

jscomp/test/Makefile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,12 +244,13 @@ OTHERS := test_literals a test_ari test_export2 test_internalOO test_obj_simple_
244244
gpr_2608_test\
245245
pipe_syntax\
246246
gpr_2352_test\
247+
gpr_2633_test\
247248
# bs_uncurry_test
248249
# needs Lam to get rid of Uncurry arity first
249250
# simple_derive_test
250251
# simple_derive_use
251-
# derive_type_test
252-
# derive_dyntype
252+
# derive_type_test
253+
# derive_dyntype
253254

254255

255256
SOURCE_LIST := $(OTHERS)
@@ -259,7 +260,7 @@ $(addsuffix .cmi, $(OTHERS)): ../runtime/js.cmi
259260
$(addsuffix .cmj, $(OTHERS)): ../runtime/js.cmj
260261

261262
# It is okay to test deprecated API
262-
COMPFLAGS+= $(MODULE_FLAGS) -w -40 -warn-error A+8-3-30-26+101-102-103-104
263+
COMPFLAGS+= $(MODULE_FLAGS) -w -40 -warn-error A+8-3-30-26+101-102-103-104
263264
COMPFLAGS+= -bs-no-version-header -absname -bs-diagnose -bs-cross-module-opt -bs-package-name bs-platform -bs-package-output commonjs:jscomp/test
264265

265266

jscomp/test/gpr_2633_test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
3+
var Curry = require("../../lib/js/curry.js");
4+
5+
function on1(foo, $$event) {
6+
foo.on((function () {
7+
switch ($$event[0]) {
8+
case 4895187 :
9+
return "bar";
10+
case 5097222 :
11+
return "foo";
12+
13+
}
14+
})(), $$event[1]);
15+
return /* () */0;
16+
}
17+
18+
function on2(foo, h, $$event) {
19+
foo.on((function () {
20+
switch (Curry._1(h, $$event)[0]) {
21+
case 4895187 :
22+
return "bar";
23+
case 5097222 :
24+
return "foo";
25+
26+
}
27+
})(), Curry._1(h, $$event)[1]);
28+
return /* () */0;
29+
}
30+
31+
exports.on1 = on1;
32+
exports.on2 = on2;
33+
/* No side effect */

jscomp/test/gpr_2633_test.ml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
type foo
2+
3+
external on :
4+
foo ->
5+
([`bar of unit -> unit
6+
| `foo of string -> unit ] [@bs.string]) ->
7+
unit = "" [@@bs.send]
8+
9+
let on1 foo event =
10+
on foo event
11+
12+
(* FIXME *)
13+
let on2 foo h event =
14+
on foo (h event)
15+

jscomp/test/poly_variant_test.js

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -104,46 +104,30 @@ function read$1(name) {
104104
}
105105

106106
function test(readline, x) {
107-
readline.on((function (action) {
108-
switch (x) {
109-
case -944564236 :
110-
return action[0];
111-
case -933029960 :
112-
return action[0];
113-
114-
}
115-
}), (function (action) {
116-
switch (x) {
117-
case -944564236 :
118-
return action[1];
119-
case -933029960 :
120-
return action[1];
121-
122-
}
123-
}));
107+
readline.on((function () {
108+
switch (x[0]) {
109+
case -944564236 :
110+
return "line";
111+
case -933029960 :
112+
return "close";
113+
114+
}
115+
})(), x[1]);
124116
return /* () */0;
125117
}
126118

127119
Mt.from_pair_suites("poly_variant_test.ml", suites[0]);
128120

129121
function on2(prim, prim$1) {
130-
prim.on2((function (action) {
131-
switch (prim$1) {
132-
case -944564236 :
133-
return action[0];
134-
case -933029960 :
135-
return action[0];
136-
137-
}
138-
}), (function (action) {
139-
switch (prim$1) {
140-
case -944564236 :
141-
return action[1];
142-
case -933029960 :
143-
return action[1];
144-
145-
}
146-
}));
122+
prim.on2((function () {
123+
switch (prim$1[0]) {
124+
case -944564236 :
125+
return "line";
126+
case -933029960 :
127+
return "close";
128+
129+
}
130+
})(), prim$1[1]);
147131
return /* () */0;
148132
}
149133

0 commit comments

Comments
 (0)