luau/Analysis/include/Luau/RecursionCounter.h

52 lines
930 B
C
Raw Normal View History

// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
#pragma once
#include "Luau/Common.h"
#include "Luau/Error.h"
#include <stdexcept>
2022-04-15 00:57:43 +01:00
#include <exception>
namespace Luau
{
struct RecursionLimitException : public InternalCompilerError
{
RecursionLimitException()
: InternalCompilerError("Internal recursion counter limit exceeded")
{
2022-04-15 00:57:43 +01:00
}
};
struct RecursionCounter
{
RecursionCounter(int* count)
: count(count)
{
++(*count);
}
~RecursionCounter()
{
LUAU_ASSERT(*count > 0);
--(*count);
}
protected:
int* count;
};
struct RecursionLimiter : RecursionCounter
{
2022-06-24 02:56:00 +01:00
RecursionLimiter(int* count, int limit)
: RecursionCounter(count)
{
if (limit > 0 && *count > limit)
2022-04-15 00:57:43 +01:00
{
throw RecursionLimitException();
2022-04-15 00:57:43 +01:00
}
}
};
} // namespace Luau