removed deprecated capture-by-reference closures

This commit is contained in:
Vern Paxson 2022-06-17 15:55:43 -07:00
parent 5ccba54335
commit 5a0a88526e
20 changed files with 59 additions and 1220 deletions

View file

@ -318,9 +318,6 @@ ScriptFunc::ScriptFunc(std::string _name, FuncTypePtr ft, std::vector<StmtPtr> b
ScriptFunc::~ScriptFunc()
{
if ( ! weak_closure_ref )
Unref(closure);
delete captures_frame;
delete captures_offset_mapping;
}
@ -358,9 +355,6 @@ ValPtr ScriptFunc::Invoke(zeek::Args* args, Frame* parent) const
auto f = make_intrusive<Frame>(frame_size, this, args);
if ( closure )
f->CaptureClosure(closure, outer_ids);
// Hand down any trigger.
if ( parent )
{
@ -586,71 +580,6 @@ void ScriptFunc::ReplaceBody(const StmtPtr& old_body, StmtPtr new_body)
current_body = new_body;
}
void ScriptFunc::AddClosure(IDPList ids, Frame* f)
{
if ( ! f )
return;
SetOuterIDs(std::move(ids));
SetClosureFrame(f);
}
bool ScriptFunc::StrengthenClosureReference(Frame* f)
{
if ( closure != f )
return false;
if ( ! weak_closure_ref )
return false;
closure = closure->SelectiveClone(outer_ids, this);
weak_closure_ref = false;
return true;
}
bool ScriptFunc::HasCopySemantics() const
{
return type->GetCaptures().has_value();
}
void ScriptFunc::SetClosureFrame(Frame* f)
{
if ( closure )
reporter->InternalError("Tried to override closure for ScriptFunc %s.", Name());
// Have to use weak references initially because otherwise Ref'ing the
// original frame creates a circular reference: the function holds a
// reference to the frame and the frame contains a reference to this
// function value. And we can't just do a shallow clone of the frame
// up front because the closure semantics in Zeek allow mutating
// the outer frame.
closure = f;
weak_closure_ref = true;
f->AddFunctionWithClosureRef(this);
}
bool ScriptFunc::UpdateClosure(const broker::vector& data)
{
auto result = Frame::Unserialize(data, {});
if ( ! result.first )
return false;
auto& new_closure = result.second;
if ( new_closure )
new_closure->SetFunction(this);
if ( ! weak_closure_ref )
Unref(closure);
weak_closure_ref = false;
closure = new_closure.release();
return true;
}
bool ScriptFunc::DeserializeCaptures(const broker::vector& data)
{
auto result = Frame::Unserialize(data, GetType()->GetCaptures());
@ -671,8 +600,6 @@ FuncPtr ScriptFunc::DoClone()
CopyStateInto(other.get());
other->frame_size = frame_size;
other->closure = closure ? closure->SelectiveClone(outer_ids, this) : nullptr;
other->weak_closure_ref = false;
other->outer_ids = outer_ids;
if ( captures_frame )
@ -685,15 +612,12 @@ FuncPtr ScriptFunc::DoClone()
return other;
}
broker::expected<broker::data> ScriptFunc::SerializeClosure() const
broker::expected<broker::data> ScriptFunc::SerializeCaptures() const
{
if ( captures_frame )
return captures_frame->SerializeCopyFrame();
if ( closure )
return closure->SerializeClosureFrame(outer_ids);
// No captures/closures, return an empty vector.
// No captures, return an empty vector.
return broker::vector{};
}