Skip to content

Commit

Permalink
promote
Browse files Browse the repository at this point in the history
  • Loading branch information
hhugo committed Feb 20, 2023
1 parent 13dd856 commit f231d78
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 35 deletions.
41 changes: 27 additions & 14 deletions compiler/lib/generate.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1196,6 +1196,11 @@ let throw_statement ctx cx k loc =
, loc )
]

let is_int = function
| J.ENum n -> J.Num.is_int n
| J.EBin ((J.Bor | J.Lsr), _, _) -> true
| _ -> false

let rec translate_expr ctx queue loc x e level : _ * J.statement_list =
match e with
| Apply { f; args; exact } ->
Expand Down Expand Up @@ -1455,23 +1460,31 @@ let rec translate_expr ctx queue loc x e level : _ * J.statement_list =
| Eq, [ x; y ] ->
let (px, cx), queue = access_queue' ~ctx queue x in
let (py, cy), queue = access_queue' ~ctx queue y in
( bool
(J.call
(J.dot (s_var "Object") (Utf8_string.of_string_exn "is"))
[ cx; cy ]
loc)
, or_p px py
, queue )
let e =
if is_int cx || is_int cy
then bool (J.EBin (J.EqEqEq, cx, cy))
else
bool
(J.call
(J.dot (s_var "Object") (Utf8_string.of_string_exn "is"))
[ cx; cy ]
loc)
in
e, or_p px py, queue
| Neq, [ x; y ] ->
let (px, cx), queue = access_queue' ~ctx queue x in
let (py, cy), queue = access_queue' ~ctx queue y in
( bool_not
(J.call
(J.dot (s_var "Object") (Utf8_string.of_string_exn "is"))
[ cx; cy ]
loc)
, or_p px py
, queue )
let e =
if is_int cx || is_int cy
then bool (J.EBin (J.NotEqEq, cx, cy))
else
bool_not
(J.call
(J.dot (s_var "Object") (Utf8_string.of_string_exn "is"))
[ cx; cy ]
loc)
in
e, or_p px py, queue
| IsInt, [ x ] ->
let (px, cx), queue = access_queue' ~ctx queue x in
bool (Mlvalue.is_immediate cx), px, queue
Expand Down
9 changes: 9 additions & 0 deletions compiler/lib/javascript.ml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ module Num : sig

val is_neg : t -> bool

val is_int : t -> bool

(** Arithmetic *)

val add : t -> t -> t
Expand Down Expand Up @@ -91,6 +93,13 @@ end = struct

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

let is_int = function
| "-0" -> false
| s ->
String.for_all s ~f:(function
| '0' .. '9' | '-' | '+' -> true
| _ -> false)

let neg s =
match String.drop_prefix s ~prefix:"-" with
| None -> "-" ^ s
Expand Down
2 changes: 2 additions & 0 deletions compiler/lib/javascript.mli
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ module Num : sig

val is_neg : t -> bool

val is_int : t -> bool

(** Arithmetic *)

