luau/Analysis/include/Luau/RecursionCounter.h

51 lines
888 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 <stdexcept>
2022-04-14 17:57:15 -04:00
#include <exception>
namespace Luau
{
2022-04-14 17:57:15 -04:00
struct RecursionLimitException : public std::exception
{
const char* what() const noexcept
{
return "Internal recursion counter limit exceeded";
}
};
struct RecursionCounter
{
RecursionCounter(int* count)
: count(count)
{
++(*count);
}
~RecursionCounter()
{
LUAU_ASSERT(*count > 0);
--(*count);
}
private:
int* count;
};
struct RecursionLimiter : RecursionCounter
{
2022-06-23 21:44:07 -04:00
RecursionLimiter(int* count, int limit)
: RecursionCounter(count)
{
if (limit > 0 && *count > limit)
2022-04-14 17:57:15 -04:00
{
2022-06-23 21:44:07 -04:00
throw RecursionLimitException();
2022-04-14 17:57:15 -04:00
}
}
};
} // namespace Luau