From 1f8af1b94fb8dd49ed08bd3051e9e95d6a4f1c9b Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Thu, 30 Mar 2023 10:47:21 +0200 Subject: [PATCH] Turn off transformation for closures inside loops The scope of `var` is per-function, requiring a transformation for closures inside loops when capturing loop variables. This PR turns off the transformation. --- CHANGELOG.md | 1 + jscomp/core/j.ml | 3 +- jscomp/core/js_closure.ml | 31 ----------- jscomp/core/js_closure.mli | 35 ------------- jscomp/core/js_dump.ml | 78 +++------------------------- jscomp/core/js_fold.ml | 4 +- jscomp/core/js_fun_env.ml | 12 ----- jscomp/core/js_fun_env.mli | 4 -- jscomp/core/js_pass_scope.ml | 10 +--- jscomp/core/js_record_fold.ml | 4 +- jscomp/core/js_record_iter.ml | 4 +- jscomp/core/js_record_map.ml | 8 +-- jscomp/core/js_stmt_make.ml | 10 ++-- jscomp/core/js_stmt_make.mli | 2 - jscomp/test/async_inside_loop.js | 6 +-- jscomp/test/cps_test.js | 18 +++---- jscomp/test/for_loop_test.js | 50 +++++------------- jscomp/test/gpr_858_unit2_test.js | 6 +-- jscomp/test/hamming_test.js | 86 +++++++++++++++---------------- jscomp/test/ocaml_re_test.js | 46 ++++++----------- jscomp/test/test_closure.js | 6 +-- jscomp/test/test_cps.js | 12 ++--- jscomp/test/test_for_loop.js | 26 +++------- jscomp/test/test_while_closure.js | 6 +-- lib/es6/arg.js | 24 +++------ lib/es6/caml_format.js | 12 ++--- lib/es6/curry.js | 4 +- lib/js/arg.js | 24 +++------ lib/js/caml_format.js | 12 ++--- lib/js/curry.js | 4 +- 30 files changed, 150 insertions(+), 398 deletions(-) delete mode 100644 jscomp/core/js_closure.ml delete mode 100644 jscomp/core/js_closure.mli diff --git a/CHANGELOG.md b/CHANGELOG.md index fe4ef2de8a..898e3e72a4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,7 @@ - Fix indent for returned/thrown/wrapped in parentheses objects in generated js code. https://github.com/rescript-lang/rescript-compiler/pull/6746 - Fix indent in generated js code. https://github.com/rescript-lang/rescript-compiler/pull/6747 - In generated code, use `let` instead of `var`. https://github.com/rescript-lang/rescript-compiler/pull/6102 +- Turn off transformation for closures inside loops when capturing loop variables, now that `let` is emitted instead of `var`. https://github.com/rescript-lang/rescript-compiler/pull/6480 # 11.1.0 diff --git a/jscomp/core/j.ml b/jscomp/core/j.ml index acbdc06998..3b00a6a8a2 100644 --- a/jscomp/core/j.ml +++ b/jscomp/core/j.ml @@ -254,7 +254,7 @@ and statement_desc = (* Function declaration and Variable declaration *) | Exp of expression | If of expression * block * block - | While of label option * expression * block * Js_closure.t + | While of label option * expression * block (* check if it contains loop mutable values, happens in nested loop *) | ForRange of for_ident_expression option @@ -262,7 +262,6 @@ and statement_desc = * for_ident * for_direction * block - * Js_closure.t | Continue of label | Break (* only used when inline a fucntion *) | Return of expression diff --git a/jscomp/core/js_closure.ml b/jscomp/core/js_closure.ml deleted file mode 100644 index dc068bd446..0000000000 --- a/jscomp/core/js_closure.ml +++ /dev/null @@ -1,31 +0,0 @@ -(* Copyright (C) 2015 - 2016 Bloomberg Finance L.P. - * Copyright (C) 2016 - Hongbo Zhang, Authors of ReScript - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * In addition to the permissions granted to you by the LGPL, you may combine - * or link a "work that uses the Library" with a publicly distributed version - * of this file to produce a combined library or application, then distribute - * that combined work under the terms of your choosing, with no requirement - * to comply with the obligations normally placed on you by section 4 of the - * LGPL version 3 (or the corresponding section of a later version of the LGPL - * should you choose to use a later version). - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) - -type t = { mutable outer_loop_mutable_values : Set_ident.t } - -let empty () = { outer_loop_mutable_values = Set_ident.empty } - -let set_lexical_scope t v = t.outer_loop_mutable_values <- v - -let get_lexical_scope t = t.outer_loop_mutable_values diff --git a/jscomp/core/js_closure.mli b/jscomp/core/js_closure.mli deleted file mode 100644 index 8ff3903c0c..0000000000 --- a/jscomp/core/js_closure.mli +++ /dev/null @@ -1,35 +0,0 @@ -(* Copyright (C) 2015-2016 Bloomberg Finance L.P. - * Copyright (C) 2016 - Hongbo Zhang, Authors of ReScript - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * In addition to the permissions granted to you by the LGPL, you may combine - * or link a "work that uses the Library" with a publicly distributed version - * of this file to produce a combined library or application, then distribute - * that combined work under the terms of your choosing, with no requirement - * to comply with the obligations normally placed on you by section 4 of the - * LGPL version 3 (or the corresponding section of a later version of the LGPL - * should you choose to use a later version). - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) - -(** Define a type used in JS IR to help convert lexical scope to JS [var] - based scope convention -*) - -type t = { mutable outer_loop_mutable_values : Set_ident.t } - -val empty : unit -> t - -val get_lexical_scope : t -> Set_ident.t - -val set_lexical_scope : t -> Set_ident.t -> unit diff --git a/jscomp/core/js_dump.ml b/jscomp/core/js_dump.ml index bbd841bde6..2d9332e0ca 100644 --- a/jscomp/core/js_dump.ml +++ b/jscomp/core/js_dump.ml @@ -164,14 +164,6 @@ let exp_need_paren (e : J.expression) = | Await _ -> false | Tagged_template _ -> false -let comma_idents (cxt : cxt) f ls = iter_lst cxt f ls Ext_pp_scope.ident comma - -let pp_paren_params (inner_cxt : cxt) (f : Ext_pp.t) (lexical : Ident.t list) : - unit = - P.string f L.lparen; - let (_ : cxt) = comma_idents inner_cxt f lexical in - P.string f L.rparen - (** Print as underscore for unused vars, may not be needed in the future *) (* let ipp_ident cxt f id (un_used : bool) = @@ -381,10 +373,9 @@ and pp_function ~return_unit ~async ~is_method cxt (f : P.t) ~fn_state P.space f; P.brace_vgroup f 1 (fun _ -> function_body ~return_unit cxt f b) in - let lexical : Set_ident.t = Js_fun_env.get_lexical_scope env in - let enclose lexical = - let handle lexical = - if Set_ident.is_empty lexical then ( + let enclose () = + let handle () = + ( match fn_state with | Is_return -> return_sp f; @@ -408,46 +399,10 @@ and pp_function ~return_unit ~async ~is_method cxt (f : P.t) ~fn_state P.space f; ignore (Ext_pp_scope.ident inner_cxt f x : cxt); param_body ()) - else - (* print our closure as - {[(function(x,y){ return function(..){...}} (x,y))]} - Maybe changed to `let` in the future - *) - let lexical = Set_ident.elements lexical in - (match fn_state with - | Is_return -> return_sp f - | No_name _ -> () - | Name_non_top name | Name_top name -> - ignore (pp_var_assign inner_cxt f name : cxt)); - if async then P.string f L.await; - P.string f L.lparen; - P.string f (L.function_async ~async); - pp_paren_params inner_cxt f lexical; - P.brace_vgroup f 0 (fun _ -> - return_sp f; - P.string f (L.function_async ~async); - P.space f; - (match fn_state with - | Is_return | No_name _ -> () - | Name_non_top x | Name_top x -> - ignore (Ext_pp_scope.ident inner_cxt f x)); - param_body ()); - pp_paren_params inner_cxt f lexical; - P.string f L.rparen; - match fn_state with - | Is_return | No_name _ -> () (* expression *) - | _ -> semi f - (* has binding, a statement *) in - handle - (match fn_state with - | (Name_top name | Name_non_top name) when Set_ident.mem lexical name - -> - (*TODO: when calculating lexical we should not include itself *) - Set_ident.remove lexical name - | _ -> lexical) + handle () in - enclose lexical; + enclose (); outer_cxt (* Assume the cond would not change the context, @@ -1065,7 +1020,7 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt = P.string f L.else_; P.space f; brace_block cxt f s2) - | While (label, e, s, _env) -> + | While (label, e, s) -> (* FIXME: print scope as well *) (match label with | Some i -> @@ -1093,7 +1048,7 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt = let cxt = brace_block cxt f s in semi f; cxt - | ForRange (for_ident_expression, finish, id, direction, s, env) -> + | ForRange (for_ident_expression, finish, id, direction, s) -> let action cxt = P.vgroup f 0 (fun _ -> let cxt = @@ -1164,24 +1119,7 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt = in brace_block cxt f s) in - let lexical = Js_closure.get_lexical_scope env in - if Set_ident.is_empty lexical then action cxt - else - (* unlike function, - [print for loop] has side effect, - we should take it out - *) - let inner_cxt = Ext_pp_scope.merge cxt lexical in - let lexical = Set_ident.elements lexical in - P.vgroup f 0 (fun _ -> - P.string f L.lparen; - P.string f L.function_; - pp_paren_params inner_cxt f lexical; - let cxt = P.brace_vgroup f 0 (fun _ -> action inner_cxt) in - pp_paren_params inner_cxt f lexical; - P.string f L.rparen; - semi f; - cxt) + action cxt | Continue s -> continue f s; cxt diff --git a/jscomp/core/js_fold.ml b/jscomp/core/js_fold.ml index 506b6d5a44..c3b129f6ff 100644 --- a/jscomp/core/js_fold.ml +++ b/jscomp/core/js_fold.ml @@ -217,12 +217,12 @@ class fold = let _self = _self#block _x1 in let _self = _self#block _x2 in _self - | While (_x0, _x1, _x2, _x3) -> + | While (_x0, _x1, _x2) -> let _self = option (fun _self -> _self#label) _self _x0 in let _self = _self#expression _x1 in let _self = _self#block _x2 in _self - | ForRange (_x0, _x1, _x2, _x3, _x4, _x5) -> + | ForRange (_x0, _x1, _x2, _x3, _x4) -> let _self = option (fun _self -> _self#for_ident_expression) _self _x0 in diff --git a/jscomp/core/js_fun_env.ml b/jscomp/core/js_fun_env.ml index 6ed8101374..e90ba5ee00 100644 --- a/jscomp/core/js_fun_env.ml +++ b/jscomp/core/js_fun_env.ml @@ -44,7 +44,6 @@ type immutable_mask = type t = { mutable unbounded : Set_ident.t; - mutable bound_loop_mutable_values : Set_ident.t; used_mask : bool array; immutable_mask : immutable_mask; } @@ -58,7 +57,6 @@ let make ?immutable_mask n = (match immutable_mask with | Some x -> Immutable_mask x | None -> All_immutable_and_no_tail_call); - bound_loop_mutable_values = Set_ident.empty; } let no_tailcall x = @@ -92,13 +90,3 @@ let set_unbounded env v = (* if Set_ident.is_empty env.bound then *) env.unbounded <- v (* else assert false *) - -let set_lexical_scope env bound_loop_mutable_values = - env.bound_loop_mutable_values <- bound_loop_mutable_values - -let get_lexical_scope env = env.bound_loop_mutable_values - -(* TODO: can be refined if it - only enclose toplevel variables -*) -(* let is_empty t = Set_ident.is_empty t.unbounded *) diff --git a/jscomp/core/js_fun_env.mli b/jscomp/core/js_fun_env.mli index 5cfa7f6f34..397ebedcd8 100644 --- a/jscomp/core/js_fun_env.mli +++ b/jscomp/core/js_fun_env.mli @@ -36,10 +36,6 @@ val no_tailcall : t -> bool list val set_unbounded : t -> Set_ident.t -> unit -val set_lexical_scope : t -> Set_ident.t -> unit - -val get_lexical_scope : t -> Set_ident.t - (* val to_string : t -> string *) val mark_unused : t -> int -> unit diff --git a/jscomp/core/js_pass_scope.ml b/jscomp/core/js_pass_scope.ml index ebeb22c130..3e8b946eb4 100644 --- a/jscomp/core/js_pass_scope.ml +++ b/jscomp/core/js_pass_scope.ml @@ -173,10 +173,6 @@ let record_scope_pass = due to the recursive thing *) Js_fun_env.set_unbounded env closured_idents'; - let lexical_scopes = - Set_ident.(inter closured_idents' state.loop_mutable_values) - in - Js_fun_env.set_lexical_scope env lexical_scopes; (* tailcall , note that these varibles are used in another pass *) { state with @@ -242,7 +238,7 @@ let record_scope_pass = statement = (fun self state x -> match x.statement_desc with - | ForRange (_, _, loop_id, _, _, a_env) -> + | ForRange (_, _, loop_id, _, _) -> (* TODO: simplify definition of For *) let { defined_idents = defined_idents'; @@ -274,8 +270,6 @@ let record_scope_pass = (diff closured_idents' defined_idents') state.loop_mutable_values) in - let () = Js_closure.set_lexical_scope a_env lexical_scope in - (* set scope *) { state with used_idents = Set_ident.union state.used_idents used_idents'; @@ -293,7 +287,7 @@ let record_scope_pass = closured_idents = Set_ident.union state.closured_idents lexical_scope; } - | While (_label, pred, body, _env) -> + | While (_label, pred, body) -> with_in_loop (self.block self (with_in_loop (self.expression self state pred) true) diff --git a/jscomp/core/js_record_fold.ml b/jscomp/core/js_record_fold.ml index b92ca551ad..e6df8e0a12 100644 --- a/jscomp/core/js_record_fold.ml +++ b/jscomp/core/js_record_fold.ml @@ -223,12 +223,12 @@ let statement_desc : 'a. ('a, statement_desc) fn = let st = _self.block _self st _x1 in let st = _self.block _self st _x2 in st - | While (_x0, _x1, _x2, _x3) -> + | While (_x0, _x1, _x2) -> let st = option label _self st _x0 in let st = _self.expression _self st _x1 in let st = _self.block _self st _x2 in st - | ForRange (_x0, _x1, _x2, _x3, _x4, _x5) -> + | ForRange (_x0, _x1, _x2, _x3, _x4) -> let st = option for_ident_expression _self st _x0 in let st = finish_ident_expression _self st _x1 in let st = _self.for_ident _self st _x2 in diff --git a/jscomp/core/js_record_iter.ml b/jscomp/core/js_record_iter.ml index cbbc9f41e2..a5d3fd24da 100644 --- a/jscomp/core/js_record_iter.ml +++ b/jscomp/core/js_record_iter.ml @@ -163,11 +163,11 @@ let statement_desc : statement_desc fn = _self.expression _self _x0; _self.block _self _x1; _self.block _self _x2 - | While (_x0, _x1, _x2, _x3) -> + | While (_x0, _x1, _x2) -> option label _self _x0; _self.expression _self _x1; _self.block _self _x2 - | ForRange (_x0, _x1, _x2, _x3, _x4, _x5) -> + | ForRange (_x0, _x1, _x2, _x3, _x4) -> option for_ident_expression _self _x0; finish_ident_expression _self _x1; _self.for_ident _self _x2; diff --git a/jscomp/core/js_record_map.ml b/jscomp/core/js_record_map.ml index c7d0cdfa62..800fa56c10 100644 --- a/jscomp/core/js_record_map.ml +++ b/jscomp/core/js_record_map.ml @@ -221,18 +221,18 @@ let statement_desc : statement_desc fn = let _x1 = _self.block _self _x1 in let _x2 = _self.block _self _x2 in If (_x0, _x1, _x2) - | While (_x0, _x1, _x2, _x3) -> + | While (_x0, _x1, _x2) -> let _x0 = option label _self _x0 in let _x1 = _self.expression _self _x1 in let _x2 = _self.block _self _x2 in - While (_x0, _x1, _x2, _x3) - | ForRange (_x0, _x1, _x2, _x3, _x4, _x5) -> + While (_x0, _x1, _x2) + | ForRange (_x0, _x1, _x2, _x3, _x4) -> let _x0 = option for_ident_expression _self _x0 in let _x1 = finish_ident_expression _self _x1 in let _x2 = _self.for_ident _self _x2 in let _x3 = for_direction _self _x3 in let _x4 = _self.block _self _x4 in - ForRange (_x0, _x1, _x2, _x3, _x4, _x5) + ForRange (_x0, _x1, _x2, _x3, _x4) | Continue _x0 -> let _x0 = label _self _x0 in Continue _x0 diff --git a/jscomp/core/js_stmt_make.ml b/jscomp/core/js_stmt_make.ml index b2550bfb99..718a8b6a17 100644 --- a/jscomp/core/js_stmt_make.ml +++ b/jscomp/core/js_stmt_make.ml @@ -318,17 +318,15 @@ let if_ ?comment ?declaration ?else_ (e : J.expression) (then_ : J.block) : t = let assign ?comment id e : t = { statement_desc = J.Exp (E.assign (E.var id) e); comment } -let while_ ?comment ?label ?env (e : E.t) (st : J.block) : t = - let env = match env with None -> Js_closure.empty () | Some x -> x in - { statement_desc = While (label, e, st, env); comment } +let while_ ?comment ?label (e : E.t) (st : J.block) : t = + { statement_desc = While (label, e, st); comment } -let for_ ?comment ?env for_ident_expression finish_ident_expression id direction +let for_ ?comment for_ident_expression finish_ident_expression id direction (b : J.block) : t = - let env = match env with None -> Js_closure.empty () | Some x -> x in { statement_desc = ForRange - (for_ident_expression, finish_ident_expression, id, direction, b, env); + (for_ident_expression, finish_ident_expression, id, direction, b); comment; } diff --git a/jscomp/core/js_stmt_make.mli b/jscomp/core/js_stmt_make.mli index 2aca68ac5b..c3716e135e 100644 --- a/jscomp/core/js_stmt_make.mli +++ b/jscomp/core/js_stmt_make.mli @@ -133,14 +133,12 @@ val assign : ?comment:string -> J.ident -> J.expression -> t val while_ : ?comment:string -> ?label:J.label -> - ?env:Js_closure.t -> J.expression -> J.block -> t val for_ : ?comment:string -> - ?env:Js_closure.t -> J.for_ident_expression option -> J.finish_ident_expression -> J.for_ident -> diff --git a/jscomp/test/async_inside_loop.js b/jscomp/test/async_inside_loop.js index 4a5b8f3945..9ec90b5307 100644 --- a/jscomp/test/async_inside_loop.js +++ b/jscomp/test/async_inside_loop.js @@ -4,12 +4,10 @@ async function topLevelAsyncFunction(param) { for(let innerScopeVal = 0; innerScopeVal <= 3; ++innerScopeVal){ - let asyncClosureAccessingScopedVal = await(async function(innerScopeVal){ - return async function asyncClosureAccessingScopedVal(param) { + let asyncClosureAccessingScopedVal = async function (param) { console.log("Accessing scoped var inside loop", innerScopeVal); return await Promise.resolve(); - } - }(innerScopeVal)); + }; await asyncClosureAccessingScopedVal(); } } diff --git a/jscomp/test/cps_test.js b/jscomp/test/cps_test.js index b85d0b4ee5..8072089641 100644 --- a/jscomp/test/cps_test.js +++ b/jscomp/test/cps_test.js @@ -17,12 +17,10 @@ function test(param) { if (n === 0) { return Curry._1(acc, undefined); } - _acc = (function(n,acc){ - return function (param) { + _acc = (function (param) { v.contents = v.contents + n | 0; return Curry._1(acc, undefined); - } - }(n,acc)); + }); _n = n - 1 | 0; continue ; }; @@ -41,11 +39,9 @@ function test_closure(param) { return x; })); for(let i = 0; i <= 5; ++i){ - Caml_array.set(arr, i, (function(i){ - return function (param) { + Caml_array.set(arr, i, (function (param) { return i; - } - }(i))); + })); } $$Array.iter((function (i) { v.contents = v.contents + Curry._1(i, 0) | 0; @@ -62,11 +58,9 @@ function test_closure2(param) { })); for(let i = 0; i <= 5; ++i){ let j = i + i | 0; - Caml_array.set(arr, i, (function(j){ - return function (param) { + Caml_array.set(arr, i, (function (param) { return j; - } - }(j))); + })); } $$Array.iter((function (i) { v.contents = v.contents + Curry._1(i, 0) | 0; diff --git a/jscomp/test/for_loop_test.js b/jscomp/test/for_loop_test.js index 7f447c2fd9..ace1d0413c 100644 --- a/jscomp/test/for_loop_test.js +++ b/jscomp/test/for_loop_test.js @@ -15,11 +15,9 @@ function for_3(x) { }), x); for(let i = 0 ,i_finish = x.length; i < i_finish; ++i){ let j = (i << 1); - Caml_array.set(arr, i, (function(j){ - return function (param) { + Caml_array.set(arr, i, (function (param) { v.contents = v.contents + j | 0; - } - }(j))); + })); } $$Array.iter((function (x) { Curry._1(x, undefined); @@ -37,11 +35,9 @@ function for_4(x) { for(let i = 0 ,i_finish = x.length; i < i_finish; ++i){ let j = (i << 1); let k = (j << 1); - Caml_array.set(arr, i, (function(k){ - return function (param) { + Caml_array.set(arr, i, (function (param) { v.contents = v.contents + k | 0; - } - }(k))); + })); } $$Array.iter((function (x) { Curry._1(x, undefined); @@ -58,11 +54,9 @@ function for_5(x, u) { }), x); for(let i = 0 ,i_finish = x.length; i < i_finish; ++i){ let k = Math.imul((u << 1), u); - Caml_array.set(arr, i, (function(k){ - return function (param) { + Caml_array.set(arr, i, (function (param) { v.contents = v.contents + k | 0; - } - }(k))); + })); } $$Array.iter((function (x) { Curry._1(x, undefined); @@ -90,18 +84,14 @@ function for_6(x, u) { let v2 = { contents: 0 }; - (function(v2){ for(let i = 0 ,i_finish = x.length; i < i_finish; ++i){ let k = Math.imul((u << 1), u); let h = (v5.contents << 1); v2.contents = v2.contents + 1 | 0; - Caml_array.set(arr, i, (function(k,h){ - return function (param) { + Caml_array.set(arr, i, (function (param) { v.contents = (((((v.contents + k | 0) + v2.contents | 0) + v4.contents | 0) + v5.contents | 0) + h | 0) + u | 0; - } - }(k,h))); + })); } - }(v2)); inspect_3 = v2.contents; } $$Array.iter((function (x) { @@ -123,15 +113,11 @@ function for_7(param) { })); for(let i = 0; i <= 6; ++i){ - (function(i){ for(let j = 0; j <= 2; ++j){ - Caml_array.set(arr, Math.imul(i, 3) + j | 0, (function(j){ - return function (param) { + Caml_array.set(arr, Math.imul(i, 3) + j | 0, (function (param) { v.contents = (v.contents + i | 0) + j | 0; - } - }(j))); + })); } - }(i)); } $$Array.iter((function (f) { Curry._1(f, undefined); @@ -148,16 +134,12 @@ function for_8(param) { })); for(let i = 0; i <= 6; ++i){ let k = (i << 1); - (function(i,k){ for(let j = 0; j <= 2; ++j){ let h = i + j | 0; - Caml_array.set(arr, Math.imul(i, 3) + j | 0, (function(j,h){ - return function (param) { + Caml_array.set(arr, Math.imul(i, 3) + j | 0, (function (param) { v.contents = (((v.contents + i | 0) + j | 0) + h | 0) + k | 0; - } - }(j,h))); + })); } - }(i,k)); } $$Array.iter((function (f) { Curry._1(f, undefined); @@ -192,7 +174,6 @@ function for_9(param) { contents: 0 }; v$1.contents = v$1.contents + i | 0; - (function(v$1){ for(let j = 0; j <= 1; ++j){ v$1.contents = v$1.contents + 1 | 0; collect(v$1.contents); @@ -200,12 +181,9 @@ function for_9(param) { vv.contents = vv.contents + v$1.contents | 0; })); } - }(v$1)); - Caml_array.set(arr2, i, (function(v$1){ - return function (param) { + Caml_array.set(arr2, i, (function (param) { vv2.contents = vv2.contents + v$1.contents | 0; - } - }(v$1))); + })); } $$Array.iter((function (f) { Curry._1(f, undefined); diff --git a/jscomp/test/gpr_858_unit2_test.js b/jscomp/test/gpr_858_unit2_test.js index 877e0c0185..e296bebc5d 100644 --- a/jscomp/test/gpr_858_unit2_test.js +++ b/jscomp/test/gpr_858_unit2_test.js @@ -10,8 +10,7 @@ let delayed = { }; for(let i = 1; i <= 2; ++i){ - let f = (function(i){ - return function f(n, x) { + let f = function (n, x) { if (x !== 0) { let prev = delayed.contents; delayed.contents = (function (param) { @@ -32,8 +31,7 @@ for(let i = 1; i <= 2; ++i){ ], Error: new Error() }; - } - }(i)); + }; f(0, i); } diff --git a/jscomp/test/hamming_test.js b/jscomp/test/hamming_test.js index 6a42718478..7be16c6a50 100644 --- a/jscomp/test/hamming_test.js +++ b/jscomp/test/hamming_test.js @@ -1,45 +1,45 @@ // Generated by ReScript, PLEASE EDIT WITH CARE 'use strict'; -var Mt = require("./mt.js"); -var Curry = require("../../lib/js/curry.js"); -var $$Buffer = require("../../lib/js/buffer.js"); -var Caml_obj = require("../../lib/js/caml_obj.js"); -var Caml_int64 = require("../../lib/js/caml_int64.js"); -var Caml_format = require("../../lib/js/caml_format.js"); -var CamlinternalLazy = require("../../lib/js/camlinternalLazy.js"); +let Mt = require("./mt.js"); +let Curry = require("../../lib/js/curry.js"); +let $$Buffer = require("../../lib/js/buffer.js"); +let Caml_obj = require("../../lib/js/caml_obj.js"); +let Caml_int64 = require("../../lib/js/caml_int64.js"); +let Caml_format = require("../../lib/js/caml_format.js"); +let CamlinternalLazy = require("../../lib/js/camlinternalLazy.js"); -var n0 = Caml_int64.zero; +let n0 = Caml_int64.zero; -var n1 = Caml_int64.one; +let n1 = Caml_int64.one; -var n2 = [ +let n2 = [ 0, 2 ]; -var n3 = [ +let n3 = [ 0, 3 ]; -var n5 = [ +let n5 = [ 0, 5 ]; -var $percent = Caml_int64.mod_; +let $percent = Caml_int64.mod_; -var $star = Caml_int64.mul; +let $star = Caml_int64.mul; -var $slash = Caml_int64.div; +let $slash = Caml_int64.div; -var $plus = Caml_int64.add; +let $plus = Caml_int64.add; -var digit = Caml_format.int64_of_string("1000000000000000000"); +let digit = Caml_format.int64_of_string("1000000000000000000"); function mul(n, param) { - var pl = param[0]; + let pl = param[0]; return [ Caml_int64.mod_(Caml_int64.mul(n, pl), digit), Caml_int64.add(Caml_int64.mul(n, param[1]), Caml_int64.div(Caml_int64.mul(n, pl), digit)) @@ -47,16 +47,16 @@ function mul(n, param) { } function cmp(param, param$1) { - var ph = param$1[1]; - var nh = param[1]; + let ph = param$1[1]; + let nh = param[1]; if (Caml_obj.lessthan(nh, ph)) { return -1; } if (Caml_obj.greaterthan(nh, ph)) { return 1; } - var pl = param$1[0]; - var nl = param[0]; + let pl = param$1[0]; + let nl = param[0]; if (Caml_obj.lessthan(nl, pl)) { return -1; } else if (Caml_obj.greaterthan(nl, pl)) { @@ -78,12 +78,12 @@ function x5(p) { return mul(n5, p); } -var nn1 = [ +let nn1 = [ n1, n0 ]; -var buf = $$Buffer.create(5000); +let buf = $$Buffer.create(5000); function paddding(s) { if (s.length <= 18) { @@ -94,8 +94,8 @@ function paddding(s) { } function pr(param) { - var nh = param[1]; - var nl = param[0]; + let nh = param[1]; + let nl = param[0]; if (Caml_int64.equal(nh, n0)) { $$Buffer.add_string(buf, Caml_int64.to_string(nl)); return $$Buffer.add_string(buf, "\n"); @@ -110,7 +110,7 @@ function map(f, l) { return { LAZY_DONE: false, VAL: (function () { - var match = CamlinternalLazy.force(l); + let match = CamlinternalLazy.force(l); return { TAG: "Cons", _0: Curry._1(f, match._0), @@ -124,13 +124,13 @@ function merge(cmp, l1, l2) { return { LAZY_DONE: false, VAL: (function () { - var match = CamlinternalLazy.force(l1); - var match$1 = CamlinternalLazy.force(l2); - var ll2 = match$1._1; - var x2 = match$1._0; - var ll1 = match._1; - var x1 = match._0; - var c = Curry._2(cmp, x1, x2); + let match = CamlinternalLazy.force(l1); + let match$1 = CamlinternalLazy.force(l2); + let ll2 = match$1._1; + let x2 = match$1._0; + let ll1 = match._1; + let x1 = match._0; + let c = Curry._2(cmp, x1, x2); if (c === 0) { return { TAG: "Cons", @@ -156,14 +156,14 @@ function merge(cmp, l1, l2) { function iter_interval(f, _l, _param) { while(true) { - var param = _param; - var l = _l; - var stop = param[1]; + let param = _param; + let l = _l; + let stop = param[1]; if (stop === 0) { return ; } - var start = param[0]; - var match = CamlinternalLazy.force(l); + let start = param[0]; + let match = CamlinternalLazy.force(l); if (start <= 0) { Curry._1(f, match._0); } @@ -176,7 +176,7 @@ function iter_interval(f, _l, _param) { }; } -var hamming = { +let hamming = { LAZY_DONE: false, VAL: (function () { return { @@ -187,21 +187,21 @@ var hamming = { }) }; -var ham2 = { +let ham2 = { LAZY_DONE: false, VAL: (function () { return CamlinternalLazy.force(map(x2, hamming)); }) }; -var ham3 = { +let ham3 = { LAZY_DONE: false, VAL: (function () { return CamlinternalLazy.force(map(x3, hamming)); }) }; -var ham5 = { +let ham5 = { LAZY_DONE: false, VAL: (function () { return CamlinternalLazy.force(map(x5, hamming)); diff --git a/jscomp/test/ocaml_re_test.js b/jscomp/test/ocaml_re_test.js index e56c8c5f38..6909a86aac 100644 --- a/jscomp/test/ocaml_re_test.js +++ b/jscomp/test/ocaml_re_test.js @@ -2414,12 +2414,10 @@ function translate(ids, kind, _ign_group, ign_case, _greedy, pos, cache, c, _x) ]; } return [ - alt(ids, List.map((function(ign_group,greedy){ - return function (r$p) { + alt(ids, List.map((function (r$p) { let match = translate(ids, kind, ign_group, ign_case, greedy, pos, cache, c, r$p); return enforce_kind(ids, kind, match[1], match[0]); - } - }(ign_group,greedy)), merged_sequences)), + }), merged_sequences)), kind ]; case "Repeat" : @@ -2430,8 +2428,7 @@ function translate(ids, kind, _ign_group, ign_case, _greedy, pos, cache, c, _x) let cr = match$1[0]; let rem; if (j !== undefined) { - let f = greedy === "Non_greedy" ? (function(cr,kind$p){ - return function (rem) { + let f = greedy === "Non_greedy" ? (function (rem) { return alt(ids, { hd: mk_expr(ids, "Eps"), tl: { @@ -2439,9 +2436,7 @@ function translate(ids, kind, _ign_group, ign_case, _greedy, pos, cache, c, _x) tl: /* [] */0 } }); - } - }(cr,kind$p)) : (function(cr,kind$p){ - return function (rem) { + }) : (function (rem) { return alt(ids, { hd: seq$1(ids, kind$p, rename(ids, cr), rem), tl: { @@ -2449,18 +2444,15 @@ function translate(ids, kind, _ign_group, ign_case, _greedy, pos, cache, c, _x) tl: /* [] */0 } }); - } - }(cr,kind$p)); + }); rem = iter(j - i | 0, f, mk_expr(ids, "Eps")); } else { rem = rep(ids, greedy, kind$p, cr); } return [ - iter(i, (function(cr,kind$p){ - return function (rem) { + iter(i, (function (rem) { return seq$1(ids, kind$p, rename(ids, cr), rem); - } - }(cr,kind$p)), rem), + }), rem), kind ]; case "Sem" : @@ -2607,18 +2599,14 @@ function handle_case(_ign_case, _x) { case "Sequence" : return { TAG: "Sequence", - _0: List.map((function(ign_case){ - return function (param) { + _0: List.map((function (param) { return handle_case(ign_case, param); - } - }(ign_case)), x._0) + }), x._0) }; case "Alternative" : - let l$p = List.map((function(ign_case){ - return function (param) { + let l$p = List.map((function (param) { return handle_case(ign_case, param); - } - }(ign_case)), x._0); + }), x._0); if (is_charset({ TAG: "Alternative", _0: l$p @@ -2698,11 +2686,9 @@ function handle_case(_ign_case, _x) { _ign_case = true; continue ; case "Intersection" : - let l$p$1 = List.map((function(ign_case){ - return function (r) { + let l$p$1 = List.map((function (r) { return handle_case(ign_case, r); - } - }(ign_case)), x._0); + }), x._0); return { TAG: "Set", _0: List.fold_left((function (s, r) { @@ -2710,11 +2696,9 @@ function handle_case(_ign_case, _x) { }), cany, l$p$1) }; case "Complement" : - let l$p$2 = List.map((function(ign_case){ - return function (r) { + let l$p$2 = List.map((function (r) { return handle_case(ign_case, r); - } - }(ign_case)), x._0); + }), x._0); return { TAG: "Set", _0: diff(cany, List.fold_left((function (s, r) { diff --git a/jscomp/test/test_closure.js b/jscomp/test/test_closure.js index 0476b869e6..513556665c 100644 --- a/jscomp/test/test_closure.js +++ b/jscomp/test/test_closure.js @@ -14,11 +14,9 @@ function f(param) { })); for(let i = 0; i <= 9; ++i){ - Caml_array.set(arr, i, (function(i){ - return function (param) { + Caml_array.set(arr, i, (function (param) { v.contents = v.contents + i | 0; - } - }(i))); + })); } return arr; } diff --git a/jscomp/test/test_cps.js b/jscomp/test/test_cps.js index cce2161b26..d1f9dba439 100644 --- a/jscomp/test/test_cps.js +++ b/jscomp/test/test_cps.js @@ -11,12 +11,10 @@ function f(_n, _acc) { if (n === 0) { return Curry._1(acc, undefined); } - _acc = (function(n,acc){ - return function (param) { + _acc = (function (param) { console.log(String(n)); return Curry._1(acc, undefined); - } - }(n,acc)); + }); _n = n - 1 | 0; continue ; }; @@ -27,11 +25,9 @@ function test_closure(param) { return x; })); for(let i = 0; i <= 6; ++i){ - Caml_array.set(arr, i, (function(i){ - return function (param) { + Caml_array.set(arr, i, (function (param) { return i; - } - }(i))); + })); } return arr; } diff --git a/jscomp/test/test_for_loop.js b/jscomp/test/test_for_loop.js index e9bed9b19e..b3f53e4e04 100644 --- a/jscomp/test/test_for_loop.js +++ b/jscomp/test/test_for_loop.js @@ -26,11 +26,9 @@ function for_3(x) { }), x); for(let i = 0 ,i_finish = x.length; i <= i_finish; ++i){ let j = (i << 1); - Caml_array.set(arr, i, (function(j){ - return function (param) { + Caml_array.set(arr, i, (function (param) { v.contents = v.contents + j | 0; - } - }(j))); + })); } $$Array.iter((function (x) { Curry._1(x, undefined); @@ -48,11 +46,9 @@ function for_4(x) { for(let i = 0 ,i_finish = x.length; i <= i_finish; ++i){ let j = (i << 1); let k = (j << 1); - Caml_array.set(arr, i, (function(k){ - return function (param) { + Caml_array.set(arr, i, (function (param) { v.contents = v.contents + k | 0; - } - }(k))); + })); } $$Array.iter((function (x) { Curry._1(x, undefined); @@ -69,11 +65,9 @@ function for_5(x, u) { }), x); for(let i = 0 ,i_finish = x.length; i <= i_finish; ++i){ let k = Math.imul((u << 1), u); - Caml_array.set(arr, i, (function(k){ - return function (param) { + Caml_array.set(arr, i, (function (param) { v.contents = v.contents + k | 0; - } - }(k))); + })); } $$Array.iter((function (x) { Curry._1(x, undefined); @@ -100,18 +94,14 @@ function for_6(x, u) { let v2 = { contents: 0 }; - (function(v2){ for(let i = 0 ,i_finish = x.length; i <= i_finish; ++i){ let k = Math.imul((u << 1), u); let h = (v5.contents << 1); v2.contents = v2.contents + 1 | 0; - Caml_array.set(arr, i, (function(k,h){ - return function (param) { + Caml_array.set(arr, i, (function (param) { v.contents = (((((v.contents + k | 0) + v2.contents | 0) + u | 0) + v4.contents | 0) + v5.contents | 0) + h | 0; - } - }(k,h))); + })); } - }(v2)); } $$Array.iter((function (x) { Curry._1(x, undefined); diff --git a/jscomp/test/test_while_closure.js b/jscomp/test/test_while_closure.js index f94badf794..7501a52a1e 100644 --- a/jscomp/test/test_while_closure.js +++ b/jscomp/test/test_while_closure.js @@ -17,11 +17,9 @@ function f(param) { let n = 0; while(n < 10) { let j = n; - Caml_array.set(arr, j, (function(j){ - return function (param) { + Caml_array.set(arr, j, (function (param) { v.contents = v.contents + j | 0; - } - }(j))); + })); n = n + 1 | 0; }; } diff --git a/lib/es6/arg.js b/lib/es6/arg.js index ac450a1bc3..090c82baee 100644 --- a/lib/es6/arg.js +++ b/lib/es6/arg.js @@ -276,8 +276,7 @@ function parse_and_expand_argv_dynamic_aux(allow_expand, current, argv, speclist } } let follow = match[1]; - let no_arg = (function(s,follow){ - return function no_arg(param) { + let no_arg = function (param) { if (follow === undefined) { return; } @@ -291,10 +290,8 @@ function parse_and_expand_argv_dynamic_aux(allow_expand, current, argv, speclist }, Error: new Error() }; - } - }(s,follow)); - let get_arg = (function(s,follow){ - return function get_arg(param) { + }; + let get_arg = function (param) { if (follow !== undefined) { return follow; } @@ -309,20 +306,16 @@ function parse_and_expand_argv_dynamic_aux(allow_expand, current, argv, speclist }, Error: new Error() }; - } - }(s,follow)); - let consume_arg = (function(follow){ - return function consume_arg(param) { + }; + let consume_arg = function (param) { if (follow !== undefined) { return; } else { current.contents = current.contents + 1 | 0; return; } - } - }(follow)); - let treat_action = (function(s){ - return function treat_action(f) { + }; + let treat_action = function (f) { switch (f.TAG) { case "Unit" : return Curry._1(f._0, undefined); @@ -483,8 +476,7 @@ function parse_and_expand_argv_dynamic_aux(allow_expand, current, argv, speclist return; } - } - }(s)); + }; treat_action(match[0]); } else { Curry._1(anonfun, s); diff --git a/lib/es6/caml_format.js b/lib/es6/caml_format.js index e0022127e5..bb971de832 100644 --- a/lib/es6/caml_format.js +++ b/lib/es6/caml_format.js @@ -404,12 +404,10 @@ function parse_format(fmt) { case 46 : f.prec = 0; let j = i + 1 | 0; - while((function(j){ - return function () { + while((function () { let w = fmt.codePointAt(j) - /* '0' */48 | 0; return w >= 0 && w <= 9; - } - }(j))()) { + })()) { f.prec = (Math.imul(f.prec, 10) + fmt.codePointAt(j) | 0) - /* '0' */48 | 0; j = j + 1 | 0; }; @@ -445,12 +443,10 @@ function parse_format(fmt) { case 3 : f.width = 0; let j$1 = i; - while((function(j$1){ - return function () { + while((function () { let w = fmt.codePointAt(j$1) - /* '0' */48 | 0; return w >= 0 && w <= 9; - } - }(j$1))()) { + })()) { f.width = (Math.imul(f.width, 10) + fmt.codePointAt(j$1) | 0) - /* '0' */48 | 0; j$1 = j$1 + 1 | 0; }; diff --git a/lib/es6/curry.js b/lib/es6/curry.js index c1ce91c791..5a384d8740 100644 --- a/lib/es6/curry.js +++ b/lib/es6/curry.js @@ -14,11 +14,9 @@ function app(_f, _args) { return f.apply(null, args); } if (d >= 0) { - return (function(f,args){ return function (x) { return app(f, args.concat([x])); - } - }(f,args)); + }; } _args = Caml_array.sub(args, arity, -d | 0); _f = f.apply(null, Caml_array.sub(args, 0, arity)); diff --git a/lib/js/arg.js b/lib/js/arg.js index 9b76941676..daa48ce130 100644 --- a/lib/js/arg.js +++ b/lib/js/arg.js @@ -276,8 +276,7 @@ function parse_and_expand_argv_dynamic_aux(allow_expand, current, argv, speclist } } let follow = match[1]; - let no_arg = (function(s,follow){ - return function no_arg(param) { + let no_arg = function (param) { if (follow === undefined) { return; } @@ -291,10 +290,8 @@ function parse_and_expand_argv_dynamic_aux(allow_expand, current, argv, speclist }, Error: new Error() }; - } - }(s,follow)); - let get_arg = (function(s,follow){ - return function get_arg(param) { + }; + let get_arg = function (param) { if (follow !== undefined) { return follow; } @@ -309,20 +306,16 @@ function parse_and_expand_argv_dynamic_aux(allow_expand, current, argv, speclist }, Error: new Error() }; - } - }(s,follow)); - let consume_arg = (function(follow){ - return function consume_arg(param) { + }; + let consume_arg = function (param) { if (follow !== undefined) { return; } else { current.contents = current.contents + 1 | 0; return; } - } - }(follow)); - let treat_action = (function(s){ - return function treat_action(f) { + }; + let treat_action = function (f) { switch (f.TAG) { case "Unit" : return Curry._1(f._0, undefined); @@ -483,8 +476,7 @@ function parse_and_expand_argv_dynamic_aux(allow_expand, current, argv, speclist return; } - } - }(s)); + }; treat_action(match[0]); } else { Curry._1(anonfun, s); diff --git a/lib/js/caml_format.js b/lib/js/caml_format.js index 6cd47ba67f..2cabc1f1c0 100644 --- a/lib/js/caml_format.js +++ b/lib/js/caml_format.js @@ -404,12 +404,10 @@ function parse_format(fmt) { case 46 : f.prec = 0; let j = i + 1 | 0; - while((function(j){ - return function () { + while((function () { let w = fmt.codePointAt(j) - /* '0' */48 | 0; return w >= 0 && w <= 9; - } - }(j))()) { + })()) { f.prec = (Math.imul(f.prec, 10) + fmt.codePointAt(j) | 0) - /* '0' */48 | 0; j = j + 1 | 0; }; @@ -445,12 +443,10 @@ function parse_format(fmt) { case 3 : f.width = 0; let j$1 = i; - while((function(j$1){ - return function () { + while((function () { let w = fmt.codePointAt(j$1) - /* '0' */48 | 0; return w >= 0 && w <= 9; - } - }(j$1))()) { + })()) { f.width = (Math.imul(f.width, 10) + fmt.codePointAt(j$1) | 0) - /* '0' */48 | 0; j$1 = j$1 + 1 | 0; }; diff --git a/lib/js/curry.js b/lib/js/curry.js index 9fd82ae358..cc5354bb5d 100644 --- a/lib/js/curry.js +++ b/lib/js/curry.js @@ -14,11 +14,9 @@ function app(_f, _args) { return f.apply(null, args); } if (d >= 0) { - return (function(f,args){ return function (x) { return app(f, args.concat([x])); - } - }(f,args)); + }; } _args = Caml_array.sub(args, arity, -d | 0); _f = f.apply(null, Caml_array.sub(args, 0, arity));