Skip to content

Commit 45d10cb

Browse files
committed
promote
1 parent c637bf5 commit 45d10cb

File tree

10 files changed

+188
-21
lines changed

10 files changed

+188
-21
lines changed

compiler/lib/generate.ml

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,11 @@ let throw_statement ctx cx k loc =
10161016
, loc )
10171017
]
10181018

1019+
let is_int = function
1020+
| J.ENum n -> J.Num.is_int n
1021+
| J.EBin ((J.Bor | J.Lsr), _, _) -> true
1022+
| _ -> false
1023+
10191024
let rec translate_expr ctx queue loc x e level : _ * J.statement_list =
10201025
match e with
10211026
| Apply { f; args; exact } ->
@@ -1294,23 +1299,31 @@ let rec translate_expr ctx queue loc x e level : _ * J.statement_list =
12941299
| Eq, [ x; y ] ->
12951300
let (px, cx), queue = access_queue' ~ctx queue x in
12961301
let (py, cy), queue = access_queue' ~ctx queue y in
1297-
( bool
1298-
(J.call
1299-
(J.dot (s_var "Object") (Utf8_string.of_string_exn "is"))
1300-
[ cx; cy ]
1301-
loc)
1302-
, or_p px py
1303-
, queue )
1302+
let e =
1303+
if is_int cx || is_int cy
1304+
then bool (J.EBin (J.EqEqEq, cx, cy))
1305+
else
1306+
bool
1307+
(J.call
1308+
(J.dot (s_var "Object") (Utf8_string.of_string_exn "is"))
1309+
[ cx; cy ]
1310+
loc)
1311+
in
1312+
e, or_p px py, queue
13041313
| Neq, [ x; y ] ->
13051314
let (px, cx), queue = access_queue' ~ctx queue x in
13061315
let (py, cy), queue = access_queue' ~ctx queue y in
1307-
( bool_not
1308-
(J.call
1309-
(J.dot (s_var "Object") (Utf8_string.of_string_exn "is"))
1310-
[ cx; cy ]
1311-
loc)
1312-
, or_p px py
1313-
, queue )
1316+
let e =
1317+
if is_int cx || is_int cy
1318+
then bool (J.EBin (J.NotEqEq, cx, cy))
1319+
else
1320+
bool_not
1321+
(J.call
1322+
(J.dot (s_var "Object") (Utf8_string.of_string_exn "is"))
1323+
[ cx; cy ]
1324+
loc)
1325+
in
1326+
e, or_p px py, queue
13141327
| IsInt, [ x ] ->
13151328
let (px, cx), queue = access_queue' ~ctx queue x in
13161329
bool (Mlvalue.is_immediate cx), px, queue

compiler/lib/javascript.ml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ module Num : sig
4242

4343
val is_neg : t -> bool
4444

45+
val is_int : t -> bool
46+
4547
(** Arithmetic *)
4648

4749
val add : t -> t -> t
@@ -133,6 +135,13 @@ end = struct
133135

134136
let is_neg s = Char.equal s.[0] '-'
135137

138+
let is_int = function
139+
| "-0" -> false
140+
| s ->
141+
String.for_all s ~f:(function
142+
| '0' .. '9' | '-' | '+' -> true
143+
| _ -> false)
144+
136145
let neg s =
137146
match String.drop_prefix s ~prefix:"-" with
138147
| None -> "-" ^ s

compiler/lib/javascript.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ module Num : sig
4343

4444
val is_neg : t -> bool
4545

46+
val is_int : t -> bool
47+
4648
(** Arithmetic *)
4749

4850
val add : t -> t -> t

compiler/tests-compiler/effects.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ let fff () =
4747
_b_ =
4848
[0,
4949
function(e, cont){
50-
return e === E
50+
return Object.is(e, E)
5151
? cont([0, function(k, cont){return cont(11);}])
5252
: cont(0);
5353
}],

