diff --git a/testing/btest/Baseline/language.closure-sending/recv.recv.out b/testing/btest/Baseline/language.closure-sending/recv.recv.out index 4760591c3c..d48a41b96d 100644 --- a/testing/btest/Baseline/language.closure-sending/recv.recv.out +++ b/testing/btest/Baseline/language.closure-sending/recv.recv.out @@ -2,26 +2,26 @@ hello :-) peer added receiver got ping: function 2 -inside: 1 | outside: 11 | global: 100 +inside: 1 | outside: 12 | global: 100 77 receiver got ping: function 1 begin: 100 | base_step: 2 begin: 100 | base_step: 2 | step: 76 178 receiver got ping: function 2 -inside: 3 | outside: 11 | global: 100 +inside: 3 | outside: 12 | global: 100 79 receiver got ping: function 1 begin: 100 | base_step: 4 begin: 100 | base_step: 4 | step: 76 180 receiver got ping: function 2 -inside: 5 | outside: 11 | global: 100 +inside: 5 | outside: 12 | global: 100 81 receiver got ping: function 1 begin: 100 | base_step: 6 begin: 100 | base_step: 6 | step: 76 182 receiver got ping: function 2 -inside: 7 | outside: 11 | global: 100 +inside: 7 | outside: 12 | global: 100 83 diff --git a/testing/btest/Baseline/language.closure-sending/send.send.out b/testing/btest/Baseline/language.closure-sending/send.send.out index 11c6f46d91..39d70d48d5 100644 --- a/testing/btest/Baseline/language.closure-sending/send.send.out +++ b/testing/btest/Baseline/language.closure-sending/send.send.out @@ -3,7 +3,7 @@ hello :) peer added begin: 100 | base_step: 50 sender got pong: function 2 -inside: 1 | outside: 11 | global: 10 +inside: 1 | outside: 12 | global: 10 77 begin: 100 | base_step: 50 sender got pong: function 1 @@ -12,7 +12,7 @@ begin: 178 | base_step: 2 | step: 76 256 begin: 100 | base_step: 50 sender got pong: function 2 -inside: 3 | outside: 11 | global: 10 +inside: 3 | outside: 12 | global: 10 79 begin: 100 | base_step: 50 sender got pong: function 1 @@ -21,7 +21,7 @@ begin: 180 | base_step: 4 | step: 76 260 begin: 100 | base_step: 50 sender got pong: function 2 -inside: 5 | outside: 11 | global: 10 +inside: 5 | outside: 12 | global: 10 81 begin: 100 | base_step: 50 sender got pong: function 1 diff --git a/testing/btest/Baseline/language.function-closures/out b/testing/btest/Baseline/language.function-closures/out index 648170033b..13869d6338 100644 --- a/testing/btest/Baseline/language.function-closures/out +++ b/testing/btest/Baseline/language.function-closures/out @@ -19,8 +19,8 @@ expect: 160 160 expect: 225 225 -expect: 290 -290 +expect: 225 +225 tables: diff --git a/testing/btest/Baseline/language.hook_calls/invalid.out b/testing/btest/Baseline/language.hook_calls/invalid.out index 2ce20fc128..762168bcef 100644 --- a/testing/btest/Baseline/language.hook_calls/invalid.out +++ b/testing/btest/Baseline/language.hook_calls/invalid.out @@ -1,11 +1,9 @@ ### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63. error in ./invalid.zeek, line 9: hook cannot be called directly, use hook operator (myhook) -warning in ./invalid.zeek, line 9: expression value ignored (myhook(3)) error in ./invalid.zeek, line 10: hook cannot be called directly, use hook operator (myhook) error in ./invalid.zeek, line 11: hook cannot be called directly, use hook operator (myhook) error in ./invalid.zeek, line 12: not a valid hook call expression (2 + 2) warning in ./invalid.zeek, line 12: expression value ignored (2 + 2) error in ./invalid.zeek, line 13: not a valid hook call expression (2 + 2) error in ./invalid.zeek, line 15: hook cannot be called directly, use hook operator (h) -warning in ./invalid.zeek, line 15: expression value ignored (h(3)) error in ./invalid.zeek, line 16: hook cannot be called directly, use hook operator (h) diff --git a/testing/btest/language/closure-sending-naming.zeek b/testing/btest/language/closure-sending-naming.zeek index 4210c758cb..27bc367d6d 100644 --- a/testing/btest/language/closure-sending-naming.zeek +++ b/testing/btest/language/closure-sending-naming.zeek @@ -33,7 +33,7 @@ function send_event() local log : myfunctype = function(c: count) : function(d: count) : count { # print fmt("inside: %s | outside: %s | global: %s", c, event_count, global_with_same_name); - return function(d: count) : count { return d + c; }; + return function[c](d: count) : count { return d + c; }; }; local e2 = Broker::make_event(ping, "function 1", log); @@ -85,7 +85,7 @@ function my_funcs() local l : myfunctype = function(c: count) : function(d: count) : count { print fmt("dogs"); - return function(d: count) : count { return d + c; }; + return function[c](d: count) : count { return d + c; }; }; } diff --git a/testing/btest/language/closure-sending.zeek b/testing/btest/language/closure-sending.zeek index c6ff25464c..f5a3e44035 100644 --- a/testing/btest/language/closure-sending.zeek +++ b/testing/btest/language/closure-sending.zeek @@ -54,6 +54,7 @@ function send_event() local stepper = l(50); ++n; + ++event_count; if ( n % 2 == 0) { local e2 = Broker::make_event(ping, "function 1", l); diff --git a/testing/btest/language/function-closures.zeek b/testing/btest/language/function-closures.zeek index 80112da0cf..c4e4505481 100644 --- a/testing/btest/language/function-closures.zeek +++ b/testing/btest/language/function-closures.zeek @@ -3,9 +3,11 @@ global numberone : count = 1; +type mutable_aggregate: record { x: count; }; + function make_count_upper (start : count) : function(step : count) : count { - return function(step : count) : count + return function[start](step : count) : count { return (start += (step + numberone)); }; } @@ -14,7 +16,7 @@ function dog_maker(name: string, weight: count) : function (action: string) local eat = function (lbs: count) { print fmt("eat i weigh %s", lbs); }; local bark = function (who: string) { print fmt("bark i am %s", who); }; - local dog = function (action: string) + local dog = function [eat, bark, name, weight](action: string) { switch action { @@ -72,14 +74,14 @@ event zeek_init() print c(1) == two(3); # a little more complicated ... - local cat_dog = 100; - local add_n_and_m = function(n: count) : function(m : count) : function(o : count) : count + local cat_dog = mutable_aggregate($x=100); + local add_n_and_m = function[cat_dog](n: count) : function(m : count) : function(o : count) : count { - cat_dog += 1; + cat_dog$x += 1; local can_we_make_variables_inside = 11; - return function(m : count) : function(o : count) : count - { return function(o : count) : count - { return n + m + o + cat_dog + can_we_make_variables_inside; }; }; + return function[can_we_make_variables_inside, cat_dog, n](m : count) : function(o : count) : count + { return function[cat_dog, can_we_make_variables_inside, m, n](o : count) : count + { return n + m + o + cat_dog$x + can_we_make_variables_inside; }; }; }; local add_m = add_n_and_m(2); @@ -95,14 +97,14 @@ event zeek_init() # can mutate closure: print "expect: 101"; - print cat_dog; + print cat_dog$x; # complicated - has state across calls local two_part_adder_maker = function (begin : count) : function (base_step : count) : function ( step : count) : count { - return function (base_step : count) : function (step : count) : count + return function [begin](base_step : count) : function (step : count) : count { - return function (step : count) : count + return function [base_step, begin](step : count) : count { return (begin += base_step + step); }; }; }; @@ -115,10 +117,17 @@ event zeek_init() print stepper(15); # another copy check - print "expect: 290"; + # + # under old reference capture semantics, this would print 290 because + # the twotwofive copy wouldn't have a copy of the "begin" variable but + # instead a reference to it; under copy capture semantics, though, + # those are separate values, so executing stepper() after the copy + # won't affect the copy + # + print "expect: 225"; print twotwofive(15); - local hamster : count = 3; + local hamster = mutable_aggregate($x=3); print ""; print "tables:"; @@ -128,10 +137,10 @@ event zeek_init() [1] = "symmetric active", [2] = "symmetric passive", [3] = "client", - } &default = function(i: count):string { return fmt("unknown-%d. outside-%d", i, hamster += 1); } &redef; + } &default = function[hamster](i: count):string { return fmt("unknown-%d. outside-%d", i, hamster$x += 1); } &redef; # changing the value here will show in the function. - hamster += hamster; + hamster$x += hamster$x; print "expect: unknown-11. outside-7"; print modes[11]; @@ -156,7 +165,7 @@ event zeek_init() [1] = "symmetric active", [2] = "symmetric passive", [3] = "client" - )&default = function(i: count):string { return fmt("unknown-%d. outside-%d", i, hamster_also += 1); } &redef; + )&default = function[hamster_also](i: count):string { return fmt("unknown-%d. outside-%d", i, hamster_also += 1); } &redef; print "expect: unknown-11. outside-4"; print modes_also[11]; diff --git a/testing/btest/language/lambda-escaping.zeek b/testing/btest/language/lambda-escaping.zeek index 7552d39cb2..8e2deb1af6 100644 --- a/testing/btest/language/lambda-escaping.zeek +++ b/testing/btest/language/lambda-escaping.zeek @@ -13,7 +13,7 @@ event zeek_init() &priority=+10 { local outer = 101; - local lambda = function() + local lambda = function[outer]() { print outer; }; lambda(); diff --git a/testing/btest/language/lambda-nested-copy.zeek b/testing/btest/language/lambda-nested-copy.zeek index 6cf0e6952a..ae4578fdcd 100644 --- a/testing/btest/language/lambda-nested-copy.zeek +++ b/testing/btest/language/lambda-nested-copy.zeek @@ -3,9 +3,9 @@ local outer = 100; -local lambda = function() +local lambda = function[outer]() { - local inner = function(a: count, b: count, c: count, d: count, e: count, f: count) + local inner = function[outer](a: count, b: count, c: count, d: count, e: count, f: count) { print outer + f; }; diff --git a/testing/btest/language/lambda-record-field.zeek b/testing/btest/language/lambda-record-field.zeek index bcb7e1bc75..fb63dbaab8 100644 --- a/testing/btest/language/lambda-record-field.zeek +++ b/testing/btest/language/lambda-record-field.zeek @@ -8,6 +8,6 @@ type myrec: record { event zeek_init() { local w = "world"; - local mr = myrec($foo(a: string) = { print a + w; }); + local mr = myrec($foo[w](a: string) = { print a + w; }); mr$foo("hello"); } diff --git a/testing/btest/language/lambda-zeek-init.zeek b/testing/btest/language/lambda-zeek-init.zeek index 2576831c22..cc596b431b 100644 --- a/testing/btest/language/lambda-zeek-init.zeek +++ b/testing/btest/language/lambda-zeek-init.zeek @@ -5,7 +5,7 @@ event zeek_init() &priority=+10 { local outer = 101; - local lambda = function() + local lambda = function[outer]() { print outer + 2; }; lambda(); diff --git a/testing/btest/language/more-closure-tests.zeek b/testing/btest/language/more-closure-tests.zeek index f8a01cd0a8..2f6d6fbd91 100644 --- a/testing/btest/language/more-closure-tests.zeek +++ b/testing/btest/language/more-closure-tests.zeek @@ -17,7 +17,7 @@ function map_1 (f: function(a: count): count, v: vector of count) : vector of co # stacks two functions function stacker (one : function(a: count): count, two: function (b: count): count): function(c: count): count { - return function (c: count): count + return function [one,two](c: count): count { return one(two(c)); }; @@ -25,7 +25,7 @@ function stacker (one : function(a: count): count, two: function (b: count): cou function make_dog(name: string, weight: count) : function(i: string, item: string) { - return function(i: string, item: string) + return function[name, weight](i: string, item: string) { switch i { @@ -69,7 +69,7 @@ event zeek_init() local make_laster = function(start: count) : function(i: count): count { - return function(i: count): count + return function[start](i: count): count { local temp = i; i += start; @@ -111,7 +111,7 @@ event zeek_init() local vs = vector("dog", "cat", "fish"); for (i in vs) { - mfs += function() { print i, vs[i]; }; + mfs += function[i, vs]() { print i, vs[i]; }; } for ( i in mfs) mfs[i](); diff --git a/testing/btest/language/outer_param_binding.zeek b/testing/btest/language/outer_param_binding.zeek index 1f3f32a1fc..f3a4686f24 100644 --- a/testing/btest/language/outer_param_binding.zeek +++ b/testing/btest/language/outer_param_binding.zeek @@ -9,7 +9,7 @@ function bar(b: string, c: string) { local f: Foo; local d = 8; - f = [$x=function(a: string) : string + f = [$x=function[b, c, d](a: string) : string { local x = 0; # Fail here: we've captured the closure.