From 015f850b12ee646da9a25c9b05a34a93f3d2cf09 Mon Sep 17 00:00:00 2001 From: Hugo Heuzard Date: Thu, 9 Nov 2023 06:26:15 +0100 Subject: [PATCH 1/3] Tests: exhibit gh#1520 --- compiler/tests-compiler/minify.ml | 62 +++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/compiler/tests-compiler/minify.ml b/compiler/tests-compiler/minify.ml index 1fd6f4d9a3..536f0b4dc2 100644 --- a/compiler/tests-compiler/minify.ml +++ b/compiler/tests-compiler/minify.ml @@ -378,3 +378,65 @@ function f () { 2: f(){const 3: a=2;return function(){var 4: a=a+2;return a}} |}]) + +let%expect_test _ = + with_temp_dir ~f:(fun () -> + let js_prog = + {| +function test() { + var x = {a:1,b:2} + function f (a, b = x.b) { + return (a + b); + } + console.log(f(1)); +} +test() +|} + in + let js_file = + js_prog |> Filetype.js_text_of_string |> Filetype.write_js ~name:"test.js" + in + let js_min_file = + js_file |> jsoo_minify ~flags:[ "--enable"; "shortvar" ] ~pretty:false + in + print_file (Filetype.path_of_js_file js_file); + print_file (Filetype.path_of_js_file js_min_file); + [%expect + {| + $ cat "test.js" + 1: + 2: function test() { + 3: var x = {a:1,b:2} + 4: function f (a, b = x.b) { + 5: return (a + b); + 6: } + 7: console.log(f(1)); + 8: } + 9: test() + $ cat "test.min.js" + 1: function + 2: test(){var + 3: b={a:1,b:2};function + 4: a(a,b=b.b){return a+b}console.log(a(1))}test(); |}]; + print_endline (run_javascript js_min_file); + [%expect + {| + /tmp/build_f4b6d0_dune/jsoo-test9c182b/test.min.js:4 + a(a,b=b.b){return a+b}console.log(a(1))}test(); + ^ + + ReferenceError: Cannot access 'b' before initialization + at a (/tmp/build_f4b6d0_dune/jsoo-test9c182b/test.min.js:4:7) + at test (/tmp/build_f4b6d0_dune/jsoo-test9c182b/test.min.js:4:35) + at Object. (/tmp/build_f4b6d0_dune/jsoo-test9c182b/test.min.js:4:41) + at Module._compile (node:internal/modules/cjs/loader:1267:14) + at Module._extensions..js (node:internal/modules/cjs/loader:1321:10) + at Module.load (node:internal/modules/cjs/loader:1125:32) + at Module._load (node:internal/modules/cjs/loader:965:12) + at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12) + at node:internal/main/run_main_module:23:47 + + Node.js v20.0.0 + + process exited with error code 1 + node test.min.js |}]) From 3a3ed0157ee0c69b5d7b307a180e0798283f7358 Mon Sep 17 00:00:00 2001 From: Hugo Heuzard Date: Thu, 9 Nov 2023 06:41:29 +0100 Subject: [PATCH 2/3] Compiler: fix freevar wrt parameter default value --- compiler/lib/js_traverse.ml | 3 +++ compiler/tests-compiler/minify.ml | 26 +++----------------------- 2 files changed, 6 insertions(+), 23 deletions(-) diff --git a/compiler/lib/js_traverse.ml b/compiler/lib/js_traverse.ml index 7ee45bf611..8553a4f581 100644 --- a/compiler/lib/js_traverse.ml +++ b/compiler/lib/js_traverse.ml @@ -813,6 +813,7 @@ class free = let ids = bound_idents_of_params params in List.iter ids ~f:tbody#def_var; let body = tbody#function_body body in + let params = tbody#formal_parameter_list params in tbody#record_block (Params params); m#merge_info tbody; k, params, body, nid @@ -827,6 +828,7 @@ class free = let ids = bound_idents_of_params params in List.iter ids ~f:tbody#def_var; let body = tbody#function_body body in + let params = tbody#formal_parameter_list params in let ident = match ident with | Some i -> @@ -866,6 +868,7 @@ class free = let ids = bound_idents_of_params params in List.iter ids ~f:tbody#def_var; let body = tbody#function_body body in + let params = tbody#formal_parameter_list params in tbody#record_block (Params params); m#def_var id; m#merge_info tbody; diff --git a/compiler/tests-compiler/minify.ml b/compiler/tests-compiler/minify.ml index 536f0b4dc2..ada53d028f 100644 --- a/compiler/tests-compiler/minify.ml +++ b/compiler/tests-compiler/minify.ml @@ -416,27 +416,7 @@ test() $ cat "test.min.js" 1: function 2: test(){var - 3: b={a:1,b:2};function - 4: a(a,b=b.b){return a+b}console.log(a(1))}test(); |}]; + 3: c={a:1,b:2};function + 4: a(a,b=c.b){return a+b}console.log(a(1))}test(); |}]; print_endline (run_javascript js_min_file); - [%expect - {| - /tmp/build_f4b6d0_dune/jsoo-test9c182b/test.min.js:4 - a(a,b=b.b){return a+b}console.log(a(1))}test(); - ^ - - ReferenceError: Cannot access 'b' before initialization - at a (/tmp/build_f4b6d0_dune/jsoo-test9c182b/test.min.js:4:7) - at test (/tmp/build_f4b6d0_dune/jsoo-test9c182b/test.min.js:4:35) - at Object. (/tmp/build_f4b6d0_dune/jsoo-test9c182b/test.min.js:4:41) - at Module._compile (node:internal/modules/cjs/loader:1267:14) - at Module._extensions..js (node:internal/modules/cjs/loader:1321:10) - at Module.load (node:internal/modules/cjs/loader:1125:32) - at Module._load (node:internal/modules/cjs/loader:965:12) - at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12) - at node:internal/main/run_main_module:23:47 - - Node.js v20.0.0 - - process exited with error code 1 - node test.min.js |}]) + [%expect {| 3 |}]) From 73cbbd126744b0d470326eba60a8801d517c5f35 Mon Sep 17 00:00:00 2001 From: Hugo Heuzard Date: Thu, 9 Nov 2023 06:46:50 +0100 Subject: [PATCH 3/3] Changes --- CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.md b/CHANGES.md index 2ba018e26e..5570328a49 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -15,6 +15,7 @@ * Runtime: fix Unix.lstat, Unix.LargeFile.lstat (#1519) * Compiler: fix global flow analysis (#1494) * Compiler: fix js parser/printer wrt async functions (#1515) +* Compiler: fix free variables pass wrt parameters' default value (#1521) # 5.4.0 (2023-07-06) - Lille