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"
2022-10-28 06:22:49 +08:00
#include "Luau/Error.h"
#include <stdexcept>
2022-04-15 05:57:15 +08:00
#include <exception>
namespace Luau
{
2022-10-28 06:22:49 +08:00
struct RecursionLimitException : public InternalCompilerError
{
RecursionLimitException()
: InternalCompilerError("Internal recursion counter limit exceeded")
{
2022-04-15 05:57:15 +08:00
}
};
struct RecursionCounter
{
RecursionCounter(int* count)
: count(count)
{
++(*count);
}
~RecursionCounter()
{
LUAU_ASSERT(*count > 0);
--(*count);
}
2023-01-07 00:07:19 +08:00
protected:
int* count;
};
struct RecursionLimiter : RecursionCounter
{
2022-06-24 09:44:07 +08:00
RecursionLimiter(int* count, int limit)
: RecursionCounter(count)
{
if (limit > 0 && *count > limit)
2022-04-15 05:57:15 +08:00
{
2022-12-02 18:46:05 +08:00
throw RecursionLimitException();
2022-04-15 05:57:15 +08:00
}
}
};
} // namespace Luau