mirror of
https://github.com/zeek/zeek.git
synced 2025-10-02 14:48:21 +00:00
Spicy: Cleanup some runtime code.
This commit is contained in:
parent
5a74db30ef
commit
cee92cbf6b
1 changed files with 45 additions and 72 deletions
|
@ -30,11 +30,7 @@
|
|||
namespace zeek::spicy::rt {
|
||||
|
||||
// Adapt to rename of exception.
|
||||
#if SPICY_VERSION_NUMBER >= 10700
|
||||
using UsageError = ::hilti::rt::UsageError;
|
||||
#else
|
||||
using UsageError = ::hilti::rt::UserException;
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Exception thrown by event generation code if the value of an `$...`
|
||||
|
@ -71,11 +67,11 @@ class TypeMismatch : public UsageError {
|
|||
public:
|
||||
TypeMismatch(const std::string_view& msg, std::string_view location = "")
|
||||
: UsageError(hilti::rt::fmt("Event parameter mismatch, %s", msg)) {}
|
||||
TypeMismatch(const std::string_view& have, TypePtr want, std::string_view location = "")
|
||||
TypeMismatch(const std::string_view& have, const TypePtr& want, std::string_view location = "")
|
||||
: TypeMismatch(_fmt(have, want)) {}
|
||||
|
||||
private:
|
||||
std::string _fmt(const std::string_view& have, TypePtr want) {
|
||||
std::string _fmt(const std::string_view& have, const TypePtr& want) {
|
||||
ODesc d;
|
||||
want->Describe(&d);
|
||||
return hilti::rt::fmt("cannot convert Spicy value of type '%s' to Zeek value of type '%s'", have,
|
||||
|
@ -456,38 +452,38 @@ hilti::rt::Time network_time();
|
|||
|
||||
// Forward-declare to_val() functions.
|
||||
template<typename T, typename std::enable_if_t<hilti::rt::is_tuple<T>::value>* = nullptr>
|
||||
ValPtr to_val(const T& t, TypePtr target);
|
||||
ValPtr to_val(const T& t, const TypePtr& target);
|
||||
template<typename... Ts>
|
||||
inline ValPtr to_val(const hilti::rt::Bitfield<Ts...>& v, TypePtr target);
|
||||
inline ValPtr to_val(const hilti::rt::Bitfield<Ts...>& v, const TypePtr& target);
|
||||
template<typename T, typename std::enable_if_t<std::is_base_of<::hilti::rt::trait::isStruct, T>::value>* = nullptr>
|
||||
ValPtr to_val(const T& t, TypePtr target);
|
||||
ValPtr to_val(const T& t, const TypePtr& target);
|
||||
template<typename T, typename std::enable_if_t<std::is_enum<typename T::Value>::value>* = nullptr>
|
||||
ValPtr to_val(const T& t, TypePtr target);
|
||||
ValPtr to_val(const T& t, const TypePtr& target);
|
||||
template<typename T, typename std::enable_if_t<std::is_enum<T>::value>* = nullptr>
|
||||
ValPtr to_val(const T& t, TypePtr target);
|
||||
ValPtr to_val(const T& t, const TypePtr& target);
|
||||
template<typename K, typename V>
|
||||
ValPtr to_val(const hilti::rt::Map<K, V>& s, TypePtr target);
|
||||
ValPtr to_val(const hilti::rt::Map<K, V>& s, const TypePtr& target);
|
||||
template<typename T>
|
||||
ValPtr to_val(const hilti::rt::Set<T>& s, TypePtr target);
|
||||
ValPtr to_val(const hilti::rt::Set<T>& s, const TypePtr& target);
|
||||
template<typename T>
|
||||
ValPtr to_val(const hilti::rt::Vector<T>& v, TypePtr target);
|
||||
ValPtr to_val(const hilti::rt::Vector<T>& v, const TypePtr& target);
|
||||
template<typename T>
|
||||
ValPtr to_val(const std::optional<T>& t, TypePtr target);
|
||||
ValPtr to_val(const std::optional<T>& t, const TypePtr& target);
|
||||
template<typename T>
|
||||
ValPtr to_val(const hilti::rt::DeferredExpression<T>& t, TypePtr target);
|
||||
ValPtr to_val(const hilti::rt::DeferredExpression<T>& t, const TypePtr& target);
|
||||
template<typename T>
|
||||
ValPtr to_val(hilti::rt::integer::safe<T> i, TypePtr target);
|
||||
ValPtr to_val(hilti::rt::integer::safe<T> i, const TypePtr& target);
|
||||
template<typename T>
|
||||
ValPtr to_val(const hilti::rt::ValueReference<T>& t, TypePtr target);
|
||||
ValPtr to_val(const hilti::rt::ValueReference<T>& t, const TypePtr& target);
|
||||
|
||||
inline ValPtr to_val(const hilti::rt::Bool& b, TypePtr target);
|
||||
inline ValPtr to_val(const hilti::rt::Address& d, TypePtr target);
|
||||
inline ValPtr to_val(const hilti::rt::Bytes& b, TypePtr target);
|
||||
inline ValPtr to_val(const hilti::rt::Interval& t, TypePtr target);
|
||||
inline ValPtr to_val(const hilti::rt::Port& d, TypePtr target);
|
||||
inline ValPtr to_val(const hilti::rt::Time& t, TypePtr target);
|
||||
inline ValPtr to_val(const std::string& s, TypePtr target);
|
||||
inline ValPtr to_val(double r, TypePtr target);
|
||||
inline ValPtr to_val(const hilti::rt::Bool& b, const TypePtr& target);
|
||||
inline ValPtr to_val(const hilti::rt::Address& d, const TypePtr& target);
|
||||
inline ValPtr to_val(const hilti::rt::Bytes& b, const TypePtr& target);
|
||||
inline ValPtr to_val(const hilti::rt::Interval& t, const TypePtr& target);
|
||||
inline ValPtr to_val(const hilti::rt::Port& d, const TypePtr& target);
|
||||
inline ValPtr to_val(const hilti::rt::Time& t, const TypePtr& target);
|
||||
inline ValPtr to_val(const std::string& s, const TypePtr& target);
|
||||
inline ValPtr to_val(double r, const TypePtr& target);
|
||||
|
||||
/**
|
||||
* Converts a Spicy-side optional value to a Zeek value. This assumes the
|
||||
|
@ -495,7 +491,7 @@ inline ValPtr to_val(double r, TypePtr target);
|
|||
* returned with ref count +1.
|
||||
*/
|
||||
template<typename T>
|
||||
inline ValPtr to_val(const std::optional<T>& t, TypePtr target) {
|
||||
inline ValPtr to_val(const std::optional<T>& t, const TypePtr& target) {
|
||||
if ( t.has_value() )
|
||||
return to_val(hilti::rt::optional::value(t), target);
|
||||
|
||||
|
@ -509,7 +505,7 @@ inline ValPtr to_val(const std::optional<T>& t, TypePtr target) {
|
|||
* picks up on).
|
||||
*/
|
||||
template<typename T>
|
||||
inline ValPtr to_val(const hilti::rt::DeferredExpression<T>& t, TypePtr target) {
|
||||
inline ValPtr to_val(const hilti::rt::DeferredExpression<T>& t, const TypePtr& target) {
|
||||
try {
|
||||
return to_val(t(), target);
|
||||
} catch ( const hilti::rt::AttributeNotSet& ) {
|
||||
|
@ -521,7 +517,7 @@ inline ValPtr to_val(const hilti::rt::DeferredExpression<T>& t, TypePtr target)
|
|||
* Converts a Spicy-side string to a Zeek value. The result is returned with
|
||||
* ref count +1.
|
||||
*/
|
||||
inline ValPtr to_val(const std::string& s, TypePtr target) {
|
||||
inline ValPtr to_val(const std::string& s, const TypePtr& target) {
|
||||
if ( target->Tag() != TYPE_STRING )
|
||||
throw TypeMismatch("string", target);
|
||||
|
||||
|
@ -532,7 +528,7 @@ inline ValPtr to_val(const std::string& s, TypePtr target) {
|
|||
* Converts a Spicy-side bytes instance to a Zeek value. The result is returned with
|
||||
* ref count +1.
|
||||
*/
|
||||
inline ValPtr to_val(const hilti::rt::Bytes& b, TypePtr target) {
|
||||
inline ValPtr to_val(const hilti::rt::Bytes& b, const TypePtr& target) {
|
||||
if ( target->Tag() != TYPE_STRING )
|
||||
throw TypeMismatch("string", target);
|
||||
|
||||
|
@ -544,7 +540,7 @@ inline ValPtr to_val(const hilti::rt::Bytes& b, TypePtr target) {
|
|||
* returned with ref count +1.
|
||||
*/
|
||||
template<typename T>
|
||||
inline ValPtr to_val(hilti::rt::integer::safe<T> i, TypePtr target) {
|
||||
inline ValPtr to_val(hilti::rt::integer::safe<T> i, const TypePtr& target) {
|
||||
ValPtr v = nullptr;
|
||||
if constexpr ( std::is_unsigned<T>::value ) {
|
||||
if ( target->Tag() == TYPE_COUNT )
|
||||
|
@ -571,7 +567,7 @@ inline ValPtr to_val(hilti::rt::integer::safe<T> i, TypePtr target) {
|
|||
}
|
||||
|
||||
template<typename T>
|
||||
ValPtr to_val(const hilti::rt::ValueReference<T>& t, TypePtr target) {
|
||||
ValPtr to_val(const hilti::rt::ValueReference<T>& t, const TypePtr& target) {
|
||||
if ( auto* x = t.get() )
|
||||
return to_val(*x, target);
|
||||
|
||||
|
@ -582,7 +578,7 @@ ValPtr to_val(const hilti::rt::ValueReference<T>& t, TypePtr target) {
|
|||
* Converts a Spicy-side signed bool to a Zeek value. The result is
|
||||
* returned with ref count +1.
|
||||
*/
|
||||
inline ValPtr to_val(const hilti::rt::Bool& b, TypePtr target) {
|
||||
inline ValPtr to_val(const hilti::rt::Bool& b, const TypePtr& target) {
|
||||
if ( target->Tag() != TYPE_BOOL )
|
||||
throw TypeMismatch("bool", target);
|
||||
|
||||
|
@ -593,7 +589,7 @@ inline ValPtr to_val(const hilti::rt::Bool& b, TypePtr target) {
|
|||
* Converts a Spicy-side real to a Zeek value. The result is returned with
|
||||
* ref count +1.
|
||||
*/
|
||||
inline ValPtr to_val(double r, TypePtr target) {
|
||||
inline ValPtr to_val(double r, const TypePtr& target) {
|
||||
if ( target->Tag() != TYPE_DOUBLE )
|
||||
throw TypeMismatch("double", target);
|
||||
|
||||
|
@ -604,7 +600,7 @@ inline ValPtr to_val(double r, TypePtr target) {
|
|||
* Converts a Spicy-side address to a Zeek value. The result is returned with
|
||||
* ref count +1.
|
||||
*/
|
||||
inline ValPtr to_val(const hilti::rt::Address& d, TypePtr target) {
|
||||
inline ValPtr to_val(const hilti::rt::Address& d, const TypePtr& target) {
|
||||
if ( target->Tag() != TYPE_ADDR )
|
||||
throw TypeMismatch("addr", target);
|
||||
|
||||
|
@ -621,17 +617,11 @@ inline ValPtr to_val(const hilti::rt::Address& d, TypePtr target) {
|
|||
* Converts a Spicy-side address to a Zeek value. The result is returned with
|
||||
* ref count +1.
|
||||
*/
|
||||
inline ValPtr to_val(const hilti::rt::Port& p, TypePtr target) {
|
||||
inline ValPtr to_val(const hilti::rt::Port& p, const TypePtr& target) {
|
||||
if ( target->Tag() != TYPE_PORT )
|
||||
throw TypeMismatch("port", target);
|
||||
|
||||
#if SPICY_VERSION_NUMBER >= 10700
|
||||
auto proto = p.protocol().value();
|
||||
#else
|
||||
auto proto = p.protocol();
|
||||
#endif
|
||||
|
||||
switch ( proto ) {
|
||||
switch ( p.protocol().value() ) {
|
||||
case hilti::rt::Protocol::TCP: return val_mgr->Port(p.port(), ::TransportProto::TRANSPORT_TCP);
|
||||
|
||||
case hilti::rt::Protocol::UDP: return val_mgr->Port(p.port(), ::TransportProto::TRANSPORT_UDP);
|
||||
|
@ -646,7 +636,7 @@ inline ValPtr to_val(const hilti::rt::Port& p, TypePtr target) {
|
|||
* Converts a Spicy-side time to a Zeek value. The result is returned with
|
||||
* ref count +1.
|
||||
*/
|
||||
inline ValPtr to_val(const hilti::rt::Interval& i, TypePtr target) {
|
||||
inline ValPtr to_val(const hilti::rt::Interval& i, const TypePtr& target) {
|
||||
if ( target->Tag() != TYPE_INTERVAL )
|
||||
throw TypeMismatch("interval", target);
|
||||
|
||||
|
@ -657,7 +647,7 @@ inline ValPtr to_val(const hilti::rt::Interval& i, TypePtr target) {
|
|||
* Converts a Spicy-side time to a Zeek value. The result is returned with
|
||||
* ref count +1.
|
||||
*/
|
||||
inline ValPtr to_val(const hilti::rt::Time& t, TypePtr target) {
|
||||
inline ValPtr to_val(const hilti::rt::Time& t, const TypePtr& target) {
|
||||
if ( target->Tag() != TYPE_TIME )
|
||||
throw TypeMismatch("time", target);
|
||||
|
||||
|
@ -669,7 +659,7 @@ inline ValPtr to_val(const hilti::rt::Time& t, TypePtr target) {
|
|||
* ref count +1.
|
||||
*/
|
||||
template<typename T>
|
||||
inline ValPtr to_val(const hilti::rt::Vector<T>& v, TypePtr target) {
|
||||
inline ValPtr to_val(const hilti::rt::Vector<T>& v, const TypePtr& target) {
|
||||
if ( target->Tag() != TYPE_VECTOR && target->Tag() != TYPE_LIST )
|
||||
throw TypeMismatch("expected vector or list", target);
|
||||
|
||||
|
@ -686,7 +676,7 @@ inline ValPtr to_val(const hilti::rt::Vector<T>& v, TypePtr target) {
|
|||
* ref count +1.
|
||||
*/
|
||||
template<typename K, typename V>
|
||||
inline ValPtr to_val(const hilti::rt::Map<K, V>& m, TypePtr target) {
|
||||
inline ValPtr to_val(const hilti::rt::Map<K, V>& m, const TypePtr& target) {
|
||||
if constexpr ( hilti::rt::is_tuple<K>::value )
|
||||
throw TypeMismatch("internal error: sets with tuples not yet supported in to_val()");
|
||||
|
||||
|
@ -716,7 +706,7 @@ inline ValPtr to_val(const hilti::rt::Map<K, V>& m, TypePtr target) {
|
|||
* ref count +1.
|
||||
*/
|
||||
template<typename T>
|
||||
inline ValPtr to_val(const hilti::rt::Set<T>& s, TypePtr target) {
|
||||
inline ValPtr to_val(const hilti::rt::Set<T>& s, const TypePtr& target) {
|
||||
if ( target->Tag() != TYPE_TABLE )
|
||||
throw TypeMismatch("set", target);
|
||||
|
||||
|
@ -822,7 +812,7 @@ inline void set_record_field(RecordVal* rval, const IntrusivePtr<RecordType>& rt
|
|||
* with ref count +1.
|
||||
*/
|
||||
template<typename T, typename std::enable_if_t<hilti::rt::is_tuple<T>::value>*>
|
||||
inline ValPtr to_val(const T& t, TypePtr target) {
|
||||
inline ValPtr to_val(const T& t, const TypePtr& target) {
|
||||
if ( target->Tag() != TYPE_RECORD )
|
||||
throw TypeMismatch("tuple", target);
|
||||
|
||||
|
@ -843,7 +833,7 @@ inline ValPtr to_val(const T& t, TypePtr target) {
|
|||
* with ref count +1.
|
||||
*/
|
||||
template<typename... Ts>
|
||||
inline ValPtr to_val(const hilti::rt::Bitfield<Ts...>& v, TypePtr target) {
|
||||
inline ValPtr to_val(const hilti::rt::Bitfield<Ts...>& v, const TypePtr& target) {
|
||||
using Bitfield = hilti::rt::Bitfield<Ts...>;
|
||||
|
||||
if ( target->Tag() != TYPE_RECORD )
|
||||
|
@ -876,7 +866,7 @@ constexpr bool is_optional = is_optional_impl<std::remove_cv_t<std::remove_refer
|
|||
* with a ref count +1.
|
||||
*/
|
||||
template<typename T, typename std::enable_if_t<std::is_base_of<::hilti::rt::trait::isStruct, T>::value>*>
|
||||
inline ValPtr to_val(const T& t, TypePtr target) {
|
||||
inline ValPtr to_val(const T& t, const TypePtr& target) {
|
||||
if ( target->Tag() != TYPE_RECORD )
|
||||
throw TypeMismatch("struct", target);
|
||||
|
||||
|
@ -944,35 +934,18 @@ inline ValPtr to_val_for_transport_proto(int64_t val, const TypePtr& target) {
|
|||
}
|
||||
|
||||
/**
|
||||
* Converts a Spicy-side enum to a Zeek record value. The result is returned
|
||||
* Converts a Spicy-side enum to a Zeek enum value. The result is returned
|
||||
* with ref count +1.
|
||||
*/
|
||||
template<typename T, typename std::enable_if_t<std::is_enum<typename T::Value>::value>*>
|
||||
inline ValPtr to_val(const T& t, TypePtr target) {
|
||||
#if SPICY_VERSION_NUMBER >= 10700
|
||||
auto proto = typename T::Value(t.value());
|
||||
#else
|
||||
auto proto = t;
|
||||
#endif
|
||||
|
||||
return to_val(proto, target);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a C++ Spicy-side enum to a Zeek record value. The result is returned
|
||||
* with ref count +1. This specialization is provided for compatibility with <spicy-1.7.0.
|
||||
*
|
||||
* TODO(bbannier): remove this once we drop support for Spicy versions before 1.7.0.
|
||||
*/
|
||||
template<typename T, typename std::enable_if_t<std::is_enum<T>::value>*>
|
||||
inline ValPtr to_val(const T& t, TypePtr target) {
|
||||
inline ValPtr to_val(const T& t, const TypePtr& target) {
|
||||
if ( target->Tag() != TYPE_ENUM )
|
||||
throw TypeMismatch("enum", target);
|
||||
|
||||
// We'll usually be getting an int64_t for T, but allow other signed ints
|
||||
// as well.
|
||||
static_assert(std::is_signed<std::underlying_type_t<T>>{});
|
||||
auto it = static_cast<int64_t>(t);
|
||||
static_assert(std::is_signed<std::underlying_type_t<typename T::Value>>{});
|
||||
auto it = static_cast<int64_t>(t.value());
|
||||
|
||||
// Special case: map enum values to Zeek's semantics.
|
||||
if ( target->GetName() == "transport_proto" ) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue