Skip to content

Commit 4d269e3

Browse files
committed
promote
1 parent a1ec950 commit 4d269e3

10 files changed

+56
-35
lines changed

compiler/lib/generate.ml

+27-14
Original file line numberDiff line numberDiff line change
@@ -1196,6 +1196,11 @@ let throw_statement ctx cx k loc =
11961196
, loc )
11971197
]
11981198

1199+
let is_int = function
1200+
| J.ENum n -> J.Num.is_int n
1201+
| J.EBin ((J.Bor | J.Lsr), _, _) -> true
1202+
| _ -> false
1203+
11991204
let rec translate_expr ctx queue loc x e level : _ * J.statement_list =
12001205
match e with
12011206
| Apply { f; args; exact } ->
@@ -1463,23 +1468,31 @@ let rec translate_expr ctx queue loc x e level : _ * J.statement_list =
14631468
| Eq, [ x; y ] ->
14641469
let (px, cx), queue = access_queue' ~ctx queue x in
14651470
let (py, cy), queue = access_queue' ~ctx queue y in
1466-
( bool
1467-
(J.call
1468-
(J.dot (s_var "Object") (Utf8_string.of_string_exn "is"))
1469-
[ cx; cy ]
1470-
loc)
1471-
, or_p px py
1472-
, queue )
1471+
let e =
1472+
if is_int cx || is_int cy
1473+
then bool (J.EBin (J.EqEqEq, cx, cy))
1474+
else
1475+
bool
1476+
(J.call
1477+
(J.dot (s_var "Object") (Utf8_string.of_string_exn "is"))
1478+
[ cx; cy ]
1479+
loc)
1480+
in
1481+
e, or_p px py, queue
14731482
| Neq, [ x; y ] ->
14741483
let (px, cx), queue = access_queue' ~ctx queue x in
14751484
let (py, cy), queue = access_queue' ~ctx queue y in
1476-
( bool_not
1477-
(J.call
1478-
(J.dot (s_var "Object") (Utf8_string.of_string_exn "is"))
1479-
[ cx; cy ]
1480-
loc)
1481-
, or_p px py
1482-
, queue )
1485+
let e =
1486+
if is_int cx || is_int cy
1487+
then bool (J.EBin (J.NotEqEq, cx, cy))
1488+
else
1489+
bool_not
1490+
(J.call
1491+
(J.dot (s_var "Object") (Utf8_string.of_string_exn "is"))
1492+
[ cx; cy ]
1493+
loc)
1494+
in
1495+
e, or_p px py, queue
14831496
| IsInt, [ x ] ->
14841497
let (px, cx), queue = access_queue' ~ctx queue x in
14851498
bool (Mlvalue.is_immediate cx), px, queue

compiler/lib/javascript.ml

+9
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

+2
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

+1-1
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

+3-3
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ let%expect_test "test-compiler/lib-effects/test1.ml" =
101101
try{var _C_ = runtime.caml_int_of_string(s), n = _C_;}
102102
catch(_G_){
103103
var _v_ = caml_wrap_exception(_G_);
104-
if(_v_[1] !== Stdlib[7]){
104+
if(! Object.is(_v_[1], Stdlib[7])){
105105
var raise$1 = caml_pop_trap();
106106
return raise$1(caml_maybe_attach_backtrace(_v_, 0));
107107
}
@@ -114,15 +114,15 @@ let%expect_test "test-compiler/lib-effects/test1.ml" =
114114
}
115115
catch(_F_){
116116
var _x_ = caml_wrap_exception(_F_);
117-
if(_x_ !== Stdlib[8]){
117+
if(! Object.is(_x_, Stdlib[8])){
118118
var raise$0 = caml_pop_trap();
119119
return raise$0(caml_maybe_attach_backtrace(_x_, 0));
120120
}
121121
var m = 0, _y_ = 0;
122122
}
123123
runtime.caml_push_trap
124124
(function(_E_){
125-
if(_E_ === Stdlib[8]) return cont(0);
125+
if(Object.is(_E_, Stdlib[8])) return cont(0);
126126
var raise = caml_pop_trap();
127127
return raise(caml_maybe_attach_backtrace(_E_, 0));
128128
});

compiler/tests-compiler/effects_exceptions.ml

+3-3
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ let%expect_test "test-compiler/lib-effects/test1.ml" =
6060
try{var _p_ = runtime.caml_int_of_string(s), n = _p_;}
6161
catch(_t_){
6262
var _i_ = caml_wrap_exception(_t_);
63-
if(_i_[1] !== Stdlib[7]){
63+
if(! Object.is(_i_[1], Stdlib[7])){
6464
var raise$1 = caml_pop_trap();
6565
return raise$1(caml_maybe_attach_backtrace(_i_, 0));
6666
}
@@ -73,15 +73,15 @@ let%expect_test "test-compiler/lib-effects/test1.ml" =
7373
}
7474
catch(_s_){
7575
var _k_ = caml_wrap_exception(_s_);
76-
if(_k_ !== Stdlib[8]){
76+
if(! Object.is(_k_, Stdlib[8])){
7777
var raise$0 = caml_pop_trap();
7878
return raise$0(caml_maybe_attach_backtrace(_k_, 0));
7979
}
8080
var m = 0, _l_ = 0;
8181
}
8282
caml_push_trap
8383
(function(_r_){
84-
if(_r_ === Stdlib[8]) return cont(0);
84+
if(Object.is(_r_, Stdlib[8])) return cont(0);
8585
var raise = caml_pop_trap();
8686
return raise(caml_maybe_attach_backtrace(_r_, 0));
8787
});

compiler/tests-compiler/gh1354.ml

+3-3
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

+4-7
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ let f t x =
257257
try{var val$0 = caml_call2(Stdlib_Hashtbl[6], t, x);}
258258
catch(_f_){
259259
var _c_ = caml_wrap_exception(_f_);
260-
if(_c_ === Stdlib[8]) return - 1;
260+
if(Object.is(_c_, Stdlib[8])) return - 1;
261261
throw caml_maybe_attach_backtrace(_c_, 0);
262262
}
263263
if(val$0 && ! val$0[2]){
@@ -267,7 +267,7 @@ let f t x =
267267
try{var val = caml_call2(Stdlib_Hashtbl[6], t, x$0); switch$0 = 1;}
268268
catch(_e_){
269269
var _a_ = caml_wrap_exception(_e_);
270-
if(_a_ !== Stdlib[3]) throw caml_maybe_attach_backtrace(_a_, 0);
270+
if(! Object.is(_a_, Stdlib[3])) throw caml_maybe_attach_backtrace(_a_, 0);
271271
var _d_ = 0;
272272
}
273273
if(switch$0){
@@ -499,14 +499,11 @@ let add_substitute =
499499
var lim = caml_ml_string_length(s), k = k$2, stop = new_start;
500500
for(;;){
501501
if(lim <= stop) throw caml_maybe_attach_backtrace(Stdlib[8], 1);
502-
if(caml_string_get(s, stop) === opening){
502+
if(Object.is(caml_string_get(s, stop), opening)){
503503
var i = stop + 1 | 0, k$0 = k + 1 | 0, k = k$0, stop = i;
504504
continue;
505505
}
506-
if(caml_string_get(s, stop) !== closing){
507-
var i$1 = stop + 1 | 0, stop = i$1;
508-
continue;
509-
}
506+
if(! Object.is(caml_string_get(s, stop), closing)){var i$1 = stop + 1 | 0, stop = i$1; continue;}
510507
if(0 !== k){
511508
var i$0 = stop + 1 | 0, k$1 = k - 1 | 0, k = k$1, stop = i$0;
512509
continue;

compiler/tests-compiler/match_with_exn.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ let fun2 () =
7777
try{var i$1 = caml_call1(Stdlib_Random[5], 2);}
7878
catch(_e_){
7979
var _d_ = caml_wrap_exception(_e_);
80-
if(_d_[1] !== A) throw caml_maybe_attach_backtrace(_d_, 0);
80+
if(! Object.is(_d_[1], A)) throw caml_maybe_attach_backtrace(_d_, 0);
8181
var i = _d_[2];
8282
if(2 !== i) return i + 2 | 0;
8383
var i$0 = i;
@@ -92,7 +92,7 @@ let fun2 () =
9292
try{var i$0 = caml_call1(Stdlib_Random[5], 2);}
9393
catch(_c_){
9494
var _a_ = caml_wrap_exception(_c_), switch$1 = 0;
95-
if(_a_[1] === A){
95+
if(Object.is(_a_[1], A)){
9696
var _b_ = _a_[2];
9797
if(2 === _b_){var i = _b_; switch$0 = 1;} else switch$1 = 1;
9898
}

compiler/tests-compiler/tailcall.ml

+2-2
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)