mirror of
https://github.com/zeek/zeek.git
synced 2025-10-07 17:18:20 +00:00
Add builtin_exception() functions
These work like builtin_error(), but also throw an InterpreterException
This commit is contained in:
parent
9f16fa6474
commit
2b698c4e1b
2 changed files with 57 additions and 15 deletions
67
src/Func.cc
67
src/Func.cc
|
@ -663,29 +663,50 @@ void BuiltinFunc::Describe(ODesc* d) const
|
|||
d->AddCount(is_pure);
|
||||
}
|
||||
|
||||
void builtin_error(const char* msg)
|
||||
{
|
||||
builtin_error(msg, IntrusivePtr<Val>{});
|
||||
}
|
||||
|
||||
void builtin_error(const char* msg, IntrusivePtr<Val> arg)
|
||||
{
|
||||
builtin_error(msg, arg.get());
|
||||
}
|
||||
|
||||
void builtin_error(const char* msg, BroObj* arg)
|
||||
static void builtin_error_common(const char* msg, BroObj* arg, bool unwind)
|
||||
{
|
||||
auto emit = [=](const zeek::detail::CallExpr* ce)
|
||||
{
|
||||
if ( ce )
|
||||
ce->Error(msg, arg);
|
||||
{
|
||||
if ( unwind )
|
||||
{
|
||||
if ( arg )
|
||||
{
|
||||
ODesc d;
|
||||
arg->Describe(&d);
|
||||
reporter->ExprRuntimeError(ce, "%s (%s), during call:", msg,
|
||||
d.Description());
|
||||
}
|
||||
else
|
||||
reporter->ExprRuntimeError(ce, "%s", msg);
|
||||
}
|
||||
else
|
||||
ce->Error(msg, arg);
|
||||
}
|
||||
else
|
||||
reporter->Error(msg, arg);
|
||||
{
|
||||
if ( arg )
|
||||
{
|
||||
if ( unwind )
|
||||
reporter->RuntimeError(arg->GetLocationInfo(), "%s", msg);
|
||||
else
|
||||
arg->Error(msg);
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( unwind )
|
||||
reporter->RuntimeError(nullptr, "%s", msg);
|
||||
else
|
||||
reporter->Error("%s", msg);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
if ( call_stack.empty() )
|
||||
{
|
||||
// Shouldn't happen unless someone (mistakenly) calls builtin_error()
|
||||
// from somewhere that's not even evaluating script-code.
|
||||
emit(nullptr);
|
||||
return;
|
||||
}
|
||||
|
@ -739,6 +760,24 @@ void builtin_error(const char* msg, BroObj* arg)
|
|||
emit(last_call.call);
|
||||
}
|
||||
|
||||
void builtin_error(const char* msg)
|
||||
{ builtin_error_common(msg, nullptr, false); }
|
||||
|
||||
void builtin_error(const char* msg, const IntrusivePtr<Val>& arg)
|
||||
{ builtin_error_common(msg, arg.get(), false); }
|
||||
|
||||
void builtin_error(const char* msg, BroObj* arg)
|
||||
{ builtin_error_common(msg, arg, false); }
|
||||
|
||||
void builtin_exception(const char* msg)
|
||||
{ builtin_error_common(msg, nullptr, true); }
|
||||
|
||||
void builtin_exception(const char* msg, const IntrusivePtr<Val>& arg)
|
||||
{ builtin_error_common(msg, arg.get(), true); }
|
||||
|
||||
void builtin_exception(const char* msg, BroObj* arg)
|
||||
{ builtin_error_common(msg, arg, true); }
|
||||
|
||||
#include "zeek.bif.func_h"
|
||||
#include "stats.bif.func_h"
|
||||
#include "reporter.bif.func_h"
|
||||
|
|
|
@ -236,8 +236,11 @@ protected:
|
|||
|
||||
|
||||
extern void builtin_error(const char* msg);
|
||||
extern void builtin_error(const char* msg, IntrusivePtr<Val>);
|
||||
extern void builtin_error(const char* msg, const IntrusivePtr<Val>& arg);
|
||||
extern void builtin_error(const char* msg, BroObj* arg);
|
||||
extern void builtin_exception(const char* msg);
|
||||
extern void builtin_exception(const char* msg, const IntrusivePtr<Val>& arg);
|
||||
extern void builtin_exception(const char* msg, BroObj* arg);
|
||||
extern void init_builtin_funcs();
|
||||
extern void init_builtin_funcs_subdirs();
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue