Add lambda expressions with closures to Zeek.

This allows anonymous functions in Zeek to capture their closures.
they do so by creating a copy of their enclosing frame and joining
that with their own frame.

There is no way to specify what specific items to capture from the
closure like C++, nor is there a nonlocal keyword like Python.
Attemptying to declare a local variable that has already been caught
by the closure will error nicely. At the worst this is an inconvenience
for people who are using lambdas which use the same variable names
as their closures.

As a result of functions copying their enclosing frames there is no
way for a function with a closure to reach back up and modify the
state of the frame that it was created in. This lets functions that
generate functions work as expected. The function can reach back and
modify its copy of the frame that it is captured in though.

Implementation wise this is done by creating two new subclasses in
Zeek. The first is a LambdaExpression which can be thought of as a
function generator. It gathers all of the ingredients for a function
at parse time, and then when evaluated creats a new version of that
function with the frame it is being evaluated in as a closure. The
second subclass is a ClosureFrame. This acts for most intents and
purposes like a regular Frame, but it routes lookups of values to its
closure as needed.
This commit is contained in:
Zeke Medley 2019-06-12 14:40:40 -07:00
parent eef669f048
commit a3001f1b2b
17 changed files with 636 additions and 52 deletions

View file

@ -12,6 +12,10 @@
#include "Debug.h"
#include "EventHandler.h"
#include "TraverseTypes.h"
#include "Func.h" // function_ingredients
#include <memory> // std::shared_ptr
#include <utility> // std::move
typedef enum {
EXPR_ANY = -1,
@ -62,6 +66,8 @@ class AssignExpr;
class CallExpr;
class EventExpr;
struct function_ingredients;
class Expr : public BroObj {
public:
@ -997,7 +1003,7 @@ public:
protected:
friend class Expr;
CallExpr() { func = 0; args = 0; }
CallExpr() { func = 0; args = 0; }
void ExprDescribe(ODesc* d) const override;
@ -1007,6 +1013,32 @@ protected:
ListExpr* args;
};
/*
Class to handle the creation of anonymous functions with closures.
Facts:
- LambdaExpr creates a new BroFunc on every call to Eval.
- LambdaExpr must be given all the information to create a BroFunc on
construction except for the closure.
- The closure for created BroFuncs is the frame that the LambdaExpr is
evaluated in.
*/
class LambdaExpr : public Expr {
public:
LambdaExpr(std::unique_ptr<function_ingredients> ingredients,
std::shared_ptr<id_list> outer_ids);
Val* Eval(Frame* f) const override;
TraversalCode Traverse(TraversalCallback* cb) const override;
protected:
void ExprDescribe(ODesc* d) const override;
private:
std::unique_ptr<function_ingredients> ingredients;
std::shared_ptr<id_list> outer_ids;
};
class EventExpr : public Expr {
public:
EventExpr(const char* name, ListExpr* args);