compiler/tests-compiler/effects_continuations.ml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,17 @@ let%expect_test "test-compiler/lib-effects/test1.ml" =
103103
{|
104104

105105
function exceptions(s, cont){
106+
<<<<<<< HEAD
106107
try{var _z_ = runtime.caml_int_of_string(s), n = _z_;}
107108
catch(_D_){
108109
var _u_ = caml_wrap_exception(_D_);
109110
if(_u_[1] !== Stdlib[7]){
111+
=======
112+
try{var _C_ = runtime.caml_int_of_string(s), n = _C_;}
113+
catch(_G_){
114+
var _v_ = caml_wrap_exception(_G_);
115+
if(! Object.is(_v_[1], Stdlib[7])){
116+
>>>>>>> c650ced771 (promote)
110117
var raise$1 = caml_pop_trap();
111118
return raise$1(caml_maybe_attach_backtrace(_u_, 0));
112119
}
@@ -117,17 +124,28 @@ let%expect_test "test-compiler/lib-effects/test1.ml" =
117124
throw caml_maybe_attach_backtrace(Stdlib[8], 1);
118125
var _y_ = 7, m = _y_;
119126
}
127+
<<<<<<< HEAD
120128
catch(_C_){
121129
var _v_ = caml_wrap_exception(_C_);
122130
if(_v_ !== Stdlib[8]){
131+
=======
132+
catch(_F_){
133+
var _x_ = caml_wrap_exception(_F_);
134+
if(! Object.is(_x_, Stdlib[8])){
135+
>>>>>>> c650ced771 (promote)
123136
var raise$0 = caml_pop_trap();
124137
return raise$0(caml_maybe_attach_backtrace(_v_, 0));
125138
}
126139
var m = 0;
127140
}
128141
runtime.caml_push_trap
142+
<<<<<<< HEAD
129143
(function(_B_){
130144
if(_B_ === Stdlib[8]) return cont(0);
145+
=======
146+
(function(_E_){
147+
if(Object.is(_E_, Stdlib[8])) return cont(0);
148+
>>>>>>> c650ced771 (promote)
131149
var raise = caml_pop_trap();
132150
return raise(caml_maybe_attach_backtrace(_B_, 0));
133151
});

compiler/tests-compiler/effects_exceptions.ml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,17 @@ let%expect_test "test-compiler/lib-effects/test1.ml" =
5757
{|
5858

5959
function exceptions(s, cont){
60+
<<<<<<< HEAD
6061
try{var _n_ = runtime.caml_int_of_string(s), n = _n_;}
6162
catch(_r_){
6263
var _i_ = caml_wrap_exception(_r_);
6364
if(_i_[1] !== Stdlib[7]){
65+
=======
66+
try{var _p_ = runtime.caml_int_of_string(s), n = _p_;}
67+
catch(_t_){
68+
var _i_ = caml_wrap_exception(_t_);
69+
if(! Object.is(_i_[1], Stdlib[7])){
70+
>>>>>>> c650ced771 (promote)
6471
var raise$1 = caml_pop_trap();
6572
return raise$1(caml_maybe_attach_backtrace(_i_, 0));
6673
}
@@ -71,17 +78,28 @@ let%expect_test "test-compiler/lib-effects/test1.ml" =
7178
throw caml_maybe_attach_backtrace(Stdlib[8], 1);
7279
var _m_ = 7, m = _m_;
7380
}
81+
<<<<<<< HEAD
7482
catch(_q_){
7583
var _j_ = caml_wrap_exception(_q_);
7684
if(_j_ !== Stdlib[8]){
85+
=======
86+
catch(_s_){
87+
var _k_ = caml_wrap_exception(_s_);
88+
if(! Object.is(_k_, Stdlib[8])){
89+
>>>>>>> c650ced771 (promote)
7790
var raise$0 = caml_pop_trap();
7891
return raise$0(caml_maybe_attach_backtrace(_j_, 0));
7992
}
8093
var m = 0;
8194
}
8295
caml_push_trap
96+
<<<<<<< HEAD
8397
(function(_p_){
8498
if(_p_ === Stdlib[8]) return cont(0);
99+
=======
100+
(function(_r_){
101+
if(Object.is(_r_, Stdlib[8])) return cont(0);
102+
>>>>>>> c650ced771 (promote)
85103
var raise = caml_pop_trap();
86104
return raise(caml_maybe_attach_backtrace(_p_, 0));
87105
});

compiler/tests-compiler/gh1354.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ with Exit ->
6666
try{0; _b_ = _a_ + 1 | 0; throw caml_maybe_attach_backtrace(Stdlib[3], 1);}
6767
catch(_e_){
6868
var _c_ = caml_wrap_exception(_e_);
69-
if(_c_ !== Stdlib[3]) throw caml_maybe_attach_backtrace(_c_, 0);
69+
if(! Object.is(_c_, Stdlib[3])) throw caml_maybe_attach_backtrace(_c_, 0);
7070
caml_call2(Stdlib_Printf[3], _d_, _b_ | 0);
7171
var Test = [0];
7272
runtime.caml_register_global(3, Test, "Test");
@@ -153,7 +153,7 @@ with Exit ->
153153
}
154154
catch(_j_){
155155
var _d_ = caml_wrap_exception(_j_);
156-
if(_d_ !== Stdlib[3]) throw caml_maybe_attach_backtrace(_d_, 0);
156+
if(! Object.is(_d_, Stdlib[3])) throw caml_maybe_attach_backtrace(_d_, 0);
157157
caml_call3(Stdlib_Printf[3], _e_, _c_ | 0, _b_);
158158
var Test = [0];
159159
runtime.caml_register_global(4, Test, "Test");
@@ -229,7 +229,7 @@ with Exit ->
229229
}
230230
catch(_h_){
231231
var _c_ = caml_wrap_exception(_h_);
232-
if(_c_ !== Stdlib[3]) throw caml_maybe_attach_backtrace(_c_, 0);
232+
if(! Object.is(_c_, Stdlib[3])) throw caml_maybe_attach_backtrace(_c_, 0);
233233
caml_call2(Stdlib_Printf[3], _d_, _b_);
234234
var Test = [0];
235235
runtime.caml_register_global(4, Test, "Test");

compiler/tests-compiler/loops.ml

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,12 +253,13 @@ let f t x =
253253
try{var val$0 = caml_call2(Stdlib_Hashtbl[6], t, x);}
254254
catch(_f_){
255255
var _c_ = caml_wrap_exception(_f_);
256-
if(_c_ === Stdlib[8]) return - 1;
256+
if(Object.is(_c_, Stdlib[8])) return - 1;
257257
throw caml_maybe_attach_backtrace(_c_, 0);
258258
}
259259
if(val$0 && ! val$0[2]){
260260
var x$1 = val$0[1], x$0 = x$1;
261261
for(;;){
262+
<<<<<<< HEAD
262263
a:
263264
{
264265
try{var val = caml_call2(Stdlib_Hashtbl[6], t, x$0);}
@@ -268,6 +269,17 @@ let f t x =
268269
var _d_ = 0;
269270
break a;
270271
}
272+
=======
273+
var switch$0 = 0;
274+
try{var val = caml_call2(Stdlib_Hashtbl[6], t, x$0); switch$0 = 1;}
275+
catch(_e_){
276+
var _a_ = caml_wrap_exception(_e_);
277+
if(! Object.is(_a_, Stdlib[3])) throw caml_maybe_attach_backtrace(_a_, 0);
278+
var _d_ = 0;
279+
}
280+
if(switch$0){
281+
var switch$1 = 0;
282+
>>>>>>> c650ced771 (promote)
271283
if(val && ! val[2]){
272284
var y = val[1], _b_ = y === (x$0 + 1 | 0) ? 1 : 0;
273285
if(_b_){var _d_ = _b_; break a;}
@@ -525,6 +537,79 @@ let add_substitute =
525537
caml_call2(add_char, b, previous$0);
526538
var i$8 = i$4 + 1 | 0, previous = previous$0, i$4 = i$8;
527539
}
540+
<<<<<<< HEAD
541+
=======
542+
if(92 === previous){
543+
caml_call2(add_char, b, previous$0);
544+
var i$5 = i$4 + 1 | 0, previous = 32, i$4 = i$5;
545+
continue;
546+
}
547+
var start$0 = i$4 + 1 | 0;
548+
if(lim$1 <= start$0) throw caml_maybe_attach_backtrace(Stdlib[8], 1);
549+
var opening = caml_string_get(s, start$0), switch$0 = 0;
550+
if(40 !== opening && 123 !== opening){
551+
var start = start$0 + 1 | 0, lim$0 = caml_ml_string_length(s), i$2 = start;
552+
for(;;){
553+
if(lim$0 <= i$2)
554+
var stop$0 = lim$0;
555+
else{
556+
var match = caml_string_get(s, i$2), switch$1 = 0;
557+
if(91 <= match){
558+
if(97 <= match){
559+
if(123 > match) switch$1 = 1;
560+
}
561+
else if(95 === match) switch$1 = 1;
562+
}
563+
else if(58 <= match){
564+
if(65 <= match) switch$1 = 1;
565+
}
566+
else if(48 <= match) switch$1 = 1;
567+
if(switch$1){var i$3 = i$2 + 1 | 0, i$2 = i$3; continue;}
568+
var stop$0 = i$2;
569+
}
570+
var
571+
match$0 =
572+
[0,
573+
caml_call3(Stdlib_String[15], s, start$0, stop$0 - start$0 | 0),
574+
stop$0];
575+
switch$0 = 1;
576+
break;
577+
}
578+
}
579+
if(! switch$0){
580+
var new_start = start$0 + 1 | 0, k$2 = 0;
581+
if(40 === opening)
582+
var closing = 41;
583+
else{
584+
if(123 !== opening)
585+
throw caml_maybe_attach_backtrace([0, Assert_failure, _a_], 1);
586+
var closing = 125;
587+
}
588+
var lim = caml_ml_string_length(s), k = k$2, stop = new_start;
589+
for(;;){
590+
if(lim <= stop) throw caml_maybe_attach_backtrace(Stdlib[8], 1);
591+
if(Object.is(caml_string_get(s, stop), opening)){
592+
var i = stop + 1 | 0, k$0 = k + 1 | 0, k = k$0, stop = i;
593+
continue;
594+
}
595+
if(! Object.is(caml_string_get(s, stop), closing)){var i$1 = stop + 1 | 0, stop = i$1; continue;}
596+
if(0 !== k){
597+
var i$0 = stop + 1 | 0, k$1 = k - 1 | 0, k = k$1, stop = i$0;
598+
continue;
599+
}
600+
var
601+
match$0 =
602+
[0,
603+
caml_call3
604+
(Stdlib_String[15], s, new_start, (stop - start$0 | 0) - 1 | 0),
605+
stop + 1 | 0];
606+
break;
607+
}
608+
}
609+
var next_i = match$0[2], ident = match$0[1];
610+
caml_call2(add_string, b, caml_call1(f, ident));
611+
var previous = 32, i$4 = next_i;
612+
>>>>>>> c650ced771 (promote)
528613
}
529614
}
530615
//end |}]

compiler/tests-compiler/match_with_exn.ml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ let fun2 () =
7373
[%expect
7474
{|
7575
function fun1(param){
76+
<<<<<<< HEAD
7677
a:
7778
{
7879
try{var i$1 = caml_call1(Stdlib_Random[5], 2);}
@@ -86,18 +87,39 @@ let fun2 () =
8687
}
8788
if(0 !== i$1) return i$1 + 1 | 0;
8889
var i$0 = i$1;
90+
=======
91+
var switch$0 = 0;
92+
try{var i$1 = caml_call1(Stdlib_Random[5], 2);}
93+
catch(_e_){
94+
var _d_ = caml_wrap_exception(_e_);
95+
if(! Object.is(_d_[1], A)) throw caml_maybe_attach_backtrace(_d_, 0);
96+
var i = _d_[2];
97+
if(2 !== i) return i + 2 | 0;
98+
var i$0 = i;
99+
switch$0 = 1;
100+
>>>>>>> c650ced771 (promote)
89101
}
90102
return i$0;
91103
}
92104
//end
93105
function fun2(param){
106+
<<<<<<< HEAD
94107
a:
95108
{
96109
try{var i$0 = caml_call1(Stdlib_Random[5], 2);}
97110
catch(_c_){
98111
var _a_ = caml_wrap_exception(_c_);
99112
if(_a_[1] === A){var _b_ = _a_[2]; if(2 === _b_){var i = _b_; break a;}}
100113
throw caml_maybe_attach_backtrace(_a_, 0);
114+
=======
115+
var switch$0 = 0;
116+
try{var i$0 = caml_call1(Stdlib_Random[5], 2);}
117+
catch(_c_){
118+
var _a_ = caml_wrap_exception(_c_), switch$1 = 0;
119+
if(Object.is(_a_[1], A)){
120+
var _b_ = _a_[2];
121+
if(2 === _b_){var i = _b_; switch$0 = 1;} else switch$1 = 1;
122+
>>>>>>> c650ced771 (promote)
101123
}
102124
if(0 !== i$0) return i$0 + 1 | 0;
103125
var i = i$0;

compiler/tests-compiler/tailcall.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ let%expect_test _ =
6262
function odd(x){return caml_trampoline(odd$0(0, x));}
6363
function even(x){return caml_trampoline(even$0(0, x));}
6464
var _c_ = even(1);
65-
if(odd(1) === _c_)
65+
if(Object.is(odd(1), _c_))
6666
throw caml_maybe_attach_backtrace([0, Assert_failure, _b_], 1);
6767
try{odd(5000); var _d_ = log_success(0); return _d_;}
6868
catch(_e_){return caml_call1(log_failure, cst_too_much_recursion);}
@@ -103,7 +103,7 @@ let%expect_test _ =
103103
function odd(x){return caml_trampoline(odd$0(x));}
104104
function even(x){return caml_trampoline(even$0(x));}
105105
var _c_ = even(1);
106-
if(odd(1) === _c_)
106+
if(Object.is(odd(1), _c_))
107107
throw caml_maybe_attach_backtrace([0, Assert_failure, _b_], 1);
108108
try{odd(5000); var _d_ = log_success(0); return _d_;}
109109
catch(_e_){return caml_call1(log_failure, cst_too_much_recursion);}

0 commit comments

Comments
 (0)