diff --git a/compiler/tests-compiler/direct_calls.ml b/compiler/tests-compiler/direct_calls.ml index 30fdcc82fd..ca096609d1 100644 --- a/compiler/tests-compiler/direct_calls.ml +++ b/compiler/tests-compiler/direct_calls.ml @@ -123,53 +123,91 @@ let%expect_test "direct calls with --enable effects" = M1.f 1; M2.f 2 |} in - print_fun_decl code (Some "test1"); - print_fun_decl code (Some "test2"); - print_fun_decl code (Some "test3"); - print_fun_decl code (Some "test4"); + print_fun_decl code (Some "test1$0"); + print_fun_decl code (Some "test1$1"); + print_var_decl code "test1"; + print_fun_decl code (Some "test2$0"); + print_fun_decl code (Some "test2$1"); + print_var_decl code "test2"; + print_fun_decl code (Some "test3$0"); + print_fun_decl code (Some "test3$1"); + print_var_decl code "test3"; + print_fun_decl code (Some "test4$0"); + print_fun_decl code (Some "test4$1"); + print_var_decl code "test4"; [%expect {| - function test1(param, cont){ - function f(g, x){return g(x);} - var _k_ = 7; - f(function(x){return x + 1 | 0;}, _k_); - var _l_ = 4.; - f(function(x){return x * 2.;}, _l_); + function test1$0(param){ + function f(g, x){return caml_call1(g, x);} + var _H_ = 7; + f(function(x){return x + 1 | 0;}, _H_); + var _I_ = 4.; + f(function(x){return x * 2.;}, _I_); + return 0; + } + //end + function test1$1(param, cont){ + function f(g, x){return caml_call1(g, x);} + var _F_ = 7; + f(function(x){return x + 1 | 0;}, _F_); + var _G_ = 4.; + f(function(x){return x * 2.;}, _G_); return cont(0); } //end - function test2(param, cont){ - function f(g, x, cont){return caml_cps_exact_call2(g, x, cont);} - var _f_ = 7; - function _g_(x, cont){return cont(x + 1 | 0);} + var test1 = caml_cps_closure(test1$0, test1$1); + //end + function test2$0(param){ + var f = f$0(); + f(_h_(), 7); + f(_j_(), cst_a); + return 0; + } + //end + function test2$1(param, cont){ + var f = f$0(), _y_ = 7, _z_ = _h_(); return caml_cps_exact_call3 (f, - _g_, - _f_, - function(_h_){ - function _i_(x, cont){ - return caml_cps_call3(Stdlib[28], x, cst_a$0, cont); - } + _z_, + _y_, + function(_A_){ + var _B_ = _j_(); return caml_cps_exact_call3 - (f, _i_, cst_a, function(_j_){return cont(0);}); + (f, _B_, cst_a, function(_C_){return cont(0);}); }); } //end - function test3(x, cont){ + var test2 = caml_cps_closure(test2$0, test2$1); + //end + function test3$0(x){ function F(symbol){function f(x){return x + 1 | 0;} return [0, f];} - var M1 = F([0]), M2 = F([0]), _e_ = M2[1].call(null, 2); - return cont([0, M1[1].call(null, 1), _e_]); + var M1 = F([0]), M2 = F([0]), _x_ = caml_call1(M2[1], 2); + return [0, caml_call1(M1[1], 1), _x_]; } //end - function test4(x, cont){ - function F(symbol){ - function f(x, cont){return caml_cps_call3(Stdlib_Printf[2], _a_, x, cont);} - return [0, f]; - } - var M1 = F([0]), M2 = F([0]), _b_ = 1, _c_ = M1[1]; + function test3$1(x, cont){ + function F(symbol){function f(x){return x + 1 | 0;} return [0, f];} + var M1 = F([0]), M2 = F([0]), _w_ = M2[1].call(null, 2); + return cont([0, M1[1].call(null, 1), _w_]); + } + //end + var test3 = caml_cps_closure(test3$0, test3$1); + //end + function test4$0(x){ + function F(symbol){var f$0 = f(); return [0, f$0];} + var M1 = F([0]), M2 = F([0]); + caml_call1(M1[1], 1); + return caml_call1(M2[1], 2); + } + //end + function test4$1(x, cont){ + function F(symbol){var f$0 = f(); return [0, f$0];} + var M1 = F([0]), M2 = F([0]), _t_ = 1, _u_ = M1[1]; return caml_cps_exact_call2 - (_c_, - _b_, - function(_d_){return caml_cps_exact_call2(M2[1], 2, cont);}); + (_u_, + _t_, + function(_v_){return caml_cps_exact_call2(M2[1], 2, cont);}); } + //end + var test4 = caml_cps_closure(test4$0, test4$1); //end |}] diff --git a/compiler/tests-compiler/effects.ml b/compiler/tests-compiler/effects.ml index 48ae87ea85..16cde3fb70 100644 --- a/compiler/tests-compiler/effects.ml +++ b/compiler/tests-compiler/effects.ml @@ -39,32 +39,35 @@ let fff () = | e -> None) } |} in - print_fun_decl program (Some "fff"); + (*ignore @@ Js_of_ocaml_compiler.(Js_output.program (Pretty_print.to_out_channel stdout) program);*) + print_fun_decl program (Some "fff$0"); + print_fun_decl program (Some "fff$1"); + print_var_decl program "fff"; [%expect {| - function fff(param, cont){ + function fff$0(param){ var - _b_ = - [0, - function(e, cont){ - return e === E - ? cont([0, function(k, cont){return cont(11);}]) - : cont(0); - }], - _c_ = 10; - function _d_(x, cont){return cont(x);} - var _e_ = Stdlib_Effect[3][5]; + _p_ = [0, _d_()], + _q_ = _f_(), + _r_ = caml_call3(Stdlib_Effect[3][5], _q_, 10, _p_); + return caml_call1(caml_call1(Stdlib_Printf[2], _h_), _r_); + } + //end + function fff$1(param, cont){ + var _i_ = [0, _d_()], _k_ = _f_(), _j_ = 10, _l_ = Stdlib_Effect[3][5]; return caml_cps_call4 - (_e_, - _d_, - _c_, - _b_, - function(_f_){ - var _g_ = Stdlib_Printf[2]; + (_l_, + _k_, + _j_, + _i_, + function(_m_){ + var _n_ = Stdlib_Printf[2]; return caml_cps_call2 - (_g_, - _a_, - function(_h_){return caml_cps_call2(_h_, _f_, cont);}); + (_n_, + _h_, + function(_o_){return caml_cps_call2(_o_, _m_, cont);}); }); } + //end + var fff = caml_cps_closure(fff$0, fff$1); //end |}] diff --git a/compiler/tests-compiler/effects_continuations.ml b/compiler/tests-compiler/effects_continuations.ml index b42a2d1831..c652ad00d3 100644 --- a/compiler/tests-compiler/effects_continuations.ml +++ b/compiler/tests-compiler/effects_continuations.ml @@ -87,131 +87,225 @@ let%expect_test "test-compiler/lib-effects/test1.ml" = f l |} in - print_fun_decl code (Some "exceptions"); - print_fun_decl code (Some "cond1"); - print_fun_decl code (Some "cond2"); - print_fun_decl code (Some "cond3"); - print_fun_decl code (Some "loop1"); - print_fun_decl code (Some "loop2"); - print_fun_decl code (Some "loop3"); + print_fun_decl code (Some "exceptions$0"); + print_fun_decl code (Some "exceptions$1"); + print_var_decl code "exceptions"; + print_fun_decl code (Some "cond1$0"); + print_fun_decl code (Some "cond1$1"); + print_var_decl code "cond1"; + print_fun_decl code (Some "cond2$0"); + print_fun_decl code (Some "cond2$1"); + print_var_decl code "cond2"; + print_fun_decl code (Some "cond3$0"); + print_fun_decl code (Some "cond3$1"); + print_var_decl code "cond3"; + print_fun_decl code (Some "loop1$0"); + print_fun_decl code (Some "loop1$1"); + print_var_decl code "loop1"; + print_fun_decl code (Some "loop2$0"); + print_fun_decl code (Some "loop2$1"); + print_var_decl code "loop2"; + print_fun_decl code (Some "loop3$0"); + print_fun_decl code (Some "loop3$0"); + print_var_decl code "loop3"; [%expect {| - function exceptions(s, cont){ - try{var _C_ = runtime.caml_int_of_string(s), n = _C_;} - catch(_G_){ - var _v_ = caml_wrap_exception(_G_); - if(_v_[1] !== Stdlib[7]){ + function exceptions$0(s){ + try{var _T_ = caml_int_of_string(s), n = _T_;} + catch(_W_){ + var _M_ = caml_wrap_exception(_W_); + if(_M_[1] !== Stdlib[7]) throw caml_maybe_attach_backtrace(_M_, 0); + var n = 0, _N_ = 0; + } + try{ + if(caml_string_equal(s, cst$0)) + throw caml_maybe_attach_backtrace(Stdlib[8], 1); + var _S_ = 7, m = _S_; + } + catch(_V_){ + var _O_ = caml_wrap_exception(_V_); + if(_O_ !== Stdlib[8]) throw caml_maybe_attach_backtrace(_O_, 0); + var m = 0, _P_ = 0; + } + try{ + if(caml_string_equal(s, cst)) + throw caml_maybe_attach_backtrace(Stdlib[8], 1); + var _R_ = [0, [0, caml_call1(Stdlib[79], cst_toto), n, m]]; + return _R_; + } + catch(_U_){ + var _Q_ = caml_wrap_exception(_U_); + if(_Q_ === Stdlib[8]) return 0; + throw caml_maybe_attach_backtrace(_Q_, 0); + } + } + //end + function exceptions$1(s, cont){ + try{var _H_ = caml_int_of_string(s), n = _H_;} + catch(_L_){ + var _C_ = caml_wrap_exception(_L_); + if(_C_[1] !== Stdlib[7]){ var raise$1 = caml_pop_trap(); - return raise$1(caml_maybe_attach_backtrace(_v_, 0)); + return raise$1(caml_maybe_attach_backtrace(_C_, 0)); } - var n = 0, _w_ = 0; + var n = 0; } try{ if(caml_string_equal(s, cst$0)) throw caml_maybe_attach_backtrace(Stdlib[8], 1); - var _B_ = 7, m = _B_; + var _G_ = 7, m = _G_; } - catch(_F_){ - var _x_ = caml_wrap_exception(_F_); - if(_x_ !== Stdlib[8]){ + catch(_K_){ + var _D_ = caml_wrap_exception(_K_); + if(_D_ !== Stdlib[8]){ var raise$0 = caml_pop_trap(); - return raise$0(caml_maybe_attach_backtrace(_x_, 0)); + return raise$0(caml_maybe_attach_backtrace(_D_, 0)); } - var m = 0, _y_ = 0; + var m = 0; } runtime.caml_push_trap - (function(_E_){ - if(_E_ === Stdlib[8]) return cont(0); + (function(_J_){ + if(_J_ === Stdlib[8]) return cont(0); var raise = caml_pop_trap(); - return raise(caml_maybe_attach_backtrace(_E_, 0)); + return raise(caml_maybe_attach_backtrace(_J_, 0)); }); if(caml_string_equal(s, cst)){ - var _z_ = Stdlib[8], raise = caml_pop_trap(); - return raise(caml_maybe_attach_backtrace(_z_, 1)); + var _E_ = Stdlib[8], raise = caml_pop_trap(); + return raise(caml_maybe_attach_backtrace(_E_, 1)); } - var _A_ = Stdlib[79]; + var _F_ = Stdlib[79]; return caml_cps_call2 - (_A_, + (_F_, cst_toto, - function(_D_){caml_pop_trap(); return cont([0, [0, _D_, n, m]]);}); + function(_I_){caml_pop_trap(); return cont([0, [0, _I_, n, m]]);}); + } + //end + var exceptions = caml_cps_closure(exceptions$0, exceptions$1); + //end + function cond1$0(b){ + var + ic = + b ? caml_call1(Stdlib[79], cst_toto$0) : caml_call1(Stdlib[79], cst_titi); + return [0, ic, 7]; } //end - function cond1(b, cont){ - function _u_(ic){return cont([0, ic, 7]);} + function cond1$1(b, cont){ + function _B_(ic){return cont([0, ic, 7]);} return b - ? caml_cps_call2(Stdlib[79], cst_toto$0, _u_) - : caml_cps_call2(Stdlib[79], cst_titi, _u_); + ? caml_cps_call2(Stdlib[79], cst_toto$0, _B_) + : caml_cps_call2(Stdlib[79], cst_titi, _B_); } //end - function cond2(b, cont){ - function _s_(_t_){return cont(7);} + var cond1 = caml_cps_closure(cond1$0, cond1$1); + //end + function cond2$0(b){ + if(b) + caml_call1(Stdlib_Printf[3], _h_); + else + caml_call1(Stdlib_Printf[3], _i_); + return 7; + } + //end + function cond2$1(b, cont){ + function _z_(_A_){return cont(7);} return b - ? caml_cps_call2(Stdlib_Printf[3], _a_, _s_) - : caml_cps_call2(Stdlib_Printf[3], _b_, _s_); + ? caml_cps_call2(Stdlib_Printf[3], _h_, _z_) + : caml_cps_call2(Stdlib_Printf[3], _i_, _z_); } //end - function cond3(b, cont){ + var cond2 = caml_cps_closure(cond2$0, cond2$1); + //end + function cond3$0(b){ var x = [0, 0]; - function _q_(_r_){return cont(x[1]);} - return b ? (x[1] = 1, _q_(0)) : caml_cps_call2(Stdlib_Printf[3], _c_, _q_); + if(b) x[1] = 1; else caml_call1(Stdlib_Printf[3], _j_); + return x[1]; } //end - function loop1(b, cont){ - var all = [0, 0], _m_ = Stdlib[79]; + function cond3$1(b, cont){ + var x = [0, 0]; + function _x_(_y_){return cont(x[1]);} + return b ? (x[1] = 1, _x_(0)) : caml_cps_call2(Stdlib_Printf[3], _j_, _x_); + } + //end + var cond3 = caml_cps_closure(cond3$0, cond3$1); + //end + function loop1$0(b){ + var all = [0, 0], ic = caml_call1(Stdlib[79], cst_static_examples_ml); + for(;;){ + var line = caml_call1(Stdlib[83], ic); + all[1] = [0, line, all[1]]; + if(! b) continue; + caml_call1(Stdlib[53], line); + } + } + //end + function loop1$1(b, cont){ + var all = [0, 0], _t_ = Stdlib[79]; return caml_cps_call2 - (_m_, + (_t_, cst_static_examples_ml, function(ic){ - function _n_(_p_){ - var _o_ = Stdlib[83]; + function _u_(_w_){ + var _v_ = Stdlib[83]; return caml_cps_call2 - (_o_, + (_v_, ic, function(line){ all[1] = [0, line, all[1]]; return b - ? caml_cps_call2(Stdlib[53], line, _n_) - : caml_cps_exact_call1(_n_, 0); + ? caml_cps_call2(Stdlib[53], line, _u_) + : caml_cps_exact_mono_call1(_u_, 0); }); } - return _n_(0); + return _u_(0); }); } //end - function loop2(param, cont){ - var all = [0, 0], _h_ = Stdlib[79]; + var loop1 = caml_cps_closure(loop1$0, loop1$1); + //end + function loop2$0(param){ + var all = [0, 0], ic = caml_call1(Stdlib[79], cst_static_examples_ml$0); + caml_call1(Stdlib_Printf[3], _k_); + for(;;){ + var line = caml_call1(Stdlib[83], ic); + all[1] = [0, line, all[1]]; + caml_call1(Stdlib[53], line); + } + } + //end + function loop2$1(param, cont){ + var all = [0, 0], _o_ = Stdlib[79]; return caml_cps_call2 - (_h_, + (_o_, cst_static_examples_ml$0, function(ic){ - var _i_ = Stdlib_Printf[3]; - function _j_(_l_){ - var _k_ = Stdlib[83]; + var _p_ = Stdlib_Printf[3]; + function _q_(_s_){ + var _r_ = Stdlib[83]; return caml_cps_call2 - (_k_, + (_r_, ic, function(line){ all[1] = [0, line, all[1]]; - return caml_cps_call2(Stdlib[53], line, _j_); + return caml_cps_call2(Stdlib[53], line, _q_); }); } - return caml_cps_call2(_i_, _d_, _j_); + return caml_cps_call2(_p_, _k_, _q_); }); } //end - function loop3(param, cont){ - var _f_ = Stdlib_List[9]; - return caml_cps_call2 - (_f_, - _e_, - function(l){ - function _g_(x){ - if(! x) return cont(l); - var r = x[2]; - return caml_cps_exact_call1(_g_, r); - } - return _g_(l); - }); + var loop2 = caml_cps_closure(loop2$0, loop2$1); + //end + function loop3$0(param){ + var l = caml_call1(Stdlib_List[9], _l_), x = l; + for(;;){if(! x) return l; var r = x[2], x = r;} + } + //end + function loop3$0(param){ + var l = caml_call1(Stdlib_List[9], _l_), x = l; + for(;;){if(! x) return l; var r = x[2], x = r;} } + //end + var loop3 = caml_cps_closure(loop3$0, loop3$1); //end |}] diff --git a/compiler/tests-compiler/effects_exceptions.ml b/compiler/tests-compiler/effects_exceptions.ml index fd3f892912..b37e9a269a 100644 --- a/compiler/tests-compiler/effects_exceptions.ml +++ b/compiler/tests-compiler/effects_exceptions.ml @@ -52,64 +52,113 @@ let%expect_test "test-compiler/lib-effects/test1.ml" = s ^ "aaa" |} in - print_fun_decl code (Some "exceptions"); + print_fun_decl code (Some "exceptions$0"); + print_fun_decl code (Some "exceptions$1"); + print_var_decl code "exceptions"; [%expect {| - function exceptions(s, cont){ - try{var _p_ = runtime.caml_int_of_string(s), n = _p_;} - catch(_t_){ - var _i_ = caml_wrap_exception(_t_); - if(_i_[1] !== Stdlib[7]){ + function exceptions$0(s){ + try{var _G_ = caml_int_of_string(s), n = _G_;} + catch(_J_){ + var _z_ = caml_wrap_exception(_J_); + if(_z_[1] !== Stdlib[7]) throw caml_maybe_attach_backtrace(_z_, 0); + var n = 0, _A_ = 0; + } + try{ + if(caml_string_equal(s, cst$0)) + throw caml_maybe_attach_backtrace(Stdlib[8], 1); + var _F_ = 7, m = _F_; + } + catch(_I_){ + var _B_ = caml_wrap_exception(_I_); + if(_B_ !== Stdlib[8]) throw caml_maybe_attach_backtrace(_B_, 0); + var m = 0, _C_ = 0; + } + try{ + if(caml_string_equal(s, cst)) + throw caml_maybe_attach_backtrace(Stdlib[8], 1); + var _E_ = [0, [0, caml_call1(Stdlib[79], cst_toto), n, m]]; + return _E_; + } + catch(_H_){ + var _D_ = caml_wrap_exception(_H_); + if(_D_ === Stdlib[8]) return 0; + throw caml_maybe_attach_backtrace(_D_, 0); + } + } + //end + function exceptions$1(s, cont){ + try{var _u_ = caml_int_of_string(s), n = _u_;} + catch(_y_){ + var _p_ = caml_wrap_exception(_y_); + if(_p_[1] !== Stdlib[7]){ var raise$1 = caml_pop_trap(); - return raise$1(caml_maybe_attach_backtrace(_i_, 0)); + return raise$1(caml_maybe_attach_backtrace(_p_, 0)); } - var n = 0, _j_ = 0; + var n = 0; } try{ if(caml_string_equal(s, cst$0)) throw caml_maybe_attach_backtrace(Stdlib[8], 1); - var _o_ = 7, m = _o_; + var _t_ = 7, m = _t_; } - catch(_s_){ - var _k_ = caml_wrap_exception(_s_); - if(_k_ !== Stdlib[8]){ + catch(_x_){ + var _q_ = caml_wrap_exception(_x_); + if(_q_ !== Stdlib[8]){ var raise$0 = caml_pop_trap(); - return raise$0(caml_maybe_attach_backtrace(_k_, 0)); + return raise$0(caml_maybe_attach_backtrace(_q_, 0)); } - var m = 0, _l_ = 0; + var m = 0; } caml_push_trap - (function(_r_){ - if(_r_ === Stdlib[8]) return cont(0); + (function(_w_){ + if(_w_ === Stdlib[8]) return cont(0); var raise = caml_pop_trap(); - return raise(caml_maybe_attach_backtrace(_r_, 0)); + return raise(caml_maybe_attach_backtrace(_w_, 0)); }); if(caml_string_equal(s, cst)){ - var _m_ = Stdlib[8], raise = caml_pop_trap(); - return raise(caml_maybe_attach_backtrace(_m_, 1)); + var _r_ = Stdlib[8], raise = caml_pop_trap(); + return raise(caml_maybe_attach_backtrace(_r_, 1)); } - var _n_ = Stdlib[79]; + var _s_ = Stdlib[79]; return caml_cps_call2 - (_n_, + (_s_, cst_toto, - function(_q_){caml_pop_trap(); return cont([0, [0, _q_, n, m]]);}); + function(_v_){caml_pop_trap(); return cont([0, [0, _v_, n, m]]);}); } + //end + var exceptions = caml_cps_closure(exceptions$0, exceptions$1); //end |}]; - print_fun_decl code (Some "handler_is_loop"); + print_fun_decl code (Some "handler_is_loop$0"); + print_fun_decl code (Some "handler_is_loop$1"); + print_var_decl code "handler_is_loop"; [%expect {| - function handler_is_loop(f, g, l, cont){ + function handler_is_loop$0(f, g, l){ + try{var _n_ = caml_call1(f, 0); return _n_;} + catch(_o_){ + var l$0 = l; + for(;;){ + var match = caml_call1(g, l$0); + if(72330306 <= match[1]){var l$1 = match[2], l$0 = l$1; continue;} + var exn = match[2]; + throw caml_maybe_attach_backtrace(exn, 1); + } + } + } + //end + function handler_is_loop$1(f, g, l, cont){ caml_push_trap - (function(_g_){ - function _h_(l){ + (function(_l_){ + function _m_(l){ return caml_cps_call2 (g, l, function(match){ if(72330306 <= match[1]){ var l = match[2]; - return caml_cps_exact_call1(_h_, l); + return caml_cps_exact_mono_call1(_m_, l); } var exn = match[2], @@ -118,21 +167,32 @@ let%expect_test "test-compiler/lib-effects/test1.ml" = return raise(exn$0); }); } - return _h_(l); + return _m_(l); }); - var _e_ = 0; + var _j_ = 0; return caml_cps_call2 - (f, _e_, function(_f_){caml_pop_trap(); return cont(_f_);}); + (f, _j_, function(_k_){caml_pop_trap(); return cont(_k_);}); } + //end + var handler_is_loop = caml_cps_closure(handler_is_loop$0, handler_is_loop$1); //end |}]; - print_fun_decl code (Some "handler_is_merge_node"); + print_fun_decl code (Some "handler_is_merge_node$0"); + print_fun_decl code (Some "handler_is_merge_node$1"); + print_var_decl code "handler_is_merge_node"; [%expect {| - function handler_is_merge_node(g, cont){ - function _b_(s){return caml_cps_call3(Stdlib[28], s, cst_aaa, cont);} - caml_push_trap(function(_d_){return _b_(cst$1);}); - var _a_ = 0; + function handler_is_merge_node$0(g){ + try{var _h_ = caml_call1(g, 0), s = _h_;}catch(_i_){var s = cst$1;} + return caml_call2(Stdlib[28], s, cst_aaa); + } + //end + function handler_is_merge_node$1(g, cont){ + function _e_(s){return caml_cps_call3(Stdlib[28], s, cst_aaa, cont);} + caml_push_trap(function(_g_){return _e_(cst$1);}); + var _d_ = 0; return caml_cps_call2 - (g, _a_, function(_c_){caml_pop_trap(); return _b_(_c_);}); + (g, _d_, function(_f_){caml_pop_trap(); return _e_(_f_);}); } + //end + var handler_is_merge_node = caml_cps_closure(handler_is_merge_node$0, handler_is_merge_node$1); //end |}] diff --git a/compiler/tests-compiler/effects_toplevel.ml b/compiler/tests-compiler/effects_toplevel.ml index 38c9e8d1f5..629278768c 100644 --- a/compiler/tests-compiler/effects_toplevel.ml +++ b/compiler/tests-compiler/effects_toplevel.ml @@ -39,60 +39,47 @@ let%expect_test "test-compiler/lib-effects/test1.ml" = "use strict"; var runtime = globalThis.jsoo_runtime, - caml_callback = runtime.caml_callback, caml_string_of_jsbytes = runtime.caml_string_of_jsbytes; - function caml_cps_exact_call1(f, a0){ - return runtime.caml_stack_check_depth() + function caml_call1(f, a0){ + return (f.l >= 0 ? f.l : f.l = f.length) == 1 ? f(a0) - : runtime.caml_trampoline_return(f, [a0]); + : runtime.caml_call_gen(f, [a0]); } function caml_cps_call2(f, a0, a1){ return runtime.caml_stack_check_depth() - ? (f.l + ? (f.cps.l >= 0 - ? f.l - : f.l = f.length) + ? f.cps.l + : f.cps.l = f.cps.length) == 2 - ? f(a0, a1) - : runtime.caml_call_gen(f, [a0, a1]) + ? f.cps.call(null, a0, a1) + : runtime.caml_call_gen_cps(f, [a0, a1]) : runtime.caml_trampoline_return(f, [a0, a1]); } - function caml_cps_exact_call2(f, a0, a1){ - return runtime.caml_stack_check_depth() - ? f(a0, a1) - : runtime.caml_trampoline_return(f, [a0, a1]); + runtime.caml_initialize_fiber_stack(); + var + global_data = runtime.caml_get_global_data(), + _b_ = + [0, + [11, caml_string_of_jsbytes("abc"), 0], + caml_string_of_jsbytes("abc")], + Stdlib_Printf = global_data.Stdlib__Printf; + function g$0(param){return caml_call1(Stdlib_Printf[2], _b_);} + function g$1(param, cont){ + return caml_cps_call2(Stdlib_Printf[2], _b_, cont); + } + var g = runtime.caml_cps_closure(g$0, g$1); + g(0); + var i = 1; + for(;;){ + g(0); + var _c_ = i + 1 | 0; + if(5 !== i){var i = _c_; continue;} + g(0); + var Test = [0]; + runtime.caml_register_global(2, Test, "Test"); + return; } - return caml_callback - (function(cont){ - var - global_data = runtime.caml_get_global_data(), - Stdlib_Printf = global_data.Stdlib__Printf, - _a_ = - [0, - [11, caml_string_of_jsbytes("abc"), 0], - caml_string_of_jsbytes("abc")]; - function g(param, cont){ - return caml_cps_call2(Stdlib_Printf[2], _a_, cont); - } - caml_callback(g, [0]); - var _b_ = 1; - function _c_(i){ - var _d_ = 0; - return caml_cps_exact_call2 - (g, - _d_, - function(_e_){ - var _f_ = i + 1 | 0; - if(5 !== i) return caml_cps_exact_call1(_c_, _f_); - caml_callback(g, [0]); - var Test = [0]; - runtime.caml_register_global(2, Test, "Test"); - return; - }); - } - return _c_(_b_); - }, - []); } (globalThis)); //end |}] diff --git a/compiler/tests-compiler/error.ml b/compiler/tests-compiler/error.ml index ba32032f09..5416a8245b 100644 --- a/compiler/tests-compiler/error.ml +++ b/compiler/tests-compiler/error.ml @@ -25,14 +25,17 @@ let normalize x = |> Str.global_replace (Str.regexp "node\\(.exe\\)?") "%{NODE}" let%expect_test "uncaugh error" = - let prog = {| let _ = raise Not_found |} in + let prog = {| +(* Ignore OCAMLRUNPARAM=b to be reproducible *) +Printexc.record_backtrace false;; +let _ = raise Not_found |} + in compile_and_run prog; print_endline (normalize [%expect.output]); [%expect {| Fatal error: exception Not_found - process exited with error code 2 %{NODE} test.js |}]; compile_and_run_bytecode prog; @@ -46,6 +49,9 @@ let%expect_test "uncaugh error" = (* Test caml_format_exception by un-registeting "Printexc.handle_uncaught_exception". Note that this hack unly work with jsoo *) let prog = {| +(* Ignore OCAMLRUNPARAM=b to be reproducible *) +Printexc.record_backtrace false;; + let null = Array.unsafe_get [|1|] 1 let () = Callback.register "Printexc.handle_uncaught_exception" null exception C @@ -62,6 +68,9 @@ let _ = raise C |} %{NODE} test.js |}]; let prog = {| +(* Ignore OCAMLRUNPARAM=b to be reproducible *) +Printexc.record_backtrace false;; + let null = Array.unsafe_get [|1|] 1 let () = Callback.register "Printexc.handle_uncaught_exception" null exception D of int * string * Int64.t @@ -79,6 +88,9 @@ let _ = raise (D(2,"test",43L)) %{NODE} test.js |}]; let prog = {| +(* Ignore OCAMLRUNPARAM=b to be reproducible *) +Printexc.record_backtrace false;; + let null = Array.unsafe_get [|1|] 1 let () = Callback.register "Printexc.handle_uncaught_exception" null let _ = assert false |} @@ -87,13 +99,16 @@ let _ = assert false |} print_endline (normalize [%expect.output]); [%expect {| - Fatal error: exception Assert_failure("test.ml", 4, 8) + Fatal error: exception Assert_failure("test.ml", 7, 8) process exited with error code 2 %{NODE} test.js |}]; let prog = {| +(* Ignore OCAMLRUNPARAM=b to be reproducible *) +Printexc.record_backtrace false;; + let null = Array.unsafe_get [|1|] 1 let () = Callback.register "Printexc.handle_uncaught_exception" null [@@@ocaml.warning "-8"] let _ = match 3 with 2 -> () |} @@ -102,7 +117,7 @@ let () = Callback.register "Printexc.handle_uncaught_exception" null print_endline (normalize [%expect.output]); [%expect {| - Fatal error: exception Match_failure("test.ml", 4, 33) + Fatal error: exception Match_failure("test.ml", 7, 33) process exited with error code 2 @@ -111,6 +126,9 @@ let () = Callback.register "Printexc.handle_uncaught_exception" null (* Uncaught javascript exception *) let prog = {| +(* Ignore OCAMLRUNPARAM=b to be reproducible *) +Printexc.record_backtrace false;; + let null : _ -> _ -> _ = Array.unsafe_get [||] 0 let () = Callback.register "Printexc.handle_uncaught_exception" null exception D of int * string * Int64.t diff --git a/compiler/tests-compiler/lambda_lifting.ml b/compiler/tests-compiler/lambda_lifting.ml index f788ed7684..e272567fb1 100644 --- a/compiler/tests-compiler/lambda_lifting.ml +++ b/compiler/tests-compiler/lambda_lifting.ml @@ -22,17 +22,24 @@ Printf.printf "%d\n" (f 3) {| (function(globalThis){ "use strict"; + var runtime = globalThis.jsoo_runtime; + function caml_call2(f, a0, a1){ + return (f.l >= 0 ? f.l : f.l = f.length) == 2 + ? f(a0, a1) + : runtime.caml_call_gen(f, [a0, a1]); + } + runtime.caml_initialize_fiber_stack(); var - runtime = globalThis.jsoo_runtime, global_data = runtime.caml_get_global_data(), Stdlib_Printf = global_data.Stdlib__Printf, - _b_ = + _e_ = [0, [4, 0, 0, 0, [12, 10, 0]], runtime.caml_string_of_jsbytes("%d\n")]; - function f(x){var g$0 = g(x); return g$0(5);} - function h(x, y){function h(z){return (x + y | 0) + z | 0;} return h;} - function g(x){function g(y){var h$0 = h(x, y); return h$0(7);} return g;} - var _a_ = f(3); - runtime.caml_callback(Stdlib_Printf[2], [_b_, _a_]); + function f(x){ + function g(y){function h(z){return (x + y | 0) + z | 0;} return h(7);} + return g(5); + } + var _d_ = f(3); + caml_call2(Stdlib_Printf[2], _e_, _d_); var Test = [0]; runtime.caml_register_global(2, Test, "Test"); return; diff --git a/compiler/tests-jsoo/bin/error1.ml b/compiler/tests-jsoo/bin/error1.ml index 2e806c85d0..344a6c8f58 100644 --- a/compiler/tests-jsoo/bin/error1.ml +++ b/compiler/tests-jsoo/bin/error1.ml @@ -1,3 +1,6 @@ +(* Ignore OCAMLRUNPARAM=b to be reproducible *) +let () = Printexc.record_backtrace false + let () = match Array.to_list Sys.argv with | _ :: "unregister" :: _ -> diff --git a/compiler/tests-jsoo/bin/error2-unregister.expected b/compiler/tests-jsoo/bin/error2-unregister.expected index d0b406a4bf..9a9606e185 100644 --- a/compiler/tests-jsoo/bin/error2-unregister.expected +++ b/compiler/tests-jsoo/bin/error2-unregister.expected @@ -1,2 +1,2 @@ -Fatal error: exception Match_failure("compiler/tests-jsoo/bin/error2.ml", 13, 2) +Fatal error: exception Match_failure("compiler/tests-jsoo/bin/error2.ml", 16, 2) diff --git a/compiler/tests-jsoo/bin/error2.expected b/compiler/tests-jsoo/bin/error2.expected index 09937afc5a..746a067445 100644 --- a/compiler/tests-jsoo/bin/error2.expected +++ b/compiler/tests-jsoo/bin/error2.expected @@ -1 +1 @@ -Fatal error: exception File "compiler/tests-jsoo/bin/error2.ml", line 13, characters 2-7: Pattern matching failed +Fatal error: exception File "compiler/tests-jsoo/bin/error2.ml", line 16, characters 2-7: Pattern matching failed diff --git a/compiler/tests-jsoo/bin/error2.ml b/compiler/tests-jsoo/bin/error2.ml index f0274d9be8..ab27b2d5b0 100644 --- a/compiler/tests-jsoo/bin/error2.ml +++ b/compiler/tests-jsoo/bin/error2.ml @@ -1,3 +1,6 @@ +(* Ignore OCAMLRUNPARAM=b to be reproducible *) +let () = Printexc.record_backtrace false + let () = (* Make sure Printexc is linked *) let _ = Printexc.to_string Not_found in diff --git a/runtime/stdlib.js b/runtime/stdlib.js index 704d101789..b3ed3dbf5c 100644 --- a/runtime/stdlib.js +++ b/runtime/stdlib.js @@ -154,11 +154,13 @@ var caml_call_gen_tuple = ( //Provides: caml_call_gen //Requires: caml_call_gen_tuple //If: effects +//Weakdef var caml_call_gen = caml_call_gen_tuple[0]; //Provides: caml_call_gen_cps //Requires: caml_call_gen_tuple //If: effects +//Weakdef var caml_call_gen_cps = caml_call_gen_tuple[1]; //Provides: caml_named_values diff --git a/runtime/stdlib_modern.js b/runtime/stdlib_modern.js index 26c5ccd8d7..4718d87b3a 100644 --- a/runtime/stdlib_modern.js +++ b/runtime/stdlib_modern.js @@ -64,60 +64,95 @@ function caml_call_gen(f, args) { } } -//Provides: caml_call_gen (const, shallow) +//Provides: caml_call_gen_tuple (const, shallow) +//Requires: caml_fiber_stack, caml_cps_closure //If: effects -function caml_call_gen(f, args) { - var n = (f.l >= 0)?f.l:(f.l = f.length); - var argsLen = args.length; - var d = n - argsLen; - if (d == 0) - return f(...args); - else if (d < 0) { - var rest = args.slice(n - 1); - var k = args [argsLen - 1]; - args = args.slice(0, n); - args[n - 1] = function (g) { - if(typeof g !== "function") return k(g); - var args = rest.slice(); - args[args.length - 1] = k; - return caml_call_gen(g, args); }; - return f(...args); - } else { - argsLen--; - var k = args [argsLen]; - switch (d) { - case 1: { - var g = function (x, y){ - var nargs = new Array(argsLen + 2); - for(var i = 0; i < argsLen; i++ ) nargs[i] = args[i]; - nargs[argsLen] = x; - nargs[argsLen + 1] = y; - return f.apply(null, nargs) - }; - break; +var caml_call_gen_tuple = ( + function() { + function caml_call_gen_direct(f, args) { + var n = (f.l >= 0)?f.l:(f.l = f.length); + var argsLen = args.length; + var d = n - argsLen; + if (d == 0) { + return f(...args); + } else if (d < 0) { + return caml_call_gen_direct(f.apply(...args.slice(0,n)), args.slice(n)); + } else { + // FIXME: Restore the optimization of handling specially d = 1 or 2 + var ret = caml_cps_closure( + function (){ + var extra_args = (arguments.length+extra_args); + var nargs = new Array(args.length+extra_args); + for(var i = 0; i < args.length; i++) nargs[i] = args[i]; + for(var i = 0; i < arguments.length; i++) nargs[args.length+i] = arguments[i]; + return caml_call_gen_direct(f, nargs); + }, + function(){ + var extra_args = (arguments.length == 0)?1:arguments.length; + var nargs = new Array(argsLen + extra_args); + for(var i = 0; i < argsLen; i++ ) nargs[i] = args[i]; + for(var i = 0; i < arguments.length; i++ ) nargs[argsLen+i] = arguments[i]; + var cont = nargs[argsLen + extra_args - 1]; + return caml_call_gen_cps(f, nargs); + } + ); + ret.l = d; + ret.cps.l = d + 1; + return ret; + } } - case 2: { - var g = function (x, y, z){ - var nargs = new Array(argsLen + 3); - for(var i = 0; i < argsLen; i++ ) nargs[i] = args[i]; - nargs[argsLen] = x; - nargs[argsLen + 1] = y; - nargs[argsLen + 2] = z; - return f.apply(null, nargs) - }; - break; + function caml_call_gen_cps(f, args) { + var n = (f.cps.l >= 0)?f.cps.l:(f.cps.l = f.cps.length); + if (n === 0) return f.cps(...args); + var argsLen = args.length; + var d = n - argsLen; + if (d == 0) { + return f.cps(...args); + } + else if (d < 0) { + var rest = args.slice(n - 1); + var k = args[argsLen - 1]; + args = args.slice(0, n); + args[n - 1] = function(g) { + var args = rest.slice(); + args[args.length - 1] = k; + return caml_call_gen_cps(g, args); + }; + return f.cps(...args); + } else { + argsLen--; + var k = args[argsLen]; + var cont = + caml_cps_closure( + function (){ + var extra_args = (arguments.length == 0)?1:arguments.length; + var nargs = new Array(argsLen+extra_args); + for(var i = 0; i < argsLen; i++ ) nargs[i] = args[i]; + for(var i = 0; i < arguments.length; i++ ) nargs[argsLen+i] = arguments[i]; + return caml_call_gen_direct(f, nargs) + }, + function () { + var extra_args = (arguments.length == 0)?1:arguments.length; + var nargs = new Array(argsLen + extra_args); + for(var i = 0; i < argsLen; i++ ) nargs[i] = args[i]; + for(var i = 0; i < arguments.length; i++ ) nargs[argsLen+i] = arguments[i]; + return caml_call_gen_cps(f, nargs) + } ); + cont.l = d; + cont.cps.l = d + 1; + return k(cont); + } } - default: { - var g = function (){ - var extra_args = (arguments.length == 0)?1:arguments.length; - var nargs = new Array(argsLen + extra_args); - for(var i = 0; i < argsLen; i++ ) nargs[i] = args[i]; - for(var i = 0; i < arguments.length; i++ ) - nargs[argsLen + i] = arguments[i]; - return caml_call_gen(f, nargs) - }; - }} - g.l = d + 1; - return k(g); + return [caml_call_gen_direct, caml_call_gen_cps] } -} +)() + +//Provides: caml_call_gen +//Requires: caml_call_gen_tuple +//If: effects +var caml_call_gen = caml_call_gen_tuple[0]; + +//Provides: caml_call_gen_cps +//Requires: caml_call_gen_tuple +//If: effects +var caml_call_gen_cps = caml_call_gen_tuple[1];