mirror of
https://github.com/zeek/zeek.git
synced 2025-10-04 07:38:19 +00:00
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:
parent
eef669f048
commit
a3001f1b2b
17 changed files with 636 additions and 52 deletions
40
src/Func.h
40
src/Func.h
|
@ -4,10 +4,13 @@
|
|||
#define func_h
|
||||
|
||||
#include <utility>
|
||||
#include <memory> // std::shared_ptr, std::unique_ptr
|
||||
|
||||
#include "BroList.h"
|
||||
#include "Obj.h"
|
||||
#include "Debug.h"
|
||||
#include "Frame.h"
|
||||
// #include "Val.h"
|
||||
|
||||
class Val;
|
||||
class ListExpr;
|
||||
|
@ -17,6 +20,8 @@ class Frame;
|
|||
class ID;
|
||||
class CallExpr;
|
||||
|
||||
struct CloneState;
|
||||
|
||||
class Func : public BroObj {
|
||||
public:
|
||||
|
||||
|
@ -62,6 +67,7 @@ public:
|
|||
// This (un-)serializes only a single body (as given in SerialInfo).
|
||||
bool Serialize(SerialInfo* info) const;
|
||||
static Func* Unserialize(UnserialInfo* info);
|
||||
virtual Val* DoClone();
|
||||
|
||||
virtual TraversalCode Traverse(TraversalCallback* cb) const;
|
||||
|
||||
|
@ -95,9 +101,14 @@ public:
|
|||
int IsPure() const override;
|
||||
Val* Call(val_list* args, Frame* parent) const override;
|
||||
|
||||
void AddClosure(std::shared_ptr<id_list> ids, Frame* f);
|
||||
void AddBody(Stmt* new_body, id_list* new_inits, int new_frame_size,
|
||||
int priority) override;
|
||||
|
||||
void fsets();
|
||||
|
||||
Val* DoClone() override;
|
||||
|
||||
int FrameSize() const { return frame_size; }
|
||||
|
||||
void Describe(ODesc* d) const override;
|
||||
|
@ -109,6 +120,23 @@ protected:
|
|||
DECLARE_SERIAL(BroFunc);
|
||||
|
||||
int frame_size;
|
||||
|
||||
private:
|
||||
// Shifts the offsets of each id in "idl" by "shift".
|
||||
static void ShiftOffsets(int shift, std::shared_ptr<id_list> idl);
|
||||
|
||||
// Makes a deep copy of the input frame and captures it.
|
||||
void SetClosureFrame(Frame* f);
|
||||
|
||||
void SetOuterIDs(std::shared_ptr<id_list> ids)
|
||||
{ outer_ids = std::move(ids); }
|
||||
|
||||
// List of the outer IDs used in the function. Shared becase other instances
|
||||
// would like to use it as well.
|
||||
std::shared_ptr<id_list> outer_ids = nullptr;
|
||||
// The frame the Func was initialized in. This is not guaranteed to be
|
||||
// initialized and should be handled with care.
|
||||
Frame* closure = nullptr;
|
||||
};
|
||||
|
||||
typedef Val* (*built_in_func)(Frame* frame, val_list* args);
|
||||
|
@ -146,6 +174,18 @@ struct CallInfo {
|
|||
const val_list* args;
|
||||
};
|
||||
|
||||
// Struct that collects the arguments for a Func.
|
||||
// Used for BroFuncs with closures.
|
||||
struct function_ingredients
|
||||
{
|
||||
ID* id;
|
||||
Stmt* body;
|
||||
id_list* inits;
|
||||
int frame_size;
|
||||
int priority;
|
||||
Scope* scope;
|
||||
};
|
||||
|
||||
extern vector<CallInfo> call_stack;
|
||||
|
||||
extern std::string render_call_stack();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue