From b0fc6108c923a198325354ae36b71f90c4c68e19 Mon Sep 17 00:00:00 2001 From: Michael Ennen Date: Thu, 5 Jan 2017 17:13:23 -0700 Subject: vim-patch:7.4.1727 Problem: Cannot detect a crash in tests when caused by garbagecollect(). Solution: Add garbagecollect_for_testing(). Do not free a job if is still useful. https://github.com/vim/vim/commit/ebf7dfa6f121c82f97d2adca3d45fbaba9ad8f7e --- src/nvim/testdir/test_lambda.vim | 286 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 286 insertions(+) create mode 100644 src/nvim/testdir/test_lambda.vim (limited to 'src/nvim/testdir/test_lambda.vim') diff --git a/src/nvim/testdir/test_lambda.vim b/src/nvim/testdir/test_lambda.vim new file mode 100644 index 0000000000..d51b6f7c5a --- /dev/null +++ b/src/nvim/testdir/test_lambda.vim @@ -0,0 +1,286 @@ +" Test for lambda and closure + +function! Test_lambda_feature() + call assert_equal(1, has('lambda')) +endfunction + +function! Test_lambda_with_filter() + let s:x = 2 + call assert_equal([2, 3], filter([1, 2, 3], {i, v -> v >= s:x})) +endfunction + +function! Test_lambda_with_map() + let s:x = 1 + call assert_equal([2, 3, 4], map([1, 2, 3], {i, v -> v + s:x})) +endfunction + +function! Test_lambda_with_sort() + call assert_equal([1, 2, 3, 4, 7], sort([3,7,2,1,4], {a, b -> a - b})) +endfunction + +" function! Test_lambda_with_timer() +" if !has('timers') +" return +" endif + +" let s:n = 0 +" let s:timer_id = 0 +" function! s:Foo() +" "let n = 0 +" let s:timer_id = timer_start(50, {-> execute("let s:n += 1 | echo s:n", "")}, {"repeat": -1}) +" endfunction + +" call s:Foo() +" sleep 200ms +" " do not collect lambda +" call garbagecollect() +" let m = s:n +" sleep 200ms +" call timer_stop(s:timer_id) +" call assert_true(m > 1) +" call assert_true(s:n > m + 1) +" call assert_true(s:n < 9) +" endfunction + +function! Test_lambda_with_partial() + let l:Cb = function({... -> ['zero', a:1, a:2, a:3]}, ['one', 'two']) + call assert_equal(['zero', 'one', 'two', 'three'], l:Cb('three')) +endfunction + +function Test_lambda_fails() + call assert_equal(3, {a, b -> a + b}(1, 2)) + call assert_fails('echo {a, a -> a + a}(1, 2)', 'E15:') + call assert_fails('echo {a, b -> a + b)}(1, 2)', 'E15:') +endfunc + +func Test_not_lambda() + let x = {'>' : 'foo'} + call assert_equal('foo', x['>']) +endfunc + +function! Test_lambda_capture_by_reference() + let v = 1 + let l:F = {x -> x + v} + let v = 2 + call assert_equal(12, l:F(10)) +endfunction + +function! Test_lambda_side_effect() + function! s:update_and_return(arr) + let a:arr[1] = 5 + return a:arr + endfunction + + function! s:foo(arr) + return {-> s:update_and_return(a:arr)} + endfunction + + let arr = [3,2,1] + call assert_equal([3, 5, 1], s:foo(arr)()) +endfunction + +function! Test_lambda_refer_local_variable_from_other_scope() + function! s:foo(X) + return a:X() " refer l:x in s:bar() + endfunction + + function! s:bar() + let x = 123 + return s:foo({-> x}) + endfunction + + call assert_equal(123, s:bar()) +endfunction + +function! Test_lambda_do_not_share_local_variable() + function! s:define_funcs() + let l:One = {-> split(execute("let a = 'abc' | echo a"))[0]} + let l:Two = {-> exists("a") ? a : "no"} + return [l:One, l:Two] + endfunction + + let l:F = s:define_funcs() + + call assert_equal('no', l:F[1]()) + call assert_equal('abc', l:F[0]()) + call assert_equal('no', l:F[1]()) +endfunction + +function! Test_lambda_closure_counter() + function! s:foo() + let x = 0 + return {-> [execute("let x += 1"), x][-1]} + endfunction + + let l:F = s:foo() + call garbagecollect() + call assert_equal(1, l:F()) + call assert_equal(2, l:F()) + call assert_equal(3, l:F()) + call assert_equal(4, l:F()) +endfunction + +function! Test_lambda_with_a_var() + function! s:foo() + let x = 2 + return {... -> a:000 + [x]} + endfunction + function! s:bar() + return s:foo()(1) + endfunction + + call assert_equal([1, 2], s:bar()) +endfunction + +function! Test_lambda_call_lambda_from_lambda() + function! s:foo(x) + let l:F1 = {-> {-> a:x}} + return {-> l:F1()} + endfunction + + let l:F = s:foo(1) + call assert_equal(1, l:F()()) +endfunction + +function! Test_lambda_delfunc() + function! s:gen() + let pl = l: + let l:Foo = {-> get(pl, "Foo", get(pl, "Bar", {-> 0}))} + let l:Bar = l:Foo + delfunction l:Foo + return l:Bar + endfunction + + let l:F = s:gen() + call assert_fails(':call l:F()', 'E933:') +endfunction + +function! Test_lambda_scope() + function! s:NewCounter() + let c = 0 + return {-> [execute('let c += 1'), c][-1]} + endfunction + + function! s:NewCounter2() + return {-> [execute('let c += 100'), c][-1]} + endfunction + + let l:C = s:NewCounter() + let l:D = s:NewCounter2() + + call assert_equal(1, l:C()) + call assert_fails(':call l:D()', 'E15:') " E121: then E15: + call assert_equal(2, l:C()) +endfunction + +function! Test_lambda_share_scope() + function! s:New() + let c = 0 + let l:Inc0 = {-> [execute('let c += 1'), c][-1]} + let l:Dec0 = {-> [execute('let c -= 1'), c][-1]} + return [l:Inc0, l:Dec0] + endfunction + + let [l:Inc, l:Dec] = s:New() + + call assert_equal(1, l:Inc()) + call assert_equal(2, l:Inc()) + call assert_equal(1, l:Dec()) +endfunction + +function! Test_lambda_circular_reference() + function! s:Foo() + let d = {} + let d.f = {-> d} + return d.f + endfunction + + call s:Foo() + call garbagecollect() + let i = 0 | while i < 10000 | call s:Foo() | let i+= 1 | endwhile + call garbagecollect() +endfunction + +function! Test_lambda_combination() + call assert_equal(2, {x -> {x -> x}}(1)(2)) + call assert_equal(10, {y -> {x -> x(y)(10)}({y -> y})}({z -> z})) + call assert_equal(5.0, {x -> {y -> x / y}}(10)(2.0)) + call assert_equal(6, {x -> {y -> {z -> x + y + z}}}(1)(2)(3)) + + call assert_equal(6, {x -> {f -> f(x)}}(3)({x -> x * 2})) + call assert_equal(6, {f -> {x -> f(x)}}({x -> x * 2})(3)) + + " Z combinator + let Z = {f -> {x -> f({y -> x(x)(y)})}({x -> f({y -> x(x)(y)})})} + let Fact = {f -> {x -> x == 0 ? 1 : x * f(x - 1)}} + call assert_equal(120, Z(Fact)(5)) +endfunction + +function! Test_closure_counter() + function! s:foo() + let x = 0 + function! s:bar() closure + let x += 1 + return x + endfunction + return function('s:bar') + endfunction + + let l:F = s:foo() + call garbagecollect() + call assert_equal(1, l:F()) + call assert_equal(2, l:F()) + call assert_equal(3, l:F()) + call assert_equal(4, l:F()) +endfunction + +function! Test_closure_unlet() + function! s:foo() + let x = 1 + function! s:bar() closure + unlet x + endfunction + call s:bar() + return l: + endfunction + + call assert_false(has_key(s:foo(), 'x')) + call garbagecollect() +endfunction + +function! LambdaFoo() + let x = 0 + function! LambdaBar() closure + let x += 1 + return x + endfunction + return function('LambdaBar') +endfunction + +func Test_closure_refcount() + let g:Count = LambdaFoo() + call test_garbagecollect_now() + call assert_equal(1, g:Count()) + let g:Count2 = LambdaFoo() + call test_garbagecollect_now() + call assert_equal(1, g:Count2()) + call assert_equal(2, g:Count()) + call assert_equal(3, g:Count2()) + + delfunc LambdaFoo + delfunc LambdaBar +endfunc + +func Test_named_function_closure() + func! Afoo() + let x = 14 + func! s:Abar() closure + return x + endfunc + call assert_equal(14, s:Abar()) + endfunc + call Afoo() + call assert_equal(14, s:Abar()) + call garbagecollect() + call assert_equal(14, s:Abar()) +endfunc -- cgit From bb7d0deb2f93bd7980a51dca05ae222e751ab632 Mon Sep 17 00:00:00 2001 From: Michael Ennen Date: Wed, 14 Dec 2016 17:06:36 -0700 Subject: vim-patch:7.4.2044 Problem: filter() and map() either require a string or defining a function. Solution: Support lambda, a short way to define a function that evaluates an expression. (Yasuhiro Matsumoto, Ken Takata) https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544 --- src/nvim/testdir/test_lambda.vim | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/nvim/testdir/test_lambda.vim') diff --git a/src/nvim/testdir/test_lambda.vim b/src/nvim/testdir/test_lambda.vim index d51b6f7c5a..43483841ac 100644 --- a/src/nvim/testdir/test_lambda.vim +++ b/src/nvim/testdir/test_lambda.vim @@ -284,3 +284,5 @@ func Test_named_function_closure() call garbagecollect() call assert_equal(14, s:Abar()) endfunc +======= +>>>>>>> 381a8796... vim-patch:7.4.2044 -- cgit From a0ce66371096db21f341f061df8c1b7b4d9e38fe Mon Sep 17 00:00:00 2001 From: Michael Ennen Date: Wed, 14 Dec 2016 17:33:20 -0700 Subject: vim-patch:7.4.2076 Problem: Syntax error when dict has '>' key. Solution: Check for endchar. (Ken Takata) https://github.com/vim/vim/commit/4f0383bc3fe5af0229fb66b53fe94329af783eff --- src/nvim/testdir/test_lambda.vim | 2 -- 1 file changed, 2 deletions(-) (limited to 'src/nvim/testdir/test_lambda.vim') diff --git a/src/nvim/testdir/test_lambda.vim b/src/nvim/testdir/test_lambda.vim index 43483841ac..d51b6f7c5a 100644 --- a/src/nvim/testdir/test_lambda.vim +++ b/src/nvim/testdir/test_lambda.vim @@ -284,5 +284,3 @@ func Test_named_function_closure() call garbagecollect() call assert_equal(14, s:Abar()) endfunc -======= ->>>>>>> 381a8796... vim-patch:7.4.2044 -- cgit From 9f6f7fe26d7caa89083f5d5b00c55bf2046591ca Mon Sep 17 00:00:00 2001 From: Michael Ennen Date: Sat, 17 Dec 2016 13:07:37 -0700 Subject: vim-patch:7.4.2119 Problem: Closures are not supported. Solution: Capture variables in lambdas from the outer scope. (Yasuhiro Matsumoto, Ken Takata) https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f --- src/nvim/testdir/test_lambda.vim | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/nvim/testdir/test_lambda.vim') diff --git a/src/nvim/testdir/test_lambda.vim b/src/nvim/testdir/test_lambda.vim index d51b6f7c5a..8e572bea80 100644 --- a/src/nvim/testdir/test_lambda.vim +++ b/src/nvim/testdir/test_lambda.vim @@ -284,3 +284,5 @@ func Test_named_function_closure() call garbagecollect() call assert_equal(14, s:Abar()) endfunc +======= +>>>>>>> 42b34811... vim-patch:7.4.2119 -- cgit From f59321e319ecfc6977f666fb04a52ac50bead09d Mon Sep 17 00:00:00 2001 From: Michael Ennen Date: Sat, 17 Dec 2016 16:01:42 -0700 Subject: vim-patch:7.4.2120 Problem: User defined functions can't be a closure. Solution: Add the "closure" argument. Allow using :unlet on a bound variable. (Yasuhiro Matsumoto, Ken Takata) https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740 --- src/nvim/testdir/test_lambda.vim | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/nvim/testdir/test_lambda.vim') diff --git a/src/nvim/testdir/test_lambda.vim b/src/nvim/testdir/test_lambda.vim index 8e572bea80..98d8ffab40 100644 --- a/src/nvim/testdir/test_lambda.vim +++ b/src/nvim/testdir/test_lambda.vim @@ -286,3 +286,5 @@ func Test_named_function_closure() endfunc ======= >>>>>>> 42b34811... vim-patch:7.4.2119 +======= +>>>>>>> 789a3319... vim-patch:7.4.2120 -- cgit From 1f715ac1c1a1eee43360be911636020ed855e12c Mon Sep 17 00:00:00 2001 From: Michael Ennen Date: Sat, 17 Dec 2016 22:33:03 -0700 Subject: vim-patch:7.4.2136 Problem: Closure function fails. Solution: Don't reset uf_scoped when it points to another funccal. https://github.com/vim/vim/commit/580164481924ed8611eb79f0247a0eb1ca0b3b9a --- src/nvim/testdir/test_lambda.vim | 4 ---- 1 file changed, 4 deletions(-) (limited to 'src/nvim/testdir/test_lambda.vim') diff --git a/src/nvim/testdir/test_lambda.vim b/src/nvim/testdir/test_lambda.vim index 98d8ffab40..d51b6f7c5a 100644 --- a/src/nvim/testdir/test_lambda.vim +++ b/src/nvim/testdir/test_lambda.vim @@ -284,7 +284,3 @@ func Test_named_function_closure() call garbagecollect() call assert_equal(14, s:Abar()) endfunc -======= ->>>>>>> 42b34811... vim-patch:7.4.2119 -======= ->>>>>>> 789a3319... vim-patch:7.4.2120 -- cgit From 53fad45115d3ee438dfb537d99ccf3b021ebc6b7 Mon Sep 17 00:00:00 2001 From: Michael Ennen Date: Fri, 16 Dec 2016 14:51:49 -0700 Subject: vim-patch:7.4.2137 Problem: Using function() with a name will find another function when it is redefined. Solution: Add funcref(). Refer to lambda using a partial. Fix several reference counting issues. https://github.com/vim/vim/commit/437bafe4c8a83ed71ee006eda7f54b65a90f0d4c --- src/nvim/testdir/test_lambda.vim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/nvim/testdir/test_lambda.vim') diff --git a/src/nvim/testdir/test_lambda.vim b/src/nvim/testdir/test_lambda.vim index d51b6f7c5a..00665810bd 100644 --- a/src/nvim/testdir/test_lambda.vim +++ b/src/nvim/testdir/test_lambda.vim @@ -259,10 +259,10 @@ endfunction func Test_closure_refcount() let g:Count = LambdaFoo() - call test_garbagecollect_now() + call garbagecollect() call assert_equal(1, g:Count()) let g:Count2 = LambdaFoo() - call test_garbagecollect_now() + call garbagecollect() call assert_equal(1, g:Count2()) call assert_equal(2, g:Count()) call assert_equal(3, g:Count2()) -- cgit From ef8701610baa18ecf2568990eab4ecf02ca8f6c1 Mon Sep 17 00:00:00 2001 From: Michael Ennen Date: Mon, 19 Dec 2016 20:09:07 -0700 Subject: Allow lambdas to be used with jobs, timers and dictwatchers. --- src/nvim/testdir/test_lambda.vim | 51 ++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 25 deletions(-) (limited to 'src/nvim/testdir/test_lambda.vim') diff --git a/src/nvim/testdir/test_lambda.vim b/src/nvim/testdir/test_lambda.vim index 00665810bd..311cc6e2cb 100644 --- a/src/nvim/testdir/test_lambda.vim +++ b/src/nvim/testdir/test_lambda.vim @@ -18,29 +18,29 @@ function! Test_lambda_with_sort() call assert_equal([1, 2, 3, 4, 7], sort([3,7,2,1,4], {a, b -> a - b})) endfunction -" function! Test_lambda_with_timer() -" if !has('timers') -" return -" endif - -" let s:n = 0 -" let s:timer_id = 0 -" function! s:Foo() -" "let n = 0 -" let s:timer_id = timer_start(50, {-> execute("let s:n += 1 | echo s:n", "")}, {"repeat": -1}) -" endfunction - -" call s:Foo() -" sleep 200ms -" " do not collect lambda -" call garbagecollect() -" let m = s:n -" sleep 200ms -" call timer_stop(s:timer_id) -" call assert_true(m > 1) -" call assert_true(s:n > m + 1) -" call assert_true(s:n < 9) -" endfunction +function! Test_lambda_with_timer() + if !has('timers') + return + endif + + let s:n = 0 + let s:timer_id = 0 + function! s:Foo() + "let n = 0 + let s:timer_id = timer_start(50, {-> execute("let s:n += 1 | echo s:n", "")}, {"repeat": -1}) + endfunction + + call s:Foo() + sleep 200ms + " do not collect lambda + call garbagecollect() + let m = s:n + sleep 200ms + call timer_stop(s:timer_id) + call assert_true(m > 1) + call assert_true(s:n > m + 1) + call assert_true(s:n < 9) +endfunction function! Test_lambda_with_partial() let l:Cb = function({... -> ['zero', a:1, a:2, a:3]}, ['one', 'two']) @@ -259,10 +259,10 @@ endfunction func Test_closure_refcount() let g:Count = LambdaFoo() - call garbagecollect() + call test_garbagecollect_now() call assert_equal(1, g:Count()) let g:Count2 = LambdaFoo() - call garbagecollect() + call test_garbagecollect_now() call assert_equal(1, g:Count2()) call assert_equal(2, g:Count()) call assert_equal(3, g:Count2()) @@ -271,6 +271,7 @@ func Test_closure_refcount() delfunc LambdaBar endfunc +" This test is causing a use-after-free on shutdown. func Test_named_function_closure() func! Afoo() let x = 14 -- cgit