2021-10-29 21:25:12 +01:00
|
|
|
// 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 11:37:29 +01:00
|
|
|
#include "Luau/Error.h"
|
2021-10-29 21:25:12 +01:00
|
|
|
|
|
|
|
#include <stdexcept>
|
2022-04-15 00:57:43 +01:00
|
|
|
#include <exception>
|
|
|
|
|
2021-10-29 21:25:12 +01:00
|
|
|
namespace Luau
|
|
|
|
{
|
|
|
|
|
2022-10-28 11:37:29 +01:00
|
|
|
struct RecursionLimitException : public InternalCompilerError
|
|
|
|
{
|
|
|
|
RecursionLimitException()
|
|
|
|
: InternalCompilerError("Internal recursion counter limit exceeded")
|
|
|
|
{
|
2022-04-15 00:57:43 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-29 21:25:12 +01:00
|
|
|
struct RecursionCounter
|
|
|
|
{
|
|
|
|
RecursionCounter(int* count)
|
|
|
|
: count(count)
|
|
|
|
{
|
|
|
|
++(*count);
|
|
|
|
}
|
|
|
|
|
|
|
|
~RecursionCounter()
|
|
|
|
{
|
|
|
|
LUAU_ASSERT(*count > 0);
|
|
|
|
--(*count);
|
|
|
|
}
|
|
|
|
|
2023-01-06 21:14:35 +00:00
|
|
|
protected:
|
2021-10-29 21:25:12 +01:00
|
|
|
int* count;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct RecursionLimiter : RecursionCounter
|
|
|
|
{
|
2022-06-24 02:56:00 +01:00
|
|
|
RecursionLimiter(int* count, int limit)
|
2021-10-29 21:25:12 +01:00
|
|
|
: RecursionCounter(count)
|
|
|
|
{
|
|
|
|
if (limit > 0 && *count > limit)
|
2022-04-15 00:57:43 +01:00
|
|
|
{
|
2022-12-02 18:09:59 +00:00
|
|
|
throw RecursionLimitException();
|
2022-04-15 00:57:43 +01:00
|
|
|
}
|
2021-10-29 21:25:12 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Luau
|