Merge remote-tracking branch 'origin/topic/vern/when-lambda'

* origin/topic/vern/when-lambda:
  explicitly provide the frame for evaluating a "when" timeout expression
  attempt to make "when" btest deterministic
  tests for new "when" semantics/errors
  update existing test suite usage of "when" statements to include captures
  update uses of "when" in base scripts to include captures
  captures for "when" statements update Triggers to IntrusivePtr's and simpler AST traversal introduce IDSet type, migrate associated "ID*" types to "const ID*"
  logic (other than in profiling) for assignments that yield separate values
  option for internal use to mark a function type as allowing non-expression returns
  removed some now-obsolete profiling functionality
  minor commenting clarifications
This commit is contained in:
Tim Wojtulewicz 2022-01-14 14:41:15 -07:00
commit 3d9d6e953b
56 changed files with 931 additions and 255 deletions

View file

@ -0,0 +1,5 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
[x=1, y=2], [x=11, y=12]
[x=1, y=55], [x=11, y=12]
[x=111, y=222], [x=13, y=14]
[x=99, y=222], [x=13, y=14]

View file

@ -0,0 +1,8 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
warning in <...>/when-capture-errors.zeek, lines 19-22: "when" statement referring to locals without an explicit [] capture is deprecated: orig1 (when (0 < g) { print orig1})
warning in <...>/when-capture-errors.zeek, lines 25-28: "when" statement referring to locals without an explicit [] capture is deprecated: orig3 (when (0 < g || orig3) { print g})
warning in <...>/when-capture-errors.zeek, lines 34-38: "when" statement referring to locals without an explicit [] capture is deprecated: orig1 (when (0 < g) { print g} timeout 1.0 sec { print orig1})
error in <...>/when-capture-errors.zeek, lines 66-70: orig2 is used inside "when" statement but not captured (when (0 < g) { print orig1} timeout 1.0 sec { print orig2})
error in <...>/when-capture-errors.zeek, lines 76-80: orig3 is captured but not used inside "when" statement (when (0 < g) { print orig1} timeout 1.0 sec { print orig2})
error in <...>/when-capture-errors.zeek, line 83: no such local identifier: l1
error in <...>/when-capture-errors.zeek, line 89: no such local identifier: l2

View file

