feat(Script): bounded Lua evaluator with transactional host results (#17)
This commit is contained in:
1 parent
08038000cc
commit
025d97c6f6
1 file changed
+72
@@ -0,0 +1,72 @@
|
||||
#pragma once
|
||||
|
||||
#include <compare>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace Void::Script
|
||||
{
|
||||
|
||||
using Integer = std::int64_t;
|
||||
|
||||
enum class ErrorKind : std::uint8_t {
|
||||
compile = 0,
|
||||
runtime,
|
||||
allocator_limit,
|
||||
instruction_limit,
|
||||
host_call_limit,
|
||||
result_limit,
|
||||
host_error,
|
||||
invalid_input,
|
||||
};
|
||||
|
||||
struct Error {
|
||||
ErrorKind kind{ErrorKind::runtime};
|
||||
std::string message;
|
||||
|
||||
[[nodiscard]] bool operator==(const Error&) const = default;
|
||||
};
|
||||
|
||||
struct Limits {
|
||||
/// Total bytes the VM may allocate (including the state and libraries).
|
||||
std::size_t max_allocator_bytes{1ULL * 1024ULL * 1024ULL};
|
||||
/// VM instructions before evaluation is aborted.
|
||||
std::size_t max_instructions{100'000};
|
||||
/// Host function invocations before evaluation is aborted.
|
||||
std::size_t max_host_calls{1'000};
|
||||
/// Maximum number of integer results collected.
|
||||
std::size_t max_result_integers{1'024};
|
||||
/// Maximum number of integer-keyed inputs.
|
||||
std::size_t max_inputs{256};
|
||||
};
|
||||
|
||||
/// Integer-keyed input exposed to the script as `input[key]`.
|
||||
struct Input {
|
||||
Integer key{0};
|
||||
Integer value{0};
|
||||
|
||||
[[nodiscard]] bool operator==(const Input&) const = default;
|
||||
};
|
||||
|
||||
/// One host function call observed during a successful evaluation. The caller
|
||||
/// may commit these effects after `evaluate` returns; the evaluator itself
|
||||
/// never commits host state.
|
||||
struct HostInvocation {
|
||||
std::string host;
|
||||
std::vector<Integer> arguments;
|
||||
Integer result{0};
|
||||
|
||||
[[nodiscard]] bool operator==(const HostInvocation&) const = default;
|
||||
};
|
||||
|
||||
/// Owned result of a successful evaluation.
|
||||
struct Result {
|
||||
/// Integer values returned by the chunk, in order.
|
||||
std::vector<Integer> integers;
|
||||
/// Host calls made by the script, in order.
|
||||
std::vector<HostInvocation> host_calls;
|
||||
};
|
||||
|
||||
} // namespace Void::Script
|
||||
Reference in new issue
Block a user