val add : t -> t -> t
Expand Down
2 changes: 1 addition & 1 deletion compiler/tests-compiler/effects.ml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ let fff () =
_b_ =
[0,
function(e, cont){
return e === E
return Object.is(e, E)
? cont([0, function(k, cont){return cont(11);}])
: cont(0);
}],
Expand Down
6 changes: 3 additions & 3 deletions compiler/tests-compiler/effects_continuations.ml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ let%expect_test "test-compiler/lib-effects/test1.ml" =
try{var _C_ = runtime.caml_int_of_string(s), n = _C_;}
catch(_G_){
var _v_ = caml_wrap_exception(_G_);
if(_v_[1] !== Stdlib[7]){
if(! Object.is(_v_[1], Stdlib[7])){
var raise$1 = caml_pop_trap();
return raise$1(caml_maybe_attach_backtrace(_v_, 0));
}
Expand All @@ -114,15 +114,15 @@ let%expect_test "test-compiler/lib-effects/test1.ml" =
}
catch(_F_){
var _x_ = caml_wrap_exception(_F_);
if(_x_ !== Stdlib[8]){
if(! Object.is(_x_, Stdlib[8])){
var raise$0 = caml_pop_trap();
return raise$0(caml_maybe_attach_backtrace(_x_, 0));
}
var m = 0, _y_ = 0;
}
runtime.caml_push_trap
(function(_E_){
if(_E_ === Stdlib[8]) return cont(0);
if(Object.is(_E_, Stdlib[8])) return cont(0);
var raise = caml_pop_trap();
return raise(caml_maybe_attach_backtrace(_E_, 0));
});
Expand Down
6 changes: 3 additions & 3 deletions compiler/tests-compiler/effects_exceptions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ let%expect_test "test-compiler/lib-effects/test1.ml" =
try{var _p_ = runtime.caml_int_of_string(s), n = _p_;}
catch(_t_){
var _i_ = caml_wrap_exception(_t_);
if(_i_[1] !== Stdlib[7]){
if(! Object.is(_i_[1], Stdlib[7])){
var raise$1 = caml_pop_trap();
return raise$1(caml_maybe_attach_backtrace(_i_, 0));
}
Expand All @@ -73,15 +73,15 @@ let%expect_test "test-compiler/lib-effects/test1.ml" =
}
catch(_s_){
var _k_ = caml_wrap_exception(_s_);
if(_k_ !== Stdlib[8]){
if(! Object.is(_k_, Stdlib[8])){
var raise$0 = caml_pop_trap();
return raise$0(caml_maybe_attach_backtrace(_k_, 0));
}
var m = 0, _l_ = 0;
}
caml_push_trap
(function(_r_){
if(_r_ === Stdlib[8]) return cont(0);
if(Object.is(_r_, Stdlib[8])) return cont(0);
var raise = caml_pop_trap();
return raise(caml_maybe_attach_backtrace(_r_, 0));
});
Expand Down
6 changes: 3 additions & 3 deletions compiler/tests-compiler/gh1354.ml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ with Exit ->
try{0; _b_ = _a_ + 1 | 0; throw caml_maybe_attach_backtrace(Stdlib[3], 1);}
catch(_e_){
var _c_ = caml_wrap_exception(_e_);
if(_c_ !== Stdlib[3]) throw caml_maybe_attach_backtrace(_c_, 0);
if(! Object.is(_c_, Stdlib[3])) throw caml_maybe_attach_backtrace(_c_, 0);
caml_call2(Stdlib_Printf[3], _d_, _b_ | 0);
var Test = [0];
runtime.caml_register_global(3, Test, "Test");
Expand Down Expand Up @@ -153,7 +153,7 @@ with Exit ->
}
catch(_j_){
var _d_ = caml_wrap_exception(_j_);
if(_d_ !== Stdlib[3]) throw caml_maybe_attach_backtrace(_d_, 0);
if(! Object.is(_d_, Stdlib[3])) throw caml_maybe_attach_backtrace(_d_, 0);
caml_call3(Stdlib_Printf[3], _e_, _c_ | 0, _b_);
var Test = [0];
runtime.caml_register_global(4, Test, "Test");
Expand Down Expand Up @@ -229,7 +229,7 @@ with Exit ->
}
catch(_h_){
var _c_ = caml_wrap_exception(_h_);
if(_c_ !== Stdlib[3]) throw caml_maybe_attach_backtrace(_c_, 0);
if(! Object.is(_c_, Stdlib[3])) throw caml_maybe_attach_backtrace(_c_, 0);
caml_call2(Stdlib_Printf[3], _d_, _b_);
var Test = [0];
runtime.caml_register_global(4, Test, "Test");
Expand Down
11 changes: 4 additions & 7 deletions compiler/tests-compiler/loops.ml
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ let f t x =
try{var val$0 = caml_call2(Stdlib_Hashtbl[6], t, x);}
catch(_f_){
var _c_ = caml_wrap_exception(_f_);
if(_c_ === Stdlib[8]) return - 1;
if(Object.is(_c_, Stdlib[8])) return - 1;
throw caml_maybe_attach_backtrace(_c_, 0);
}
if(val$0 && ! val$0[2]){
Expand All @@ -267,7 +267,7 @@ let f t x =
try{var val = caml_call2(Stdlib_Hashtbl[6], t, x$0); switch$0 = 1;}
catch(_e_){
var _a_ = caml_wrap_exception(_e_);
if(_a_ !== Stdlib[3]) throw caml_maybe_attach_backtrace(_a_, 0);
if(! Object.is(_a_, Stdlib[3])) throw caml_maybe_attach_backtrace(_a_, 0);
var _d_ = 0;
}
if(switch$0){
Expand Down Expand Up @@ -499,14 +499,11 @@ let add_substitute =
var lim = caml_ml_string_length(s), k = k$2, stop = new_start;
for(;;){
if(lim <= stop) throw caml_maybe_attach_backtrace(Stdlib[8], 1);
if(caml_string_get(s, stop) === opening){
if(Object.is(caml_string_get(s, stop), opening)){
var i = stop + 1 | 0, k$0 = k + 1 | 0, k = k$0, stop = i;
continue;
}
if(caml_string_get(s, stop) !== closing){
var i$1 = stop + 1 | 0, stop = i$1;
continue;
}
if(! Object.is(caml_string_get(s, stop), closing)){var i$1 = stop + 1 | 0, stop = i$1; continue;}
if(0 !== k){
var i$0 = stop + 1 | 0, k$1 = k - 1 | 0, k = k$1, stop = i$0;
continue;
Expand Down
4 changes: 2 additions & 2 deletions compiler/tests-compiler/match_with_exn.ml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ let fun2 () =
try{var i$1 = caml_call1(Stdlib_Random[5], 2);}
catch(_e_){
var _d_ = caml_wrap_exception(_e_);
if(_d_[1] !== A) throw caml_maybe_attach_backtrace(_d_, 0);
if(! Object.is(_d_[1], A)) throw caml_maybe_attach_backtrace(_d_, 0);
var i = _d_[2];
if(2 !== i) return i + 2 | 0;
var i$0 = i;
Expand All @@ -92,7 +92,7 @@ let fun2 () =
try{var i$0 = caml_call1(Stdlib_Random[5], 2);}
catch(_c_){
var _a_ = caml_wrap_exception(_c_), switch$1 = 0;
if(_a_[1] === A){
if(Object.is(_a_[1], A)){
var _b_ = _a_[2];
if(2 === _b_){var i = _b_; switch$0 = 1;} else switch$1 = 1;
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/tests-compiler/tailcall.ml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ let%expect_test _ =
function odd(x){return caml_trampoline(odd$0(0, x));}
function even(x){return caml_trampoline(even$0(0, x));}
var _c_ = even(1);
if(odd(1) === _c_)
if(Object.is(odd(1), _c_))
throw caml_maybe_attach_backtrace([0, Assert_failure, _b_], 1);
try{odd(5000); var _d_ = log_success(0); return _d_;}
catch(_e_){return caml_call1(log_failure, cst_too_much_recursion);}
Expand Down Expand Up @@ -103,7 +103,7 @@ let%expect_test _ =
function odd(x){return caml_trampoline(odd$0(x));}
function even(x){return caml_trampoline(even$0(x));}
var _c_ = even(1);
if(odd(1) === _c_)
if(Object.is(odd(1), _c_))
throw caml_maybe_attach_backtrace([0, Assert_failure, _b_], 1);
try{odd(5000); var _d_ = log_success(0); return _d_;}
catch(_e_){return caml_call1(log_failure, cst_too_much_recursion);}
Expand Down

0 comments on commit f231d78

Please sign in to comment.