Merge pull request 'Bounded Lua evaluator with transactional host results (#17)' (#33) from issue/17-bounded-lua into main
This commit was merged in pull request #33.
This commit is contained in:
commit
706383e13f
6 files changed
+552
No files matched your search
@@ -0,0 +1,154 @@
|
||||
#include <Void/Script/Lua.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <cstdint>
|
||||
#include <expected>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
using Void::Script::ErrorKind;
|
||||
using Void::Script::Host;
|
||||
using Void::Script::HostInvocation;
|
||||
using Void::Script::Input;
|
||||
using Void::Script::Integer;
|
||||
using Void::Script::Limits;
|
||||
using Void::Script::LuaEvaluator;
|
||||
|
||||
[[nodiscard]] Host add_host()
|
||||
{
|
||||
return Host{
|
||||
.name = "add",
|
||||
.function = [](std::span<const Integer> arguments)
|
||||
-> std::expected<Integer, std::string> {
|
||||
if (arguments.size() != 2) {
|
||||
return std::unexpected{std::string{"add expects two arguments"}};
|
||||
}
|
||||
return arguments[0] + arguments[1];
|
||||
}};
|
||||
}
|
||||
|
||||
[[nodiscard]] Host fail_host()
|
||||
{
|
||||
return Host{
|
||||
.name = "fail",
|
||||
.function = [](std::span<const Integer> arguments)
|
||||
-> std::expected<Integer, std::string> {
|
||||
if (!arguments.empty() && arguments[0] < 0) {
|
||||
return std::unexpected{std::string{"negative"}};
|
||||
}
|
||||
return arguments.empty() ? Integer{0} : arguments[0];
|
||||
}};
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Void::Testing::Test lua_evaluates_integer_results_and_inputs()
|
||||
{
|
||||
LuaEvaluator evaluator;
|
||||
const Input inputs[] = {Input{.key = 1, .value = 2},
|
||||
Input{.key = 2, .value = 3}};
|
||||
auto result = evaluator.evaluate("return input[1] + input[2]", inputs);
|
||||
Void::Testing::require(result.has_value());
|
||||
Void::Testing::expect(result->integers == std::vector<Integer>{5});
|
||||
}
|
||||
|
||||
Void::Testing::Test lua_host_functions_are_recorded()
|
||||
{
|
||||
LuaEvaluator evaluator{Limits{}, {add_host()}};
|
||||
auto result = evaluator.evaluate("return add(2, 3)");
|
||||
Void::Testing::require(result.has_value());
|
||||
Void::Testing::expect(result->integers == std::vector<Integer>{5});
|
||||
Void::Testing::require(result->host_calls.size() == 1);
|
||||
Void::Testing::expect(result->host_calls[0] ==
|
||||
HostInvocation{.host = "add",
|
||||
.arguments = {2, 3},
|
||||
.result = 5});
|
||||
}
|
||||
|
||||
Void::Testing::Test lua_host_error_is_structured_and_recovers()
|
||||
{
|
||||
LuaEvaluator evaluator{Limits{}, {fail_host()}};
|
||||
auto failed = evaluator.evaluate("return fail(-1)");
|
||||
Void::Testing::require(!failed.has_value());
|
||||
Void::Testing::expect(failed.error().kind == ErrorKind::host_error);
|
||||
|
||||
// A later valid evaluation starts from a clean state.
|
||||
auto recovered = evaluator.evaluate("return fail(4)");
|
||||
Void::Testing::require(recovered.has_value());
|
||||
Void::Testing::expect(recovered->integers == std::vector<Integer>{4});
|
||||
}
|
||||
|
||||
Void::Testing::Test lua_instruction_limit()
|
||||
{
|
||||
Limits limits;
|
||||
limits.max_instructions = 1'000;
|
||||
LuaEvaluator evaluator{limits};
|
||||
auto result = evaluator.evaluate("while true do end");
|
||||
Void::Testing::require(!result.has_value());
|
||||
Void::Testing::expect(result.error().kind == ErrorKind::instruction_limit);
|
||||
}
|
||||
|
||||
Void::Testing::Test lua_allocator_limit()
|
||||
{
|
||||
Limits limits;
|
||||
limits.max_allocator_bytes = 128 * 1024;
|
||||
LuaEvaluator evaluator{limits};
|
||||
auto result = evaluator.evaluate(
|
||||
"local s = string.rep('x', 1000000) return #s");
|
||||
Void::Testing::require(!result.has_value());
|
||||
Void::Testing::expect(result.error().kind == ErrorKind::allocator_limit);
|
||||
}
|
||||
|
||||
Void::Testing::Test lua_host_call_limit()
|
||||
{
|
||||
Limits limits;
|
||||
limits.max_host_calls = 2;
|
||||
LuaEvaluator evaluator{limits, {fail_host()}};
|
||||
auto result = evaluator.evaluate("for i = 1, 10 do fail(i) end return 0");
|
||||
Void::Testing::require(!result.has_value());
|
||||
Void::Testing::expect(result.error().kind == ErrorKind::host_call_limit);
|
||||
}
|
||||
|
||||
Void::Testing::Test lua_compile_error()
|
||||
{
|
||||
LuaEvaluator evaluator;
|
||||
auto result = evaluator.evaluate("return (");
|
||||
Void::Testing::require(!result.has_value());
|
||||
Void::Testing::expect(result.error().kind == ErrorKind::compile);
|
||||
}
|
||||
|
||||
Void::Testing::Test lua_result_limit()
|
||||
{
|
||||
Limits limits;
|
||||
limits.max_result_integers = 2;
|
||||
LuaEvaluator evaluator{limits};
|
||||
auto result = evaluator.evaluate("return 1, 2, 3");
|
||||
Void::Testing::require(!result.has_value());
|
||||
Void::Testing::expect(result.error().kind == ErrorKind::result_limit);
|
||||
}
|
||||
|
||||
Void::Testing::Test lua_rejects_non_integer_results()
|
||||
{
|
||||
LuaEvaluator evaluator;
|
||||
auto result = evaluator.evaluate("return 'text'");
|
||||
Void::Testing::require(!result.has_value());
|
||||
Void::Testing::expect(result.error().kind == ErrorKind::runtime);
|
||||
}
|
||||
|
||||
Void::Testing::Test lua_input_limit()
|
||||
{
|
||||
Limits limits;
|
||||
limits.max_inputs = 1;
|
||||
LuaEvaluator evaluator{limits};
|
||||
const Input inputs[] = {Input{.key = 1, .value = 1},
|
||||
Input{.key = 2, .value = 2}};
|
||||
auto result = evaluator.evaluate("return 0", inputs);
|
||||
Void::Testing::require(!result.has_value());
|
||||
Void::Testing::expect(result.error().kind == ErrorKind::invalid_input);
|
||||
}
|
||||
Reference in new issue
Block a user