@ -46,7 +46,7 @@ function check_terminate_conditions()
function check_it(name: string, s: opaque of Broker::Store)
{
when ( local r = Broker::keys(s) )
when [name, s] ( local r = Broker::keys(s) )
{
check_terminate_conditions();
print fmt("%s keys result: %s", name, r);

View file

@ -8,7 +8,7 @@
function print_keys(a: any)
{
when ( local s = Broker::keys(a) )
when [a] ( local s = Broker::keys(a) )
{
print "keys", s;
}

View file

@ -37,7 +37,7 @@ function f(do_exception: bool): bool
local cmd = Exec::Command($cmd=fmt("echo 'f(%s)'",
do_exception));
return when ( local result = Exec::run(cmd) )
return when [cmd, do_exception] ( local result = Exec::run(cmd) )
{
print result$stdout;
@ -58,7 +58,7 @@ function g(do_exception: bool): bool
{
local stall = Exec::Command($cmd="sleep 30");
return when ( local result = Exec::run(stall) )
return when [do_exception, stall] ( local result = Exec::run(stall) )
{
print "shouldn't get here, g()", do_exception, result;
}
@ -84,14 +84,14 @@ event zeek_init()
local cmd = Exec::Command($cmd="echo 'zeek_init()'");
local stall = Exec::Command($cmd="sleep 30");
when ( local result = Exec::run(cmd) )
when [cmd] ( local result = Exec::run(cmd) )
{
print result$stdout;
event termination_check();
print myrecord$notset;
}
when ( local result2 = Exec::run(stall) )
when [stall] ( local result2 = Exec::run(stall) )
{
print "shouldn't get here", result2;
check_term_condition();

View file

@ -0,0 +1,65 @@
# @TEST-EXEC: btest-bg-run zeek zeek -b %INPUT
# @TEST-EXEC: btest-bg-wait 15
# @TEST-EXEC: btest-diff zeek/.stdout
type r: record { x: int; y: int; };
global g = 0;
function async_foo1(arg: r) : r
{
return when ( g > 0 )
{
arg$x = 99;
return r($x = 11, $y = 12);
}
}
function async_foo2(arg: r) : r
{
return when [arg] ( g > 0 )
{
arg$x = 99;
return r($x = 13, $y = 14);
}
}
event zeek_init()
{
local orig1 = r($x = 1, $y = 2);
local orig2 = copy(orig1);
when ( g == 1 && local resp1 = async_foo1(orig1) )
{
++g;
print orig1, resp1;
}
when [orig2] ( g == 2 && local resp2 = async_foo1(orig2) )
{
++g;
print orig2, resp2;
}
local orig3 = r($x = 111, $y = 222);
local orig4 = copy(orig3);
when ( g == 3 && local resp4 = async_foo2(orig3) )
{
++g;
print orig3, resp4;
}
when [orig4] ( g == 4 && local resp5 = async_foo2(orig4) )
{
print orig4, resp5;
}
orig1$y = 44;
orig2$y = 55;
}
event zeek_init() &priority=-10
{
g = 1;
}

View file

@ -0,0 +1,94 @@
# @TEST-EXEC-FAIL: zeek -b %INPUT >out 2>&1
# @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-remove-abspath btest-diff out
global g = 0;
event zeek_init()
{
local orig1 = "hello";
local orig2 = 3.5;
local orig3 = F;
# Should be okay since no local captures.
when ( g > 0 )
{
print g;
}
# Should generate a deprecation warning.
when ( g > 0 )
{
print orig1;
}
# Same.
when ( g > 0 || orig3 )
{
print g;
}
# Same.
when ( g > 0 )
{
print g;
}
timeout 1 sec
{
print orig1;
}
# Should be okay.
when [orig2] ( g > 0 && orig2 < 10.0 )
{
print g;
}
# Should be okay.
when [orig1] ( g > 0 )
{
print orig1;
}
# Should be okay.
when [orig1] ( g > 0 )
{
print g;
}
timeout 1 sec
{
print orig1;
}
# Mismatch: missing a local.
when [orig1] ( g > 0 )
{
print orig1;
}
timeout 1 sec
{
print orig2;
}
# Mismatch: overspecifies a local.
when [orig1, orig2, orig3] ( g > 0 )
{
print orig1;
}
timeout 1 sec
{
print orig2;
}
# Should generate a "no such identifier" error.
when [l1] ( local l1 = network_time() )
{
print l1;
}
# As should this.
when [l2] ( g > 0 )
{
local l2 = network_time();
print l2;
}
}

View file

@ -22,7 +22,7 @@ redef exit_only_after_terminate=T;
event on_demand_key()
{
local host = 1.2.3.4;
when ( local result = SumStats::request_key("test", [$host=host]) )
when [host] ( local result = SumStats::request_key("test", [$host=host]) )
{
print fmt("Key request for %s", host);
print fmt(" Host: %s -> %.0f", host, result["test.reducer"]$sum);

View file

@ -23,7 +23,7 @@ function check_exit_condition()
function test_request(label: string, req: ActiveHTTP::Request)
{
when ( local response = ActiveHTTP::request(req) )
when [label, req] ( local response = ActiveHTTP::request(req) )
{
print label, response;
check_exit_condition();

View file

@ -19,7 +19,7 @@ function check_exit_condition()
function test_cmd(label: string, cmd: Exec::Command)
{
when ( local result = Exec::run(cmd) )
when [label, cmd] ( local result = Exec::run(cmd) )
{
local file_content = "";